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