FreeRDP
transport.c
1 
20 #include <freerdp/config.h>
21 
22 #include "settings.h"
23 
24 #include <winpr/assert.h>
25 
26 #include <winpr/crt.h>
27 #include <winpr/synch.h>
28 #include <winpr/print.h>
29 #include <winpr/stream.h>
30 #include <winpr/winsock.h>
31 #include <winpr/crypto.h>
32 
33 #include <freerdp/log.h>
34 #include <freerdp/error.h>
35 #include <freerdp/utils/ringbuffer.h>
36 
37 #include <openssl/bio.h>
38 #include <time.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 
42 #ifndef _WIN32
43 #include <netdb.h>
44 #include <sys/socket.h>
45 #endif /* _WIN32 */
46 
47 #ifdef FREERDP_HAVE_VALGRIND_MEMCHECK_H
48 #include <valgrind/memcheck.h>
49 #endif
50 
51 #include "tpkt.h"
52 #include "fastpath.h"
53 #include "transport.h"
54 #include "rdp.h"
55 #include "proxy.h"
56 #include "utils.h"
57 #include "state.h"
58 #include "childsession.h"
59 
60 #include "gateway/rdg.h"
61 #include "gateway/wst.h"
62 #include "gateway/arm.h"
63 
64 #define TAG FREERDP_TAG("core.transport")
65 
66 #define BUFFER_SIZE 16384
67 
68 struct rdp_transport
69 {
70  TRANSPORT_LAYER layer;
71  BIO* frontBio;
72  rdpRdg* rdg;
73  rdpTsg* tsg;
74  rdpWst* wst;
75  rdpTls* tls;
76  rdpContext* context;
77  rdpNla* nla;
78  void* ReceiveExtra;
79  wStream* ReceiveBuffer;
80  TransportRecv ReceiveCallback;
81  wStreamPool* ReceivePool;
82  HANDLE connectedEvent;
83  BOOL NlaMode;
84  BOOL RdstlsMode;
85  BOOL AadMode;
86  BOOL blocking;
87  BOOL GatewayEnabled;
88  CRITICAL_SECTION ReadLock;
89  CRITICAL_SECTION WriteLock;
90  ULONG written;
91  HANDLE rereadEvent;
92  BOOL haveMoreBytesToRead;
93  wLog* log;
94  rdpTransportIo io;
95  HANDLE ioEvent;
96  BOOL useIoEvent;
97  BOOL earlyUserAuth;
98 };
99 
100 static void transport_ssl_cb(const SSL* ssl, int where, int ret)
101 {
102  if (where & SSL_CB_ALERT)
103  {
104  rdpTransport* transport = (rdpTransport*)SSL_get_app_data(ssl);
105  WINPR_ASSERT(transport);
106 
107  switch (ret)
108  {
109  case (SSL3_AL_FATAL << 8) | SSL_AD_ACCESS_DENIED:
110  {
111  if (!freerdp_get_last_error(transport_get_context(transport)))
112  {
113  WLog_Print(transport->log, WLOG_ERROR, "ACCESS DENIED");
114  freerdp_set_last_error_log(transport_get_context(transport),
115  FREERDP_ERROR_AUTHENTICATION_FAILED);
116  }
117  }
118  break;
119 
120  case (SSL3_AL_FATAL << 8) | SSL_AD_INTERNAL_ERROR:
121  {
122  if (transport->NlaMode)
123  {
124  if (!freerdp_get_last_error(transport_get_context(transport)))
125  {
126  UINT32 kret = 0;
127  if (transport->nla)
128  kret = nla_get_error(transport->nla);
129  if (kret == 0)
130  kret = FREERDP_ERROR_CONNECT_PASSWORD_CERTAINLY_EXPIRED;
131  freerdp_set_last_error_log(transport_get_context(transport), kret);
132  }
133  }
134 
135  break;
136 
137  case (SSL3_AL_WARNING << 8) | SSL3_AD_CLOSE_NOTIFY:
138  break;
139 
140  default:
141  WLog_Print(transport->log, WLOG_WARN,
142  "Unhandled SSL error (where=%d, ret=%d [%s, %s])", where, ret,
143  SSL_alert_type_string_long(ret), SSL_alert_desc_string_long(ret));
144  break;
145  }
146  }
147  }
148 }
149 
150 wStream* transport_send_stream_init(rdpTransport* transport, size_t size)
151 {
152  WINPR_ASSERT(transport);
153 
154  wStream* s = StreamPool_Take(transport->ReceivePool, size);
155  if (!s)
156  return NULL;
157 
158  if (!Stream_EnsureCapacity(s, size))
159  {
160  Stream_Release(s);
161  return NULL;
162  }
163 
164  Stream_SetPosition(s, 0);
165  return s;
166 }
167 
168 BOOL transport_attach(rdpTransport* transport, int sockfd)
169 {
170  if (!transport)
171  return FALSE;
172  return IFCALLRESULT(FALSE, transport->io.TransportAttach, transport, sockfd);
173 }
174 
175 static BOOL transport_default_attach(rdpTransport* transport, int sockfd)
176 {
177  BIO* socketBio = NULL;
178  BIO* bufferedBio = NULL;
179  const rdpSettings* settings = NULL;
180  rdpContext* context = transport_get_context(transport);
181 
182  if (sockfd < 0)
183  {
184  WLog_WARN(TAG, "Running peer without socket (sockfd=%d)", sockfd);
185  return TRUE;
186  }
187 
188  settings = context->settings;
189  WINPR_ASSERT(settings);
190 
191  if (sockfd >= 0)
192  {
193  if (!freerdp_tcp_set_keep_alive_mode(settings, sockfd))
194  goto fail;
195 
196  socketBio = BIO_new(BIO_s_simple_socket());
197 
198  if (!socketBio)
199  goto fail;
200  }
201 
202  bufferedBio = BIO_new(BIO_s_buffered_socket());
203  if (!bufferedBio)
204  goto fail;
205 
206  if (socketBio)
207  {
208  bufferedBio = BIO_push(bufferedBio, socketBio);
209  if (!bufferedBio)
210  goto fail;
211 
212  /* Attach the socket only when this function can no longer fail.
213  * This ensures solid ownership:
214  * - if this function fails, the caller is responsible to clean up
215  * - if this function is successful, the caller MUST NOT close the socket any more.
216  */
217  BIO_set_fd(socketBio, sockfd, BIO_CLOSE);
218  }
219  EnterCriticalSection(&(transport->ReadLock));
220  EnterCriticalSection(&(transport->WriteLock));
221  transport->frontBio = bufferedBio;
222  LeaveCriticalSection(&(transport->WriteLock));
223  LeaveCriticalSection(&(transport->ReadLock));
224 
225  return TRUE;
226 fail:
227 
228  if (socketBio)
229  BIO_free_all(socketBio);
230  else
231  closesocket((SOCKET)sockfd);
232 
233  return FALSE;
234 }
235 
236 BOOL transport_connect_rdp(rdpTransport* transport)
237 {
238  if (!transport)
239  return FALSE;
240 
241  switch (utils_authenticate(transport_get_context(transport)->instance, AUTH_RDP, FALSE))
242  {
243  case AUTH_SKIP:
244  case AUTH_SUCCESS:
245  case AUTH_NO_CREDENTIALS:
246  return TRUE;
247  case AUTH_CANCELLED:
248  freerdp_set_last_error_if_not(transport_get_context(transport),
249  FREERDP_ERROR_CONNECT_CANCELLED);
250  return FALSE;
251  default:
252  return FALSE;
253  }
254 }
255 
256 BOOL transport_connect_tls(rdpTransport* transport)
257 {
258  const rdpSettings* settings = NULL;
259  rdpContext* context = transport_get_context(transport);
260 
261  settings = context->settings;
262  WINPR_ASSERT(settings);
263 
264  /* Only prompt for password if we use TLS (NLA also calls this function) */
265  if (settings->SelectedProtocol == PROTOCOL_SSL)
266  {
267  switch (utils_authenticate(context->instance, AUTH_TLS, FALSE))
268  {
269  case AUTH_SKIP:
270  case AUTH_SUCCESS:
271  case AUTH_NO_CREDENTIALS:
272  break;
273  case AUTH_CANCELLED:
274  freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_CANCELLED);
275  return FALSE;
276  default:
277  return FALSE;
278  }
279  }
280 
281  return IFCALLRESULT(FALSE, transport->io.TLSConnect, transport);
282 }
283 
284 static BOOL transport_default_connect_tls(rdpTransport* transport)
285 {
286  int tlsStatus = 0;
287  rdpTls* tls = NULL;
288  rdpContext* context = NULL;
289  rdpSettings* settings = NULL;
290 
291  WINPR_ASSERT(transport);
292 
293  context = transport_get_context(transport);
294  WINPR_ASSERT(context);
295 
296  settings = context->settings;
297  WINPR_ASSERT(settings);
298 
299  if (!(tls = freerdp_tls_new(context)))
300  return FALSE;
301 
302  transport->tls = tls;
303 
304  if (transport->GatewayEnabled)
305  transport->layer = TRANSPORT_LAYER_TSG_TLS;
306  else
307  transport->layer = TRANSPORT_LAYER_TLS;
308 
309  tls->hostname = settings->ServerHostname;
310  tls->serverName = settings->UserSpecifiedServerName;
311  tls->port = WINPR_ASSERTING_INT_CAST(int32_t, MIN(UINT16_MAX, settings->ServerPort));
312 
313  if (tls->port == 0)
314  tls->port = 3389;
315 
316  tls->isGatewayTransport = FALSE;
317  tlsStatus = freerdp_tls_connect(tls, transport->frontBio);
318 
319  if (tlsStatus < 1)
320  {
321  if (tlsStatus < 0)
322  {
323  freerdp_set_last_error_if_not(context, FREERDP_ERROR_TLS_CONNECT_FAILED);
324  }
325  else
326  {
327  freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_CANCELLED);
328  }
329 
330  return FALSE;
331  }
332 
333  transport->frontBio = tls->bio;
334 
335  /* See libfreerdp/crypto/tls.c transport_default_connect_tls
336  *
337  * we are wrapping a SSL object in the BIO and actually want to set
338  *
339  * SSL_set_info_callback there. So ensure our callback is of appropriate
340  * type for that instead of what the function prototype suggests.
341  */
342  typedef void (*ssl_cb_t)(const SSL* ssl, int type, int val);
343  ssl_cb_t fkt = transport_ssl_cb;
344 
345  BIO_info_cb* bfkt = WINPR_FUNC_PTR_CAST(fkt, BIO_info_cb*);
346  BIO_callback_ctrl(tls->bio, BIO_CTRL_SET_CALLBACK, bfkt);
347  SSL_set_app_data(tls->ssl, transport);
348 
349  if (!transport->frontBio)
350  {
351  WLog_Print(transport->log, WLOG_ERROR, "unable to prepend a filtering TLS bio");
352  return FALSE;
353  }
354 
355  return TRUE;
356 }
357 
358 BOOL transport_connect_nla(rdpTransport* transport, BOOL earlyUserAuth)
359 {
360  rdpContext* context = NULL;
361  rdpSettings* settings = NULL;
362  rdpRdp* rdp = NULL;
363  if (!transport)
364  return FALSE;
365 
366  context = transport_get_context(transport);
367  WINPR_ASSERT(context);
368 
369  settings = context->settings;
370  WINPR_ASSERT(settings);
371 
372  rdp = context->rdp;
373  WINPR_ASSERT(rdp);
374 
375  if (!transport_connect_tls(transport))
376  return FALSE;
377 
378  if (!settings->Authentication)
379  return TRUE;
380 
381  nla_free(rdp->nla);
382  rdp->nla = nla_new(context, transport);
383 
384  if (!rdp->nla)
385  return FALSE;
386 
387  nla_set_early_user_auth(rdp->nla, earlyUserAuth);
388 
389  transport_set_nla_mode(transport, TRUE);
390 
391  if (settings->AuthenticationServiceClass)
392  {
393  if (!nla_set_service_principal(rdp->nla, settings->AuthenticationServiceClass,
395  return FALSE;
396  }
397 
398  if (nla_client_begin(rdp->nla) < 0)
399  {
400  WLog_Print(transport->log, WLOG_ERROR, "NLA begin failed");
401 
402  freerdp_set_last_error_if_not(context, FREERDP_ERROR_AUTHENTICATION_FAILED);
403 
404  transport_set_nla_mode(transport, FALSE);
405  return FALSE;
406  }
407 
408  return rdp_client_transition_to_state(rdp, CONNECTION_STATE_NLA);
409 }
410 
411 BOOL transport_connect_rdstls(rdpTransport* transport)
412 {
413  BOOL rc = FALSE;
414  rdpRdstls* rdstls = NULL;
415  rdpContext* context = NULL;
416 
417  WINPR_ASSERT(transport);
418 
419  context = transport_get_context(transport);
420  WINPR_ASSERT(context);
421 
422  if (!transport_connect_tls(transport))
423  goto fail;
424 
425  rdstls = rdstls_new(context, transport);
426  if (!rdstls)
427  goto fail;
428 
429  transport_set_rdstls_mode(transport, TRUE);
430 
431  if (rdstls_authenticate(rdstls) < 0)
432  {
433  WLog_Print(transport->log, WLOG_ERROR, "RDSTLS authentication failed");
434  freerdp_set_last_error_if_not(context, FREERDP_ERROR_AUTHENTICATION_FAILED);
435  goto fail;
436  }
437 
438  transport_set_rdstls_mode(transport, FALSE);
439  rc = TRUE;
440 fail:
441  rdstls_free(rdstls);
442  return rc;
443 }
444 
445 BOOL transport_connect_aad(rdpTransport* transport)
446 {
447  rdpContext* context = NULL;
448  rdpSettings* settings = NULL;
449  rdpRdp* rdp = NULL;
450  if (!transport)
451  return FALSE;
452 
453  context = transport_get_context(transport);
454  WINPR_ASSERT(context);
455 
456  settings = context->settings;
457  WINPR_ASSERT(settings);
458 
459  rdp = context->rdp;
460  WINPR_ASSERT(rdp);
461 
462  if (!transport_connect_tls(transport))
463  return FALSE;
464 
465  if (!settings->Authentication)
466  return TRUE;
467 
468  if (!rdp->aad)
469  return FALSE;
470 
471  transport_set_aad_mode(transport, TRUE);
472 
473  if (aad_client_begin(rdp->aad) < 0)
474  {
475  WLog_Print(transport->log, WLOG_ERROR, "AAD begin failed");
476 
477  freerdp_set_last_error_if_not(context, FREERDP_ERROR_AUTHENTICATION_FAILED);
478 
479  transport_set_aad_mode(transport, FALSE);
480  return FALSE;
481  }
482 
483  return rdp_client_transition_to_state(rdp, CONNECTION_STATE_AAD);
484 }
485 
486 BOOL transport_connect(rdpTransport* transport, const char* hostname, UINT16 port, DWORD timeout)
487 {
488  BOOL status = FALSE;
489  rdpSettings* settings = NULL;
490  rdpContext* context = transport_get_context(transport);
491  BOOL rpcFallback = 0;
492 
493  WINPR_ASSERT(context);
494  WINPR_ASSERT(hostname);
495 
496  settings = context->settings;
497  WINPR_ASSERT(settings);
498 
499  rpcFallback = !settings->GatewayHttpTransport;
500 
501  if (transport->GatewayEnabled)
502  {
503  if (settings->GatewayUrl)
504  {
505  WINPR_ASSERT(!transport->wst);
506  transport->wst = wst_new(context);
507 
508  if (!transport->wst)
509  return FALSE;
510 
511  status = wst_connect(transport->wst, timeout);
512 
513  if (status)
514  {
515  transport->frontBio = wst_get_front_bio_and_take_ownership(transport->wst);
516  WINPR_ASSERT(transport->frontBio);
517  BIO_set_nonblock(transport->frontBio, 0);
518  transport->layer = TRANSPORT_LAYER_TSG;
519  status = TRUE;
520  }
521  else
522  {
523  wst_free(transport->wst);
524  transport->wst = NULL;
525  }
526  }
527  if (!status && settings->GatewayHttpTransport)
528  {
529  WINPR_ASSERT(!transport->rdg);
530  transport->rdg = rdg_new(context);
531 
532  if (!transport->rdg)
533  return FALSE;
534 
535  status = rdg_connect(transport->rdg, timeout, &rpcFallback);
536 
537  if (status)
538  {
539  transport->frontBio = rdg_get_front_bio_and_take_ownership(transport->rdg);
540  WINPR_ASSERT(transport->frontBio);
541  BIO_set_nonblock(transport->frontBio, 0);
542  transport->layer = TRANSPORT_LAYER_TSG;
543  status = TRUE;
544  }
545  else
546  {
547  rdg_free(transport->rdg);
548  transport->rdg = NULL;
549  }
550  }
551 
552  if (!status && settings->GatewayRpcTransport && rpcFallback)
553  {
554  WINPR_ASSERT(!transport->tsg);
555  transport->tsg = tsg_new(transport);
556 
557  if (!transport->tsg)
558  return FALSE;
559 
560  /* Reset error condition from RDG */
561  freerdp_set_last_error_log(context, FREERDP_ERROR_SUCCESS);
562  status = tsg_connect(transport->tsg, hostname, port, timeout);
563 
564  if (status)
565  {
566  transport->frontBio = tsg_get_bio(transport->tsg);
567  transport->layer = TRANSPORT_LAYER_TSG;
568  status = TRUE;
569  }
570  else
571  {
572  tsg_free(transport->tsg);
573  transport->tsg = NULL;
574  }
575  }
576  }
577  else
578  {
579  UINT16 peerPort = 0;
580  const char* proxyHostname = NULL;
581  const char* proxyUsername = NULL;
582  const char* proxyPassword = NULL;
583  BOOL isProxyConnection =
584  proxy_prepare(settings, &proxyHostname, &peerPort, &proxyUsername, &proxyPassword);
585 
586  rdpTransportLayer* layer = NULL;
587  if (isProxyConnection)
588  layer = transport_connect_layer(transport, proxyHostname, peerPort, timeout);
589  else
590  layer = transport_connect_layer(transport, hostname, port, timeout);
591 
592  if (!layer)
593  return FALSE;
594 
595  if (!transport_attach_layer(transport, layer))
596  {
597  transport_layer_free(layer);
598  return FALSE;
599  }
600 
601  if (isProxyConnection)
602  {
603  if (!proxy_connect(context, transport->frontBio, proxyUsername, proxyPassword, hostname,
604  port))
605  return FALSE;
606  }
607 
608  status = TRUE;
609  }
610 
611  return status;
612 }
613 
614 BOOL transport_connect_childsession(rdpTransport* transport)
615 {
616  WINPR_ASSERT(transport);
617 
618  transport->frontBio = createChildSessionBio();
619  if (!transport->frontBio)
620  return FALSE;
621 
622  transport->layer = TRANSPORT_LAYER_TSG;
623  return TRUE;
624 }
625 
626 BOOL transport_accept_rdp(rdpTransport* transport)
627 {
628  if (!transport)
629  return FALSE;
630  /* RDP encryption */
631  return TRUE;
632 }
633 
634 BOOL transport_accept_tls(rdpTransport* transport)
635 {
636  if (!transport)
637  return FALSE;
638  return IFCALLRESULT(FALSE, transport->io.TLSAccept, transport);
639 }
640 
641 static BOOL transport_default_accept_tls(rdpTransport* transport)
642 {
643  rdpContext* context = transport_get_context(transport);
644  rdpSettings* settings = NULL;
645 
646  WINPR_ASSERT(context);
647 
648  settings = context->settings;
649  WINPR_ASSERT(settings);
650 
651  if (!transport->tls)
652  transport->tls = freerdp_tls_new(context);
653 
654  transport->layer = TRANSPORT_LAYER_TLS;
655 
656  if (!freerdp_tls_accept(transport->tls, transport->frontBio, settings))
657  return FALSE;
658 
659  transport->frontBio = transport->tls->bio;
660  return TRUE;
661 }
662 
663 BOOL transport_accept_nla(rdpTransport* transport)
664 {
665  rdpContext* context = transport_get_context(transport);
666  rdpSettings* settings = NULL;
667 
668  WINPR_ASSERT(context);
669 
670  settings = context->settings;
671  WINPR_ASSERT(settings);
672 
673  if (!IFCALLRESULT(FALSE, transport->io.TLSAccept, transport))
674  return FALSE;
675 
676  /* Network Level Authentication */
677 
678  if (!settings->Authentication)
679  return TRUE;
680 
681  if (!transport->nla)
682  {
683  transport->nla = nla_new(context, transport);
684  transport_set_nla_mode(transport, TRUE);
685  }
686 
687  if (nla_authenticate(transport->nla) < 0)
688  {
689  WLog_Print(transport->log, WLOG_ERROR, "client authentication failure");
690  transport_set_nla_mode(transport, FALSE);
691  nla_free(transport->nla);
692  transport->nla = NULL;
693  freerdp_tls_set_alert_code(transport->tls, TLS_ALERT_LEVEL_FATAL,
694  TLS_ALERT_DESCRIPTION_ACCESS_DENIED);
695  freerdp_tls_send_alert(transport->tls);
696  return FALSE;
697  }
698 
699  /* don't free nla module yet, we need to copy the credentials from it first */
700  transport_set_nla_mode(transport, FALSE);
701  return TRUE;
702 }
703 
704 BOOL transport_accept_rdstls(rdpTransport* transport)
705 {
706  BOOL rc = FALSE;
707  rdpRdstls* rdstls = NULL;
708  rdpContext* context = NULL;
709 
710  WINPR_ASSERT(transport);
711 
712  context = transport_get_context(transport);
713  WINPR_ASSERT(context);
714 
715  if (!IFCALLRESULT(FALSE, transport->io.TLSAccept, transport))
716  goto fail;
717 
718  rdstls = rdstls_new(context, transport);
719  if (!rdstls)
720  goto fail;
721 
722  transport_set_rdstls_mode(transport, TRUE);
723 
724  if (rdstls_authenticate(rdstls) < 0)
725  {
726  WLog_Print(transport->log, WLOG_ERROR, "client authentication failure");
727  freerdp_tls_set_alert_code(transport->tls, TLS_ALERT_LEVEL_FATAL,
728  TLS_ALERT_DESCRIPTION_ACCESS_DENIED);
729  freerdp_tls_send_alert(transport->tls);
730  goto fail;
731  }
732 
733  transport_set_rdstls_mode(transport, FALSE);
734  rc = TRUE;
735 fail:
736  rdstls_free(rdstls);
737  return rc;
738 }
739 
740 #define WLog_ERR_BIO(transport, biofunc, bio) \
741  transport_bio_error_log(transport, biofunc, bio, __FILE__, __func__, __LINE__)
742 
743 static void transport_bio_error_log(rdpTransport* transport, LPCSTR biofunc, BIO* bio, LPCSTR file,
744  LPCSTR func, DWORD line)
745 {
746  unsigned long sslerr = 0;
747  int saveerrno = 0;
748  DWORD level = 0;
749 
750  WINPR_ASSERT(transport);
751 
752  saveerrno = errno;
753  level = WLOG_ERROR;
754 
755  if (level < WLog_GetLogLevel(transport->log))
756  return;
757 
758  if (ERR_peek_error() == 0)
759  {
760  char ebuffer[256] = { 0 };
761  const char* fmt = "%s returned a system error %d: %s";
762  if (saveerrno == 0)
763  fmt = "%s retries exceeded";
764  WLog_PrintMessage(transport->log, WLOG_MESSAGE_TEXT, level, line, file, func, fmt, biofunc,
765  saveerrno, winpr_strerror(saveerrno, ebuffer, sizeof(ebuffer)));
766  return;
767  }
768 
769  while ((sslerr = ERR_get_error()))
770  {
771  char buf[120] = { 0 };
772  const char* fmt = "%s returned an error: %s";
773 
774  ERR_error_string_n(sslerr, buf, 120);
775  WLog_PrintMessage(transport->log, WLOG_MESSAGE_TEXT, level, line, file, func, fmt, biofunc,
776  buf);
777  }
778 }
779 
780 static SSIZE_T transport_read_layer(rdpTransport* transport, BYTE* data, size_t bytes)
781 {
782  SSIZE_T read = 0;
783  rdpRdp* rdp = NULL;
784  rdpContext* context = NULL;
785 
786  WINPR_ASSERT(transport);
787 
788  context = transport_get_context(transport);
789  WINPR_ASSERT(context);
790 
791  rdp = context->rdp;
792  WINPR_ASSERT(rdp);
793 
794  if (!transport->frontBio || (bytes > SSIZE_MAX))
795  {
796  transport->layer = TRANSPORT_LAYER_CLOSED;
797  freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_TRANSPORT_FAILED);
798  return -1;
799  }
800 
801  while (read < (SSIZE_T)bytes)
802  {
803  const SSIZE_T tr = (SSIZE_T)bytes - read;
804  int r = (int)((tr > INT_MAX) ? INT_MAX : tr);
805  ERR_clear_error();
806  int status = BIO_read(transport->frontBio, data + read, r);
807 
808  if (freerdp_shall_disconnect_context(context))
809  return -1;
810 
811  if (status <= 0)
812  {
813  if (!transport->frontBio || !BIO_should_retry(transport->frontBio))
814  {
815  /* something unexpected happened, let's close */
816  if (!transport->frontBio)
817  {
818  WLog_Print(transport->log, WLOG_ERROR, "BIO_read: transport->frontBio null");
819  return -1;
820  }
821 
822  WLog_ERR_BIO(transport, "BIO_read", transport->frontBio);
823  transport->layer = TRANSPORT_LAYER_CLOSED;
824  freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_TRANSPORT_FAILED);
825  return -1;
826  }
827 
828  /* non blocking will survive a partial read */
829  if (!transport->blocking)
830  return read;
831 
832  /* blocking means that we can't continue until we have read the number of requested
833  * bytes */
834  if (BIO_wait_read(transport->frontBio, 100) < 0)
835  {
836  WLog_ERR_BIO(transport, "BIO_wait_read", transport->frontBio);
837  return -1;
838  }
839 
840  continue;
841  }
842 
843 #ifdef FREERDP_HAVE_VALGRIND_MEMCHECK_H
844  VALGRIND_MAKE_MEM_DEFINED(data + read, bytes - read);
845 #endif
846  read += status;
847  rdp->inBytes += WINPR_ASSERTING_INT_CAST(uint64_t, status);
848  }
849 
850  return read;
851 }
852 
867 static SSIZE_T transport_read_layer_bytes(rdpTransport* transport, wStream* s, size_t toRead)
868 {
869  SSIZE_T status = 0;
870  if (!transport)
871  return -1;
872 
873  if (toRead > SSIZE_MAX)
874  return 0;
875 
876  status = IFCALLRESULT(-1, transport->io.ReadBytes, transport, Stream_Pointer(s), toRead);
877 
878  if (status <= 0)
879  return status;
880 
881  Stream_Seek(s, (size_t)status);
882  return status == (SSIZE_T)toRead ? 1 : 0;
883 }
884 
897 int transport_read_pdu(rdpTransport* transport, wStream* s)
898 {
899  if (!transport)
900  return -1;
901  return IFCALLRESULT(-1, transport->io.ReadPdu, transport, s);
902 }
903 
904 static SSIZE_T parse_nla_mode_pdu(rdpTransport* transport, wStream* stream)
905 {
906  SSIZE_T pduLength = 0;
907  wStream sbuffer = { 0 };
908  wStream* s = Stream_StaticConstInit(&sbuffer, Stream_Buffer(stream), Stream_Length(stream));
909  /*
910  * In case NlaMode is set TSRequest package(s) are expected
911  * 0x30 = DER encoded data with these bits set:
912  * bit 6 P/C constructed
913  * bit 5 tag number - sequence
914  */
915  UINT8 typeEncoding = 0;
916  if (Stream_GetRemainingLength(s) < 1)
917  return 0;
918  Stream_Read_UINT8(s, typeEncoding);
919  if (typeEncoding == 0x30)
920  {
921  /* TSRequest (NLA) */
922  UINT8 lengthEncoding = 0;
923  if (Stream_GetRemainingLength(s) < 1)
924  return 0;
925  Stream_Read_UINT8(s, lengthEncoding);
926  if (lengthEncoding & 0x80)
927  {
928  if ((lengthEncoding & ~(0x80)) == 1)
929  {
930  UINT8 length = 0;
931  if (Stream_GetRemainingLength(s) < 1)
932  return 0;
933  Stream_Read_UINT8(s, length);
934  pduLength = length;
935  pduLength += 3;
936  }
937  else if ((lengthEncoding & ~(0x80)) == 2)
938  {
939  /* check for header bytes already read in previous calls */
940  UINT16 length = 0;
941  if (Stream_GetRemainingLength(s) < 2)
942  return 0;
943  Stream_Read_UINT16_BE(s, length);
944  pduLength = length;
945  pduLength += 4;
946  }
947  else
948  {
949  WLog_Print(transport->log, WLOG_ERROR, "Error reading TSRequest!");
950  return -1;
951  }
952  }
953  else
954  {
955  pduLength = lengthEncoding;
956  pduLength += 2;
957  }
958  }
959 
960  return pduLength;
961 }
962 
963 static SSIZE_T parse_default_mode_pdu(rdpTransport* transport, wStream* stream)
964 {
965  SSIZE_T pduLength = 0;
966  wStream sbuffer = { 0 };
967  wStream* s = Stream_StaticConstInit(&sbuffer, Stream_Buffer(stream), Stream_Length(stream));
968 
969  UINT8 version = 0;
970  if (Stream_GetRemainingLength(s) < 1)
971  return 0;
972  Stream_Read_UINT8(s, version);
973  if (version == 0x03)
974  {
975  /* TPKT header */
976  UINT16 length = 0;
977  if (Stream_GetRemainingLength(s) < 3)
978  return 0;
979  Stream_Seek(s, 1);
980  Stream_Read_UINT16_BE(s, length);
981  pduLength = length;
982 
983  /* min and max values according to ITU-T Rec. T.123 (01/2007) section 8 */
984  if ((pduLength < 7) || (pduLength > 0xFFFF))
985  {
986  WLog_Print(transport->log, WLOG_ERROR, "tpkt - invalid pduLength: %" PRIdz, pduLength);
987  return -1;
988  }
989  }
990  else
991  {
992  /* Fast-Path Header */
993  UINT8 length1 = 0;
994  if (Stream_GetRemainingLength(s) < 1)
995  return 0;
996  Stream_Read_UINT8(s, length1);
997  if (length1 & 0x80)
998  {
999  UINT8 length2 = 0;
1000  if (Stream_GetRemainingLength(s) < 1)
1001  return 0;
1002  Stream_Read_UINT8(s, length2);
1003  pduLength = ((length1 & 0x7F) << 8) | length2;
1004  }
1005  else
1006  pduLength = length1;
1007 
1008  /*
1009  * fast-path has 7 bits for length so the maximum size, including headers is 0x8000
1010  * The theoretical minimum fast-path PDU consists only of two header bytes plus one
1011  * byte for data (e.g. fast-path input synchronize pdu)
1012  */
1013  if (pduLength < 3 || pduLength > 0x8000)
1014  {
1015  WLog_Print(transport->log, WLOG_ERROR, "fast path - invalid pduLength: %" PRIdz,
1016  pduLength);
1017  return -1;
1018  }
1019  }
1020 
1021  return pduLength;
1022 }
1023 
1024 SSIZE_T transport_parse_pdu(rdpTransport* transport, wStream* s, BOOL* incomplete)
1025 {
1026  SSIZE_T pduLength = 0;
1027 
1028  if (!transport)
1029  return -1;
1030 
1031  if (!s)
1032  return -1;
1033 
1034  if (incomplete)
1035  *incomplete = TRUE;
1036 
1037  Stream_SealLength(s);
1038  if (transport->NlaMode)
1039  pduLength = parse_nla_mode_pdu(transport, s);
1040  else if (transport->RdstlsMode)
1041  pduLength = rdstls_parse_pdu(transport->log, s);
1042  else
1043  pduLength = parse_default_mode_pdu(transport, s);
1044 
1045  if (pduLength <= 0)
1046  return pduLength;
1047 
1048  const size_t len = Stream_Length(s);
1049  if (len > WINPR_ASSERTING_INT_CAST(size_t, pduLength))
1050  return -1;
1051 
1052  if (incomplete)
1053  *incomplete = len < WINPR_ASSERTING_INT_CAST(size_t, pduLength);
1054 
1055  return pduLength;
1056 }
1057 
1058 static int transport_default_read_pdu(rdpTransport* transport, wStream* s)
1059 {
1060  BOOL incomplete = 0;
1061  SSIZE_T status = 0;
1062  size_t pduLength = 0;
1063  size_t position = 0;
1064 
1065  WINPR_ASSERT(transport);
1066  WINPR_ASSERT(s);
1067 
1068  /* RDS AAD Auth PDUs have no length indicator. We need to determine the end of the PDU by
1069  * reading in one byte at a time until we encounter the terminating null byte */
1070  if (transport->AadMode)
1071  {
1072  BYTE c = '\0';
1073  do
1074  {
1075  const SSIZE_T rc = transport_read_layer(transport, &c, 1);
1076  if (rc != 1)
1077  return (rc == 0) ? 0 : -1;
1078  if (!Stream_EnsureRemainingCapacity(s, 1))
1079  return -1;
1080  Stream_Write_UINT8(s, c);
1081  } while (c != '\0');
1082  }
1083  else if (transport->earlyUserAuth)
1084  {
1085  if (!Stream_EnsureCapacity(s, 4))
1086  return -1;
1087  const SSIZE_T rc = transport_read_layer_bytes(transport, s, 4);
1088  if (rc != 1)
1089  return (rc == 0) ? 0 : -1;
1090  }
1091  else
1092  {
1093  /* Read in pdu length */
1094  status = transport_parse_pdu(transport, s, &incomplete);
1095  while ((status == 0) && incomplete)
1096  {
1097  if (!Stream_EnsureRemainingCapacity(s, 1))
1098  return -1;
1099  SSIZE_T rc = transport_read_layer_bytes(transport, s, 1);
1100  if (rc > INT32_MAX)
1101  return INT32_MAX;
1102  if (rc != 1)
1103  return (int)rc;
1104  status = transport_parse_pdu(transport, s, &incomplete);
1105  }
1106 
1107  if (status < 0)
1108  return -1;
1109 
1110  pduLength = (size_t)status;
1111 
1112  /* Read in rest of the PDU */
1113  if (!Stream_EnsureCapacity(s, pduLength))
1114  return -1;
1115 
1116  position = Stream_GetPosition(s);
1117  if (position > pduLength)
1118  return -1;
1119  else if (position < pduLength)
1120  {
1121  status = transport_read_layer_bytes(transport, s, pduLength - position);
1122  if (status != 1)
1123  {
1124  if ((status < INT32_MIN) || (status > INT32_MAX))
1125  return -1;
1126  return (int)status;
1127  }
1128  }
1129 
1130  if (Stream_GetPosition(s) >= pduLength)
1131  WLog_Packet(transport->log, WLOG_TRACE, Stream_Buffer(s), pduLength,
1132  WLOG_PACKET_INBOUND);
1133  }
1134 
1135  Stream_SealLength(s);
1136  Stream_SetPosition(s, 0);
1137  const size_t len = Stream_Length(s);
1138  if (len > INT32_MAX)
1139  return -1;
1140  return (int)len;
1141 }
1142 
1143 int transport_write(rdpTransport* transport, wStream* s)
1144 {
1145  if (!transport)
1146  return -1;
1147 
1148  return IFCALLRESULT(-1, transport->io.WritePdu, transport, s);
1149 }
1150 
1151 static int transport_default_write(rdpTransport* transport, wStream* s)
1152 {
1153  int status = -1;
1154  rdpContext* context = transport_get_context(transport);
1155 
1156  WINPR_ASSERT(transport);
1157  WINPR_ASSERT(context);
1158 
1159  if (!s)
1160  return -1;
1161 
1162  Stream_AddRef(s);
1163 
1164  rdpRdp* rdp = context->rdp;
1165  if (!rdp)
1166  goto fail;
1167 
1168  EnterCriticalSection(&(transport->WriteLock));
1169  if (!transport->frontBio)
1170  goto out_cleanup;
1171 
1172  size_t length = Stream_GetPosition(s);
1173  size_t writtenlength = length;
1174  Stream_SetPosition(s, 0);
1175 
1176  if (length > 0)
1177  {
1178  rdp->outBytes += length;
1179  WLog_Packet(transport->log, WLOG_TRACE, Stream_Buffer(s), length, WLOG_PACKET_OUTBOUND);
1180  }
1181 
1182  while (length > 0)
1183  {
1184  ERR_clear_error();
1185  const int towrite = (length > INT32_MAX) ? INT32_MAX : (int)length;
1186  status = BIO_write(transport->frontBio, Stream_ConstPointer(s), towrite);
1187 
1188  if (status <= 0)
1189  {
1190  /* the buffered BIO that is at the end of the chain always says OK for writing,
1191  * so a retry means that for any reason we need to read. The most probable
1192  * is a SSL or TSG BIO in the chain.
1193  */
1194  if (!BIO_should_retry(transport->frontBio))
1195  {
1196  WLog_ERR_BIO(transport, "BIO_should_retry", transport->frontBio);
1197  goto out_cleanup;
1198  }
1199 
1200  /* non-blocking can live with blocked IOs */
1201  if (!transport->blocking)
1202  {
1203  WLog_ERR_BIO(transport, "BIO_write", transport->frontBio);
1204  goto out_cleanup;
1205  }
1206 
1207  if (BIO_wait_write(transport->frontBio, 100) < 0)
1208  {
1209  WLog_ERR_BIO(transport, "BIO_wait_write", transport->frontBio);
1210  status = -1;
1211  goto out_cleanup;
1212  }
1213 
1214  continue;
1215  }
1216 
1217  WINPR_ASSERT(context->settings);
1218  if (transport->blocking || context->settings->WaitForOutputBufferFlush)
1219  {
1220  while (BIO_write_blocked(transport->frontBio))
1221  {
1222  if (BIO_wait_write(transport->frontBio, 100) < 0)
1223  {
1224  WLog_Print(transport->log, WLOG_ERROR, "error when selecting for write");
1225  status = -1;
1226  goto out_cleanup;
1227  }
1228 
1229  if (BIO_flush(transport->frontBio) < 1)
1230  {
1231  WLog_Print(transport->log, WLOG_ERROR, "error when flushing outputBuffer");
1232  status = -1;
1233  goto out_cleanup;
1234  }
1235  }
1236  }
1237 
1238  length -= (size_t)status;
1239  Stream_Seek(s, (size_t)status);
1240  }
1241 
1242  transport->written += writtenlength;
1243 out_cleanup:
1244 
1245  if (status < 0)
1246  {
1247  /* A write error indicates that the peer has dropped the connection */
1248  transport->layer = TRANSPORT_LAYER_CLOSED;
1249  freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_TRANSPORT_FAILED);
1250  }
1251 
1252  LeaveCriticalSection(&(transport->WriteLock));
1253 fail:
1254  Stream_Release(s);
1255  return status;
1256 }
1257 
1258 BOOL transport_get_public_key(rdpTransport* transport, const BYTE** data, DWORD* length)
1259 {
1260  return IFCALLRESULT(FALSE, transport->io.GetPublicKey, transport, data, length);
1261 }
1262 
1263 static BOOL transport_default_get_public_key(rdpTransport* transport, const BYTE** data,
1264  DWORD* length)
1265 {
1266  rdpTls* tls = transport_get_tls(transport);
1267  if (!tls)
1268  return FALSE;
1269 
1270  *data = tls->PublicKey;
1271  *length = tls->PublicKeyLength;
1272 
1273  return TRUE;
1274 }
1275 
1276 DWORD transport_get_event_handles(rdpTransport* transport, HANDLE* events, DWORD count)
1277 {
1278  DWORD nCount = 0; /* always the reread Event */
1279 
1280  WINPR_ASSERT(transport);
1281  WINPR_ASSERT(events);
1282  WINPR_ASSERT(count > 0);
1283 
1284  if (events)
1285  {
1286  if (count < 1)
1287  {
1288  WLog_Print(transport->log, WLOG_ERROR, "provided handles array is too small");
1289  return 0;
1290  }
1291 
1292  events[nCount++] = transport->rereadEvent;
1293 
1294  if (transport->useIoEvent)
1295  {
1296  if (count < 2)
1297  return 0;
1298  events[nCount++] = transport->ioEvent;
1299  }
1300  }
1301 
1302  if (!transport->GatewayEnabled)
1303  {
1304  if (events)
1305  {
1306  if (nCount >= count)
1307  {
1308  WLog_Print(transport->log, WLOG_ERROR,
1309  "provided handles array is too small (count=%" PRIu32 " nCount=%" PRIu32
1310  ")",
1311  count, nCount);
1312  return 0;
1313  }
1314 
1315  if (transport->frontBio)
1316  {
1317  if (BIO_get_event(transport->frontBio, &events[nCount]) != 1)
1318  {
1319  WLog_Print(transport->log, WLOG_ERROR, "error getting the frontBio handle");
1320  return 0;
1321  }
1322  nCount++;
1323  }
1324  }
1325  }
1326  else
1327  {
1328  if (transport->rdg)
1329  {
1330  const DWORD tmp =
1331  rdg_get_event_handles(transport->rdg, &events[nCount], count - nCount);
1332 
1333  if (tmp == 0)
1334  return 0;
1335 
1336  nCount += tmp;
1337  }
1338  else if (transport->tsg)
1339  {
1340  const DWORD tmp =
1341  tsg_get_event_handles(transport->tsg, &events[nCount], count - nCount);
1342 
1343  if (tmp == 0)
1344  return 0;
1345 
1346  nCount += tmp;
1347  }
1348  else if (transport->wst)
1349  {
1350  const DWORD tmp =
1351  wst_get_event_handles(transport->wst, &events[nCount], count - nCount);
1352 
1353  if (tmp == 0)
1354  return 0;
1355 
1356  nCount += tmp;
1357  }
1358  }
1359 
1360  return nCount;
1361 }
1362 
1363 #if defined(WITH_FREERDP_DEPRECATED)
1364 void transport_get_fds(rdpTransport* transport, void** rfds, int* rcount)
1365 {
1366  DWORD nCount = 0;
1367  HANDLE events[MAXIMUM_WAIT_OBJECTS] = { 0 };
1368 
1369  WINPR_ASSERT(transport);
1370  WINPR_ASSERT(rfds);
1371  WINPR_ASSERT(rcount);
1372 
1373  nCount = transport_get_event_handles(transport, events, ARRAYSIZE(events));
1374  *rcount = nCount + 1;
1375 
1376  for (DWORD index = 0; index < nCount; index++)
1377  {
1378  rfds[index] = GetEventWaitObject(events[index]);
1379  }
1380 
1381  rfds[nCount] = GetEventWaitObject(transport->rereadEvent);
1382 }
1383 #endif
1384 
1385 BOOL transport_is_write_blocked(rdpTransport* transport)
1386 {
1387  WINPR_ASSERT(transport);
1388  WINPR_ASSERT(transport->frontBio);
1389  return BIO_write_blocked(transport->frontBio) != 0;
1390 }
1391 
1392 int transport_drain_output_buffer(rdpTransport* transport)
1393 {
1394  BOOL status = FALSE;
1395 
1396  WINPR_ASSERT(transport);
1397  WINPR_ASSERT(transport->frontBio);
1398  if (BIO_write_blocked(transport->frontBio))
1399  {
1400  if (BIO_flush(transport->frontBio) < 1)
1401  return -1;
1402 
1403  const long rc = BIO_write_blocked(transport->frontBio);
1404  status = (rc != 0);
1405  }
1406 
1407  return status;
1408 }
1409 
1410 int transport_check_fds(rdpTransport* transport)
1411 {
1412  int status = 0;
1413  state_run_t recv_status = STATE_RUN_FAILED;
1414  wStream* received = NULL;
1415  rdpContext* context = transport_get_context(transport);
1416 
1417  WINPR_ASSERT(context);
1418 
1419  if (transport->layer == TRANSPORT_LAYER_CLOSED)
1420  {
1421  WLog_Print(transport->log, WLOG_DEBUG, "transport_check_fds: transport layer closed");
1422  freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_TRANSPORT_FAILED);
1423  return -1;
1424  }
1425 
1435  if ((status = transport_read_pdu(transport, transport->ReceiveBuffer)) <= 0)
1436  {
1437  if (status < 0)
1438  WLog_Print(transport->log, WLOG_DEBUG, "transport_check_fds: transport_read_pdu() - %i",
1439  status);
1440  if (transport->haveMoreBytesToRead)
1441  {
1442  transport->haveMoreBytesToRead = FALSE;
1443  (void)ResetEvent(transport->rereadEvent);
1444  }
1445  return status;
1446  }
1447 
1448  received = transport->ReceiveBuffer;
1449 
1450  if (!(transport->ReceiveBuffer = StreamPool_Take(transport->ReceivePool, 0)))
1451  return -1;
1452 
1459  WINPR_ASSERT(transport->ReceiveCallback);
1460  recv_status = transport->ReceiveCallback(transport, received, transport->ReceiveExtra);
1461  Stream_Release(received);
1462 
1463  if (state_run_failed(recv_status))
1464  {
1465  char buffer[64] = { 0 };
1466  WLog_Print(transport->log, WLOG_ERROR,
1467  "transport_check_fds: transport->ReceiveCallback() - %s",
1468  state_run_result_string(recv_status, buffer, ARRAYSIZE(buffer)));
1469  return -1;
1470  }
1471 
1472  /* Run this again to be sure we consumed all input data.
1473  * This will be repeated until a (not fully) received packet is in buffer
1474  */
1475  if (!transport->haveMoreBytesToRead)
1476  {
1477  transport->haveMoreBytesToRead = TRUE;
1478  (void)SetEvent(transport->rereadEvent);
1479  }
1480  return recv_status;
1481 }
1482 
1483 BOOL transport_set_blocking_mode(rdpTransport* transport, BOOL blocking)
1484 {
1485  WINPR_ASSERT(transport);
1486 
1487  return IFCALLRESULT(FALSE, transport->io.SetBlockingMode, transport, blocking);
1488 }
1489 
1490 static BOOL transport_default_set_blocking_mode(rdpTransport* transport, BOOL blocking)
1491 {
1492  WINPR_ASSERT(transport);
1493 
1494  transport->blocking = blocking;
1495 
1496  if (transport->frontBio)
1497  {
1498  if (!BIO_set_nonblock(transport->frontBio, blocking ? FALSE : TRUE))
1499  return FALSE;
1500  }
1501 
1502  return TRUE;
1503 }
1504 
1505 rdpTransportLayer* transport_connect_layer(rdpTransport* transport, const char* hostname, int port,
1506  DWORD timeout)
1507 {
1508  WINPR_ASSERT(transport);
1509 
1510  return IFCALLRESULT(NULL, transport->io.ConnectLayer, transport, hostname, port, timeout);
1511 }
1512 
1513 static rdpTransportLayer* transport_default_connect_layer(rdpTransport* transport,
1514  const char* hostname, int port,
1515  DWORD timeout)
1516 {
1517  rdpContext* context = transport_get_context(transport);
1518  WINPR_ASSERT(context);
1519 
1520  return freerdp_tcp_connect_layer(context, hostname, port, timeout);
1521 }
1522 
1523 BOOL transport_attach_layer(rdpTransport* transport, rdpTransportLayer* layer)
1524 {
1525  WINPR_ASSERT(transport);
1526  WINPR_ASSERT(layer);
1527 
1528  return IFCALLRESULT(FALSE, transport->io.AttachLayer, transport, layer);
1529 }
1530 
1531 static BOOL transport_default_attach_layer(rdpTransport* transport, rdpTransportLayer* layer)
1532 {
1533  BIO* layerBio = BIO_new(BIO_s_transport_layer());
1534  if (!layerBio)
1535  goto fail;
1536 
1537  BIO* bufferedBio = BIO_new(BIO_s_buffered_socket());
1538  if (!bufferedBio)
1539  goto fail;
1540 
1541  bufferedBio = BIO_push(bufferedBio, layerBio);
1542  if (!bufferedBio)
1543  goto fail;
1544 
1545  /* BIO takes over the layer reference at this point. */
1546  BIO_set_data(layerBio, layer);
1547 
1548  transport->frontBio = bufferedBio;
1549 
1550  return TRUE;
1551 
1552 fail:
1553  if (layerBio)
1554  BIO_free_all(layerBio);
1555 
1556  return FALSE;
1557 }
1558 
1559 void transport_set_gateway_enabled(rdpTransport* transport, BOOL GatewayEnabled)
1560 {
1561  WINPR_ASSERT(transport);
1562  transport->GatewayEnabled = GatewayEnabled;
1563 }
1564 
1565 void transport_set_nla_mode(rdpTransport* transport, BOOL NlaMode)
1566 {
1567  WINPR_ASSERT(transport);
1568  transport->NlaMode = NlaMode;
1569 }
1570 
1571 void transport_set_rdstls_mode(rdpTransport* transport, BOOL RdstlsMode)
1572 {
1573  WINPR_ASSERT(transport);
1574  transport->RdstlsMode = RdstlsMode;
1575 }
1576 
1577 void transport_set_aad_mode(rdpTransport* transport, BOOL AadMode)
1578 {
1579  WINPR_ASSERT(transport);
1580  transport->AadMode = AadMode;
1581 }
1582 
1583 BOOL transport_disconnect(rdpTransport* transport)
1584 {
1585  if (!transport)
1586  return FALSE;
1587  return IFCALLRESULT(FALSE, transport->io.TransportDisconnect, transport);
1588 }
1589 
1590 static BOOL transport_default_disconnect(rdpTransport* transport)
1591 {
1592  BOOL status = TRUE;
1593 
1594  if (!transport)
1595  return FALSE;
1596 
1597  EnterCriticalSection(&(transport->ReadLock));
1598  EnterCriticalSection(&(transport->WriteLock));
1599  if (transport->tls)
1600  {
1601  freerdp_tls_free(transport->tls);
1602  transport->tls = NULL;
1603  }
1604  else
1605  {
1606  if (transport->frontBio)
1607  BIO_free_all(transport->frontBio);
1608  }
1609 
1610  if (transport->tsg)
1611  {
1612  tsg_free(transport->tsg);
1613  transport->tsg = NULL;
1614  }
1615 
1616  if (transport->rdg)
1617  {
1618  rdg_free(transport->rdg);
1619  transport->rdg = NULL;
1620  }
1621 
1622  if (transport->wst)
1623  {
1624  wst_free(transport->wst);
1625  transport->wst = NULL;
1626  }
1627 
1628  transport->frontBio = NULL;
1629  transport->layer = TRANSPORT_LAYER_TCP;
1630  transport->earlyUserAuth = FALSE;
1631  LeaveCriticalSection(&(transport->WriteLock));
1632  LeaveCriticalSection(&(transport->ReadLock));
1633  return status;
1634 }
1635 
1636 rdpTransport* transport_new(rdpContext* context)
1637 {
1638  rdpTransport* transport = (rdpTransport*)calloc(1, sizeof(rdpTransport));
1639 
1640  WINPR_ASSERT(context);
1641  if (!transport)
1642  return NULL;
1643 
1644  transport->log = WLog_Get(TAG);
1645 
1646  if (!transport->log)
1647  goto fail;
1648 
1649  // transport->io.DataHandler = transport_data_handler;
1650  transport->io.TCPConnect = freerdp_tcp_default_connect;
1651  transport->io.TLSConnect = transport_default_connect_tls;
1652  transport->io.TLSAccept = transport_default_accept_tls;
1653  transport->io.TransportAttach = transport_default_attach;
1654  transport->io.TransportDisconnect = transport_default_disconnect;
1655  transport->io.ReadPdu = transport_default_read_pdu;
1656  transport->io.WritePdu = transport_default_write;
1657  transport->io.ReadBytes = transport_read_layer;
1658  transport->io.GetPublicKey = transport_default_get_public_key;
1659  transport->io.SetBlockingMode = transport_default_set_blocking_mode;
1660  transport->io.ConnectLayer = transport_default_connect_layer;
1661  transport->io.AttachLayer = transport_default_attach_layer;
1662 
1663  transport->context = context;
1664  transport->ReceivePool = StreamPool_New(TRUE, BUFFER_SIZE);
1665 
1666  if (!transport->ReceivePool)
1667  goto fail;
1668 
1669  /* receive buffer for non-blocking read. */
1670  transport->ReceiveBuffer = StreamPool_Take(transport->ReceivePool, 0);
1671 
1672  if (!transport->ReceiveBuffer)
1673  goto fail;
1674 
1675  transport->connectedEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1676 
1677  if (!transport->connectedEvent || transport->connectedEvent == INVALID_HANDLE_VALUE)
1678  goto fail;
1679 
1680  transport->rereadEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1681 
1682  if (!transport->rereadEvent || transport->rereadEvent == INVALID_HANDLE_VALUE)
1683  goto fail;
1684 
1685  transport->ioEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
1686 
1687  if (!transport->ioEvent || transport->ioEvent == INVALID_HANDLE_VALUE)
1688  goto fail;
1689 
1690  transport->haveMoreBytesToRead = FALSE;
1691  transport->blocking = TRUE;
1692  transport->GatewayEnabled = FALSE;
1693  transport->layer = TRANSPORT_LAYER_TCP;
1694 
1695  if (!InitializeCriticalSectionAndSpinCount(&(transport->ReadLock), 4000))
1696  goto fail;
1697 
1698  if (!InitializeCriticalSectionAndSpinCount(&(transport->WriteLock), 4000))
1699  goto fail;
1700 
1701  return transport;
1702 fail:
1703  WINPR_PRAGMA_DIAG_PUSH
1704  WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1705  transport_free(transport);
1706  WINPR_PRAGMA_DIAG_POP
1707  return NULL;
1708 }
1709 
1710 void transport_free(rdpTransport* transport)
1711 {
1712  if (!transport)
1713  return;
1714 
1715  transport_disconnect(transport);
1716 
1717  EnterCriticalSection(&(transport->ReadLock));
1718  if (transport->ReceiveBuffer)
1719  Stream_Release(transport->ReceiveBuffer);
1720  LeaveCriticalSection(&(transport->ReadLock));
1721 
1722  (void)StreamPool_WaitForReturn(transport->ReceivePool, INFINITE);
1723 
1724  EnterCriticalSection(&(transport->ReadLock));
1725  EnterCriticalSection(&(transport->WriteLock));
1726 
1727  nla_free(transport->nla);
1728  StreamPool_Free(transport->ReceivePool);
1729  (void)CloseHandle(transport->connectedEvent);
1730  (void)CloseHandle(transport->rereadEvent);
1731  (void)CloseHandle(transport->ioEvent);
1732 
1733  LeaveCriticalSection(&(transport->ReadLock));
1734  DeleteCriticalSection(&(transport->ReadLock));
1735 
1736  LeaveCriticalSection(&(transport->WriteLock));
1737  DeleteCriticalSection(&(transport->WriteLock));
1738  free(transport);
1739 }
1740 
1741 BOOL transport_set_io_callbacks(rdpTransport* transport, const rdpTransportIo* io_callbacks)
1742 {
1743  if (!transport || !io_callbacks)
1744  return FALSE;
1745 
1746  transport->io = *io_callbacks;
1747  return TRUE;
1748 }
1749 
1750 const rdpTransportIo* transport_get_io_callbacks(rdpTransport* transport)
1751 {
1752  if (!transport)
1753  return NULL;
1754  return &transport->io;
1755 }
1756 
1757 rdpContext* transport_get_context(rdpTransport* transport)
1758 {
1759  WINPR_ASSERT(transport);
1760  return transport->context;
1761 }
1762 
1763 rdpTransport* freerdp_get_transport(rdpContext* context)
1764 {
1765  WINPR_ASSERT(context);
1766  WINPR_ASSERT(context->rdp);
1767  return context->rdp->transport;
1768 }
1769 
1770 BOOL transport_set_nla(rdpTransport* transport, rdpNla* nla)
1771 {
1772  WINPR_ASSERT(transport);
1773  nla_free(transport->nla);
1774  transport->nla = nla;
1775  return TRUE;
1776 }
1777 
1778 rdpNla* transport_get_nla(rdpTransport* transport)
1779 {
1780  WINPR_ASSERT(transport);
1781  return transport->nla;
1782 }
1783 
1784 BOOL transport_set_tls(rdpTransport* transport, rdpTls* tls)
1785 {
1786  WINPR_ASSERT(transport);
1787  freerdp_tls_free(transport->tls);
1788  transport->tls = tls;
1789  return TRUE;
1790 }
1791 
1792 rdpTls* transport_get_tls(rdpTransport* transport)
1793 {
1794  WINPR_ASSERT(transport);
1795  return transport->tls;
1796 }
1797 
1798 BOOL transport_set_tsg(rdpTransport* transport, rdpTsg* tsg)
1799 {
1800  WINPR_ASSERT(transport);
1801  tsg_free(transport->tsg);
1802  transport->tsg = tsg;
1803  return TRUE;
1804 }
1805 
1806 rdpTsg* transport_get_tsg(rdpTransport* transport)
1807 {
1808  WINPR_ASSERT(transport);
1809  return transport->tsg;
1810 }
1811 
1812 wStream* transport_take_from_pool(rdpTransport* transport, size_t size)
1813 {
1814  WINPR_ASSERT(transport);
1815  if (!transport->frontBio)
1816  return NULL;
1817  return StreamPool_Take(transport->ReceivePool, size);
1818 }
1819 
1820 ULONG transport_get_bytes_sent(rdpTransport* transport, BOOL resetCount)
1821 {
1822  ULONG rc = 0;
1823  WINPR_ASSERT(transport);
1824  rc = transport->written;
1825  if (resetCount)
1826  transport->written = 0;
1827  return rc;
1828 }
1829 
1830 TRANSPORT_LAYER transport_get_layer(rdpTransport* transport)
1831 {
1832  WINPR_ASSERT(transport);
1833  return transport->layer;
1834 }
1835 
1836 BOOL transport_set_layer(rdpTransport* transport, TRANSPORT_LAYER layer)
1837 {
1838  WINPR_ASSERT(transport);
1839  transport->layer = layer;
1840  return TRUE;
1841 }
1842 
1843 BOOL transport_set_connected_event(rdpTransport* transport)
1844 {
1845  WINPR_ASSERT(transport);
1846  return SetEvent(transport->connectedEvent);
1847 }
1848 
1849 BOOL transport_set_recv_callbacks(rdpTransport* transport, TransportRecv recv, void* extra)
1850 {
1851  WINPR_ASSERT(transport);
1852  transport->ReceiveCallback = recv;
1853  transport->ReceiveExtra = extra;
1854  return TRUE;
1855 }
1856 
1857 BOOL transport_get_blocking(rdpTransport* transport)
1858 {
1859  WINPR_ASSERT(transport);
1860  return transport->blocking;
1861 }
1862 
1863 BOOL transport_set_blocking(rdpTransport* transport, BOOL blocking)
1864 {
1865  WINPR_ASSERT(transport);
1866  transport->blocking = blocking;
1867  return TRUE;
1868 }
1869 
1870 BOOL transport_have_more_bytes_to_read(rdpTransport* transport)
1871 {
1872  WINPR_ASSERT(transport);
1873  return transport->haveMoreBytesToRead;
1874 }
1875 
1876 int transport_tcp_connect(rdpTransport* transport, const char* hostname, int port, DWORD timeout)
1877 {
1878  rdpContext* context = transport_get_context(transport);
1879  WINPR_ASSERT(context);
1880  WINPR_ASSERT(context->settings);
1881  return IFCALLRESULT(-1, transport->io.TCPConnect, context, context->settings, hostname, port,
1882  timeout);
1883 }
1884 
1885 HANDLE transport_get_front_bio(rdpTransport* transport)
1886 {
1887  HANDLE hEvent = NULL;
1888  WINPR_ASSERT(transport);
1889  WINPR_ASSERT(transport->frontBio);
1890 
1891  BIO_get_event(transport->frontBio, &hEvent);
1892  return hEvent;
1893 }
1894 
1895 BOOL transport_io_callback_set_event(rdpTransport* transport, BOOL set)
1896 {
1897  WINPR_ASSERT(transport);
1898  transport->useIoEvent = TRUE;
1899  if (!set)
1900  return ResetEvent(transport->ioEvent);
1901  return SetEvent(transport->ioEvent);
1902 }
1903 
1904 void transport_set_early_user_auth_mode(rdpTransport* transport, BOOL EUAMode)
1905 {
1906  WINPR_ASSERT(transport);
1907  transport->earlyUserAuth = EUAMode;
1908  WLog_Print(transport->log, WLOG_DEBUG, "Early User Auth Mode: %s", EUAMode ? "on" : "off");
1909 }
1910 
1911 rdpTransportLayer* transport_layer_new(rdpTransport* transport, size_t contextSize)
1912 {
1913  rdpTransportLayer* layer = (rdpTransportLayer*)calloc(1, sizeof(rdpTransportLayer));
1914  if (!layer)
1915  return NULL;
1916 
1917  if (contextSize)
1918  {
1919  layer->userContext = calloc(1, contextSize);
1920  if (!layer->userContext)
1921  {
1922  free(layer);
1923  return NULL;
1924  }
1925  }
1926 
1927  return layer;
1928 }
1929 
1930 void transport_layer_free(rdpTransportLayer* layer)
1931 {
1932  if (!layer)
1933  return;
1934 
1935  IFCALL(layer->Close, layer->userContext);
1936  free(layer->userContext);
1937  free(layer);
1938 }
1939 
1940 static int transport_layer_bio_write(BIO* bio, const char* buf, int size)
1941 {
1942  if (!buf || !size)
1943  return 0;
1944  if (size < 0)
1945  return -1;
1946 
1947  WINPR_ASSERT(bio);
1948 
1949  rdpTransportLayer* layer = (rdpTransportLayer*)BIO_get_data(bio);
1950  if (!layer)
1951  return -1;
1952 
1953  BIO_clear_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
1954 
1955  int status = IFCALLRESULT(-1, layer->Write, layer->userContext, buf, size);
1956 
1957  if (status >= 0 && status < size)
1958  BIO_set_flags(bio, (BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY));
1959 
1960  return status;
1961 }
1962 
1963 static int transport_layer_bio_read(BIO* bio, char* buf, int size)
1964 {
1965  if (!buf || !size)
1966  return 0;
1967  if (size < 0)
1968  return -1;
1969 
1970  WINPR_ASSERT(bio);
1971 
1972  rdpTransportLayer* layer = (rdpTransportLayer*)BIO_get_data(bio);
1973  if (!layer)
1974  return -1;
1975 
1976  BIO_clear_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
1977 
1978  int status = IFCALLRESULT(-1, layer->Read, layer->userContext, buf, size);
1979 
1980  if (status == 0)
1981  BIO_set_flags(bio, (BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY));
1982 
1983  return status;
1984 }
1985 
1986 static int transport_layer_bio_puts(BIO* bio, const char* str)
1987 {
1988  return 1;
1989 }
1990 
1991 static int transport_layer_bio_gets(BIO* bio, char* str, int size)
1992 {
1993  return 1;
1994 }
1995 
1996 static long transport_layer_bio_ctrl(BIO* bio, int cmd, long arg1, void* arg2)
1997 {
1998  WINPR_ASSERT(bio);
1999 
2000  rdpTransportLayer* layer = (rdpTransportLayer*)BIO_get_data(bio);
2001  if (!layer)
2002  return -1;
2003 
2004  int status = -1;
2005  switch (cmd)
2006  {
2007  case BIO_C_GET_EVENT:
2008  *((HANDLE*)arg2) = IFCALLRESULT(NULL, layer->GetEvent, layer->userContext);
2009  status = 1;
2010  break;
2011 
2012  case BIO_C_SET_NONBLOCK:
2013  status = 1;
2014  break;
2015 
2016  case BIO_C_WAIT_READ:
2017  {
2018  int timeout = (int)arg1;
2019  BOOL r = IFCALLRESULT(FALSE, layer->Wait, layer->userContext, FALSE,
2020  WINPR_ASSERTING_INT_CAST(uint32_t, timeout));
2021  /* Convert timeout to error return */
2022  if (!r)
2023  {
2024  errno = ETIMEDOUT;
2025  status = 0;
2026  }
2027  else
2028  status = 1;
2029  break;
2030  }
2031 
2032  case BIO_C_WAIT_WRITE:
2033  {
2034  int timeout = (int)arg1;
2035  BOOL r = IFCALLRESULT(FALSE, layer->Wait, layer->userContext, TRUE,
2036  WINPR_ASSERTING_INT_CAST(uint32_t, timeout));
2037  /* Convert timeout to error return */
2038  if (!r)
2039  {
2040  errno = ETIMEDOUT;
2041  status = 0;
2042  }
2043  else
2044  status = 1;
2045  break;
2046  }
2047 
2048  case BIO_CTRL_GET_CLOSE:
2049  status = BIO_get_shutdown(bio);
2050  break;
2051 
2052  case BIO_CTRL_SET_CLOSE:
2053  BIO_set_shutdown(bio, (int)arg1);
2054  status = 1;
2055  break;
2056 
2057  case BIO_CTRL_FLUSH:
2058  case BIO_CTRL_DUP:
2059  status = 1;
2060  break;
2061 
2062  default:
2063  status = 0;
2064  break;
2065  }
2066 
2067  return status;
2068 }
2069 
2070 static int transport_layer_bio_new(BIO* bio)
2071 {
2072  WINPR_ASSERT(bio);
2073 
2074  BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
2075  BIO_set_init(bio, 1);
2076  return 1;
2077 }
2078 
2079 static int transport_layer_bio_free(BIO* bio)
2080 {
2081  if (!bio)
2082  return 0;
2083 
2084  rdpTransportLayer* layer = (rdpTransportLayer*)BIO_get_data(bio);
2085  if (layer)
2086  transport_layer_free(layer);
2087 
2088  BIO_set_data(bio, NULL);
2089  BIO_set_init(bio, 0);
2090  BIO_set_flags(bio, 0);
2091 
2092  return 1;
2093 }
2094 
2095 BIO_METHOD* BIO_s_transport_layer(void)
2096 {
2097  static BIO_METHOD* bio_methods = NULL;
2098 
2099  if (bio_methods == NULL)
2100  {
2101  if (!(bio_methods = BIO_meth_new(BIO_TYPE_SIMPLE, "TransportLayer")))
2102  return NULL;
2103 
2104  BIO_meth_set_write(bio_methods, transport_layer_bio_write);
2105  BIO_meth_set_read(bio_methods, transport_layer_bio_read);
2106  BIO_meth_set_puts(bio_methods, transport_layer_bio_puts);
2107  BIO_meth_set_gets(bio_methods, transport_layer_bio_gets);
2108  BIO_meth_set_ctrl(bio_methods, transport_layer_bio_ctrl);
2109  BIO_meth_set_create(bio_methods, transport_layer_bio_new);
2110  BIO_meth_set_destroy(bio_methods, transport_layer_bio_free);
2111  }
2112 
2113  return bio_methods;
2114 }
FREERDP_API const char * freerdp_settings_get_server_name(const rdpSettings *settings)
A helper function to return the correct server name.