FreeRDP
Loading...
Searching...
No Matches
rdg.c
1
20#include <stdint.h>
21
22#include <freerdp/config.h>
23
24#include "../settings.h"
25
26#include <winpr/assert.h>
27#include <winpr/cast.h>
28
29#include <winpr/crt.h>
30#include <winpr/synch.h>
31#include <winpr/print.h>
32#include <winpr/stream.h>
33#include <winpr/winsock.h>
34#include <winpr/cred.h>
35
36#include <freerdp/log.h>
37#include <freerdp/error.h>
38#include <freerdp/utils/ringbuffer.h>
39#include <freerdp/utils/smartcardlogon.h>
40
41#include "rdg.h"
42#include "websocket.h"
43#include "../credssp_auth.h"
44#include "../proxy.h"
45#include "../rdp.h"
46#include "../../crypto/opensslcompat.h"
47#include "rpc_fault.h"
48#include "../utils.h"
49
50#define TAG FREERDP_TAG("core.gateway.rdg")
51
52#define AUTH_PKG NEGO_SSP_NAME
53
54/* HTTP channel response fields present flags. */
55#define HTTP_CHANNEL_RESPONSE_FIELD_CHANNELID 0x1
56#define HTTP_CHANNEL_RESPONSE_OPTIONAL 0x2
57#define HTTP_CHANNEL_RESPONSE_FIELD_UDPPORT 0x4
58
59/* HTTP extended auth. */
60#define HTTP_EXTENDED_AUTH_NONE 0x0
61#define HTTP_EXTENDED_AUTH_SC 0x1 /* Smart card authentication. */
62#define HTTP_EXTENDED_AUTH_PAA 0x02 /* Pluggable authentication. */
63#define HTTP_EXTENDED_AUTH_SSPI_NTLM 0x04 /* NTLM extended authentication. */
64#define HTTP_EXTENDED_AUTH_BEARER 0x08 /* HTTP Bearer authentication. */
65
66/* HTTP packet types. */
67typedef enum
68{
69 PKT_TYPE_HANDSHAKE_REQUEST = 0x1,
70 PKT_TYPE_HANDSHAKE_RESPONSE = 0x2,
71 PKT_TYPE_EXTENDED_AUTH_MSG = 0x3,
72 PKT_TYPE_TUNNEL_CREATE = 0x4,
73 PKT_TYPE_TUNNEL_RESPONSE = 0x5,
74 PKT_TYPE_TUNNEL_AUTH = 0x6,
75 PKT_TYPE_TUNNEL_AUTH_RESPONSE = 0x7,
76 PKT_TYPE_CHANNEL_CREATE = 0x8,
77 PKT_TYPE_CHANNEL_RESPONSE = 0x9,
78 PKT_TYPE_DATA = 0xA,
79 PKT_TYPE_SERVICE_MESSAGE = 0xB,
80 PKT_TYPE_REAUTH_MESSAGE = 0xC,
81 PKT_TYPE_KEEPALIVE = 0xD,
82 PKT_TYPE_CLOSE_CHANNEL = 0x10,
83 PKT_TYPE_CLOSE_CHANNEL_RESPONSE = 0x11
84} RdgPktType;
85
86/* HTTP tunnel auth fields present flags. */
87// #define HTTP_TUNNEL_AUTH_FIELD_SOH 0x1
88
89/* HTTP tunnel auth response fields present flags. */
90#define HTTP_TUNNEL_AUTH_RESPONSE_FIELD_REDIR_FLAGS 0x1
91#define HTTP_TUNNEL_AUTH_RESPONSE_FIELD_IDLE_TIMEOUT 0x2
92#define HTTP_TUNNEL_AUTH_RESPONSE_FIELD_SOH_RESPONSE 0x4
93
94/* HTTP tunnel packet fields present flags. */
95#define HTTP_TUNNEL_PACKET_FIELD_PAA_COOKIE 0x1
96// #define HTTP_TUNNEL_PACKET_FIELD_REAUTH 0x2
97
98/* HTTP tunnel response fields present flags. */
99#define HTTP_TUNNEL_RESPONSE_FIELD_TUNNEL_ID 0x1
100#define HTTP_TUNNEL_RESPONSE_FIELD_CAPS 0x2
101#define HTTP_TUNNEL_RESPONSE_FIELD_SOH_REQ 0x4
102#define HTTP_TUNNEL_RESPONSE_FIELD_CONSENT_MSG 0x10
103
104/* HTTP capability type enumeration. */
105#define HTTP_CAPABILITY_TYPE_QUAR_SOH 0x1
106#define HTTP_CAPABILITY_IDLE_TIMEOUT 0x2
107#define HTTP_CAPABILITY_MESSAGING_CONSENT_SIGN 0x4
108#define HTTP_CAPABILITY_MESSAGING_SERVICE_MSG 0x8
109#define HTTP_CAPABILITY_REAUTH 0x10
110#define HTTP_CAPABILITY_UDP_TRANSPORT 0x20
111
112typedef struct
113{
114 TRANSFER_ENCODING httpTransferEncoding;
115 BOOL isWebsocketTransport;
116 union context
117 {
119 websocket_context* websocket;
120 } context;
121} rdg_http_encoding_context;
122
123struct rdp_rdg
124{
125 rdpContext* context;
126 BOOL attached;
127 BIO* frontBio;
128 rdpTls* tlsIn;
129 rdpTls* tlsOut;
130 rdpCredsspAuth* auth;
131 HttpContext* http;
132 CRITICAL_SECTION writeSection;
133
134 UUID guid;
135
136 int state;
137 UINT16 packetRemainingCount;
138 UINT16 reserved1;
139 int timeout;
140 UINT16 extAuth;
141 UINT16 reserved2;
142 rdg_http_encoding_context transferEncoding;
143
144 SmartcardCertInfo* smartcard;
145 wLog* log;
146};
147
148enum
149{
150 RDG_CLIENT_STATE_INITIAL,
151 RDG_CLIENT_STATE_HANDSHAKE,
152 RDG_CLIENT_STATE_TUNNEL_CREATE,
153 RDG_CLIENT_STATE_TUNNEL_AUTHORIZE,
154 RDG_CLIENT_STATE_CHANNEL_CREATE,
155 RDG_CLIENT_STATE_OPENED,
156};
157
158#pragma pack(push, 1)
159
160typedef struct rdg_packet_header
161{
162 UINT16 type;
163 UINT16 reserved;
164 UINT32 packetLength;
165} RdgPacketHeader;
166
167#pragma pack(pop)
168
169typedef struct
170{
171 UINT32 code;
172 const char* name;
173} t_flag_mapping;
174
175static const t_flag_mapping tunnel_response_fields_present[] = {
176 { HTTP_TUNNEL_RESPONSE_FIELD_TUNNEL_ID, "HTTP_TUNNEL_RESPONSE_FIELD_TUNNEL_ID" },
177 { HTTP_TUNNEL_RESPONSE_FIELD_CAPS, "HTTP_TUNNEL_RESPONSE_FIELD_CAPS" },
178 { HTTP_TUNNEL_RESPONSE_FIELD_SOH_REQ, "HTTP_TUNNEL_RESPONSE_FIELD_SOH_REQ" },
179 { HTTP_TUNNEL_RESPONSE_FIELD_CONSENT_MSG, "HTTP_TUNNEL_RESPONSE_FIELD_CONSENT_MSG" }
180};
181
182static const t_flag_mapping channel_response_fields_present[] = {
183 { HTTP_CHANNEL_RESPONSE_FIELD_CHANNELID, "HTTP_CHANNEL_RESPONSE_FIELD_CHANNELID" },
184 { HTTP_CHANNEL_RESPONSE_OPTIONAL, "HTTP_CHANNEL_RESPONSE_OPTIONAL" },
185 { HTTP_CHANNEL_RESPONSE_FIELD_UDPPORT, "HTTP_CHANNEL_RESPONSE_FIELD_UDPPORT" }
186};
187
188static const t_flag_mapping tunnel_authorization_response_fields_present[] = {
189 { HTTP_TUNNEL_AUTH_RESPONSE_FIELD_REDIR_FLAGS, "HTTP_TUNNEL_AUTH_RESPONSE_FIELD_REDIR_FLAGS" },
190 { HTTP_TUNNEL_AUTH_RESPONSE_FIELD_IDLE_TIMEOUT,
191 "HTTP_TUNNEL_AUTH_RESPONSE_FIELD_IDLE_TIMEOUT" },
192 { HTTP_TUNNEL_AUTH_RESPONSE_FIELD_SOH_RESPONSE,
193 "HTTP_TUNNEL_AUTH_RESPONSE_FIELD_SOH_RESPONSE" }
194};
195
196static const t_flag_mapping extended_auth[] = {
197 { HTTP_EXTENDED_AUTH_NONE, "HTTP_EXTENDED_AUTH_NONE" },
198 { HTTP_EXTENDED_AUTH_SC, "HTTP_EXTENDED_AUTH_SC" },
199 { HTTP_EXTENDED_AUTH_PAA, "HTTP_EXTENDED_AUTH_PAA" },
200 { HTTP_EXTENDED_AUTH_SSPI_NTLM, "HTTP_EXTENDED_AUTH_SSPI_NTLM" }
201};
202
203static const t_flag_mapping capabilities_enum[] = {
204 { HTTP_CAPABILITY_TYPE_QUAR_SOH, "HTTP_CAPABILITY_TYPE_QUAR_SOH" },
205 { HTTP_CAPABILITY_IDLE_TIMEOUT, "HTTP_CAPABILITY_IDLE_TIMEOUT" },
206 { HTTP_CAPABILITY_MESSAGING_CONSENT_SIGN, "HTTP_CAPABILITY_MESSAGING_CONSENT_SIGN" },
207 { HTTP_CAPABILITY_MESSAGING_SERVICE_MSG, "HTTP_CAPABILITY_MESSAGING_SERVICE_MSG" },
208 { HTTP_CAPABILITY_REAUTH, "HTTP_CAPABILITY_REAUTH" },
209 { HTTP_CAPABILITY_UDP_TRANSPORT, "HTTP_CAPABILITY_UDP_TRANSPORT" }
210};
211
212static const char* rdg_pkt_type_to_string(int type)
213{
214#define ENTRY(x) \
215 case x: \
216 return "#x"
217
218 switch (type)
219 {
220 ENTRY(PKT_TYPE_HANDSHAKE_REQUEST);
221 ENTRY(PKT_TYPE_HANDSHAKE_RESPONSE);
222 ENTRY(PKT_TYPE_EXTENDED_AUTH_MSG);
223 ENTRY(PKT_TYPE_TUNNEL_CREATE);
224 ENTRY(PKT_TYPE_TUNNEL_RESPONSE);
225 ENTRY(PKT_TYPE_TUNNEL_AUTH);
226 ENTRY(PKT_TYPE_TUNNEL_AUTH_RESPONSE);
227 ENTRY(PKT_TYPE_CHANNEL_CREATE);
228 ENTRY(PKT_TYPE_CHANNEL_RESPONSE);
229 ENTRY(PKT_TYPE_DATA);
230 ENTRY(PKT_TYPE_SERVICE_MESSAGE);
231 ENTRY(PKT_TYPE_REAUTH_MESSAGE);
232 ENTRY(PKT_TYPE_KEEPALIVE);
233 ENTRY(PKT_TYPE_CLOSE_CHANNEL);
234 ENTRY(PKT_TYPE_CLOSE_CHANNEL_RESPONSE);
235 default:
236 return "PKT_TYPE_UNKNOWN";
237 }
238#undef ENTRY
239}
240
241static const char* flags_to_string(UINT32 flags, const t_flag_mapping* map, size_t elements)
242{
243 static char buffer[1024] = { 0 };
244 char fields[12] = { 0 };
245
246 for (size_t x = 0; x < elements; x++)
247 {
248 const t_flag_mapping* cur = &map[x];
249
250 if ((cur->code & flags) != 0)
251 winpr_str_append(cur->name, buffer, sizeof(buffer), "|");
252 }
253
254 (void)sprintf_s(fields, ARRAYSIZE(fields), " [%04" PRIx32 "]", flags);
255 winpr_str_append(fields, buffer, sizeof(buffer), NULL);
256 return buffer;
257}
258
259static const char* channel_response_fields_present_to_string(UINT16 fieldsPresent)
260{
261 return flags_to_string(fieldsPresent, channel_response_fields_present,
262 ARRAYSIZE(channel_response_fields_present));
263}
264
265static const char* tunnel_response_fields_present_to_string(UINT16 fieldsPresent)
266{
267 return flags_to_string(fieldsPresent, tunnel_response_fields_present,
268 ARRAYSIZE(tunnel_response_fields_present));
269}
270
271static const char* tunnel_authorization_response_fields_present_to_string(UINT16 fieldsPresent)
272{
273 return flags_to_string(fieldsPresent, tunnel_authorization_response_fields_present,
274 ARRAYSIZE(tunnel_authorization_response_fields_present));
275}
276
277static const char* extended_auth_to_string(UINT16 auth)
278{
279 if (auth == HTTP_EXTENDED_AUTH_NONE)
280 return "HTTP_EXTENDED_AUTH_NONE [0x0000]";
281
282 return flags_to_string(auth, extended_auth, ARRAYSIZE(extended_auth));
283}
284
285static const char* capabilities_enum_to_string(UINT32 capabilities)
286{
287 return flags_to_string(capabilities, capabilities_enum, ARRAYSIZE(capabilities_enum));
288}
289
290static BOOL rdg_read_http_unicode_string(wLog* log, wStream* s, const WCHAR** string,
291 UINT16* lengthInBytes)
292{
293 UINT16 strLenBytes = 0;
294 size_t rem = Stream_GetRemainingLength(s);
295
296 /* Read length of the string */
297 if (!Stream_CheckAndLogRequiredLengthWLog(log, s, 4))
298 {
299 WLog_Print(log, WLOG_ERROR, "Could not read stream length, only have %" PRIuz " bytes",
300 rem);
301 return FALSE;
302 }
303 Stream_Read_UINT16(s, strLenBytes);
304
305 /* Remember position of our string */
306 const WCHAR* str = Stream_ConstPointer(s);
307
308 /* seek past the string - if this fails something is wrong */
309 if (!Stream_SafeSeek(s, strLenBytes))
310 {
311 WLog_Print(log, WLOG_ERROR,
312 "Could not read stream data, only have %" PRIuz " bytes, expected %" PRIu16,
313 rem - 4, strLenBytes);
314 return FALSE;
315 }
316
317 /* return the string data (if wanted) */
318 if (string)
319 *string = str;
320 if (lengthInBytes)
321 *lengthInBytes = strLenBytes;
322
323 return TRUE;
324}
325
326static BOOL rdg_write_chunked(BIO* bio, wStream* sPacket)
327{
328 size_t len = 0;
329 int status = 0;
330 wStream* sChunk = NULL;
331 char chunkSize[11];
332 (void)sprintf_s(chunkSize, sizeof(chunkSize), "%" PRIXz "\r\n", Stream_Length(sPacket));
333 sChunk = Stream_New(NULL, strnlen(chunkSize, sizeof(chunkSize)) + Stream_Length(sPacket) + 2);
334
335 if (!sChunk)
336 return FALSE;
337
338 Stream_Write(sChunk, chunkSize, strnlen(chunkSize, sizeof(chunkSize)));
339 Stream_Write(sChunk, Stream_Buffer(sPacket), Stream_Length(sPacket));
340 Stream_Write(sChunk, "\r\n", 2);
341 Stream_SealLength(sChunk);
342 len = Stream_Length(sChunk);
343
344 if (len > INT_MAX)
345 {
346 Stream_Free(sChunk, TRUE);
347 return FALSE;
348 }
349
350 ERR_clear_error();
351 status = BIO_write(bio, Stream_Buffer(sChunk), (int)len);
352 Stream_Free(sChunk, TRUE);
353
354 if (status != (SSIZE_T)len)
355 return FALSE;
356
357 return TRUE;
358}
359
360static BOOL rdg_write_packet(rdpRdg* rdg, wStream* sPacket)
361{
362 if (rdg->transferEncoding.isWebsocketTransport)
363 return websocket_context_write_wstream(rdg->transferEncoding.context.websocket,
364 rdg->tlsOut->bio, sPacket, WebsocketBinaryOpcode);
365
366 return rdg_write_chunked(rdg->tlsIn->bio, sPacket);
367}
368
369static int rdg_socket_read(BIO* bio, BYTE* pBuffer, size_t size,
370 rdg_http_encoding_context* encodingContext)
371{
372 WINPR_ASSERT(encodingContext != NULL);
373 if (size > INT32_MAX)
374 return -1;
375
376 if (encodingContext->isWebsocketTransport)
377 return websocket_context_read(encodingContext->context.websocket, bio, pBuffer, size);
378
379 switch (encodingContext->httpTransferEncoding)
380 {
381 case TransferEncodingIdentity:
382 ERR_clear_error();
383 return BIO_read(bio, pBuffer, (int)size);
384 case TransferEncodingChunked:
385 return http_chuncked_read(bio, pBuffer, size, &encodingContext->context.chunked);
386 default:
387 return -1;
388 }
389}
390
391static BOOL rdg_shall_abort(rdpRdg* rdg)
392{
393 WINPR_ASSERT(rdg);
394 return freerdp_shall_disconnect_context(rdg->context);
395}
396
397static BOOL rdg_read_all(rdpContext* context, rdpTls* tls, BYTE* buffer, size_t size,
398 rdg_http_encoding_context* transferEncoding)
399{
400 size_t readCount = 0;
401 BYTE* pBuffer = buffer;
402
403 while (readCount < size)
404 {
405 if (freerdp_shall_disconnect_context(context))
406 return FALSE;
407
408 int status = rdg_socket_read(tls->bio, pBuffer, size - readCount, transferEncoding);
409 if (status <= 0)
410 {
411 if (!BIO_should_retry(tls->bio))
412 return FALSE;
413
414 Sleep(10);
415 continue;
416 }
417
418 readCount += WINPR_ASSERTING_INT_CAST(uint32_t, status);
419 pBuffer += WINPR_ASSERTING_INT_CAST(uint32_t, status);
420 }
421
422 return TRUE;
423}
424
425static wStream* rdg_receive_packet(rdpRdg* rdg)
426{
427 const size_t header = sizeof(RdgPacketHeader);
428 size_t packetLength = 0;
429 wStream* s = Stream_New(NULL, 1024);
430
431 if (!s)
432 return NULL;
433
434 if (!rdg_read_all(rdg->context, rdg->tlsOut, Stream_Buffer(s), header, &rdg->transferEncoding))
435 {
436 Stream_Free(s, TRUE);
437 return NULL;
438 }
439
440 Stream_Seek(s, 4);
441 Stream_Read_UINT32(s, packetLength);
442
443 if ((packetLength > INT_MAX) || !Stream_EnsureCapacity(s, packetLength) ||
444 (packetLength < header))
445 {
446 Stream_Free(s, TRUE);
447 return NULL;
448 }
449
450 if (!rdg_read_all(rdg->context, rdg->tlsOut, Stream_Buffer(s) + header, packetLength - header,
451 &rdg->transferEncoding))
452 {
453 Stream_Free(s, TRUE);
454 return NULL;
455 }
456
457 Stream_SetLength(s, packetLength);
458 return s;
459}
460
461static BOOL rdg_send_handshake(rdpRdg* rdg)
462{
463 BOOL status = FALSE;
464 wStream* s = Stream_New(NULL, 14);
465
466 if (!s)
467 return FALSE;
468
469 Stream_Write_UINT16(s, PKT_TYPE_HANDSHAKE_REQUEST); /* Type (2 bytes) */
470 Stream_Write_UINT16(s, 0); /* Reserved (2 bytes) */
471 Stream_Write_UINT32(s, 14); /* PacketLength (4 bytes) */
472 Stream_Write_UINT8(s, 1); /* VersionMajor (1 byte) */
473 Stream_Write_UINT8(s, 0); /* VersionMinor (1 byte) */
474 Stream_Write_UINT16(s, 0); /* ClientVersion (2 bytes), must be 0 */
475 Stream_Write_UINT16(s, rdg->extAuth); /* ExtendedAuthentication (2 bytes) */
476 Stream_SealLength(s);
477 status = rdg_write_packet(rdg, s);
478 Stream_Free(s, TRUE);
479
480 if (status)
481 {
482 rdg->state = RDG_CLIENT_STATE_HANDSHAKE;
483 }
484
485 return status;
486}
487
488static BOOL rdg_send_extauth_sspi(rdpRdg* rdg)
489{
490 wStream* s = NULL;
491 BOOL status = 0;
492 UINT32 packetSize = 8 + 4 + 2;
493
494 WINPR_ASSERT(rdg);
495
496 const SecBuffer* authToken = credssp_auth_get_output_buffer(rdg->auth);
497 if (!authToken)
498 return FALSE;
499 packetSize += authToken->cbBuffer;
500
501 s = Stream_New(NULL, packetSize);
502
503 if (!s)
504 return FALSE;
505
506 Stream_Write_UINT16(s, PKT_TYPE_EXTENDED_AUTH_MSG); /* Type (2 bytes) */
507 Stream_Write_UINT16(s, 0); /* Reserved (2 bytes) */
508 Stream_Write_UINT32(s, packetSize); /* PacketLength (4 bytes) */
509 Stream_Write_UINT32(s, ERROR_SUCCESS); /* Error code */
510 Stream_Write_UINT16(s, (UINT16)authToken->cbBuffer);
511 Stream_Write(s, authToken->pvBuffer, authToken->cbBuffer);
512
513 Stream_SealLength(s);
514 status = rdg_write_packet(rdg, s);
515 Stream_Free(s, TRUE);
516
517 return status;
518}
519
520static BOOL rdg_send_tunnel_request(rdpRdg* rdg)
521{
522 wStream* s = NULL;
523 BOOL status = 0;
524 UINT32 packetSize = 16;
525 UINT16 fieldsPresent = 0;
526 WCHAR* PAACookie = NULL;
527 size_t PAACookieLen = 0;
528 const UINT32 capabilities = HTTP_CAPABILITY_TYPE_QUAR_SOH |
529 HTTP_CAPABILITY_MESSAGING_CONSENT_SIGN |
530 HTTP_CAPABILITY_MESSAGING_SERVICE_MSG;
531
532 if (rdg->extAuth == HTTP_EXTENDED_AUTH_PAA)
533 {
534 PAACookie =
535 ConvertUtf8ToWCharAlloc(rdg->context->settings->GatewayAccessToken, &PAACookieLen);
536
537 if (!PAACookie || (PAACookieLen > UINT16_MAX / sizeof(WCHAR)))
538 {
539 free(PAACookie);
540 return FALSE;
541 }
542
543 PAACookieLen += 1; /* include \0 */
544 packetSize += 2 + (UINT32)(PAACookieLen) * sizeof(WCHAR);
545 fieldsPresent = HTTP_TUNNEL_PACKET_FIELD_PAA_COOKIE;
546 }
547
548 s = Stream_New(NULL, packetSize);
549
550 if (!s)
551 {
552 free(PAACookie);
553 return FALSE;
554 }
555
556 Stream_Write_UINT16(s, PKT_TYPE_TUNNEL_CREATE); /* Type (2 bytes) */
557 Stream_Write_UINT16(s, 0); /* Reserved (2 bytes) */
558 Stream_Write_UINT32(s, packetSize); /* PacketLength (4 bytes) */
559 Stream_Write_UINT32(s, capabilities); /* CapabilityFlags (4 bytes) */
560 Stream_Write_UINT16(s, fieldsPresent); /* FieldsPresent (2 bytes) */
561 Stream_Write_UINT16(s, 0); /* Reserved (2 bytes), must be 0 */
562
563 if (PAACookie)
564 {
565 Stream_Write_UINT16(s, (UINT16)PAACookieLen * sizeof(WCHAR)); /* PAA cookie string length */
566 Stream_Write_UTF16_String(s, PAACookie, PAACookieLen);
567 }
568
569 Stream_SealLength(s);
570 status = rdg_write_packet(rdg, s);
571 Stream_Free(s, TRUE);
572 free(PAACookie);
573
574 if (status)
575 {
576 rdg->state = RDG_CLIENT_STATE_TUNNEL_CREATE;
577 }
578
579 return status;
580}
581
582static BOOL rdg_send_tunnel_authorization(rdpRdg* rdg)
583{
584 wStream* s = NULL;
585 BOOL status = 0;
586 WINPR_ASSERT(rdg);
587 size_t clientNameLen = 0;
588 WCHAR* clientName = freerdp_settings_get_string_as_utf16(
589 rdg->context->settings, FreeRDP_ClientHostname, &clientNameLen);
590
591 clientNameLen++; // length including terminating '\0'
592
593 const size_t packetSize = 12ull + clientNameLen * sizeof(WCHAR);
594 if (!clientName || (clientNameLen >= UINT16_MAX / sizeof(WCHAR)) || (packetSize > UINT32_MAX))
595 {
596 free(clientName);
597 return FALSE;
598 }
599
600 s = Stream_New(NULL, packetSize);
601
602 if (!s)
603 {
604 free(clientName);
605 return FALSE;
606 }
607
608 Stream_Write_UINT16(s, PKT_TYPE_TUNNEL_AUTH); /* Type (2 bytes) */
609 Stream_Write_UINT16(s, 0); /* Reserved (2 bytes) */
610 Stream_Write_UINT32(s, (UINT32)packetSize); /* PacketLength (4 bytes) */
611 Stream_Write_UINT16(s, 0); /* FieldsPresent (2 bytes) */
612 Stream_Write_UINT16(s, (UINT16)clientNameLen * sizeof(WCHAR)); /* Client name string length */
613 Stream_Write_UTF16_String(s, clientName, clientNameLen);
614 Stream_SealLength(s);
615 status = rdg_write_packet(rdg, s);
616 Stream_Free(s, TRUE);
617 free(clientName);
618
619 if (status)
620 {
621 rdg->state = RDG_CLIENT_STATE_TUNNEL_AUTHORIZE;
622 }
623
624 return status;
625}
626
627static BOOL rdg_send_channel_create(rdpRdg* rdg)
628{
629 wStream* s = NULL;
630 BOOL status = FALSE;
631 WCHAR* serverName = NULL;
632 size_t serverNameLen = 0;
633
634 WINPR_ASSERT(rdg);
635 serverName = freerdp_settings_get_string_as_utf16(rdg->context->settings,
636 FreeRDP_ServerHostname, &serverNameLen);
637
638 serverNameLen++; // length including terminating '\0'
639 const size_t packetSize = 16ull + serverNameLen * sizeof(WCHAR);
640 if (!serverName || (serverNameLen >= UINT16_MAX / sizeof(WCHAR)) || (packetSize > UINT32_MAX))
641 goto fail;
642
643 s = Stream_New(NULL, packetSize);
644
645 if (!s)
646 goto fail;
647
648 Stream_Write_UINT16(s, PKT_TYPE_CHANNEL_CREATE); /* Type (2 bytes) */
649 Stream_Write_UINT16(s, 0); /* Reserved (2 bytes) */
650 Stream_Write_UINT32(s, (UINT32)packetSize); /* PacketLength (4 bytes) */
651 Stream_Write_UINT8(s, 1); /* Number of resources. (1 byte) */
652 Stream_Write_UINT8(s, 0); /* Number of alternative resources (1 byte) */
653 Stream_Write_UINT16(s,
654 (UINT16)rdg->context->settings->ServerPort); /* Resource port (2 bytes) */
655 Stream_Write_UINT16(s, 3); /* Protocol number (2 bytes) */
656 Stream_Write_UINT16(s, (UINT16)serverNameLen * sizeof(WCHAR));
657 Stream_Write_UTF16_String(s, serverName, serverNameLen);
658 Stream_SealLength(s);
659 status = rdg_write_packet(rdg, s);
660fail:
661 free(serverName);
662 Stream_Free(s, TRUE);
663
664 if (status)
665 rdg->state = RDG_CLIENT_STATE_CHANNEL_CREATE;
666
667 return status;
668}
669
670static BOOL rdg_set_auth_header(rdpCredsspAuth* auth, HttpRequest* request)
671{
672 const SecBuffer* authToken = credssp_auth_get_output_buffer(auth);
673 char* base64AuthToken = NULL;
674
675 if (authToken)
676 {
677 if (authToken->cbBuffer > INT_MAX)
678 return FALSE;
679
680 base64AuthToken = crypto_base64_encode(authToken->pvBuffer, authToken->cbBuffer);
681 }
682
683 if (base64AuthToken)
684 {
685 BOOL rc = http_request_set_auth_scheme(request, credssp_auth_pkg_name(auth)) &&
686 http_request_set_auth_param(request, base64AuthToken);
687 free(base64AuthToken);
688
689 if (!rc)
690 return FALSE;
691 }
692
693 return TRUE;
694}
695
696static wStream* rdg_build_http_request(rdpRdg* rdg, const char* method,
697 TRANSFER_ENCODING transferEncoding)
698{
699 wStream* s = NULL;
700 HttpRequest* request = NULL;
701 const char* uri = NULL;
702
703 if (!rdg || !method)
704 return NULL;
705
706 uri = http_context_get_uri(rdg->http);
707 request = http_request_new();
708
709 if (!request)
710 return NULL;
711
712 if (!http_request_set_method(request, method) || !http_request_set_uri(request, uri))
713 goto out;
714
715 if (rdg->auth)
716 {
717 if (!rdg_set_auth_header(rdg->auth, request))
718 goto out;
719 }
720
721 else if (rdg->extAuth == HTTP_EXTENDED_AUTH_BEARER)
722 {
723 http_request_set_auth_scheme(request, "Bearer");
724 http_request_set_auth_param(request, rdg->context->settings->GatewayHttpExtAuthBearer);
725 }
726
727 http_request_set_transfer_encoding(request, transferEncoding);
728
729 s = http_request_write(rdg->http, request);
730out:
731 http_request_free(request);
732
733 if (s)
734 Stream_SealLength(s);
735
736 return s;
737}
738
739static BOOL rdg_recv_auth_token(wLog* log, rdpCredsspAuth* auth, HttpResponse* response)
740{
741 size_t len = 0;
742 size_t authTokenLength = 0;
743 BYTE* authTokenData = NULL;
744 SecBuffer authToken = { 0 };
745 int rc = 0;
746
747 if (!auth || !response)
748 return FALSE;
749
750 const UINT16 StatusCode = http_response_get_status_code(response);
751 switch (StatusCode)
752 {
753 case HTTP_STATUS_DENIED:
754 case HTTP_STATUS_OK:
755 case HTTP_STATUS_SWITCH_PROTOCOLS:
756 break;
757 default:
758 http_response_log_error_status(log, WLOG_WARN, response);
759 return FALSE;
760 }
761
762 const char* token64 = http_response_get_auth_token(response, credssp_auth_pkg_name(auth));
763 if (!token64)
764 return FALSE;
765
766 len = strlen(token64);
767
768 crypto_base64_decode(token64, len, &authTokenData, &authTokenLength);
769
770 if (authTokenLength && authTokenData && (authTokenLength <= UINT32_MAX))
771 {
772 authToken.pvBuffer = authTokenData;
773 authToken.cbBuffer = (UINT32)authTokenLength;
774 credssp_auth_take_input_buffer(auth, &authToken);
775 }
776 else
777 free(authTokenData);
778
779 rc = credssp_auth_authenticate(auth);
780 if (rc < 0)
781 return FALSE;
782
783 return TRUE;
784}
785
786static BOOL rdg_skip_seed_payload(rdpContext* context, rdpTls* tls, size_t lastResponseLength,
787 rdg_http_encoding_context* transferEncoding)
788{
789 BYTE seed_payload[10] = { 0 };
790 const size_t size = sizeof(seed_payload);
791
792 /* Per [MS-TSGU] 3.3.5.1 step 4, after final OK response RDG server sends
793 * random "seed" payload of limited size. In practice it's 10 bytes.
794 */
795 if (lastResponseLength < size)
796 {
797 if (!rdg_read_all(context, tls, seed_payload, size - lastResponseLength, transferEncoding))
798 {
799 return FALSE;
800 }
801 }
802
803 return TRUE;
804}
805
806static BOOL rdg_process_handshake_response(rdpRdg* rdg, wStream* s)
807{
808 UINT32 errorCode = 0;
809 UINT16 serverVersion = 0;
810 UINT16 extendedAuth = 0;
811 BYTE verMajor = 0;
812 BYTE verMinor = 0;
813 const char* error = NULL;
814 WLog_Print(rdg->log, WLOG_DEBUG, "Handshake response received");
815
816 if (rdg->state != RDG_CLIENT_STATE_HANDSHAKE)
817 {
818 return FALSE;
819 }
820
821 if (!Stream_CheckAndLogRequiredLengthWLog(rdg->log, s, 10))
822 return FALSE;
823
824 Stream_Read_UINT32(s, errorCode);
825 Stream_Read_UINT8(s, verMajor);
826 Stream_Read_UINT8(s, verMinor);
827 Stream_Read_UINT16(s, serverVersion);
828 Stream_Read_UINT16(s, extendedAuth);
829 error = rpc_error_to_string(errorCode);
830 WLog_Print(rdg->log, WLOG_DEBUG,
831 "errorCode=%s, verMajor=%" PRId8 ", verMinor=%" PRId8 ", serverVersion=%" PRId16
832 ", extendedAuth=%s",
833 error, verMajor, verMinor, serverVersion, extended_auth_to_string(extendedAuth));
834
835 if (FAILED((HRESULT)errorCode))
836 {
837 WLog_Print(rdg->log, WLOG_ERROR, "Handshake error %s", error);
838 freerdp_set_last_error_log(rdg->context, errorCode);
839 return FALSE;
840 }
841
842 if (rdg->extAuth == HTTP_EXTENDED_AUTH_SSPI_NTLM)
843 return rdg_send_extauth_sspi(rdg);
844
845 return rdg_send_tunnel_request(rdg);
846}
847
848static BOOL rdg_process_tunnel_response_optional(rdpRdg* rdg, wStream* s, UINT16 fieldsPresent)
849{
850 if (fieldsPresent & HTTP_TUNNEL_RESPONSE_FIELD_TUNNEL_ID)
851 {
852 /* Seek over tunnelId (4 bytes) */
853 if (!Stream_SafeSeek(s, 4))
854 {
855 WLog_Print(rdg->log, WLOG_ERROR, "Short tunnelId, got %" PRIuz ", expected 4",
856 Stream_GetRemainingLength(s));
857 return FALSE;
858 }
859 }
860
861 if (fieldsPresent & HTTP_TUNNEL_RESPONSE_FIELD_CAPS)
862 {
863 UINT32 caps = 0;
864 if (!Stream_CheckAndLogRequiredLengthWLog(rdg->log, s, 4))
865 return FALSE;
866
867 Stream_Read_UINT32(s, caps);
868 WLog_Print(rdg->log, WLOG_DEBUG, "capabilities=%s", capabilities_enum_to_string(caps));
869 }
870
871 if (fieldsPresent & HTTP_TUNNEL_RESPONSE_FIELD_SOH_REQ)
872 {
873 /* Seek over nonce (20 bytes) */
874 if (!Stream_SafeSeek(s, 20))
875 {
876 WLog_Print(rdg->log, WLOG_ERROR, "Short nonce, got %" PRIuz ", expected 20",
877 Stream_GetRemainingLength(s));
878 return FALSE;
879 }
880
881 /* Read serverCert */
882 if (!rdg_read_http_unicode_string(rdg->log, s, NULL, NULL))
883 {
884 WLog_Print(rdg->log, WLOG_ERROR, "Failed to read server certificate");
885 return FALSE;
886 }
887 }
888
889 if (fieldsPresent & HTTP_TUNNEL_RESPONSE_FIELD_CONSENT_MSG)
890 {
891 const WCHAR* msg = NULL;
892 UINT16 msgLenBytes = 0;
893 rdpContext* context = rdg->context;
894
895 WINPR_ASSERT(context);
896 WINPR_ASSERT(context->instance);
897
898 /* Read message string and invoke callback */
899 if (!rdg_read_http_unicode_string(rdg->log, s, &msg, &msgLenBytes))
900 {
901 WLog_Print(rdg->log, WLOG_ERROR, "Failed to read consent message");
902 return FALSE;
903 }
904
905 return IFCALLRESULT(TRUE, context->instance->PresentGatewayMessage, context->instance,
906 GATEWAY_MESSAGE_CONSENT, TRUE, TRUE, msgLenBytes, msg);
907 }
908
909 return TRUE;
910}
911
912static BOOL rdg_process_tunnel_response(rdpRdg* rdg, wStream* s)
913{
914 UINT16 serverVersion = 0;
915 UINT16 fieldsPresent = 0;
916 UINT32 errorCode = 0;
917 const char* error = NULL;
918 WLog_Print(rdg->log, WLOG_DEBUG, "Tunnel response received");
919
920 if (rdg->state != RDG_CLIENT_STATE_TUNNEL_CREATE)
921 {
922 return FALSE;
923 }
924
925 if (!Stream_CheckAndLogRequiredLengthWLog(rdg->log, s, 10))
926 return FALSE;
927
928 Stream_Read_UINT16(s, serverVersion);
929 Stream_Read_UINT32(s, errorCode);
930 Stream_Read_UINT16(s, fieldsPresent);
931 Stream_Seek_UINT16(s); /* reserved */
932 error = rpc_error_to_string(errorCode);
933 WLog_Print(rdg->log, WLOG_DEBUG, "serverVersion=%" PRId16 ", errorCode=%s, fieldsPresent=%s",
934 serverVersion, error, tunnel_response_fields_present_to_string(fieldsPresent));
935
936 if (FAILED((HRESULT)errorCode))
937 {
938 WLog_Print(rdg->log, WLOG_ERROR, "Tunnel creation error %s", error);
939 freerdp_set_last_error_log(rdg->context, errorCode);
940 return FALSE;
941 }
942
943 if (!rdg_process_tunnel_response_optional(rdg, s, fieldsPresent))
944 return FALSE;
945
946 return rdg_send_tunnel_authorization(rdg);
947}
948
949static BOOL rdg_process_tunnel_authorization_response(rdpRdg* rdg, wStream* s)
950{
951 UINT32 errorCode = 0;
952 UINT16 fieldsPresent = 0;
953 const char* error = NULL;
954 WLog_Print(rdg->log, WLOG_DEBUG, "Tunnel authorization received");
955
956 if (rdg->state != RDG_CLIENT_STATE_TUNNEL_AUTHORIZE)
957 {
958 return FALSE;
959 }
960
961 if (!Stream_CheckAndLogRequiredLengthWLog(rdg->log, s, 8))
962 return FALSE;
963
964 Stream_Read_UINT32(s, errorCode);
965 Stream_Read_UINT16(s, fieldsPresent);
966 Stream_Seek_UINT16(s); /* reserved */
967 error = rpc_error_to_string(errorCode);
968 WLog_Print(rdg->log, WLOG_DEBUG, "errorCode=%s, fieldsPresent=%s", error,
969 tunnel_authorization_response_fields_present_to_string(fieldsPresent));
970
971 /* [MS-TSGU] 3.7.5.2.7 */
972 if (errorCode != S_OK && errorCode != E_PROXY_QUARANTINE_ACCESSDENIED)
973 {
974 WLog_Print(rdg->log, WLOG_ERROR, "Tunnel authorization error %s", error);
975 freerdp_set_last_error_log(rdg->context, errorCode);
976 return FALSE;
977 }
978
979 if (fieldsPresent & HTTP_TUNNEL_AUTH_RESPONSE_FIELD_REDIR_FLAGS)
980 {
981 UINT32 redirFlags = 0;
982 if (!Stream_CheckAndLogRequiredCapacityWLog(rdg->log, s, 4))
983 return FALSE;
984 Stream_Read_UINT32(s, redirFlags);
985
986 rdpContext* context = rdg->context;
987 if (!utils_apply_gateway_policy(rdg->log, context, redirFlags, "RDG"))
988 return FALSE;
989 }
990
991 if (fieldsPresent & HTTP_TUNNEL_AUTH_RESPONSE_FIELD_IDLE_TIMEOUT)
992 {
993 UINT32 idleTimeout = 0;
994 if (!Stream_CheckAndLogRequiredCapacityWLog(rdg->log, s, 4))
995 return FALSE;
996 Stream_Read_UINT32(s, idleTimeout);
997 WLog_Print(rdg->log, WLOG_DEBUG, "[IDLE_TIMEOUT] idleTimeout=%" PRIu32 ": TODO: unused",
998 idleTimeout);
999 }
1000
1001 if (fieldsPresent & HTTP_TUNNEL_AUTH_RESPONSE_FIELD_SOH_RESPONSE)
1002 {
1003 UINT16 cbLen = 0;
1004 if (!Stream_CheckAndLogRequiredCapacityWLog(rdg->log, s, 2))
1005 return FALSE;
1006 Stream_Read_UINT16(s, cbLen);
1007
1008 WLog_Print(rdg->log, WLOG_DEBUG, "[SOH_RESPONSE] cbLen=%" PRIu16 ": TODO: unused", cbLen);
1009 if (!Stream_CheckAndLogRequiredLengthWLog(rdg->log, s, cbLen))
1010 return FALSE;
1011 Stream_Seek(s, cbLen);
1012 }
1013
1014 return rdg_send_channel_create(rdg);
1015}
1016
1017static BOOL rdg_process_extauth_sspi(rdpRdg* rdg, wStream* s)
1018{
1019 INT32 errorCode = 0;
1020 UINT16 authBlobLen = 0;
1021 SecBuffer authToken = { 0 };
1022 BYTE* authTokenData = NULL;
1023
1024 WINPR_ASSERT(rdg);
1025
1026 Stream_Read_INT32(s, errorCode);
1027 Stream_Read_UINT16(s, authBlobLen);
1028
1029 if (errorCode != ERROR_SUCCESS)
1030 {
1031 WLog_Print(rdg->log, WLOG_ERROR, "EXTAUTH_SSPI_NTLM failed with error %s [0x%08X]",
1032 GetSecurityStatusString(errorCode), WINPR_CXX_COMPAT_CAST(UINT32, errorCode));
1033 return FALSE;
1034 }
1035
1036 if (authBlobLen == 0)
1037 {
1038 if (credssp_auth_is_complete(rdg->auth))
1039 {
1040 credssp_auth_free(rdg->auth);
1041 rdg->auth = NULL;
1042 return rdg_send_tunnel_request(rdg);
1043 }
1044 return FALSE;
1045 }
1046
1047 authTokenData = malloc(authBlobLen);
1048 if (authTokenData == NULL)
1049 return FALSE;
1050 Stream_Read(s, authTokenData, authBlobLen);
1051
1052 authToken.pvBuffer = authTokenData;
1053 authToken.cbBuffer = authBlobLen;
1054
1055 credssp_auth_take_input_buffer(rdg->auth, &authToken);
1056
1057 if (credssp_auth_authenticate(rdg->auth) < 0)
1058 return FALSE;
1059
1060 if (credssp_auth_have_output_token(rdg->auth))
1061 return rdg_send_extauth_sspi(rdg);
1062
1063 return FALSE;
1064}
1065
1066static BOOL rdg_process_channel_response(rdpRdg* rdg, wStream* s)
1067{
1068 UINT16 fieldsPresent = 0;
1069 UINT32 errorCode = 0;
1070 const char* error = NULL;
1071 WLog_Print(rdg->log, WLOG_DEBUG, "Channel response received");
1072
1073 if (rdg->state != RDG_CLIENT_STATE_CHANNEL_CREATE)
1074 {
1075 return FALSE;
1076 }
1077
1078 if (!Stream_CheckAndLogRequiredLengthWLog(rdg->log, s, 8))
1079 return FALSE;
1080
1081 Stream_Read_UINT32(s, errorCode);
1082 Stream_Read_UINT16(s, fieldsPresent);
1083 Stream_Seek_UINT16(s); /* reserved */
1084 error = rpc_error_to_string(errorCode);
1085 WLog_Print(rdg->log, WLOG_DEBUG, "channel response errorCode=%s, fieldsPresent=%s", error,
1086 channel_response_fields_present_to_string(fieldsPresent));
1087
1088 if (FAILED((HRESULT)errorCode))
1089 {
1090 WLog_Print(rdg->log, WLOG_ERROR, "channel response errorCode=%s, fieldsPresent=%s", error,
1091 channel_response_fields_present_to_string(fieldsPresent));
1092 freerdp_set_last_error_log(rdg->context, errorCode);
1093 return FALSE;
1094 }
1095
1096 rdg->state = RDG_CLIENT_STATE_OPENED;
1097 return TRUE;
1098}
1099
1100static BOOL rdg_process_packet(rdpRdg* rdg, wStream* s)
1101{
1102 BOOL status = TRUE;
1103 UINT16 type = 0;
1104 UINT32 packetLength = 0;
1105 Stream_SetPosition(s, 0);
1106
1107 if (!Stream_CheckAndLogRequiredLengthWLog(rdg->log, s, 8))
1108 return FALSE;
1109
1110 Stream_Read_UINT16(s, type);
1111 Stream_Seek_UINT16(s); /* reserved */
1112 Stream_Read_UINT32(s, packetLength);
1113
1114 if (Stream_Length(s) < packetLength)
1115 {
1116 WLog_Print(rdg->log, WLOG_ERROR, "Short packet %" PRIuz ", expected %" PRIu32,
1117 Stream_Length(s), packetLength);
1118 return FALSE;
1119 }
1120
1121 switch (type)
1122 {
1123 case PKT_TYPE_HANDSHAKE_RESPONSE:
1124 status = rdg_process_handshake_response(rdg, s);
1125 break;
1126
1127 case PKT_TYPE_TUNNEL_RESPONSE:
1128 status = rdg_process_tunnel_response(rdg, s);
1129 break;
1130
1131 case PKT_TYPE_TUNNEL_AUTH_RESPONSE:
1132 status = rdg_process_tunnel_authorization_response(rdg, s);
1133 break;
1134
1135 case PKT_TYPE_CHANNEL_RESPONSE:
1136 status = rdg_process_channel_response(rdg, s);
1137 break;
1138
1139 case PKT_TYPE_DATA:
1140 WLog_Print(rdg->log, WLOG_ERROR, "Unexpected packet type DATA");
1141 status = FALSE;
1142 break;
1143
1144 case PKT_TYPE_EXTENDED_AUTH_MSG:
1145 status = rdg_process_extauth_sspi(rdg, s);
1146 break;
1147
1148 default:
1149 WLog_Print(rdg->log, WLOG_ERROR, "PKG TYPE 0x%x not implemented", type);
1150 status = FALSE;
1151 break;
1152 }
1153
1154 if (status)
1155 {
1156 const size_t rem = Stream_GetRemainingLength(s);
1157 if (rem > 0)
1158 WLog_Print(rdg->log, WLOG_WARN, "[%s] unparsed data detected: %" PRIuz " bytes",
1159 rdg_pkt_type_to_string(type), rem);
1160 }
1161 return status;
1162}
1163
1164DWORD rdg_get_event_handles(rdpRdg* rdg, HANDLE* events, DWORD count)
1165{
1166 DWORD nCount = 0;
1167 WINPR_ASSERT(rdg != NULL);
1168
1169 if (rdg->tlsOut && rdg->tlsOut->bio)
1170 {
1171 if (events && (nCount < count))
1172 {
1173 BIO_get_event(rdg->tlsOut->bio, &events[nCount]);
1174 nCount++;
1175 }
1176 else
1177 return 0;
1178 }
1179
1180 /* We just need the read event handle even in non-websocket mode. */
1181
1182 return nCount;
1183}
1184
1185static BOOL rdg_get_gateway_credentials(rdpContext* context, rdp_auth_reason reason)
1186{
1187 freerdp* instance = context->instance;
1188
1189 auth_status rc = utils_authenticate_gateway(instance, reason);
1190 switch (rc)
1191 {
1192 case AUTH_SUCCESS:
1193 case AUTH_SKIP:
1194 return TRUE;
1195 case AUTH_CANCELLED:
1196 freerdp_set_last_error_log(instance->context, FREERDP_ERROR_CONNECT_CANCELLED);
1197 return FALSE;
1198 case AUTH_NO_CREDENTIALS:
1199 WLog_INFO(TAG, "No credentials provided - using NULL identity");
1200 return TRUE;
1201 case AUTH_FAILED:
1202 default:
1203 return FALSE;
1204 }
1205}
1206
1207static BOOL rdg_auth_init(rdpRdg* rdg, rdpTls* tls, TCHAR* authPkg)
1208{
1209 rdpContext* context = rdg->context;
1210 rdpSettings* settings = context->settings;
1211 SEC_WINNT_AUTH_IDENTITY identity = { 0 };
1212 int rc = 0;
1213
1214 rdg->auth = credssp_auth_new(context);
1215 if (!rdg->auth)
1216 return FALSE;
1217
1218 if (!credssp_auth_init(rdg->auth, authPkg, tls->Bindings))
1219 return FALSE;
1220
1221 BOOL doSCLogon = freerdp_settings_get_bool(settings, FreeRDP_SmartcardLogon);
1222 if (doSCLogon)
1223 {
1224 if (!smartcard_getCert(context, &rdg->smartcard, TRUE))
1225 return FALSE;
1226
1227 if (!rdg_get_gateway_credentials(context, AUTH_SMARTCARD_PIN))
1228 return FALSE;
1229 }
1230 else
1231 {
1232 if (!rdg_get_gateway_credentials(context, GW_AUTH_RDG))
1233 return FALSE;
1234
1235 /* Auth callback might changed logon to smartcard so check again */
1236 doSCLogon = freerdp_settings_get_bool(settings, FreeRDP_SmartcardLogon);
1237 if (doSCLogon && !smartcard_getCert(context, &rdg->smartcard, TRUE))
1238 return FALSE;
1239 }
1240
1241 SEC_WINNT_AUTH_IDENTITY* identityArg = &identity;
1242 if (doSCLogon)
1243 {
1244 if (!identity_set_from_smartcard_hash(&identity, settings, FreeRDP_GatewayUsername,
1245 FreeRDP_GatewayDomain, FreeRDP_GatewayPassword,
1246 rdg->smartcard->sha1Hash,
1247 sizeof(rdg->smartcard->sha1Hash)))
1248 return FALSE;
1249 }
1250 else
1251 {
1252 if (!identity_set_from_settings(&identity, settings, FreeRDP_GatewayUsername,
1253 FreeRDP_GatewayDomain, FreeRDP_GatewayPassword))
1254 return FALSE;
1255
1256 if (!settings->GatewayUsername)
1257 identityArg = NULL;
1258 }
1259
1260 if (!credssp_auth_setup_client(rdg->auth, "HTTP", settings->GatewayHostname, identityArg,
1261 rdg->smartcard ? rdg->smartcard->pkinitArgs : NULL))
1262 {
1263 sspi_FreeAuthIdentity(&identity);
1264 return FALSE;
1265 }
1266 sspi_FreeAuthIdentity(&identity);
1267
1268 credssp_auth_set_flags(rdg->auth, ISC_REQ_CONFIDENTIALITY | ISC_REQ_MUTUAL_AUTH);
1269
1270 rc = credssp_auth_authenticate(rdg->auth);
1271 if (rc < 0)
1272 return FALSE;
1273
1274 return TRUE;
1275}
1276
1277static BOOL rdg_send_http_request(rdpRdg* rdg, rdpTls* tls, const char* method,
1278 TRANSFER_ENCODING transferEncoding)
1279{
1280 int status = -1;
1281 wStream* s = rdg_build_http_request(rdg, method, transferEncoding);
1282
1283 if (!s)
1284 return FALSE;
1285
1286 const size_t sz = Stream_Length(s);
1287 status = freerdp_tls_write_all(tls, Stream_Buffer(s), sz);
1288
1289 Stream_Free(s, TRUE);
1290 return (status >= 0);
1291}
1292
1293static BOOL rdg_tls_connect(rdpRdg* rdg, rdpTls* tls, const char* peerAddress, UINT32 timeout)
1294{
1295 long status = 0;
1296 BIO* layerBio = NULL;
1297 BIO* bufferedBio = NULL;
1298 rdpTransportLayer* layer = NULL;
1299 rdpSettings* settings = rdg->context->settings;
1300 rdpTransport* transport = freerdp_get_transport(rdg->context);
1301 const char* peerHostname = settings->GatewayHostname;
1302 UINT16 peerPort = (UINT16)settings->GatewayPort;
1303 const char* proxyUsername = NULL;
1304 const char* proxyPassword = NULL;
1305 BOOL isProxyConnection =
1306 proxy_prepare(settings, &peerHostname, &peerPort, &proxyUsername, &proxyPassword);
1307
1308 if (settings->GatewayPort > UINT16_MAX)
1309 return FALSE;
1310
1311 layer = transport_connect_layer(transport, peerAddress ? peerAddress : peerHostname, peerPort,
1312 timeout);
1313
1314 if (!layer)
1315 {
1316 return FALSE;
1317 }
1318
1319 layerBio = BIO_new(BIO_s_transport_layer());
1320 if (!layerBio)
1321 {
1322 transport_layer_free(layer);
1323 return FALSE;
1324 }
1325 BIO_set_data(layerBio, layer);
1326
1327 bufferedBio = BIO_new(BIO_s_buffered_socket());
1328 if (!bufferedBio)
1329 {
1330 BIO_free_all(layerBio);
1331 return FALSE;
1332 }
1333
1334 bufferedBio = BIO_push(bufferedBio, layerBio);
1335 status = BIO_set_nonblock(bufferedBio, TRUE);
1336
1337 if (isProxyConnection)
1338 {
1339 if (!proxy_connect(rdg->context, bufferedBio, proxyUsername, proxyPassword,
1340 settings->GatewayHostname, (UINT16)settings->GatewayPort))
1341 {
1342 BIO_free_all(bufferedBio);
1343 return FALSE;
1344 }
1345 }
1346
1347 if (!status)
1348 {
1349 BIO_free_all(bufferedBio);
1350 return FALSE;
1351 }
1352
1353 tls->hostname = settings->GatewayHostname;
1354 tls->port = WINPR_ASSERTING_INT_CAST(int32_t, MIN(UINT16_MAX, settings->GatewayPort));
1355 tls->isGatewayTransport = TRUE;
1356 status = freerdp_tls_connect(tls, bufferedBio);
1357 if (status < 1)
1358 {
1359 rdpContext* context = rdg->context;
1360 if (status < 0)
1361 {
1362 freerdp_set_last_error_if_not(context, FREERDP_ERROR_TLS_CONNECT_FAILED);
1363 }
1364 else
1365 {
1366 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_CANCELLED);
1367 }
1368
1369 return FALSE;
1370 }
1371 return (status >= 1);
1372}
1373
1374static BOOL rdg_establish_data_connection(rdpRdg* rdg, rdpTls* tls, const char* method,
1375 const char* peerAddress, UINT32 timeout,
1376 BOOL* rpcFallback)
1377{
1378 char buffer[64] = { 0 };
1379 HttpResponse* response = NULL;
1380
1381 if (!rdg_tls_connect(rdg, tls, peerAddress, timeout))
1382 return FALSE;
1383
1384 WINPR_ASSERT(rpcFallback);
1385 if (rdg->context->settings->GatewayHttpExtAuthBearer && rdg->extAuth == HTTP_EXTENDED_AUTH_NONE)
1386 rdg->extAuth = HTTP_EXTENDED_AUTH_BEARER;
1387 if (rdg->extAuth == HTTP_EXTENDED_AUTH_NONE)
1388 {
1389 if (!rdg_auth_init(rdg, tls, AUTH_PKG))
1390 return FALSE;
1391
1392 if (!rdg_send_http_request(rdg, tls, method, TransferEncodingIdentity))
1393 return FALSE;
1394
1395 response = http_response_recv(tls, TRUE);
1396 /* MS RD Gateway seems to just terminate the tls connection without
1397 * sending an answer if it is not happy with the http request */
1398 if (!response)
1399 {
1400 WLog_Print(rdg->log, WLOG_INFO, "RD Gateway HTTP transport broken.");
1401 *rpcFallback = TRUE;
1402 return FALSE;
1403 }
1404
1405 (void)http_response_extract_cookies(response, rdg->http);
1406
1407 const UINT16 StatusCode = http_response_get_status_code(response);
1408 switch (StatusCode)
1409 {
1410 case HTTP_STATUS_GONE:
1411 case HTTP_STATUS_FORBIDDEN:
1412 case HTTP_STATUS_NOT_FOUND:
1413 {
1414 WLog_Print(rdg->log, WLOG_INFO, "RD Gateway does not support HTTP transport.");
1415 http_response_log_error_status(rdg->log, WLOG_DEBUG, response);
1416 *rpcFallback = TRUE;
1417
1418 http_response_free(response);
1419 return FALSE;
1420 }
1421 case HTTP_STATUS_OK:
1422 break;
1423
1424 case HTTP_STATUS_DENIED:
1425 http_response_log_error_status(rdg->log, WLOG_DEBUG, response);
1426 break;
1427
1428 default:
1429 http_response_log_error_status(rdg->log, WLOG_WARN, response);
1430 break;
1431 }
1432
1433 while (!credssp_auth_is_complete(rdg->auth))
1434 {
1435 if (!rdg_recv_auth_token(rdg->log, rdg->auth, response))
1436 {
1437 http_response_free(response);
1438 return FALSE;
1439 }
1440
1441 if (credssp_auth_have_output_token(rdg->auth))
1442 {
1443 http_response_free(response);
1444
1445 if (!rdg_send_http_request(rdg, tls, method, TransferEncodingIdentity))
1446 return FALSE;
1447
1448 response = http_response_recv(tls, TRUE);
1449 if (!response)
1450 {
1451 WLog_Print(rdg->log, WLOG_INFO, "RD Gateway HTTP transport broken.");
1452 *rpcFallback = TRUE;
1453 return FALSE;
1454 }
1455 (void)http_response_extract_cookies(response, rdg->http);
1456 }
1457 }
1458 credssp_auth_free(rdg->auth);
1459 rdg->auth = NULL;
1460 }
1461 else
1462 {
1463 credssp_auth_free(rdg->auth);
1464 rdg->auth = NULL;
1465
1466 if (!rdg_send_http_request(rdg, tls, method, TransferEncodingIdentity))
1467 return FALSE;
1468
1469 response = http_response_recv(tls, TRUE);
1470
1471 if (!response)
1472 {
1473 WLog_Print(rdg->log, WLOG_INFO, "RD Gateway HTTP transport broken.");
1474 *rpcFallback = TRUE;
1475 return FALSE;
1476 }
1477 (void)http_response_extract_cookies(response, rdg->http);
1478 }
1479
1480 const UINT16 statusCode = http_response_get_status_code(response);
1481 const size_t bodyLength = http_response_get_body_length(response);
1482 const TRANSFER_ENCODING encoding = http_response_get_transfer_encoding(response);
1483 const BOOL isWebsocket = http_response_is_websocket(rdg->http, response);
1484
1485 WLog_Print(rdg->log, WLOG_DEBUG, "%s authorization result: %s", method,
1486 freerdp_http_status_string_format(statusCode, buffer, ARRAYSIZE(buffer)));
1487
1488 switch (statusCode)
1489 {
1490 case HTTP_STATUS_OK:
1491 /* old rdg endpoint without websocket support, don't request websocket for RDG_IN_DATA
1492 */
1493 http_context_enable_websocket_upgrade(rdg->http, FALSE);
1494 http_response_free(response);
1495 break;
1496 case HTTP_STATUS_DENIED:
1497 freerdp_set_last_error_log(rdg->context, FREERDP_ERROR_CONNECT_ACCESS_DENIED);
1498 http_response_free(response);
1499 return FALSE;
1500 case HTTP_STATUS_SWITCH_PROTOCOLS:
1501 http_response_free(response);
1502 if (!isWebsocket)
1503 {
1504 /*
1505 * webserver is broken, a fallback may be possible here
1506 * but only if already tested with oppurtonistic upgrade
1507 */
1508 if (http_context_is_websocket_upgrade_enabled(rdg->http))
1509 {
1510 long fd = BIO_get_fd(tls->bio, NULL);
1511 if (fd >= 0)
1512 closesocket((SOCKET)fd);
1513 http_context_enable_websocket_upgrade(rdg->http, FALSE);
1514 return rdg_establish_data_connection(rdg, tls, method, peerAddress, timeout,
1515 rpcFallback);
1516 }
1517 return FALSE;
1518 }
1519
1520 rdg->transferEncoding.isWebsocketTransport = TRUE;
1521 if (!websocket_context_reset(rdg->transferEncoding.context.websocket))
1522 return FALSE;
1523
1524 if (rdg->extAuth == HTTP_EXTENDED_AUTH_SSPI_NTLM)
1525 {
1526 /* create a new auth context for SSPI_NTLM. This must be done after the last
1527 * rdg_send_http_request */
1528 if (!rdg_auth_init(rdg, tls, NTLM_SSP_NAME))
1529 return FALSE;
1530 }
1531 return TRUE;
1532 default:
1533 http_response_log_error_status(rdg->log, WLOG_WARN, response);
1534 http_response_free(response);
1535 return FALSE;
1536 }
1537
1538 if (strcmp(method, "RDG_OUT_DATA") == 0)
1539 {
1540 if (encoding == TransferEncodingChunked)
1541 {
1542 rdg->transferEncoding.httpTransferEncoding = TransferEncodingChunked;
1543 rdg->transferEncoding.context.chunked.nextOffset = 0;
1544 rdg->transferEncoding.context.chunked.headerFooterPos = 0;
1545 rdg->transferEncoding.context.chunked.state = ChunkStateLenghHeader;
1546 }
1547 if (!rdg_skip_seed_payload(rdg->context, tls, bodyLength, &rdg->transferEncoding))
1548 {
1549 return FALSE;
1550 }
1551 }
1552 else
1553 {
1554 if (!rdg_send_http_request(rdg, tls, method, TransferEncodingChunked))
1555 return FALSE;
1556
1557 if (rdg->extAuth == HTTP_EXTENDED_AUTH_SSPI_NTLM)
1558 {
1559 /* create a new auth context for SSPI_NTLM. This must be done after the last
1560 * rdg_send_http_request (RDG_IN_DATA is always after RDG_OUT_DATA) */
1561 if (!rdg_auth_init(rdg, tls, NTLM_SSP_NAME))
1562 return FALSE;
1563 }
1564 }
1565
1566 return TRUE;
1567}
1568
1569static BOOL rdg_tunnel_connect(rdpRdg* rdg)
1570{
1571 BOOL status = 0;
1572 wStream* s = NULL;
1573 rdg_send_handshake(rdg);
1574
1575 while (rdg->state < RDG_CLIENT_STATE_OPENED)
1576 {
1577 status = FALSE;
1578 s = rdg_receive_packet(rdg);
1579
1580 if (s)
1581 {
1582 status = rdg_process_packet(rdg, s);
1583 Stream_Free(s, TRUE);
1584 }
1585
1586 if (!status)
1587 {
1588 WINPR_ASSERT(rdg);
1589 WINPR_ASSERT(rdg->context);
1590 WINPR_ASSERT(rdg->context->rdp);
1591 transport_set_layer(rdg->context->rdp->transport, TRANSPORT_LAYER_CLOSED);
1592 return FALSE;
1593 }
1594 }
1595
1596 return TRUE;
1597}
1598
1599BOOL rdg_connect(rdpRdg* rdg, DWORD timeout, BOOL* rpcFallback)
1600{
1601 BOOL status = 0;
1602 SOCKET outConnSocket = 0;
1603 char* peerAddress = NULL;
1604 BOOL rpcFallbackLocal = FALSE;
1605
1606 WINPR_ASSERT(rdg != NULL);
1607 freerdp_set_last_error(rdg->context, ERROR_SUCCESS);
1608 status = rdg_establish_data_connection(rdg, rdg->tlsOut, "RDG_OUT_DATA", NULL, timeout,
1609 &rpcFallbackLocal);
1610
1611 if (status)
1612 {
1613 if (rdg->transferEncoding.isWebsocketTransport)
1614 {
1615 WLog_Print(rdg->log, WLOG_DEBUG, "Upgraded to websocket. RDG_IN_DATA not required");
1616 }
1617 else
1618 {
1619 /* Establish IN connection with the same peer/server as OUT connection,
1620 * even when server hostname resolves to different IP addresses.
1621 */
1622 BIO_get_socket(rdg->tlsOut->underlying, &outConnSocket);
1623 peerAddress = freerdp_tcp_get_peer_address(outConnSocket);
1624 status = rdg_establish_data_connection(rdg, rdg->tlsIn, "RDG_IN_DATA", peerAddress,
1625 timeout, &rpcFallbackLocal);
1626 free(peerAddress);
1627 }
1628 }
1629
1630 if (rpcFallback)
1631 *rpcFallback = rpcFallbackLocal;
1632
1633 if (!status)
1634 {
1635 WINPR_ASSERT(rdg);
1636 WINPR_ASSERT(rdg->context);
1637 WINPR_ASSERT(rdg->context->rdp);
1638 if (rpcFallbackLocal)
1639 {
1640 http_context_enable_websocket_upgrade(rdg->http, FALSE);
1641 credssp_auth_free(rdg->auth);
1642 rdg->auth = NULL;
1643 }
1644
1645 transport_set_layer(rdg->context->rdp->transport, TRANSPORT_LAYER_CLOSED);
1646 return FALSE;
1647 }
1648
1649 status = rdg_tunnel_connect(rdg);
1650
1651 if (!status)
1652 return FALSE;
1653
1654 return TRUE;
1655}
1656
1657static int rdg_write_websocket_data_packet(rdpRdg* rdg, const BYTE* buf, int isize)
1658{
1659 WINPR_ASSERT(rdg);
1660 if (isize < 0)
1661 return -1;
1662
1663 const size_t payloadSize = (size_t)isize + 10;
1664 union
1665 {
1666 UINT32 u32;
1667 UINT8 u8[4];
1668 } maskingKey;
1669
1670 wStream* sWS =
1671 websocket_context_packet_new(payloadSize, WebsocketBinaryOpcode, &maskingKey.u32);
1672 if (!sWS)
1673 return FALSE;
1674
1675 Stream_Write_UINT16(
1676 sWS, WINPR_ASSERTING_INT_CAST(
1677 uint16_t, PKT_TYPE_DATA ^ (maskingKey.u8[0] | maskingKey.u8[1] << 8))); /* Type */
1678 Stream_Write_UINT16(
1679 sWS, WINPR_ASSERTING_INT_CAST(
1680 uint16_t, 0 ^ (maskingKey.u8[2] | maskingKey.u8[3] << 8))); /* Reserved */
1681 Stream_Write_UINT32(
1682 sWS, WINPR_ASSERTING_INT_CAST(uint32_t, payloadSize ^ maskingKey.u32)); /* Packet length */
1683 Stream_Write_UINT16(
1684 sWS, WINPR_ASSERTING_INT_CAST(
1685 uint16_t, isize ^ (maskingKey.u8[0] | maskingKey.u8[1] << 8))); /* Data size */
1686
1687 /* masking key is now off by 2 bytes. fix that */
1688 maskingKey.u32 = (maskingKey.u32 & 0xffff) << 16 | (maskingKey.u32 >> 16);
1689
1690 WINPR_ASSERT(rdg->tlsOut);
1691 wStream sPacket = { 0 };
1692 Stream_StaticConstInit(&sPacket, buf, (size_t)isize);
1693 if (!websocket_context_mask_and_send(rdg->tlsOut->bio, sWS, &sPacket, maskingKey.u32))
1694 return -1;
1695
1696 return isize;
1697}
1698
1699static int rdg_write_chunked_data_packet(rdpRdg* rdg, const BYTE* buf, int isize)
1700{
1701 int status = 0;
1702 size_t len = 0;
1703 wStream* sChunk = NULL;
1704
1705 if (isize > UINT16_MAX)
1706 return -1;
1707
1708 const size_t size = (size_t)isize;
1709 if (size < 1)
1710 return 0;
1711
1712 const size_t packetSize = size + 10;
1713 char chunkSize[11] = { 0 };
1714 (void)sprintf_s(chunkSize, sizeof(chunkSize), "%" PRIxz "\r\n", packetSize);
1715 sChunk = Stream_New(NULL, strnlen(chunkSize, sizeof(chunkSize)) + packetSize + 2);
1716
1717 if (!sChunk)
1718 return -1;
1719
1720 Stream_Write(sChunk, chunkSize, strnlen(chunkSize, sizeof(chunkSize)));
1721 Stream_Write_UINT16(sChunk, PKT_TYPE_DATA); /* Type */
1722 Stream_Write_UINT16(sChunk, 0); /* Reserved */
1723 Stream_Write_UINT32(sChunk, (UINT32)packetSize); /* Packet length */
1724 Stream_Write_UINT16(sChunk, (UINT16)size); /* Data size */
1725 Stream_Write(sChunk, buf, size); /* Data */
1726 Stream_Write(sChunk, "\r\n", 2);
1727 Stream_SealLength(sChunk);
1728 len = Stream_Length(sChunk);
1729
1730 status = freerdp_tls_write_all(rdg->tlsIn, Stream_Buffer(sChunk), len);
1731 Stream_Free(sChunk, TRUE);
1732
1733 if (status < 0)
1734 return -1;
1735
1736 return (int)size;
1737}
1738
1739static int rdg_write_data_packet(rdpRdg* rdg, const BYTE* buf, int isize)
1740{
1741 WINPR_ASSERT(rdg);
1742 if (rdg->transferEncoding.isWebsocketTransport)
1743 return rdg_write_websocket_data_packet(rdg, buf, isize);
1744 else
1745 return rdg_write_chunked_data_packet(rdg, buf, isize);
1746}
1747
1748static BOOL rdg_process_close_packet(rdpRdg* rdg, wStream* s)
1749{
1750 int status = -1;
1751 wStream* sClose = NULL;
1752 UINT32 errorCode = 0;
1753 UINT32 packetSize = 12;
1754
1755 /* Read error code */
1756 if (!Stream_CheckAndLogRequiredLengthWLog(rdg->log, s, 4))
1757 return FALSE;
1758 Stream_Read_UINT32(s, errorCode);
1759
1760 if (errorCode != 0)
1761 freerdp_set_last_error_log(rdg->context, errorCode);
1762
1763 sClose = Stream_New(NULL, packetSize);
1764 if (!sClose)
1765 return FALSE;
1766
1767 Stream_Write_UINT16(sClose, PKT_TYPE_CLOSE_CHANNEL_RESPONSE); /* Type */
1768 Stream_Write_UINT16(sClose, 0); /* Reserved */
1769 Stream_Write_UINT32(sClose, packetSize); /* Packet length */
1770 Stream_Write_UINT32(sClose, 0); /* Status code */
1771 Stream_SealLength(sClose);
1772 status = rdg_write_packet(rdg, sClose);
1773 Stream_Free(sClose, TRUE);
1774
1775 return (status < 0 ? FALSE : TRUE);
1776}
1777
1778static BOOL rdg_process_keep_alive_packet(rdpRdg* rdg)
1779{
1780 int status = -1;
1781 wStream* sKeepAlive = NULL;
1782 size_t packetSize = 8;
1783
1784 sKeepAlive = Stream_New(NULL, packetSize);
1785
1786 if (!sKeepAlive)
1787 return FALSE;
1788
1789 Stream_Write_UINT16(sKeepAlive, PKT_TYPE_KEEPALIVE); /* Type */
1790 Stream_Write_UINT16(sKeepAlive, 0); /* Reserved */
1791 Stream_Write_UINT32(sKeepAlive, (UINT32)packetSize); /* Packet length */
1792 Stream_SealLength(sKeepAlive);
1793 status = rdg_write_packet(rdg, sKeepAlive);
1794 Stream_Free(sKeepAlive, TRUE);
1795
1796 return (status < 0 ? FALSE : TRUE);
1797}
1798
1799static BOOL rdg_process_service_message(rdpRdg* rdg, wStream* s)
1800{
1801 const WCHAR* msg = NULL;
1802 UINT16 msgLenBytes = 0;
1803 rdpContext* context = rdg->context;
1804 WINPR_ASSERT(context);
1805 WINPR_ASSERT(context->instance);
1806
1807 /* Read message string */
1808 if (!rdg_read_http_unicode_string(rdg->log, s, &msg, &msgLenBytes))
1809 {
1810 WLog_Print(rdg->log, WLOG_ERROR, "Failed to read string");
1811 return FALSE;
1812 }
1813
1814 return IFCALLRESULT(TRUE, context->instance->PresentGatewayMessage, context->instance,
1815 GATEWAY_MESSAGE_SERVICE, TRUE, FALSE, msgLenBytes, msg);
1816}
1817
1818static BOOL rdg_process_unknown_packet(rdpRdg* rdg, int type)
1819{
1820 WINPR_UNUSED(rdg);
1821 WINPR_UNUSED(type);
1822 WLog_Print(rdg->log, WLOG_WARN, "Unknown Control Packet received: %" PRIX32,
1823 WINPR_CXX_COMPAT_CAST(UINT32, type));
1824 return TRUE;
1825}
1826
1827static BOOL rdg_process_control_packet(rdpRdg* rdg, int type, size_t packetLength)
1828{
1829 wStream* s = NULL;
1830 size_t readCount = 0;
1831 int status = 0;
1832 size_t payloadSize = packetLength - sizeof(RdgPacketHeader);
1833
1834 if (packetLength < sizeof(RdgPacketHeader))
1835 return FALSE;
1836
1837 // NOLINTNEXTLINE(bugprone-sizeof-expression)
1838 WINPR_ASSERT(sizeof(RdgPacketHeader) < INT_MAX);
1839
1840 if (payloadSize)
1841 {
1842 s = Stream_New(NULL, payloadSize);
1843
1844 if (!s)
1845 return FALSE;
1846
1847 while (readCount < payloadSize)
1848 {
1849 if (rdg_shall_abort(rdg))
1850 {
1851 Stream_Free(s, TRUE);
1852 return FALSE;
1853 }
1854 status = rdg_socket_read(rdg->tlsOut->bio, Stream_Pointer(s), payloadSize - readCount,
1855 &rdg->transferEncoding);
1856
1857 if (status <= 0)
1858 {
1859 if (!BIO_should_retry(rdg->tlsOut->bio))
1860 {
1861 Stream_Free(s, TRUE);
1862 return FALSE;
1863 }
1864
1865 continue;
1866 }
1867
1868 Stream_Seek(s, (size_t)status);
1869 readCount += (size_t)status;
1870
1871 if (readCount > INT_MAX)
1872 {
1873 Stream_Free(s, TRUE);
1874 return FALSE;
1875 }
1876 }
1877
1878 Stream_SetPosition(s, 0);
1879 }
1880
1881 switch (type)
1882 {
1883 case PKT_TYPE_CLOSE_CHANNEL:
1884 EnterCriticalSection(&rdg->writeSection);
1885 status = rdg_process_close_packet(rdg, s);
1886 LeaveCriticalSection(&rdg->writeSection);
1887 break;
1888
1889 case PKT_TYPE_KEEPALIVE:
1890 EnterCriticalSection(&rdg->writeSection);
1891 status = rdg_process_keep_alive_packet(rdg);
1892 LeaveCriticalSection(&rdg->writeSection);
1893 break;
1894
1895 case PKT_TYPE_SERVICE_MESSAGE:
1896 if (!s)
1897 {
1898 WLog_Print(rdg->log, WLOG_ERROR,
1899 "PKT_TYPE_SERVICE_MESSAGE requires payload but none was sent");
1900 return FALSE;
1901 }
1902 status = rdg_process_service_message(rdg, s);
1903 break;
1904
1905 case PKT_TYPE_REAUTH_MESSAGE:
1906 default:
1907 status = rdg_process_unknown_packet(rdg, type);
1908 break;
1909 }
1910
1911 Stream_Free(s, TRUE);
1912 return status;
1913}
1914
1915static int rdg_read_data_packet(rdpRdg* rdg, BYTE* buffer, size_t size)
1916{
1917 RdgPacketHeader header = { 0 };
1918 size_t readCount = 0;
1919 size_t readSize = 0;
1920 int status = 0;
1921
1922 if (!rdg->packetRemainingCount)
1923 {
1924 // NOLINTNEXTLINE(bugprone-sizeof-expression)
1925 WINPR_ASSERT(sizeof(RdgPacketHeader) < INT_MAX);
1926
1927 while (readCount < sizeof(RdgPacketHeader))
1928 {
1929 if (rdg_shall_abort(rdg))
1930 return -1;
1931
1932 status = rdg_socket_read(rdg->tlsOut->bio, (BYTE*)(&header) + readCount,
1933 sizeof(RdgPacketHeader) - readCount, &rdg->transferEncoding);
1934
1935 if (status <= 0)
1936 {
1937 if (!BIO_should_retry(rdg->tlsOut->bio))
1938 return -1;
1939
1940 if (!readCount)
1941 return 0;
1942
1943 BIO_wait_read(rdg->tlsOut->bio, 50);
1944 continue;
1945 }
1946
1947 readCount += (size_t)status;
1948
1949 if (readCount > INT_MAX)
1950 return -1;
1951 }
1952
1953 if (header.type != PKT_TYPE_DATA)
1954 {
1955 status = rdg_process_control_packet(rdg, header.type, header.packetLength);
1956
1957 if (!status)
1958 return -1;
1959
1960 return 0;
1961 }
1962
1963 readCount = 0;
1964
1965 while (readCount < 2)
1966 {
1967 if (rdg_shall_abort(rdg))
1968 return -1;
1969 status =
1970 rdg_socket_read(rdg->tlsOut->bio, (BYTE*)(&rdg->packetRemainingCount) + readCount,
1971 2 - readCount, &rdg->transferEncoding);
1972
1973 if (status < 0)
1974 {
1975 if (!BIO_should_retry(rdg->tlsOut->bio))
1976 return -1;
1977
1978 BIO_wait_read(rdg->tlsOut->bio, 50);
1979 continue;
1980 }
1981
1982 readCount += (size_t)status;
1983 }
1984 }
1985
1986 readSize = (rdg->packetRemainingCount < size) ? rdg->packetRemainingCount : size;
1987 status = rdg_socket_read(rdg->tlsOut->bio, buffer, readSize, &rdg->transferEncoding);
1988
1989 if (status <= 0)
1990 {
1991 if (!BIO_should_retry(rdg->tlsOut->bio))
1992 {
1993 return -1;
1994 }
1995
1996 return 0;
1997 }
1998
1999 rdg->packetRemainingCount -= status;
2000 return status;
2001}
2002
2003static int rdg_bio_write(BIO* bio, const char* buf, int num)
2004{
2005 int status = 0;
2006 rdpRdg* rdg = (rdpRdg*)BIO_get_data(bio);
2007 if (num < 0)
2008 return num;
2009
2010 BIO_clear_flags(bio, BIO_FLAGS_WRITE);
2011 EnterCriticalSection(&rdg->writeSection);
2012 status = rdg_write_data_packet(rdg, (const BYTE*)buf, num);
2013 LeaveCriticalSection(&rdg->writeSection);
2014
2015 if (status < 0)
2016 {
2017 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
2018 return -1;
2019 }
2020 else if (status < num)
2021 {
2022 BIO_set_flags(bio, BIO_FLAGS_WRITE);
2023 WSASetLastError(WSAEWOULDBLOCK);
2024 }
2025 else
2026 {
2027 BIO_set_flags(bio, BIO_FLAGS_WRITE);
2028 }
2029
2030 return status;
2031}
2032
2033static int rdg_bio_read(BIO* bio, char* buf, int size)
2034{
2035 int status = 0;
2036 rdpRdg* rdg = (rdpRdg*)BIO_get_data(bio);
2037 if (size < 0)
2038 return size;
2039 status = rdg_read_data_packet(rdg, (BYTE*)buf, (size_t)size);
2040
2041 if (status < 0)
2042 {
2043 BIO_clear_retry_flags(bio);
2044 return -1;
2045 }
2046 else if (status == 0)
2047 {
2048 BIO_set_retry_read(bio);
2049 WSASetLastError(WSAEWOULDBLOCK);
2050 return -1;
2051 }
2052 else
2053 {
2054 BIO_set_flags(bio, BIO_FLAGS_READ);
2055 }
2056
2057 return status;
2058}
2059
2060static int rdg_bio_puts(BIO* bio, const char* str)
2061{
2062 WINPR_UNUSED(bio);
2063 WINPR_UNUSED(str);
2064 return -2;
2065}
2066
2067// NOLINTNEXTLINE(readability-non-const-parameter)
2068static int rdg_bio_gets(BIO* bio, char* str, int size)
2069{
2070 WINPR_UNUSED(bio);
2071 WINPR_UNUSED(str);
2072 WINPR_UNUSED(size);
2073 return -2;
2074}
2075
2076static long rdg_bio_ctrl(BIO* in_bio, int cmd, long arg1, void* arg2)
2077{
2078 long status = -1;
2079 rdpRdg* rdg = (rdpRdg*)BIO_get_data(in_bio);
2080 rdpTls* tlsOut = rdg->tlsOut;
2081 rdpTls* tlsIn = rdg->tlsIn;
2082
2083 if (cmd == BIO_CTRL_FLUSH)
2084 {
2085 (void)BIO_flush(tlsOut->bio);
2086 if (!rdg->transferEncoding.isWebsocketTransport)
2087 (void)BIO_flush(tlsIn->bio);
2088 status = 1;
2089 }
2090 else if (cmd == BIO_C_SET_NONBLOCK)
2091 {
2092 status = 1;
2093 }
2094 else if (cmd == BIO_C_READ_BLOCKED)
2095 {
2096 BIO* cbio = tlsOut->bio;
2097 status = BIO_read_blocked(cbio);
2098 }
2099 else if (cmd == BIO_C_WRITE_BLOCKED)
2100 {
2101 BIO* cbio = tlsIn->bio;
2102
2103 if (rdg->transferEncoding.isWebsocketTransport)
2104 cbio = tlsOut->bio;
2105
2106 status = BIO_write_blocked(cbio);
2107 }
2108 else if (cmd == BIO_C_WAIT_READ)
2109 {
2110 int timeout = (int)arg1;
2111 BIO* cbio = tlsOut->bio;
2112
2113 if (BIO_read_blocked(cbio))
2114 return BIO_wait_read(cbio, timeout);
2115 else if (BIO_write_blocked(cbio))
2116 return BIO_wait_write(cbio, timeout);
2117 else
2118 status = 1;
2119 }
2120 else if (cmd == BIO_C_WAIT_WRITE)
2121 {
2122 int timeout = (int)arg1;
2123 BIO* cbio = tlsIn->bio;
2124
2125 if (rdg->transferEncoding.isWebsocketTransport)
2126 cbio = tlsOut->bio;
2127
2128 if (BIO_write_blocked(cbio))
2129 status = BIO_wait_write(cbio, timeout);
2130 else if (BIO_read_blocked(cbio))
2131 status = BIO_wait_read(cbio, timeout);
2132 else
2133 status = 1;
2134 }
2135 else if (cmd == BIO_C_GET_EVENT || cmd == BIO_C_GET_FD)
2136 {
2137 /*
2138 * A note about BIO_C_GET_FD:
2139 * Even if two FDs are part of RDG, only one FD can be returned here.
2140 *
2141 * In FreeRDP, BIO FDs are only used for polling, so it is safe to use the outgoing FD only
2142 *
2143 * See issue #3602
2144 */
2145 status = BIO_ctrl(tlsOut->bio, cmd, arg1, arg2);
2146 }
2147#if OPENSSL_VERSION_NUMBER >= 0x30000000L
2148 else if (cmd == BIO_CTRL_GET_KTLS_SEND)
2149 {
2150 /* Even though BIO_get_ktls_send says that returning negative values is valid
2151 * openssl internal sources are full of if(!BIO_get_ktls_send && ) stuff. This has some
2152 * nasty sideeffects. return 0 as proper no KTLS offloading flag
2153 */
2154 status = 0;
2155 }
2156 else if (cmd == BIO_CTRL_GET_KTLS_RECV)
2157 {
2158 /* Even though BIO_get_ktls_recv says that returning negative values is valid
2159 * there is no reason to trust trust negative values are implemented right everywhere
2160 */
2161 status = 0;
2162 }
2163#endif
2164 return status;
2165}
2166
2167static int rdg_bio_new(BIO* bio)
2168{
2169 BIO_set_init(bio, 1);
2170 BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
2171 return 1;
2172}
2173
2174static int rdg_bio_free(BIO* bio)
2175{
2176 WINPR_UNUSED(bio);
2177 return 1;
2178}
2179
2180static BIO_METHOD* BIO_s_rdg(void)
2181{
2182 static BIO_METHOD* bio_methods = NULL;
2183
2184 if (bio_methods == NULL)
2185 {
2186 if (!(bio_methods = BIO_meth_new(BIO_TYPE_TSG, "RDGateway")))
2187 return NULL;
2188
2189 BIO_meth_set_write(bio_methods, rdg_bio_write);
2190 BIO_meth_set_read(bio_methods, rdg_bio_read);
2191 BIO_meth_set_puts(bio_methods, rdg_bio_puts);
2192 BIO_meth_set_gets(bio_methods, rdg_bio_gets);
2193 BIO_meth_set_ctrl(bio_methods, rdg_bio_ctrl);
2194 BIO_meth_set_create(bio_methods, rdg_bio_new);
2195 BIO_meth_set_destroy(bio_methods, rdg_bio_free);
2196 }
2197
2198 return bio_methods;
2199}
2200
2201rdpRdg* rdg_new(rdpContext* context)
2202{
2203 if (!context)
2204 return NULL;
2205
2206 rdpRdg* rdg = (rdpRdg*)calloc(1, sizeof(rdpRdg));
2207 if (!rdg)
2208 return NULL;
2209
2210 rdg->log = WLog_Get(TAG);
2211 rdg->state = RDG_CLIENT_STATE_INITIAL;
2212 rdg->context = context;
2213 rdg->extAuth =
2214 (rdg->context->settings->GatewayHttpExtAuthSspiNtlm ? HTTP_EXTENDED_AUTH_SSPI_NTLM
2215 : HTTP_EXTENDED_AUTH_NONE);
2216
2217 if (rdg->context->settings->GatewayAccessToken)
2218 rdg->extAuth = HTTP_EXTENDED_AUTH_PAA;
2219
2220 UuidCreate(&rdg->guid);
2221
2222 rdg->tlsOut = freerdp_tls_new(rdg->context);
2223
2224 if (!rdg->tlsOut)
2225 goto rdg_alloc_error;
2226
2227 rdg->tlsIn = freerdp_tls_new(rdg->context);
2228
2229 if (!rdg->tlsIn)
2230 goto rdg_alloc_error;
2231
2232 rdg->http = http_context_new();
2233
2234 if (!rdg->http)
2235 goto rdg_alloc_error;
2236
2237 if (!http_context_set_uri(rdg->http, "/remoteDesktopGateway/") ||
2238 !http_context_set_accept(rdg->http, "*/*") ||
2239 !http_context_set_cache_control(rdg->http, "no-cache") ||
2240 !http_context_set_pragma(rdg->http, "no-cache") ||
2241 !http_context_set_connection(rdg->http, "Keep-Alive") ||
2242 !http_context_set_user_agent(rdg->http, "MS-RDGateway/1.0") ||
2243 !http_context_set_host(rdg->http, rdg->context->settings->GatewayHostname) ||
2244 !http_context_set_rdg_connection_id(rdg->http, &rdg->guid) ||
2245 !http_context_set_rdg_correlation_id(rdg->http, &rdg->guid) ||
2246 !http_context_enable_websocket_upgrade(
2247 rdg->http,
2248 freerdp_settings_get_bool(rdg->context->settings, FreeRDP_GatewayHttpUseWebsockets)))
2249 {
2250 goto rdg_alloc_error;
2251 }
2252
2253 if (rdg->extAuth != HTTP_EXTENDED_AUTH_NONE)
2254 {
2255 switch (rdg->extAuth)
2256 {
2257 case HTTP_EXTENDED_AUTH_PAA:
2258 if (!http_context_set_rdg_auth_scheme(rdg->http, "PAA"))
2259 goto rdg_alloc_error;
2260
2261 break;
2262
2263 case HTTP_EXTENDED_AUTH_SSPI_NTLM:
2264 if (!http_context_set_rdg_auth_scheme(rdg->http, "SSPI_NTLM"))
2265 goto rdg_alloc_error;
2266
2267 break;
2268
2269 default:
2270 WLog_Print(rdg->log, WLOG_DEBUG,
2271 "RDG extended authentication method %d not supported", rdg->extAuth);
2272 }
2273 }
2274
2275 rdg->frontBio = BIO_new(BIO_s_rdg());
2276
2277 if (!rdg->frontBio)
2278 goto rdg_alloc_error;
2279
2280 BIO_set_data(rdg->frontBio, rdg);
2281 InitializeCriticalSection(&rdg->writeSection);
2282
2283 rdg->transferEncoding.httpTransferEncoding = TransferEncodingIdentity;
2284 rdg->transferEncoding.isWebsocketTransport = FALSE;
2285
2286 rdg->transferEncoding.context.websocket = websocket_context_new();
2287 if (!rdg->transferEncoding.context.websocket)
2288 goto rdg_alloc_error;
2289
2290 return rdg;
2291rdg_alloc_error:
2292 WINPR_PRAGMA_DIAG_PUSH
2293 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2294 rdg_free(rdg);
2295 WINPR_PRAGMA_DIAG_POP
2296 return NULL;
2297}
2298
2299void rdg_free(rdpRdg* rdg)
2300{
2301 if (!rdg)
2302 return;
2303
2304 freerdp_tls_free(rdg->tlsOut);
2305 freerdp_tls_free(rdg->tlsIn);
2306 http_context_free(rdg->http);
2307 credssp_auth_free(rdg->auth);
2308
2309 if (!rdg->attached)
2310 BIO_free_all(rdg->frontBio);
2311
2312 DeleteCriticalSection(&rdg->writeSection);
2313
2314 smartcardCertInfo_Free(rdg->smartcard);
2315
2316 websocket_context_free(rdg->transferEncoding.context.websocket);
2317
2318 free(rdg);
2319}
2320
2321BIO* rdg_get_front_bio_and_take_ownership(rdpRdg* rdg)
2322{
2323 if (!rdg)
2324 return NULL;
2325
2326 rdg->attached = TRUE;
2327 return rdg->frontBio;
2328}
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API WCHAR * freerdp_settings_get_string_as_utf16(const rdpSettings *settings, FreeRDP_Settings_Keys_String id, size_t *pCharLen)
Return an allocated UTF16 string.
Definition wtypes.h:254