22 #include <freerdp/config.h>
26 #include <winpr/crt.h>
27 #include <winpr/assert.h>
29 #include <freerdp/crypto/crypto.h>
30 #include <freerdp/log.h>
31 #include <freerdp/session.h>
38 #define TAG FREERDP_TAG("core.info")
40 #define logonInfoV2Size (2 + 4 + 4 + 4 + 4)
41 #define logonInfoV2ReservedSize 558
42 #define logonInfoV2TotalSize (logonInfoV2Size + logonInfoV2ReservedSize)
44 static const char* INFO_TYPE_LOGON_STRINGS[4] = {
"Logon Info V1",
"Logon Info V2",
45 "Logon Plain Notify",
"Logon Extended Info" };
48 #define MAX_LABEL_LENGTH 40
55 static const struct info_flags_t info_flags[] = {
56 { INFO_MOUSE,
"INFO_MOUSE" },
57 { INFO_DISABLECTRLALTDEL,
"INFO_DISABLECTRLALTDEL" },
58 { INFO_AUTOLOGON,
"INFO_AUTOLOGON" },
59 { INFO_UNICODE,
"INFO_UNICODE" },
60 { INFO_MAXIMIZESHELL,
"INFO_MAXIMIZESHELL" },
61 { INFO_LOGONNOTIFY,
"INFO_LOGONNOTIFY" },
62 { INFO_COMPRESSION,
"INFO_COMPRESSION" },
63 { INFO_ENABLEWINDOWSKEY,
"INFO_ENABLEWINDOWSKEY" },
64 { INFO_REMOTECONSOLEAUDIO,
"INFO_REMOTECONSOLEAUDIO" },
65 { INFO_FORCE_ENCRYPTED_CS_PDU,
"INFO_FORCE_ENCRYPTED_CS_PDU" },
66 { INFO_RAIL,
"INFO_RAIL" },
67 { INFO_LOGONERRORS,
"INFO_LOGONERRORS" },
68 { INFO_MOUSE_HAS_WHEEL,
"INFO_MOUSE_HAS_WHEEL" },
69 { INFO_PASSWORD_IS_SC_PIN,
"INFO_PASSWORD_IS_SC_PIN" },
70 { INFO_NOAUDIOPLAYBACK,
"INFO_NOAUDIOPLAYBACK" },
71 { INFO_USING_SAVED_CREDS,
"INFO_USING_SAVED_CREDS" },
72 { INFO_AUDIOCAPTURE,
"INFO_AUDIOCAPTURE" },
73 { INFO_VIDEO_DISABLE,
"INFO_VIDEO_DISABLE" },
74 { INFO_HIDEF_RAIL_SUPPORTED,
"INFO_HIDEF_RAIL_SUPPORTED" },
77 static BOOL rdp_read_info_null_string(rdpSettings* settings, FreeRDP_Settings_Keys_String
id,
78 const char* what, UINT32 flags,
wStream* s,
size_t cbLen,
81 const BOOL unicode = (flags & INFO_UNICODE) ? TRUE : FALSE;
86 if (!Stream_CheckAndLogRequiredLength(TAG, s, (
size_t)(cbLen)))
91 if ((cbLen > max) || (unicode && ((cbLen % 2) != 0)))
93 WLog_ERR(TAG,
"protocol error: %s has invalid value: %" PRIuz
"", what, cbLen);
99 const WCHAR* domain = Stream_PointerAs(s, WCHAR);
101 cbLen /
sizeof(WCHAR)))
103 WLog_ERR(TAG,
"protocol error: no data to read for %s [expected %" PRIuz
"]", what,
110 const char* domain = Stream_ConstPointer(s);
115 Stream_Seek(s, cbLen);
120 static char* rdp_info_package_flags_description(UINT32 flags)
123 size_t maximum_size = 1 + MAX_LABEL_LENGTH * ARRAYSIZE(info_flags);
125 result = calloc(maximum_size,
sizeof(
char));
130 for (
size_t i = 0; i < ARRAYSIZE(info_flags); i++)
132 const struct info_flags_t* cur = &info_flags[i];
133 if (cur->flag & flags)
135 winpr_str_append(cur->label, result, maximum_size,
"|");
142 static BOOL rdp_compute_client_auto_reconnect_cookie(rdpRdp* rdp)
144 BYTE ClientRandom[CLIENT_RANDOM_LENGTH] = { 0 };
145 BYTE AutoReconnectRandom[32] = { 0 };
150 rdpSettings* settings = rdp->settings;
151 WINPR_ASSERT(settings);
153 serverCookie = settings->ServerAutoReconnectCookie;
154 clientCookie = settings->ClientAutoReconnectCookie;
155 clientCookie->cbLen = 28;
156 clientCookie->version = serverCookie->version;
157 clientCookie->logonId = serverCookie->logonId;
158 ZeroMemory(clientCookie->securityVerifier,
sizeof(clientCookie->securityVerifier));
159 CopyMemory(AutoReconnectRandom, serverCookie->arcRandomBits,
160 sizeof(serverCookie->arcRandomBits));
162 if (settings->SelectedProtocol == PROTOCOL_RDP)
163 CopyMemory(ClientRandom, settings->ClientRandom, settings->ClientRandomLength);
167 if (!winpr_HMAC(WINPR_MD_MD5, AutoReconnectRandom, 16, ClientRandom,
sizeof(ClientRandom),
168 clientCookie->securityVerifier,
sizeof(clientCookie->securityVerifier)))
179 static BOOL rdp_read_server_auto_reconnect_cookie(rdpRdp* rdp,
wStream* s, logon_info_ex* info)
183 rdpSettings* settings = rdp->settings;
184 autoReconnectCookie = settings->ServerAutoReconnectCookie;
186 if (!Stream_CheckAndLogRequiredLength(TAG, s, 28))
189 Stream_Read_UINT32(s, autoReconnectCookie->cbLen);
191 if (autoReconnectCookie->cbLen != 28)
193 WLog_ERR(TAG,
"ServerAutoReconnectCookie.cbLen != 28");
197 Stream_Read_UINT32(s, autoReconnectCookie->version);
198 Stream_Read_UINT32(s, autoReconnectCookie->logonId);
199 Stream_Read(s, autoReconnectCookie->arcRandomBits, 16);
200 p = autoReconnectCookie->arcRandomBits;
202 "ServerAutoReconnectCookie: Version: %" PRIu32
" LogonId: %" PRIu32
203 " SecurityVerifier: "
204 "%02" PRIX8
"%02" PRIX8
"%02" PRIX8
"%02" PRIX8
"%02" PRIX8
"%02" PRIX8
"%02" PRIX8
206 "%02" PRIX8
"%02" PRIX8
"%02" PRIX8
"%02" PRIX8
"%02" PRIX8
"%02" PRIX8
"%02" PRIX8
208 autoReconnectCookie->version, autoReconnectCookie->logonId, p[0], p[1], p[2], p[3],
209 p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
210 info->LogonId = autoReconnectCookie->logonId;
211 CopyMemory(info->ArcRandomBits, p, 16);
213 if ((settings->PrintReconnectCookie))
217 WLog_INFO(TAG,
"Reconnect-cookie: %s", base64);
229 static BOOL rdp_read_client_auto_reconnect_cookie(rdpRdp* rdp,
wStream* s)
232 rdpSettings* settings = rdp->settings;
233 autoReconnectCookie = settings->ClientAutoReconnectCookie;
235 if (!Stream_CheckAndLogRequiredLength(TAG, s, 28))
238 Stream_Read_UINT32(s, autoReconnectCookie->cbLen);
239 Stream_Read_UINT32(s, autoReconnectCookie->version);
240 Stream_Read_UINT32(s, autoReconnectCookie->logonId);
241 Stream_Read(s, autoReconnectCookie->securityVerifier, 16);
250 static BOOL rdp_write_client_auto_reconnect_cookie(rdpRdp* rdp,
wStream* s)
254 rdpSettings* settings = NULL;
258 settings = rdp->settings;
259 WINPR_ASSERT(settings);
261 autoReconnectCookie = settings->ClientAutoReconnectCookie;
262 WINPR_ASSERT(autoReconnectCookie);
264 p = autoReconnectCookie->securityVerifier;
268 "ClientAutoReconnectCookie: Version: %" PRIu32
" LogonId: %" PRIu32
" ArcRandomBits: "
269 "%02" PRIX8
"%02" PRIX8
"%02" PRIX8
"%02" PRIX8
"%02" PRIX8
"%02" PRIX8
"%02" PRIX8
271 "%02" PRIX8
"%02" PRIX8
"%02" PRIX8
"%02" PRIX8
"%02" PRIX8
"%02" PRIX8
"%02" PRIX8
273 autoReconnectCookie->version, autoReconnectCookie->logonId, p[0], p[1], p[2], p[3],
274 p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
275 if (!Stream_EnsureRemainingCapacity(s, 12ull + 16ull))
277 Stream_Write_UINT32(s, autoReconnectCookie->cbLen);
278 Stream_Write_UINT32(s, autoReconnectCookie->version);
279 Stream_Write_UINT32(s, autoReconnectCookie->logonId);
280 Stream_Write(s, autoReconnectCookie->securityVerifier, 16);
289 static size_t rdp_get_client_address_max_size(
const rdpRdp* rdp)
292 rdpSettings* settings = NULL;
296 settings = rdp->settings;
297 WINPR_ASSERT(settings);
300 if (version < RDP_VERSION_10_0)
310 static BOOL rdp_read_extended_info_packet(rdpRdp* rdp,
wStream* s)
312 UINT16 clientAddressFamily = 0;
313 UINT16 cbClientAddress = 0;
314 UINT16 cbClientDir = 0;
315 UINT16 cbAutoReconnectLen = 0;
319 rdpSettings* settings = rdp->settings;
320 WINPR_ASSERT(settings);
322 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
325 Stream_Read_UINT16(s, clientAddressFamily);
326 Stream_Read_UINT16(s, cbClientAddress);
328 settings->IPv6Enabled = (clientAddressFamily == ADDRESS_FAMILY_INET6 ? TRUE : FALSE);
330 if (!rdp_read_info_null_string(settings, FreeRDP_ClientAddress,
"cbClientAddress", INFO_UNICODE,
331 s, cbClientAddress, rdp_get_client_address_max_size(rdp)))
334 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
337 Stream_Read_UINT16(s, cbClientDir);
347 if (!rdp_read_info_null_string(settings, FreeRDP_ClientDir,
"cbClientDir", INFO_UNICODE, s,
357 if (Stream_GetRemainingLength(s) == 0)
360 if (!rdp_read_client_time_zone(s, settings))
364 if (Stream_GetRemainingLength(s) == 0)
366 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
369 Stream_Read_UINT32(s, settings->ClientSessionId);
372 if (Stream_GetRemainingLength(s) == 0)
375 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
378 Stream_Read_UINT32(s, settings->PerformanceFlags);
379 freerdp_performance_flags_split(settings);
382 if (Stream_GetRemainingLength(s) == 0)
385 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
388 Stream_Read_UINT16(s, cbAutoReconnectLen);
392 if (cbAutoReconnectLen > 0)
394 if (!rdp_read_client_auto_reconnect_cookie(rdp, s))
399 if (Stream_GetRemainingLength(s) == 0)
402 if (!Stream_SafeSeek(s, 2))
405 if (Stream_GetRemainingLength(s) == 0)
408 if (!Stream_SafeSeek(s, 2))
411 if (Stream_GetRemainingLength(s) == 0)
414 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
419 UINT16 cbDynamicDSTTimeZoneKeyName = 0;
421 Stream_Read_UINT16(s, cbDynamicDSTTimeZoneKeyName);
423 if (!rdp_read_info_null_string(settings, FreeRDP_DynamicDSTTimeZoneKeyName,
424 "cbDynamicDSTTimeZoneKeyName", INFO_UNICODE, s,
425 cbDynamicDSTTimeZoneKeyName, 254))
428 if (Stream_GetRemainingLength(s) == 0)
431 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
433 UINT16 DynamicDaylightTimeDisabled = 0;
434 Stream_Read_UINT16(s, DynamicDaylightTimeDisabled);
435 if (DynamicDaylightTimeDisabled > 1)
438 "[MS-RDPBCGR] 2.2.1.11.1.1.1 Extended Info Packet "
439 "(TS_EXTENDED_INFO_PACKET)::dynamicDaylightTimeDisabled value %" PRIu16
440 " not allowed in [0,1]",
441 settings->DynamicDaylightTimeDisabled);
445 DynamicDaylightTimeDisabled != 0))
447 DEBUG_TIMEZONE(
"DynamicTimeZone=%s [%s]",
463 static BOOL rdp_write_extended_info_packet(rdpRdp* rdp,
wStream* s)
466 size_t cbClientAddress = 0;
467 const size_t cbClientAddressMax = rdp_get_client_address_max_size(rdp);
468 WCHAR* clientDir = NULL;
469 size_t cbClientDir = 0;
470 const size_t cbClientDirMax = 512;
471 UINT16 cbAutoReconnectCookie = 0;
475 rdpSettings* settings = rdp->settings;
476 WINPR_ASSERT(settings);
478 UINT16 clientAddressFamily = ADDRESS_FAMILY_INET;
479 if (settings->ConnectChildSession)
480 clientAddressFamily = 0x0000;
481 else if (settings->IPv6Enabled)
482 clientAddressFamily = ADDRESS_FAMILY_INET6;
484 WCHAR* clientAddress = ConvertUtf8ToWCharAlloc(settings->ClientAddress, &cbClientAddress);
486 if (cbClientAddress > (UINT16_MAX /
sizeof(WCHAR)))
488 WLog_ERR(TAG,
"cbClientAddress > UINT16_MAX");
492 if (cbClientAddress > 0)
494 cbClientAddress = (UINT16)(cbClientAddress + 1) *
sizeof(WCHAR);
495 if (cbClientAddress > cbClientAddressMax)
498 "the client address %s [%" PRIuz
"] exceeds the limit of %" PRIuz
500 settings->ClientAddress, cbClientAddress, cbClientAddressMax);
502 clientAddress[(cbClientAddressMax /
sizeof(WCHAR)) - 1] =
'\0';
503 cbClientAddress = cbClientAddressMax;
507 clientDir = ConvertUtf8ToWCharAlloc(settings->ClientDir, &cbClientDir);
508 if (cbClientDir > (UINT16_MAX /
sizeof(WCHAR)))
510 WLog_ERR(TAG,
"cbClientDir > UINT16_MAX");
516 cbClientDir = (UINT16)(cbClientDir + 1) *
sizeof(WCHAR);
517 if (cbClientDir > cbClientDirMax)
520 "the client dir %s [%" PRIuz
"] exceeds the limit of %" PRIuz
", truncating.",
521 settings->ClientDir, cbClientDir, cbClientDirMax);
523 clientDir[(cbClientDirMax /
sizeof(WCHAR)) - 1] =
'\0';
524 cbClientDir = cbClientDirMax;
528 if (settings->ServerAutoReconnectCookie->cbLen > UINT16_MAX)
530 WLog_ERR(TAG,
"ServerAutoreconnectCookie::cbLen > UINT16_MAX");
534 cbAutoReconnectCookie = (UINT16)settings->ServerAutoReconnectCookie->cbLen;
536 if (!Stream_EnsureRemainingCapacity(s, 4ull + cbClientAddress + 2ull + cbClientDir))
539 Stream_Write_UINT16(s, clientAddressFamily);
540 Stream_Write_UINT16(s, (UINT16)cbClientAddress);
542 Stream_Write(s, clientAddress, cbClientAddress);
544 Stream_Write_UINT16(s, (UINT16)cbClientDir);
546 Stream_Write(s, clientDir, cbClientDir);
548 if (!rdp_write_client_time_zone(s, settings))
551 if (!Stream_EnsureRemainingCapacity(s, 10ull))
555 Stream_Write_UINT32(s, settings->ClientSessionId);
556 freerdp_performance_flags_make(settings);
557 Stream_Write_UINT32(s, settings->PerformanceFlags);
558 Stream_Write_UINT16(s, cbAutoReconnectCookie);
560 if (cbAutoReconnectCookie > 0)
562 if (!rdp_compute_client_auto_reconnect_cookie(rdp))
564 if (!rdp_write_client_auto_reconnect_cookie(rdp, s))
570 if (!Stream_EnsureRemainingCapacity(s, 8 + 254 *
sizeof(WCHAR)))
573 Stream_Write_UINT16(s, 0);
574 Stream_Write_UINT16(s, 0);
579 rlen = strnlen(tz, 254);
580 Stream_Write_UINT16(s, (UINT16)rlen *
sizeof(WCHAR));
581 if (Stream_Write_UTF16_String_From_UTF8(s, rlen, tz, rlen, FALSE) < 0)
583 Stream_Write_UINT16(s, settings->DynamicDaylightTimeDisabled ? 0x01 : 0x00);
593 static BOOL rdp_read_info_string(rdpSettings* settings, FreeRDP_Settings_Keys_String
id,
594 UINT32 flags,
wStream* s,
size_t cbLenNonNull,
size_t max)
603 const BOOL unicode = (flags & INFO_UNICODE) ? TRUE : FALSE;
604 const size_t nullSize = unicode ?
sizeof(WCHAR) :
sizeof(CHAR);
609 if (!Stream_CheckAndLogRequiredLength(TAG, s, (
size_t)(cbLenNonNull + nullSize)))
612 if (cbLenNonNull > 0)
618 if ((cbLenNonNull % 2) || (cbLenNonNull > (max - nullSize)))
620 WLog_ERR(TAG,
"protocol error: invalid value: %" PRIuz
"", cbLenNonNull);
626 const WCHAR* domain = Stream_PointerAs(s, WCHAR);
628 cbLenNonNull /
sizeof(WCHAR)))
633 const char* domain = Stream_PointerAs(s,
char);
639 Stream_Seek(s, cbLenNonNull);
641 terminator.w = L
'\0';
642 Stream_Read(s, terminator.b, nullSize);
644 if (terminator.w != L
'\0')
646 WLog_ERR(TAG,
"protocol error: Domain must be null terminated");
659 static BOOL rdp_read_info_packet(rdpRdp* rdp,
wStream* s, UINT16 tpktlength)
661 BOOL smallsize = FALSE;
664 UINT16 cbUserName = 0;
665 UINT16 cbPassword = 0;
666 UINT16 cbAlternateShell = 0;
667 UINT16 cbWorkingDir = 0;
668 UINT32 CompressionLevel = 0;
669 rdpSettings* settings = rdp->settings;
671 if (!Stream_CheckAndLogRequiredLength(TAG, s, 18))
674 Stream_Read_UINT32(s, settings->KeyboardCodePage);
675 Stream_Read_UINT32(s, flags);
676 settings->AudioCapture = ((flags & INFO_AUDIOCAPTURE) ? TRUE : FALSE);
677 settings->AudioPlayback = ((flags & INFO_NOAUDIOPLAYBACK) ? FALSE : TRUE);
678 settings->AutoLogonEnabled = ((flags & INFO_AUTOLOGON) ? TRUE : FALSE);
679 settings->RemoteApplicationMode = ((flags & INFO_RAIL) ? TRUE : FALSE);
680 settings->HiDefRemoteApp = ((flags & INFO_HIDEF_RAIL_SUPPORTED) ? TRUE : FALSE);
681 settings->RemoteConsoleAudio = ((flags & INFO_REMOTECONSOLEAUDIO) ? TRUE : FALSE);
682 settings->CompressionEnabled = ((flags & INFO_COMPRESSION) ? TRUE : FALSE);
683 settings->LogonNotify = ((flags & INFO_LOGONNOTIFY) ? TRUE : FALSE);
684 settings->MouseHasWheel = ((flags & INFO_MOUSE_HAS_WHEEL) ? TRUE : FALSE);
685 settings->DisableCtrlAltDel = ((flags & INFO_DISABLECTRLALTDEL) ? TRUE : FALSE);
686 settings->ForceEncryptedCsPdu = ((flags & INFO_FORCE_ENCRYPTED_CS_PDU) ? TRUE : FALSE);
687 settings->PasswordIsSmartcardPin = ((flags & INFO_PASSWORD_IS_SC_PIN) ? TRUE : FALSE);
689 if (flags & INFO_COMPRESSION)
691 CompressionLevel = ((flags & 0x00001E00) >> 9);
692 settings->CompressionLevel = CompressionLevel;
696 settings->CompressionLevel = 0;
700 if (settings->RdpVersion < RDP_VERSION_5_PLUS)
703 Stream_Read_UINT16(s, cbDomain);
704 Stream_Read_UINT16(s, cbUserName);
705 Stream_Read_UINT16(s, cbPassword);
706 Stream_Read_UINT16(s, cbAlternateShell);
707 Stream_Read_UINT16(s, cbWorkingDir);
709 if (!rdp_read_info_string(settings, FreeRDP_Domain, flags, s, cbDomain, smallsize ? 52 : 512))
712 if (!rdp_read_info_string(settings, FreeRDP_Username, flags, s, cbUserName,
713 smallsize ? 44 : 512))
716 if (!rdp_read_info_string(settings, FreeRDP_Password, flags, s, cbPassword,
717 smallsize ? 32 : 512))
720 if (!rdp_read_info_string(settings, FreeRDP_AlternateShell, flags, s, cbAlternateShell, 512))
723 if (!rdp_read_info_string(settings, FreeRDP_ShellWorkingDirectory, flags, s, cbWorkingDir, 512))
726 if (settings->RdpVersion >= RDP_VERSION_5_PLUS)
728 if (!rdp_read_extended_info_packet(rdp, s))
732 const size_t xrem = Stream_GetRemainingLength(s);
733 if (!tpkt_ensure_stream_consumed(s, tpktlength))
734 Stream_Seek(s, xrem);
743 static BOOL rdp_write_info_packet(rdpRdp* rdp,
wStream* s)
747 WCHAR* domainW = NULL;
749 WCHAR* userNameW = NULL;
750 size_t cbUserName = 0;
751 WCHAR* passwordW = NULL;
752 size_t cbPassword = 0;
753 WCHAR* alternateShellW = NULL;
754 size_t cbAlternateShell = 0;
755 WCHAR* workingDirW = NULL;
756 size_t cbWorkingDir = 0;
757 BOOL usedPasswordCookie = FALSE;
758 rdpSettings* settings = NULL;
761 settings = rdp->settings;
762 WINPR_ASSERT(settings);
764 flags = INFO_MOUSE | INFO_UNICODE | INFO_LOGONERRORS | INFO_MAXIMIZESHELL |
765 INFO_ENABLEWINDOWSKEY | INFO_DISABLECTRLALTDEL | INFO_MOUSE_HAS_WHEEL |
766 INFO_FORCE_ENCRYPTED_CS_PDU;
768 if (settings->SmartcardLogon)
770 flags |= INFO_AUTOLOGON;
771 flags |= INFO_PASSWORD_IS_SC_PIN;
774 if (settings->AudioCapture)
775 flags |= INFO_AUDIOCAPTURE;
777 if (!settings->AudioPlayback)
778 flags |= INFO_NOAUDIOPLAYBACK;
780 if (settings->VideoDisable)
781 flags |= INFO_VIDEO_DISABLE;
783 if (settings->AutoLogonEnabled)
784 flags |= INFO_AUTOLOGON;
786 if (settings->RemoteApplicationMode)
788 if (settings->HiDefRemoteApp)
789 flags |= INFO_HIDEF_RAIL_SUPPORTED;
794 if (settings->RemoteConsoleAudio)
795 flags |= INFO_REMOTECONSOLEAUDIO;
797 if (settings->CompressionEnabled)
799 flags |= INFO_COMPRESSION;
800 flags |= ((settings->CompressionLevel << 9) & 0x00001E00);
803 if (settings->LogonNotify)
804 flags |= INFO_LOGONNOTIFY;
806 if (settings->PasswordIsSmartcardPin)
807 flags |= INFO_PASSWORD_IS_SC_PIN;
810 char* flags_description = rdp_info_package_flags_description(flags);
812 if (flags_description)
814 WLog_DBG(TAG,
"Client Info Packet Flags = %s", flags_description);
815 free(flags_description);
820 if (cbDomain > UINT16_MAX /
sizeof(WCHAR))
822 WLog_ERR(TAG,
"cbDomain > UINT16_MAX");
825 cbDomain *=
sizeof(WCHAR);
829 if (cbUserName > UINT16_MAX /
sizeof(WCHAR))
831 WLog_ERR(TAG,
"cbUserName > UINT16_MAX");
834 cbUserName *=
sizeof(WCHAR);
836 const char* pin =
"*";
837 if (!settings->RemoteAssistanceMode)
840 if (((flags & INFO_PASSWORD_IS_SC_PIN) == 0) && settings->RedirectionPassword &&
841 (settings->RedirectionPasswordLength > 0))
849 if (settings->RedirectionPasswordLength > UINT16_MAX)
851 WLog_ERR(TAG,
"RedirectionPasswordLength > UINT16_MAX");
854 usedPasswordCookie = TRUE;
856 ptrconv.bp = settings->RedirectionPassword;
857 passwordW = ptrconv.wp;
858 cbPassword = (UINT16)settings->RedirectionPasswordLength;
864 if (!usedPasswordCookie && pin)
866 passwordW = ConvertUtf8ToWCharAlloc(pin, &cbPassword);
867 if (cbPassword > UINT16_MAX /
sizeof(WCHAR))
869 WLog_ERR(TAG,
"cbPassword > UINT16_MAX");
872 cbPassword = (UINT16)cbPassword *
sizeof(WCHAR);
875 const char* altShell = NULL;
876 if (!settings->RemoteAssistanceMode)
878 else if (settings->RemoteAssistancePassStub)
883 if (altShell && strlen(altShell) > 0)
885 alternateShellW = ConvertUtf8ToWCharAlloc(altShell, &cbAlternateShell);
886 if (!alternateShellW)
888 WLog_ERR(TAG,
"alternateShellW == NULL");
891 if (cbAlternateShell > (UINT16_MAX /
sizeof(WCHAR)))
893 WLog_ERR(TAG,
"cbAlternateShell > UINT16_MAX");
896 cbAlternateShell = (UINT16)cbAlternateShell *
sizeof(WCHAR);
899 FreeRDP_Settings_Keys_String inputId = FreeRDP_RemoteAssistanceSessionId;
901 inputId = FreeRDP_ShellWorkingDirectory;
904 if (cbWorkingDir > (UINT16_MAX /
sizeof(WCHAR)))
906 WLog_ERR(TAG,
"cbWorkingDir > UINT16_MAX");
909 cbWorkingDir = (UINT16)cbWorkingDir *
sizeof(WCHAR);
911 if (!Stream_EnsureRemainingCapacity(s, 18ull + cbDomain + cbUserName + cbPassword +
912 cbAlternateShell + cbWorkingDir + 5 *
sizeof(WCHAR)))
915 Stream_Write_UINT32(s, settings->KeyboardCodePage);
916 Stream_Write_UINT32(s, flags);
917 Stream_Write_UINT16(s, (UINT16)cbDomain);
918 Stream_Write_UINT16(s, (UINT16)cbUserName);
919 Stream_Write_UINT16(s, (UINT16)cbPassword);
920 Stream_Write_UINT16(s, (UINT16)cbAlternateShell);
921 Stream_Write_UINT16(s, (UINT16)cbWorkingDir);
923 Stream_Write(s, domainW, cbDomain);
926 Stream_Write_UINT16(s, 0);
928 Stream_Write(s, userNameW, cbUserName);
931 Stream_Write_UINT16(s, 0);
933 Stream_Write(s, passwordW, cbPassword);
936 Stream_Write_UINT16(s, 0);
938 Stream_Write(s, alternateShellW, cbAlternateShell);
941 Stream_Write_UINT16(s, 0);
943 Stream_Write(s, workingDirW, cbWorkingDir);
946 Stream_Write_UINT16(s, 0);
951 free(alternateShellW);
954 if (!usedPasswordCookie)
960 if (settings->RdpVersion >= RDP_VERSION_5_PLUS)
961 ret = rdp_write_extended_info_packet(rdp, s);
973 BOOL rdp_recv_client_info(rdpRdp* rdp,
wStream* s)
976 UINT16 channelId = 0;
977 UINT16 securityFlags = 0;
979 WINPR_ASSERT(rdp_get_state(rdp) == CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE);
981 if (!rdp_read_header(rdp, s, &length, &channelId))
984 if (!rdp_read_security_header(rdp, s, &securityFlags, &length))
987 if ((securityFlags & SEC_INFO_PKT) == 0)
990 if (rdp->settings->UseRdpSecurityLayer)
992 if (securityFlags & SEC_REDIRECTION_PKT)
994 WLog_ERR(TAG,
"Error: SEC_REDIRECTION_PKT unsupported");
998 if (securityFlags & SEC_ENCRYPT)
1000 if (!rdp_decrypt(rdp, s, &length, securityFlags))
1005 return rdp_read_info_packet(rdp, s, length);
1014 BOOL rdp_send_client_info(rdpRdp* rdp)
1018 rdp->sec_flags |= SEC_INFO_PKT;
1019 s = rdp_send_stream_init(rdp);
1023 WLog_ERR(TAG,
"Stream_New failed!");
1027 if (!rdp_write_info_packet(rdp, s))
1032 return rdp_send(rdp, s, MCS_GLOBAL_CHANNEL_ID);
1035 static void rdp_free_logon_info(logon_info* info)
1040 free(info->username);
1042 const logon_info empty = { 0 };
1046 static BOOL rdp_info_read_string(
const char* what,
wStream* s,
size_t size,
size_t max,
1047 BOOL skipMax,
char** dst)
1055 return Stream_SafeSeek(s, max);
1059 if (((size %
sizeof(WCHAR)) != 0) || (size > max))
1061 WLog_ERR(TAG,
"protocol error: invalid %s value: %" PRIu32
"", what, size);
1065 const WCHAR* str = Stream_ConstPointer(s);
1066 if (!Stream_SafeSeek(s, skipMax ? max : size))
1069 if (str[size /
sizeof(WCHAR) - 1])
1071 WLog_ERR(TAG,
"protocol error: %s must be null terminated", what);
1076 char* rc = ConvertWCharNToUtf8Alloc(str, size /
sizeof(WCHAR), &len);
1079 WLog_ERR(TAG,
"failed to convert the %s string", what);
1088 static BOOL rdp_recv_logon_info_v1(rdpRdp* rdp,
wStream* s, logon_info* info)
1090 UINT32 cbDomain = 0;
1091 UINT32 cbUserName = 0;
1096 if (!Stream_CheckAndLogRequiredLength(TAG, s, 576))
1099 Stream_Read_UINT32(s, cbDomain);
1104 if (!rdp_info_read_string(
"Domain", s, cbDomain, 52, TRUE, &info->domain))
1107 Stream_Read_UINT32(s, cbUserName);
1112 if (!rdp_info_read_string(
"UserName", s, cbUserName, 512, TRUE, &info->username))
1115 Stream_Read_UINT32(s, info->sessionId);
1116 WLog_DBG(TAG,
"LogonInfoV1: SessionId: 0x%08" PRIX32
" UserName: [%s] Domain: [%s]",
1117 info->sessionId, info->username, info->domain);
1123 static BOOL rdp_recv_logon_info_v2(rdpRdp* rdp,
wStream* s, logon_info* info)
1127 UINT32 cbDomain = 0;
1128 UINT32 cbUserName = 0;
1136 if (!Stream_CheckAndLogRequiredLength(TAG, s, logonInfoV2TotalSize))
1139 Stream_Read_UINT16(s, Version);
1140 if (Version != SAVE_SESSION_PDU_VERSION_ONE)
1142 WLog_WARN(TAG,
"LogonInfoV2::Version expected %" PRIu16
" bytes, got %" PRIu16,
1143 SAVE_SESSION_PDU_VERSION_ONE, Version);
1147 Stream_Read_UINT32(s, Size);
1153 if (Size != logonInfoV2TotalSize)
1155 if (Size != logonInfoV2Size)
1157 WLog_WARN(TAG,
"LogonInfoV2::Size expected %" PRIu32
" bytes, got %" PRIu32,
1158 logonInfoV2TotalSize, Size);
1163 Stream_Read_UINT32(s, info->sessionId);
1164 Stream_Read_UINT32(s, cbDomain);
1165 Stream_Read_UINT32(s, cbUserName);
1166 Stream_Seek(s, logonInfoV2ReservedSize);
1174 if (!rdp_info_read_string(
"Domain", s, cbDomain, 52, FALSE, &info->domain))
1183 if (!rdp_info_read_string(
"UserName", s, cbUserName, 512, FALSE, &info->username))
1190 const size_t rem = Stream_GetRemainingLength(s);
1194 const char* str = Stream_ConstPointer(s);
1195 for (
size_t x = 0; x < rem; x++)
1202 WLog_WARN(TAG,
"unexpected padding of %" PRIuz
" bytes, data not '\0'", rem);
1203 winpr_HexDump(TAG, WLOG_TRACE, str, rem);
1206 if (!Stream_SafeSeek(s, rem))
1210 WLog_DBG(TAG,
"LogonInfoV2: SessionId: 0x%08" PRIX32
" UserName: [%s] Domain: [%s]",
1211 info->sessionId, info->username, info->domain);
1217 static BOOL rdp_recv_logon_plain_notify(rdpRdp* rdp,
wStream* s)
1220 if (!Stream_CheckAndLogRequiredLength(TAG, s, 576))
1223 Stream_Seek(s, 576);
1224 WLog_DBG(TAG,
"LogonPlainNotify");
1228 static BOOL rdp_recv_logon_error_info(rdpRdp* rdp,
wStream* s, logon_info_ex* info)
1230 freerdp* instance = NULL;
1231 UINT32 errorNotificationType = 0;
1232 UINT32 errorNotificationData = 0;
1235 WINPR_ASSERT(rdp->context);
1239 instance = rdp->context->instance;
1240 WINPR_ASSERT(instance);
1242 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1245 Stream_Read_UINT32(s, errorNotificationType);
1246 Stream_Read_UINT32(s, errorNotificationData);
1247 WLog_DBG(TAG,
"LogonErrorInfo: Data: 0x%08" PRIX32
" Type: 0x%08" PRIX32
"",
1248 errorNotificationData, errorNotificationType);
1249 IFCALL(instance->LogonErrorInfo, instance, errorNotificationData, errorNotificationType);
1250 info->ErrorNotificationType = errorNotificationType;
1251 info->ErrorNotificationData = errorNotificationData;
1255 static BOOL rdp_recv_logon_info_extended(rdpRdp* rdp,
wStream* s, logon_info_ex* info)
1257 UINT32 cbFieldData = 0;
1258 UINT32 fieldsPresent = 0;
1265 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
1267 WLog_WARN(TAG,
"received short logon info extended, need 6 bytes, got %" PRIuz,
1268 Stream_GetRemainingLength(s));
1272 Stream_Read_UINT16(s, Length);
1273 Stream_Read_UINT32(s, fieldsPresent);
1275 if ((Length < 6) || (!Stream_CheckAndLogRequiredLength(TAG, s, (Length - 6U))))
1278 "received short logon info extended, need %" PRIu16
" - 6 bytes, got %" PRIuz,
1279 Length, Stream_GetRemainingLength(s));
1283 WLog_DBG(TAG,
"LogonInfoExtended: fieldsPresent: 0x%08" PRIX32
"", fieldsPresent);
1287 if (fieldsPresent & LOGON_EX_AUTORECONNECTCOOKIE)
1289 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1292 info->haveCookie = TRUE;
1293 Stream_Read_UINT32(s, cbFieldData);
1295 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbFieldData))
1298 if (!rdp_read_server_auto_reconnect_cookie(rdp, s, info))
1302 if (fieldsPresent & LOGON_EX_LOGONERRORS)
1304 info->haveErrorInfo = TRUE;
1306 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1309 Stream_Read_UINT32(s, cbFieldData);
1311 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbFieldData))
1314 if (!rdp_recv_logon_error_info(rdp, s, info))
1318 if (!Stream_CheckAndLogRequiredLength(TAG, s, 570))
1321 Stream_Seek(s, 570);
1325 BOOL rdp_recv_save_session_info(rdpRdp* rdp,
wStream* s)
1327 UINT32 infoType = 0;
1329 logon_info logonInfo = { 0 };
1330 logon_info_ex logonInfoEx = { 0 };
1331 rdpContext* context = rdp->context;
1332 rdpUpdate* update = rdp->context->update;
1334 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1337 Stream_Read_UINT32(s, infoType);
1341 case INFO_TYPE_LOGON:
1342 status = rdp_recv_logon_info_v1(rdp, s, &logonInfo);
1344 if (status && update->SaveSessionInfo)
1345 status = update->SaveSessionInfo(context, infoType, &logonInfo);
1347 rdp_free_logon_info(&logonInfo);
1350 case INFO_TYPE_LOGON_LONG:
1351 status = rdp_recv_logon_info_v2(rdp, s, &logonInfo);
1353 if (status && update->SaveSessionInfo)
1354 status = update->SaveSessionInfo(context, infoType, &logonInfo);
1356 rdp_free_logon_info(&logonInfo);
1359 case INFO_TYPE_LOGON_PLAIN_NOTIFY:
1360 status = rdp_recv_logon_plain_notify(rdp, s);
1362 if (status && update->SaveSessionInfo)
1363 status = update->SaveSessionInfo(context, infoType, NULL);
1367 case INFO_TYPE_LOGON_EXTENDED_INF:
1368 status = rdp_recv_logon_info_extended(rdp, s, &logonInfoEx);
1370 if (status && update->SaveSessionInfo)
1371 status = update->SaveSessionInfo(context, infoType, &logonInfoEx);
1376 WLog_ERR(TAG,
"Unhandled saveSessionInfo type 0x%" PRIx32
"", infoType);
1383 WLog_DBG(TAG,
"SaveSessionInfo error: infoType: %s (%" PRIu32
")",
1384 infoType < 4 ? INFO_TYPE_LOGON_STRINGS[infoType % 4] :
"Unknown", infoType);
1390 static BOOL rdp_write_logon_info_v1(
wStream* s, logon_info* info)
1392 const size_t charLen = 52 /
sizeof(WCHAR);
1393 const size_t userCharLen = 512 /
sizeof(WCHAR);
1395 size_t sz = 4 + 52 + 4 + 512 + 4;
1397 if (!Stream_EnsureRemainingCapacity(s, sz))
1403 if (!info->domain || !info->username)
1405 const size_t len = strnlen(info->domain, charLen + 1);
1409 const size_t wlen = len *
sizeof(WCHAR);
1410 if (wlen > UINT32_MAX)
1413 Stream_Write_UINT32(s, (UINT32)wlen);
1414 if (Stream_Write_UTF16_String_From_UTF8(s, charLen, info->domain, len, TRUE) < 0)
1420 const size_t len = strnlen(info->username, userCharLen + 1);
1421 if (len > userCharLen)
1424 const size_t wlen = len *
sizeof(WCHAR);
1425 if (wlen > UINT32_MAX)
1428 Stream_Write_UINT32(s, (UINT32)wlen);
1430 if (Stream_Write_UTF16_String_From_UTF8(s, userCharLen, info->username, len, TRUE) < 0)
1435 Stream_Write_UINT32(s, info->sessionId);
1439 static BOOL rdp_write_logon_info_v2(
wStream* s, logon_info* info)
1441 size_t domainLen = 0;
1442 size_t usernameLen = 0;
1444 if (!Stream_EnsureRemainingCapacity(s, logonInfoV2TotalSize))
1447 Stream_Write_UINT16(s, SAVE_SESSION_PDU_VERSION_ONE);
1452 Stream_Write_UINT32(s, logonInfoV2Size);
1453 Stream_Write_UINT32(s, info->sessionId);
1454 domainLen = strnlen(info->domain, UINT32_MAX);
1455 if (domainLen >= UINT32_MAX /
sizeof(WCHAR))
1457 Stream_Write_UINT32(s, (UINT32)(domainLen + 1) *
sizeof(WCHAR));
1458 usernameLen = strnlen(info->username, UINT32_MAX);
1459 if (usernameLen >= UINT32_MAX /
sizeof(WCHAR))
1461 Stream_Write_UINT32(s, (UINT32)(usernameLen + 1) *
sizeof(WCHAR));
1462 Stream_Seek(s, logonInfoV2ReservedSize);
1463 if (Stream_Write_UTF16_String_From_UTF8(s, domainLen + 1, info->domain, domainLen, TRUE) < 0)
1465 if (Stream_Write_UTF16_String_From_UTF8(s, usernameLen + 1, info->username, usernameLen, TRUE) <
1471 static BOOL rdp_write_logon_info_plain(
wStream* s)
1473 if (!Stream_EnsureRemainingCapacity(s, 576))
1476 Stream_Seek(s, 576);
1480 static BOOL rdp_write_logon_info_ex(
wStream* s, logon_info_ex* info)
1482 UINT32 FieldsPresent = 0;
1483 UINT16 Size = 2 + 4 + 570;
1485 if (info->haveCookie)
1487 FieldsPresent |= LOGON_EX_AUTORECONNECTCOOKIE;
1491 if (info->haveErrorInfo)
1493 FieldsPresent |= LOGON_EX_LOGONERRORS;
1497 if (!Stream_EnsureRemainingCapacity(s, Size))
1500 Stream_Write_UINT16(s, Size);
1501 Stream_Write_UINT32(s, FieldsPresent);
1503 if (info->haveCookie)
1505 Stream_Write_UINT32(s, 28);
1506 Stream_Write_UINT32(s, 28);
1507 Stream_Write_UINT32(s, AUTO_RECONNECT_VERSION_1);
1508 Stream_Write_UINT32(s, info->LogonId);
1509 Stream_Write(s, info->ArcRandomBits, 16);
1512 if (info->haveErrorInfo)
1514 Stream_Write_UINT32(s, 8);
1515 Stream_Write_UINT32(s, info->ErrorNotificationType);
1516 Stream_Write_UINT32(s, info->ErrorNotificationData);
1519 Stream_Seek(s, 570);
1523 BOOL rdp_send_save_session_info(rdpContext* context, UINT32 type,
void* data)
1527 rdpRdp* rdp = context->rdp;
1528 s = rdp_data_pdu_init(rdp);
1533 Stream_Write_UINT32(s, type);
1537 case INFO_TYPE_LOGON:
1538 status = rdp_write_logon_info_v1(s, (logon_info*)data);
1541 case INFO_TYPE_LOGON_LONG:
1542 status = rdp_write_logon_info_v2(s, (logon_info*)data);
1545 case INFO_TYPE_LOGON_PLAIN_NOTIFY:
1546 status = rdp_write_logon_info_plain(s);
1549 case INFO_TYPE_LOGON_EXTENDED_INF:
1550 status = rdp_write_logon_info_ex(s, (logon_info_ex*)data);
1554 WLog_ERR(TAG,
"saveSessionInfo type 0x%" PRIx32
" not handled", type);
1560 status = rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_SAVE_SESSION_INFO, rdp->mcs->userId);
1567 BOOL rdp_send_server_status_info(rdpContext* context, UINT32 status)
1570 rdpRdp* rdp = context->rdp;
1571 s = rdp_data_pdu_init(rdp);
1576 Stream_Write_UINT32(s, status);
1577 return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_STATUS_INFO, rdp->mcs->userId);
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_set_string(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *param)
Sets a string settings value. The param is copied.
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API BOOL freerdp_settings_set_string_from_utf16N(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const WCHAR *param, size_t length)
Sets a string settings value. The param is converted to UTF-8 and the copy stored.
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
FREERDP_API WCHAR * freerdp_settings_get_string_as_utf16(const rdpSettings *settings, FreeRDP_Settings_Keys_String id, size_t *pCharLen)
Return an allocated UTF16 string.
FREERDP_API BOOL freerdp_settings_set_string_len(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *param, size_t len)
Sets a string settings value. The param is copied.
FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL param)
Sets a BOOL settings value.