24 #include <freerdp/config.h>
33 #include "connection.h"
34 #include "transport.h"
36 #include <winpr/crt.h>
37 #include <winpr/crypto.h>
38 #include <winpr/ssl.h>
40 #include <freerdp/log.h>
41 #include <freerdp/error.h>
42 #include <freerdp/listener.h>
44 #include "../cache/pointer.h"
45 #include "../crypto/crypto.h"
46 #include "../crypto/privatekey.h"
47 #include "../crypto/certificate.h"
48 #include "gateway/arm.h"
52 #define TAG FREERDP_TAG("core.connection")
197 static BOOL rdp_set_state(rdpRdp* rdp, CONNECTION_STATE state);
199 static BOOL rdp_client_reset_codecs(rdpContext* context)
201 rdpSettings* settings = NULL;
203 if (!context || !context->settings)
206 settings = context->settings;
211 freerdp_client_codecs_free(context->codecs);
212 context->codecs = freerdp_client_codecs_new(flags);
214 if (!context->codecs)
217 if (!freerdp_client_codecs_prepare(context->codecs,
219 settings->DesktopWidth, settings->DesktopHeight))
225 #if defined(WITH_GFX_H264) && defined(WITH_OPENH264_LOADING)
226 if (!context->codecs->h264)
228 settings->GfxH264 = FALSE;
229 settings->GfxAVC444 = FALSE;
230 settings->GfxAVC444v2 = FALSE;
238 static BOOL rdp_client_wait_for_activation(rdpRdp* rdp)
240 BOOL timedout = FALSE;
243 const rdpSettings* settings = rdp->settings;
244 WINPR_ASSERT(settings);
246 UINT64 now = GetTickCount64();
249 for (; (now < dueDate) && !timedout; now = GetTickCount64())
251 HANDLE events[MAXIMUM_WAIT_OBJECTS] = { 0 };
253 DWORD nevents = freerdp_get_event_handles(rdp->context, events, ARRAYSIZE(events));
256 WLog_ERR(TAG,
"error retrieving connection events");
260 const UINT64 timeout = (dueDate - now);
261 WINPR_ASSERT(timeout <= UINT32_MAX);
262 wstatus = WaitForMultipleObjectsEx(nevents, events, FALSE, (UINT32)timeout, TRUE);
272 case WAIT_IO_COMPLETION:
277 if (rdp_check_fds(rdp) < 0)
279 freerdp_set_last_error_if_not(rdp->context,
280 FREERDP_ERROR_CONNECT_TRANSPORT_FAILED);
286 if (rdp_is_active_state(rdp))
290 WLog_ERR(TAG,
"Timeout waiting for activation");
291 freerdp_set_last_error_if_not(rdp->context, FREERDP_ERROR_CONNECT_ACTIVATION_TIMEOUT);
301 BOOL rdp_client_connect(rdpRdp* rdp)
303 UINT32 SelectedProtocol = 0;
305 rdpSettings* settings = NULL;
308 DWORD flags = WINPR_SSL_INIT_DEFAULT;
312 settings = rdp->settings;
313 WINPR_ASSERT(settings);
315 if (!rdp_client_reset_codecs(rdp->context))
318 if (settings->FIPSMode)
319 flags |= WINPR_SSL_INIT_ENABLE_FIPS;
321 winpr_InitializeSSL(flags);
329 if (settings->FIPSMode || winpr_FIPSMode())
331 settings->NlaSecurity = FALSE;
332 settings->EncryptionMethods = ENCRYPTION_METHOD_FIPS;
336 if (settings->GatewayArmTransport)
338 if (!arm_resolve_endpoint(rdp->context, TcpConnectTimeout))
340 WLog_ERR(TAG,
"error retrieving ARM configuration");
345 const char* hostname = settings->ServerHostname;
348 WLog_ERR(TAG,
"Missing hostname, can not connect to NULL target");
352 const UINT32 port = settings->ServerPort;
353 WINPR_ASSERT(port <= UINT32_MAX);
355 nego_init(rdp->nego);
356 nego_set_target(rdp->nego, hostname, (UINT16)port);
358 if (settings->GatewayEnabled)
362 size_t user_length = 0;
364 if (settings->Username)
366 user = settings->Username;
367 user_length = strlen(settings->Username);
370 if (settings->Domain)
371 domain = settings->Domain;
373 domain = settings->ComputerName;
375 const size_t domain_length = strlen(domain);
376 const size_t cookie_length = domain_length + 1 + user_length;
377 char* cookie = malloc(cookie_length + 1);
382 CopyMemory(cookie, domain, domain_length);
383 WINPR_ASSERT(domain_length <= UINT32_MAX);
384 CharUpperBuffA(cookie, (UINT32)domain_length);
385 cookie[domain_length] =
'\\';
387 if (settings->Username)
388 CopyMemory(&cookie[domain_length + 1], user, user_length);
390 cookie[cookie_length] =
'\0';
391 status = nego_set_cookie(rdp->nego, cookie);
396 status = nego_set_cookie(rdp->nego, settings->Username);
402 nego_set_childsession_enabled(rdp->nego, settings->ConnectChildSession);
403 nego_set_send_preconnection_pdu(rdp->nego, settings->SendPreconnectionPdu);
404 nego_set_preconnection_id(rdp->nego, settings->PreconnectionId);
405 nego_set_preconnection_blob(rdp->nego, settings->PreconnectionBlob);
406 nego_set_negotiation_enabled(rdp->nego, settings->NegotiateSecurityLayer);
407 nego_set_restricted_admin_mode_required(rdp->nego, settings->RestrictedAdminModeRequired);
408 nego_set_RCG_required(rdp->nego, settings->RemoteCredentialGuard);
409 nego_set_gateway_enabled(rdp->nego, settings->GatewayEnabled);
410 nego_set_gateway_bypass_local(rdp->nego, settings->GatewayBypassLocal);
411 nego_enable_rdp(rdp->nego, settings->RdpSecurity);
412 nego_enable_tls(rdp->nego, settings->TlsSecurity);
413 nego_enable_nla(rdp->nego, settings->NlaSecurity);
414 nego_enable_ext(rdp->nego, settings->ExtSecurity);
415 nego_enable_rdstls(rdp->nego, settings->RdstlsSecurity);
416 nego_enable_aad(rdp->nego, settings->AadSecurity);
418 if (settings->MstscCookieMode)
419 settings->CookieMaxLength = MSTSC_COOKIE_MAX_LENGTH;
421 nego_set_cookie_max_length(rdp->nego, settings->CookieMaxLength);
423 if (settings->LoadBalanceInfo && (settings->LoadBalanceInfoLength > 0))
425 if (!nego_set_routing_token(rdp->nego, settings->LoadBalanceInfo,
426 settings->LoadBalanceInfoLength))
432 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_NEGO))
435 if (!nego_connect(rdp->nego))
437 if (!freerdp_get_last_error(rdp->context))
439 freerdp_set_last_error_log(rdp->context,
440 FREERDP_ERROR_SECURITY_NEGO_CONNECT_FAILED);
441 WLog_ERR(TAG,
"Error: protocol security negotiation or connection failure");
447 SelectedProtocol = nego_get_selected_protocol(rdp->nego);
449 if ((SelectedProtocol & PROTOCOL_SSL) || (SelectedProtocol == PROTOCOL_RDP) ||
450 (SelectedProtocol == PROTOCOL_RDSTLS))
454 if ((settings->Username != NULL) &&
456 (settings->RedirectionPassword != NULL &&
457 settings->RedirectionPasswordLength > 0)))
458 settings->AutoLogonEnabled = TRUE;
460 if (rdp_recv_callback(rdp->transport, &s, rdp) < 0)
464 transport_set_blocking_mode(rdp->transport, FALSE);
468 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_CREATE_REQUEST))
473 if (!transport_set_recv_callbacks(rdp->transport, rdp_recv_callback, rdp))
476 return rdp_client_wait_for_activation(rdp);
479 BOOL rdp_client_disconnect(rdpRdp* rdp)
481 rdpContext* context = NULL;
483 if (!rdp || !rdp->settings || !rdp->context)
486 context = rdp->context;
490 if (!nego_disconnect(rdp->nego))
497 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_INITIAL))
500 if (freerdp_channels_disconnect(context->channels, context->instance) != CHANNEL_RC_OK)
503 freerdp_client_codecs_free(context->codecs);
504 context->codecs = NULL;
508 BOOL rdp_client_disconnect_and_clear(rdpRdp* rdp)
510 rdpContext* context = NULL;
512 if (!rdp_client_disconnect(rdp))
517 context = rdp->context;
518 WINPR_ASSERT(context);
520 if (freerdp_get_last_error(context) == FREERDP_ERROR_CONNECT_CANCELLED)
523 context->LastError = FREERDP_ERROR_SUCCESS;
524 clearChannelError(context);
525 return utils_reset_abort(rdp);
528 static BOOL rdp_client_reconnect_channels(rdpRdp* rdp, BOOL redirect)
531 rdpContext* context = NULL;
533 if (!rdp || !rdp->context || !rdp->context->channels)
536 context = rdp->context;
538 if (context->instance->ConnectionCallbackState == CLIENT_STATE_INITIAL)
541 if (context->instance->ConnectionCallbackState == CLIENT_STATE_PRECONNECT_PASSED)
546 pointer_cache_register_callbacks(context->update);
548 if (!IFCALLRESULT(FALSE, context->instance->PostConnect, context->instance))
551 context->instance->ConnectionCallbackState = CLIENT_STATE_POSTCONNECT_PASSED;
554 if (context->instance->ConnectionCallbackState == CLIENT_STATE_POSTCONNECT_PASSED)
556 (freerdp_channels_post_connect(context->channels, context->instance) == CHANNEL_RC_OK);
561 static BOOL rdp_client_redirect_resolvable(
const char* host)
563 struct addrinfo* result = freerdp_tcp_resolve_host(host, -1, 0);
568 freeaddrinfo(result);
572 static BOOL rdp_client_redirect_try_fqdn(rdpSettings* settings)
574 if (settings->RedirectionFlags & LB_TARGET_FQDN)
576 if (settings->GatewayEnabled ||
577 rdp_client_redirect_resolvable(settings->RedirectionTargetFQDN))
580 settings->RedirectionTargetFQDN))
590 static BOOL rdp_client_redirect_try_ip(rdpSettings* settings)
592 if (settings->RedirectionFlags & LB_TARGET_NET_ADDRESS)
595 settings->TargetNetAddress))
604 static BOOL rdp_client_redirect_try_netbios(rdpSettings* settings)
606 if (settings->RedirectionFlags & LB_TARGET_NETBIOS_NAME)
608 if (settings->GatewayEnabled ||
609 rdp_client_redirect_resolvable(settings->RedirectionTargetNetBiosName))
612 settings->RedirectionTargetNetBiosName))
622 BOOL rdp_client_redirect(rdpRdp* rdp)
625 rdpSettings* settings = NULL;
627 if (!rdp_client_disconnect_and_clear(rdp))
632 freerdp_channels_disconnect(rdp->context->channels, rdp->context->instance);
633 freerdp_channels_close(rdp->context->channels, rdp->context->instance);
635 if (rdp_redirection_apply_settings(rdp) != 0)
640 settings = rdp->settings;
641 WINPR_ASSERT(settings);
643 if ((settings->RedirectionFlags & LB_LOAD_BALANCE_INFO) == 0)
645 BOOL haveRedirectAddress = FALSE;
646 UINT32 redirectionMask = settings->RedirectionPreferType;
650 const BOOL tryFQDN = (redirectionMask & 0x01) == 0;
651 const BOOL tryNetAddress = (redirectionMask & 0x02) == 0;
652 const BOOL tryNetbios = (redirectionMask & 0x04) == 0;
654 if (tryFQDN && !haveRedirectAddress)
655 haveRedirectAddress = rdp_client_redirect_try_fqdn(settings);
657 if (tryNetAddress && !haveRedirectAddress)
658 haveRedirectAddress = rdp_client_redirect_try_ip(settings);
660 if (tryNetbios && !haveRedirectAddress)
661 haveRedirectAddress = rdp_client_redirect_try_netbios(settings);
663 redirectionMask >>= 3;
664 }
while (!haveRedirectAddress && (redirectionMask != 0));
667 if (settings->RedirectionFlags & LB_USERNAME)
670 settings, FreeRDP_Username,
675 if (settings->RedirectionFlags & LB_DOMAIN)
678 settings, FreeRDP_Domain,
683 settings->RdstlsSecurity =
684 (settings->RedirectionFlags & LB_PASSWORD_IS_PK_ENCRYPTED) != 0 ? TRUE : FALSE;
686 WINPR_ASSERT(rdp->context);
687 WINPR_ASSERT(rdp->context->instance);
688 if (!IFCALLRESULT(TRUE, rdp->context->instance->Redirect, rdp->context->instance))
691 BOOL ok = utils_reload_channels(rdp->context);
695 status = rdp_client_connect(rdp);
698 status = rdp_client_reconnect_channels(rdp, TRUE);
703 BOOL rdp_client_reconnect(rdpRdp* rdp)
707 if (!rdp_client_disconnect_and_clear(rdp))
710 status = rdp_client_connect(rdp);
713 status = rdp_client_reconnect_channels(rdp, FALSE);
718 static const BYTE fips_ivec[8] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
720 static BOOL rdp_client_establish_keys(rdpRdp* rdp)
727 rdpSettings* settings = rdp->settings;
728 BYTE* crypt_client_random = NULL;
730 WINPR_ASSERT(settings);
731 if (!settings->UseRdpSecurityLayer)
737 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT))
742 CLIENT_RANDOM_LENGTH))
744 winpr_RAND(settings->ClientRandom, settings->ClientRandomLength);
746 const rdpCertInfo* info = freerdp_certificate_get_info(settings->RdpServerCertificate);
749 WLog_ERR(TAG,
"Failed to get rdpCertInfo from RdpServerCertificate");
757 crypt_client_random = calloc(info->ModulusLength, 1);
759 if (!crypt_client_random)
762 crypto_rsa_public_encrypt(settings->ClientRandom, settings->ClientRandomLength, info,
763 crypt_client_random, info->ModulusLength);
765 const size_t length = RDP_PACKET_HEADER_MAX_LENGTH + RDP_SECURITY_HEADER_LENGTH + 4ULL +
766 info->ModulusLength + 8ULL;
767 if (length > UINT16_MAX)
770 s = Stream_New(NULL, length);
774 WLog_ERR(TAG,
"Stream_New failed!");
778 if (!rdp_write_header(rdp, s, length, MCS_GLOBAL_CHANNEL_ID))
780 if (!rdp_write_security_header(rdp, s, SEC_EXCHANGE_PKT | SEC_LICENSE_ENCRYPT_SC))
783 Stream_Write_UINT32(s, info->ModulusLength + 8);
784 Stream_Write(s, crypt_client_random, info->ModulusLength);
786 Stream_SealLength(s);
787 status = transport_write(rdp->mcs->transport, s);
792 rdp->do_crypt_license = TRUE;
795 if (!security_establish_keys(rdp))
798 rdp->do_crypt = TRUE;
800 if (settings->SaltedChecksum)
801 rdp->do_secure_checksum = TRUE;
803 if (settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
806 winpr_Cipher_NewEx(WINPR_CIPHER_DES_EDE3_CBC, WINPR_ENCRYPT, rdp->fips_encrypt_key,
807 sizeof(rdp->fips_encrypt_key), fips_ivec,
sizeof(fips_ivec));
809 if (!rdp->fips_encrypt)
811 WLog_ERR(TAG,
"unable to allocate des3 encrypt key");
816 winpr_Cipher_NewEx(WINPR_CIPHER_DES_EDE3_CBC, WINPR_DECRYPT, rdp->fips_decrypt_key,
817 sizeof(rdp->fips_decrypt_key), fips_ivec,
sizeof(fips_ivec));
819 if (!rdp->fips_decrypt)
821 WLog_ERR(TAG,
"unable to allocate des3 decrypt key");
829 if (!rdp_reset_rc4_encrypt_keys(rdp))
831 if (!rdp_reset_rc4_decrypt_keys(rdp))
836 Stream_Free(s, TRUE);
837 free(crypt_client_random);
841 winpr_Cipher_Free(rdp->fips_decrypt);
842 winpr_Cipher_Free(rdp->fips_encrypt);
843 rdp->fips_decrypt = NULL;
844 rdp->fips_encrypt = NULL;
846 rdp_free_rc4_decrypt_keys(rdp);
847 rdp_free_rc4_encrypt_keys(rdp);
853 static BOOL rdp_update_client_random(rdpSettings* settings,
const BYTE* crypt_random,
854 size_t crypt_random_len)
856 const size_t length = 32;
857 WINPR_ASSERT(settings);
862 const rdpCertInfo* cinfo = freerdp_key_get_info(rsa);
865 if (crypt_random_len != cinfo->ModulusLength + 8)
867 WLog_ERR(TAG,
"invalid encrypted client random length");
874 WINPR_ASSERT(client_random);
875 return crypto_rsa_private_decrypt(crypt_random, crypt_random_len - 8, rsa, client_random,
879 BOOL rdp_server_establish_keys(rdpRdp* rdp,
wStream* s)
882 UINT16 channel_id = 0;
884 UINT16 sec_flags = 0;
889 if (!rdp->settings->UseRdpSecurityLayer)
895 if (!rdp_read_header(rdp, s, &length, &channel_id))
898 if (!rdp_read_security_header(rdp, s, &sec_flags, NULL))
900 WLog_ERR(TAG,
"invalid security header");
904 if ((sec_flags & SEC_EXCHANGE_PKT) == 0)
906 WLog_ERR(TAG,
"missing SEC_EXCHANGE_PKT in security header");
910 rdp->do_crypt_license = (sec_flags & SEC_LICENSE_ENCRYPT_SC) != 0 ? TRUE : FALSE;
912 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
915 Stream_Read_UINT32(s, rand_len);
918 if (!Stream_CheckAndLogRequiredLength(TAG, s, rand_len))
921 const BYTE* crypt_random = Stream_ConstPointer(s);
922 if (!Stream_SafeSeek(s, rand_len))
924 if (!rdp_update_client_random(rdp->settings, crypt_random, rand_len))
928 if (!security_establish_keys(rdp))
931 rdp->do_crypt = TRUE;
933 if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
936 winpr_Cipher_NewEx(WINPR_CIPHER_DES_EDE3_CBC, WINPR_ENCRYPT, rdp->fips_encrypt_key,
937 sizeof(rdp->fips_encrypt_key), fips_ivec,
sizeof(fips_ivec));
939 if (!rdp->fips_encrypt)
941 WLog_ERR(TAG,
"unable to allocate des3 encrypt key");
946 winpr_Cipher_NewEx(WINPR_CIPHER_DES_EDE3_CBC, WINPR_DECRYPT, rdp->fips_decrypt_key,
947 sizeof(rdp->fips_decrypt_key), fips_ivec,
sizeof(fips_ivec));
949 if (!rdp->fips_decrypt)
951 WLog_ERR(TAG,
"unable to allocate des3 decrypt key");
959 if (!rdp_reset_rc4_encrypt_keys(rdp))
962 if (!rdp_reset_rc4_decrypt_keys(rdp))
965 ret = tpkt_ensure_stream_consumed(s, length);
970 winpr_Cipher_Free(rdp->fips_encrypt);
971 winpr_Cipher_Free(rdp->fips_decrypt);
972 rdp->fips_encrypt = NULL;
973 rdp->fips_decrypt = NULL;
975 rdp_free_rc4_encrypt_keys(rdp);
976 rdp_free_rc4_decrypt_keys(rdp);
982 static BOOL rdp_client_send_client_info_and_change_state(rdpRdp* rdp)
985 if (!rdp_client_establish_keys(rdp))
987 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE))
989 if (!rdp_send_client_info(rdp))
991 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_REQUEST))
996 BOOL rdp_client_skip_mcs_channel_join(rdpRdp* rdp)
1000 rdpMcs* mcs = rdp->mcs;
1003 mcs->userChannelJoined = TRUE;
1004 mcs->globalChannelJoined = TRUE;
1005 mcs->messageChannelJoined = TRUE;
1007 for (UINT32 i = 0; i < mcs->channelCount; i++)
1009 rdpMcsChannel* cur = &mcs->channels[i];
1010 WLog_DBG(TAG,
" %s [%" PRIu16
"]", cur->Name, cur->ChannelId);
1014 return rdp_client_send_client_info_and_change_state(rdp);
1017 static BOOL rdp_client_join_channel(rdpRdp* rdp, UINT16 ChannelId)
1021 rdpMcs* mcs = rdp->mcs;
1022 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST))
1024 if (!mcs_send_channel_join_request(mcs, ChannelId))
1026 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_CHANNEL_JOIN_RESPONSE))
1031 BOOL rdp_client_connect_mcs_channel_join_confirm(rdpRdp* rdp,
wStream* s)
1033 UINT16 channelId = 0;
1034 BOOL allJoined = TRUE;
1037 rdpMcs* mcs = rdp->mcs;
1039 if (!mcs_recv_channel_join_confirm(mcs, s, &channelId))
1042 if (!mcs->userChannelJoined)
1044 if (channelId != mcs->userId)
1046 WLog_ERR(TAG,
"expected user channel id %" PRIu16
", but received %" PRIu16,
1047 mcs->userId, channelId);
1051 mcs->userChannelJoined = TRUE;
1052 if (!rdp_client_join_channel(rdp, MCS_GLOBAL_CHANNEL_ID))
1055 else if (!mcs->globalChannelJoined)
1057 if (channelId != MCS_GLOBAL_CHANNEL_ID)
1059 WLog_ERR(TAG,
"expected uglobalser channel id %" PRIu16
", but received %" PRIu16,
1060 MCS_GLOBAL_CHANNEL_ID, channelId);
1063 mcs->globalChannelJoined = TRUE;
1065 if (mcs->messageChannelId != 0)
1067 if (!rdp_client_join_channel(rdp, mcs->messageChannelId))
1073 if (mcs->channelCount > 0)
1075 const rdpMcsChannel* cur = &mcs->channels[0];
1076 if (!rdp_client_join_channel(rdp, cur->ChannelId))
1082 else if ((mcs->messageChannelId != 0) && !mcs->messageChannelJoined)
1084 if (channelId != mcs->messageChannelId)
1086 WLog_ERR(TAG,
"expected messageChannelId=%" PRIu16
", got %" PRIu16,
1087 mcs->messageChannelId, channelId);
1091 mcs->messageChannelJoined = TRUE;
1093 if (mcs->channelCount > 0)
1095 const rdpMcsChannel* cur = &mcs->channels[0];
1096 if (!rdp_client_join_channel(rdp, cur->ChannelId))
1104 for (; i < mcs->channelCount; i++)
1106 rdpMcsChannel* cur = &mcs->channels[i];
1110 if (cur->ChannelId != channelId)
1112 WLog_ERR(TAG,
"expected channel id %" PRIu16
", but received %" PRIu16,
1113 MCS_GLOBAL_CHANNEL_ID, channelId);
1120 if (i + 1 < mcs->channelCount)
1122 const rdpMcsChannel* cur = &mcs->channels[i + 1];
1123 if (!rdp_client_join_channel(rdp, cur->ChannelId))
1129 if (mcs->userChannelJoined && mcs->globalChannelJoined && allJoined)
1131 if (!rdp_client_send_client_info_and_change_state(rdp))
1138 BOOL rdp_handle_message_channel(rdpRdp* rdp,
wStream* s, UINT16 channelId, UINT16 length)
1141 WINPR_ASSERT(rdp->mcs);
1143 if (!rdp->mcs->messageChannelJoined)
1145 WLog_Print(rdp->log, WLOG_WARN,
"MCS message channel not joined!");
1148 const UINT16 messageChannelId = rdp->mcs->messageChannelId;
1149 if (messageChannelId == 0)
1151 WLog_Print(rdp->log, WLOG_WARN,
"MCS message channel id == 0");
1155 if (channelId != messageChannelId)
1157 WLog_Print(rdp->log, WLOG_WARN,
"MCS message channel expected id=%" PRIu16
", got %" PRIu16,
1158 messageChannelId, channelId);
1162 UINT16 securityFlags = 0;
1163 if (!rdp_read_security_header(rdp, s, &securityFlags, &length))
1166 if (securityFlags & SEC_ENCRYPT)
1168 if (!rdp_decrypt(rdp, s, &length, securityFlags))
1172 if (rdp_recv_message_channel_pdu(rdp, s, securityFlags) != STATE_RUN_SUCCESS)
1175 return tpkt_ensure_stream_consumed(s, length);
1178 BOOL rdp_client_connect_auto_detect(rdpRdp* rdp,
wStream* s)
1181 WINPR_ASSERT(rdp->mcs);
1183 const size_t pos = Stream_GetPosition(s);
1185 UINT16 channelId = 0;
1187 if (rdp_read_header(rdp, s, &length, &channelId))
1189 const UINT16 messageChannelId = rdp->mcs->messageChannelId;
1193 if (rdp->mcs->messageChannelJoined && (channelId == messageChannelId))
1195 if (rdp_handle_message_channel(rdp, s, channelId, length))
1200 WLog_WARN(TAG,
"expected messageChannelId=%" PRIu16
", got %" PRIu16, messageChannelId,
1205 Stream_SetPosition(s, pos);
1209 state_run_t rdp_client_connect_license(rdpRdp* rdp,
wStream* s)
1211 state_run_t status = STATE_RUN_FAILED;
1212 LICENSE_STATE state = LICENSE_STATE_ABORTED;
1214 UINT16 channelId = 0;
1215 UINT16 securityFlags = 0;
1218 if (!rdp_read_header(rdp, s, &length, &channelId))
1219 return STATE_RUN_FAILED;
1224 const UINT16 messageChannelId = rdp->mcs->messageChannelId;
1225 if (rdp->mcs->messageChannelJoined && (channelId == messageChannelId))
1227 if (!rdp_handle_message_channel(rdp, s, channelId, length))
1228 return STATE_RUN_FAILED;
1229 return STATE_RUN_SUCCESS;
1232 if (!rdp_read_security_header(rdp, s, &securityFlags, &length))
1233 return STATE_RUN_FAILED;
1235 if (securityFlags & SEC_ENCRYPT)
1237 if (!rdp_decrypt(rdp, s, &length, securityFlags))
1238 return STATE_RUN_FAILED;
1241 if (channelId != MCS_GLOBAL_CHANNEL_ID)
1242 WLog_WARN(TAG,
"unexpected message for channel %u, expected %u", channelId,
1243 MCS_GLOBAL_CHANNEL_ID);
1245 if ((securityFlags & SEC_LICENSE_PKT) == 0)
1247 char buffer[512] = { 0 };
1248 char lbuffer[32] = { 0 };
1249 WLog_ERR(TAG,
"securityFlags=%s, missing required flag %s",
1250 rdp_security_flag_string(securityFlags, buffer,
sizeof(buffer)),
1251 rdp_security_flag_string(SEC_LICENSE_PKT, lbuffer,
sizeof(lbuffer)));
1252 return STATE_RUN_FAILED;
1255 status = license_recv(rdp->license, s);
1257 if (state_run_failed(status))
1260 state = license_get_state(rdp->license);
1263 case LICENSE_STATE_ABORTED:
1264 WLog_ERR(TAG,
"license connection sequence aborted.");
1265 return STATE_RUN_FAILED;
1266 case LICENSE_STATE_COMPLETED:
1267 if (rdp->settings->MultitransportFlags)
1269 if (!rdp_client_transition_to_state(
1270 rdp, CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_REQUEST))
1271 return STATE_RUN_FAILED;
1275 if (!rdp_client_transition_to_state(
1276 rdp, CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE))
1277 return STATE_RUN_FAILED;
1279 return STATE_RUN_SUCCESS;
1281 return STATE_RUN_SUCCESS;
1285 state_run_t rdp_client_connect_demand_active(rdpRdp* rdp,
wStream* s)
1288 UINT16 channelId = 0;
1290 UINT16 pduSource = 0;
1294 WINPR_ASSERT(rdp->settings);
1296 if (!rdp_recv_get_active_header(rdp, s, &channelId, &length))
1297 return STATE_RUN_FAILED;
1299 if (freerdp_shall_disconnect_context(rdp->context))
1300 return STATE_RUN_QUIT_SESSION;
1302 if (rdp->mcs->messageChannelId && (channelId == rdp->mcs->messageChannelId))
1305 if (!rdp_handle_message_channel(rdp, s, channelId, length))
1306 return STATE_RUN_FAILED;
1307 return STATE_RUN_SUCCESS;
1310 if (!rdp_handle_optional_rdp_decryption(rdp, s, &length, NULL))
1311 return STATE_RUN_FAILED;
1313 if (!rdp_read_share_control_header(rdp, s, NULL, NULL, &pduType, &pduSource))
1314 return STATE_RUN_FAILED;
1318 case PDU_TYPE_DEMAND_ACTIVE:
1319 if (!rdp_recv_demand_active(rdp, s, pduSource, length))
1320 return STATE_RUN_FAILED;
1321 return STATE_RUN_ACTIVE;
1323 return rdp_recv_out_of_sequence_pdu(rdp, s, pduType, length);
1327 state_run_t rdp_client_connect_finalize(rdpRdp* rdp)
1336 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_SYNC))
1337 return STATE_RUN_FAILED;
1339 if (!rdp_send_client_synchronize_pdu(rdp))
1340 return STATE_RUN_FAILED;
1342 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_COOPERATE))
1343 return STATE_RUN_FAILED;
1344 if (!rdp_send_client_control_pdu(rdp, CTRLACTION_COOPERATE))
1345 return STATE_RUN_FAILED;
1347 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_REQUEST_CONTROL))
1348 return STATE_RUN_FAILED;
1349 if (!rdp_send_client_control_pdu(rdp, CTRLACTION_REQUEST_CONTROL))
1350 return STATE_RUN_FAILED;
1359 if (!rdp_finalize_is_flag_set(rdp, FINALIZE_DEACTIVATE_REACTIVATE) &&
1362 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_PERSISTENT_KEY_LIST))
1363 return STATE_RUN_FAILED;
1364 if (!rdp_send_client_persistent_key_list_pdu(rdp))
1365 return STATE_RUN_FAILED;
1368 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_FONT_LIST))
1369 return STATE_RUN_FAILED;
1370 if (!rdp_send_client_font_list_pdu(rdp, FONTLIST_FIRST | FONTLIST_LAST))
1371 return STATE_RUN_FAILED;
1373 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_CLIENT_SYNC))
1374 return STATE_RUN_FAILED;
1375 return STATE_RUN_SUCCESS;
1378 BOOL rdp_client_transition_to_state(rdpRdp* rdp, CONNECTION_STATE state)
1380 const char* name = rdp_state_string(state);
1383 WLog_Print(rdp->log, WLOG_DEBUG,
"%s --> %s", rdp_get_state_string(rdp), name);
1385 if (!rdp_set_state(rdp, state))
1390 case CONNECTION_STATE_FINALIZATION_SYNC:
1391 case CONNECTION_STATE_FINALIZATION_COOPERATE:
1392 case CONNECTION_STATE_FINALIZATION_REQUEST_CONTROL:
1393 case CONNECTION_STATE_FINALIZATION_PERSISTENT_KEY_LIST:
1394 case CONNECTION_STATE_FINALIZATION_FONT_LIST:
1395 update_reset_state(rdp->update);
1398 case CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE:
1400 ActivatedEventArgs activatedEvent = { 0 };
1401 rdpContext* context = rdp->context;
1402 EventArgsInit(&activatedEvent,
"libfreerdp");
1403 activatedEvent.firstActivation =
1404 !rdp_finalize_is_flag_set(rdp, FINALIZE_DEACTIVATE_REACTIVATE);
1405 PubSub_OnActivated(rdp->pubSub, context, &activatedEvent);
1415 ConnectionStateChangeEventArgs stateEvent = { 0 };
1416 rdpContext* context = rdp->context;
1417 EventArgsInit(&stateEvent,
"libfreerdp");
1418 stateEvent.state = rdp_get_state(rdp);
1419 stateEvent.active = rdp_is_active_state(rdp);
1420 PubSub_OnConnectionStateChange(rdp->pubSub, context, &stateEvent);
1426 BOOL rdp_server_accept_nego(rdpRdp* rdp,
wStream* s)
1428 UINT32 SelectedProtocol = 0;
1429 UINT32 RequestedProtocols = 0;
1431 rdpSettings* settings = NULL;
1432 rdpNego* nego = NULL;
1437 settings = rdp->settings;
1438 WINPR_ASSERT(settings);
1443 transport_set_blocking_mode(rdp->transport, TRUE);
1445 if (!nego_read_request(nego, s))
1448 RequestedProtocols = nego_get_requested_protocols(nego);
1449 WLog_DBG(TAG,
"Client Security: RDSTLS:%d NLA:%d TLS:%d RDP:%d",
1450 (RequestedProtocols & PROTOCOL_RDSTLS) ? 1 : 0,
1451 (RequestedProtocols & PROTOCOL_HYBRID) ? 1 : 0,
1452 (RequestedProtocols & PROTOCOL_SSL) ? 1 : 0,
1453 (RequestedProtocols == PROTOCOL_RDP) ? 1 : 0);
1455 "Server Security: RDSTLS:%" PRId32
" NLA:%" PRId32
" TLS:%" PRId32
" RDP:%" PRId32
"",
1456 settings->RdstlsSecurity, settings->NlaSecurity, settings->TlsSecurity,
1457 settings->RdpSecurity);
1459 if ((settings->RdstlsSecurity) && (RequestedProtocols & PROTOCOL_RDSTLS))
1461 SelectedProtocol = PROTOCOL_RDSTLS;
1463 else if ((settings->NlaSecurity) && (RequestedProtocols & PROTOCOL_HYBRID))
1465 SelectedProtocol = PROTOCOL_HYBRID;
1467 else if ((settings->TlsSecurity) && (RequestedProtocols & PROTOCOL_SSL))
1469 SelectedProtocol = PROTOCOL_SSL;
1471 else if ((settings->RdpSecurity) && (RequestedProtocols == PROTOCOL_RDP))
1473 SelectedProtocol = PROTOCOL_RDP;
1481 SelectedProtocol = PROTOCOL_FAILED_NEGO;
1483 if (settings->RdpSecurity)
1485 WLog_ERR(TAG,
"server supports only Standard RDP Security");
1486 SelectedProtocol |= SSL_NOT_ALLOWED_BY_SERVER;
1490 if (settings->NlaSecurity && !settings->TlsSecurity)
1492 WLog_WARN(TAG,
"server supports only NLA Security");
1493 SelectedProtocol |= HYBRID_REQUIRED_BY_SERVER;
1497 WLog_WARN(TAG,
"server supports only a SSL based Security (TLS or NLA)");
1498 SelectedProtocol |= SSL_REQUIRED_BY_SERVER;
1502 WLog_ERR(TAG,
"Protocol security negotiation failure");
1505 if (!(SelectedProtocol & PROTOCOL_FAILED_NEGO))
1507 WLog_DBG(TAG,
"Negotiated Security: RDSTLS:%d NLA:%d TLS:%d RDP:%d",
1508 (SelectedProtocol & PROTOCOL_RDSTLS) ? 1 : 0,
1509 (SelectedProtocol & PROTOCOL_HYBRID) ? 1 : 0,
1510 (SelectedProtocol & PROTOCOL_SSL) ? 1 : 0,
1511 (SelectedProtocol == PROTOCOL_RDP) ? 1 : 0);
1514 if (!nego_set_selected_protocol(nego, SelectedProtocol))
1517 if (!nego_send_negotiation_response(nego))
1520 SelectedProtocol = nego_get_selected_protocol(nego);
1523 if (SelectedProtocol & PROTOCOL_RDSTLS)
1524 status = transport_accept_rdstls(rdp->transport);
1525 else if (SelectedProtocol & PROTOCOL_HYBRID)
1526 status = transport_accept_nla(rdp->transport);
1527 else if (SelectedProtocol & PROTOCOL_SSL)
1528 status = transport_accept_tls(rdp->transport);
1529 else if (SelectedProtocol == PROTOCOL_RDP)
1530 status = transport_accept_rdp(rdp->transport);
1535 transport_set_blocking_mode(rdp->transport, FALSE);
1540 static BOOL rdp_update_encryption_level(rdpSettings* settings)
1542 WINPR_ASSERT(settings);
1557 if (!settings->UseRdpSecurityLayer)
1560 EncryptionLevel = ENCRYPTION_LEVEL_NONE;
1565 switch (EncryptionLevel)
1567 case ENCRYPTION_LEVEL_NONE:
1568 WLog_INFO(TAG,
"Active rdp encryption level: NONE");
1571 case ENCRYPTION_LEVEL_FIPS:
1572 WLog_INFO(TAG,
"Active rdp encryption level: FIPS Compliant");
1575 case ENCRYPTION_LEVEL_HIGH:
1576 WLog_INFO(TAG,
"Active rdp encryption level: HIGH");
1579 case ENCRYPTION_LEVEL_LOW:
1580 WLog_INFO(TAG,
"Active rdp encryption level: LOW");
1583 case ENCRYPTION_LEVEL_CLIENT_COMPATIBLE:
1584 WLog_INFO(TAG,
"Active rdp encryption level: CLIENT-COMPATIBLE");
1588 WLog_ERR(TAG,
"Invalid server encryption level 0x%08" PRIX32
"", EncryptionLevel);
1589 WLog_ERR(TAG,
"Switching to encryption level CLIENT-COMPATIBLE");
1590 EncryptionLevel = ENCRYPTION_LEVEL_CLIENT_COMPATIBLE;
1595 switch (EncryptionLevel)
1597 case ENCRYPTION_LEVEL_NONE:
1599 EncryptionMethods = ENCRYPTION_METHOD_NONE;
1602 case ENCRYPTION_LEVEL_FIPS:
1605 if (!(EncryptionMethods & ENCRYPTION_METHOD_FIPS))
1607 WLog_WARN(TAG,
"client does not support FIPS as required by server configuration");
1610 EncryptionMethods = ENCRYPTION_METHOD_FIPS;
1613 case ENCRYPTION_LEVEL_HIGH:
1616 if (!(EncryptionMethods & ENCRYPTION_METHOD_128BIT))
1618 WLog_WARN(TAG,
"client does not support 128 bit encryption method as required by "
1619 "server configuration");
1622 EncryptionMethods = ENCRYPTION_METHOD_128BIT;
1625 case ENCRYPTION_LEVEL_LOW:
1626 case ENCRYPTION_LEVEL_CLIENT_COMPATIBLE:
1629 if (EncryptionMethods & ENCRYPTION_METHOD_128BIT)
1630 EncryptionMethods = ENCRYPTION_METHOD_128BIT;
1631 else if (EncryptionMethods & ENCRYPTION_METHOD_56BIT)
1632 EncryptionMethods = ENCRYPTION_METHOD_56BIT;
1633 else if (EncryptionMethods & ENCRYPTION_METHOD_40BIT)
1634 EncryptionMethods = ENCRYPTION_METHOD_40BIT;
1635 else if (EncryptionMethods & ENCRYPTION_METHOD_FIPS)
1636 EncryptionMethods = ENCRYPTION_METHOD_FIPS;
1639 WLog_WARN(TAG,
"client has not announced any supported encryption methods");
1640 EncryptionMethods = ENCRYPTION_METHOD_128BIT;
1646 WLog_ERR(TAG,
"internal error: unknown encryption level");
1651 if (settings->UseRdpSecurityLayer)
1653 switch (EncryptionMethods)
1655 case ENCRYPTION_METHOD_NONE:
1656 WLog_INFO(TAG,
"Selected rdp encryption method: NONE");
1659 case ENCRYPTION_METHOD_40BIT:
1660 WLog_INFO(TAG,
"Selected rdp encryption method: 40BIT");
1663 case ENCRYPTION_METHOD_56BIT:
1664 WLog_INFO(TAG,
"Selected rdp encryption method: 56BIT");
1667 case ENCRYPTION_METHOD_128BIT:
1668 WLog_INFO(TAG,
"Selected rdp encryption method: 128BIT");
1671 case ENCRYPTION_METHOD_FIPS:
1672 WLog_INFO(TAG,
"Selected rdp encryption method: FIPS");
1676 WLog_ERR(TAG,
"internal error: unknown encryption method");
1688 BOOL rdp_server_accept_mcs_connect_initial(rdpRdp* rdp,
wStream* s)
1693 rdpMcs* mcs = rdp->mcs;
1696 WINPR_ASSERT(rdp_get_state(rdp) == CONNECTION_STATE_MCS_CREATE_REQUEST);
1697 if (!mcs_recv_connect_initial(mcs, s))
1699 WINPR_ASSERT(rdp->settings);
1701 if (!mcs_server_apply_to_settings(mcs, rdp->settings))
1704 WLog_DBG(TAG,
"Accepted client: %s", rdp->settings->ClientHostname);
1705 WLog_DBG(TAG,
"Accepted channels:");
1707 WINPR_ASSERT(mcs->channels || (mcs->channelCount == 0));
1708 for (UINT32 i = 0; i < mcs->channelCount; i++)
1711 rdpMcsChannel* cur = &mcs->channels[i];
1712 const char* params[1] = { cur->Name };
1713 WLog_DBG(TAG,
" %s [%" PRIu16
"]", cur->Name, cur->ChannelId);
1714 arg = freerdp_addin_argv_new(ARRAYSIZE(params), params);
1718 if (!freerdp_static_channel_collection_add(rdp->settings, arg))
1720 freerdp_addin_argv_free(arg);
1725 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_CREATE_RESPONSE))
1727 if (!rdp_update_encryption_level(rdp->settings))
1729 if (!mcs_send_connect_response(mcs))
1731 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_ERECT_DOMAIN))
1737 BOOL rdp_server_accept_mcs_erect_domain_request(rdpRdp* rdp,
wStream* s)
1741 WINPR_ASSERT(rdp_get_state(rdp) == CONNECTION_STATE_MCS_ERECT_DOMAIN);
1743 if (!mcs_recv_erect_domain_request(rdp->mcs, s))
1746 return rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_ATTACH_USER);
1749 static BOOL rdp_server_skip_mcs_channel_join(rdpRdp* rdp)
1753 rdpMcs* mcs = rdp->mcs;
1756 mcs->userChannelJoined = TRUE;
1757 mcs->globalChannelJoined = TRUE;
1758 mcs->messageChannelJoined = TRUE;
1760 for (UINT32 i = 0; i < mcs->channelCount; i++)
1762 rdpMcsChannel* cur = &mcs->channels[i];
1763 WLog_DBG(TAG,
" %s [%" PRIu16
"]", cur->Name, cur->ChannelId);
1766 return rdp_server_transition_to_state(rdp, CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT);
1769 BOOL rdp_server_accept_mcs_attach_user_request(rdpRdp* rdp,
wStream* s)
1771 if (!mcs_recv_attach_user_request(rdp->mcs, s))
1774 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_ATTACH_USER_CONFIRM))
1777 if (!mcs_send_attach_user_confirm(rdp->mcs))
1781 return rdp_server_skip_mcs_channel_join(rdp);
1782 return rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST);
1785 BOOL rdp_server_accept_mcs_channel_join_request(rdpRdp* rdp,
wStream* s)
1787 UINT16 channelId = 0;
1788 BOOL allJoined = TRUE;
1792 WINPR_ASSERT(rdp->context);
1797 WINPR_ASSERT(rdp_get_state(rdp) == CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST);
1799 if (!mcs_recv_channel_join_request(mcs, rdp->settings, s, &channelId))
1802 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_CHANNEL_JOIN_RESPONSE))
1805 if (!mcs_send_channel_join_confirm(mcs, channelId))
1808 if (channelId == mcs->userId)
1809 mcs->userChannelJoined = TRUE;
1810 if (channelId == MCS_GLOBAL_CHANNEL_ID)
1811 mcs->globalChannelJoined = TRUE;
1812 if (channelId == mcs->messageChannelId)
1813 mcs->messageChannelJoined = TRUE;
1815 for (UINT32 i = 0; i < mcs->channelCount; i++)
1817 rdpMcsChannel* cur = &mcs->channels[i];
1818 WLog_DBG(TAG,
" %s [%" PRIu16
"]", cur->Name, cur->ChannelId);
1819 if (cur->ChannelId == channelId)
1826 CONNECTION_STATE rc = CONNECTION_STATE_INITIAL;
1827 if ((mcs->userChannelJoined) && (mcs->globalChannelJoined) &&
1828 (mcs->messageChannelId == 0 || mcs->messageChannelJoined) && allJoined)
1829 rc = CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT;
1831 rc = CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST;
1833 return rdp_server_transition_to_state(rdp, rc);
1836 static BOOL rdp_server_send_sync(rdpRdp* rdp)
1840 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_CLIENT_SYNC))
1842 if (!rdp_send_server_synchronize_pdu(rdp))
1844 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_CLIENT_COOPERATE))
1846 if (!rdp_send_server_control_cooperate_pdu(rdp))
1848 if (!rdp_finalize_reset_flags(rdp, FALSE))
1850 return rdp_server_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_SYNC);
1853 BOOL rdp_server_accept_confirm_active(rdpRdp* rdp,
wStream* s, UINT16 pduLength)
1856 WINPR_ASSERT(rdp->context);
1857 WINPR_ASSERT(rdp->settings);
1860 freerdp_peer* peer = rdp->context->peer;
1863 if (rdp_get_state(rdp) != CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE)
1866 rdp_finalize_set_flag(rdp, FINALIZE_DEACTIVATE_REACTIVATE);
1869 WLog_WARN(TAG,
"Invalid state, got %s, expected %s", rdp_get_state_string(rdp),
1870 rdp_state_string(CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE));
1875 if (!rdp_recv_confirm_active(rdp, s, pduLength))
1878 if (peer->ClientCapabilities && !peer->ClientCapabilities(peer))
1880 WLog_WARN(TAG,
"peer->ClientCapabilities failed");
1884 if (rdp->settings->SaltedChecksum)
1885 rdp->do_secure_checksum = TRUE;
1887 return rdp_server_send_sync(rdp);
1890 BOOL rdp_server_reactivate(rdpRdp* rdp)
1892 freerdp_peer* client = NULL;
1894 if (rdp->context && rdp->context->peer)
1895 client = rdp->context->peer;
1898 client->activated = FALSE;
1900 if (!rdp_send_deactivate_all(rdp))
1903 rdp_finalize_set_flag(rdp, FINALIZE_DEACTIVATE_REACTIVATE);
1904 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE))
1907 state_run_t rc = rdp_peer_handle_state_demand_active(client);
1908 return state_run_success(rc);
1911 static BOOL rdp_is_active_peer_state(CONNECTION_STATE state)
1919 case CONNECTION_STATE_FINALIZATION_CLIENT_SYNC:
1920 case CONNECTION_STATE_FINALIZATION_CLIENT_COOPERATE:
1921 case CONNECTION_STATE_FINALIZATION_CLIENT_GRANTED_CONTROL:
1922 case CONNECTION_STATE_FINALIZATION_CLIENT_FONT_MAP:
1923 case CONNECTION_STATE_ACTIVE:
1930 static BOOL rdp_is_active_client_state(CONNECTION_STATE state)
1938 case CONNECTION_STATE_FINALIZATION_SYNC:
1939 case CONNECTION_STATE_FINALIZATION_COOPERATE:
1940 case CONNECTION_STATE_FINALIZATION_REQUEST_CONTROL:
1941 case CONNECTION_STATE_FINALIZATION_PERSISTENT_KEY_LIST:
1942 case CONNECTION_STATE_FINALIZATION_FONT_LIST:
1943 case CONNECTION_STATE_FINALIZATION_CLIENT_SYNC:
1944 case CONNECTION_STATE_FINALIZATION_CLIENT_COOPERATE:
1945 case CONNECTION_STATE_FINALIZATION_CLIENT_GRANTED_CONTROL:
1946 case CONNECTION_STATE_FINALIZATION_CLIENT_FONT_MAP:
1947 case CONNECTION_STATE_ACTIVE:
1954 BOOL rdp_is_active_state(
const rdpRdp* rdp)
1957 WINPR_ASSERT(rdp->context);
1959 const CONNECTION_STATE state = rdp_get_state(rdp);
1961 return rdp_is_active_peer_state(state);
1963 return rdp_is_active_client_state(state);
1966 BOOL rdp_server_transition_to_state(rdpRdp* rdp, CONNECTION_STATE state)
1968 BOOL status = FALSE;
1969 freerdp_peer* client = NULL;
1970 const CONNECTION_STATE cstate = rdp_get_state(rdp);
1972 if (cstate >= CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT)
1974 WINPR_ASSERT(rdp->context);
1975 client = rdp->context->peer;
1978 if (!rdp_is_active_peer_state(cstate))
1981 client->activated = FALSE;
1984 WLog_Print(rdp->log, WLOG_DEBUG,
"%s --> %s", rdp_get_state_string(rdp),
1985 rdp_state_string(state));
1986 if (!rdp_set_state(rdp, state))
1994 const char* rdp_client_connection_state_string(
int state)
1998 case CLIENT_STATE_INITIAL:
1999 return "CLIENT_STATE_INITIAL";
2000 case CLIENT_STATE_PRECONNECT_PASSED:
2001 return "CLIENT_STATE_PRECONNECT_PASSED";
2002 case CLIENT_STATE_POSTCONNECT_PASSED:
2003 return "CLIENT_STATE_POSTCONNECT_PASSED";
2009 const char* rdp_state_string(CONNECTION_STATE state)
2013 case CONNECTION_STATE_INITIAL:
2014 return "CONNECTION_STATE_INITIAL";
2015 case CONNECTION_STATE_NEGO:
2016 return "CONNECTION_STATE_NEGO";
2017 case CONNECTION_STATE_NLA:
2018 return "CONNECTION_STATE_NLA";
2019 case CONNECTION_STATE_AAD:
2020 return "CONNECTION_STATE_AAD";
2021 case CONNECTION_STATE_MCS_CREATE_REQUEST:
2022 return "CONNECTION_STATE_MCS_CREATE_REQUEST";
2023 case CONNECTION_STATE_MCS_CREATE_RESPONSE:
2024 return "CONNECTION_STATE_MCS_CREATE_RESPONSE";
2025 case CONNECTION_STATE_MCS_ERECT_DOMAIN:
2026 return "CONNECTION_STATE_MCS_ERECT_DOMAIN";
2027 case CONNECTION_STATE_MCS_ATTACH_USER:
2028 return "CONNECTION_STATE_MCS_ATTACH_USER";
2029 case CONNECTION_STATE_MCS_ATTACH_USER_CONFIRM:
2030 return "CONNECTION_STATE_MCS_ATTACH_USER_CONFIRM";
2031 case CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST:
2032 return "CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST";
2033 case CONNECTION_STATE_MCS_CHANNEL_JOIN_RESPONSE:
2034 return "CONNECTION_STATE_MCS_CHANNEL_JOIN_RESPONSE";
2035 case CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT:
2036 return "CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT";
2037 case CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE:
2038 return "CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE";
2039 case CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_REQUEST:
2040 return "CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_REQUEST";
2041 case CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_RESPONSE:
2042 return "CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_RESPONSE";
2043 case CONNECTION_STATE_LICENSING:
2044 return "CONNECTION_STATE_LICENSING";
2045 case CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_REQUEST:
2046 return "CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_REQUEST";
2047 case CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_RESPONSE:
2048 return "CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_RESPONSE";
2049 case CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE:
2050 return "CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE";
2051 case CONNECTION_STATE_CAPABILITIES_EXCHANGE_MONITOR_LAYOUT:
2052 return "CONNECTION_STATE_CAPABILITIES_EXCHANGE_MONITOR_LAYOUT";
2053 case CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE:
2054 return "CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE";
2055 case CONNECTION_STATE_FINALIZATION_SYNC:
2056 return "CONNECTION_STATE_FINALIZATION_SYNC";
2057 case CONNECTION_STATE_FINALIZATION_COOPERATE:
2058 return "CONNECTION_STATE_FINALIZATION_COOPERATE";
2059 case CONNECTION_STATE_FINALIZATION_REQUEST_CONTROL:
2060 return "CONNECTION_STATE_FINALIZATION_REQUEST_CONTROL";
2061 case CONNECTION_STATE_FINALIZATION_PERSISTENT_KEY_LIST:
2062 return "CONNECTION_STATE_FINALIZATION_PERSISTENT_KEY_LIST";
2063 case CONNECTION_STATE_FINALIZATION_FONT_LIST:
2064 return "CONNECTION_STATE_FINALIZATION_FONT_LIST";
2065 case CONNECTION_STATE_FINALIZATION_CLIENT_SYNC:
2066 return "CONNECTION_STATE_FINALIZATION_CLIENT_SYNC";
2067 case CONNECTION_STATE_FINALIZATION_CLIENT_COOPERATE:
2068 return "CONNECTION_STATE_FINALIZATION_CLIENT_COOPERATE";
2069 case CONNECTION_STATE_FINALIZATION_CLIENT_GRANTED_CONTROL:
2070 return "CONNECTION_STATE_FINALIZATION_CLIENT_GRANTED_CONTROL";
2071 case CONNECTION_STATE_FINALIZATION_CLIENT_FONT_MAP:
2072 return "CONNECTION_STATE_FINALIZATION_CLIENT_FONT_MAP";
2073 case CONNECTION_STATE_ACTIVE:
2074 return "CONNECTION_STATE_ACTIVE";
2080 CONNECTION_STATE rdp_get_state(
const rdpRdp* rdp)
2086 BOOL rdp_set_state(rdpRdp* rdp, CONNECTION_STATE state)
2093 const char* rdp_get_state_string(
const rdpRdp* rdp)
2095 CONNECTION_STATE state = rdp_get_state(rdp);
2096 return rdp_state_string(state);
2099 BOOL rdp_channels_from_mcs(rdpSettings* settings,
const rdpRdp* rdp)
2101 const rdpMcs* mcs = NULL;
2112 for (UINT32 x = 0; x < mcs->channelCount; x++)
2114 const rdpMcsChannel* mchannel = &mcs->channels[x];
2117 memcpy(cur.name, mchannel->Name,
sizeof(cur.name));
2118 cur.options = mchannel->options;
2119 if (!freerdp_settings_set_pointer_array(settings, FreeRDP_ChannelDefArray, x, &cur))
2132 state_run_t rdp_client_connect_confirm_active(rdpRdp* rdp,
wStream* s)
2135 WINPR_ASSERT(rdp->settings);
2138 const UINT32 width = rdp->settings->DesktopWidth;
2139 const UINT32 height = rdp->settings->DesktopHeight;
2141 if (!rdp_send_confirm_active(rdp))
2142 return STATE_RUN_FAILED;
2144 if (!input_register_client_callbacks(rdp->input))
2146 WLog_ERR(TAG,
"error registering client callbacks");
2147 return STATE_RUN_FAILED;
2154 const BOOL deactivate_reactivate =
2155 rdp->was_deactivated && ((rdp->deactivated_width != rdp->settings->DesktopWidth) ||
2156 (rdp->deactivated_height != rdp->settings->DesktopHeight));
2157 const BOOL resolution_change =
2158 ((width != rdp->settings->DesktopWidth) || (height != rdp->settings->DesktopHeight));
2159 if (deactivate_reactivate || resolution_change)
2162 IFCALLRET(rdp->update->DesktopResize, status, rdp->update->context);
2166 WLog_ERR(TAG,
"client desktop resize callback failed");
2167 return STATE_RUN_FAILED;
2171 WINPR_ASSERT(rdp->context);
2172 if (freerdp_shall_disconnect_context(rdp->context))
2173 return STATE_RUN_SUCCESS;
2175 state_run_t status = STATE_RUN_SUCCESS;
2176 if (!rdp->settings->SupportMonitorLayoutPdu)
2177 status = rdp_client_connect_finalize(rdp);
2180 if (!rdp_client_transition_to_state(rdp,
2181 CONNECTION_STATE_CAPABILITIES_EXCHANGE_MONITOR_LAYOUT))
2182 status = STATE_RUN_FAILED;
2184 if (!rdp_finalize_reset_flags(rdp, FALSE))
2185 status = STATE_RUN_FAILED;
2189 BOOL rdp_handle_optional_rdp_decryption(rdpRdp* rdp,
wStream* s, UINT16* length,
2190 UINT16* pSecurityFlags)
2194 WINPR_ASSERT(rdp->settings);
2196 UINT16 securityFlags = 0;
2197 if (rdp->settings->UseRdpSecurityLayer)
2199 if (!rdp_read_security_header(rdp, s, &securityFlags, length))
2202 if (securityFlags & SEC_ENCRYPT)
2204 if (!rdp_decrypt(rdp, s, length, securityFlags))
2213 *pSecurityFlags = securityFlags;
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_set_string(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *param)
Sets a string settings value. The param is copied.
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 const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
FREERDP_API BOOL freerdp_settings_set_pointer_len(rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id, const void *data, size_t len)
Set a pointer to value data.
FREERDP_API UINT32 freerdp_settings_get_codecs_flags(const rdpSettings *settings)
helper function to get a mask of supported codec flags.
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.
FREERDP_API void * freerdp_settings_get_pointer_writable(rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id)
Returns a mutable pointer settings value.