21 #include <freerdp/config.h>
25 #include <winpr/crt.h>
26 #include <winpr/string.h>
27 #include <winpr/synch.h>
28 #include <winpr/assert.h>
29 #include <winpr/cast.h>
30 #include <winpr/json.h>
38 #include "redirection.h"
40 #include <freerdp/codec/bulk.h>
41 #include <freerdp/crypto/per.h>
42 #include <freerdp/log.h>
43 #include <freerdp/buildflags.h>
45 #define RDP_TAG FREERDP_TAG("core.rdp")
55 static const char* DATA_PDU_TYPE_STRINGS[80] = {
97 "Set Keyboard Indicators",
99 "Bitmap Cache Persistent List",
100 "Bitmap Cache Error",
101 "Set Keyboard IME Status",
102 "Offscreen Cache Error",
104 "Draw Nine Grid Error",
123 #define rdp_check_monitor_layout_pdu_state(rdp, expected) \
124 rdp_check_monitor_layout_pdu_state_(rdp, expected, __FILE__, __func__, __LINE__)
126 static BOOL rdp_check_monitor_layout_pdu_state_(
const rdpRdp* rdp, BOOL expected,
const char* file,
127 const char* fkt,
size_t line)
130 if (expected != rdp->monitor_layout_pdu)
132 const DWORD log_level = WLOG_ERROR;
133 if (WLog_IsLevelActive(rdp->log, log_level))
135 WLog_PrintMessage(rdp->log, WLOG_MESSAGE_TEXT, log_level, line, file, fkt,
136 "Expected rdp->monitor_layout_pdu == %s",
137 expected ?
"TRUE" :
"FALSE");
144 #define rdp_set_monitor_layout_pdu_state(rdp, expected) \
145 rdp_set_monitor_layout_pdu_state_(rdp, expected, __FILE__, __func__, __LINE__)
146 static BOOL rdp_set_monitor_layout_pdu_state_(rdpRdp* rdp, BOOL value,
const char* file,
147 const char* fkt,
size_t line)
151 if (value && (value == rdp->monitor_layout_pdu))
153 const DWORD log_level = WLOG_WARN;
154 if (WLog_IsLevelActive(rdp->log, log_level))
156 WLog_PrintMessage(rdp->log, WLOG_MESSAGE_TEXT, log_level, line, file, fkt,
157 "rdp->monitor_layout_pdu == %s, expected FALSE",
158 value ?
"TRUE" :
"FALSE");
162 rdp->monitor_layout_pdu = value;
166 const char* data_pdu_type_to_string(UINT8 type)
168 if (type >= ARRAYSIZE(DATA_PDU_TYPE_STRINGS))
170 return DATA_PDU_TYPE_STRINGS[type];
173 static BOOL rdp_read_flow_control_pdu(rdpRdp* rdp,
wStream* s, UINT16* type, UINT16* channel_id);
174 static BOOL rdp_write_share_control_header(rdpRdp* rdp,
wStream* s,
size_t length, UINT16 type,
176 static BOOL rdp_write_share_data_header(rdpRdp* rdp,
wStream* s,
size_t length, BYTE type,
189 BOOL rdp_read_security_header(rdpRdp* rdp,
wStream* s, UINT16* flags, UINT16* length)
191 char buffer[256] = { 0 };
197 if ((length && (*length < 4)))
199 WLog_Print(rdp->log, WLOG_WARN,
200 "invalid security header length, have %" PRIu16
", must be >= 4", *length);
203 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 4))
206 Stream_Read_UINT16(s, *flags);
208 WLog_Print(rdp->log, WLOG_TRACE,
"%s",
209 rdp_security_flag_string(*flags, buffer,
sizeof(buffer)));
225 BOOL rdp_write_security_header(rdpRdp* rdp,
wStream* s, UINT16 flags)
227 char buffer[256] = { 0 };
231 if (!Stream_CheckAndLogRequiredCapacityWLog(rdp->log, (s), 4))
234 WLog_Print(rdp->log, WLOG_TRACE,
"%s", rdp_security_flag_string(flags, buffer,
sizeof(buffer)));
236 Stream_Write_UINT16(s, flags);
237 Stream_Write_UINT16(s, 0);
241 BOOL rdp_read_share_control_header(rdpRdp* rdp,
wStream* s, UINT16* tpktLength,
242 UINT16* remainingLength, UINT16* type, UINT16* channel_id)
250 WINPR_ASSERT(channel_id);
252 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 2))
256 Stream_Read_UINT16(s, len);
262 if (!rdp_read_flow_control_pdu(rdp, s, type, channel_id))
268 *remainingLength = 0;
270 char buffer[128] = { 0 };
271 WLog_Print(rdp->log, WLOG_DEBUG,
272 "[Flow control PDU] type=%s, tpktLength=%" PRIuz
", remainingLength=%" PRIuz,
273 pdu_type_to_str(*type, buffer,
sizeof(buffer)), tpktLength ? *tpktLength : 0,
274 remainingLength ? *remainingLength : 0);
280 WLog_Print(rdp->log, WLOG_ERROR,
281 "Invalid share control header, length is %" PRIu16
", must be >4", len);
288 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 2))
291 Stream_Read_UINT16(s, tmp);
294 size_t remLen = len - 4;
297 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 2))
300 Stream_Read_UINT16(s, *channel_id);
306 char buffer[128] = { 0 };
307 WLog_Print(rdp->log, WLOG_DEBUG,
"type=%s, tpktLength=%" PRIuz
", remainingLength=%" PRIuz,
308 pdu_type_to_str(*type, buffer,
sizeof(buffer)), len, remLen);
311 WINPR_ASSERT(remLen <= UINT16_MAX);
312 *remainingLength = (UINT16)remLen;
314 return Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, remLen);
317 BOOL rdp_write_share_control_header(rdpRdp* rdp,
wStream* s,
size_t length, UINT16 type,
322 if (length > UINT16_MAX)
325 if (length < RDP_PACKET_HEADER_MAX_LENGTH)
327 if (!Stream_CheckAndLogRequiredCapacityWLog(rdp->log, (s), 6))
329 length -= RDP_PACKET_HEADER_MAX_LENGTH;
331 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, length));
332 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, type | 0x10));
333 Stream_Write_UINT16(s, WINPR_ASSERTING_INT_CAST(uint16_t, channel_id));
337 BOOL rdp_read_share_data_header(rdpRdp* rdp,
wStream* s, UINT16* length, BYTE* type,
338 UINT32* shareId, BYTE* compressedType, UINT16* compressedLength)
343 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 12))
347 Stream_Read_UINT32(s, *shareId);
348 Stream_Seek_UINT8(s);
349 Stream_Seek_UINT8(s);
350 Stream_Read_UINT16(s, *length);
351 Stream_Read_UINT8(s, *type);
352 Stream_Read_UINT8(s, *compressedType);
353 Stream_Read_UINT16(s, *compressedLength);
357 BOOL rdp_write_share_data_header(rdpRdp* rdp,
wStream* s,
size_t length, BYTE type, UINT32 share_id)
359 const size_t headerLen = RDP_PACKET_HEADER_MAX_LENGTH + RDP_SHARE_CONTROL_HEADER_LENGTH +
360 RDP_SHARE_DATA_HEADER_LENGTH;
364 if (length > UINT16_MAX)
367 if (length < headerLen)
370 if (!Stream_CheckAndLogRequiredCapacityWLog(rdp->log, (s), 12))
374 Stream_Write_UINT32(s, share_id);
375 Stream_Write_UINT8(s, 0);
376 Stream_Write_UINT8(s, STREAM_LOW);
378 s, WINPR_ASSERTING_INT_CAST(uint16_t, length));
379 Stream_Write_UINT8(s, type);
380 Stream_Write_UINT8(s, 0);
381 Stream_Write_UINT16(s, 0);
385 static BOOL rdp_security_stream_init(rdpRdp* rdp,
wStream* s, BOOL sec_header)
392 if (!Stream_SafeSeek(s, 12))
395 if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
397 if (!Stream_SafeSeek(s, 4))
401 rdp->sec_flags |= SEC_ENCRYPT;
403 if (rdp->do_secure_checksum)
404 rdp->sec_flags |= SEC_SECURE_CHECKSUM;
406 else if (rdp->sec_flags != 0 || sec_header)
408 if (!Stream_SafeSeek(s, 4))
415 wStream* rdp_send_stream_init(rdpRdp* rdp)
420 WINPR_ASSERT(rdp->transport);
422 s = transport_send_stream_init(rdp->transport, 4096);
427 if (!Stream_SafeSeek(s, RDP_PACKET_HEADER_MAX_LENGTH))
430 if (!rdp_security_stream_init(rdp, s, FALSE))
439 wStream* rdp_send_stream_pdu_init(rdpRdp* rdp)
441 wStream* s = rdp_send_stream_init(rdp);
446 if (!Stream_SafeSeek(s, RDP_SHARE_CONTROL_HEADER_LENGTH))
455 wStream* rdp_data_pdu_init(rdpRdp* rdp)
457 wStream* s = rdp_send_stream_pdu_init(rdp);
462 if (!Stream_SafeSeek(s, RDP_SHARE_DATA_HEADER_LENGTH))
471 BOOL rdp_set_error_info(rdpRdp* rdp, UINT32 errorInfo)
475 rdp->errorInfo = errorInfo;
477 if (rdp->errorInfo != ERRINFO_SUCCESS)
479 rdpContext* context = rdp->context;
480 WINPR_ASSERT(context);
482 rdp_print_errinfo(rdp->errorInfo);
486 freerdp_set_last_error_log(context, MAKE_FREERDP_ERROR(
ERRINFO, errorInfo));
490 ErrorInfoEventArgs e = { 0 };
491 EventArgsInit(&e,
"freerdp");
492 e.code = rdp->errorInfo;
493 PubSub_OnErrorInfo(context->pubSub, context, &e);
497 WLog_Print(rdp->log, WLOG_ERROR,
"missing context=%p", context);
501 freerdp_set_last_error_log(rdp->context, FREERDP_ERROR_SUCCESS);
507 wStream* rdp_message_channel_pdu_init(rdpRdp* rdp)
513 s = transport_send_stream_init(rdp->transport, 4096);
518 if (!Stream_SafeSeek(s, RDP_PACKET_HEADER_MAX_LENGTH))
521 if (!rdp_security_stream_init(rdp, s, TRUE))
540 BOOL rdp_read_header(rdpRdp* rdp,
wStream* s, UINT16* length, UINT16* channelId)
545 UINT16 initiator = 0;
548 WINPR_ASSERT(rdp->settings);
550 DomainMCSPDU MCSPDU = (rdp->settings->ServerMode) ? DomainMCSPDU_SendDataRequest
551 : DomainMCSPDU_SendDataIndication;
554 if (!tpkt_read_header(s, length))
557 if (!tpdu_read_header(s, &code, &li, *length))
560 if (code != X224_TPDU_DATA)
562 if (code == X224_TPDU_DISCONNECT_REQUEST)
564 WLog_Print(rdp->log, WLOG_WARN,
"Received X224_TPDU_DISCONNECT_REQUEST, terminating");
565 utils_abort_connect(rdp);
569 WLog_Print(rdp->log, WLOG_WARN,
570 "Unexpected X224 TPDU type %s [%08" PRIx32
"] instead of %s",
571 tpdu_type_to_string(code), code, tpdu_type_to_string(X224_TPDU_DATA));
575 if (!per_read_choice(s, &choice))
578 const DomainMCSPDU domainMCSPDU = (DomainMCSPDU)(choice >> 2);
580 if (domainMCSPDU != MCSPDU)
582 if (domainMCSPDU != DomainMCSPDU_DisconnectProviderUltimatum)
584 WLog_Print(rdp->log, WLOG_WARN,
"Received %s instead of %s",
585 mcs_domain_pdu_string(domainMCSPDU), mcs_domain_pdu_string(MCSPDU));
590 MCSPDU = domainMCSPDU;
594 WLog_Print(rdp->log, WLOG_WARN,
"TPDU invalid length, got %" PRIu16
", expected at least 8",
599 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, *length - 8))
602 if (MCSPDU == DomainMCSPDU_DisconnectProviderUltimatum)
605 TerminateEventArgs e = { 0 };
607 if (!mcs_recv_disconnect_provider_ultimatum(rdp->mcs, s, &reason))
610 rdpContext* context = rdp->context;
611 WINPR_ASSERT(context);
612 context->disconnectUltimatum = reason;
614 if (rdp->errorInfo == ERRINFO_SUCCESS)
621 UINT32 errorInfo = ERRINFO_RPC_INITIATED_DISCONNECT;
622 if (reason == Disconnect_Ultimatum_provider_initiated)
623 errorInfo = ERRINFO_RPC_INITIATED_DISCONNECT;
624 else if (reason == Disconnect_Ultimatum_user_requested)
625 errorInfo = ERRINFO_LOGOFF_BY_USER;
627 rdp_set_error_info(rdp, errorInfo);
630 WLog_Print(rdp->log, WLOG_DEBUG,
"DisconnectProviderUltimatum: reason: %d", reason);
631 utils_abort_connect(rdp);
632 EventArgsInit(&e,
"freerdp");
634 PubSub_OnTerminate(rdp->pubSub, context, &e);
638 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 5))
641 if (!per_read_integer16(s, &initiator, MCS_BASE_CHANNEL_ID))
644 if (!per_read_integer16(s, channelId, 0))
647 const uint8_t
byte = Stream_Get_UINT8(s);
649 if (!per_read_length(s, length))
652 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, *length))
668 BOOL rdp_write_header(rdpRdp* rdp,
wStream* s,
size_t length, UINT16 channelId)
671 WINPR_ASSERT(rdp->settings);
673 WINPR_ASSERT(length >= RDP_PACKET_HEADER_MAX_LENGTH);
674 if (length > UINT16_MAX)
677 DomainMCSPDU MCSPDU = (rdp->settings->ServerMode) ? DomainMCSPDU_SendDataIndication
678 : DomainMCSPDU_SendDataRequest;
680 if ((rdp->sec_flags & SEC_ENCRYPT) &&
681 (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS))
683 const UINT16 body_length = (UINT16)length - RDP_PACKET_HEADER_MAX_LENGTH;
684 const UINT16 pad = 8 - (body_length % 8);
690 if (!mcs_write_domain_mcspdu_header(s, MCSPDU, (UINT16)length, 0))
692 if (!per_write_integer16(s, rdp->mcs->userId, MCS_BASE_CHANNEL_ID))
694 if (!per_write_integer16(s, channelId, 0))
696 if (!Stream_EnsureRemainingCapacity(s, 3))
698 Stream_Write_UINT8(s, 0x70);
705 length = (length - RDP_PACKET_HEADER_MAX_LENGTH) | 0x8000;
706 Stream_Write_UINT16_BE(
707 s, WINPR_ASSERTING_INT_CAST(uint16_t, length));
711 static BOOL rdp_security_stream_out(rdpRdp* rdp,
wStream* s,
size_t length, UINT32 sec_flags,
716 if (length > UINT16_MAX)
719 sec_flags |= rdp->sec_flags;
724 WINPR_ASSERT(sec_flags <= UINT16_MAX);
725 if (!rdp_write_security_header(rdp, s, (UINT16)sec_flags))
728 if (sec_flags & SEC_ENCRYPT)
731 if (!security_lock(rdp))
734 if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
736 BYTE* data = Stream_PointerAs(s, BYTE) + 12;
737 const size_t size = WINPR_ASSERTING_INT_CAST(
size_t, (data - Stream_Buffer(s)));
743 Stream_Write_UINT16(s, 0x10);
744 Stream_Write_UINT8(s, 0x1);
746 *pad = 8 - (length % 8);
752 memset(data + length, 0, *pad);
754 Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(uint8_t, *pad));
756 if (!Stream_CheckAndLogRequiredCapacityWLog(rdp->log, s, 8))
758 if (!security_hmac_signature(data, length, Stream_Pointer(s), 8, rdp))
762 if (!security_fips_encrypt(data, length + *pad, rdp))
767 const BYTE* data = Stream_PointerAs(s,
const BYTE) + 8;
768 const size_t diff = Stream_GetPosition(s) + 8ULL;
773 if (!Stream_CheckAndLogRequiredCapacityWLog(rdp->log, s, 8))
775 if (sec_flags & SEC_SECURE_CHECKSUM)
776 status = security_salted_mac_signature(rdp, data, (UINT32)length, TRUE,
777 Stream_Pointer(s), 8);
779 status = security_mac_signature(rdp, data, (UINT32)length,
780 Stream_PointerAs(s, BYTE), 8);
787 if (!security_encrypt(Stream_Pointer(s), length, rdp))
794 if (!security_unlock(rdp))
806 static UINT32 rdp_get_sec_bytes(rdpRdp* rdp, UINT16 sec_flags)
808 UINT32 sec_bytes = 0;
810 if (rdp->sec_flags & SEC_ENCRYPT)
814 if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
817 else if (rdp->sec_flags != 0 || sec_flags != 0)
836 BOOL rdp_send(rdpRdp* rdp,
wStream* s, UINT16 channel_id)
847 size_t length = Stream_GetPosition(s);
848 Stream_SetPosition(s, 0);
849 if (!rdp_write_header(rdp, s, length, channel_id))
852 if (!rdp_security_stream_out(rdp, s, length, 0, &pad))
856 Stream_SetPosition(s, length);
857 Stream_SealLength(s);
859 if (transport_write(rdp->transport, s) < 0)
868 BOOL rdp_send_pdu(rdpRdp* rdp,
wStream* s, UINT16 type, UINT16 channel_id)
870 UINT32 sec_bytes = 0;
877 size_t length = Stream_GetPosition(s);
878 Stream_SetPosition(s, 0);
879 if (!rdp_write_header(rdp, s, length, MCS_GLOBAL_CHANNEL_ID))
881 sec_bytes = rdp_get_sec_bytes(rdp, 0);
882 sec_hold = Stream_GetPosition(s);
883 Stream_Seek(s, sec_bytes);
884 if (!rdp_write_share_control_header(rdp, s, length - sec_bytes, type, channel_id))
886 Stream_SetPosition(s, sec_hold);
888 if (!rdp_security_stream_out(rdp, s, length, 0, &pad))
892 Stream_SetPosition(s, length);
893 Stream_SealLength(s);
895 if (transport_write(rdp->transport, s) < 0)
901 BOOL rdp_send_data_pdu(rdpRdp* rdp,
wStream* s, BYTE type, UINT16 channel_id)
904 UINT32 sec_bytes = 0;
914 size_t length = Stream_GetPosition(s);
915 Stream_SetPosition(s, 0);
916 if (!rdp_write_header(rdp, s, length, MCS_GLOBAL_CHANNEL_ID))
918 sec_bytes = rdp_get_sec_bytes(rdp, 0);
919 sec_hold = Stream_GetPosition(s);
920 Stream_Seek(s, sec_bytes);
921 if (!rdp_write_share_control_header(rdp, s, length - sec_bytes, PDU_TYPE_DATA, channel_id))
923 if (!rdp_write_share_data_header(rdp, s, length - sec_bytes, type, rdp->settings->ShareId))
925 Stream_SetPosition(s, sec_hold);
927 if (!rdp_security_stream_out(rdp, s, length, 0, &pad))
931 Stream_SetPosition(s, length);
932 Stream_SealLength(s);
933 WLog_Print(rdp->log, WLOG_DEBUG,
934 "sending data (type=0x%x size=%" PRIuz
" channelId=%" PRIu16
")", type,
935 Stream_Length(s), channel_id);
938 if (transport_write(rdp->transport, s) < 0)
947 BOOL rdp_send_message_channel_pdu(rdpRdp* rdp,
wStream* s, UINT16 sec_flags)
955 size_t length = Stream_GetPosition(s);
956 Stream_SetPosition(s, 0);
957 if (!rdp_write_header(rdp, s, length, rdp->mcs->messageChannelId))
960 if (!rdp_security_stream_out(rdp, s, length, sec_flags, &pad))
964 Stream_SetPosition(s, length);
965 Stream_SealLength(s);
967 if (transport_write(rdp->transport, s) < 0)
976 static BOOL rdp_recv_server_shutdown_denied_pdu(rdpRdp* rdp,
wStream* s)
981 static BOOL rdp_recv_server_set_keyboard_indicators_pdu(rdpRdp* rdp,
wStream* s)
988 rdpContext* context = rdp->context;
989 WINPR_ASSERT(context);
990 WINPR_ASSERT(context->update);
992 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 4))
995 const uint16_t unitId = Stream_Get_UINT16(s);
996 Stream_Read_UINT16(s, ledFlags);
997 return IFCALLRESULT(TRUE, context->update->SetKeyboardIndicators, context, ledFlags);
1000 static BOOL rdp_recv_server_set_keyboard_ime_status_pdu(rdpRdp* rdp,
wStream* s)
1003 UINT32 imeState = 0;
1004 UINT32 imeConvMode = 0;
1006 if (!rdp || !rdp->input)
1009 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 10))
1012 Stream_Read_UINT16(s, unitId);
1013 Stream_Read_UINT32(s, imeState);
1014 Stream_Read_UINT32(s, imeConvMode);
1015 IFCALL(rdp->update->SetKeyboardImeStatus, rdp->context, unitId, imeState, imeConvMode);
1019 static BOOL rdp_recv_set_error_info_data_pdu(rdpRdp* rdp,
wStream* s)
1021 UINT32 errorInfo = 0;
1023 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 4))
1026 Stream_Read_UINT32(s, errorInfo);
1027 return rdp_set_error_info(rdp, errorInfo);
1030 static BOOL rdp_recv_server_auto_reconnect_status_pdu(rdpRdp* rdp,
wStream* s)
1032 UINT32 arcStatus = 0;
1034 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 4))
1037 Stream_Read_UINT32(s, arcStatus);
1038 WLog_Print(rdp->log, WLOG_WARN,
"AutoReconnectStatus: 0x%08" PRIX32
"", arcStatus);
1042 static BOOL rdp_recv_server_status_info_pdu(rdpRdp* rdp,
wStream* s)
1044 UINT32 statusCode = 0;
1046 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 4))
1049 Stream_Read_UINT32(s, statusCode);
1051 if (rdp->update->ServerStatusInfo)
1052 return rdp->update->ServerStatusInfo(rdp->context, statusCode);
1057 static BOOL rdp_recv_monitor_layout_pdu(rdpRdp* rdp,
wStream* s)
1059 UINT32 monitorCount = 0;
1064 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 4))
1067 Stream_Read_UINT32(s, monitorCount);
1069 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(rdp->log, s, monitorCount, 20ull))
1074 if (!monitorDefArray)
1077 for (UINT32 index = 0; index < monitorCount; index++)
1080 Stream_Read_INT32(s, monitor->left);
1081 Stream_Read_INT32(s, monitor->top);
1082 Stream_Read_INT32(s, monitor->right);
1083 Stream_Read_INT32(s, monitor->bottom);
1084 Stream_Read_UINT32(s, monitor->flags);
1087 IFCALLRET(rdp->update->RemoteMonitors, ret, rdp->context, monitorCount, monitorDefArray);
1088 free(monitorDefArray);
1091 return rdp_set_monitor_layout_pdu_state(rdp, TRUE);
1094 state_run_t rdp_recv_data_pdu(rdpRdp* rdp,
wStream* s)
1100 BYTE compressedType = 0;
1101 UINT16 compressedLength = 0;
1104 if (!rdp_read_share_data_header(rdp, s, &length, &type, &shareId, &compressedType,
1107 WLog_Print(rdp->log, WLOG_ERROR,
"rdp_read_share_data_header() failed");
1108 return STATE_RUN_FAILED;
1113 if (compressedType & PACKET_COMPRESSED)
1115 if (compressedLength < 18)
1117 WLog_Print(rdp->log, WLOG_ERROR,
1118 "bulk_decompress: not enough bytes for compressedLength %" PRIu16
"",
1120 return STATE_RUN_FAILED;
1124 const BYTE* pDstData = NULL;
1125 UINT16 SrcSize = compressedLength - 18;
1127 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, SrcSize))
1129 WLog_Print(rdp->log, WLOG_ERROR,
1130 "bulk_decompress: not enough bytes for compressedLength %" PRIu16
"",
1132 return STATE_RUN_FAILED;
1135 if (bulk_decompress(rdp->bulk, Stream_ConstPointer(s), SrcSize, &pDstData, &DstSize,
1138 cs = transport_take_from_pool(rdp->transport, DstSize);
1141 WLog_Print(rdp->log, WLOG_ERROR,
"Couldn't take stream from pool");
1142 return STATE_RUN_FAILED;
1145 Stream_SetPosition(cs, 0);
1146 Stream_Write(cs, pDstData, DstSize);
1147 Stream_SealLength(cs);
1148 Stream_SetPosition(cs, 0);
1152 WLog_Print(rdp->log, WLOG_ERROR,
"bulk_decompress() failed");
1153 return STATE_RUN_FAILED;
1156 Stream_Seek(s, SrcSize);
1159 WLog_Print(rdp->log, WLOG_DEBUG,
"recv %s Data PDU (0x%02" PRIX8
"), length: %" PRIu16
"",
1160 data_pdu_type_to_string(type), type, length);
1164 case DATA_PDU_TYPE_UPDATE:
1165 if (!update_recv(rdp->update, cs))
1167 WLog_Print(rdp->log, WLOG_ERROR,
"DATA_PDU_TYPE_UPDATE - update_recv() failed");
1173 case DATA_PDU_TYPE_CONTROL:
1174 if (!rdp_recv_server_control_pdu(rdp, cs))
1176 WLog_Print(rdp->log, WLOG_ERROR,
1177 "DATA_PDU_TYPE_CONTROL - rdp_recv_server_control_pdu() failed");
1183 case DATA_PDU_TYPE_POINTER:
1184 if (!update_recv_pointer(rdp->update, cs))
1186 WLog_Print(rdp->log, WLOG_ERROR,
1187 "DATA_PDU_TYPE_POINTER - update_recv_pointer() failed");
1193 case DATA_PDU_TYPE_SYNCHRONIZE:
1194 if (!rdp_recv_server_synchronize_pdu(rdp, cs))
1196 WLog_Print(rdp->log, WLOG_ERROR,
1197 "DATA_PDU_TYPE_SYNCHRONIZE - rdp_recv_synchronize_pdu() failed");
1203 case DATA_PDU_TYPE_PLAY_SOUND:
1204 if (!update_recv_play_sound(rdp->update, cs))
1206 WLog_Print(rdp->log, WLOG_ERROR,
1207 "DATA_PDU_TYPE_PLAY_SOUND - update_recv_play_sound() failed");
1213 case DATA_PDU_TYPE_SHUTDOWN_DENIED:
1214 if (!rdp_recv_server_shutdown_denied_pdu(rdp, cs))
1217 rdp->log, WLOG_ERROR,
1218 "DATA_PDU_TYPE_SHUTDOWN_DENIED - rdp_recv_server_shutdown_denied_pdu() failed");
1224 case DATA_PDU_TYPE_SAVE_SESSION_INFO:
1225 if (!rdp_recv_save_session_info(rdp, cs))
1227 WLog_Print(rdp->log, WLOG_ERROR,
1228 "DATA_PDU_TYPE_SAVE_SESSION_INFO - rdp_recv_save_session_info() failed");
1234 case DATA_PDU_TYPE_FONT_MAP:
1235 if (!rdp_recv_font_map_pdu(rdp, cs))
1237 WLog_Print(rdp->log, WLOG_ERROR,
1238 "DATA_PDU_TYPE_FONT_MAP - rdp_recv_font_map_pdu() failed");
1244 case DATA_PDU_TYPE_SET_KEYBOARD_INDICATORS:
1245 if (!rdp_recv_server_set_keyboard_indicators_pdu(rdp, cs))
1247 WLog_Print(rdp->log, WLOG_ERROR,
1248 "DATA_PDU_TYPE_SET_KEYBOARD_INDICATORS - "
1249 "rdp_recv_server_set_keyboard_indicators_pdu() failed");
1255 case DATA_PDU_TYPE_SET_KEYBOARD_IME_STATUS:
1256 if (!rdp_recv_server_set_keyboard_ime_status_pdu(rdp, cs))
1258 WLog_Print(rdp->log, WLOG_ERROR,
1259 "DATA_PDU_TYPE_SET_KEYBOARD_IME_STATUS - "
1260 "rdp_recv_server_set_keyboard_ime_status_pdu() failed");
1266 case DATA_PDU_TYPE_SET_ERROR_INFO:
1267 if (!rdp_recv_set_error_info_data_pdu(rdp, cs))
1270 rdp->log, WLOG_ERROR,
1271 "DATA_PDU_TYPE_SET_ERROR_INFO - rdp_recv_set_error_info_data_pdu() failed");
1277 case DATA_PDU_TYPE_ARC_STATUS:
1278 if (!rdp_recv_server_auto_reconnect_status_pdu(rdp, cs))
1280 WLog_Print(rdp->log, WLOG_ERROR,
1281 "DATA_PDU_TYPE_ARC_STATUS - "
1282 "rdp_recv_server_auto_reconnect_status_pdu() failed");
1288 case DATA_PDU_TYPE_STATUS_INFO:
1289 if (!rdp_recv_server_status_info_pdu(rdp, cs))
1291 WLog_Print(rdp->log, WLOG_ERROR,
1292 "DATA_PDU_TYPE_STATUS_INFO - rdp_recv_server_status_info_pdu() failed");
1298 case DATA_PDU_TYPE_MONITOR_LAYOUT:
1299 if (!rdp_recv_monitor_layout_pdu(rdp, cs))
1301 WLog_Print(rdp->log, WLOG_ERROR,
1302 "DATA_PDU_TYPE_MONITOR_LAYOUT - rdp_recv_monitor_layout_pdu() failed");
1309 WLog_Print(rdp->log, WLOG_WARN,
1310 "[UNHANDLED] %s Data PDU (0x%02" PRIX8
"), length: %" PRIu16
"",
1311 data_pdu_type_to_string(type), type, length);
1318 return STATE_RUN_SUCCESS;
1324 return STATE_RUN_FAILED;
1327 state_run_t rdp_recv_message_channel_pdu(rdpRdp* rdp,
wStream* s, UINT16 securityFlags)
1332 if (securityFlags & SEC_AUTODETECT_REQ)
1335 return autodetect_recv_request_packet(rdp->autodetect, RDP_TRANSPORT_TCP, s);
1338 if (securityFlags & SEC_AUTODETECT_RSP)
1341 return autodetect_recv_response_packet(rdp->autodetect, RDP_TRANSPORT_TCP, s);
1344 if (securityFlags & SEC_HEARTBEAT)
1347 return rdp_recv_heartbeat_packet(rdp, s);
1350 if (securityFlags & SEC_TRANSPORT_REQ)
1352 return multitransport_recv_request(rdp->multitransport, s);
1355 if (securityFlags & SEC_TRANSPORT_RSP)
1357 return multitransport_recv_response(rdp->multitransport, s);
1360 if (securityFlags & SEC_LICENSE_PKT)
1362 return license_recv(rdp->license, s);
1365 if (securityFlags & SEC_LICENSE_ENCRYPT_CS)
1367 return license_recv(rdp->license, s);
1370 if (securityFlags & SEC_LICENSE_ENCRYPT_SC)
1372 return license_recv(rdp->license, s);
1375 return STATE_RUN_SUCCESS;
1378 state_run_t rdp_recv_out_of_sequence_pdu(rdpRdp* rdp,
wStream* s, UINT16 pduType, UINT16 length)
1380 state_run_t rc = STATE_RUN_FAILED;
1386 rc = rdp_recv_data_pdu(rdp, s);
1388 case PDU_TYPE_SERVER_REDIRECTION:
1389 rc = rdp_recv_enhanced_security_redirection_packet(rdp, s);
1391 case PDU_TYPE_FLOW_RESPONSE:
1392 case PDU_TYPE_FLOW_STOP:
1393 case PDU_TYPE_FLOW_TEST:
1394 rc = STATE_RUN_SUCCESS;
1398 char buffer1[256] = { 0 };
1399 char buffer2[256] = { 0 };
1401 WLog_Print(rdp->log, WLOG_ERROR,
"expected %s, got %s",
1402 pdu_type_to_str(PDU_TYPE_DEMAND_ACTIVE, buffer1,
sizeof(buffer1)),
1403 pdu_type_to_str(pduType, buffer2,
sizeof(buffer2)));
1404 rc = STATE_RUN_FAILED;
1409 if (!tpkt_ensure_stream_consumed(s, length))
1410 return STATE_RUN_FAILED;
1414 BOOL rdp_read_flow_control_pdu(rdpRdp* rdp,
wStream* s, UINT16* type, UINT16* channel_id)
1428 WINPR_ASSERT(channel_id);
1430 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 6))
1432 Stream_Read_UINT8(s, pduType);
1434 Stream_Seek_UINT8(s);
1435 Stream_Seek_UINT8(s);
1436 Stream_Seek_UINT8(s);
1437 Stream_Read_UINT16(s, *channel_id);
1452 BOOL rdp_decrypt(rdpRdp* rdp,
wStream* s, UINT16* pLength, UINT16 securityFlags)
1455 BYTE cmac[8] = { 0 };
1456 BYTE wmac[8] = { 0 };
1457 BOOL status = FALSE;
1460 WINPR_ASSERT(rdp->settings);
1462 WINPR_ASSERT(pLength);
1464 if (!security_lock(rdp))
1467 INT32 length = *pLength;
1468 if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_NONE)
1471 if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
1473 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 12))
1477 Stream_Read_UINT16(s, len);
1479 WLog_Print(rdp->log, WLOG_WARN,
"ENCRYPTION_METHOD_FIPS length %" PRIu16
" != 0x10",
1483 Stream_Read_UINT8(s, version);
1485 WLog_Print(rdp->log, WLOG_WARN,
"ENCRYPTION_METHOD_FIPS version %" PRIu16
" != 1",
1489 Stream_Read_UINT8(s, pad);
1490 const BYTE* sig = Stream_ConstPointer(s);
1493 const INT32 padLength = length - pad;
1495 if ((length <= 0) || (padLength <= 0) || (padLength > UINT16_MAX))
1497 WLog_Print(rdp->log, WLOG_ERROR,
"FATAL: invalid pad length %" PRId32, padLength);
1501 if (!security_fips_decrypt(Stream_Pointer(s), (
size_t)length, rdp))
1504 if (!security_fips_check_signature(Stream_ConstPointer(s), (
size_t)padLength, sig, 8, rdp))
1507 Stream_SetLength(s, Stream_Length(s) - pad);
1508 *pLength = (UINT16)padLength;
1512 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s,
sizeof(wmac)))
1515 Stream_Read(s, wmac,
sizeof(wmac));
1516 length -=
sizeof(wmac);
1520 WLog_Print(rdp->log, WLOG_ERROR,
"FATAL: invalid length field");
1524 if (!security_decrypt(Stream_PointerAs(s, BYTE), (
size_t)length, rdp))
1527 if (securityFlags & SEC_SECURE_CHECKSUM)
1528 status = security_salted_mac_signature(rdp, Stream_ConstPointer(s), (UINT32)length,
1529 FALSE, cmac,
sizeof(cmac));
1531 status = security_mac_signature(rdp, Stream_ConstPointer(s), (UINT32)length, cmac,
1537 if (memcmp(wmac, cmac,
sizeof(wmac)) != 0)
1539 WLog_Print(rdp->log, WLOG_ERROR,
"WARNING: invalid packet signature");
1550 *pLength = (UINT16)length;
1554 if (!security_unlock(rdp))
1559 const char* pdu_type_to_str(UINT16 pduType,
char* buffer,
size_t length)
1561 const char* str = NULL;
1564 case PDU_TYPE_DEMAND_ACTIVE:
1565 str =
"PDU_TYPE_DEMAND_ACTIVE";
1567 case PDU_TYPE_CONFIRM_ACTIVE:
1568 str =
"PDU_TYPE_CONFIRM_ACTIVE";
1570 case PDU_TYPE_DEACTIVATE_ALL:
1571 str =
"PDU_TYPE_DEACTIVATE_ALL";
1574 str =
"PDU_TYPE_DATA";
1576 case PDU_TYPE_SERVER_REDIRECTION:
1577 str =
"PDU_TYPE_SERVER_REDIRECTION";
1579 case PDU_TYPE_FLOW_TEST:
1580 str =
"PDU_TYPE_FLOW_TEST";
1582 case PDU_TYPE_FLOW_RESPONSE:
1583 str =
"PDU_TYPE_FLOW_RESPONSE";
1585 case PDU_TYPE_FLOW_STOP:
1586 str =
"PDU_TYPE_FLOW_STOP";
1589 str =
"PDU_TYPE_UNKNOWN";
1593 winpr_str_append(str, buffer, length,
"");
1595 char msg[32] = { 0 };
1596 (void)_snprintf(msg,
sizeof(msg),
"[0x%08" PRIx32
"]", pduType);
1597 winpr_str_append(msg, buffer, length,
"");
1608 static state_run_t rdp_recv_tpkt_pdu(rdpRdp* rdp,
wStream* s)
1610 state_run_t rc = STATE_RUN_SUCCESS;
1613 UINT16 pduSource = 0;
1614 UINT16 channelId = 0;
1615 UINT16 securityFlags = 0;
1616 freerdp* instance = NULL;
1619 WINPR_ASSERT(rdp->context);
1622 instance = rdp->context->instance;
1623 WINPR_ASSERT(instance);
1625 if (!rdp_read_header(rdp, s, &length, &channelId))
1626 return STATE_RUN_FAILED;
1628 if (freerdp_shall_disconnect_context(rdp->context))
1629 return STATE_RUN_SUCCESS;
1631 if (rdp->autodetect->bandwidthMeasureStarted)
1633 rdp->autodetect->bandwidthMeasureByteCount += length;
1636 if (rdp->mcs->messageChannelId && (channelId == rdp->mcs->messageChannelId))
1639 if (!rdp_handle_message_channel(rdp, s, channelId, length))
1640 return STATE_RUN_FAILED;
1641 return STATE_RUN_SUCCESS;
1644 if (rdp->settings->UseRdpSecurityLayer)
1646 if (!rdp_read_security_header(rdp, s, &securityFlags, &length))
1647 return STATE_RUN_FAILED;
1649 if (securityFlags & (SEC_ENCRYPT | SEC_REDIRECTION_PKT))
1651 if (!rdp_decrypt(rdp, s, &length, securityFlags))
1652 return STATE_RUN_FAILED;
1655 if (securityFlags & SEC_REDIRECTION_PKT)
1661 Stream_Rewind(s, 2);
1664 rc = rdp_recv_enhanced_security_redirection_packet(rdp, s);
1669 if (channelId == MCS_GLOBAL_CHANNEL_ID)
1671 while (Stream_GetRemainingLength(s) > 3)
1678 if (!rdp_read_share_control_header(rdp, s, NULL, &remain, &pduType, &pduSource))
1679 return STATE_RUN_FAILED;
1681 sub = Stream_StaticInit(&subbuffer, Stream_Pointer(s), remain);
1682 if (!Stream_SafeSeek(s, remain))
1683 return STATE_RUN_FAILED;
1685 rdp->settings->PduSource = pduSource;
1691 rc = rdp_recv_data_pdu(rdp, sub);
1692 if (state_run_failed(rc))
1696 case PDU_TYPE_DEACTIVATE_ALL:
1697 if (!rdp_recv_deactivate_all(rdp, sub))
1699 WLog_Print(rdp->log, WLOG_ERROR,
1700 "rdp_recv_tpkt_pdu: rdp_recv_deactivate_all() fail");
1701 return STATE_RUN_FAILED;
1706 case PDU_TYPE_SERVER_REDIRECTION:
1707 return rdp_recv_enhanced_security_redirection_packet(rdp, sub);
1709 case PDU_TYPE_FLOW_RESPONSE:
1710 case PDU_TYPE_FLOW_STOP:
1711 case PDU_TYPE_FLOW_TEST:
1712 WLog_Print(rdp->log, WLOG_DEBUG,
"flow message 0x%04" PRIX16
"", pduType);
1714 if (!Stream_SafeSeek(sub, remain))
1715 return STATE_RUN_FAILED;
1720 char buffer[256] = { 0 };
1721 WLog_Print(rdp->log, WLOG_ERROR,
"incorrect PDU type: %s",
1722 pdu_type_to_str(pduType, buffer,
sizeof(buffer)));
1727 diff = Stream_GetRemainingLength(sub);
1730 char buffer[256] = { 0 };
1731 WLog_Print(rdp->log, WLOG_WARN,
1732 "pduType %s not properly parsed, %" PRIdz
1733 " bytes remaining unhandled. Skipping.",
1734 pdu_type_to_str(pduType, buffer,
sizeof(buffer)), diff);
1742 if (!freerdp_channel_process(instance, s, channelId, length))
1743 return STATE_RUN_FAILED;
1747 if (!tpkt_ensure_stream_consumed(s, length))
1748 return STATE_RUN_FAILED;
1752 static state_run_t rdp_recv_fastpath_pdu(rdpRdp* rdp,
wStream* s)
1757 rdpFastPath* fastpath = rdp->fastpath;
1759 if (!fastpath_read_header_rdp(fastpath, s, &length))
1761 WLog_Print(rdp->log, WLOG_ERROR,
"rdp_recv_fastpath_pdu: fastpath_read_header_rdp() fail");
1762 return STATE_RUN_FAILED;
1765 if ((length == 0) || (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, length)))
1767 WLog_Print(rdp->log, WLOG_ERROR,
"incorrect FastPath PDU header length %" PRIu16
"",
1769 return STATE_RUN_FAILED;
1772 if (rdp->autodetect->bandwidthMeasureStarted)
1774 rdp->autodetect->bandwidthMeasureByteCount += length;
1777 if (!fastpath_decrypt(fastpath, s, &length))
1778 return STATE_RUN_FAILED;
1780 return fastpath_recv_updates(rdp->fastpath, s);
1783 static state_run_t rdp_recv_pdu(rdpRdp* rdp,
wStream* s)
1785 const int rc = tpkt_verify_header(s);
1787 return rdp_recv_tpkt_pdu(rdp, s);
1789 return rdp_recv_fastpath_pdu(rdp, s);
1791 return STATE_RUN_FAILED;
1794 static state_run_t rdp_handle_sc_flags(rdpRdp* rdp,
wStream* s, UINT32 flag,
1795 CONNECTION_STATE nextState)
1797 const UINT32 mask = FINALIZE_SC_SYNCHRONIZE_PDU | FINALIZE_SC_CONTROL_COOPERATE_PDU |
1798 FINALIZE_SC_CONTROL_GRANTED_PDU | FINALIZE_SC_FONT_MAP_PDU;
1800 state_run_t status = rdp_recv_pdu(rdp, s);
1801 if (state_run_success(status))
1803 const UINT32 flags = rdp->finalize_sc_pdus & mask;
1804 if ((flags & flag) == flag)
1806 if (!rdp_client_transition_to_state(rdp, nextState))
1807 status = STATE_RUN_FAILED;
1809 status = STATE_RUN_SUCCESS;
1813 char flag_buffer[256] = { 0 };
1814 char mask_buffer[256] = { 0 };
1815 WLog_Print(rdp->log, WLOG_WARN,
1816 "[%s] unexpected server message, expected flag %s [have %s]",
1817 rdp_get_state_string(rdp),
1818 rdp_finalize_flags_to_str(flag, flag_buffer,
sizeof(flag_buffer)),
1819 rdp_finalize_flags_to_str(flags, mask_buffer,
sizeof(mask_buffer)));
1825 static state_run_t rdp_client_exchange_monitor_layout(rdpRdp* rdp,
wStream* s)
1829 if (!rdp_check_monitor_layout_pdu_state(rdp, FALSE))
1835 const UINT32 old = rdp->finalize_sc_pdus;
1836 state_run_t status = rdp_recv_pdu(rdp, s);
1837 const UINT32 now = rdp->finalize_sc_pdus;
1838 const BOOL changed = (old != now) || rdp->monitor_layout_pdu;
1841 if (state_run_success(status) && changed)
1843 if (!rdp->monitor_layout_pdu)
1845 if (!rdp_finalize_is_flag_set(rdp, FINALIZE_SC_SYNCHRONIZE_PDU))
1846 return STATE_RUN_FAILED;
1849 status = rdp_client_connect_finalize(rdp);
1850 if (state_run_success(status) && !rdp->monitor_layout_pdu)
1851 status = STATE_RUN_TRY_AGAIN;
1856 static state_run_t rdp_recv_callback_int(rdpTransport* transport,
wStream* s,
void* extra)
1858 state_run_t status = STATE_RUN_SUCCESS;
1859 rdpRdp* rdp = (rdpRdp*)extra;
1861 WINPR_ASSERT(transport);
1865 switch (rdp_get_state(rdp))
1867 case CONNECTION_STATE_NEGO:
1868 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_CREATE_REQUEST))
1869 status = STATE_RUN_FAILED;
1871 status = STATE_RUN_CONTINUE;
1873 case CONNECTION_STATE_NLA:
1874 if (nla_get_state(rdp->nla) < NLA_STATE_AUTH_INFO)
1876 if (nla_recv_pdu(rdp->nla, s) < 1)
1878 WLog_Print(rdp->log, WLOG_ERROR,
"%s - nla_recv_pdu() fail",
1879 rdp_get_state_string(rdp));
1880 status = STATE_RUN_FAILED;
1883 else if (nla_get_state(rdp->nla) == NLA_STATE_POST_NEGO)
1885 nego_recv(rdp->transport, s, (
void*)rdp->nego);
1887 if (!nego_update_settings_from_state(rdp->nego, rdp->settings))
1890 if (nego_get_state(rdp->nego) != NEGO_STATE_FINAL)
1892 WLog_Print(rdp->log, WLOG_ERROR,
"%s - nego_recv() fail",
1893 rdp_get_state_string(rdp));
1894 status = STATE_RUN_FAILED;
1896 else if (!nla_set_state(rdp->nla, NLA_STATE_FINAL))
1897 status = STATE_RUN_FAILED;
1900 if (state_run_success(status))
1902 if (nla_get_state(rdp->nla) == NLA_STATE_AUTH_INFO)
1904 transport_set_nla_mode(rdp->transport, FALSE);
1906 if (rdp->settings->VmConnectMode)
1908 if (!nego_set_state(rdp->nego, NEGO_STATE_NLA))
1909 status = STATE_RUN_FAILED;
1910 else if (!nego_set_requested_protocols(rdp->nego,
1911 PROTOCOL_HYBRID | PROTOCOL_SSL))
1912 status = STATE_RUN_FAILED;
1913 else if (!nego_send_negotiation_request(rdp->nego))
1914 status = STATE_RUN_FAILED;
1915 else if (!nla_set_state(rdp->nla, NLA_STATE_POST_NEGO))
1916 status = STATE_RUN_FAILED;
1920 if (!nla_set_state(rdp->nla, NLA_STATE_FINAL))
1921 status = STATE_RUN_FAILED;
1925 if (state_run_success(status))
1928 if (nla_get_state(rdp->nla) == NLA_STATE_FINAL)
1930 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_CREATE_REQUEST))
1931 status = STATE_RUN_FAILED;
1933 status = STATE_RUN_CONTINUE;
1938 case CONNECTION_STATE_AAD:
1939 if (aad_recv(rdp->aad, s) < 1)
1941 WLog_Print(rdp->log, WLOG_ERROR,
"%s - aad_recv() fail", rdp_get_state_string(rdp));
1942 status = STATE_RUN_FAILED;
1944 if (state_run_success(status))
1946 if (aad_get_state(rdp->aad) == AAD_STATE_FINAL)
1948 transport_set_aad_mode(rdp->transport, FALSE);
1949 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_CREATE_REQUEST))
1950 status = STATE_RUN_FAILED;
1952 status = STATE_RUN_CONTINUE;
1957 case CONNECTION_STATE_MCS_CREATE_REQUEST:
1958 if (!mcs_client_begin(rdp->mcs))
1960 WLog_Print(rdp->log, WLOG_ERROR,
"%s - mcs_client_begin() fail",
1961 rdp_get_state_string(rdp));
1962 status = STATE_RUN_FAILED;
1964 else if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_CREATE_RESPONSE))
1965 status = STATE_RUN_FAILED;
1966 else if (Stream_GetRemainingLength(s) > 0)
1967 status = STATE_RUN_CONTINUE;
1970 case CONNECTION_STATE_MCS_CREATE_RESPONSE:
1971 if (!mcs_recv_connect_response(rdp->mcs, s))
1973 WLog_Print(rdp->log, WLOG_ERROR,
"mcs_recv_connect_response failure");
1974 status = STATE_RUN_FAILED;
1978 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_ERECT_DOMAIN))
1979 status = STATE_RUN_FAILED;
1980 else if (!mcs_send_erect_domain_request(rdp->mcs))
1982 WLog_Print(rdp->log, WLOG_ERROR,
"mcs_send_erect_domain_request failure");
1983 status = STATE_RUN_FAILED;
1985 else if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_ATTACH_USER))
1986 status = STATE_RUN_FAILED;
1987 else if (!mcs_send_attach_user_request(rdp->mcs))
1989 WLog_Print(rdp->log, WLOG_ERROR,
"mcs_send_attach_user_request failure");
1990 status = STATE_RUN_FAILED;
1992 else if (!rdp_client_transition_to_state(rdp,
1993 CONNECTION_STATE_MCS_ATTACH_USER_CONFIRM))
1994 status = STATE_RUN_FAILED;
1998 case CONNECTION_STATE_MCS_ATTACH_USER_CONFIRM:
1999 if (!mcs_recv_attach_user_confirm(rdp->mcs, s))
2001 WLog_Print(rdp->log, WLOG_ERROR,
"mcs_recv_attach_user_confirm failure");
2002 status = STATE_RUN_FAILED;
2006 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST))
2007 status = STATE_RUN_FAILED;
2008 else if (!mcs_send_channel_join_request(rdp->mcs, rdp->mcs->userId))
2010 WLog_Print(rdp->log, WLOG_ERROR,
"mcs_send_channel_join_request failure");
2011 status = STATE_RUN_FAILED;
2013 else if (!rdp_client_transition_to_state(
2014 rdp, CONNECTION_STATE_MCS_CHANNEL_JOIN_RESPONSE))
2015 status = STATE_RUN_FAILED;
2020 if (!rdp_client_skip_mcs_channel_join(rdp))
2021 status = STATE_RUN_FAILED;
2025 case CONNECTION_STATE_MCS_CHANNEL_JOIN_RESPONSE:
2026 if (!rdp_client_connect_mcs_channel_join_confirm(rdp, s))
2028 WLog_Print(rdp->log, WLOG_ERROR,
2030 "rdp_client_connect_mcs_channel_join_confirm() fail",
2031 rdp_get_state_string(rdp));
2032 status = STATE_RUN_FAILED;
2037 case CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_REQUEST:
2038 if (!rdp_client_connect_auto_detect(rdp, s))
2040 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_LICENSING))
2041 status = STATE_RUN_FAILED;
2043 status = STATE_RUN_TRY_AGAIN;
2047 case CONNECTION_STATE_LICENSING:
2048 status = rdp_client_connect_license(rdp, s);
2050 if (state_run_failed(status))
2052 char buffer[64] = { 0 };
2053 WLog_Print(rdp->log, WLOG_DEBUG,
"%s - rdp_client_connect_license() - %s",
2054 rdp_get_state_string(rdp),
2055 state_run_result_string(status, buffer, ARRAYSIZE(buffer)));
2060 case CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_REQUEST:
2061 if (!rdp_client_connect_auto_detect(rdp, s))
2063 (void)rdp_client_transition_to_state(
2064 rdp, CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE);
2065 status = STATE_RUN_TRY_AGAIN;
2069 case CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE:
2070 status = rdp_client_connect_demand_active(rdp, s);
2072 if (state_run_failed(status))
2074 char buffer[64] = { 0 };
2075 WLog_Print(rdp->log, WLOG_DEBUG,
2077 "rdp_client_connect_demand_active() - %s",
2078 rdp_get_state_string(rdp),
2079 state_run_result_string(status, buffer, ARRAYSIZE(buffer)));
2081 else if (status == STATE_RUN_ACTIVE)
2083 if (!rdp_client_transition_to_state(
2084 rdp, CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE))
2085 status = STATE_RUN_FAILED;
2087 status = STATE_RUN_CONTINUE;
2091 case CONNECTION_STATE_CAPABILITIES_EXCHANGE_MONITOR_LAYOUT:
2092 status = rdp_client_exchange_monitor_layout(rdp, s);
2095 case CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE:
2096 status = rdp_client_connect_confirm_active(rdp, s);
2099 case CONNECTION_STATE_FINALIZATION_CLIENT_SYNC:
2100 status = rdp_handle_sc_flags(rdp, s, FINALIZE_SC_SYNCHRONIZE_PDU,
2101 CONNECTION_STATE_FINALIZATION_CLIENT_COOPERATE);
2103 case CONNECTION_STATE_FINALIZATION_CLIENT_COOPERATE:
2104 status = rdp_handle_sc_flags(rdp, s, FINALIZE_SC_CONTROL_COOPERATE_PDU,
2105 CONNECTION_STATE_FINALIZATION_CLIENT_GRANTED_CONTROL);
2107 case CONNECTION_STATE_FINALIZATION_CLIENT_GRANTED_CONTROL:
2108 status = rdp_handle_sc_flags(rdp, s, FINALIZE_SC_CONTROL_GRANTED_PDU,
2109 CONNECTION_STATE_FINALIZATION_CLIENT_FONT_MAP);
2111 case CONNECTION_STATE_FINALIZATION_CLIENT_FONT_MAP:
2112 status = rdp_handle_sc_flags(rdp, s, FINALIZE_SC_FONT_MAP_PDU, CONNECTION_STATE_ACTIVE);
2115 case CONNECTION_STATE_ACTIVE:
2116 status = rdp_recv_pdu(rdp, s);
2118 if (state_run_failed(status))
2120 char buffer[64] = { 0 };
2121 WLog_Print(rdp->log, WLOG_DEBUG,
"%s - rdp_recv_pdu() - %s",
2122 rdp_get_state_string(rdp),
2123 state_run_result_string(status, buffer, ARRAYSIZE(buffer)));
2128 WLog_Print(rdp->log, WLOG_ERROR,
"%s state %d", rdp_get_state_string(rdp),
2129 rdp_get_state(rdp));
2130 status = STATE_RUN_FAILED;
2134 if (state_run_failed(status))
2136 char buffer[64] = { 0 };
2137 WLog_Print(rdp->log, WLOG_ERROR,
"%s status %s", rdp_get_state_string(rdp),
2138 state_run_result_string(status, buffer, ARRAYSIZE(buffer)));
2143 state_run_t rdp_recv_callback(rdpTransport* transport,
wStream* s,
void* extra)
2145 char buffer[64] = { 0 };
2146 state_run_t rc = STATE_RUN_FAILED;
2147 const size_t start = Stream_GetPosition(s);
2148 const rdpContext* context = transport_get_context(transport);
2150 WINPR_ASSERT(context);
2153 const rdpRdp* rdp = context->rdp;
2156 if (rc == STATE_RUN_TRY_AGAIN)
2157 Stream_SetPosition(s, start);
2159 const char* old = rdp_get_state_string(rdp);
2160 const size_t orem = Stream_GetRemainingLength(s);
2161 rc = rdp_recv_callback_int(transport, s, extra);
2163 const char* now = rdp_get_state_string(rdp);
2164 const size_t rem = Stream_GetRemainingLength(s);
2166 WLog_Print(rdp->log, WLOG_TRACE,
2167 "(client)[%s -> %s] current return %s [feeding %" PRIuz
" bytes, %" PRIuz
2168 " bytes not processed]",
2169 old, now, state_run_result_string(rc, buffer,
sizeof(buffer)), orem, rem);
2170 }
while ((rc == STATE_RUN_TRY_AGAIN) || (rc == STATE_RUN_CONTINUE));
2174 BOOL rdp_send_channel_data(rdpRdp* rdp, UINT16 channelId,
const BYTE* data,
size_t size)
2176 return freerdp_channel_send(rdp, channelId, data, size);
2179 BOOL rdp_channel_send_packet(rdpRdp* rdp, UINT16 channelId,
size_t totalSize, UINT32 flags,
2180 const BYTE* data,
size_t chunkSize)
2182 return freerdp_channel_send_packet(rdp, channelId, totalSize, flags, data, chunkSize);
2185 BOOL rdp_send_error_info(rdpRdp* rdp)
2190 if (rdp->errorInfo == ERRINFO_SUCCESS)
2193 s = rdp_data_pdu_init(rdp);
2198 Stream_Write_UINT32(s, rdp->errorInfo);
2199 status = rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_SET_ERROR_INFO, 0);
2203 int rdp_check_fds(rdpRdp* rdp)
2207 rdpTransport* transport = NULL;
2210 transport = rdp->transport;
2212 tsg = transport_get_tsg(transport);
2215 if (!tsg_check_event_handles(tsg))
2217 WLog_Print(rdp->log, WLOG_ERROR,
"rdp_check_fds: tsg_check_event_handles()");
2221 if (tsg_get_state(tsg) != TSG_STATE_PIPE_CREATED)
2225 status = transport_check_fds(transport);
2229 if (!rdp_client_redirect(rdp))
2234 WLog_Print(rdp->log, WLOG_DEBUG,
"transport_check_fds() - %i", status);
2239 BOOL freerdp_get_stats(rdpRdp* rdp, UINT64* inBytes, UINT64* outBytes, UINT64* inPackets,
2246 *inBytes = rdp->inBytes;
2248 *outBytes = rdp->outBytes;
2250 *inPackets = rdp->inPackets;
2252 *outPackets = rdp->outPackets;
2262 rdpRdp* rdp_new(rdpContext* context)
2266 rdp = (rdpRdp*)calloc(1,
sizeof(rdpRdp));
2271 rdp->log = WLog_Get(RDP_TAG);
2272 WINPR_ASSERT(rdp->log);
2274 (void)_snprintf(rdp->log_context,
sizeof(rdp->log_context),
"%p", (
void*)context);
2275 WLog_SetContext(rdp->log, NULL, rdp->log_context);
2277 InitializeCriticalSection(&rdp->critical);
2278 rdp->context = context;
2279 WINPR_ASSERT(rdp->context);
2281 if (context->ServerMode)
2284 if (!context->settings)
2292 rdp->settings = context->settings;
2295 if (!rdp_set_backup_settings(rdp))
2298 rdp->settings->instance = context->instance;
2300 context->settings = rdp->settings;
2301 if (context->instance)
2302 context->settings->instance = context->instance;
2303 else if (context->peer)
2305 rdp->settings->instance = context->peer;
2307 #if defined(WITH_FREERDP_DEPRECATED)
2308 context->peer->settings = rdp->settings;
2312 rdp->transport = transport_new(context);
2314 if (!rdp->transport)
2318 const rdpTransportIo* io = transport_get_io_callbacks(rdp->transport);
2321 rdp->io = calloc(1,
sizeof(rdpTransportIo));
2327 rdp->aad = aad_new(context, rdp->transport);
2331 rdp->license = license_new(rdp);
2336 rdp->input = input_new(rdp);
2341 rdp->update = update_new(rdp);
2346 rdp->fastpath = fastpath_new(rdp);
2351 rdp->nego = nego_new(rdp->transport);
2356 rdp->mcs = mcs_new(rdp->transport);
2361 rdp->redirection = redirection_new();
2363 if (!rdp->redirection)
2366 rdp->autodetect = autodetect_new(rdp->context);
2368 if (!rdp->autodetect)
2371 rdp->heartbeat = heartbeat_new();
2373 if (!rdp->heartbeat)
2376 rdp->multitransport = multitransport_new(rdp, INITIATE_REQUEST_PROTOCOL_UDPFECL |
2377 INITIATE_REQUEST_PROTOCOL_UDPFECR);
2379 if (!rdp->multitransport)
2382 rdp->bulk = bulk_new(context);
2387 rdp->pubSub = PubSub_New(TRUE);
2391 rdp->abortEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2392 if (!rdp->abortEvent)
2397 WINPR_PRAGMA_DIAG_PUSH
2398 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2400 WINPR_PRAGMA_DIAG_POP
2404 static void rdp_reset_free(rdpRdp* rdp)
2408 (void)security_lock(rdp);
2409 rdp_free_rc4_decrypt_keys(rdp);
2410 rdp_free_rc4_encrypt_keys(rdp);
2412 winpr_Cipher_Free(rdp->fips_encrypt);
2413 winpr_Cipher_Free(rdp->fips_decrypt);
2414 rdp->fips_encrypt = NULL;
2415 rdp->fips_decrypt = NULL;
2416 (void)security_unlock(rdp);
2419 nego_free(rdp->nego);
2420 license_free(rdp->license);
2421 transport_free(rdp->transport);
2422 fastpath_free(rdp->fastpath);
2426 rdp->license = NULL;
2427 rdp->transport = NULL;
2428 rdp->fastpath = NULL;
2431 BOOL rdp_reset(rdpRdp* rdp)
2434 rdpContext* context = NULL;
2435 rdpSettings* settings = NULL;
2439 context = rdp->context;
2440 WINPR_ASSERT(context);
2442 settings = rdp->settings;
2443 WINPR_ASSERT(settings);
2445 bulk_reset(rdp->bulk);
2447 rdp_reset_free(rdp);
2462 rdp->transport = transport_new(context);
2463 if (!rdp->transport)
2468 if (!transport_set_io_callbacks(rdp->transport, rdp->io))
2473 rdp->aad = aad_new(context, rdp->transport);
2477 rdp->nego = nego_new(rdp->transport);
2481 rdp->mcs = mcs_new(rdp->transport);
2485 if (!transport_set_layer(rdp->transport, TRANSPORT_LAYER_TCP))
2488 rdp->license = license_new(rdp);
2492 rdp->fastpath = fastpath_new(rdp);
2497 rc = rdp_finalize_reset_flags(rdp, TRUE);
2508 void rdp_free(rdpRdp* rdp)
2512 rdp_reset_free(rdp);
2518 input_free(rdp->input);
2519 update_free(rdp->update);
2521 redirection_free(rdp->redirection);
2522 autodetect_free(rdp->autodetect);
2523 heartbeat_free(rdp->heartbeat);
2524 multitransport_free(rdp->multitransport);
2525 bulk_free(rdp->bulk);
2527 PubSub_Free(rdp->pubSub);
2528 if (rdp->abortEvent)
2529 (void)CloseHandle(rdp->abortEvent);
2532 DeleteCriticalSection(&rdp->critical);
2537 BOOL rdp_io_callback_set_event(rdpRdp* rdp, BOOL set)
2541 return transport_io_callback_set_event(rdp->transport, set);
2544 const rdpTransportIo* rdp_get_io_callbacks(rdpRdp* rdp)
2551 BOOL rdp_set_io_callbacks(rdpRdp* rdp,
const rdpTransportIo* io_callbacks)
2559 rdp->io = malloc(
sizeof(rdpTransportIo));
2562 *rdp->io = *io_callbacks;
2563 return transport_set_io_callbacks(rdp->transport, rdp->io);
2568 BOOL rdp_set_io_callback_context(rdpRdp* rdp,
void* usercontext)
2571 rdp->ioContext = usercontext;
2575 void* rdp_get_io_callback_context(rdpRdp* rdp)
2578 return rdp->ioContext;
2581 const char* rdp_finalize_flags_to_str(UINT32 flags,
char* buffer,
size_t size)
2583 char number[32] = { 0 };
2585 (uint32_t)~(FINALIZE_SC_SYNCHRONIZE_PDU | FINALIZE_SC_CONTROL_COOPERATE_PDU |
2586 FINALIZE_SC_CONTROL_GRANTED_PDU | FINALIZE_SC_FONT_MAP_PDU |
2587 FINALIZE_CS_SYNCHRONIZE_PDU | FINALIZE_CS_CONTROL_COOPERATE_PDU |
2588 FINALIZE_CS_CONTROL_REQUEST_PDU | FINALIZE_CS_PERSISTENT_KEY_LIST_PDU |
2589 FINALIZE_CS_FONT_LIST_PDU | FINALIZE_DEACTIVATE_REACTIVATE);
2591 if (flags & FINALIZE_SC_SYNCHRONIZE_PDU)
2592 winpr_str_append(
"FINALIZE_SC_SYNCHRONIZE_PDU", buffer, size,
"|");
2593 if (flags & FINALIZE_SC_CONTROL_COOPERATE_PDU)
2594 winpr_str_append(
"FINALIZE_SC_CONTROL_COOPERATE_PDU", buffer, size,
"|");
2595 if (flags & FINALIZE_SC_CONTROL_GRANTED_PDU)
2596 winpr_str_append(
"FINALIZE_SC_CONTROL_GRANTED_PDU", buffer, size,
"|");
2597 if (flags & FINALIZE_SC_FONT_MAP_PDU)
2598 winpr_str_append(
"FINALIZE_SC_FONT_MAP_PDU", buffer, size,
"|");
2599 if (flags & FINALIZE_CS_SYNCHRONIZE_PDU)
2600 winpr_str_append(
"FINALIZE_CS_SYNCHRONIZE_PDU", buffer, size,
"|");
2601 if (flags & FINALIZE_CS_CONTROL_COOPERATE_PDU)
2602 winpr_str_append(
"FINALIZE_CS_CONTROL_COOPERATE_PDU", buffer, size,
"|");
2603 if (flags & FINALIZE_CS_CONTROL_REQUEST_PDU)
2604 winpr_str_append(
"FINALIZE_CS_CONTROL_REQUEST_PDU", buffer, size,
"|");
2605 if (flags & FINALIZE_CS_PERSISTENT_KEY_LIST_PDU)
2606 winpr_str_append(
"FINALIZE_CS_PERSISTENT_KEY_LIST_PDU", buffer, size,
"|");
2607 if (flags & FINALIZE_CS_FONT_LIST_PDU)
2608 winpr_str_append(
"FINALIZE_CS_FONT_LIST_PDU", buffer, size,
"|");
2609 if (flags & FINALIZE_DEACTIVATE_REACTIVATE)
2610 winpr_str_append(
"FINALIZE_DEACTIVATE_REACTIVATE", buffer, size,
"|");
2612 winpr_str_append(
"UNKNOWN_FLAG", buffer, size,
"|");
2614 winpr_str_append(
"NO_FLAG_SET", buffer, size,
"|");
2615 (void)_snprintf(number,
sizeof(number),
" [0x%08" PRIx32
"]", flags);
2616 winpr_str_append(number, buffer, size,
"");
2620 BOOL rdp_finalize_reset_flags(rdpRdp* rdp, BOOL clearAll)
2623 WLog_Print(rdp->log, WLOG_DEBUG,
"[%s] reset finalize_sc_pdus", rdp_get_state_string(rdp));
2625 rdp->finalize_sc_pdus = 0;
2627 rdp->finalize_sc_pdus &= FINALIZE_DEACTIVATE_REACTIVATE;
2629 return rdp_set_monitor_layout_pdu_state(rdp, FALSE);
2632 BOOL rdp_finalize_set_flag(rdpRdp* rdp, UINT32 flag)
2634 char buffer[1024] = { 0 };
2638 WLog_Print(rdp->log, WLOG_DEBUG,
"[%s] received flag %s", rdp_get_state_string(rdp),
2639 rdp_finalize_flags_to_str(flag, buffer,
sizeof(buffer)));
2640 rdp->finalize_sc_pdus |= flag;
2644 BOOL rdp_finalize_is_flag_set(rdpRdp* rdp, UINT32 flag)
2647 return (rdp->finalize_sc_pdus & flag) == flag;
2650 BOOL rdp_reset_rc4_encrypt_keys(rdpRdp* rdp)
2653 rdp_free_rc4_encrypt_keys(rdp);
2654 rdp->rc4_encrypt_key = winpr_RC4_New(rdp->encrypt_key, rdp->rc4_key_len);
2656 rdp->encrypt_use_count = 0;
2657 return rdp->rc4_encrypt_key != NULL;
2660 void rdp_free_rc4_encrypt_keys(rdpRdp* rdp)
2663 winpr_RC4_Free(rdp->rc4_encrypt_key);
2664 rdp->rc4_encrypt_key = NULL;
2667 void rdp_free_rc4_decrypt_keys(rdpRdp* rdp)
2670 winpr_RC4_Free(rdp->rc4_decrypt_key);
2671 rdp->rc4_decrypt_key = NULL;
2674 BOOL rdp_reset_rc4_decrypt_keys(rdpRdp* rdp)
2677 rdp_free_rc4_decrypt_keys(rdp);
2678 rdp->rc4_decrypt_key = winpr_RC4_New(rdp->decrypt_key, rdp->rc4_key_len);
2680 rdp->decrypt_use_count = 0;
2681 return rdp->rc4_decrypt_key != NULL;
2684 const char* rdp_security_flag_string(UINT32 securityFlags,
char* buffer,
size_t size)
2686 if (securityFlags & SEC_EXCHANGE_PKT)
2687 winpr_str_append(
"SEC_EXCHANGE_PKT", buffer, size,
"|");
2688 if (securityFlags & SEC_TRANSPORT_REQ)
2689 winpr_str_append(
"SEC_TRANSPORT_REQ", buffer, size,
"|");
2690 if (securityFlags & SEC_TRANSPORT_RSP)
2691 winpr_str_append(
"SEC_TRANSPORT_RSP", buffer, size,
"|");
2692 if (securityFlags & SEC_ENCRYPT)
2693 winpr_str_append(
"SEC_ENCRYPT", buffer, size,
"|");
2694 if (securityFlags & SEC_RESET_SEQNO)
2695 winpr_str_append(
"SEC_RESET_SEQNO", buffer, size,
"|");
2696 if (securityFlags & SEC_IGNORE_SEQNO)
2697 winpr_str_append(
"SEC_IGNORE_SEQNO", buffer, size,
"|");
2698 if (securityFlags & SEC_INFO_PKT)
2699 winpr_str_append(
"SEC_INFO_PKT", buffer, size,
"|");
2700 if (securityFlags & SEC_LICENSE_PKT)
2701 winpr_str_append(
"SEC_LICENSE_PKT", buffer, size,
"|");
2702 if (securityFlags & SEC_LICENSE_ENCRYPT_CS)
2703 winpr_str_append(
"SEC_LICENSE_ENCRYPT_CS", buffer, size,
"|");
2704 if (securityFlags & SEC_LICENSE_ENCRYPT_SC)
2705 winpr_str_append(
"SEC_LICENSE_ENCRYPT_SC", buffer, size,
"|");
2706 if (securityFlags & SEC_REDIRECTION_PKT)
2707 winpr_str_append(
"SEC_REDIRECTION_PKT", buffer, size,
"|");
2708 if (securityFlags & SEC_SECURE_CHECKSUM)
2709 winpr_str_append(
"SEC_SECURE_CHECKSUM", buffer, size,
"|");
2710 if (securityFlags & SEC_AUTODETECT_REQ)
2711 winpr_str_append(
"SEC_AUTODETECT_REQ", buffer, size,
"|");
2712 if (securityFlags & SEC_AUTODETECT_RSP)
2713 winpr_str_append(
"SEC_AUTODETECT_RSP", buffer, size,
"|");
2714 if (securityFlags & SEC_HEARTBEAT)
2715 winpr_str_append(
"SEC_HEARTBEAT", buffer, size,
"|");
2716 if (securityFlags & SEC_FLAGSHI_VALID)
2717 winpr_str_append(
"SEC_FLAGSHI_VALID", buffer, size,
"|");
2719 char msg[32] = { 0 };
2721 (void)_snprintf(msg,
sizeof(msg),
"[0x%08" PRIx32
"]", securityFlags);
2722 winpr_str_append(msg, buffer, size,
"");
2727 static BOOL rdp_reset_remote_settings(rdpRdp* rdp)
2729 UINT32 flags = FREERDP_SETTINGS_REMOTE_MODE;
2736 return rdp->remoteSettings != NULL;
2739 BOOL rdp_set_backup_settings(rdpRdp* rdp)
2744 if (!rdp->originalSettings)
2746 return rdp_reset_remote_settings(rdp);
2749 BOOL rdp_reset_runtime_settings(rdpRdp* rdp)
2752 WINPR_ASSERT(rdp->context);
2759 return rdp_reset_remote_settings(rdp);
2762 static BOOL starts_with(
const char* tok,
const char* val)
2764 const size_t len = strlen(val);
2765 if (strncmp(tok, val, len) != 0)
2767 if (tok[len] !=
'=')
2772 static BOOL option_equals(
const char* what,
const char* val)
2774 return _stricmp(what, val) == 0;
2777 static BOOL parse_on_off_option(
const char* value)
2779 WINPR_ASSERT(value);
2780 const char* sep = strchr(value,
'=');
2783 if (option_equals(
"on", &sep[1]))
2785 if (option_equals(
"true", &sep[1]))
2787 if (option_equals(
"off", &sep[1]))
2789 if (option_equals(
"false", &sep[1]))
2793 long val = strtol(value, NULL, 0);
2795 return val == 0 ? FALSE : TRUE;
2802 static BOOL option_is_runtime_checks(wLog* log,
const char* tok)
2804 const char* experimental[] = { STR(WITH_VERBOSE_WINPR_ASSERT) };
2805 for (
size_t x = 0; x < ARRAYSIZE(experimental); x++)
2807 const char* opt = experimental[x];
2808 if (starts_with(tok, opt))
2810 return parse_on_off_option(tok);
2816 static BOOL option_is_experimental(wLog* log,
const char* tok)
2818 const char* experimental[] = { STR(WITH_DSP_EXPERIMENTAL), STR(WITH_VAAPI) };
2819 for (
size_t x = 0; x < ARRAYSIZE(experimental); x++)
2821 const char* opt = experimental[x];
2822 if (starts_with(tok, opt))
2824 return parse_on_off_option(tok);
2830 static BOOL option_is_debug(wLog* log,
const char* tok)
2833 const char* debug[] = { STR(WITH_DEBUG_ALL),
2834 STR(WITH_DEBUG_CERTIFICATE),
2835 STR(WITH_DEBUG_CAPABILITIES),
2836 STR(WITH_DEBUG_CHANNELS),
2837 STR(WITH_DEBUG_CLIPRDR),
2838 STR(WITH_DEBUG_CODECS),
2839 STR(WITH_DEBUG_RDPGFX),
2840 STR(WITH_DEBUG_DVC),
2841 STR(WITH_DEBUG_TSMF),
2842 STR(WITH_DEBUG_KBD),
2843 STR(WITH_DEBUG_LICENSE),
2844 STR(WITH_DEBUG_NEGO),
2845 STR(WITH_DEBUG_NLA),
2846 STR(WITH_DEBUG_TSG),
2847 STR(WITH_DEBUG_RAIL),
2848 STR(WITH_DEBUG_RDP),
2849 STR(WITH_DEBUG_RDPEI),
2850 STR(WITH_DEBUG_REDIR),
2851 STR(WITH_DEBUG_RDPDR),
2852 STR(WITH_DEBUG_RFX),
2853 STR(WITH_DEBUG_SCARD),
2854 STR(WITH_DEBUG_SND),
2855 STR(WITH_DEBUG_SVC),
2856 STR(WITH_DEBUG_TRANSPORT),
2857 STR(WITH_DEBUG_TIMEZONE),
2858 STR(WITH_DEBUG_WND),
2859 STR(WITH_DEBUG_X11_CLIPRDR),
2860 STR(WITH_DEBUG_X11_LOCAL_MOVESIZE),
2861 STR(WITH_DEBUG_X11),
2863 STR(WITH_DEBUG_RINGBUFFER),
2864 STR(WITH_DEBUG_SYMBOLS),
2865 STR(WITH_DEBUG_EVENTS),
2866 STR(WITH_DEBUG_MUTEX),
2867 STR(WITH_DEBUG_NTLM),
2868 STR(WITH_DEBUG_SDL_EVENTS),
2869 STR(WITH_DEBUG_SDL_KBD_EVENTS),
2870 STR(WITH_DEBUG_THREADS),
2871 STR(WITH_DEBUG_URBDRC) };
2873 for (
size_t x = 0; x < ARRAYSIZE(debug); x++)
2875 const char* opt = debug[x];
2876 if (starts_with(tok, opt))
2877 return parse_on_off_option(tok);
2880 if (starts_with(tok,
"WITH_DEBUG"))
2882 WLog_Print(log, WLOG_WARN,
"[BUG] Unmapped Debug-Build option '%s'.", tok);
2883 return parse_on_off_option(tok);
2889 static void log_build_warn(rdpRdp* rdp,
const char* what,
const char* msg,
2890 BOOL (*cmp)(wLog* log,
const char* tok))
2893 WINPR_PRAGMA_DIAG_PUSH
2894 WINPR_PRAGMA_DIAG_IGNORED_OVERLENGTH_STRINGS
2896 size_t len =
sizeof(FREERDP_BUILD_CONFIG);
2897 char* list = calloc(len,
sizeof(
char));
2898 char* config = _strdup(FREERDP_BUILD_CONFIG);
2899 WINPR_PRAGMA_DIAG_POP
2903 char* saveptr = NULL;
2904 char* tok = strtok_s(config,
" ", &saveptr);
2907 if (cmp(rdp->log, tok))
2908 winpr_str_append(tok, list, len,
" ");
2910 tok = strtok_s(NULL,
" ", &saveptr);
2917 if (strlen(list) > 0)
2919 WLog_Print(rdp->log, WLOG_WARN,
"*************************************************");
2920 WLog_Print(rdp->log, WLOG_WARN,
"This build is using [%s] build options:", what);
2922 char* saveptr = NULL;
2923 char* tok = strtok_s(list,
" ", &saveptr);
2926 WLog_Print(rdp->log, WLOG_WARN,
"* '%s'", tok);
2927 tok = strtok_s(NULL,
" ", &saveptr);
2929 WLog_Print(rdp->log, WLOG_WARN,
"");
2930 WLog_Print(rdp->log, WLOG_WARN,
"[%s] build options %s", what, msg);
2931 WLog_Print(rdp->log, WLOG_WARN,
"*************************************************");
2937 #define print_first_line(log, firstLine, what) \
2938 print_first_line_int((log), (firstLine), (what), __FILE__, __func__, __LINE__)
2939 static void print_first_line_int(wLog* log, log_line_t* firstLine,
const char* what,
2940 const char* file,
const char* fkt,
size_t line)
2942 WINPR_ASSERT(firstLine);
2943 if (!firstLine->fkt)
2945 const DWORD level = WLOG_WARN;
2946 if (WLog_IsLevelActive(log, level))
2948 WLog_PrintMessage(log, WLOG_MESSAGE_TEXT, level, line, file, fkt,
2949 "*************************************************");
2950 WLog_PrintMessage(log, WLOG_MESSAGE_TEXT, level, line, file, fkt,
2951 "[SSL] {%s} build or configuration missing:", what);
2953 firstLine->line = line;
2954 firstLine->file = file;
2955 firstLine->fkt = fkt;
2956 firstLine->level = level;
2960 static void print_last_line(wLog* log,
const log_line_t* firstLine)
2962 WINPR_ASSERT(firstLine);
2965 if (WLog_IsLevelActive(log, firstLine->level))
2966 WLog_PrintMessage(log, WLOG_MESSAGE_TEXT, firstLine->level, firstLine->line,
2967 firstLine->file, firstLine->fkt,
2968 "*************************************************");
2972 static void log_build_warn_cipher(rdpRdp* rdp, log_line_t* firstLine, WINPR_CIPHER_TYPE md,
2975 BOOL haveCipher = FALSE;
2977 char key[WINPR_CIPHER_MAX_KEY_LENGTH] = { 0 };
2978 char iv[WINPR_CIPHER_MAX_IV_LENGTH] = { 0 };
2982 if (md == WINPR_CIPHER_ARC4_128)
2984 WINPR_RC4_CTX* enc = winpr_RC4_New(key,
sizeof(key));
2985 haveCipher = enc != NULL;
2986 winpr_RC4_Free(enc);
2990 WINPR_CIPHER_CTX* enc =
2991 winpr_Cipher_NewEx(md, WINPR_ENCRYPT, key,
sizeof(key), iv,
sizeof(iv));
2992 WINPR_CIPHER_CTX* dec =
2993 winpr_Cipher_NewEx(md, WINPR_DECRYPT, key,
sizeof(key), iv,
sizeof(iv));
2997 winpr_Cipher_Free(enc);
2998 winpr_Cipher_Free(dec);
3003 print_first_line(rdp->log, firstLine,
"Cipher");
3004 WLog_Print(rdp->log, WLOG_WARN,
"* %s: %s", winpr_cipher_type_to_string(md), what);
3008 static void log_build_warn_hmac(rdpRdp* rdp, log_line_t* firstLine, WINPR_MD_TYPE md,
3011 BOOL haveHmacX = FALSE;
3012 WINPR_HMAC_CTX* hmac = winpr_HMAC_New();
3018 char key[WINPR_CIPHER_MAX_KEY_LENGTH] = { 0 };
3019 haveHmacX = winpr_HMAC_Init(hmac, md, key,
sizeof(key));
3021 winpr_HMAC_Free(hmac);
3025 print_first_line(rdp->log, firstLine,
"HMAC");
3026 WLog_Print(rdp->log, WLOG_WARN,
" * %s: %s", winpr_md_type_to_string(md), what);
3030 static void log_build_warn_hash(rdpRdp* rdp, log_line_t* firstLine, WINPR_MD_TYPE md,
3033 BOOL haveDigestX = FALSE;
3035 WINPR_DIGEST_CTX* digest = winpr_Digest_New();
3037 haveDigestX = winpr_Digest_Init(digest, md);
3038 winpr_Digest_Free(digest);
3042 print_first_line(rdp->log, firstLine,
"Digest");
3043 WLog_Print(rdp->log, WLOG_WARN,
" * %s: %s", winpr_md_type_to_string(md), what);
3047 static void log_build_warn_ssl(rdpRdp* rdp)
3051 log_line_t firstHashLine = { 0 };
3052 log_build_warn_hash(rdp, &firstHashLine, WINPR_MD_MD4,
"NTLM support not available");
3053 log_build_warn_hash(rdp, &firstHashLine, WINPR_MD_MD5,
3054 "NTLM, assistance files with encrypted passwords, autoreconnect cookies, "
3055 "licensing and RDP security will not work");
3056 log_build_warn_hash(rdp, &firstHashLine, WINPR_MD_SHA1,
3057 "assistance files with encrypted passwords, Kerberos, Smartcard Logon, RDP "
3058 "security support not available");
3059 log_build_warn_hash(
3060 rdp, &firstHashLine, WINPR_MD_SHA256,
3061 "file clipboard, AAD gateway, NLA security and certificates might not work");
3062 print_last_line(rdp->log, &firstHashLine);
3064 log_line_t firstHmacLine = { 0 };
3065 log_build_warn_hmac(rdp, &firstHmacLine, WINPR_MD_MD5,
"Autoreconnect cookie not supported");
3066 log_build_warn_hmac(rdp, &firstHmacLine, WINPR_MD_SHA1,
"RDP security not supported");
3067 print_last_line(rdp->log, &firstHmacLine);
3069 log_line_t firstCipherLine = { 0 };
3070 log_build_warn_cipher(rdp, &firstCipherLine, WINPR_CIPHER_ARC4_128,
3071 "assistance files with encrypted passwords, NTLM, RDP licensing and RDP "
3072 "security will not work");
3073 log_build_warn_cipher(rdp, &firstCipherLine, WINPR_CIPHER_DES_EDE3_CBC,
3074 "RDP security FIPS mode will not work");
3075 log_build_warn_cipher(
3076 rdp, &firstCipherLine, WINPR_CIPHER_AES_128_CBC,
3077 "assistance file encrypted LHTicket will not work and ARM gateway might not");
3078 log_build_warn_cipher(rdp, &firstCipherLine, WINPR_CIPHER_AES_192_CBC,
3079 "ARM gateway might not work");
3080 log_build_warn_cipher(rdp, &firstCipherLine, WINPR_CIPHER_AES_256_CBC,
3081 "ARM gateway might not work");
3082 print_last_line(rdp->log, &firstCipherLine);
3085 void rdp_log_build_warnings(rdpRdp* rdp)
3087 static unsigned count = 0;
3096 log_build_warn(rdp,
"experimental",
"might crash the application", option_is_experimental);
3097 log_build_warn(rdp,
"debug",
"might leak sensitive information (credentials, ...)",
3099 log_build_warn(rdp,
"runtime-check",
"might slow down the application",
3100 option_is_runtime_checks);
3101 log_build_warn_ssl(rdp);
WINPR_API void WINPR_JSON_Delete(WINPR_JSON *item)
Delete a WinPR JSON wrapper object.
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 rdpSettings * freerdp_settings_new(DWORD flags)
creates a new setting struct
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 void freerdp_settings_free(rdpSettings *settings)
Free a settings struct with all data in it.
#define FREERDP_SETTINGS_SERVER_MODE
FREERDP_API rdpSettings * freerdp_settings_clone(const rdpSettings *settings)
Creates a deep copy of settings.