23 #include <freerdp/config.h>
25 #include <winpr/crt.h>
26 #include <winpr/assert.h>
27 #include <winpr/stream.h>
29 #include <freerdp/log.h>
36 #include "transport.h"
38 #define TAG FREERDP_TAG("core.nego")
47 DWORD RoutingTokenLength;
48 BOOL SendPreconnectionPdu;
49 UINT32 PreconnectionId;
50 const char* PreconnectionBlob;
54 BOOL SecurityConnected;
55 UINT32 CookieMaxLength;
58 UINT32 SelectedProtocol;
59 UINT32 RequestedProtocols;
60 BOOL NegotiateSecurityLayer;
61 BOOL EnabledProtocols[32];
62 BOOL RestrictedAdminModeRequired;
63 BOOL RemoteCredsGuardRequired;
64 BOOL RemoteCredsGuardActive;
65 BOOL RemoteCredsGuardSupported;
67 BOOL GatewayBypassLocal;
68 BOOL ConnectChildSession;
70 rdpTransport* transport;
73 static const char* nego_state_string(NEGO_STATE state)
75 static const char*
const NEGO_STATE_STRINGS[] = {
"NEGO_STATE_INITIAL",
"NEGO_STATE_RDSTLS",
76 "NEGO_STATE_AAD",
"NEGO_STATE_EXT",
77 "NEGO_STATE_NLA",
"NEGO_STATE_TLS",
78 "NEGO_STATE_RDP",
"NEGO_STATE_FAIL",
79 "NEGO_STATE_FINAL",
"NEGO_STATE_INVALID" };
80 if (state >= ARRAYSIZE(NEGO_STATE_STRINGS))
81 return NEGO_STATE_STRINGS[ARRAYSIZE(NEGO_STATE_STRINGS) - 1];
82 return NEGO_STATE_STRINGS[state];
85 static BOOL nego_tcp_connect(rdpNego* nego);
86 static BOOL nego_transport_connect(rdpNego* nego);
87 static BOOL nego_transport_disconnect(rdpNego* nego);
88 static BOOL nego_security_connect(rdpNego* nego);
89 static BOOL nego_send_preconnection_pdu(rdpNego* nego);
90 static BOOL nego_recv_response(rdpNego* nego);
91 static void nego_send(rdpNego* nego);
92 static BOOL nego_process_negotiation_request(rdpNego* nego,
wStream* s);
93 static BOOL nego_process_negotiation_response(rdpNego* nego,
wStream* s);
94 static BOOL nego_process_negotiation_failure(rdpNego* nego,
wStream* s);
96 BOOL nego_update_settings_from_state(rdpNego* nego, rdpSettings* settings)
102 nego->RequestedProtocols) &&
104 nego->SelectedProtocol) &&
116 BOOL nego_connect(rdpNego* nego)
118 rdpContext* context = NULL;
119 rdpSettings* settings = NULL;
121 context = transport_get_context(nego->transport);
122 WINPR_ASSERT(context);
123 settings = context->settings;
124 WINPR_ASSERT(settings);
126 if (nego_get_state(nego) == NEGO_STATE_INITIAL)
128 if (nego->EnabledProtocols[PROTOCOL_RDSAAD])
130 nego_set_state(nego, NEGO_STATE_AAD);
132 else if (nego->EnabledProtocols[PROTOCOL_RDSTLS])
134 nego_set_state(nego, NEGO_STATE_RDSTLS);
136 else if (nego->EnabledProtocols[PROTOCOL_HYBRID_EX])
138 nego_set_state(nego, NEGO_STATE_EXT);
140 else if (nego->EnabledProtocols[PROTOCOL_HYBRID])
142 nego_set_state(nego, NEGO_STATE_NLA);
144 else if (nego->EnabledProtocols[PROTOCOL_SSL])
146 nego_set_state(nego, NEGO_STATE_TLS);
148 else if (nego->EnabledProtocols[PROTOCOL_RDP])
150 nego_set_state(nego, NEGO_STATE_RDP);
154 WLog_ERR(TAG,
"No security protocol is enabled");
155 nego_set_state(nego, NEGO_STATE_FAIL);
159 if (!nego->NegotiateSecurityLayer)
161 WLog_DBG(TAG,
"Security Layer Negotiation is disabled");
163 nego->EnabledProtocols[PROTOCOL_RDSAAD] = FALSE;
164 nego->EnabledProtocols[PROTOCOL_HYBRID] = FALSE;
165 nego->EnabledProtocols[PROTOCOL_SSL] = FALSE;
166 nego->EnabledProtocols[PROTOCOL_RDP] = FALSE;
167 nego->EnabledProtocols[PROTOCOL_HYBRID_EX] = FALSE;
168 nego->EnabledProtocols[PROTOCOL_RDSTLS] = FALSE;
170 UINT32 SelectedProtocol = 0;
171 switch (nego_get_state(nego))
174 nego->EnabledProtocols[PROTOCOL_RDSAAD] = TRUE;
175 SelectedProtocol = PROTOCOL_RDSAAD;
177 case NEGO_STATE_RDSTLS:
178 nego->EnabledProtocols[PROTOCOL_RDSTLS] = TRUE;
179 SelectedProtocol = PROTOCOL_RDSTLS;
182 nego->EnabledProtocols[PROTOCOL_HYBRID_EX] = TRUE;
183 nego->EnabledProtocols[PROTOCOL_HYBRID] = TRUE;
184 SelectedProtocol = PROTOCOL_HYBRID_EX;
187 nego->EnabledProtocols[PROTOCOL_HYBRID] = TRUE;
188 SelectedProtocol = PROTOCOL_HYBRID;
191 nego->EnabledProtocols[PROTOCOL_SSL] = TRUE;
192 SelectedProtocol = PROTOCOL_SSL;
195 nego->EnabledProtocols[PROTOCOL_RDP] = TRUE;
196 SelectedProtocol = PROTOCOL_RDP;
199 WLog_ERR(TAG,
"Invalid NEGO state 0x%08" PRIx32, nego_get_state(nego));
202 if (!nego_set_selected_protocol(nego, SelectedProtocol))
206 if (!nego_tcp_connect(nego))
208 WLog_ERR(TAG,
"Failed to connect");
212 if (nego->SendPreconnectionPdu)
214 if (!nego_send_preconnection_pdu(nego))
216 WLog_ERR(TAG,
"Failed to send preconnection pdu");
217 nego_set_state(nego, NEGO_STATE_FINAL);
223 if (!nego->NegotiateSecurityLayer)
225 nego_set_state(nego, NEGO_STATE_FINAL);
231 WLog_DBG(TAG,
"state: %s", nego_state_string(nego_get_state(nego)));
234 if (nego_get_state(nego) == NEGO_STATE_FAIL)
236 if (freerdp_get_last_error(transport_get_context(nego->transport)) ==
237 FREERDP_ERROR_SUCCESS)
238 WLog_ERR(TAG,
"Protocol Security Negotiation Failure");
240 nego_set_state(nego, NEGO_STATE_FINAL);
243 }
while (nego_get_state(nego) != NEGO_STATE_FINAL);
247 char buffer[64] = { 0 };
248 WLog_DBG(TAG,
"Negotiated %s security",
249 nego_protocol_to_str(nego->SelectedProtocol, buffer,
sizeof(buffer)));
253 if (!nego_update_settings_from_state(nego, settings))
256 if (nego->SelectedProtocol == PROTOCOL_RDP)
268 ENCRYPTION_METHOD_40BIT | ENCRYPTION_METHOD_56BIT |
269 ENCRYPTION_METHOD_128BIT | ENCRYPTION_METHOD_FIPS))
275 if (!nego_security_connect(nego))
277 char buffer[64] = { 0 };
278 WLog_DBG(TAG,
"Failed to connect with %s security",
279 nego_protocol_to_str(nego->SelectedProtocol, buffer,
sizeof(buffer)));
286 BOOL nego_disconnect(rdpNego* nego)
289 nego_set_state(nego, NEGO_STATE_INITIAL);
290 return nego_transport_disconnect(nego);
293 static BOOL nego_try_connect(rdpNego* nego)
297 switch (nego->SelectedProtocol)
299 case PROTOCOL_RDSAAD:
300 WLog_DBG(TAG,
"nego_security_connect with PROTOCOL_RDSAAD");
301 nego->SecurityConnected = transport_connect_aad(nego->transport);
303 case PROTOCOL_RDSTLS:
304 WLog_DBG(TAG,
"nego_security_connect with PROTOCOL_RDSTLS");
305 nego->SecurityConnected = transport_connect_rdstls(nego->transport);
307 case PROTOCOL_HYBRID:
308 WLog_DBG(TAG,
"nego_security_connect with PROTOCOL_HYBRID");
309 nego->SecurityConnected = transport_connect_nla(nego->transport, FALSE);
311 case PROTOCOL_HYBRID_EX:
312 WLog_DBG(TAG,
"nego_security_connect with PROTOCOL_HYBRID_EX");
313 nego->SecurityConnected = transport_connect_nla(nego->transport, TRUE);
316 WLog_DBG(TAG,
"nego_security_connect with PROTOCOL_SSL");
317 nego->SecurityConnected = transport_connect_tls(nego->transport);
320 WLog_DBG(TAG,
"nego_security_connect with PROTOCOL_RDP");
321 nego->SecurityConnected = transport_connect_rdp(nego->transport);
325 "cannot connect security layer because no protocol has been selected yet.");
328 return nego->SecurityConnected;
332 BOOL nego_security_connect(rdpNego* nego)
335 if (!nego->TcpConnected)
337 nego->SecurityConnected = FALSE;
339 else if (!nego->SecurityConnected)
341 if (!nego_try_connect(nego))
345 return nego->SecurityConnected;
348 static BOOL nego_tcp_connect(rdpNego* nego)
350 rdpContext* context = NULL;
352 if (!nego->TcpConnected)
354 UINT32 TcpConnectTimeout = 0;
356 context = transport_get_context(nego->transport);
357 WINPR_ASSERT(context);
362 if (nego->GatewayEnabled)
364 if (nego->GatewayBypassLocal)
368 "Detecting if host can be reached locally. - This might take some time.");
369 WLog_INFO(TAG,
"To disable auto detection use /gateway-usage-method:direct");
370 transport_set_gateway_enabled(nego->transport, FALSE);
371 nego->TcpConnected = transport_connect(nego->transport, nego->hostname, nego->port,
375 if (!nego->TcpConnected)
377 transport_set_gateway_enabled(nego->transport, TRUE);
378 nego->TcpConnected = transport_connect(nego->transport, nego->hostname, nego->port,
382 else if (nego->ConnectChildSession)
384 nego->TcpConnected = transport_connect_childsession(nego->transport);
389 transport_connect(nego->transport, nego->hostname, nego->port, TcpConnectTimeout);
393 return nego->TcpConnected;
404 BOOL nego_transport_connect(rdpNego* nego)
407 if (!nego_tcp_connect(nego))
410 if (nego->TcpConnected && !nego->NegotiateSecurityLayer)
411 return nego_security_connect(nego);
413 return nego->TcpConnected;
424 BOOL nego_transport_disconnect(rdpNego* nego)
427 if (nego->TcpConnected)
428 transport_disconnect(nego->transport);
430 nego->TcpConnected = FALSE;
431 nego->SecurityConnected = FALSE;
443 BOOL nego_send_preconnection_pdu(rdpNego* nego)
448 WCHAR* wszPCB = NULL;
452 WLog_DBG(TAG,
"Sending preconnection PDU");
454 if (!nego_tcp_connect(nego))
458 cbSize = PRECONNECTION_PDU_V2_MIN_SIZE;
460 if (nego->PreconnectionBlob)
463 wszPCB = ConvertUtf8ToWCharAlloc(nego->PreconnectionBlob, &len);
464 if (len > UINT16_MAX - 1)
469 cchPCB = (UINT16)len;
471 cbSize += cchPCB *
sizeof(WCHAR);
474 s = Stream_New(NULL, cbSize);
479 WLog_ERR(TAG,
"Stream_New failed!");
483 Stream_Write_UINT32(s, cbSize);
484 Stream_Write_UINT32(s, 0);
485 Stream_Write_UINT32(s, PRECONNECTION_PDU_V2);
486 Stream_Write_UINT32(s, nego->PreconnectionId);
487 Stream_Write_UINT16(s, cchPCB);
491 Stream_Write(s, wszPCB, cchPCB *
sizeof(WCHAR));
495 Stream_SealLength(s);
497 if (transport_write(nego->transport, s) < 0)
499 Stream_Free(s, TRUE);
503 Stream_Free(s, TRUE);
507 static void nego_attempt_rdstls(rdpNego* nego)
510 nego->RequestedProtocols = PROTOCOL_RDSTLS | PROTOCOL_SSL;
511 WLog_DBG(TAG,
"Attempting RDSTLS security");
513 if (!nego_transport_connect(nego))
515 nego_set_state(nego, NEGO_STATE_FAIL);
519 if (!nego_send_negotiation_request(nego))
521 nego_set_state(nego, NEGO_STATE_FAIL);
525 if (!nego_recv_response(nego))
527 nego_set_state(nego, NEGO_STATE_FAIL);
531 WLog_DBG(TAG,
"state: %s", nego_state_string(nego_get_state(nego)));
533 if (nego_get_state(nego) != NEGO_STATE_FINAL)
535 nego_transport_disconnect(nego);
537 if (nego->EnabledProtocols[PROTOCOL_HYBRID_EX])
538 nego_set_state(nego, NEGO_STATE_EXT);
539 else if (nego->EnabledProtocols[PROTOCOL_HYBRID])
540 nego_set_state(nego, NEGO_STATE_NLA);
541 else if (nego->EnabledProtocols[PROTOCOL_SSL])
542 nego_set_state(nego, NEGO_STATE_TLS);
543 else if (nego->EnabledProtocols[PROTOCOL_RDP])
544 nego_set_state(nego, NEGO_STATE_RDP);
546 nego_set_state(nego, NEGO_STATE_FAIL);
550 static void nego_attempt_rdsaad(rdpNego* nego)
553 nego->RequestedProtocols = PROTOCOL_RDSAAD;
554 WLog_DBG(TAG,
"Attempting RDS AAD Auth security");
556 if (!nego_transport_connect(nego))
558 nego_set_state(nego, NEGO_STATE_FAIL);
562 if (!nego_send_negotiation_request(nego))
564 nego_set_state(nego, NEGO_STATE_FAIL);
568 if (!nego_recv_response(nego))
570 nego_set_state(nego, NEGO_STATE_FAIL);
574 WLog_DBG(TAG,
"state: %s", nego_state_string(nego_get_state(nego)));
576 if (nego_get_state(nego) != NEGO_STATE_FINAL)
578 nego_transport_disconnect(nego);
580 if (nego->EnabledProtocols[PROTOCOL_HYBRID_EX])
581 nego_set_state(nego, NEGO_STATE_EXT);
582 else if (nego->EnabledProtocols[PROTOCOL_HYBRID])
583 nego_set_state(nego, NEGO_STATE_NLA);
584 else if (nego->EnabledProtocols[PROTOCOL_SSL])
585 nego_set_state(nego, NEGO_STATE_TLS);
586 else if (nego->EnabledProtocols[PROTOCOL_RDP])
587 nego_set_state(nego, NEGO_STATE_RDP);
589 nego_set_state(nego, NEGO_STATE_FAIL);
593 static void nego_attempt_ext(rdpNego* nego)
596 nego->RequestedProtocols = PROTOCOL_HYBRID | PROTOCOL_SSL | PROTOCOL_HYBRID_EX;
597 WLog_DBG(TAG,
"Attempting NLA extended security");
599 if (!nego_transport_connect(nego))
601 nego_set_state(nego, NEGO_STATE_FAIL);
605 if (!nego_send_negotiation_request(nego))
607 nego_set_state(nego, NEGO_STATE_FAIL);
611 if (!nego_recv_response(nego))
613 nego_set_state(nego, NEGO_STATE_FAIL);
617 WLog_DBG(TAG,
"state: %s", nego_state_string(nego_get_state(nego)));
619 if (nego_get_state(nego) != NEGO_STATE_FINAL)
621 nego_transport_disconnect(nego);
623 if (nego->EnabledProtocols[PROTOCOL_HYBRID])
624 nego_set_state(nego, NEGO_STATE_NLA);
625 else if (nego->EnabledProtocols[PROTOCOL_SSL])
626 nego_set_state(nego, NEGO_STATE_TLS);
627 else if (nego->EnabledProtocols[PROTOCOL_RDP])
628 nego_set_state(nego, NEGO_STATE_RDP);
630 nego_set_state(nego, NEGO_STATE_FAIL);
634 static void nego_attempt_nla(rdpNego* nego)
637 nego->RequestedProtocols = PROTOCOL_HYBRID | PROTOCOL_SSL;
638 WLog_DBG(TAG,
"Attempting NLA security");
640 if (!nego_transport_connect(nego))
642 nego_set_state(nego, NEGO_STATE_FAIL);
646 if (!nego_send_negotiation_request(nego))
648 nego_set_state(nego, NEGO_STATE_FAIL);
652 if (!nego_recv_response(nego))
654 nego_set_state(nego, NEGO_STATE_FAIL);
658 WLog_DBG(TAG,
"state: %s", nego_state_string(nego_get_state(nego)));
660 if (nego_get_state(nego) != NEGO_STATE_FINAL)
662 nego_transport_disconnect(nego);
664 if (nego->EnabledProtocols[PROTOCOL_SSL])
665 nego_set_state(nego, NEGO_STATE_TLS);
666 else if (nego->EnabledProtocols[PROTOCOL_RDP])
667 nego_set_state(nego, NEGO_STATE_RDP);
669 nego_set_state(nego, NEGO_STATE_FAIL);
673 static void nego_attempt_tls(rdpNego* nego)
676 nego->RequestedProtocols = PROTOCOL_SSL;
677 WLog_DBG(TAG,
"Attempting TLS security");
679 if (!nego_transport_connect(nego))
681 nego_set_state(nego, NEGO_STATE_FAIL);
685 if (!nego_send_negotiation_request(nego))
687 nego_set_state(nego, NEGO_STATE_FAIL);
691 if (!nego_recv_response(nego))
693 nego_set_state(nego, NEGO_STATE_FAIL);
697 if (nego_get_state(nego) != NEGO_STATE_FINAL)
699 nego_transport_disconnect(nego);
701 if (nego->EnabledProtocols[PROTOCOL_RDP])
702 nego_set_state(nego, NEGO_STATE_RDP);
704 nego_set_state(nego, NEGO_STATE_FAIL);
708 static void nego_attempt_rdp(rdpNego* nego)
711 nego->RequestedProtocols = PROTOCOL_RDP;
712 WLog_DBG(TAG,
"Attempting RDP security");
714 if (!nego_transport_connect(nego))
716 nego_set_state(nego, NEGO_STATE_FAIL);
720 if (!nego_send_negotiation_request(nego))
722 nego_set_state(nego, NEGO_STATE_FAIL);
726 if (!nego_recv_response(nego))
728 nego_set_state(nego, NEGO_STATE_FAIL);
741 BOOL nego_recv_response(rdpNego* nego)
747 s = Stream_New(NULL, 1024);
751 WLog_ERR(TAG,
"Stream_New failed!");
755 status = transport_read_pdu(nego->transport, s);
759 Stream_Free(s, TRUE);
763 status = nego_recv(nego->transport, s, nego);
764 Stream_Free(s, TRUE);
783 int nego_recv(rdpTransport* transport,
wStream* s,
void* extra)
788 rdpNego* nego = (rdpNego*)extra;
791 if (!tpkt_read_header(s, &length))
794 if (!tpdu_read_connection_confirm(s, &li, length))
800 Stream_Read_UINT8(s, type);
804 case TYPE_RDP_NEG_RSP:
805 if (!nego_process_negotiation_response(nego, s))
808 char buffer[64] = { 0 };
809 WLog_DBG(TAG,
"selected_protocol: %s",
810 nego_protocol_to_str(nego->SelectedProtocol, buffer,
sizeof(buffer)));
815 if (nego->SelectedProtocol)
817 if ((nego->SelectedProtocol == PROTOCOL_RDSAAD) &&
818 (!nego->EnabledProtocols[PROTOCOL_RDSAAD]))
820 nego_set_state(nego, NEGO_STATE_FAIL);
822 if ((nego->SelectedProtocol == PROTOCOL_HYBRID) &&
823 (!nego->EnabledProtocols[PROTOCOL_HYBRID]))
825 nego_set_state(nego, NEGO_STATE_FAIL);
828 if ((nego->SelectedProtocol == PROTOCOL_SSL) &&
829 (!nego->EnabledProtocols[PROTOCOL_SSL]))
831 nego_set_state(nego, NEGO_STATE_FAIL);
834 else if (!nego->EnabledProtocols[PROTOCOL_RDP])
836 nego_set_state(nego, NEGO_STATE_FAIL);
841 case TYPE_RDP_NEG_FAILURE:
842 if (!nego_process_negotiation_failure(nego, s))
851 WLog_DBG(TAG,
"no rdpNegData");
853 if (!nego->EnabledProtocols[PROTOCOL_RDP])
854 nego_set_state(nego, NEGO_STATE_FAIL);
856 nego_set_state(nego, NEGO_STATE_FINAL);
860 WLog_ERR(TAG,
"invalid negotiation response");
861 nego_set_state(nego, NEGO_STATE_FAIL);
864 if (!tpkt_ensure_stream_consumed(s, length))
874 static BOOL nego_read_request_token_or_cookie(rdpNego* nego,
wStream* s)
890 BOOL isToken = FALSE;
891 size_t remain = Stream_GetRemainingLength(s);
895 const char* str = Stream_ConstPointer(s);
896 const size_t pos = Stream_GetPosition(s);
902 if (memcmp(Stream_ConstPointer(s),
"Cookie: mstshash=", 17) != 0)
904 if (memcmp(Stream_ConstPointer(s),
"Cookie: msts=", 13) != 0)
906 if (memcmp(Stream_ConstPointer(s),
"tsv:", 4) != 0)
908 if (memcmp(Stream_ConstPointer(s),
"mth://", 6) != 0)
926 while (Stream_GetRemainingLength(s) >= 2)
928 Stream_Read_UINT16(s, crlf);
939 const size_t len = Stream_GetPosition(s) - pos;
940 Stream_Write_UINT16(s, 0);
942 if (len > UINT32_MAX)
945 if (strnlen(str, len) == len)
948 result = nego_set_routing_token(nego, str, (UINT32)len);
950 result = nego_set_cookie(nego, str);
956 Stream_SetPosition(s, pos);
957 WLog_ERR(TAG,
"invalid %s received", isToken ?
"routing token" :
"cookie");
961 WLog_DBG(TAG,
"received %s [%s]", isToken ?
"routing token" :
"cookie", str);
976 BOOL nego_read_request(rdpNego* nego,
wStream* s)
985 if (!tpkt_read_header(s, &length))
988 if (!tpdu_read_connection_request(s, &li, length))
991 if (li != Stream_GetRemainingLength(s) + 6)
993 WLog_ERR(TAG,
"Incorrect TPDU length indicator.");
997 if (!nego_read_request_token_or_cookie(nego, s))
999 WLog_ERR(TAG,
"Failed to parse routing token or cookie.");
1003 if (Stream_GetRemainingLength(s) >= 8)
1006 Stream_Read_UINT8(s, type);
1008 if (type != TYPE_RDP_NEG_REQ)
1010 WLog_ERR(TAG,
"Incorrect negotiation request type %" PRIu8
"", type);
1014 if (!nego_process_negotiation_request(nego, s))
1018 return tpkt_ensure_stream_consumed(s, length);
1027 void nego_send(rdpNego* nego)
1031 switch (nego_get_state(nego))
1033 case NEGO_STATE_AAD:
1034 nego_attempt_rdsaad(nego);
1036 case NEGO_STATE_RDSTLS:
1037 nego_attempt_rdstls(nego);
1039 case NEGO_STATE_EXT:
1040 nego_attempt_ext(nego);
1042 case NEGO_STATE_NLA:
1043 nego_attempt_nla(nego);
1045 case NEGO_STATE_TLS:
1046 nego_attempt_tls(nego);
1048 case NEGO_STATE_RDP:
1049 nego_attempt_rdp(nego);
1052 WLog_ERR(TAG,
"invalid negotiation state for sending");
1067 BOOL nego_send_negotiation_request(rdpNego* nego)
1075 size_t cookie_length = 0;
1076 s = Stream_New(NULL, 512);
1081 WLog_ERR(TAG,
"Stream_New failed!");
1085 length = TPDU_CONNECTION_REQUEST_LENGTH;
1086 bm = Stream_GetPosition(s);
1087 Stream_Seek(s, length);
1089 if (nego->RoutingToken)
1091 Stream_Write(s, nego->RoutingToken, nego->RoutingTokenLength);
1095 if ((nego->RoutingTokenLength > 2) &&
1096 (nego->RoutingToken[nego->RoutingTokenLength - 2] == 0x0D) &&
1097 (nego->RoutingToken[nego->RoutingTokenLength - 1] == 0x0A))
1099 WLog_DBG(TAG,
"Routing token looks correctly terminated - use verbatim");
1100 length += nego->RoutingTokenLength;
1104 WLog_DBG(TAG,
"Adding terminating CRLF to routing token");
1105 Stream_Write_UINT8(s, 0x0D);
1106 Stream_Write_UINT8(s, 0x0A);
1107 length += nego->RoutingTokenLength + 2;
1110 else if (nego->cookie)
1112 cookie_length = strlen(nego->cookie);
1114 if (cookie_length > nego->CookieMaxLength)
1115 cookie_length = nego->CookieMaxLength;
1117 Stream_Write(s,
"Cookie: mstshash=", 17);
1118 Stream_Write(s, (BYTE*)nego->cookie, cookie_length);
1119 Stream_Write_UINT8(s, 0x0D);
1120 Stream_Write_UINT8(s, 0x0A);
1121 length += cookie_length + 19;
1125 char buffer[64] = { 0 };
1126 WLog_DBG(TAG,
"RequestedProtocols: %s",
1127 nego_protocol_to_str(nego->RequestedProtocols, buffer,
sizeof(buffer)));
1130 if ((nego->RequestedProtocols > PROTOCOL_RDP) || (nego->sendNegoData))
1133 if (nego->RestrictedAdminModeRequired)
1134 flags |= RESTRICTED_ADMIN_MODE_REQUIRED;
1136 if (nego->RemoteCredsGuardRequired)
1137 flags |= REDIRECTED_AUTHENTICATION_MODE_REQUIRED;
1139 Stream_Write_UINT8(s, TYPE_RDP_NEG_REQ);
1140 Stream_Write_UINT8(s, flags);
1141 Stream_Write_UINT16(s, 8);
1142 Stream_Write_UINT32(s, nego->RequestedProtocols);
1146 if (length > UINT16_MAX)
1149 em = Stream_GetPosition(s);
1150 Stream_SetPosition(s, bm);
1151 if (!tpkt_write_header(s, (UINT16)length))
1153 if (!tpdu_write_connection_request(s, (UINT16)length - 5))
1155 Stream_SetPosition(s, em);
1156 Stream_SealLength(s);
1157 rc = (transport_write(nego->transport, s) >= 0);
1159 Stream_Free(s, TRUE);
1163 static BOOL nego_process_correlation_info(rdpNego* nego,
wStream* s)
1168 BYTE correlationId[16] = { 0 };
1170 if (!Stream_CheckAndLogRequiredLength(TAG, s, 36))
1172 WLog_ERR(TAG,
"RDP_NEG_REQ::flags CORRELATION_INFO_PRESENT but data is missing");
1176 Stream_Read_UINT8(s, type);
1177 if (type != TYPE_RDP_CORRELATION_INFO)
1179 WLog_ERR(TAG,
"(RDP_NEG_CORRELATION_INFO::type != TYPE_RDP_CORRELATION_INFO");
1182 Stream_Read_UINT8(s, flags);
1185 WLog_ERR(TAG,
"(RDP_NEG_CORRELATION_INFO::flags != 0");
1188 Stream_Read_UINT16(s, length);
1191 WLog_ERR(TAG,
"(RDP_NEG_CORRELATION_INFO::length != 36");
1195 Stream_Read(s, correlationId,
sizeof(correlationId));
1196 if ((correlationId[0] == 0x00) || (correlationId[0] == 0xF4))
1198 WLog_ERR(TAG,
"(RDP_NEG_CORRELATION_INFO::correlationId[0] has invalid value 0x%02" PRIx8,
1202 for (
size_t x = 0; x < ARRAYSIZE(correlationId); x++)
1204 if (correlationId[x] == 0x0D)
1207 "(RDP_NEG_CORRELATION_INFO::correlationId[%" PRIuz
1208 "] has invalid value 0x%02" PRIx8,
1209 x, correlationId[x]);
1216 "RDP_NEG_CORRELATION_INFO::correlationId = { %02" PRIx8
", %02" PRIx8
", %02" PRIx8
1217 ", %02" PRIx8
", %02" PRIx8
", %02" PRIx8
", %02" PRIx8
", %02" PRIx8
", %02" PRIx8
1218 ", %02" PRIx8
", %02" PRIx8
", %02" PRIx8
", %02" PRIx8
", %02" PRIx8
", %02" PRIx8
1220 correlationId[0], correlationId[1], correlationId[2], correlationId[3],
1221 correlationId[4], correlationId[5], correlationId[6], correlationId[7],
1222 correlationId[8], correlationId[9], correlationId[10], correlationId[11],
1223 correlationId[12], correlationId[13], correlationId[14], correlationId[15]);
1227 BOOL nego_process_negotiation_request(rdpNego* nego,
wStream* s)
1235 if (!Stream_CheckAndLogRequiredLength(TAG, s, 7))
1237 Stream_Read_UINT8(s, flags);
1238 if ((flags & ~(RESTRICTED_ADMIN_MODE_REQUIRED | REDIRECTED_AUTHENTICATION_MODE_REQUIRED |
1239 CORRELATION_INFO_PRESENT)) != 0)
1241 WLog_ERR(TAG,
"RDP_NEG_REQ::flags invalid value 0x%02" PRIx8, flags);
1244 if (flags & RESTRICTED_ADMIN_MODE_REQUIRED)
1245 WLog_INFO(TAG,
"RDP_NEG_REQ::flags RESTRICTED_ADMIN_MODE_REQUIRED");
1247 if (flags & REDIRECTED_AUTHENTICATION_MODE_REQUIRED)
1249 if (!nego->RemoteCredsGuardSupported)
1252 "RDP_NEG_REQ::flags REDIRECTED_AUTHENTICATION_MODE_REQUIRED but disabled");
1257 WLog_INFO(TAG,
"RDP_NEG_REQ::flags REDIRECTED_AUTHENTICATION_MODE_REQUIRED");
1259 nego->RemoteCredsGuardActive = TRUE;
1262 Stream_Read_UINT16(s, length);
1265 WLog_ERR(TAG,
"RDP_NEG_REQ::length != 8");
1268 Stream_Read_UINT32(s, nego->RequestedProtocols);
1270 if (flags & CORRELATION_INFO_PRESENT)
1272 if (!nego_process_correlation_info(nego, s))
1277 char buffer[64] = { 0 };
1278 WLog_DBG(TAG,
"RDP_NEG_REQ: RequestedProtocol: %s",
1279 nego_protocol_to_str(nego->RequestedProtocols, buffer,
sizeof(buffer)));
1281 nego_set_state(nego, NEGO_STATE_FINAL);
1285 static const char* nego_rdp_neg_rsp_flags_str(UINT32 flags)
1287 static char buffer[1024] = { 0 };
1289 (void)_snprintf(buffer, ARRAYSIZE(buffer),
"[0x%02" PRIx32
"] ", flags);
1290 if (flags & EXTENDED_CLIENT_DATA_SUPPORTED)
1291 winpr_str_append(
"EXTENDED_CLIENT_DATA_SUPPORTED", buffer,
sizeof(buffer),
"|");
1292 if (flags & DYNVC_GFX_PROTOCOL_SUPPORTED)
1293 winpr_str_append(
"DYNVC_GFX_PROTOCOL_SUPPORTED", buffer,
sizeof(buffer),
"|");
1294 if (flags & RDP_NEGRSP_RESERVED)
1295 winpr_str_append(
"RDP_NEGRSP_RESERVED", buffer,
sizeof(buffer),
"|");
1296 if (flags & RESTRICTED_ADMIN_MODE_SUPPORTED)
1297 winpr_str_append(
"RESTRICTED_ADMIN_MODE_SUPPORTED", buffer,
sizeof(buffer),
"|");
1298 if (flags & REDIRECTED_AUTHENTICATION_MODE_SUPPORTED)
1299 winpr_str_append(
"REDIRECTED_AUTHENTICATION_MODE_SUPPORTED", buffer,
sizeof(buffer),
"|");
1301 ~(EXTENDED_CLIENT_DATA_SUPPORTED | DYNVC_GFX_PROTOCOL_SUPPORTED | RDP_NEGRSP_RESERVED |
1302 RESTRICTED_ADMIN_MODE_SUPPORTED | REDIRECTED_AUTHENTICATION_MODE_SUPPORTED)))
1303 winpr_str_append(
"UNKNOWN", buffer,
sizeof(buffer),
"|");
1308 BOOL nego_process_negotiation_response(rdpNego* nego,
wStream* s)
1315 if (!Stream_CheckAndLogRequiredLength(TAG, s, 7))
1317 nego_set_state(nego, NEGO_STATE_FAIL);
1321 Stream_Read_UINT8(s, nego->flags);
1322 WLog_DBG(TAG,
"RDP_NEG_RSP::flags = { %s }", nego_rdp_neg_rsp_flags_str(nego->flags));
1324 Stream_Read_UINT16(s, length);
1327 WLog_ERR(TAG,
"RDP_NEG_RSP::length != 8");
1328 nego_set_state(nego, NEGO_STATE_FAIL);
1331 UINT32 SelectedProtocol = 0;
1332 Stream_Read_UINT32(s, SelectedProtocol);
1334 if (!nego_set_selected_protocol(nego, SelectedProtocol))
1336 return nego_set_state(nego, NEGO_STATE_FINAL);
1347 BOOL nego_process_negotiation_failure(rdpNego* nego,
wStream* s)
1351 UINT32 failureCode = 0;
1356 WLog_DBG(TAG,
"RDP_NEG_FAILURE");
1357 if (!Stream_CheckAndLogRequiredLength(TAG, s, 7))
1360 Stream_Read_UINT8(s, flags);
1363 WLog_WARN(TAG,
"RDP_NEG_FAILURE::flags = 0x%02" PRIx8, flags);
1366 Stream_Read_UINT16(s, length);
1369 WLog_ERR(TAG,
"RDP_NEG_FAILURE::length != 8");
1372 Stream_Read_UINT32(s, failureCode);
1374 switch (failureCode)
1376 case SSL_REQUIRED_BY_SERVER:
1377 WLog_WARN(TAG,
"Error: SSL_REQUIRED_BY_SERVER");
1380 case SSL_NOT_ALLOWED_BY_SERVER:
1381 WLog_WARN(TAG,
"Error: SSL_NOT_ALLOWED_BY_SERVER");
1382 nego->sendNegoData = TRUE;
1385 case SSL_CERT_NOT_ON_SERVER:
1386 WLog_ERR(TAG,
"Error: SSL_CERT_NOT_ON_SERVER");
1387 nego->sendNegoData = TRUE;
1390 case INCONSISTENT_FLAGS:
1391 WLog_ERR(TAG,
"Error: INCONSISTENT_FLAGS");
1394 case HYBRID_REQUIRED_BY_SERVER:
1395 WLog_WARN(TAG,
"Error: HYBRID_REQUIRED_BY_SERVER");
1399 WLog_ERR(TAG,
"Error: Unknown protocol security error %" PRIu32
"", failureCode);
1403 nego_set_state(nego, NEGO_STATE_FAIL);
1412 BOOL nego_send_negotiation_response(rdpNego* nego)
1420 rdpContext* context = NULL;
1421 rdpSettings* settings = NULL;
1424 context = transport_get_context(nego->transport);
1425 WINPR_ASSERT(context);
1427 settings = context->settings;
1428 WINPR_ASSERT(settings);
1430 s = Stream_New(NULL, 512);
1434 WLog_ERR(TAG,
"Stream_New failed!");
1438 length = TPDU_CONNECTION_CONFIRM_LENGTH;
1439 bm = Stream_GetPosition(s);
1440 Stream_Seek(s, length);
1442 if (nego->SelectedProtocol & PROTOCOL_FAILED_NEGO)
1444 UINT32 errorCode = (nego->SelectedProtocol & ~PROTOCOL_FAILED_NEGO);
1446 Stream_Write_UINT8(s, TYPE_RDP_NEG_FAILURE);
1447 Stream_Write_UINT8(s, flags);
1448 Stream_Write_UINT16(s, 8);
1449 Stream_Write_UINT32(s, errorCode);
1454 flags = EXTENDED_CLIENT_DATA_SUPPORTED;
1457 flags |= DYNVC_GFX_PROTOCOL_SUPPORTED;
1460 flags |= RESTRICTED_ADMIN_MODE_SUPPORTED;
1462 if (nego->RemoteCredsGuardSupported)
1463 flags |= REDIRECTED_AUTHENTICATION_MODE_SUPPORTED;
1466 Stream_Write_UINT8(s, TYPE_RDP_NEG_RSP);
1467 Stream_Write_UINT8(s, flags);
1468 Stream_Write_UINT16(s, 8);
1469 Stream_Write_UINT32(s, nego->SelectedProtocol);
1473 em = Stream_GetPosition(s);
1474 Stream_SetPosition(s, bm);
1475 status = tpkt_write_header(s, length);
1478 tpdu_write_connection_confirm(s, length - 5);
1479 Stream_SetPosition(s, em);
1480 Stream_SealLength(s);
1482 status = (transport_write(nego->transport, s) >= 0);
1484 Stream_Free(s, TRUE);
1490 nego->RequestedProtocols))
1493 nego->SelectedProtocol))
1496 switch (nego->SelectedProtocol)
1509 ENCRYPTION_LEVEL_NONE)
1516 ENCRYPTION_LEVEL_CLIENT_COMPATIBLE))
1529 "Turning off encryption for local peer with standard rdp security");
1533 ENCRYPTION_LEVEL_NONE))
1538 WLog_ERR(TAG,
"Missing server certificate");
1555 ENCRYPTION_LEVEL_NONE))
1558 case PROTOCOL_HYBRID:
1571 ENCRYPTION_LEVEL_NONE))
1574 case PROTOCOL_RDSTLS:
1587 ENCRYPTION_LEVEL_NONE))
1603 void nego_init(rdpNego* nego)
1606 nego_set_state(nego, NEGO_STATE_INITIAL);
1607 nego->RequestedProtocols = PROTOCOL_RDP;
1608 nego->CookieMaxLength = DEFAULT_COOKIE_MAX_LENGTH;
1609 nego->sendNegoData = FALSE;
1621 rdpNego* nego_new(rdpTransport* transport)
1623 rdpNego* nego = (rdpNego*)calloc(1,
sizeof(rdpNego));
1628 nego->transport = transport;
1638 void nego_free(rdpNego* nego)
1642 free(nego->RoutingToken);
1657 BOOL nego_set_target(rdpNego* nego,
const char* hostname, UINT16 port)
1659 if (!nego || !hostname)
1662 nego->hostname = hostname;
1674 void nego_set_negotiation_enabled(rdpNego* nego, BOOL NegotiateSecurityLayer)
1676 WLog_DBG(TAG,
"Enabling security layer negotiation: %s",
1677 NegotiateSecurityLayer ?
"TRUE" :
"FALSE");
1678 nego->NegotiateSecurityLayer = NegotiateSecurityLayer;
1688 void nego_set_restricted_admin_mode_required(rdpNego* nego, BOOL RestrictedAdminModeRequired)
1690 WLog_DBG(TAG,
"Enabling restricted admin mode: %s",
1691 RestrictedAdminModeRequired ?
"TRUE" :
"FALSE");
1692 nego->RestrictedAdminModeRequired = RestrictedAdminModeRequired;
1695 void nego_set_RCG_required(rdpNego* nego, BOOL enabled)
1699 WLog_DBG(TAG,
"Enabling remoteCredentialGuards: %s", enabled ?
"TRUE" :
"FALSE");
1700 nego->RemoteCredsGuardRequired = enabled;
1703 void nego_set_RCG_supported(rdpNego* nego, BOOL enabled)
1707 nego->RemoteCredsGuardSupported = enabled;
1710 BOOL nego_get_remoteCredentialGuard(rdpNego* nego)
1714 return nego->RemoteCredsGuardActive;
1717 void nego_set_childsession_enabled(rdpNego* nego, BOOL ChildSessionEnabled)
1720 nego->ConnectChildSession = ChildSessionEnabled;
1723 void nego_set_gateway_enabled(rdpNego* nego, BOOL GatewayEnabled)
1725 nego->GatewayEnabled = GatewayEnabled;
1728 void nego_set_gateway_bypass_local(rdpNego* nego, BOOL GatewayBypassLocal)
1730 nego->GatewayBypassLocal = GatewayBypassLocal;
1739 void nego_enable_rdp(rdpNego* nego, BOOL enable_rdp)
1741 WLog_DBG(TAG,
"Enabling RDP security: %s", enable_rdp ?
"TRUE" :
"FALSE");
1742 nego->EnabledProtocols[PROTOCOL_RDP] = enable_rdp;
1751 void nego_enable_tls(rdpNego* nego, BOOL enable_tls)
1753 WLog_DBG(TAG,
"Enabling TLS security: %s", enable_tls ?
"TRUE" :
"FALSE");
1754 nego->EnabledProtocols[PROTOCOL_SSL] = enable_tls;
1764 void nego_enable_nla(rdpNego* nego, BOOL enable_nla)
1766 WLog_DBG(TAG,
"Enabling NLA security: %s", enable_nla ?
"TRUE" :
"FALSE");
1767 nego->EnabledProtocols[PROTOCOL_HYBRID] = enable_nla;
1777 void nego_enable_rdstls(rdpNego* nego, BOOL enable_rdstls)
1779 WLog_DBG(TAG,
"Enabling RDSTLS security: %s", enable_rdstls ?
"TRUE" :
"FALSE");
1780 nego->EnabledProtocols[PROTOCOL_RDSTLS] = enable_rdstls;
1790 void nego_enable_ext(rdpNego* nego, BOOL enable_ext)
1792 WLog_DBG(TAG,
"Enabling NLA extended security: %s", enable_ext ?
"TRUE" :
"FALSE");
1793 nego->EnabledProtocols[PROTOCOL_HYBRID_EX] = enable_ext;
1803 void nego_enable_aad(rdpNego* nego, BOOL enable_aad)
1805 if (aad_is_supported())
1807 WLog_DBG(TAG,
"Enabling RDS AAD security: %s", enable_aad ?
"TRUE" :
"FALSE");
1808 nego->EnabledProtocols[PROTOCOL_RDSAAD] = enable_aad;
1812 WLog_WARN(TAG,
"This build does not support AAD security, disabling.");
1825 BOOL nego_set_routing_token(rdpNego* nego,
const void* RoutingToken, DWORD RoutingTokenLength)
1827 if (RoutingTokenLength == 0)
1830 free(nego->RoutingToken);
1831 nego->RoutingTokenLength = RoutingTokenLength;
1832 nego->RoutingToken = (BYTE*)malloc(nego->RoutingTokenLength);
1834 if (!nego->RoutingToken)
1837 CopyMemory(nego->RoutingToken, RoutingToken, nego->RoutingTokenLength);
1849 BOOL nego_set_cookie(rdpNego* nego,
const char* cookie)
1854 nego->cookie = NULL;
1860 nego->cookie = _strdup(cookie);
1874 void nego_set_cookie_max_length(rdpNego* nego, UINT32 CookieMaxLength)
1876 nego->CookieMaxLength = CookieMaxLength;
1885 void nego_set_send_preconnection_pdu(rdpNego* nego, BOOL SendPreconnectionPdu)
1887 nego->SendPreconnectionPdu = SendPreconnectionPdu;
1896 void nego_set_preconnection_id(rdpNego* nego, UINT32 PreconnectionId)
1898 nego->PreconnectionId = PreconnectionId;
1907 void nego_set_preconnection_blob(rdpNego* nego,
const char* PreconnectionBlob)
1909 nego->PreconnectionBlob = PreconnectionBlob;
1912 UINT32 nego_get_selected_protocol(rdpNego* nego)
1917 return nego->SelectedProtocol;
1920 BOOL nego_set_selected_protocol(rdpNego* nego, UINT32 SelectedProtocol)
1923 nego->SelectedProtocol = SelectedProtocol;
1927 UINT32 nego_get_requested_protocols(rdpNego* nego)
1932 return nego->RequestedProtocols;
1935 BOOL nego_set_requested_protocols(rdpNego* nego, UINT32 RequestedProtocols)
1940 nego->RequestedProtocols = RequestedProtocols;
1944 NEGO_STATE nego_get_state(rdpNego* nego)
1947 return NEGO_STATE_FAIL;
1952 BOOL nego_set_state(rdpNego* nego, NEGO_STATE state)
1957 nego->state = state;
1961 SEC_WINNT_AUTH_IDENTITY* nego_get_identity(rdpNego* nego)
1967 nla = transport_get_nla(nego->transport);
1968 return nla_get_identity(nla);
1971 void nego_free_nla(rdpNego* nego)
1973 if (!nego || !nego->transport)
1976 transport_set_nla(nego->transport, NULL);
1979 const BYTE* nego_get_routing_token(rdpNego* nego, DWORD* RoutingTokenLength)
1983 if (RoutingTokenLength)
1984 *RoutingTokenLength = nego->RoutingTokenLength;
1985 return nego->RoutingToken;
1988 const char* nego_protocol_to_str(UINT32 protocol,
char* buffer,
size_t size)
1990 const UINT32 mask = ~(PROTOCOL_SSL | PROTOCOL_HYBRID | PROTOCOL_RDSTLS | PROTOCOL_HYBRID_EX |
1991 PROTOCOL_RDSAAD | PROTOCOL_FAILED_NEGO);
1992 char str[48] = { 0 };
1994 if (protocol & PROTOCOL_SSL)
1995 (void)winpr_str_append(
"SSL", str,
sizeof(str),
"|");
1996 if (protocol & PROTOCOL_HYBRID)
1997 (void)winpr_str_append(
"HYBRID", str,
sizeof(str),
"|");
1998 if (protocol & PROTOCOL_RDSTLS)
1999 (void)winpr_str_append(
"RDSTLS", str,
sizeof(str),
"|");
2000 if (protocol & PROTOCOL_HYBRID_EX)
2001 (void)winpr_str_append(
"HYBRID_EX", str,
sizeof(str),
"|");
2002 if (protocol & PROTOCOL_RDSAAD)
2003 (void)winpr_str_append(
"RDSAAD", str,
sizeof(str),
"|");
2004 if (protocol & PROTOCOL_FAILED_NEGO)
2005 (void)winpr_str_append(
"NEGO FAILED", str,
sizeof(str),
"|");
2007 if (protocol == PROTOCOL_RDP)
2008 (void)winpr_str_append(
"RDP", str,
sizeof(str),
"");
2009 else if ((protocol & mask) != 0)
2010 (
void)winpr_str_append(
"UNKNOWN", str,
sizeof(str),
"|");
2012 (void)_snprintf(buffer, size,
"[%s][0x%08" PRIx32
"]", str, protocol);
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API const void * freerdp_settings_get_pointer(const rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id)
Returns a immutable pointer settings value.
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL param)
Sets a BOOL settings value.