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>
36 #include "redirection.h"
38 #include <freerdp/codec/bulk.h>
39 #include <freerdp/crypto/per.h>
40 #include <freerdp/log.h>
41 #include <freerdp/buildflags.h>
43 #define RDP_TAG FREERDP_TAG("core.rdp")
53 static const char* DATA_PDU_TYPE_STRINGS[80] = {
95 "Set Keyboard Indicators",
97 "Bitmap Cache Persistent List",
99 "Set Keyboard IME Status",
100 "Offscreen Cache Error",
102 "Draw Nine Grid Error",
121 #define rdp_check_monitor_layout_pdu_state(rdp, expected) \
122 rdp_check_monitor_layout_pdu_state_(rdp, expected, __FILE__, __func__, __LINE__)
124 static BOOL rdp_check_monitor_layout_pdu_state_(
const rdpRdp* rdp, BOOL expected,
const char* file,
125 const char* fkt,
size_t line)
128 if (expected != rdp->monitor_layout_pdu)
130 const DWORD log_level = WLOG_ERROR;
131 if (WLog_IsLevelActive(rdp->log, log_level))
133 WLog_PrintMessage(rdp->log, WLOG_MESSAGE_TEXT, log_level, line, file, fkt,
134 "Expected rdp->monitor_layout_pdu == %s",
135 expected ?
"TRUE" :
"FALSE");
142 #define rdp_set_monitor_layout_pdu_state(rdp, expected) \
143 rdp_set_monitor_layout_pdu_state_(rdp, expected, __FILE__, __func__, __LINE__)
144 static BOOL rdp_set_monitor_layout_pdu_state_(rdpRdp* rdp, BOOL value,
const char* file,
145 const char* fkt,
size_t line)
149 if (value && (value == rdp->monitor_layout_pdu))
151 const DWORD log_level = WLOG_WARN;
152 if (WLog_IsLevelActive(rdp->log, log_level))
154 WLog_PrintMessage(rdp->log, WLOG_MESSAGE_TEXT, log_level, line, file, fkt,
155 "rdp->monitor_layout_pdu == %s, expected FALSE",
156 value ?
"TRUE" :
"FALSE");
160 rdp->monitor_layout_pdu = value;
164 const char* data_pdu_type_to_string(UINT8 type)
166 if (type >= ARRAYSIZE(DATA_PDU_TYPE_STRINGS))
168 return DATA_PDU_TYPE_STRINGS[type];
171 static BOOL rdp_read_flow_control_pdu(rdpRdp* rdp,
wStream* s, UINT16* type, UINT16* channel_id);
172 static BOOL rdp_write_share_control_header(rdpRdp* rdp,
wStream* s,
size_t length, UINT16 type,
174 static BOOL rdp_write_share_data_header(rdpRdp* rdp,
wStream* s,
size_t length, BYTE type,
187 BOOL rdp_read_security_header(rdpRdp* rdp,
wStream* s, UINT16* flags, UINT16* length)
189 char buffer[256] = { 0 };
195 if ((length && (*length < 4)))
197 WLog_Print(rdp->log, WLOG_WARN,
198 "invalid security header length, have %" PRIu16
", must be >= 4", *length);
201 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 4))
204 Stream_Read_UINT16(s, *flags);
206 WLog_Print(rdp->log, WLOG_TRACE,
"%s",
207 rdp_security_flag_string(*flags, buffer,
sizeof(buffer)));
223 BOOL rdp_write_security_header(rdpRdp* rdp,
wStream* s, UINT16 flags)
225 char buffer[256] = { 0 };
229 if (!Stream_CheckAndLogRequiredCapacityWLog(rdp->log, (s), 4))
232 WLog_Print(rdp->log, WLOG_TRACE,
"%s", rdp_security_flag_string(flags, buffer,
sizeof(buffer)));
234 Stream_Write_UINT16(s, flags);
235 Stream_Write_UINT16(s, 0);
239 BOOL rdp_read_share_control_header(rdpRdp* rdp,
wStream* s, UINT16* tpktLength,
240 UINT16* remainingLength, UINT16* type, UINT16* channel_id)
248 WINPR_ASSERT(channel_id);
250 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 2))
254 Stream_Read_UINT16(s, len);
260 if (!rdp_read_flow_control_pdu(rdp, s, type, channel_id))
266 *remainingLength = 0;
268 char buffer[128] = { 0 };
269 WLog_Print(rdp->log, WLOG_DEBUG,
270 "[Flow control PDU] type=%s, tpktLength=%" PRIuz
", remainingLength=%" PRIuz,
271 pdu_type_to_str(*type, buffer,
sizeof(buffer)), tpktLength ? *tpktLength : 0,
272 remainingLength ? *remainingLength : 0);
278 WLog_Print(rdp->log, WLOG_ERROR,
279 "Invalid share control header, length is %" PRIu16
", must be >4", len);
286 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 2))
289 Stream_Read_UINT16(s, tmp);
292 size_t remLen = len - 4;
295 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 2))
298 Stream_Read_UINT16(s, *channel_id);
304 char buffer[128] = { 0 };
305 WLog_Print(rdp->log, WLOG_DEBUG,
"type=%s, tpktLength=%" PRIuz
", remainingLength=%" PRIuz,
306 pdu_type_to_str(*type, buffer,
sizeof(buffer)), len, remLen);
308 *remainingLength = remLen;
309 return Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, remLen);
312 BOOL rdp_write_share_control_header(rdpRdp* rdp,
wStream* s,
size_t length, UINT16 type,
317 if (length > UINT16_MAX)
320 if (length < RDP_PACKET_HEADER_MAX_LENGTH)
322 if (!Stream_CheckAndLogRequiredCapacityWLog(rdp->log, (s), 6))
324 length -= RDP_PACKET_HEADER_MAX_LENGTH;
326 Stream_Write_UINT16(s, length);
327 Stream_Write_UINT16(s, type | 0x10);
328 Stream_Write_UINT16(s, channel_id);
332 BOOL rdp_read_share_data_header(rdpRdp* rdp,
wStream* s, UINT16* length, BYTE* type,
333 UINT32* shareId, BYTE* compressedType, UINT16* compressedLength)
338 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 12))
342 Stream_Read_UINT32(s, *shareId);
343 Stream_Seek_UINT8(s);
344 Stream_Seek_UINT8(s);
345 Stream_Read_UINT16(s, *length);
346 Stream_Read_UINT8(s, *type);
347 Stream_Read_UINT8(s, *compressedType);
348 Stream_Read_UINT16(s, *compressedLength);
352 BOOL rdp_write_share_data_header(rdpRdp* rdp,
wStream* s,
size_t length, BYTE type, UINT32 share_id)
354 const size_t headerLen = RDP_PACKET_HEADER_MAX_LENGTH + RDP_SHARE_CONTROL_HEADER_LENGTH +
355 RDP_SHARE_DATA_HEADER_LENGTH;
359 if (length > UINT16_MAX)
362 if (length < headerLen)
365 if (!Stream_CheckAndLogRequiredCapacityWLog(rdp->log, (s), 12))
369 Stream_Write_UINT32(s, share_id);
370 Stream_Write_UINT8(s, 0);
371 Stream_Write_UINT8(s, STREAM_LOW);
372 Stream_Write_UINT16(s, length);
373 Stream_Write_UINT8(s, type);
374 Stream_Write_UINT8(s, 0);
375 Stream_Write_UINT16(s, 0);
379 static BOOL rdp_security_stream_init(rdpRdp* rdp,
wStream* s, BOOL sec_header)
386 if (!Stream_SafeSeek(s, 12))
389 if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
391 if (!Stream_SafeSeek(s, 4))
395 rdp->sec_flags |= SEC_ENCRYPT;
397 if (rdp->do_secure_checksum)
398 rdp->sec_flags |= SEC_SECURE_CHECKSUM;
400 else if (rdp->sec_flags != 0 || sec_header)
402 if (!Stream_SafeSeek(s, 4))
409 wStream* rdp_send_stream_init(rdpRdp* rdp)
414 WINPR_ASSERT(rdp->transport);
416 s = transport_send_stream_init(rdp->transport, 4096);
421 if (!Stream_SafeSeek(s, RDP_PACKET_HEADER_MAX_LENGTH))
424 if (!rdp_security_stream_init(rdp, s, FALSE))
433 wStream* rdp_send_stream_pdu_init(rdpRdp* rdp)
435 wStream* s = rdp_send_stream_init(rdp);
440 if (!Stream_SafeSeek(s, RDP_SHARE_CONTROL_HEADER_LENGTH))
449 wStream* rdp_data_pdu_init(rdpRdp* rdp)
451 wStream* s = rdp_send_stream_pdu_init(rdp);
456 if (!Stream_SafeSeek(s, RDP_SHARE_DATA_HEADER_LENGTH))
465 BOOL rdp_set_error_info(rdpRdp* rdp, UINT32 errorInfo)
469 rdp->errorInfo = errorInfo;
471 if (rdp->errorInfo != ERRINFO_SUCCESS)
473 rdpContext* context = rdp->context;
474 WINPR_ASSERT(context);
476 rdp_print_errinfo(rdp->errorInfo);
480 freerdp_set_last_error_log(context, MAKE_FREERDP_ERROR(
ERRINFO, errorInfo));
484 ErrorInfoEventArgs e = { 0 };
485 EventArgsInit(&e,
"freerdp");
486 e.code = rdp->errorInfo;
487 PubSub_OnErrorInfo(context->pubSub, context, &e);
491 WLog_Print(rdp->log, WLOG_ERROR,
"missing context=%p", context);
495 freerdp_set_last_error_log(rdp->context, FREERDP_ERROR_SUCCESS);
501 wStream* rdp_message_channel_pdu_init(rdpRdp* rdp)
507 s = transport_send_stream_init(rdp->transport, 4096);
512 if (!Stream_SafeSeek(s, RDP_PACKET_HEADER_MAX_LENGTH))
515 if (!rdp_security_stream_init(rdp, s, TRUE))
534 BOOL rdp_read_header(rdpRdp* rdp,
wStream* s, UINT16* length, UINT16* channelId)
540 UINT16 initiator = 0;
543 WINPR_ASSERT(rdp->settings);
545 DomainMCSPDU MCSPDU = (rdp->settings->ServerMode) ? DomainMCSPDU_SendDataRequest
546 : DomainMCSPDU_SendDataIndication;
549 if (!tpkt_read_header(s, length))
552 if (!tpdu_read_header(s, &code, &li, *length))
555 if (code != X224_TPDU_DATA)
557 if (code == X224_TPDU_DISCONNECT_REQUEST)
559 WLog_Print(rdp->log, WLOG_WARN,
"Received X224_TPDU_DISCONNECT_REQUEST, terminating");
560 utils_abort_connect(rdp);
564 WLog_Print(rdp->log, WLOG_WARN,
565 "Unexpected X224 TPDU type %s [%08" PRIx32
"] instead of %s",
566 tpdu_type_to_string(code), code, tpdu_type_to_string(X224_TPDU_DATA));
570 if (!per_read_choice(s, &choice))
573 const DomainMCSPDU domainMCSPDU = (DomainMCSPDU)(choice >> 2);
575 if (domainMCSPDU != MCSPDU)
577 if (domainMCSPDU != DomainMCSPDU_DisconnectProviderUltimatum)
579 WLog_Print(rdp->log, WLOG_WARN,
"Received %s instead of %s",
580 mcs_domain_pdu_string(domainMCSPDU), mcs_domain_pdu_string(MCSPDU));
585 MCSPDU = domainMCSPDU;
589 WLog_Print(rdp->log, WLOG_WARN,
"TPDU invalid length, got %" PRIu16
", expected at least 8",
594 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, *length - 8))
597 if (MCSPDU == DomainMCSPDU_DisconnectProviderUltimatum)
600 TerminateEventArgs e = { 0 };
602 if (!mcs_recv_disconnect_provider_ultimatum(rdp->mcs, s, &reason))
605 rdpContext* context = rdp->context;
606 WINPR_ASSERT(context);
607 context->disconnectUltimatum = reason;
609 if (rdp->errorInfo == ERRINFO_SUCCESS)
616 UINT32 errorInfo = ERRINFO_RPC_INITIATED_DISCONNECT;
617 if (reason == Disconnect_Ultimatum_provider_initiated)
618 errorInfo = ERRINFO_RPC_INITIATED_DISCONNECT;
619 else if (reason == Disconnect_Ultimatum_user_requested)
620 errorInfo = ERRINFO_LOGOFF_BY_USER;
622 rdp_set_error_info(rdp, errorInfo);
625 WLog_Print(rdp->log, WLOG_DEBUG,
"DisconnectProviderUltimatum: reason: %d", reason);
626 utils_abort_connect(rdp);
627 EventArgsInit(&e,
"freerdp");
629 PubSub_OnTerminate(rdp->pubSub, context, &e);
633 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 5))
636 if (!per_read_integer16(s, &initiator, MCS_BASE_CHANNEL_ID))
639 if (!per_read_integer16(s, channelId, 0))
642 Stream_Read_UINT8(s,
byte);
644 if (!per_read_length(s, length))
647 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, *length))
663 BOOL rdp_write_header(rdpRdp* rdp,
wStream* s,
size_t length, UINT16 channelId)
666 WINPR_ASSERT(rdp->settings);
668 WINPR_ASSERT(length >= RDP_PACKET_HEADER_MAX_LENGTH);
669 if (length > UINT16_MAX)
672 DomainMCSPDU MCSPDU = (rdp->settings->ServerMode) ? DomainMCSPDU_SendDataIndication
673 : DomainMCSPDU_SendDataRequest;
675 if ((rdp->sec_flags & SEC_ENCRYPT) &&
676 (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS))
678 const UINT16 body_length = length - RDP_PACKET_HEADER_MAX_LENGTH;
679 const UINT16 pad = 8 - (body_length % 8);
685 if (!mcs_write_domain_mcspdu_header(s, MCSPDU, length, 0))
687 if (!per_write_integer16(s, rdp->mcs->userId, MCS_BASE_CHANNEL_ID))
689 if (!per_write_integer16(s, channelId, 0))
691 if (!Stream_EnsureRemainingCapacity(s, 3))
693 Stream_Write_UINT8(s, 0x70);
700 length = (length - RDP_PACKET_HEADER_MAX_LENGTH) | 0x8000;
701 Stream_Write_UINT16_BE(s, length);
705 static BOOL rdp_security_stream_out(rdpRdp* rdp,
wStream* s,
size_t length, UINT32 sec_flags,
710 if (length > UINT16_MAX)
713 sec_flags |= rdp->sec_flags;
718 if (!rdp_write_security_header(rdp, s, sec_flags))
721 if (sec_flags & SEC_ENCRYPT)
724 if (!security_lock(rdp))
727 if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
729 BYTE* data = Stream_PointerAs(s, BYTE) + 12;
730 const size_t size = (data - Stream_Buffer(s));
736 Stream_Write_UINT16(s, 0x10);
737 Stream_Write_UINT8(s, 0x1);
739 *pad = 8 - (length % 8);
745 memset(data + length, 0, *pad);
747 Stream_Write_UINT8(s, *pad);
749 if (!Stream_CheckAndLogRequiredCapacityWLog(rdp->log, s, 8))
751 if (!security_hmac_signature(data, length, Stream_Pointer(s), 8, rdp))
755 if (!security_fips_encrypt(data, length + *pad, rdp))
760 const BYTE* data = Stream_PointerAs(s,
const BYTE) + 8;
761 const size_t diff = Stream_GetPosition(s) + 8ULL;
766 if (!Stream_CheckAndLogRequiredCapacityWLog(rdp->log, s, 8))
768 if (sec_flags & SEC_SECURE_CHECKSUM)
769 status = security_salted_mac_signature(rdp, data, (UINT32)length, TRUE,
770 Stream_Pointer(s), 8);
772 status = security_mac_signature(rdp, data, (UINT32)length,
773 Stream_PointerAs(s, BYTE), 8);
780 if (!security_encrypt(Stream_Pointer(s), length, rdp))
787 if (!security_unlock(rdp))
799 static UINT32 rdp_get_sec_bytes(rdpRdp* rdp, UINT16 sec_flags)
801 UINT32 sec_bytes = 0;
803 if (rdp->sec_flags & SEC_ENCRYPT)
807 if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
810 else if (rdp->sec_flags != 0 || sec_flags != 0)
829 BOOL rdp_send(rdpRdp* rdp,
wStream* s, UINT16 channel_id)
840 size_t length = Stream_GetPosition(s);
841 Stream_SetPosition(s, 0);
842 if (!rdp_write_header(rdp, s, length, channel_id))
845 if (!rdp_security_stream_out(rdp, s, length, 0, &pad))
849 Stream_SetPosition(s, length);
850 Stream_SealLength(s);
852 if (transport_write(rdp->transport, s) < 0)
861 BOOL rdp_send_pdu(rdpRdp* rdp,
wStream* s, UINT16 type, UINT16 channel_id)
863 UINT32 sec_bytes = 0;
870 size_t length = Stream_GetPosition(s);
871 Stream_SetPosition(s, 0);
872 if (!rdp_write_header(rdp, s, length, MCS_GLOBAL_CHANNEL_ID))
874 sec_bytes = rdp_get_sec_bytes(rdp, 0);
875 sec_hold = Stream_GetPosition(s);
876 Stream_Seek(s, sec_bytes);
877 if (!rdp_write_share_control_header(rdp, s, length - sec_bytes, type, channel_id))
879 Stream_SetPosition(s, sec_hold);
881 if (!rdp_security_stream_out(rdp, s, length, 0, &pad))
885 Stream_SetPosition(s, length);
886 Stream_SealLength(s);
888 if (transport_write(rdp->transport, s) < 0)
894 BOOL rdp_send_data_pdu(rdpRdp* rdp,
wStream* s, BYTE type, UINT16 channel_id)
897 UINT32 sec_bytes = 0;
907 size_t length = Stream_GetPosition(s);
908 Stream_SetPosition(s, 0);
909 if (!rdp_write_header(rdp, s, length, MCS_GLOBAL_CHANNEL_ID))
911 sec_bytes = rdp_get_sec_bytes(rdp, 0);
912 sec_hold = Stream_GetPosition(s);
913 Stream_Seek(s, sec_bytes);
914 if (!rdp_write_share_control_header(rdp, s, length - sec_bytes, PDU_TYPE_DATA, channel_id))
916 if (!rdp_write_share_data_header(rdp, s, length - sec_bytes, type, rdp->settings->ShareId))
918 Stream_SetPosition(s, sec_hold);
920 if (!rdp_security_stream_out(rdp, s, length, 0, &pad))
924 Stream_SetPosition(s, length);
925 Stream_SealLength(s);
926 WLog_Print(rdp->log, WLOG_DEBUG,
927 "sending data (type=0x%x size=%" PRIuz
" channelId=%" PRIu16
")", type,
928 Stream_Length(s), channel_id);
931 if (transport_write(rdp->transport, s) < 0)
940 BOOL rdp_send_message_channel_pdu(rdpRdp* rdp,
wStream* s, UINT16 sec_flags)
948 size_t length = Stream_GetPosition(s);
949 Stream_SetPosition(s, 0);
950 if (!rdp_write_header(rdp, s, length, rdp->mcs->messageChannelId))
953 if (!rdp_security_stream_out(rdp, s, length, sec_flags, &pad))
957 Stream_SetPosition(s, length);
958 Stream_SealLength(s);
960 if (transport_write(rdp->transport, s) < 0)
969 static BOOL rdp_recv_server_shutdown_denied_pdu(rdpRdp* rdp,
wStream* s)
974 static BOOL rdp_recv_server_set_keyboard_indicators_pdu(rdpRdp* rdp,
wStream* s)
982 rdpContext* context = rdp->context;
983 WINPR_ASSERT(context);
984 WINPR_ASSERT(context->update);
986 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 4))
989 Stream_Read_UINT16(s, unitId);
990 Stream_Read_UINT16(s, ledFlags);
991 return IFCALLRESULT(TRUE, context->update->SetKeyboardIndicators, context, ledFlags);
994 static BOOL rdp_recv_server_set_keyboard_ime_status_pdu(rdpRdp* rdp,
wStream* s)
998 UINT32 imeConvMode = 0;
1000 if (!rdp || !rdp->input)
1003 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 10))
1006 Stream_Read_UINT16(s, unitId);
1007 Stream_Read_UINT32(s, imeState);
1008 Stream_Read_UINT32(s, imeConvMode);
1009 IFCALL(rdp->update->SetKeyboardImeStatus, rdp->context, unitId, imeState, imeConvMode);
1013 static BOOL rdp_recv_set_error_info_data_pdu(rdpRdp* rdp,
wStream* s)
1015 UINT32 errorInfo = 0;
1017 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 4))
1020 Stream_Read_UINT32(s, errorInfo);
1021 return rdp_set_error_info(rdp, errorInfo);
1024 static BOOL rdp_recv_server_auto_reconnect_status_pdu(rdpRdp* rdp,
wStream* s)
1026 UINT32 arcStatus = 0;
1028 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 4))
1031 Stream_Read_UINT32(s, arcStatus);
1032 WLog_Print(rdp->log, WLOG_WARN,
"AutoReconnectStatus: 0x%08" PRIX32
"", arcStatus);
1036 static BOOL rdp_recv_server_status_info_pdu(rdpRdp* rdp,
wStream* s)
1038 UINT32 statusCode = 0;
1040 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 4))
1043 Stream_Read_UINT32(s, statusCode);
1045 if (rdp->update->ServerStatusInfo)
1046 return rdp->update->ServerStatusInfo(rdp->context, statusCode);
1051 static BOOL rdp_recv_monitor_layout_pdu(rdpRdp* rdp,
wStream* s)
1053 UINT32 monitorCount = 0;
1058 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 4))
1061 Stream_Read_UINT32(s, monitorCount);
1063 if (!Stream_CheckAndLogRequiredLengthOfSizeWLog(rdp->log, s, monitorCount, 20ull))
1068 if (!monitorDefArray)
1071 for (UINT32 index = 0; index < monitorCount; index++)
1074 Stream_Read_INT32(s, monitor->left);
1075 Stream_Read_INT32(s, monitor->top);
1076 Stream_Read_INT32(s, monitor->right);
1077 Stream_Read_INT32(s, monitor->bottom);
1078 Stream_Read_UINT32(s, monitor->flags);
1081 IFCALLRET(rdp->update->RemoteMonitors, ret, rdp->context, monitorCount, monitorDefArray);
1082 free(monitorDefArray);
1085 return rdp_set_monitor_layout_pdu_state(rdp, TRUE);
1088 state_run_t rdp_recv_data_pdu(rdpRdp* rdp,
wStream* s)
1094 BYTE compressedType = 0;
1095 UINT16 compressedLength = 0;
1098 if (!rdp_read_share_data_header(rdp, s, &length, &type, &shareId, &compressedType,
1101 WLog_Print(rdp->log, WLOG_ERROR,
"rdp_read_share_data_header() failed");
1102 return STATE_RUN_FAILED;
1107 if (compressedType & PACKET_COMPRESSED)
1109 if (compressedLength < 18)
1111 WLog_Print(rdp->log, WLOG_ERROR,
1112 "bulk_decompress: not enough bytes for compressedLength %" PRIu16
"",
1114 return STATE_RUN_FAILED;
1118 const BYTE* pDstData = NULL;
1119 UINT16 SrcSize = compressedLength - 18;
1121 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, SrcSize))
1123 WLog_Print(rdp->log, WLOG_ERROR,
1124 "bulk_decompress: not enough bytes for compressedLength %" PRIu16
"",
1126 return STATE_RUN_FAILED;
1129 if (bulk_decompress(rdp->bulk, Stream_ConstPointer(s), SrcSize, &pDstData, &DstSize,
1132 cs = transport_take_from_pool(rdp->transport, DstSize);
1135 WLog_Print(rdp->log, WLOG_ERROR,
"Couldn't take stream from pool");
1136 return STATE_RUN_FAILED;
1139 Stream_SetPosition(cs, 0);
1140 Stream_Write(cs, pDstData, DstSize);
1141 Stream_SealLength(cs);
1142 Stream_SetPosition(cs, 0);
1146 WLog_Print(rdp->log, WLOG_ERROR,
"bulk_decompress() failed");
1147 return STATE_RUN_FAILED;
1150 Stream_Seek(s, SrcSize);
1153 WLog_Print(rdp->log, WLOG_DEBUG,
"recv %s Data PDU (0x%02" PRIX8
"), length: %" PRIu16
"",
1154 data_pdu_type_to_string(type), type, length);
1158 case DATA_PDU_TYPE_UPDATE:
1159 if (!update_recv(rdp->update, cs))
1161 WLog_Print(rdp->log, WLOG_ERROR,
"DATA_PDU_TYPE_UPDATE - update_recv() failed");
1167 case DATA_PDU_TYPE_CONTROL:
1168 if (!rdp_recv_server_control_pdu(rdp, cs))
1170 WLog_Print(rdp->log, WLOG_ERROR,
1171 "DATA_PDU_TYPE_CONTROL - rdp_recv_server_control_pdu() failed");
1177 case DATA_PDU_TYPE_POINTER:
1178 if (!update_recv_pointer(rdp->update, cs))
1180 WLog_Print(rdp->log, WLOG_ERROR,
1181 "DATA_PDU_TYPE_POINTER - update_recv_pointer() failed");
1187 case DATA_PDU_TYPE_SYNCHRONIZE:
1188 if (!rdp_recv_server_synchronize_pdu(rdp, cs))
1190 WLog_Print(rdp->log, WLOG_ERROR,
1191 "DATA_PDU_TYPE_SYNCHRONIZE - rdp_recv_synchronize_pdu() failed");
1197 case DATA_PDU_TYPE_PLAY_SOUND:
1198 if (!update_recv_play_sound(rdp->update, cs))
1200 WLog_Print(rdp->log, WLOG_ERROR,
1201 "DATA_PDU_TYPE_PLAY_SOUND - update_recv_play_sound() failed");
1207 case DATA_PDU_TYPE_SHUTDOWN_DENIED:
1208 if (!rdp_recv_server_shutdown_denied_pdu(rdp, cs))
1211 rdp->log, WLOG_ERROR,
1212 "DATA_PDU_TYPE_SHUTDOWN_DENIED - rdp_recv_server_shutdown_denied_pdu() failed");
1218 case DATA_PDU_TYPE_SAVE_SESSION_INFO:
1219 if (!rdp_recv_save_session_info(rdp, cs))
1221 WLog_Print(rdp->log, WLOG_ERROR,
1222 "DATA_PDU_TYPE_SAVE_SESSION_INFO - rdp_recv_save_session_info() failed");
1228 case DATA_PDU_TYPE_FONT_MAP:
1229 if (!rdp_recv_font_map_pdu(rdp, cs))
1231 WLog_Print(rdp->log, WLOG_ERROR,
1232 "DATA_PDU_TYPE_FONT_MAP - rdp_recv_font_map_pdu() failed");
1238 case DATA_PDU_TYPE_SET_KEYBOARD_INDICATORS:
1239 if (!rdp_recv_server_set_keyboard_indicators_pdu(rdp, cs))
1241 WLog_Print(rdp->log, WLOG_ERROR,
1242 "DATA_PDU_TYPE_SET_KEYBOARD_INDICATORS - "
1243 "rdp_recv_server_set_keyboard_indicators_pdu() failed");
1249 case DATA_PDU_TYPE_SET_KEYBOARD_IME_STATUS:
1250 if (!rdp_recv_server_set_keyboard_ime_status_pdu(rdp, cs))
1252 WLog_Print(rdp->log, WLOG_ERROR,
1253 "DATA_PDU_TYPE_SET_KEYBOARD_IME_STATUS - "
1254 "rdp_recv_server_set_keyboard_ime_status_pdu() failed");
1260 case DATA_PDU_TYPE_SET_ERROR_INFO:
1261 if (!rdp_recv_set_error_info_data_pdu(rdp, cs))
1264 rdp->log, WLOG_ERROR,
1265 "DATA_PDU_TYPE_SET_ERROR_INFO - rdp_recv_set_error_info_data_pdu() failed");
1271 case DATA_PDU_TYPE_ARC_STATUS:
1272 if (!rdp_recv_server_auto_reconnect_status_pdu(rdp, cs))
1274 WLog_Print(rdp->log, WLOG_ERROR,
1275 "DATA_PDU_TYPE_ARC_STATUS - "
1276 "rdp_recv_server_auto_reconnect_status_pdu() failed");
1282 case DATA_PDU_TYPE_STATUS_INFO:
1283 if (!rdp_recv_server_status_info_pdu(rdp, cs))
1285 WLog_Print(rdp->log, WLOG_ERROR,
1286 "DATA_PDU_TYPE_STATUS_INFO - rdp_recv_server_status_info_pdu() failed");
1292 case DATA_PDU_TYPE_MONITOR_LAYOUT:
1293 if (!rdp_recv_monitor_layout_pdu(rdp, cs))
1295 WLog_Print(rdp->log, WLOG_ERROR,
1296 "DATA_PDU_TYPE_MONITOR_LAYOUT - rdp_recv_monitor_layout_pdu() failed");
1303 WLog_Print(rdp->log, WLOG_WARN,
1304 "[UNHANDLED] %s Data PDU (0x%02" PRIX8
"), length: %" PRIu16
"",
1305 data_pdu_type_to_string(type), type, length);
1312 return STATE_RUN_SUCCESS;
1318 return STATE_RUN_FAILED;
1321 state_run_t rdp_recv_message_channel_pdu(rdpRdp* rdp,
wStream* s, UINT16 securityFlags)
1326 if (securityFlags & SEC_AUTODETECT_REQ)
1329 return autodetect_recv_request_packet(rdp->autodetect, RDP_TRANSPORT_TCP, s);
1332 if (securityFlags & SEC_AUTODETECT_RSP)
1335 return autodetect_recv_response_packet(rdp->autodetect, RDP_TRANSPORT_TCP, s);
1338 if (securityFlags & SEC_HEARTBEAT)
1341 return rdp_recv_heartbeat_packet(rdp, s);
1344 if (securityFlags & SEC_TRANSPORT_REQ)
1346 return multitransport_recv_request(rdp->multitransport, s);
1349 if (securityFlags & SEC_TRANSPORT_RSP)
1351 return multitransport_recv_response(rdp->multitransport, s);
1354 if (securityFlags & SEC_LICENSE_PKT)
1356 return license_recv(rdp->license, s);
1359 if (securityFlags & SEC_LICENSE_ENCRYPT_CS)
1361 return license_recv(rdp->license, s);
1364 if (securityFlags & SEC_LICENSE_ENCRYPT_SC)
1366 return license_recv(rdp->license, s);
1369 return STATE_RUN_SUCCESS;
1372 state_run_t rdp_recv_out_of_sequence_pdu(rdpRdp* rdp,
wStream* s, UINT16 pduType, UINT16 length)
1374 state_run_t rc = STATE_RUN_FAILED;
1380 rc = rdp_recv_data_pdu(rdp, s);
1382 case PDU_TYPE_SERVER_REDIRECTION:
1383 rc = rdp_recv_enhanced_security_redirection_packet(rdp, s);
1385 case PDU_TYPE_FLOW_RESPONSE:
1386 case PDU_TYPE_FLOW_STOP:
1387 case PDU_TYPE_FLOW_TEST:
1388 rc = STATE_RUN_SUCCESS;
1392 char buffer1[256] = { 0 };
1393 char buffer2[256] = { 0 };
1395 WLog_Print(rdp->log, WLOG_ERROR,
"expected %s, got %s",
1396 pdu_type_to_str(PDU_TYPE_DEMAND_ACTIVE, buffer1,
sizeof(buffer1)),
1397 pdu_type_to_str(pduType, buffer2,
sizeof(buffer2)));
1398 rc = STATE_RUN_FAILED;
1403 if (!tpkt_ensure_stream_consumed(s, length))
1404 return STATE_RUN_FAILED;
1408 BOOL rdp_read_flow_control_pdu(rdpRdp* rdp,
wStream* s, UINT16* type, UINT16* channel_id)
1422 WINPR_ASSERT(channel_id);
1424 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 6))
1426 Stream_Read_UINT8(s, pduType);
1428 Stream_Seek_UINT8(s);
1429 Stream_Seek_UINT8(s);
1430 Stream_Seek_UINT8(s);
1431 Stream_Read_UINT16(s, *channel_id);
1446 BOOL rdp_decrypt(rdpRdp* rdp,
wStream* s, UINT16* pLength, UINT16 securityFlags)
1449 BYTE cmac[8] = { 0 };
1450 BYTE wmac[8] = { 0 };
1451 BOOL status = FALSE;
1455 WINPR_ASSERT(rdp->settings);
1457 WINPR_ASSERT(pLength);
1459 if (!security_lock(rdp))
1463 if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_NONE)
1466 if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
1468 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 12))
1472 Stream_Read_UINT16(s, len);
1474 WLog_Print(rdp->log, WLOG_WARN,
"ENCRYPTION_METHOD_FIPS length %" PRIu16
" != 0x10",
1478 Stream_Read_UINT8(s, version);
1480 WLog_Print(rdp->log, WLOG_WARN,
"ENCRYPTION_METHOD_FIPS version %" PRIu16
" != 1",
1484 Stream_Read_UINT8(s, pad);
1485 const BYTE* sig = Stream_ConstPointer(s);
1488 const INT32 padLength = length - pad;
1490 if ((length <= 0) || (padLength <= 0) || (padLength > UINT16_MAX))
1492 WLog_Print(rdp->log, WLOG_ERROR,
"FATAL: invalid pad length %" PRId32, padLength);
1496 if (!security_fips_decrypt(Stream_Pointer(s), length, rdp))
1499 if (!security_fips_check_signature(Stream_ConstPointer(s), length - pad, sig, 8, rdp))
1502 Stream_SetLength(s, Stream_Length(s) - pad);
1503 *pLength = (UINT16)padLength;
1507 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s,
sizeof(wmac)))
1510 Stream_Read(s, wmac,
sizeof(wmac));
1511 length -=
sizeof(wmac);
1515 WLog_Print(rdp->log, WLOG_ERROR,
"FATAL: invalid length field");
1519 if (!security_decrypt(Stream_PointerAs(s, BYTE), length, rdp))
1522 if (securityFlags & SEC_SECURE_CHECKSUM)
1523 status = security_salted_mac_signature(rdp, Stream_ConstPointer(s), length, FALSE, cmac,
1527 security_mac_signature(rdp, Stream_ConstPointer(s), length, cmac,
sizeof(cmac));
1532 if (memcmp(wmac, cmac,
sizeof(wmac)) != 0)
1534 WLog_Print(rdp->log, WLOG_ERROR,
"WARNING: invalid packet signature");
1549 if (!security_unlock(rdp))
1554 const char* pdu_type_to_str(UINT16 pduType,
char* buffer,
size_t length)
1556 const char* str = NULL;
1559 case PDU_TYPE_DEMAND_ACTIVE:
1560 str =
"PDU_TYPE_DEMAND_ACTIVE";
1562 case PDU_TYPE_CONFIRM_ACTIVE:
1563 str =
"PDU_TYPE_CONFIRM_ACTIVE";
1565 case PDU_TYPE_DEACTIVATE_ALL:
1566 str =
"PDU_TYPE_DEACTIVATE_ALL";
1569 str =
"PDU_TYPE_DATA";
1571 case PDU_TYPE_SERVER_REDIRECTION:
1572 str =
"PDU_TYPE_SERVER_REDIRECTION";
1574 case PDU_TYPE_FLOW_TEST:
1575 str =
"PDU_TYPE_FLOW_TEST";
1577 case PDU_TYPE_FLOW_RESPONSE:
1578 str =
"PDU_TYPE_FLOW_RESPONSE";
1580 case PDU_TYPE_FLOW_STOP:
1581 str =
"PDU_TYPE_FLOW_STOP";
1584 str =
"PDU_TYPE_UNKNOWN";
1588 winpr_str_append(str, buffer, length,
"");
1590 char msg[32] = { 0 };
1591 (void)_snprintf(msg,
sizeof(msg),
"[0x%08" PRIx32
"]", pduType);
1592 winpr_str_append(msg, buffer, length,
"");
1603 static state_run_t rdp_recv_tpkt_pdu(rdpRdp* rdp,
wStream* s)
1605 state_run_t rc = STATE_RUN_SUCCESS;
1608 UINT16 pduSource = 0;
1609 UINT16 channelId = 0;
1610 UINT16 securityFlags = 0;
1611 freerdp* instance = NULL;
1614 WINPR_ASSERT(rdp->context);
1617 instance = rdp->context->instance;
1618 WINPR_ASSERT(instance);
1620 if (!rdp_read_header(rdp, s, &length, &channelId))
1621 return STATE_RUN_FAILED;
1623 if (freerdp_shall_disconnect_context(rdp->context))
1624 return STATE_RUN_SUCCESS;
1626 if (rdp->autodetect->bandwidthMeasureStarted)
1628 rdp->autodetect->bandwidthMeasureByteCount += length;
1631 if (rdp->mcs->messageChannelId && (channelId == rdp->mcs->messageChannelId))
1634 if (!rdp_handle_message_channel(rdp, s, channelId, length))
1635 return STATE_RUN_FAILED;
1636 return STATE_RUN_SUCCESS;
1639 if (rdp->settings->UseRdpSecurityLayer)
1641 if (!rdp_read_security_header(rdp, s, &securityFlags, &length))
1642 return STATE_RUN_FAILED;
1644 if (securityFlags & (SEC_ENCRYPT | SEC_REDIRECTION_PKT))
1646 if (!rdp_decrypt(rdp, s, &length, securityFlags))
1647 return STATE_RUN_FAILED;
1650 if (securityFlags & SEC_REDIRECTION_PKT)
1656 Stream_Rewind(s, 2);
1659 rc = rdp_recv_enhanced_security_redirection_packet(rdp, s);
1664 if (channelId == MCS_GLOBAL_CHANNEL_ID)
1666 while (Stream_GetRemainingLength(s) > 3)
1673 if (!rdp_read_share_control_header(rdp, s, NULL, &remain, &pduType, &pduSource))
1674 return STATE_RUN_FAILED;
1676 sub = Stream_StaticInit(&subbuffer, Stream_Pointer(s), remain);
1677 if (!Stream_SafeSeek(s, remain))
1678 return STATE_RUN_FAILED;
1680 rdp->settings->PduSource = pduSource;
1686 rc = rdp_recv_data_pdu(rdp, sub);
1687 if (state_run_failed(rc))
1691 case PDU_TYPE_DEACTIVATE_ALL:
1692 if (!rdp_recv_deactivate_all(rdp, sub))
1694 WLog_Print(rdp->log, WLOG_ERROR,
1695 "rdp_recv_tpkt_pdu: rdp_recv_deactivate_all() fail");
1696 return STATE_RUN_FAILED;
1701 case PDU_TYPE_SERVER_REDIRECTION:
1702 return rdp_recv_enhanced_security_redirection_packet(rdp, sub);
1704 case PDU_TYPE_FLOW_RESPONSE:
1705 case PDU_TYPE_FLOW_STOP:
1706 case PDU_TYPE_FLOW_TEST:
1707 WLog_Print(rdp->log, WLOG_DEBUG,
"flow message 0x%04" PRIX16
"", pduType);
1709 if (!Stream_SafeSeek(sub, remain))
1710 return STATE_RUN_FAILED;
1715 char buffer[256] = { 0 };
1716 WLog_Print(rdp->log, WLOG_ERROR,
"incorrect PDU type: %s",
1717 pdu_type_to_str(pduType, buffer,
sizeof(buffer)));
1722 diff = Stream_GetRemainingLength(sub);
1725 char buffer[256] = { 0 };
1726 WLog_Print(rdp->log, WLOG_WARN,
1727 "pduType %s not properly parsed, %" PRIdz
1728 " bytes remaining unhandled. Skipping.",
1729 pdu_type_to_str(pduType, buffer,
sizeof(buffer)), diff);
1737 if (!freerdp_channel_process(instance, s, channelId, length))
1738 return STATE_RUN_FAILED;
1742 if (!tpkt_ensure_stream_consumed(s, length))
1743 return STATE_RUN_FAILED;
1747 static state_run_t rdp_recv_fastpath_pdu(rdpRdp* rdp,
wStream* s)
1752 rdpFastPath* fastpath = rdp->fastpath;
1754 if (!fastpath_read_header_rdp(fastpath, s, &length))
1756 WLog_Print(rdp->log, WLOG_ERROR,
"rdp_recv_fastpath_pdu: fastpath_read_header_rdp() fail");
1757 return STATE_RUN_FAILED;
1760 if ((length == 0) || (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, length)))
1762 WLog_Print(rdp->log, WLOG_ERROR,
"incorrect FastPath PDU header length %" PRIu16
"",
1764 return STATE_RUN_FAILED;
1767 if (rdp->autodetect->bandwidthMeasureStarted)
1769 rdp->autodetect->bandwidthMeasureByteCount += length;
1772 if (!fastpath_decrypt(fastpath, s, &length))
1773 return STATE_RUN_FAILED;
1775 return fastpath_recv_updates(rdp->fastpath, s);
1778 static state_run_t rdp_recv_pdu(rdpRdp* rdp,
wStream* s)
1780 const int rc = tpkt_verify_header(s);
1782 return rdp_recv_tpkt_pdu(rdp, s);
1784 return rdp_recv_fastpath_pdu(rdp, s);
1786 return STATE_RUN_FAILED;
1789 static state_run_t rdp_handle_sc_flags(rdpRdp* rdp,
wStream* s, UINT32 flag,
1790 CONNECTION_STATE nextState)
1792 const UINT32 mask = FINALIZE_SC_SYNCHRONIZE_PDU | FINALIZE_SC_CONTROL_COOPERATE_PDU |
1793 FINALIZE_SC_CONTROL_GRANTED_PDU | FINALIZE_SC_FONT_MAP_PDU;
1795 state_run_t status = rdp_recv_pdu(rdp, s);
1796 if (state_run_success(status))
1798 const UINT32 flags = rdp->finalize_sc_pdus & mask;
1799 if ((flags & flag) == flag)
1801 if (!rdp_client_transition_to_state(rdp, nextState))
1802 status = STATE_RUN_FAILED;
1804 status = STATE_RUN_SUCCESS;
1808 char flag_buffer[256] = { 0 };
1809 char mask_buffer[256] = { 0 };
1810 WLog_Print(rdp->log, WLOG_WARN,
1811 "[%s] unexpected server message, expected flag %s [have %s]",
1812 rdp_get_state_string(rdp),
1813 rdp_finalize_flags_to_str(flag, flag_buffer,
sizeof(flag_buffer)),
1814 rdp_finalize_flags_to_str(flags, mask_buffer,
sizeof(mask_buffer)));
1820 static state_run_t rdp_client_exchange_monitor_layout(rdpRdp* rdp,
wStream* s)
1824 if (!rdp_check_monitor_layout_pdu_state(rdp, FALSE))
1830 const UINT32 old = rdp->finalize_sc_pdus;
1831 state_run_t status = rdp_recv_pdu(rdp, s);
1832 const UINT32 now = rdp->finalize_sc_pdus;
1833 const BOOL changed = (old != now) || rdp->monitor_layout_pdu;
1836 if (state_run_success(status) && changed)
1838 if (!rdp->monitor_layout_pdu)
1840 if (!rdp_finalize_is_flag_set(rdp, FINALIZE_SC_SYNCHRONIZE_PDU))
1841 return STATE_RUN_FAILED;
1844 status = rdp_client_connect_finalize(rdp);
1845 if (state_run_success(status) && !rdp->monitor_layout_pdu)
1846 status = STATE_RUN_TRY_AGAIN;
1851 static state_run_t rdp_recv_callback_int(rdpTransport* transport,
wStream* s,
void* extra)
1853 state_run_t status = STATE_RUN_SUCCESS;
1854 rdpRdp* rdp = (rdpRdp*)extra;
1856 WINPR_ASSERT(transport);
1860 switch (rdp_get_state(rdp))
1862 case CONNECTION_STATE_NEGO:
1863 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_CREATE_REQUEST))
1864 status = STATE_RUN_FAILED;
1866 status = STATE_RUN_CONTINUE;
1868 case CONNECTION_STATE_NLA:
1869 if (nla_get_state(rdp->nla) < NLA_STATE_AUTH_INFO)
1871 if (nla_recv_pdu(rdp->nla, s) < 1)
1873 WLog_Print(rdp->log, WLOG_ERROR,
"%s - nla_recv_pdu() fail",
1874 rdp_get_state_string(rdp));
1875 status = STATE_RUN_FAILED;
1878 else if (nla_get_state(rdp->nla) == NLA_STATE_POST_NEGO)
1880 nego_recv(rdp->transport, s, (
void*)rdp->nego);
1882 if (!nego_update_settings_from_state(rdp->nego, rdp->settings))
1885 if (nego_get_state(rdp->nego) != NEGO_STATE_FINAL)
1887 WLog_Print(rdp->log, WLOG_ERROR,
"%s - nego_recv() fail",
1888 rdp_get_state_string(rdp));
1889 status = STATE_RUN_FAILED;
1891 else if (!nla_set_state(rdp->nla, NLA_STATE_FINAL))
1892 status = STATE_RUN_FAILED;
1895 if (state_run_success(status))
1897 if (nla_get_state(rdp->nla) == NLA_STATE_AUTH_INFO)
1899 transport_set_nla_mode(rdp->transport, FALSE);
1901 if (rdp->settings->VmConnectMode)
1903 if (!nego_set_state(rdp->nego, NEGO_STATE_NLA))
1904 status = STATE_RUN_FAILED;
1905 else if (!nego_set_requested_protocols(rdp->nego,
1906 PROTOCOL_HYBRID | PROTOCOL_SSL))
1907 status = STATE_RUN_FAILED;
1908 else if (!nego_send_negotiation_request(rdp->nego))
1909 status = STATE_RUN_FAILED;
1910 else if (!nla_set_state(rdp->nla, NLA_STATE_POST_NEGO))
1911 status = STATE_RUN_FAILED;
1915 if (!nla_set_state(rdp->nla, NLA_STATE_FINAL))
1916 status = STATE_RUN_FAILED;
1920 if (state_run_success(status))
1923 if (nla_get_state(rdp->nla) == NLA_STATE_FINAL)
1925 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_CREATE_REQUEST))
1926 status = STATE_RUN_FAILED;
1928 status = STATE_RUN_CONTINUE;
1933 case CONNECTION_STATE_AAD:
1934 if (aad_recv(rdp->aad, s) < 1)
1936 WLog_Print(rdp->log, WLOG_ERROR,
"%s - aad_recv() fail", rdp_get_state_string(rdp));
1937 status = STATE_RUN_FAILED;
1939 if (state_run_success(status))
1941 if (aad_get_state(rdp->aad) == AAD_STATE_FINAL)
1943 transport_set_aad_mode(rdp->transport, FALSE);
1944 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_CREATE_REQUEST))
1945 status = STATE_RUN_FAILED;
1947 status = STATE_RUN_CONTINUE;
1952 case CONNECTION_STATE_MCS_CREATE_REQUEST:
1953 if (!mcs_client_begin(rdp->mcs))
1955 WLog_Print(rdp->log, WLOG_ERROR,
"%s - mcs_client_begin() fail",
1956 rdp_get_state_string(rdp));
1957 status = STATE_RUN_FAILED;
1959 else if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_CREATE_RESPONSE))
1960 status = STATE_RUN_FAILED;
1961 else if (Stream_GetRemainingLength(s) > 0)
1962 status = STATE_RUN_CONTINUE;
1965 case CONNECTION_STATE_MCS_CREATE_RESPONSE:
1966 if (!mcs_recv_connect_response(rdp->mcs, s))
1968 WLog_Print(rdp->log, WLOG_ERROR,
"mcs_recv_connect_response failure");
1969 status = STATE_RUN_FAILED;
1973 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_ERECT_DOMAIN))
1974 status = STATE_RUN_FAILED;
1975 else if (!mcs_send_erect_domain_request(rdp->mcs))
1977 WLog_Print(rdp->log, WLOG_ERROR,
"mcs_send_erect_domain_request failure");
1978 status = STATE_RUN_FAILED;
1980 else if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_ATTACH_USER))
1981 status = STATE_RUN_FAILED;
1982 else if (!mcs_send_attach_user_request(rdp->mcs))
1984 WLog_Print(rdp->log, WLOG_ERROR,
"mcs_send_attach_user_request failure");
1985 status = STATE_RUN_FAILED;
1987 else if (!rdp_client_transition_to_state(rdp,
1988 CONNECTION_STATE_MCS_ATTACH_USER_CONFIRM))
1989 status = STATE_RUN_FAILED;
1993 case CONNECTION_STATE_MCS_ATTACH_USER_CONFIRM:
1994 if (!mcs_recv_attach_user_confirm(rdp->mcs, s))
1996 WLog_Print(rdp->log, WLOG_ERROR,
"mcs_recv_attach_user_confirm failure");
1997 status = STATE_RUN_FAILED;
2001 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_MCS_CHANNEL_JOIN_REQUEST))
2002 status = STATE_RUN_FAILED;
2003 else if (!mcs_send_channel_join_request(rdp->mcs, rdp->mcs->userId))
2005 WLog_Print(rdp->log, WLOG_ERROR,
"mcs_send_channel_join_request failure");
2006 status = STATE_RUN_FAILED;
2008 else if (!rdp_client_transition_to_state(
2009 rdp, CONNECTION_STATE_MCS_CHANNEL_JOIN_RESPONSE))
2010 status = STATE_RUN_FAILED;
2015 if (!rdp_client_skip_mcs_channel_join(rdp))
2016 status = STATE_RUN_FAILED;
2020 case CONNECTION_STATE_MCS_CHANNEL_JOIN_RESPONSE:
2021 if (!rdp_client_connect_mcs_channel_join_confirm(rdp, s))
2023 WLog_Print(rdp->log, WLOG_ERROR,
2025 "rdp_client_connect_mcs_channel_join_confirm() fail",
2026 rdp_get_state_string(rdp));
2027 status = STATE_RUN_FAILED;
2032 case CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT_REQUEST:
2033 if (!rdp_client_connect_auto_detect(rdp, s))
2035 if (!rdp_client_transition_to_state(rdp, CONNECTION_STATE_LICENSING))
2036 status = STATE_RUN_FAILED;
2038 status = STATE_RUN_TRY_AGAIN;
2042 case CONNECTION_STATE_LICENSING:
2043 status = rdp_client_connect_license(rdp, s);
2045 if (state_run_failed(status))
2047 char buffer[64] = { 0 };
2048 WLog_Print(rdp->log, WLOG_DEBUG,
"%s - rdp_client_connect_license() - %s",
2049 rdp_get_state_string(rdp),
2050 state_run_result_string(status, buffer, ARRAYSIZE(buffer)));
2055 case CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING_REQUEST:
2056 if (!rdp_client_connect_auto_detect(rdp, s))
2058 (void)rdp_client_transition_to_state(
2059 rdp, CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE);
2060 status = STATE_RUN_TRY_AGAIN;
2064 case CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE:
2065 status = rdp_client_connect_demand_active(rdp, s);
2067 if (state_run_failed(status))
2069 char buffer[64] = { 0 };
2070 WLog_Print(rdp->log, WLOG_DEBUG,
2072 "rdp_client_connect_demand_active() - %s",
2073 rdp_get_state_string(rdp),
2074 state_run_result_string(status, buffer, ARRAYSIZE(buffer)));
2076 else if (status == STATE_RUN_ACTIVE)
2078 if (!rdp_client_transition_to_state(
2079 rdp, CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE))
2080 status = STATE_RUN_FAILED;
2082 status = STATE_RUN_CONTINUE;
2086 case CONNECTION_STATE_CAPABILITIES_EXCHANGE_MONITOR_LAYOUT:
2087 status = rdp_client_exchange_monitor_layout(rdp, s);
2090 case CONNECTION_STATE_CAPABILITIES_EXCHANGE_CONFIRM_ACTIVE:
2091 status = rdp_client_connect_confirm_active(rdp, s);
2094 case CONNECTION_STATE_FINALIZATION_CLIENT_SYNC:
2095 status = rdp_handle_sc_flags(rdp, s, FINALIZE_SC_SYNCHRONIZE_PDU,
2096 CONNECTION_STATE_FINALIZATION_CLIENT_COOPERATE);
2098 case CONNECTION_STATE_FINALIZATION_CLIENT_COOPERATE:
2099 status = rdp_handle_sc_flags(rdp, s, FINALIZE_SC_CONTROL_COOPERATE_PDU,
2100 CONNECTION_STATE_FINALIZATION_CLIENT_GRANTED_CONTROL);
2102 case CONNECTION_STATE_FINALIZATION_CLIENT_GRANTED_CONTROL:
2103 status = rdp_handle_sc_flags(rdp, s, FINALIZE_SC_CONTROL_GRANTED_PDU,
2104 CONNECTION_STATE_FINALIZATION_CLIENT_FONT_MAP);
2106 case CONNECTION_STATE_FINALIZATION_CLIENT_FONT_MAP:
2107 status = rdp_handle_sc_flags(rdp, s, FINALIZE_SC_FONT_MAP_PDU, CONNECTION_STATE_ACTIVE);
2110 case CONNECTION_STATE_ACTIVE:
2111 status = rdp_recv_pdu(rdp, s);
2113 if (state_run_failed(status))
2115 char buffer[64] = { 0 };
2116 WLog_Print(rdp->log, WLOG_DEBUG,
"%s - rdp_recv_pdu() - %s",
2117 rdp_get_state_string(rdp),
2118 state_run_result_string(status, buffer, ARRAYSIZE(buffer)));
2123 WLog_Print(rdp->log, WLOG_ERROR,
"%s state %d", rdp_get_state_string(rdp),
2124 rdp_get_state(rdp));
2125 status = STATE_RUN_FAILED;
2129 if (state_run_failed(status))
2131 char buffer[64] = { 0 };
2132 WLog_Print(rdp->log, WLOG_ERROR,
"%s status %s", rdp_get_state_string(rdp),
2133 state_run_result_string(status, buffer, ARRAYSIZE(buffer)));
2138 state_run_t rdp_recv_callback(rdpTransport* transport,
wStream* s,
void* extra)
2140 char buffer[64] = { 0 };
2141 state_run_t rc = STATE_RUN_FAILED;
2142 const size_t start = Stream_GetPosition(s);
2143 const rdpContext* context = transport_get_context(transport);
2145 WINPR_ASSERT(context);
2148 const rdpRdp* rdp = context->rdp;
2151 if (rc == STATE_RUN_TRY_AGAIN)
2152 Stream_SetPosition(s, start);
2154 const char* old = rdp_get_state_string(rdp);
2155 const size_t orem = Stream_GetRemainingLength(s);
2156 rc = rdp_recv_callback_int(transport, s, extra);
2158 const char* now = rdp_get_state_string(rdp);
2159 const size_t rem = Stream_GetRemainingLength(s);
2161 WLog_Print(rdp->log, WLOG_TRACE,
2162 "(client)[%s -> %s] current return %s [feeding %" PRIuz
" bytes, %" PRIuz
2163 " bytes not processed]",
2164 old, now, state_run_result_string(rc, buffer,
sizeof(buffer)), orem, rem);
2165 }
while ((rc == STATE_RUN_TRY_AGAIN) || (rc == STATE_RUN_CONTINUE));
2169 BOOL rdp_send_channel_data(rdpRdp* rdp, UINT16 channelId,
const BYTE* data,
size_t size)
2171 return freerdp_channel_send(rdp, channelId, data, size);
2174 BOOL rdp_channel_send_packet(rdpRdp* rdp, UINT16 channelId,
size_t totalSize, UINT32 flags,
2175 const BYTE* data,
size_t chunkSize)
2177 return freerdp_channel_send_packet(rdp, channelId, totalSize, flags, data, chunkSize);
2180 BOOL rdp_send_error_info(rdpRdp* rdp)
2185 if (rdp->errorInfo == ERRINFO_SUCCESS)
2188 s = rdp_data_pdu_init(rdp);
2193 Stream_Write_UINT32(s, rdp->errorInfo);
2194 status = rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_SET_ERROR_INFO, 0);
2198 int rdp_check_fds(rdpRdp* rdp)
2202 rdpTransport* transport = NULL;
2205 transport = rdp->transport;
2207 tsg = transport_get_tsg(transport);
2210 if (!tsg_check_event_handles(tsg))
2212 WLog_Print(rdp->log, WLOG_ERROR,
"rdp_check_fds: tsg_check_event_handles()");
2216 if (tsg_get_state(tsg) != TSG_STATE_PIPE_CREATED)
2220 status = transport_check_fds(transport);
2224 if (!rdp_client_redirect(rdp))
2229 WLog_Print(rdp->log, WLOG_DEBUG,
"transport_check_fds() - %i", status);
2234 BOOL freerdp_get_stats(rdpRdp* rdp, UINT64* inBytes, UINT64* outBytes, UINT64* inPackets,
2241 *inBytes = rdp->inBytes;
2243 *outBytes = rdp->outBytes;
2245 *inPackets = rdp->inPackets;
2247 *outPackets = rdp->outPackets;
2257 rdpRdp* rdp_new(rdpContext* context)
2261 rdp = (rdpRdp*)calloc(1,
sizeof(rdpRdp));
2266 rdp->log = WLog_Get(RDP_TAG);
2267 WINPR_ASSERT(rdp->log);
2269 (void)_snprintf(rdp->log_context,
sizeof(rdp->log_context),
"%p", (
void*)context);
2270 WLog_SetContext(rdp->log, NULL, rdp->log_context);
2272 InitializeCriticalSection(&rdp->critical);
2273 rdp->context = context;
2274 WINPR_ASSERT(rdp->context);
2276 if (context->ServerMode)
2279 if (!context->settings)
2287 rdp->settings = context->settings;
2290 if (!rdp_set_backup_settings(rdp))
2293 rdp->settings->instance = context->instance;
2295 context->settings = rdp->settings;
2296 if (context->instance)
2297 context->settings->instance = context->instance;
2298 else if (context->peer)
2300 rdp->settings->instance = context->peer;
2302 #if defined(WITH_FREERDP_DEPRECATED)
2303 context->peer->settings = rdp->settings;
2307 rdp->transport = transport_new(context);
2309 if (!rdp->transport)
2313 const rdpTransportIo* io = transport_get_io_callbacks(rdp->transport);
2316 rdp->io = calloc(1,
sizeof(rdpTransportIo));
2322 rdp->aad = aad_new(context, rdp->transport);
2326 rdp->license = license_new(rdp);
2331 rdp->input = input_new(rdp);
2336 rdp->update = update_new(rdp);
2341 rdp->fastpath = fastpath_new(rdp);
2346 rdp->nego = nego_new(rdp->transport);
2351 rdp->mcs = mcs_new(rdp->transport);
2356 rdp->redirection = redirection_new();
2358 if (!rdp->redirection)
2361 rdp->autodetect = autodetect_new(rdp->context);
2363 if (!rdp->autodetect)
2366 rdp->heartbeat = heartbeat_new();
2368 if (!rdp->heartbeat)
2371 rdp->multitransport = multitransport_new(rdp, INITIATE_REQUEST_PROTOCOL_UDPFECL |
2372 INITIATE_REQUEST_PROTOCOL_UDPFECR);
2374 if (!rdp->multitransport)
2377 rdp->bulk = bulk_new(context);
2382 rdp->pubSub = PubSub_New(TRUE);
2386 rdp->abortEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
2387 if (!rdp->abortEvent)
2392 WINPR_PRAGMA_DIAG_PUSH
2393 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2395 WINPR_PRAGMA_DIAG_POP
2399 static void rdp_reset_free(rdpRdp* rdp)
2403 rdp_free_rc4_decrypt_keys(rdp);
2404 rdp_free_rc4_encrypt_keys(rdp);
2406 winpr_Cipher_Free(rdp->fips_encrypt);
2407 winpr_Cipher_Free(rdp->fips_decrypt);
2408 rdp->fips_encrypt = NULL;
2409 rdp->fips_decrypt = NULL;
2412 nego_free(rdp->nego);
2413 license_free(rdp->license);
2414 transport_free(rdp->transport);
2415 fastpath_free(rdp->fastpath);
2419 rdp->license = NULL;
2420 rdp->transport = NULL;
2421 rdp->fastpath = NULL;
2424 BOOL rdp_reset(rdpRdp* rdp)
2427 rdpContext* context = NULL;
2428 rdpSettings* settings = NULL;
2432 context = rdp->context;
2433 WINPR_ASSERT(context);
2435 settings = rdp->settings;
2436 WINPR_ASSERT(settings);
2438 bulk_reset(rdp->bulk);
2440 rdp_reset_free(rdp);
2455 rdp->transport = transport_new(context);
2456 if (!rdp->transport)
2461 if (!transport_set_io_callbacks(rdp->transport, rdp->io))
2466 rdp->aad = aad_new(context, rdp->transport);
2470 rdp->nego = nego_new(rdp->transport);
2474 rdp->mcs = mcs_new(rdp->transport);
2478 if (!transport_set_layer(rdp->transport, TRANSPORT_LAYER_TCP))
2481 rdp->license = license_new(rdp);
2485 rdp->fastpath = fastpath_new(rdp);
2490 rc = rdp_finalize_reset_flags(rdp, TRUE);
2501 void rdp_free(rdpRdp* rdp)
2505 DeleteCriticalSection(&rdp->critical);
2506 rdp_reset_free(rdp);
2512 input_free(rdp->input);
2513 update_free(rdp->update);
2515 redirection_free(rdp->redirection);
2516 autodetect_free(rdp->autodetect);
2517 heartbeat_free(rdp->heartbeat);
2518 multitransport_free(rdp->multitransport);
2519 bulk_free(rdp->bulk);
2521 PubSub_Free(rdp->pubSub);
2522 if (rdp->abortEvent)
2523 (void)CloseHandle(rdp->abortEvent);
2529 BOOL rdp_io_callback_set_event(rdpRdp* rdp, BOOL set)
2533 return transport_io_callback_set_event(rdp->transport, set);
2536 const rdpTransportIo* rdp_get_io_callbacks(rdpRdp* rdp)
2543 BOOL rdp_set_io_callbacks(rdpRdp* rdp,
const rdpTransportIo* io_callbacks)
2551 rdp->io = malloc(
sizeof(rdpTransportIo));
2554 *rdp->io = *io_callbacks;
2555 return transport_set_io_callbacks(rdp->transport, rdp->io);
2560 BOOL rdp_set_io_callback_context(rdpRdp* rdp,
void* usercontext)
2563 rdp->ioContext = usercontext;
2567 void* rdp_get_io_callback_context(rdpRdp* rdp)
2570 return rdp->ioContext;
2573 const char* rdp_finalize_flags_to_str(UINT32 flags,
char* buffer,
size_t size)
2575 char number[32] = { 0 };
2576 const UINT32 mask = ~(FINALIZE_SC_SYNCHRONIZE_PDU | FINALIZE_SC_CONTROL_COOPERATE_PDU |
2577 FINALIZE_SC_CONTROL_GRANTED_PDU | FINALIZE_SC_FONT_MAP_PDU |
2578 FINALIZE_CS_SYNCHRONIZE_PDU | FINALIZE_CS_CONTROL_COOPERATE_PDU |
2579 FINALIZE_CS_CONTROL_REQUEST_PDU | FINALIZE_CS_PERSISTENT_KEY_LIST_PDU |
2580 FINALIZE_CS_FONT_LIST_PDU | FINALIZE_DEACTIVATE_REACTIVATE);
2582 if (flags & FINALIZE_SC_SYNCHRONIZE_PDU)
2583 winpr_str_append(
"FINALIZE_SC_SYNCHRONIZE_PDU", buffer, size,
"|");
2584 if (flags & FINALIZE_SC_CONTROL_COOPERATE_PDU)
2585 winpr_str_append(
"FINALIZE_SC_CONTROL_COOPERATE_PDU", buffer, size,
"|");
2586 if (flags & FINALIZE_SC_CONTROL_GRANTED_PDU)
2587 winpr_str_append(
"FINALIZE_SC_CONTROL_GRANTED_PDU", buffer, size,
"|");
2588 if (flags & FINALIZE_SC_FONT_MAP_PDU)
2589 winpr_str_append(
"FINALIZE_SC_FONT_MAP_PDU", buffer, size,
"|");
2590 if (flags & FINALIZE_CS_SYNCHRONIZE_PDU)
2591 winpr_str_append(
"FINALIZE_CS_SYNCHRONIZE_PDU", buffer, size,
"|");
2592 if (flags & FINALIZE_CS_CONTROL_COOPERATE_PDU)
2593 winpr_str_append(
"FINALIZE_CS_CONTROL_COOPERATE_PDU", buffer, size,
"|");
2594 if (flags & FINALIZE_CS_CONTROL_REQUEST_PDU)
2595 winpr_str_append(
"FINALIZE_CS_CONTROL_REQUEST_PDU", buffer, size,
"|");
2596 if (flags & FINALIZE_CS_PERSISTENT_KEY_LIST_PDU)
2597 winpr_str_append(
"FINALIZE_CS_PERSISTENT_KEY_LIST_PDU", buffer, size,
"|");
2598 if (flags & FINALIZE_CS_FONT_LIST_PDU)
2599 winpr_str_append(
"FINALIZE_CS_FONT_LIST_PDU", buffer, size,
"|");
2600 if (flags & FINALIZE_DEACTIVATE_REACTIVATE)
2601 winpr_str_append(
"FINALIZE_DEACTIVATE_REACTIVATE", buffer, size,
"|");
2603 winpr_str_append(
"UNKNOWN_FLAG", buffer, size,
"|");
2605 winpr_str_append(
"NO_FLAG_SET", buffer, size,
"|");
2606 (void)_snprintf(number,
sizeof(number),
" [0x%08" PRIx32
"]", flags);
2607 winpr_str_append(number, buffer, size,
"");
2611 BOOL rdp_finalize_reset_flags(rdpRdp* rdp, BOOL clearAll)
2614 WLog_Print(rdp->log, WLOG_DEBUG,
"[%s] reset finalize_sc_pdus", rdp_get_state_string(rdp));
2616 rdp->finalize_sc_pdus = 0;
2618 rdp->finalize_sc_pdus &= FINALIZE_DEACTIVATE_REACTIVATE;
2620 return rdp_set_monitor_layout_pdu_state(rdp, FALSE);
2623 BOOL rdp_finalize_set_flag(rdpRdp* rdp, UINT32 flag)
2625 char buffer[1024] = { 0 };
2629 WLog_Print(rdp->log, WLOG_DEBUG,
"[%s] received flag %s", rdp_get_state_string(rdp),
2630 rdp_finalize_flags_to_str(flag, buffer,
sizeof(buffer)));
2631 rdp->finalize_sc_pdus |= flag;
2635 BOOL rdp_finalize_is_flag_set(rdpRdp* rdp, UINT32 flag)
2638 return (rdp->finalize_sc_pdus & flag) == flag;
2641 BOOL rdp_reset_rc4_encrypt_keys(rdpRdp* rdp)
2644 rdp_free_rc4_encrypt_keys(rdp);
2645 rdp->rc4_encrypt_key = winpr_RC4_New(rdp->encrypt_key, rdp->rc4_key_len);
2647 rdp->encrypt_use_count = 0;
2648 return rdp->rc4_encrypt_key != NULL;
2651 void rdp_free_rc4_encrypt_keys(rdpRdp* rdp)
2654 winpr_RC4_Free(rdp->rc4_encrypt_key);
2655 rdp->rc4_encrypt_key = NULL;
2658 void rdp_free_rc4_decrypt_keys(rdpRdp* rdp)
2661 winpr_RC4_Free(rdp->rc4_decrypt_key);
2662 rdp->rc4_decrypt_key = NULL;
2665 BOOL rdp_reset_rc4_decrypt_keys(rdpRdp* rdp)
2668 rdp_free_rc4_decrypt_keys(rdp);
2669 rdp->rc4_decrypt_key = winpr_RC4_New(rdp->decrypt_key, rdp->rc4_key_len);
2671 rdp->decrypt_use_count = 0;
2672 return rdp->rc4_decrypt_key != NULL;
2675 const char* rdp_security_flag_string(UINT32 securityFlags,
char* buffer,
size_t size)
2677 if (securityFlags & SEC_EXCHANGE_PKT)
2678 winpr_str_append(
"SEC_EXCHANGE_PKT", buffer, size,
"|");
2679 if (securityFlags & SEC_TRANSPORT_REQ)
2680 winpr_str_append(
"SEC_TRANSPORT_REQ", buffer, size,
"|");
2681 if (securityFlags & SEC_TRANSPORT_RSP)
2682 winpr_str_append(
"SEC_TRANSPORT_RSP", buffer, size,
"|");
2683 if (securityFlags & SEC_ENCRYPT)
2684 winpr_str_append(
"SEC_ENCRYPT", buffer, size,
"|");
2685 if (securityFlags & SEC_RESET_SEQNO)
2686 winpr_str_append(
"SEC_RESET_SEQNO", buffer, size,
"|");
2687 if (securityFlags & SEC_IGNORE_SEQNO)
2688 winpr_str_append(
"SEC_IGNORE_SEQNO", buffer, size,
"|");
2689 if (securityFlags & SEC_INFO_PKT)
2690 winpr_str_append(
"SEC_INFO_PKT", buffer, size,
"|");
2691 if (securityFlags & SEC_LICENSE_PKT)
2692 winpr_str_append(
"SEC_LICENSE_PKT", buffer, size,
"|");
2693 if (securityFlags & SEC_LICENSE_ENCRYPT_CS)
2694 winpr_str_append(
"SEC_LICENSE_ENCRYPT_CS", buffer, size,
"|");
2695 if (securityFlags & SEC_LICENSE_ENCRYPT_SC)
2696 winpr_str_append(
"SEC_LICENSE_ENCRYPT_SC", buffer, size,
"|");
2697 if (securityFlags & SEC_REDIRECTION_PKT)
2698 winpr_str_append(
"SEC_REDIRECTION_PKT", buffer, size,
"|");
2699 if (securityFlags & SEC_SECURE_CHECKSUM)
2700 winpr_str_append(
"SEC_SECURE_CHECKSUM", buffer, size,
"|");
2701 if (securityFlags & SEC_AUTODETECT_REQ)
2702 winpr_str_append(
"SEC_AUTODETECT_REQ", buffer, size,
"|");
2703 if (securityFlags & SEC_AUTODETECT_RSP)
2704 winpr_str_append(
"SEC_AUTODETECT_RSP", buffer, size,
"|");
2705 if (securityFlags & SEC_HEARTBEAT)
2706 winpr_str_append(
"SEC_HEARTBEAT", buffer, size,
"|");
2707 if (securityFlags & SEC_FLAGSHI_VALID)
2708 winpr_str_append(
"SEC_FLAGSHI_VALID", buffer, size,
"|");
2710 char msg[32] = { 0 };
2712 (void)_snprintf(msg,
sizeof(msg),
"[0x%08" PRIx32
"]", securityFlags);
2713 winpr_str_append(msg, buffer, size,
"");
2718 static BOOL rdp_reset_remote_settings(rdpRdp* rdp)
2720 UINT32 flags = FREERDP_SETTINGS_REMOTE_MODE;
2727 return rdp->remoteSettings != NULL;
2730 BOOL rdp_set_backup_settings(rdpRdp* rdp)
2735 if (!rdp->originalSettings)
2737 return rdp_reset_remote_settings(rdp);
2740 BOOL rdp_reset_runtime_settings(rdpRdp* rdp)
2743 WINPR_ASSERT(rdp->context);
2750 return rdp_reset_remote_settings(rdp);
2753 static BOOL starts_with(
const char* tok,
const char* val)
2755 const size_t len = strlen(val);
2756 if (strncmp(tok, val, len) != 0)
2758 if (tok[len] !=
'=')
2763 static BOOL option_equals(
const char* what,
const char* val)
2765 return _stricmp(what, val) == 0;
2768 static BOOL parse_on_off_option(
const char* value)
2770 WINPR_ASSERT(value);
2771 const char* sep = strchr(value,
'=');
2774 if (option_equals(
"on", &sep[1]))
2776 if (option_equals(
"true", &sep[1]))
2778 if (option_equals(
"off", &sep[1]))
2780 if (option_equals(
"false", &sep[1]))
2784 long val = strtol(value, NULL, 0);
2786 return val == 0 ? FALSE : TRUE;
2793 static BOOL option_is_runtime_checks(wLog* log,
const char* tok)
2795 const char* experimental[] = { STR(WITH_VERBOSE_WINPR_ASSERT) };
2796 for (
size_t x = 0; x < ARRAYSIZE(experimental); x++)
2798 const char* opt = experimental[x];
2799 if (starts_with(tok, opt))
2801 return parse_on_off_option(tok);
2807 static BOOL option_is_experimental(wLog* log,
const char* tok)
2809 const char* experimental[] = { STR(WITH_DSP_EXPERIMENTAL), STR(WITH_VAAPI) };
2810 for (
size_t x = 0; x < ARRAYSIZE(experimental); x++)
2812 const char* opt = experimental[x];
2813 if (starts_with(tok, opt))
2815 return parse_on_off_option(tok);
2821 static BOOL option_is_debug(wLog* log,
const char* tok)
2824 const char* debug[] = { STR(WITH_DEBUG_ALL),
2825 STR(WITH_DEBUG_CERTIFICATE),
2826 STR(WITH_DEBUG_CAPABILITIES),
2827 STR(WITH_DEBUG_CHANNELS),
2828 STR(WITH_DEBUG_CLIPRDR),
2829 STR(WITH_DEBUG_CODECS),
2830 STR(WITH_DEBUG_RDPGFX),
2831 STR(WITH_DEBUG_DVC),
2832 STR(WITH_DEBUG_TSMF),
2833 STR(WITH_DEBUG_KBD),
2834 STR(WITH_DEBUG_LICENSE),
2835 STR(WITH_DEBUG_NEGO),
2836 STR(WITH_DEBUG_NLA),
2837 STR(WITH_DEBUG_TSG),
2838 STR(WITH_DEBUG_RAIL),
2839 STR(WITH_DEBUG_RDP),
2840 STR(WITH_DEBUG_RDPEI),
2841 STR(WITH_DEBUG_REDIR),
2842 STR(WITH_DEBUG_RDPDR),
2843 STR(WITH_DEBUG_RFX),
2844 STR(WITH_DEBUG_SCARD),
2845 STR(WITH_DEBUG_SND),
2846 STR(WITH_DEBUG_SVC),
2847 STR(WITH_DEBUG_TRANSPORT),
2848 STR(WITH_DEBUG_TIMEZONE),
2849 STR(WITH_DEBUG_WND),
2850 STR(WITH_DEBUG_X11_CLIPRDR),
2851 STR(WITH_DEBUG_X11_LOCAL_MOVESIZE),
2852 STR(WITH_DEBUG_X11),
2854 STR(WITH_DEBUG_RINGBUFFER),
2855 STR(WITH_DEBUG_SYMBOLS),
2856 STR(WITH_DEBUG_EVENTS),
2857 STR(WITH_DEBUG_MUTEX),
2858 STR(WITH_DEBUG_NTLM),
2859 STR(WITH_DEBUG_SDL_EVENTS),
2860 STR(WITH_DEBUG_SDL_KBD_EVENTS),
2861 STR(WITH_DEBUG_THREADS),
2862 STR(WITH_DEBUG_URBDRC) };
2864 for (
size_t x = 0; x < ARRAYSIZE(debug); x++)
2866 const char* opt = debug[x];
2867 if (starts_with(tok, opt))
2868 return parse_on_off_option(tok);
2871 if (starts_with(tok,
"WITH_DEBUG"))
2873 WLog_Print(log, WLOG_WARN,
"[BUG] Unmapped Debug-Build option '%s'.", tok);
2874 return parse_on_off_option(tok);
2880 static void log_build_warn(rdpRdp* rdp,
const char* what,
const char* msg,
2881 BOOL (*cmp)(wLog* log,
const char* tok))
2884 WINPR_PRAGMA_DIAG_PUSH
2885 WINPR_PRAGMA_DIAG_IGNORED_OVERLENGTH_STRINGS
2887 size_t len =
sizeof(FREERDP_BUILD_CONFIG);
2888 char* list = calloc(len,
sizeof(
char));
2889 char* config = _strdup(FREERDP_BUILD_CONFIG);
2890 WINPR_PRAGMA_DIAG_POP
2894 char* saveptr = NULL;
2895 char* tok = strtok_s(config,
" ", &saveptr);
2898 if (cmp(rdp->log, tok))
2899 winpr_str_append(tok, list, len,
" ");
2901 tok = strtok_s(NULL,
" ", &saveptr);
2908 if (strlen(list) > 0)
2910 WLog_Print(rdp->log, WLOG_WARN,
"*************************************************");
2911 WLog_Print(rdp->log, WLOG_WARN,
"This build is using [%s] build options:", what);
2913 char* saveptr = NULL;
2914 char* tok = strtok_s(list,
" ", &saveptr);
2917 WLog_Print(rdp->log, WLOG_WARN,
"* '%s'", tok);
2918 tok = strtok_s(NULL,
" ", &saveptr);
2920 WLog_Print(rdp->log, WLOG_WARN,
"");
2921 WLog_Print(rdp->log, WLOG_WARN,
"[%s] build options %s", what, msg);
2922 WLog_Print(rdp->log, WLOG_WARN,
"*************************************************");
2928 #define print_first_line(log, firstLine, what) \
2929 print_first_line_int((log), (firstLine), (what), __FILE__, __func__, __LINE__)
2930 static void print_first_line_int(wLog* log, log_line_t* firstLine,
const char* what,
2931 const char* file,
const char* fkt,
size_t line)
2933 WINPR_ASSERT(firstLine);
2934 if (!firstLine->fkt)
2936 const DWORD level = WLOG_WARN;
2937 if (WLog_IsLevelActive(log, level))
2939 WLog_PrintMessage(log, WLOG_MESSAGE_TEXT, level, line, file, fkt,
2940 "*************************************************");
2941 WLog_PrintMessage(log, WLOG_MESSAGE_TEXT, level, line, file, fkt,
2942 "[SSL] {%s} build or configuration missing:", what);
2944 firstLine->line = line;
2945 firstLine->file = file;
2946 firstLine->fkt = fkt;
2947 firstLine->level = level;
2951 static void print_last_line(wLog* log,
const log_line_t* firstLine)
2953 WINPR_ASSERT(firstLine);
2956 if (WLog_IsLevelActive(log, firstLine->level))
2957 WLog_PrintMessage(log, WLOG_MESSAGE_TEXT, firstLine->level, firstLine->line,
2958 firstLine->file, firstLine->fkt,
2959 "*************************************************");
2963 static void log_build_warn_cipher(rdpRdp* rdp, log_line_t* firstLine, WINPR_CIPHER_TYPE md,
2966 BOOL haveCipher = FALSE;
2968 char key[WINPR_CIPHER_MAX_KEY_LENGTH] = { 0 };
2969 char iv[WINPR_CIPHER_MAX_IV_LENGTH] = { 0 };
2973 if (md == WINPR_CIPHER_ARC4_128)
2975 WINPR_RC4_CTX* enc = winpr_RC4_New(key,
sizeof(key));
2976 haveCipher = enc != NULL;
2977 winpr_RC4_Free(enc);
2981 WINPR_CIPHER_CTX* enc =
2982 winpr_Cipher_NewEx(md, WINPR_ENCRYPT, key,
sizeof(key), iv,
sizeof(iv));
2983 WINPR_CIPHER_CTX* dec =
2984 winpr_Cipher_NewEx(md, WINPR_DECRYPT, key,
sizeof(key), iv,
sizeof(iv));
2988 winpr_Cipher_Free(enc);
2989 winpr_Cipher_Free(dec);
2994 print_first_line(rdp->log, firstLine,
"Cipher");
2995 WLog_Print(rdp->log, WLOG_WARN,
"* %s: %s", winpr_cipher_type_to_string(md), what);
2999 static void log_build_warn_hmac(rdpRdp* rdp, log_line_t* firstLine, WINPR_MD_TYPE md,
3002 BOOL haveHmacX = FALSE;
3003 WINPR_HMAC_CTX* hmac = winpr_HMAC_New();
3009 char key[WINPR_CIPHER_MAX_KEY_LENGTH] = { 0 };
3010 haveHmacX = winpr_HMAC_Init(hmac, md, key,
sizeof(key));
3012 winpr_HMAC_Free(hmac);
3016 print_first_line(rdp->log, firstLine,
"HMAC");
3017 WLog_Print(rdp->log, WLOG_WARN,
" * %s: %s", winpr_md_type_to_string(md), what);
3021 static void log_build_warn_hash(rdpRdp* rdp, log_line_t* firstLine, WINPR_MD_TYPE md,
3024 BOOL haveDigestX = FALSE;
3026 WINPR_DIGEST_CTX* digest = winpr_Digest_New();
3028 haveDigestX = winpr_Digest_Init(digest, md);
3029 winpr_Digest_Free(digest);
3033 print_first_line(rdp->log, firstLine,
"Digest");
3034 WLog_Print(rdp->log, WLOG_WARN,
" * %s: %s", winpr_md_type_to_string(md), what);
3038 static void log_build_warn_ssl(rdpRdp* rdp)
3042 log_line_t firstHashLine = { 0 };
3043 log_build_warn_hash(rdp, &firstHashLine, WINPR_MD_MD4,
"NTLM support not available");
3044 log_build_warn_hash(rdp, &firstHashLine, WINPR_MD_MD5,
3045 "NTLM, assistance files with encrypted passwords, autoreconnect cookies, "
3046 "licensing and RDP security will not work");
3047 log_build_warn_hash(rdp, &firstHashLine, WINPR_MD_SHA1,
3048 "assistance files with encrypted passwords, Kerberos, Smartcard Logon, RDP "
3049 "security support not available");
3050 log_build_warn_hash(
3051 rdp, &firstHashLine, WINPR_MD_SHA256,
3052 "file clipboard, AAD gateway, NLA security and certificates might not work");
3053 print_last_line(rdp->log, &firstHashLine);
3055 log_line_t firstHmacLine = { 0 };
3056 log_build_warn_hmac(rdp, &firstHmacLine, WINPR_MD_MD5,
"Autoreconnect cookie not supported");
3057 log_build_warn_hmac(rdp, &firstHmacLine, WINPR_MD_SHA1,
"RDP security not supported");
3058 print_last_line(rdp->log, &firstHmacLine);
3060 log_line_t firstCipherLine = { 0 };
3061 log_build_warn_cipher(rdp, &firstCipherLine, WINPR_CIPHER_ARC4_128,
3062 "assistance files with encrypted passwords, NTLM, RDP licensing and RDP "
3063 "security will not work");
3064 log_build_warn_cipher(rdp, &firstCipherLine, WINPR_CIPHER_DES_EDE3_CBC,
3065 "RDP security FIPS mode will not work");
3066 log_build_warn_cipher(
3067 rdp, &firstCipherLine, WINPR_CIPHER_AES_128_CBC,
3068 "assistance file encrypted LHTicket will not work and ARM gateway might not");
3069 log_build_warn_cipher(rdp, &firstCipherLine, WINPR_CIPHER_AES_192_CBC,
3070 "ARM gateway might not work");
3071 log_build_warn_cipher(rdp, &firstCipherLine, WINPR_CIPHER_AES_256_CBC,
3072 "ARM gateway might not work");
3073 print_last_line(rdp->log, &firstCipherLine);
3076 void rdp_log_build_warnings(rdpRdp* rdp)
3078 static unsigned count = 0;
3087 log_build_warn(rdp,
"experimental",
"might crash the application", option_is_experimental);
3088 log_build_warn(rdp,
"debug",
"might leak sensitive information (credentials, ...)",
3090 log_build_warn(rdp,
"runtime-check",
"might slow down the application",
3091 option_is_runtime_checks);
3092 log_build_warn_ssl(rdp);
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.