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);
322 rdp_log_build_warnings(rdp);
330 if (settings->FIPSMode || winpr_FIPSMode())
332 settings->NlaSecurity = FALSE;
333 settings->EncryptionMethods = ENCRYPTION_METHOD_FIPS;
337 if (settings->GatewayArmTransport)
339 if (!arm_resolve_endpoint(rdp->log, rdp->context, TcpConnectTimeout))
341 WLog_ERR(TAG,
"error retrieving ARM configuration");
346 const char* hostname = settings->ServerHostname;
349 WLog_ERR(TAG,
"Missing hostname, can not connect to NULL target");
353 const UINT32 port = settings->ServerPort;
354 WINPR_ASSERT(port <= UINT32_MAX);
356 nego_init(rdp->nego);
357 nego_set_target(rdp->nego, hostname, (UINT16)port);
359 if (settings->GatewayEnabled)
363 size_t user_length = 0;
365 if (settings->Username)
367 user = settings->Username;
368 user_length = strlen(settings->Username);
371 if (settings->Domain)
372 domain = settings->Domain;
374 domain = settings->ComputerName;
376 const size_t domain_length = strlen(domain);
377 const size_t cookie_length = domain_length + 1 + user_length;
378 char* cookie = malloc(cookie_length + 1);
383 CopyMemory(cookie, domain, domain_length);
384 WINPR_ASSERT(domain_length <= UINT32_MAX);
385 CharUpperBuffA(cookie, (UINT32)domain_length);
386 cookie[domain_length] =
'\\';
388 if (settings->Username)
389 CopyMemory(&cookie[domain_length + 1], user, user_length);
391 cookie[cookie_length] =
'\0';
392 status = nego_set_cookie(rdp->nego, cookie);
397 status = nego_set_cookie(rdp->nego, settings->Username);
403 nego_set_childsession_enabled(rdp->nego, settings->ConnectChildSession);
404 nego_set_send_preconnection_pdu(rdp->nego, settings->SendPreconnectionPdu);
405 nego_set_preconnection_id(rdp->nego, settings->PreconnectionId);
406 nego_set_preconnection_blob(rdp->nego, settings->PreconnectionBlob);
407 nego_set_negotiation_enabled(rdp->nego, settings->NegotiateSecurityLayer);
408 nego_set_restricted_admin_mode_required(rdp->nego, settings->RestrictedAdminModeRequired);
409 nego_set_RCG_required(rdp->nego, settings->RemoteCredentialGuard);
410 nego_set_gateway_enabled(rdp->nego, settings->GatewayEnabled);
411 nego_set_gateway_bypass_local(rdp->nego, settings->GatewayBypassLocal);
412 nego_enable_rdp(rdp->nego, settings->RdpSecurity);
413 nego_enable_tls(rdp->nego, settings->TlsSecurity);
414 nego_enable_nla(rdp->nego, settings->NlaSecurity);
415 nego_enable_ext(rdp->nego, settings->ExtSecurity);
416 nego_enable_rdstls(rdp->nego, settings->RdstlsSecurity);
417 nego_enable_aad(rdp->nego, settings->AadSecurity);
419 if (settings->MstscCookieMode)
420 settings->CookieMaxLength = MSTSC_COOKIE_MAX_LENGTH;
422 nego_set_cookie_max_length(rdp->nego, settings->CookieMaxLength);
424 if (settings->LoadBalanceInfo && (settings->LoadBalanceInfoLength > 0))
426 if (!nego_set_routing_token(rdp->nego, settings->LoadBalanceInfo,
427 settings->LoadBalanceInfoLength))
433 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_NEGO))
436 if (!nego_connect(rdp->nego))
438 if (!freerdp_get_last_error(rdp->context))
440 freerdp_set_last_error_log(rdp->context,
441 FREERDP_ERROR_SECURITY_NEGO_CONNECT_FAILED);
442 WLog_ERR(TAG,
"Error: protocol security negotiation or connection failure");
448 SelectedProtocol = nego_get_selected_protocol(rdp->nego);
450 if ((SelectedProtocol & PROTOCOL_SSL) || (SelectedProtocol == PROTOCOL_RDP) ||
451 (SelectedProtocol == PROTOCOL_RDSTLS))
455 if ((settings->Username != NULL) &&
457 (settings->RedirectionPassword != NULL &&
458 settings->RedirectionPasswordLength > 0)))
459 settings->AutoLogonEnabled = TRUE;
461 if (rdp_recv_callback(rdp->transport, &s, rdp) < 0)
465 transport_set_blocking_mode(rdp->transport, FALSE);
469 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_CREATE_REQUEST))
474 if (!transport_set_recv_callbacks(rdp->transport, rdp_recv_callback, rdp))
477 return rdp_client_wait_for_activation(rdp);
480 BOOL rdp_client_disconnect(rdpRdp* rdp)
482 rdpContext* context = NULL;
484 if (!rdp || !rdp->settings || !rdp->context)
487 context = rdp->context;
491 if (!nego_disconnect(rdp->nego))
495 if (!transport_disconnect(rdp->transport))
501 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_INITIAL))
504 if (freerdp_channels_disconnect(context->channels, context->instance) != CHANNEL_RC_OK)
507 freerdp_client_codecs_free(context->codecs);
508 context->codecs = NULL;
512 BOOL rdp_client_disconnect_and_clear(rdpRdp* rdp)
514 rdpContext* context = NULL;
516 if (!rdp_client_disconnect(rdp))
521 context = rdp->context;
522 WINPR_ASSERT(context);
524 if (freerdp_get_last_error(context) == FREERDP_ERROR_CONNECT_CANCELLED)
527 context->LastError = FREERDP_ERROR_SUCCESS;
528 clearChannelError(context);
529 return utils_reset_abort(rdp);
532 static BOOL rdp_client_reconnect_channels(rdpRdp* rdp, BOOL redirect)
535 rdpContext* context = NULL;
537 if (!rdp || !rdp->context || !rdp->context->channels)
540 context = rdp->context;
542 if (context->instance->ConnectionCallbackState == CLIENT_STATE_INITIAL)
545 if (context->instance->ConnectionCallbackState == CLIENT_STATE_PRECONNECT_PASSED)
550 pointer_cache_register_callbacks(context->update);
552 if (!IFCALLRESULT(FALSE, context->instance->PostConnect, context->instance))
555 context->instance->ConnectionCallbackState = CLIENT_STATE_POSTCONNECT_PASSED;
558 if (context->instance->ConnectionCallbackState == CLIENT_STATE_POSTCONNECT_PASSED)
560 (freerdp_channels_post_connect(context->channels, context->instance) == CHANNEL_RC_OK);
565 static BOOL rdp_client_redirect_resolvable(
const char* host)
567 struct addrinfo* result = freerdp_tcp_resolve_host(host, -1, 0);
572 freeaddrinfo(result);
576 static BOOL rdp_client_redirect_try_fqdn(rdpSettings* settings)
578 if (settings->RedirectionFlags & LB_TARGET_FQDN)
580 if (settings->GatewayEnabled ||
581 rdp_client_redirect_resolvable(settings->RedirectionTargetFQDN))
584 settings->RedirectionTargetFQDN))
594 static BOOL rdp_client_redirect_try_ip(rdpSettings* settings)
596 if (settings->RedirectionFlags & LB_TARGET_NET_ADDRESS)
599 settings->TargetNetAddress))
608 static BOOL rdp_client_redirect_try_netbios(rdpSettings* settings)
610 if (settings->RedirectionFlags & LB_TARGET_NETBIOS_NAME)
612 if (settings->GatewayEnabled ||
613 rdp_client_redirect_resolvable(settings->RedirectionTargetNetBiosName))
616 settings->RedirectionTargetNetBiosName))
626 BOOL rdp_client_redirect(rdpRdp* rdp)
629 rdpSettings* settings = NULL;
631 if (!rdp_client_disconnect_and_clear(rdp))
636 freerdp_channels_disconnect(rdp->context->channels, rdp->context->instance);
637 freerdp_channels_close(rdp->context->channels, rdp->context->instance);
639 if (rdp_redirection_apply_settings(rdp) != 0)
644 settings = rdp->settings;
645 WINPR_ASSERT(settings);
647 if ((settings->RedirectionFlags & LB_LOAD_BALANCE_INFO) == 0)
649 BOOL haveRedirectAddress = FALSE;
650 UINT32 redirectionMask = settings->RedirectionPreferType;
654 const BOOL tryFQDN = (redirectionMask & 0x01) == 0;
655 const BOOL tryNetAddress = (redirectionMask & 0x02) == 0;
656 const BOOL tryNetbios = (redirectionMask & 0x04) == 0;
658 if (tryFQDN && !haveRedirectAddress)
659 haveRedirectAddress = rdp_client_redirect_try_fqdn(settings);
661 if (tryNetAddress && !haveRedirectAddress)
662 haveRedirectAddress = rdp_client_redirect_try_ip(settings);
664 if (tryNetbios && !haveRedirectAddress)
665 haveRedirectAddress = rdp_client_redirect_try_netbios(settings);
667 redirectionMask >>= 3;
668 }
while (!haveRedirectAddress && (redirectionMask != 0));
671 if (settings->RedirectionFlags & LB_USERNAME)
674 settings, FreeRDP_Username,
679 if (settings->RedirectionFlags & LB_DOMAIN)
682 settings, FreeRDP_Domain,
687 settings->RdstlsSecurity =
688 (settings->RedirectionFlags & LB_PASSWORD_IS_PK_ENCRYPTED) != 0 ? TRUE : FALSE;
690 WINPR_ASSERT(rdp->context);
691 WINPR_ASSERT(rdp->context->instance);
692 if (!IFCALLRESULT(TRUE, rdp->context->instance->Redirect, rdp->context->instance))
695 BOOL ok = utils_reload_channels(rdp->context);
699 status = rdp_client_connect(rdp);
702 status = rdp_client_reconnect_channels(rdp, TRUE);
707 BOOL rdp_client_reconnect(rdpRdp* rdp)
711 if (!rdp_client_disconnect_and_clear(rdp))
714 status = rdp_client_connect(rdp);
717 status = rdp_client_reconnect_channels(rdp, FALSE);
722 static const BYTE fips_ivec[8] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
724 static BOOL rdp_client_establish_keys(rdpRdp* rdp)
731 rdpSettings* settings = rdp->settings;
732 BYTE* crypt_client_random = NULL;
734 WINPR_ASSERT(settings);
735 if (!settings->UseRdpSecurityLayer)
741 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT))
746 CLIENT_RANDOM_LENGTH))
748 winpr_RAND(settings->ClientRandom, settings->ClientRandomLength);
750 const rdpCertInfo* info = freerdp_certificate_get_info(settings->RdpServerCertificate);
753 WLog_ERR(TAG,
"Failed to get rdpCertInfo from RdpServerCertificate");
761 crypt_client_random = calloc(info->ModulusLength, 1);
763 if (!crypt_client_random)
766 crypto_rsa_public_encrypt(settings->ClientRandom, settings->ClientRandomLength, info,
767 crypt_client_random, info->ModulusLength);
769 const size_t length = RDP_PACKET_HEADER_MAX_LENGTH + RDP_SECURITY_HEADER_LENGTH + 4ULL +
770 info->ModulusLength + 8ULL;
771 if (length > UINT16_MAX)
774 s = Stream_New(NULL, length);
778 WLog_ERR(TAG,
"Stream_New failed!");
782 if (!rdp_write_header(rdp, s, length, MCS_GLOBAL_CHANNEL_ID))
784 if (!rdp_write_security_header(rdp, s, SEC_EXCHANGE_PKT | SEC_LICENSE_ENCRYPT_SC))
787 Stream_Write_UINT32(s, info->ModulusLength + 8);
788 Stream_Write(s, crypt_client_random, info->ModulusLength);
790 Stream_SealLength(s);
791 status = transport_write(rdp->mcs->transport, s);
796 rdp->do_crypt_license = TRUE;
799 if (!security_establish_keys(rdp))
802 rdp->do_crypt = TRUE;
804 if (settings->SaltedChecksum)
805 rdp->do_secure_checksum = TRUE;
807 if (settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
810 winpr_Cipher_NewEx(WINPR_CIPHER_DES_EDE3_CBC, WINPR_ENCRYPT, rdp->fips_encrypt_key,
811 sizeof(rdp->fips_encrypt_key), fips_ivec,
sizeof(fips_ivec));
813 if (!rdp->fips_encrypt)
815 WLog_ERR(TAG,
"unable to allocate des3 encrypt key");
820 winpr_Cipher_NewEx(WINPR_CIPHER_DES_EDE3_CBC, WINPR_DECRYPT, rdp->fips_decrypt_key,
821 sizeof(rdp->fips_decrypt_key), fips_ivec,
sizeof(fips_ivec));
823 if (!rdp->fips_decrypt)
825 WLog_ERR(TAG,
"unable to allocate des3 decrypt key");
833 if (!rdp_reset_rc4_encrypt_keys(rdp))
835 if (!rdp_reset_rc4_decrypt_keys(rdp))
840 Stream_Free(s, TRUE);
841 free(crypt_client_random);
845 winpr_Cipher_Free(rdp->fips_decrypt);
846 winpr_Cipher_Free(rdp->fips_encrypt);
847 rdp->fips_decrypt = NULL;
848 rdp->fips_encrypt = NULL;
850 rdp_free_rc4_decrypt_keys(rdp);
851 rdp_free_rc4_encrypt_keys(rdp);
857 static BOOL rdp_update_client_random(rdpSettings* settings,
const BYTE* crypt_random,
858 size_t crypt_random_len)
860 const size_t length = 32;
861 WINPR_ASSERT(settings);
866 const rdpCertInfo* cinfo = freerdp_key_get_info(rsa);
869 if (crypt_random_len != cinfo->ModulusLength + 8)
871 WLog_ERR(TAG,
"invalid encrypted client random length");
878 WINPR_ASSERT(client_random);
879 return crypto_rsa_private_decrypt(crypt_random, crypt_random_len - 8, rsa, client_random,
883 BOOL rdp_server_establish_keys(rdpRdp* rdp,
wStream* s)
886 UINT16 channel_id = 0;
888 UINT16 sec_flags = 0;
893 if (!rdp->settings->UseRdpSecurityLayer)
899 if (!rdp_read_header(rdp, s, &length, &channel_id))
902 if (!rdp_read_security_header(rdp, s, &sec_flags, NULL))
904 WLog_ERR(TAG,
"invalid security header");
908 if ((sec_flags & SEC_EXCHANGE_PKT) == 0)
910 WLog_ERR(TAG,
"missing SEC_EXCHANGE_PKT in security header");
914 rdp->do_crypt_license = (sec_flags & SEC_LICENSE_ENCRYPT_SC) != 0 ? TRUE : FALSE;
916 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
919 Stream_Read_UINT32(s, rand_len);
922 if (!Stream_CheckAndLogRequiredLength(TAG, s, rand_len))
925 const BYTE* crypt_random = Stream_ConstPointer(s);
926 if (!Stream_SafeSeek(s, rand_len))
928 if (!rdp_update_client_random(rdp->settings, crypt_random, rand_len))
932 if (!security_establish_keys(rdp))
935 rdp->do_crypt = TRUE;
937 if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
940 winpr_Cipher_NewEx(WINPR_CIPHER_DES_EDE3_CBC, WINPR_ENCRYPT, rdp->fips_encrypt_key,
941 sizeof(rdp->fips_encrypt_key), fips_ivec,
sizeof(fips_ivec));
943 if (!rdp->fips_encrypt)
945 WLog_ERR(TAG,
"unable to allocate des3 encrypt key");
950 winpr_Cipher_NewEx(WINPR_CIPHER_DES_EDE3_CBC, WINPR_DECRYPT, rdp->fips_decrypt_key,
951 sizeof(rdp->fips_decrypt_key), fips_ivec,
sizeof(fips_ivec));
953 if (!rdp->fips_decrypt)
955 WLog_ERR(TAG,
"unable to allocate des3 decrypt key");
963 if (!rdp_reset_rc4_encrypt_keys(rdp))
966 if (!rdp_reset_rc4_decrypt_keys(rdp))
969 ret = tpkt_ensure_stream_consumed(s, length);
974 winpr_Cipher_Free(rdp->fips_encrypt);
975 winpr_Cipher_Free(rdp->fips_decrypt);
976 rdp->fips_encrypt = NULL;
977 rdp->fips_decrypt = NULL;
979 rdp_free_rc4_encrypt_keys(rdp);
980 rdp_free_rc4_decrypt_keys(rdp);
986 static BOOL rdp_client_send_client_info_and_change_state(rdpRdp* rdp)
989 if (!rdp_client_establish_keys(rdp))
991 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE))
993 if (!rdp_send_client_info(rdp))
995 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_REQUEST))
1000 BOOL rdp_client_skip_mcs_channel_join(rdpRdp* rdp)
1004 rdpMcs* mcs = rdp->mcs;
1007 mcs->userChannelJoined = TRUE;
1008 mcs->globalChannelJoined = TRUE;
1009 mcs->messageChannelJoined = TRUE;
1011 for (UINT32 i = 0; i < mcs->channelCount; i++)
1013 rdpMcsChannel* cur = &mcs->channels[i];
1014 WLog_DBG(TAG,
" %s [%" PRIu16
"]", cur->Name, cur->ChannelId);
1018 return rdp_client_send_client_info_and_change_state(rdp);
1021 static BOOL rdp_client_join_channel(rdpRdp* rdp, UINT16 ChannelId)
1025 rdpMcs* mcs = rdp->mcs;
1026 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST))
1028 if (!mcs_send_channel_join_request(mcs, ChannelId))
1030 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_CHANNEL_JOIN_RESPONSE))
1035 BOOL rdp_client_connect_mcs_channel_join_confirm(rdpRdp* rdp,
wStream* s)
1037 UINT16 channelId = 0;
1038 BOOL allJoined = TRUE;
1041 rdpMcs* mcs = rdp->mcs;
1043 if (!mcs_recv_channel_join_confirm(mcs, s, &channelId))
1046 if (!mcs->userChannelJoined)
1048 if (channelId != mcs->userId)
1050 WLog_ERR(TAG,
"expected user channel id %" PRIu16
", but received %" PRIu16,
1051 mcs->userId, channelId);
1055 mcs->userChannelJoined = TRUE;
1056 if (!rdp_client_join_channel(rdp, MCS_GLOBAL_CHANNEL_ID))
1059 else if (!mcs->globalChannelJoined)
1061 if (channelId != MCS_GLOBAL_CHANNEL_ID)
1063 WLog_ERR(TAG,
"expected uglobalser channel id %" PRIu16
", but received %" PRIu16,
1064 MCS_GLOBAL_CHANNEL_ID, channelId);
1067 mcs->globalChannelJoined = TRUE;
1069 if (mcs->messageChannelId != 0)
1071 if (!rdp_client_join_channel(rdp, mcs->messageChannelId))
1077 if (mcs->channelCount > 0)
1079 const rdpMcsChannel* cur = &mcs->channels[0];
1080 if (!rdp_client_join_channel(rdp, cur->ChannelId))
1086 else if ((mcs->messageChannelId != 0) && !mcs->messageChannelJoined)
1088 if (channelId != mcs->messageChannelId)
1090 WLog_ERR(TAG,
"expected messageChannelId=%" PRIu16
", got %" PRIu16,
1091 mcs->messageChannelId, channelId);
1095 mcs->messageChannelJoined = TRUE;
1097 if (mcs->channelCount > 0)
1099 const rdpMcsChannel* cur = &mcs->channels[0];
1100 if (!rdp_client_join_channel(rdp, cur->ChannelId))
1108 for (; i < mcs->channelCount; i++)
1110 rdpMcsChannel* cur = &mcs->channels[i];
1114 if (cur->ChannelId != channelId)
1116 WLog_ERR(TAG,
"expected channel id %" PRIu16
", but received %" PRIu16,
1117 MCS_GLOBAL_CHANNEL_ID, channelId);
1124 if (i + 1 < mcs->channelCount)
1126 const rdpMcsChannel* cur = &mcs->channels[i + 1];
1127 if (!rdp_client_join_channel(rdp, cur->ChannelId))
1133 if (mcs->userChannelJoined && mcs->globalChannelJoined && allJoined)
1135 if (!rdp_client_send_client_info_and_change_state(rdp))
1142 BOOL rdp_handle_message_channel(rdpRdp* rdp,
wStream* s, UINT16 channelId, UINT16 length)
1145 WINPR_ASSERT(rdp->mcs);
1147 if (!rdp->mcs->messageChannelJoined)
1149 WLog_Print(rdp->log, WLOG_WARN,
"MCS message channel not joined!");
1152 const UINT16 messageChannelId = rdp->mcs->messageChannelId;
1153 if (messageChannelId == 0)
1155 WLog_Print(rdp->log, WLOG_WARN,
"MCS message channel id == 0");
1159 if (channelId != messageChannelId)
1161 WLog_Print(rdp->log, WLOG_WARN,
"MCS message channel expected id=%" PRIu16
", got %" PRIu16,
1162 messageChannelId, channelId);
1166 UINT16 securityFlags = 0;
1167 if (!rdp_read_security_header(rdp, s, &securityFlags, &length))
1170 if (securityFlags & SEC_ENCRYPT)
1172 if (!rdp_decrypt(rdp, s, &length, securityFlags))
1176 if (rdp_recv_message_channel_pdu(rdp, s, securityFlags) != STATE_RUN_SUCCESS)
1179 return tpkt_ensure_stream_consumed(s, length);
1182 BOOL rdp_client_connect_auto_detect(rdpRdp* rdp,
wStream* s)
1185 WINPR_ASSERT(rdp->mcs);
1187 const size_t pos = Stream_GetPosition(s);
1189 UINT16 channelId = 0;
1191 if (rdp_read_header(rdp, s, &length, &channelId))
1193 const UINT16 messageChannelId = rdp->mcs->messageChannelId;
1197 if (rdp->mcs->messageChannelJoined && (channelId == messageChannelId))
1199 if (rdp_handle_message_channel(rdp, s, channelId, length))
1204 WLog_WARN(TAG,
"expected messageChannelId=%" PRIu16
", got %" PRIu16, messageChannelId,
1209 Stream_SetPosition(s, pos);
1213 state_run_t rdp_client_connect_license(rdpRdp* rdp,
wStream* s)
1215 state_run_t status = STATE_RUN_FAILED;
1216 LICENSE_STATE state = LICENSE_STATE_ABORTED;
1218 UINT16 channelId = 0;
1219 UINT16 securityFlags = 0;
1222 if (!rdp_read_header(rdp, s, &length, &channelId))
1223 return STATE_RUN_FAILED;
1228 const UINT16 messageChannelId = rdp->mcs->messageChannelId;
1229 if (rdp->mcs->messageChannelJoined && (channelId == messageChannelId))
1231 if (!rdp_handle_message_channel(rdp, s, channelId, length))
1232 return STATE_RUN_FAILED;
1233 return STATE_RUN_SUCCESS;
1236 if (!rdp_read_security_header(rdp, s, &securityFlags, &length))
1237 return STATE_RUN_FAILED;
1239 if (securityFlags & SEC_ENCRYPT)
1241 if (!rdp_decrypt(rdp, s, &length, securityFlags))
1242 return STATE_RUN_FAILED;
1245 if (channelId != MCS_GLOBAL_CHANNEL_ID)
1246 WLog_WARN(TAG,
"unexpected message for channel %u, expected %u", channelId,
1247 MCS_GLOBAL_CHANNEL_ID);
1249 if ((securityFlags & SEC_LICENSE_PKT) == 0)
1251 char buffer[512] = { 0 };
1252 char lbuffer[32] = { 0 };
1253 WLog_ERR(TAG,
"securityFlags=%s, missing required flag %s",
1254 rdp_security_flag_string(securityFlags, buffer,
sizeof(buffer)),
1255 rdp_security_flag_string(SEC_LICENSE_PKT, lbuffer,
sizeof(lbuffer)));
1256 return STATE_RUN_FAILED;
1259 status = license_recv(rdp->license, s);
1261 if (state_run_failed(status))
1264 state = license_get_state(rdp->license);
1267 case LICENSE_STATE_ABORTED:
1268 WLog_ERR(TAG,
"license connection sequence aborted.");
1269 return STATE_RUN_FAILED;
1270 case LICENSE_STATE_COMPLETED:
1271 if (rdp->settings->MultitransportFlags)
1273 if (!rdp_client_transition_to_state(
1274 rdp, CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_REQUEST))
1275 return STATE_RUN_FAILED;
1279 if (!rdp_client_transition_to_state(
1280 rdp, CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE))
1281 return STATE_RUN_FAILED;
1283 return STATE_RUN_SUCCESS;
1285 return STATE_RUN_SUCCESS;
1289 state_run_t rdp_client_connect_demand_active(rdpRdp* rdp,
wStream* s)
1292 UINT16 channelId = 0;
1294 UINT16 pduSource = 0;
1298 WINPR_ASSERT(rdp->settings);
1300 if (!rdp_recv_get_active_header(rdp, s, &channelId, &length))
1301 return STATE_RUN_FAILED;
1303 if (freerdp_shall_disconnect_context(rdp->context))
1304 return STATE_RUN_QUIT_SESSION;
1306 if (rdp->mcs->messageChannelId && (channelId == rdp->mcs->messageChannelId))
1309 if (!rdp_handle_message_channel(rdp, s, channelId, length))
1310 return STATE_RUN_FAILED;
1311 return STATE_RUN_SUCCESS;
1314 if (!rdp_handle_optional_rdp_decryption(rdp, s, &length, NULL))
1315 return STATE_RUN_FAILED;
1317 if (!rdp_read_share_control_header(rdp, s, NULL, NULL, &pduType, &pduSource))
1318 return STATE_RUN_FAILED;
1322 case PDU_TYPE_DEMAND_ACTIVE:
1323 if (!rdp_recv_demand_active(rdp, s, pduSource, length))
1324 return STATE_RUN_FAILED;
1325 return STATE_RUN_ACTIVE;
1327 return rdp_recv_out_of_sequence_pdu(rdp, s, pduType, length);
1331 state_run_t rdp_client_connect_finalize(rdpRdp* rdp)
1340 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_SYNC))
1341 return STATE_RUN_FAILED;
1343 if (!rdp_send_client_synchronize_pdu(rdp))
1344 return STATE_RUN_FAILED;
1346 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_COOPERATE))
1347 return STATE_RUN_FAILED;
1348 if (!rdp_send_client_control_pdu(rdp, CTRLACTION_COOPERATE))
1349 return STATE_RUN_FAILED;
1351 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_REQUEST_CONTROL))
1352 return STATE_RUN_FAILED;
1353 if (!rdp_send_client_control_pdu(rdp, CTRLACTION_REQUEST_CONTROL))
1354 return STATE_RUN_FAILED;
1363 if (!rdp_finalize_is_flag_set(rdp, FINALIZE_DEACTIVATE_REACTIVATE) &&
1366 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_PERSISTENT_KEY_LIST))
1367 return STATE_RUN_FAILED;
1368 if (!rdp_send_client_persistent_key_list_pdu(rdp))
1369 return STATE_RUN_FAILED;
1372 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_FONT_LIST))
1373 return STATE_RUN_FAILED;
1374 if (!rdp_send_client_font_list_pdu(rdp, FONTLIST_FIRST | FONTLIST_LAST))
1375 return STATE_RUN_FAILED;
1377 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_CLIENT_SYNC))
1378 return STATE_RUN_FAILED;
1379 return STATE_RUN_SUCCESS;
1382 BOOL rdp_client_transition_to_state(rdpRdp* rdp, CONNECTION_STATE state)
1384 const char* name = rdp_state_string(state);
1387 WLog_Print(rdp->log, WLOG_DEBUG,
"%s --> %s", rdp_get_state_string(rdp), name);
1389 if (!rdp_set_state(rdp, state))
1394 case CONNECTION_STATE_FINALIZATION_SYNC:
1395 case CONNECTION_STATE_FINALIZATION_COOPERATE:
1396 case CONNECTION_STATE_FINALIZATION_REQUEST_CONTROL:
1397 case CONNECTION_STATE_FINALIZATION_PERSISTENT_KEY_LIST:
1398 case CONNECTION_STATE_FINALIZATION_FONT_LIST:
1399 update_reset_state(rdp->update);
1402 case CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE:
1404 ActivatedEventArgs activatedEvent = { 0 };
1405 rdpContext* context = rdp->context;
1406 EventArgsInit(&activatedEvent,
"libfreerdp");
1407 activatedEvent.firstActivation =
1408 !rdp_finalize_is_flag_set(rdp, FINALIZE_DEACTIVATE_REACTIVATE);
1409 PubSub_OnActivated(rdp->pubSub, context, &activatedEvent);
1419 ConnectionStateChangeEventArgs stateEvent = { 0 };
1420 rdpContext* context = rdp->context;
1421 EventArgsInit(&stateEvent,
"libfreerdp");
1422 stateEvent.state = WINPR_ASSERTING_INT_CAST(int32_t, rdp_get_state(rdp));
1423 stateEvent.active = rdp_is_active_state(rdp);
1424 PubSub_OnConnectionStateChange(rdp->pubSub, context, &stateEvent);
1430 BOOL rdp_server_accept_nego(rdpRdp* rdp,
wStream* s)
1432 UINT32 SelectedProtocol = 0;
1433 UINT32 RequestedProtocols = 0;
1435 rdpSettings* settings = NULL;
1436 rdpNego* nego = NULL;
1441 settings = rdp->settings;
1442 WINPR_ASSERT(settings);
1447 transport_set_blocking_mode(rdp->transport, TRUE);
1449 if (!nego_read_request(nego, s))
1452 RequestedProtocols = nego_get_requested_protocols(nego);
1453 WLog_DBG(TAG,
"Client Security: RDSTLS:%d NLA:%d TLS:%d RDP:%d",
1454 (RequestedProtocols & PROTOCOL_RDSTLS) ? 1 : 0,
1455 (RequestedProtocols & PROTOCOL_HYBRID) ? 1 : 0,
1456 (RequestedProtocols & PROTOCOL_SSL) ? 1 : 0,
1457 (RequestedProtocols == PROTOCOL_RDP) ? 1 : 0);
1459 "Server Security: RDSTLS:%" PRId32
" NLA:%" PRId32
" TLS:%" PRId32
" RDP:%" PRId32
"",
1460 settings->RdstlsSecurity, settings->NlaSecurity, settings->TlsSecurity,
1461 settings->RdpSecurity);
1463 if ((settings->RdstlsSecurity) && (RequestedProtocols & PROTOCOL_RDSTLS))
1465 SelectedProtocol = PROTOCOL_RDSTLS;
1467 else if ((settings->NlaSecurity) && (RequestedProtocols & PROTOCOL_HYBRID))
1469 SelectedProtocol = PROTOCOL_HYBRID;
1471 else if ((settings->TlsSecurity) && (RequestedProtocols & PROTOCOL_SSL))
1473 SelectedProtocol = PROTOCOL_SSL;
1475 else if ((settings->RdpSecurity) && (RequestedProtocols == PROTOCOL_RDP))
1477 SelectedProtocol = PROTOCOL_RDP;
1485 SelectedProtocol = PROTOCOL_FAILED_NEGO;
1487 if (settings->RdpSecurity)
1489 WLog_ERR(TAG,
"server supports only Standard RDP Security");
1490 SelectedProtocol |= SSL_NOT_ALLOWED_BY_SERVER;
1494 if (settings->NlaSecurity && !settings->TlsSecurity)
1496 WLog_WARN(TAG,
"server supports only NLA Security");
1497 SelectedProtocol |= HYBRID_REQUIRED_BY_SERVER;
1501 WLog_WARN(TAG,
"server supports only a SSL based Security (TLS or NLA)");
1502 SelectedProtocol |= SSL_REQUIRED_BY_SERVER;
1506 WLog_ERR(TAG,
"Protocol security negotiation failure");
1509 if (!(SelectedProtocol & PROTOCOL_FAILED_NEGO))
1511 WLog_DBG(TAG,
"Negotiated Security: RDSTLS:%d NLA:%d TLS:%d RDP:%d",
1512 (SelectedProtocol & PROTOCOL_RDSTLS) ? 1 : 0,
1513 (SelectedProtocol & PROTOCOL_HYBRID) ? 1 : 0,
1514 (SelectedProtocol & PROTOCOL_SSL) ? 1 : 0,
1515 (SelectedProtocol == PROTOCOL_RDP) ? 1 : 0);
1518 if (!nego_set_selected_protocol(nego, SelectedProtocol))
1521 if (!nego_send_negotiation_response(nego))
1524 SelectedProtocol = nego_get_selected_protocol(nego);
1527 if (SelectedProtocol & PROTOCOL_RDSTLS)
1528 status = transport_accept_rdstls(rdp->transport);
1529 else if (SelectedProtocol & PROTOCOL_HYBRID)
1530 status = transport_accept_nla(rdp->transport);
1531 else if (SelectedProtocol & PROTOCOL_SSL)
1532 status = transport_accept_tls(rdp->transport);
1533 else if (SelectedProtocol == PROTOCOL_RDP)
1534 status = transport_accept_rdp(rdp->transport);
1539 transport_set_blocking_mode(rdp->transport, FALSE);
1544 static BOOL rdp_update_encryption_level(rdpSettings* settings)
1546 WINPR_ASSERT(settings);
1561 if (!settings->UseRdpSecurityLayer)
1564 EncryptionLevel = ENCRYPTION_LEVEL_NONE;
1569 switch (EncryptionLevel)
1571 case ENCRYPTION_LEVEL_NONE:
1572 WLog_INFO(TAG,
"Active rdp encryption level: NONE");
1575 case ENCRYPTION_LEVEL_FIPS:
1576 WLog_INFO(TAG,
"Active rdp encryption level: FIPS Compliant");
1579 case ENCRYPTION_LEVEL_HIGH:
1580 WLog_INFO(TAG,
"Active rdp encryption level: HIGH");
1583 case ENCRYPTION_LEVEL_LOW:
1584 WLog_INFO(TAG,
"Active rdp encryption level: LOW");
1587 case ENCRYPTION_LEVEL_CLIENT_COMPATIBLE:
1588 WLog_INFO(TAG,
"Active rdp encryption level: CLIENT-COMPATIBLE");
1592 WLog_ERR(TAG,
"Invalid server encryption level 0x%08" PRIX32
"", EncryptionLevel);
1593 WLog_ERR(TAG,
"Switching to encryption level CLIENT-COMPATIBLE");
1594 EncryptionLevel = ENCRYPTION_LEVEL_CLIENT_COMPATIBLE;
1599 switch (EncryptionLevel)
1601 case ENCRYPTION_LEVEL_NONE:
1603 EncryptionMethods = ENCRYPTION_METHOD_NONE;
1606 case ENCRYPTION_LEVEL_FIPS:
1609 if (!(EncryptionMethods & ENCRYPTION_METHOD_FIPS))
1611 WLog_WARN(TAG,
"client does not support FIPS as required by server configuration");
1614 EncryptionMethods = ENCRYPTION_METHOD_FIPS;
1617 case ENCRYPTION_LEVEL_HIGH:
1620 if (!(EncryptionMethods & ENCRYPTION_METHOD_128BIT))
1622 WLog_WARN(TAG,
"client does not support 128 bit encryption method as required by "
1623 "server configuration");
1626 EncryptionMethods = ENCRYPTION_METHOD_128BIT;
1629 case ENCRYPTION_LEVEL_LOW:
1630 case ENCRYPTION_LEVEL_CLIENT_COMPATIBLE:
1633 if (EncryptionMethods & ENCRYPTION_METHOD_128BIT)
1634 EncryptionMethods = ENCRYPTION_METHOD_128BIT;
1635 else if (EncryptionMethods & ENCRYPTION_METHOD_56BIT)
1636 EncryptionMethods = ENCRYPTION_METHOD_56BIT;
1637 else if (EncryptionMethods & ENCRYPTION_METHOD_40BIT)
1638 EncryptionMethods = ENCRYPTION_METHOD_40BIT;
1639 else if (EncryptionMethods & ENCRYPTION_METHOD_FIPS)
1640 EncryptionMethods = ENCRYPTION_METHOD_FIPS;
1643 WLog_WARN(TAG,
"client has not announced any supported encryption methods");
1644 EncryptionMethods = ENCRYPTION_METHOD_128BIT;
1650 WLog_ERR(TAG,
"internal error: unknown encryption level");
1655 if (settings->UseRdpSecurityLayer)
1657 switch (EncryptionMethods)
1659 case ENCRYPTION_METHOD_NONE:
1660 WLog_INFO(TAG,
"Selected rdp encryption method: NONE");
1663 case ENCRYPTION_METHOD_40BIT:
1664 WLog_INFO(TAG,
"Selected rdp encryption method: 40BIT");
1667 case ENCRYPTION_METHOD_56BIT:
1668 WLog_INFO(TAG,
"Selected rdp encryption method: 56BIT");
1671 case ENCRYPTION_METHOD_128BIT:
1672 WLog_INFO(TAG,
"Selected rdp encryption method: 128BIT");
1675 case ENCRYPTION_METHOD_FIPS:
1676 WLog_INFO(TAG,
"Selected rdp encryption method: FIPS");
1680 WLog_ERR(TAG,
"internal error: unknown encryption method");
1692 BOOL rdp_server_accept_mcs_connect_initial(rdpRdp* rdp,
wStream* s)
1697 rdpMcs* mcs = rdp->mcs;
1700 WINPR_ASSERT(rdp_get_state(rdp) == CONNECTION_STATE_MCS_CREATE_REQUEST);
1701 if (!mcs_recv_connect_initial(mcs, s))
1703 WINPR_ASSERT(rdp->settings);
1705 if (!mcs_server_apply_to_settings(mcs, rdp->settings))
1708 WLog_DBG(TAG,
"Accepted client: %s", rdp->settings->ClientHostname);
1709 WLog_DBG(TAG,
"Accepted channels:");
1711 WINPR_ASSERT(mcs->channels || (mcs->channelCount == 0));
1712 for (UINT32 i = 0; i < mcs->channelCount; i++)
1715 rdpMcsChannel* cur = &mcs->channels[i];
1716 const char* params[1] = { cur->Name };
1717 WLog_DBG(TAG,
" %s [%" PRIu16
"]", cur->Name, cur->ChannelId);
1718 arg = freerdp_addin_argv_new(ARRAYSIZE(params), params);
1722 if (!freerdp_static_channel_collection_add(rdp->settings, arg))
1724 freerdp_addin_argv_free(arg);
1729 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_CREATE_RESPONSE))
1731 if (!rdp_update_encryption_level(rdp->settings))
1733 if (!mcs_send_connect_response(mcs))
1735 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_ERECT_DOMAIN))
1741 BOOL rdp_server_accept_mcs_erect_domain_request(rdpRdp* rdp,
wStream* s)
1745 WINPR_ASSERT(rdp_get_state(rdp) == CONNECTION_STATE_MCS_ERECT_DOMAIN);
1747 if (!mcs_recv_erect_domain_request(rdp->mcs, s))
1750 return rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_ATTACH_USER);
1753 static BOOL rdp_server_skip_mcs_channel_join(rdpRdp* rdp)
1757 rdpMcs* mcs = rdp->mcs;
1760 mcs->userChannelJoined = TRUE;
1761 mcs->globalChannelJoined = TRUE;
1762 mcs->messageChannelJoined = TRUE;
1764 for (UINT32 i = 0; i < mcs->channelCount; i++)
1766 rdpMcsChannel* cur = &mcs->channels[i];
1767 WLog_DBG(TAG,
" %s [%" PRIu16
"]", cur->Name, cur->ChannelId);
1770 return rdp_server_transition_to_state(rdp, CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT);
1773 BOOL rdp_server_accept_mcs_attach_user_request(rdpRdp* rdp,
wStream* s)
1775 if (!mcs_recv_attach_user_request(rdp->mcs, s))
1778 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_ATTACH_USER_CONFIRM))
1781 if (!mcs_send_attach_user_confirm(rdp->mcs))
1785 return rdp_server_skip_mcs_channel_join(rdp);
1786 return rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST);
1789 BOOL rdp_server_accept_mcs_channel_join_request(rdpRdp* rdp,
wStream* s)
1791 UINT16 channelId = 0;
1792 BOOL allJoined = TRUE;
1796 WINPR_ASSERT(rdp->context);
1801 WINPR_ASSERT(rdp_get_state(rdp) == CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST);
1803 if (!mcs_recv_channel_join_request(mcs, rdp->settings, s, &channelId))
1806 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_CHANNEL_JOIN_RESPONSE))
1809 if (!mcs_send_channel_join_confirm(mcs, channelId))
1812 if (channelId == mcs->userId)
1813 mcs->userChannelJoined = TRUE;
1814 if (channelId == MCS_GLOBAL_CHANNEL_ID)
1815 mcs->globalChannelJoined = TRUE;
1816 if (channelId == mcs->messageChannelId)
1817 mcs->messageChannelJoined = TRUE;
1819 for (UINT32 i = 0; i < mcs->channelCount; i++)
1821 rdpMcsChannel* cur = &mcs->channels[i];
1822 WLog_DBG(TAG,
" %s [%" PRIu16
"]", cur->Name, cur->ChannelId);
1823 if (cur->ChannelId == channelId)
1830 CONNECTION_STATE rc = CONNECTION_STATE_INITIAL;
1831 if ((mcs->userChannelJoined) && (mcs->globalChannelJoined) &&
1832 (mcs->messageChannelId == 0 || mcs->messageChannelJoined) && allJoined)
1833 rc = CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT;
1835 rc = CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST;
1837 return rdp_server_transition_to_state(rdp, rc);
1840 static BOOL rdp_server_send_sync(rdpRdp* rdp)
1844 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_CLIENT_SYNC))
1846 if (!rdp_send_server_synchronize_pdu(rdp))
1848 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_CLIENT_COOPERATE))
1850 if (!rdp_send_server_control_cooperate_pdu(rdp))
1852 if (!rdp_finalize_reset_flags(rdp, FALSE))
1854 return rdp_server_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_SYNC);
1857 BOOL rdp_server_accept_confirm_active(rdpRdp* rdp,
wStream* s, UINT16 pduLength)
1860 WINPR_ASSERT(rdp->context);
1861 WINPR_ASSERT(rdp->settings);
1864 freerdp_peer* peer = rdp->context->peer;
1867 if (rdp_get_state(rdp) != CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE)
1870 rdp_finalize_set_flag(rdp, FINALIZE_DEACTIVATE_REACTIVATE);
1873 WLog_WARN(TAG,
"Invalid state, got %s, expected %s", rdp_get_state_string(rdp),
1874 rdp_state_string(CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE));
1879 if (!rdp_recv_confirm_active(rdp, s, pduLength))
1882 if (peer->ClientCapabilities && !peer->ClientCapabilities(peer))
1884 WLog_WARN(TAG,
"peer->ClientCapabilities failed");
1888 if (rdp->settings->SaltedChecksum)
1889 rdp->do_secure_checksum = TRUE;
1891 return rdp_server_send_sync(rdp);
1894 BOOL rdp_server_reactivate(rdpRdp* rdp)
1896 freerdp_peer* client = NULL;
1898 if (rdp->context && rdp->context->peer)
1899 client = rdp->context->peer;
1902 client->activated = FALSE;
1904 if (!rdp_send_deactivate_all(rdp))
1907 rdp_finalize_set_flag(rdp, FINALIZE_DEACTIVATE_REACTIVATE);
1908 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE))
1911 state_run_t rc = rdp_peer_handle_state_demand_active(client);
1912 return state_run_success(rc);
1915 static BOOL rdp_is_active_peer_state(CONNECTION_STATE state)
1923 case CONNECTION_STATE_FINALIZATION_CLIENT_SYNC:
1924 case CONNECTION_STATE_FINALIZATION_CLIENT_COOPERATE:
1925 case CONNECTION_STATE_FINALIZATION_CLIENT_GRANTED_CONTROL:
1926 case CONNECTION_STATE_FINALIZATION_CLIENT_FONT_MAP:
1927 case CONNECTION_STATE_ACTIVE:
1934 static BOOL rdp_is_active_client_state(CONNECTION_STATE state)
1942 case CONNECTION_STATE_FINALIZATION_SYNC:
1943 case CONNECTION_STATE_FINALIZATION_COOPERATE:
1944 case CONNECTION_STATE_FINALIZATION_REQUEST_CONTROL:
1945 case CONNECTION_STATE_FINALIZATION_PERSISTENT_KEY_LIST:
1946 case CONNECTION_STATE_FINALIZATION_FONT_LIST:
1947 case CONNECTION_STATE_FINALIZATION_CLIENT_SYNC:
1948 case CONNECTION_STATE_FINALIZATION_CLIENT_COOPERATE:
1949 case CONNECTION_STATE_FINALIZATION_CLIENT_GRANTED_CONTROL:
1950 case CONNECTION_STATE_FINALIZATION_CLIENT_FONT_MAP:
1951 case CONNECTION_STATE_ACTIVE:
1958 BOOL rdp_is_active_state(
const rdpRdp* rdp)
1961 WINPR_ASSERT(rdp->context);
1963 const CONNECTION_STATE state = rdp_get_state(rdp);
1965 return rdp_is_active_peer_state(state);
1967 return rdp_is_active_client_state(state);
1970 BOOL rdp_server_transition_to_state(rdpRdp* rdp, CONNECTION_STATE state)
1972 BOOL status = FALSE;
1973 freerdp_peer* client = NULL;
1974 const CONNECTION_STATE cstate = rdp_get_state(rdp);
1976 if (cstate >= CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT)
1978 WINPR_ASSERT(rdp->context);
1979 client = rdp->context->peer;
1982 if (!rdp_is_active_peer_state(cstate))
1985 client->activated = FALSE;
1988 WLog_Print(rdp->log, WLOG_DEBUG,
"%s --> %s", rdp_get_state_string(rdp),
1989 rdp_state_string(state));
1990 if (!rdp_set_state(rdp, state))
1998 const char* rdp_client_connection_state_string(UINT state)
2002 case CLIENT_STATE_INITIAL:
2003 return "CLIENT_STATE_INITIAL";
2004 case CLIENT_STATE_PRECONNECT_PASSED:
2005 return "CLIENT_STATE_PRECONNECT_PASSED";
2006 case CLIENT_STATE_POSTCONNECT_PASSED:
2007 return "CLIENT_STATE_POSTCONNECT_PASSED";
2013 const char* rdp_state_string(CONNECTION_STATE state)
2017 case CONNECTION_STATE_INITIAL:
2018 return "CONNECTION_STATE_INITIAL";
2019 case CONNECTION_STATE_NEGO:
2020 return "CONNECTION_STATE_NEGO";
2021 case CONNECTION_STATE_NLA:
2022 return "CONNECTION_STATE_NLA";
2023 case CONNECTION_STATE_AAD:
2024 return "CONNECTION_STATE_AAD";
2025 case CONNECTION_STATE_MCS_CREATE_REQUEST:
2026 return "CONNECTION_STATE_MCS_CREATE_REQUEST";
2027 case CONNECTION_STATE_MCS_CREATE_RESPONSE:
2028 return "CONNECTION_STATE_MCS_CREATE_RESPONSE";
2029 case CONNECTION_STATE_MCS_ERECT_DOMAIN:
2030 return "CONNECTION_STATE_MCS_ERECT_DOMAIN";
2031 case CONNECTION_STATE_MCS_ATTACH_USER:
2032 return "CONNECTION_STATE_MCS_ATTACH_USER";
2033 case CONNECTION_STATE_MCS_ATTACH_USER_CONFIRM:
2034 return "CONNECTION_STATE_MCS_ATTACH_USER_CONFIRM";
2035 case CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST:
2036 return "CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST";
2037 case CONNECTION_STATE_MCS_CHANNEL_JOIN_RESPONSE:
2038 return "CONNECTION_STATE_MCS_CHANNEL_JOIN_RESPONSE";
2039 case CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT:
2040 return "CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT";
2041 case CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE:
2042 return "CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE";
2043 case CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_REQUEST:
2044 return "CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_REQUEST";
2045 case CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_RESPONSE:
2046 return "CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_RESPONSE";
2047 case CONNECTION_STATE_LICENSING:
2048 return "CONNECTION_STATE_LICENSING";
2049 case CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_REQUEST:
2050 return "CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_REQUEST";
2051 case CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_RESPONSE:
2052 return "CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_RESPONSE";
2053 case CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE:
2054 return "CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE";
2055 case CONNECTION_STATE_CAPABILITIES_EXCHANGE_MONITOR_LAYOUT:
2056 return "CONNECTION_STATE_CAPABILITIES_EXCHANGE_MONITOR_LAYOUT";
2057 case CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE:
2058 return "CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE";
2059 case CONNECTION_STATE_FINALIZATION_SYNC:
2060 return "CONNECTION_STATE_FINALIZATION_SYNC";
2061 case CONNECTION_STATE_FINALIZATION_COOPERATE:
2062 return "CONNECTION_STATE_FINALIZATION_COOPERATE";
2063 case CONNECTION_STATE_FINALIZATION_REQUEST_CONTROL:
2064 return "CONNECTION_STATE_FINALIZATION_REQUEST_CONTROL";
2065 case CONNECTION_STATE_FINALIZATION_PERSISTENT_KEY_LIST:
2066 return "CONNECTION_STATE_FINALIZATION_PERSISTENT_KEY_LIST";
2067 case CONNECTION_STATE_FINALIZATION_FONT_LIST:
2068 return "CONNECTION_STATE_FINALIZATION_FONT_LIST";
2069 case CONNECTION_STATE_FINALIZATION_CLIENT_SYNC:
2070 return "CONNECTION_STATE_FINALIZATION_CLIENT_SYNC";
2071 case CONNECTION_STATE_FINALIZATION_CLIENT_COOPERATE:
2072 return "CONNECTION_STATE_FINALIZATION_CLIENT_COOPERATE";
2073 case CONNECTION_STATE_FINALIZATION_CLIENT_GRANTED_CONTROL:
2074 return "CONNECTION_STATE_FINALIZATION_CLIENT_GRANTED_CONTROL";
2075 case CONNECTION_STATE_FINALIZATION_CLIENT_FONT_MAP:
2076 return "CONNECTION_STATE_FINALIZATION_CLIENT_FONT_MAP";
2077 case CONNECTION_STATE_ACTIVE:
2078 return "CONNECTION_STATE_ACTIVE";
2084 CONNECTION_STATE rdp_get_state(
const rdpRdp* rdp)
2090 BOOL rdp_set_state(rdpRdp* rdp, CONNECTION_STATE state)
2097 const char* rdp_get_state_string(
const rdpRdp* rdp)
2099 CONNECTION_STATE state = rdp_get_state(rdp);
2100 return rdp_state_string(state);
2103 BOOL rdp_channels_from_mcs(rdpSettings* settings,
const rdpRdp* rdp)
2105 const rdpMcs* mcs = NULL;
2116 for (UINT32 x = 0; x < mcs->channelCount; x++)
2118 const rdpMcsChannel* mchannel = &mcs->channels[x];
2121 memcpy(cur.name, mchannel->Name,
sizeof(cur.name));
2122 cur.options = mchannel->options;
2123 if (!freerdp_settings_set_pointer_array(settings, FreeRDP_ChannelDefArray, x, &cur))
2136 state_run_t rdp_client_connect_confirm_active(rdpRdp* rdp,
wStream* s)
2139 WINPR_ASSERT(rdp->settings);
2142 const UINT32 width = rdp->settings->DesktopWidth;
2143 const UINT32 height = rdp->settings->DesktopHeight;
2145 if (!rdp_send_confirm_active(rdp))
2146 return STATE_RUN_FAILED;
2148 if (!input_register_client_callbacks(rdp->input))
2150 WLog_ERR(TAG,
"error registering client callbacks");
2151 return STATE_RUN_FAILED;
2158 const BOOL deactivate_reactivate =
2159 rdp->was_deactivated && ((rdp->deactivated_width != rdp->settings->DesktopWidth) ||
2160 (rdp->deactivated_height != rdp->settings->DesktopHeight));
2161 const BOOL resolution_change =
2162 ((width != rdp->settings->DesktopWidth) || (height != rdp->settings->DesktopHeight));
2163 if (deactivate_reactivate || resolution_change)
2166 IFCALLRET(rdp->update->DesktopResize, status, rdp->update->context);
2170 WLog_ERR(TAG,
"client desktop resize callback failed");
2171 return STATE_RUN_FAILED;
2175 WINPR_ASSERT(rdp->context);
2176 if (freerdp_shall_disconnect_context(rdp->context))
2177 return STATE_RUN_SUCCESS;
2179 state_run_t status = STATE_RUN_SUCCESS;
2180 if (!rdp->settings->SupportMonitorLayoutPdu)
2181 status = rdp_client_connect_finalize(rdp);
2184 if (!rdp_client_transition_to_state(rdp,
2185 CONNECTION_STATE_CAPABILITIES_EXCHANGE_MONITOR_LAYOUT))
2186 status = STATE_RUN_FAILED;
2188 if (!rdp_finalize_reset_flags(rdp, FALSE))
2189 status = STATE_RUN_FAILED;
2193 BOOL rdp_handle_optional_rdp_decryption(rdpRdp* rdp,
wStream* s, UINT16* length,
2194 UINT16* pSecurityFlags)
2198 WINPR_ASSERT(rdp->settings);
2200 UINT16 securityFlags = 0;
2201 if (rdp->settings->UseRdpSecurityLayer)
2203 if (!rdp_read_security_header(rdp, s, &securityFlags, length))
2206 if (securityFlags & SEC_ENCRYPT)
2208 if (!rdp_decrypt(rdp, s, length, securityFlags))
2217 *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.