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