24#include <freerdp/config.h>
28#include <freerdp/license.h>
31#include <winpr/assert.h>
32#include <winpr/crypto.h>
33#include <winpr/shell.h>
34#include <winpr/path.h>
35#include <winpr/file.h>
37#include <freerdp/log.h>
39#include <freerdp/crypto/certificate.h>
43#include "../crypto/crypto.h"
44#include "../crypto/certificate.h"
46#define LICENSE_TAG FREERDP_TAG("core.license")
53#define PLATFORM_CHALLENGE_RESPONSE_VERSION 0x0100
56enum LicenseRequestType
58 LICENSE_REQUEST = 0x01,
59 PLATFORM_CHALLENGE = 0x02,
61 UPGRADE_LICENSE = 0x04,
63 NEW_LICENSE_REQUEST = 0x13,
64 PLATFORM_CHALLENGE_RESPONSE = 0x15,
75#define LICENSE_PREAMBLE_LENGTH 4
79#define SERVER_RANDOM_LENGTH 32
80#define MASTER_SECRET_LENGTH 48
81#define PREMASTER_SECRET_LENGTH 48
82#define SESSION_KEY_BLOB_LENGTH 48
83#define MAC_SALT_KEY_LENGTH 16
84#define LICENSING_ENCRYPTION_KEY_LENGTH 16
85#define HWID_PLATFORM_ID_LENGTH 4
93#define PREAMBLE_VERSION_3_0 0x03
95#define EXTENDED_ERROR_MSG_SUPPORTED 0x80
100 BB_ANY_BLOB = 0x0000,
101 BB_DATA_BLOB = 0x0001,
102 BB_RANDOM_BLOB = 0x0002,
103 BB_CERTIFICATE_BLOB = 0x0003,
104 BB_ERROR_BLOB = 0x0004,
105 BB_ENCRYPTED_DATA_BLOB = 0x0009,
106 BB_KEY_EXCHG_ALG_BLOB = 0x000D,
107 BB_SCOPE_BLOB = 0x000E,
108 BB_CLIENT_USER_NAME_BLOB = 0x000F,
109 BB_CLIENT_MACHINE_NAME_BLOB = 0x0010
114#define KEY_EXCHANGE_ALG_RSA 0x00000001
120 ERR_INVALID_SERVER_CERTIFICATE = 0x00000001,
121 ERR_NO_LICENSE = 0x00000002,
122 ERR_INVALID_MAC = 0x00000003,
123 ERR_INVALID_SCOPE = 0x00000004,
124 ERR_NO_LICENSE_SERVER = 0x00000006,
125 STATUS_VALID_CLIENT = 0x00000007,
126 ERR_INVALID_CLIENT = 0x00000008,
127 ERR_INVALID_PRODUCT_ID = 0x0000000B,
128 ERR_INVALID_MESSAGE_LENGTH = 0x0000000C
135 ST_TOTAL_ABORT = 0x00000001,
136 ST_NO_TRANSITION = 0x00000002,
137 ST_RESET_PHASE_TO_START = 0x00000003,
138 ST_RESEND_LAST_MESSAGE = 0x00000004
145 WIN32_PLATFORM_CHALLENGE_TYPE = 0x0100,
146 WIN16_PLATFORM_CHALLENGE_TYPE = 0x0200,
147 WINCE_PLATFORM_CHALLENGE_TYPE = 0x0300,
148 OTHER_PLATFORM_CHALLENGE_TYPE = 0xFF00
155 LICENSE_DETAIL_SIMPLE = 0x0001,
156 LICENSE_DETAIL_MODERATE = 0x0002,
157 LICENSE_DETAIL_DETAIL = 0x0003
175 CLIENT_OS_ID_WINNT_351 = 0x01000000,
176 CLIENT_OS_ID_WINNT_40 = 0x02000000,
177 CLIENT_OS_ID_WINNT_50 = 0x03000000,
178 CLIENT_OS_ID_WINNT_POST_52 = 0x04000000,
180 CLIENT_IMAGE_ID_MICROSOFT = 0x00010000,
181 CLIENT_IMAGE_ID_CITRIX = 0x00020000,
189 rdpCertificate* certificate;
190 BYTE HardwareId[HWID_LENGTH];
191 BYTE ClientRandom[CLIENT_RANDOM_LENGTH];
192 BYTE ServerRandom[SERVER_RANDOM_LENGTH];
193 BYTE MasterSecret[MASTER_SECRET_LENGTH];
194 BYTE PremasterSecret[PREMASTER_SECRET_LENGTH];
195 BYTE SessionKeyBlob[SESSION_KEY_BLOB_LENGTH];
196 BYTE MacSaltKey[MAC_SALT_KEY_LENGTH];
197 BYTE LicensingEncryptionKey[LICENSING_ENCRYPTION_KEY_LENGTH];
212 BYTE MACData[LICENSING_ENCRYPTION_KEY_LENGTH];
214 UINT32 PacketHeaderLength;
215 UINT32 PreferredKeyExchangeAlg;
218 UINT16 LicenseDetailLevel;
224static BOOL license_send_error_alert(rdpLicense* license, UINT32 dwErrorCode,
227static void license_set_state(rdpLicense* license, LICENSE_STATE state);
230static const char* license_get_state_string(LICENSE_STATE state);
233static const char* license_preferred_key_exchange_alg_string(UINT32 alg,
char* buffer,
size_t size)
235 const char* name =
nullptr;
239 case KEY_EXCHANGE_ALG_RSA:
240 name =
"KEY_EXCHANGE_ALG_RSA";
243 name =
"KEY_EXCHANGE_ALG_UNKNOWN";
247 (void)_snprintf(buffer, size,
"%s [0x%08" PRIx32
"]", name, alg);
252static const char* license_request_type_string(UINT32 type)
256 case LICENSE_REQUEST:
257 return "LICENSE_REQUEST";
258 case PLATFORM_CHALLENGE:
259 return "PLATFORM_CHALLENGE";
261 return "NEW_LICENSE";
262 case UPGRADE_LICENSE:
263 return "UPGRADE_LICENSE";
265 return "LICENSE_INFO";
266 case NEW_LICENSE_REQUEST:
267 return "NEW_LICENSE_REQUEST";
268 case PLATFORM_CHALLENGE_RESPONSE:
269 return "PLATFORM_CHALLENGE_RESPONSE";
271 return "ERROR_ALERT";
273 return "LICENSE_REQUEST_TYPE_UNKNOWN";
278static const char* licencse_blob_type_string(UINT16 type)
283 return "BB_ANY_BLOB";
285 return "BB_DATA_BLOB";
287 return "BB_RANDOM_BLOB";
288 case BB_CERTIFICATE_BLOB:
289 return "BB_CERTIFICATE_BLOB";
291 return "BB_ERROR_BLOB";
292 case BB_ENCRYPTED_DATA_BLOB:
293 return "BB_ENCRYPTED_DATA_BLOB";
294 case BB_KEY_EXCHG_ALG_BLOB:
295 return "BB_KEY_EXCHG_ALG_BLOB";
297 return "BB_SCOPE_BLOB";
298 case BB_CLIENT_USER_NAME_BLOB:
299 return "BB_CLIENT_USER_NAME_BLOB";
300 case BB_CLIENT_MACHINE_NAME_BLOB:
301 return "BB_CLIENT_MACHINE_NAME_BLOB";
308static wStream* license_send_stream_init(rdpLicense* license, UINT16* sec_flags);
311static BOOL license_generate_randoms(rdpLicense* license);
314static BOOL license_generate_keys(rdpLicense* license);
317static BOOL license_generate_hwid(rdpLicense* license);
320static BOOL license_encrypt_premaster_secret(rdpLicense* license);
324WINPR_ATTR_MALLOC(license_free_product_info, 1)
330static
void license_free_binary_blob(
LICENSE_BLOB* blob);
332WINPR_ATTR_MALLOC(license_free_binary_blob, 1)
333static
LICENSE_BLOB* license_new_binary_blob(UINT16 type);
336static BOOL license_read_binary_blob_data(wLog* log,
LICENSE_BLOB* blob, UINT16 type,
337 const
void* data,
size_t length);
345static
void license_free_scope_list(
SCOPE_LIST* scopeList);
347WINPR_ATTR_MALLOC(license_free_scope_list, 1)
348static
SCOPE_LIST* license_new_scope_list(
void);
351static BOOL license_scope_list_resize(
SCOPE_LIST* scopeList, UINT32 count);
354static BOOL license_read_scope_list(wLog* log,
wStream* s,
SCOPE_LIST* scopeList);
357static BOOL license_write_scope_list(wLog* log,
wStream* s, const
SCOPE_LIST* scopeList);
360static BOOL license_read_license_request_packet(rdpLicense* license,
wStream* s);
363static BOOL license_write_license_request_packet(const rdpLicense* license,
wStream* s);
366static BOOL license_read_platform_challenge_packet(rdpLicense* license,
wStream* s);
369static BOOL license_send_platform_challenge_packet(rdpLicense* license);
372static BOOL license_read_new_or_upgrade_license_packet(rdpLicense* license,
wStream* s);
375static BOOL license_read_error_alert_packet(rdpLicense* license,
wStream* s);
378static BOOL license_write_new_license_request_packet(const rdpLicense* license,
wStream* s);
381static BOOL license_read_new_license_request_packet(rdpLicense* license,
wStream* s);
384static BOOL license_answer_license_request(rdpLicense* license);
387static BOOL license_send_platform_challenge_response(rdpLicense* license);
390static BOOL license_read_platform_challenge_response(rdpLicense* license);
393static BOOL license_read_client_platform_challenge_response(rdpLicense* license,
wStream* s);
396static BOOL license_write_client_platform_challenge_response(rdpLicense* license,
wStream* s);
399static BOOL license_write_server_upgrade_license(const rdpLicense* license,
wStream* s);
402static BOOL license_send_license_info(rdpLicense* license, const
LICENSE_BLOB* calBlob,
403 const BYTE* signature,
size_t signature_length);
406static BOOL license_read_license_info(rdpLicense* license,
wStream* s);
409static state_run_t license_client_recv(rdpLicense* license,
wStream* s);
412static state_run_t license_server_recv(rdpLicense* license,
wStream* s);
414#define PLATFORMID (CLIENT_OS_ID_WINNT_POST_52 | CLIENT_IMAGE_ID_MICROSOFT)
416#ifdef WITH_DEBUG_LICENSE
418static const char* error_codes[] = {
"ERR_UNKNOWN",
419 "ERR_INVALID_SERVER_CERTIFICATE",
424 "ERR_NO_LICENSE_SERVER",
425 "STATUS_VALID_CLIENT",
426 "ERR_INVALID_CLIENT",
429 "ERR_INVALID_PRODUCT_ID",
430 "ERR_INVALID_MESSAGE_LENGTH" };
432static const char* state_transitions[] = {
"ST_UNKNOWN",
"ST_TOTAL_ABORT",
"ST_NO_TRANSITION",
433 "ST_RESET_PHASE_TO_START",
"ST_RESEND_LAST_MESSAGE" };
437 char* CompanyName =
nullptr;
438 char* ProductId =
nullptr;
440 WINPR_ASSERT(productInfo);
441 WINPR_ASSERT(productInfo->pbCompanyName);
442 WINPR_ASSERT(productInfo->pbProductId);
444 CompanyName = ConvertWCharNToUtf8Alloc((
const WCHAR*)productInfo->pbCompanyName,
445 productInfo->cbCompanyName /
sizeof(WCHAR),
nullptr);
446 ProductId = ConvertWCharNToUtf8Alloc((
const WCHAR*)productInfo->pbProductId,
447 productInfo->cbProductId /
sizeof(WCHAR),
nullptr);
448 WLog_Print(log, WLOG_INFO,
"ProductInfo:");
449 WLog_Print(log, WLOG_INFO,
"\tdwVersion: 0x%08" PRIX32
"", productInfo->dwVersion);
450 WLog_Print(log, WLOG_INFO,
"\tCompanyName: %s", CompanyName);
451 WLog_Print(log, WLOG_INFO,
"\tProductId: %s", ProductId);
456static void license_print_scope_list(wLog* log,
const SCOPE_LIST* scopeList)
458 WINPR_ASSERT(scopeList);
460 WLog_Print(log, WLOG_INFO,
"ScopeList (%" PRIu32
"):", scopeList->count);
462 for (UINT32 index = 0; index < scopeList->count; index++)
466 WINPR_ASSERT(scopeList->array);
467 scope = scopeList->array[index];
470 WLog_Print(log, WLOG_INFO,
"\t%s", (
const char*)scope->data);
475static const char licenseStore[] =
"licenses";
478static BOOL license_ensure_state(rdpLicense* license, LICENSE_STATE state, UINT32 msg)
480 const LICENSE_STATE cstate = license_get_state(license);
482 WINPR_ASSERT(license);
486 const char* scstate = license_get_state_string(cstate);
487 const char* sstate = license_get_state_string(state);
488 const char* where = license_request_type_string(msg);
490 WLog_Print(license->log, WLOG_WARN,
491 "Received [%s], but found invalid licensing state %s, expected %s", where,
498state_run_t license_recv(rdpLicense* license,
wStream* s)
500 WINPR_ASSERT(license);
501 WINPR_ASSERT(license->rdp);
502 WINPR_ASSERT(license->rdp->settings);
505 return license_server_recv(license, s);
507 return license_client_recv(license, s);
511static BOOL license_check_stream_length(wLog* log,
wStream* s, SSIZE_T expect,
const char* where)
513 const size_t remain = Stream_GetRemainingLength(s);
519 WLog_Print(log, WLOG_WARN,
"invalid %s, expected value %" PRIdz
" invalid", where, expect);
522 if (remain < (
size_t)expect)
524 WLog_Print(log, WLOG_WARN,
"short %s, expected %" PRIdz
" bytes, got %" PRIuz, where,
532static BOOL license_check_stream_capacity(wLog* log,
wStream* s,
size_t expect,
const char* where)
536 return (Stream_CheckAndLogRequiredCapacityWLogEx(log, WLOG_WARN, s, expect, 1,
537 "%s(%s:%" PRIuz
") %s", __func__, __FILE__,
538 (
size_t)__LINE__, where));
542static BOOL computeCalHash(wLog* log,
const char* hostname,
char* hashStr,
size_t len)
544 WINPR_DIGEST_CTX* sha1 =
nullptr;
546 BYTE hash[20] = WINPR_C_ARRAY_INIT;
548 WINPR_ASSERT(hostname);
549 WINPR_ASSERT(hashStr);
551 if (len < 2 *
sizeof(hash) + 1)
554 if (!(sha1 = winpr_Digest_New()))
556 if (!winpr_Digest_Init(sha1, WINPR_MD_SHA1))
558 if (!winpr_Digest_Update(sha1, (
const BYTE*)hostname, strlen(hostname)))
560 if (!winpr_Digest_Final(sha1, hash,
sizeof(hash)))
563 for (
size_t i = 0; i <
sizeof(hash); i++, hashStr += 2)
564 (
void)sprintf_s(hashStr, 3,
"%.2x", hash[i]);
569 WLog_Print(log, WLOG_ERROR,
"failed to generate SHA1 of hostname '%s'", hostname);
570 winpr_Digest_Free(sha1);
575static BOOL saveCal(wLog* log,
const rdpSettings* settings,
const BYTE* data,
size_t length,
576 const char* hostname)
578 char hash[41] = WINPR_C_ARRAY_INIT;
580 char* licenseStorePath =
nullptr;
581 char filename[MAX_PATH] = WINPR_C_ARRAY_INIT;
582 char filenameNew[MAX_PATH] = WINPR_C_ARRAY_INIT;
583 char* filepath =
nullptr;
584 char* filepathNew =
nullptr;
591 WINPR_ASSERT(data || (length == 0));
592 WINPR_ASSERT(hostname);
594 if (!winpr_PathFileExists(path))
596 if (!winpr_PathMakePath(path,
nullptr))
598 WLog_Print(log, WLOG_ERROR,
"error creating directory '%s'", path);
601 WLog_Print(log, WLOG_INFO,
"creating directory %s", path);
604 if (!(licenseStorePath = GetCombinedPath(path, licenseStore)))
606 WLog_Print(log, WLOG_ERROR,
"Failed to get license store path from '%s' + '%s'", path,
611 if (!winpr_PathFileExists(licenseStorePath))
613 if (!winpr_PathMakePath(licenseStorePath,
nullptr))
615 WLog_Print(log, WLOG_ERROR,
"error creating directory '%s'", licenseStorePath);
618 WLog_Print(log, WLOG_INFO,
"creating directory %s", licenseStorePath);
621 if (!computeCalHash(log, hostname, hash,
sizeof(hash)))
623 (void)sprintf_s(filename,
sizeof(filename) - 1,
"%s.cal", hash);
624 (void)sprintf_s(filenameNew,
sizeof(filenameNew) - 1,
"%s.cal.new", hash);
626 if (!(filepath = GetCombinedPath(licenseStorePath, filename)))
628 WLog_Print(log, WLOG_ERROR,
"Failed to get license file path from '%s' + '%s'", path,
633 if (!(filepathNew = GetCombinedPath(licenseStorePath, filenameNew)))
635 WLog_Print(log, WLOG_ERROR,
"Failed to get license new file path from '%s' + '%s'", path,
640 fp = winpr_fopen(filepathNew,
"wb");
643 WLog_Print(log, WLOG_ERROR,
"Failed to open license file '%s'", filepathNew);
647 written = fwrite(data, length, 1, fp);
652 WLog_Print(log, WLOG_ERROR,
"Failed to write to license file '%s'", filepathNew);
653 winpr_DeleteFile(filepathNew);
657 ret = winpr_MoveFileEx(filepathNew, filepath, MOVEFILE_REPLACE_EXISTING);
659 WLog_Print(log, WLOG_ERROR,
"Failed to move license file '%s' to '%s'", filepathNew,
665 free(licenseStorePath);
669WINPR_ATTR_MALLOC(free, 1)
670static BYTE* loadCalFile(wLog* log, const rdpSettings* settings, const
char* hostname,
673 char* licenseStorePath =
nullptr;
674 char* calPath =
nullptr;
675 char calFilename[MAX_PATH] = WINPR_C_ARRAY_INIT;
676 char hash[41] = WINPR_C_ARRAY_INIT;
682 WINPR_ASSERT(settings);
683 WINPR_ASSERT(hostname);
684 WINPR_ASSERT(dataLen);
686 if (!computeCalHash(log, hostname, hash,
sizeof(hash)))
688 WLog_Print(log, WLOG_ERROR,
"loadCalFile: unable to compute hostname hash");
692 (void)sprintf_s(calFilename,
sizeof(calFilename) - 1,
"%s.cal", hash);
694 if (!(licenseStorePath = GetCombinedPath(
698 if (!(calPath = GetCombinedPath(licenseStorePath, calFilename)))
701 fp = winpr_fopen(calPath,
"rb");
705 if (_fseeki64(fp, 0, SEEK_END) != 0)
707 length = _ftelli64(fp);
708 if (_fseeki64(fp, 0, SEEK_SET) != 0)
713 ret = (BYTE*)malloc((
size_t)length);
717 status = fread(ret, (
size_t)length, 1, fp);
721 *dataLen = (size_t)length;
725 free(licenseStorePath);
735 free(licenseStorePath);
749static BOOL license_read_preamble(wLog* log,
wStream* s, BYTE* bMsgType, BYTE* flags,
752 WINPR_ASSERT(bMsgType);
754 WINPR_ASSERT(wMsgSize);
757 if (!license_check_stream_length(log, s, 4,
"license preamble"))
760 Stream_Read_UINT8(s, *bMsgType);
761 Stream_Read_UINT8(s, *flags);
762 Stream_Read_UINT16(s, *wMsgSize);
763 return license_check_stream_length(log, s, *wMsgSize - 4ll,
"license preamble::wMsgSize");
776static BOOL license_write_preamble(
wStream* s, BYTE bMsgType, BYTE flags, UINT16 wMsgSize)
778 if (!Stream_EnsureRemainingCapacity(s, 4))
782 Stream_Write_UINT8(s, bMsgType);
783 Stream_Write_UINT8(s, flags);
784 Stream_Write_UINT16(s, wMsgSize);
796wStream* license_send_stream_init(rdpLicense* license, UINT16* sec_flags)
798 WINPR_ASSERT(license);
799 WINPR_ASSERT(license->rdp);
800 WINPR_ASSERT(sec_flags);
802 const BOOL do_crypt = license->rdp->do_crypt;
804 *sec_flags = SEC_LICENSE_PKT;
816 *sec_flags |= SEC_LICENSE_ENCRYPT_CS;
817 license->rdp->do_crypt = license->rdp->do_crypt_license;
820 wStream* s = rdp_send_stream_init(license->rdp, sec_flags);
824 license->rdp->do_crypt = do_crypt;
825 license->PacketHeaderLength = (UINT16)Stream_GetPosition(s);
826 if (!Stream_SafeSeek(s, LICENSE_PREAMBLE_LENGTH))
842static BOOL license_send(rdpLicense* license,
wStream* s, BYTE type, UINT16 sec_flags)
844 WINPR_ASSERT(license);
845 WINPR_ASSERT(license->rdp);
847 rdpRdp* rdp = license->rdp;
848 WINPR_ASSERT(rdp->settings);
850 DEBUG_LICENSE(
"Sending %s Packet", license_request_type_string(type));
851 const size_t length = Stream_GetPosition(s);
852 WINPR_ASSERT(length >= license->PacketHeaderLength);
853 WINPR_ASSERT(length <= UINT16_MAX + license->PacketHeaderLength);
855 const UINT16 wMsgSize = (UINT16)(length - license->PacketHeaderLength);
856 if (!Stream_SetPosition(s, license->PacketHeaderLength))
858 BYTE flags = PREAMBLE_VERSION_3_0;
865 if (!rdp->settings->ServerMode)
866 flags |= EXTENDED_ERROR_MSG_SUPPORTED;
868 if (!license_write_preamble(s, type, flags, wMsgSize))
874#ifdef WITH_DEBUG_LICENSE
875 WLog_Print(license->log, WLOG_DEBUG,
"Sending %s Packet, length %" PRIu16
"",
876 license_request_type_string(type), wMsgSize);
877 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_PointerAs(s,
char) - LICENSE_PREAMBLE_LENGTH,
880 if (!Stream_SetPosition(s, length))
882 const BOOL ret = rdp_send(rdp, s, MCS_GLOBAL_CHANNEL_ID, sec_flags);
886BOOL license_write_server_upgrade_license(
const rdpLicense* license,
wStream* s)
888 WINPR_ASSERT(license);
890 if (!license_write_binary_blob(s, license->EncryptedLicenseInfo))
892 if (!license_check_stream_capacity(license->log, s,
sizeof(license->MACData),
893 "SERVER_UPGRADE_LICENSE::MACData"))
895 Stream_Write(s, license->MACData,
sizeof(license->MACData));
900static BOOL license_server_send_new_or_upgrade_license(rdpLicense* license, BOOL upgrade)
902 UINT16 sec_flags = 0;
903 wStream* s = license_send_stream_init(license, &sec_flags);
904 const BYTE type = upgrade ? UPGRADE_LICENSE : NEW_LICENSE;
909 if (!license_write_server_upgrade_license(license, s))
912 return license_send(license, s, type, sec_flags);
927static state_run_t license_client_recv_int(rdpLicense* license,
wStream* s)
932 const size_t length = Stream_GetRemainingLength(s);
934 WINPR_ASSERT(license);
936 if (!license_read_preamble(license->log, s, &bMsgType, &flags,
938 return STATE_RUN_FAILED;
940 DEBUG_LICENSE(
"Receiving %s Packet", license_request_type_string(bMsgType));
944 case LICENSE_REQUEST:
946 if (license_get_state(license) == LICENSE_STATE_INITIAL)
947 license_set_state(license, LICENSE_STATE_CONFIGURED);
949 if (!license_ensure_state(license, LICENSE_STATE_CONFIGURED, bMsgType))
950 return STATE_RUN_FAILED;
952 if (!license_read_license_request_packet(license, s))
953 return STATE_RUN_FAILED;
955 if (!license_answer_license_request(license))
956 return STATE_RUN_FAILED;
958 license_set_state(license, LICENSE_STATE_NEW_REQUEST);
961 case PLATFORM_CHALLENGE:
962 if (!license_ensure_state(license, LICENSE_STATE_NEW_REQUEST, bMsgType))
963 return STATE_RUN_FAILED;
965 if (!license_read_platform_challenge_packet(license, s))
966 return STATE_RUN_FAILED;
968 if (!license_send_platform_challenge_response(license))
969 return STATE_RUN_FAILED;
970 license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE);
974 case UPGRADE_LICENSE:
975 if (!license_ensure_state(license, LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE, bMsgType))
976 return STATE_RUN_FAILED;
977 if (!license_read_new_or_upgrade_license_packet(license, s))
978 return STATE_RUN_FAILED;
982 if (!license_read_error_alert_packet(license, s))
983 return STATE_RUN_FAILED;
987 WLog_Print(license->log, WLOG_ERROR,
"invalid bMsgType:%" PRIu8
"", bMsgType);
988 return STATE_RUN_FAILED;
991 if (!tpkt_ensure_stream_consumed(license->log, s, length))
992 return STATE_RUN_FAILED;
993 return STATE_RUN_SUCCESS;
996state_run_t license_client_recv(rdpLicense* license,
wStream* s)
998 state_run_t rc = license_client_recv_int(license, s);
999 if (state_run_failed(rc))
1001 freerdp_set_last_error(license->rdp->context, ERROR_CTX_LICENSE_CLIENT_INVALID);
1006state_run_t license_server_recv(rdpLicense* license,
wStream* s)
1008 state_run_t rc = STATE_RUN_FAILED;
1011 UINT16 wMsgSize = 0;
1012 const size_t length = Stream_GetRemainingLength(s);
1014 WINPR_ASSERT(license);
1016 if (!license_read_preamble(license->log, s, &bMsgType, &flags,
1020 DEBUG_LICENSE(
"Receiving %s Packet", license_request_type_string(bMsgType));
1024 case NEW_LICENSE_REQUEST:
1025 if (!license_ensure_state(license, LICENSE_STATE_REQUEST, bMsgType))
1027 if (!license_read_new_license_request_packet(license, s))
1030 if (!license_send_error_alert(license, ERR_INVALID_MAC, ST_TOTAL_ABORT,
1031 license->ErrorInfo))
1033 if (!license_send_platform_challenge_packet(license))
1035 license->update = FALSE;
1036 license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE);
1039 if (!license_ensure_state(license, LICENSE_STATE_REQUEST, bMsgType))
1041 if (!license_read_license_info(license, s))
1044 if (!license_send_platform_challenge_packet(license))
1046 license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE);
1047 license->update = TRUE;
1050 case PLATFORM_CHALLENGE_RESPONSE:
1051 if (!license_ensure_state(license, LICENSE_STATE_PLATFORM_CHALLENGE, bMsgType))
1053 if (!license_read_client_platform_challenge_response(license, s))
1059 if (license_send_error_alert(license, ERR_INVALID_CLIENT, ST_TOTAL_ABORT,
1060 license->ErrorInfo))
1065 if (!license_server_send_new_or_upgrade_license(license, license->update))
1068 license->type = LICENSE_TYPE_ISSUED;
1069 license_set_state(license, LICENSE_STATE_COMPLETED);
1071 rc = STATE_RUN_CONTINUE;
1076 if (!license_read_error_alert_packet(license, s))
1081 WLog_Print(license->log, WLOG_ERROR,
"invalid bMsgType:%" PRIu8
"", bMsgType);
1085 if (!tpkt_ensure_stream_consumed(license->log, s, length))
1088 if (!state_run_success(rc))
1089 rc = STATE_RUN_SUCCESS;
1092 if (state_run_failed(rc))
1094 if (flags & EXTENDED_ERROR_MSG_SUPPORTED)
1096 if (!license_send_error_alert(license, ERR_INVALID_CLIENT, ST_TOTAL_ABORT,
nullptr))
1098 WLog_Print(license->log, WLOG_ERROR,
"license_send_error_alert failed");
1101 license_set_state(license, LICENSE_STATE_ABORTED);
1107BOOL license_generate_randoms(rdpLicense* license)
1109 WINPR_ASSERT(license);
1111#ifdef LICENSE_NULL_CLIENT_RANDOM
1112 ZeroMemory(license->ClientRandom,
sizeof(license->ClientRandom));
1114 if (winpr_RAND(license->ClientRandom,
sizeof(license->ClientRandom)) < 0)
1118 if (winpr_RAND(license->ServerRandom,
sizeof(license->ServerRandom)) < 0)
1121#ifdef LICENSE_NULL_PREMASTER_SECRET
1122 ZeroMemory(license->PremasterSecret,
sizeof(license->PremasterSecret));
1124 if (winpr_RAND(license->PremasterSecret,
sizeof(license->PremasterSecret)) <
1136static BOOL license_generate_keys(rdpLicense* license)
1138 WINPR_ASSERT(license);
1142 !security_master_secret(license->PremasterSecret,
sizeof(license->PremasterSecret),
1143 license->ClientRandom,
sizeof(license->ClientRandom),
1144 license->ServerRandom,
sizeof(license->ServerRandom),
1145 license->MasterSecret,
sizeof(license->MasterSecret)) ||
1147 !security_session_key_blob(license->MasterSecret,
sizeof(license->MasterSecret),
1148 license->ClientRandom,
sizeof(license->ClientRandom),
1149 license->ServerRandom,
sizeof(license->ServerRandom),
1150 license->SessionKeyBlob,
sizeof(license->SessionKeyBlob)))
1154 security_mac_salt_key(license->SessionKeyBlob,
sizeof(license->SessionKeyBlob),
1155 license->ClientRandom,
sizeof(license->ClientRandom),
1156 license->ServerRandom,
sizeof(license->ServerRandom), license->MacSaltKey,
1157 sizeof(license->MacSaltKey));
1158 const BOOL ret = security_licensing_encryption_key(
1159 license->SessionKeyBlob,
sizeof(license->SessionKeyBlob), license->ClientRandom,
1160 sizeof(license->ClientRandom), license->ServerRandom,
sizeof(license->ServerRandom),
1161 license->LicensingEncryptionKey,
1162 sizeof(license->LicensingEncryptionKey));
1164 WLog_Print(license->log, WLOG_TRACE,
"license keys %s generated", ret ?
"successfully" :
"NOT");
1166#ifdef WITH_DEBUG_LICENSE
1167 WLog_Print(license->log, WLOG_DEBUG,
"ClientRandom:");
1168 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ClientRandom,
1169 sizeof(license->ClientRandom));
1170 WLog_Print(license->log, WLOG_DEBUG,
"ServerRandom:");
1171 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ServerRandom,
1172 sizeof(license->ServerRandom));
1173 WLog_Print(license->log, WLOG_DEBUG,
"PremasterSecret:");
1174 winpr_HexLogDump(license->log, WLOG_DEBUG, license->PremasterSecret,
1175 sizeof(license->PremasterSecret));
1176 WLog_Print(license->log, WLOG_DEBUG,
"MasterSecret:");
1177 winpr_HexLogDump(license->log, WLOG_DEBUG, license->MasterSecret,
1178 sizeof(license->MasterSecret));
1179 WLog_Print(license->log, WLOG_DEBUG,
"SessionKeyBlob:");
1180 winpr_HexLogDump(license->log, WLOG_DEBUG, license->SessionKeyBlob,
1181 sizeof(license->SessionKeyBlob));
1182 WLog_Print(license->log, WLOG_DEBUG,
"MacSaltKey:");
1183 winpr_HexLogDump(license->log, WLOG_DEBUG, license->MacSaltKey,
sizeof(license->MacSaltKey));
1184 WLog_Print(license->log, WLOG_DEBUG,
"LicensingEncryptionKey:");
1185 winpr_HexLogDump(license->log, WLOG_DEBUG, license->LicensingEncryptionKey,
1186 sizeof(license->LicensingEncryptionKey));
1196BOOL license_generate_hwid(rdpLicense* license)
1198 const BYTE* hashTarget =
nullptr;
1199 size_t targetLen = 0;
1200 BYTE macAddress[6] = WINPR_C_ARRAY_INIT;
1202 WINPR_ASSERT(license);
1203 WINPR_ASSERT(license->rdp);
1204 WINPR_ASSERT(license->rdp->settings);
1206 ZeroMemory(license->HardwareId,
sizeof(license->HardwareId));
1208 if (license->rdp->settings->OldLicenseBehaviour)
1210 hashTarget = macAddress;
1211 targetLen =
sizeof(macAddress);
1215 wStream buffer = WINPR_C_ARRAY_INIT;
1216 const char* hostname = license->rdp->settings->ClientHostname;
1217 wStream* s = Stream_StaticInit(&buffer, license->HardwareId, 4);
1218 Stream_Write_UINT32(s, license->PlatformId);
1220 hashTarget = (
const BYTE*)hostname;
1221 targetLen = hostname ? strlen(hostname) : 0;
1230 return winpr_Digest_Allow_FIPS(WINPR_MD_MD5, hashTarget, targetLen,
1231 &license->HardwareId[HWID_PLATFORM_ID_LENGTH],
1232 WINPR_MD5_DIGEST_LENGTH);
1236static BOOL license_get_server_rsa_public_key(rdpLicense* license)
1238 rdpSettings* settings =
nullptr;
1240 WINPR_ASSERT(license);
1241 WINPR_ASSERT(license->certificate);
1242 WINPR_ASSERT(license->rdp);
1244 settings = license->rdp->settings;
1245 WINPR_ASSERT(settings);
1247 if (license->ServerCertificate->length < 1)
1249 if (!freerdp_certificate_read_server_cert(license->certificate, settings->ServerCertificate,
1250 settings->ServerCertificateLength))
1257BOOL license_encrypt_premaster_secret(rdpLicense* license)
1259 WINPR_ASSERT(license);
1260 WINPR_ASSERT(license->certificate);
1262 if (!license_get_server_rsa_public_key(license))
1265 WINPR_ASSERT(license->EncryptedPremasterSecret);
1267 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1270 WLog_Print(license->log, WLOG_ERROR,
"info=%p, license->certificate=%p", (
const void*)info,
1271 (
void*)license->certificate);
1275#ifdef WITH_DEBUG_LICENSE
1276 WLog_Print(license->log, WLOG_DEBUG,
"Modulus (%" PRIu32
" bits):", info->ModulusLength * 8);
1277 winpr_HexLogDump(license->log, WLOG_DEBUG, info->Modulus, info->ModulusLength);
1278 WLog_Print(license->log, WLOG_DEBUG,
"Exponent:");
1279 winpr_HexLogDump(license->log, WLOG_DEBUG, info->exponent,
sizeof(info->exponent));
1282 BYTE* EncryptedPremasterSecret = (BYTE*)calloc(1, info->ModulusLength);
1283 if (!EncryptedPremasterSecret)
1285 WLog_Print(license->log, WLOG_ERROR,
1286 "EncryptedPremasterSecret=%p, info->ModulusLength=%" PRIu32,
1287 (
const void*)EncryptedPremasterSecret, info->ModulusLength);
1291 license->EncryptedPremasterSecret->type = BB_RANDOM_BLOB;
1292 license->EncryptedPremasterSecret->length =
sizeof(license->PremasterSecret);
1293#ifndef LICENSE_NULL_PREMASTER_SECRET
1295 const SSIZE_T length =
1296 crypto_rsa_public_encrypt(license->PremasterSecret,
sizeof(license->PremasterSecret),
1297 info, EncryptedPremasterSecret, info->ModulusLength);
1298 if ((length < 0) || (length > UINT16_MAX))
1300 WLog_Print(license->log, WLOG_ERROR,
"RSA public encrypt length=%" PRIdz
" < 0 || > %d",
1301 length, UINT16_MAX);
1304 license->EncryptedPremasterSecret->length = (UINT16)length;
1307 license->EncryptedPremasterSecret->data = EncryptedPremasterSecret;
1312static BOOL license_rc4_with_licenseKey(
const rdpLicense* license,
const BYTE* input,
size_t len,
1315 WINPR_ASSERT(license);
1316 WINPR_ASSERT(input || (len == 0));
1317 WINPR_ASSERT(target);
1318 WINPR_ASSERT(len <= UINT16_MAX);
1320 WINPR_RC4_CTX* rc4 = winpr_RC4_New_Allow_FIPS(license->LicensingEncryptionKey,
1321 sizeof(license->LicensingEncryptionKey));
1324 WLog_Print(license->log, WLOG_ERROR,
"Failed to allocate RC4");
1328 BYTE* buffer =
nullptr;
1330 buffer = realloc(target->data, len);
1334 target->data = buffer;
1335 target->length = (UINT16)len;
1337 if (!winpr_RC4_Update(rc4, len, input, buffer))
1340 winpr_RC4_Free(rc4);
1344 WLog_Print(license->log, WLOG_ERROR,
"Failed to create/update RC4: len=%" PRIuz
", buffer=%p",
1345 len, (
const void*)buffer);
1346 winpr_RC4_Free(rc4);
1362static BOOL license_encrypt_and_MAC(rdpLicense* license,
const BYTE* input,
size_t len,
1365 WINPR_ASSERT(license);
1366 return license_rc4_with_licenseKey(license, input, len, target) &&
1367 security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), input, len, mac,
1383static BOOL license_decrypt_and_check_MAC(rdpLicense* license,
const BYTE* input,
size_t len,
1386 BYTE macData[
sizeof(license->MACData)] = WINPR_C_ARRAY_INIT;
1388 WINPR_ASSERT(license);
1389 WINPR_ASSERT(target);
1393 WLog_Print(license->log, WLOG_DEBUG,
"TransportDumpReplay active, skipping...");
1397 if (!license_rc4_with_licenseKey(license, input, len, target))
1400 if (!security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), target->data, len,
1401 macData,
sizeof(macData)))
1404 if (memcmp(packetMac, macData,
sizeof(macData)) != 0)
1406 WLog_Print(license->log, WLOG_ERROR,
"packetMac != expectedMac");
1421 WINPR_ASSERT(productInfo);
1423 if (!license_check_stream_length(log, s, 8,
"license product info::cbCompanyName"))
1426 Stream_Read_UINT32(s, productInfo->dwVersion);
1427 Stream_Read_UINT32(s, productInfo->cbCompanyName);
1430 if ((productInfo->cbCompanyName < 2) || (productInfo->cbCompanyName % 2 != 0))
1432 WLog_Print(log, WLOG_WARN,
"license product info invalid cbCompanyName %" PRIu32,
1433 productInfo->cbCompanyName);
1437 if (!license_check_stream_length(log, s, productInfo->cbCompanyName,
1438 "license product info::CompanyName"))
1441 productInfo->pbProductId =
nullptr;
1442 productInfo->pbCompanyName = (BYTE*)malloc(productInfo->cbCompanyName);
1443 if (!productInfo->pbCompanyName)
1445 Stream_Read(s, productInfo->pbCompanyName, productInfo->cbCompanyName);
1447 if (!license_check_stream_length(log, s, 4,
"license product info::cbProductId"))
1450 Stream_Read_UINT32(s, productInfo->cbProductId);
1452 if ((productInfo->cbProductId < 2) || (productInfo->cbProductId % 2 != 0))
1454 WLog_Print(log, WLOG_WARN,
"license product info invalid cbProductId %" PRIu32,
1455 productInfo->cbProductId);
1459 if (!license_check_stream_length(log, s, productInfo->cbProductId,
1460 "license product info::ProductId"))
1463 productInfo->pbProductId = (BYTE*)malloc(productInfo->cbProductId);
1464 if (!productInfo->pbProductId)
1466 Stream_Read(s, productInfo->pbProductId, productInfo->cbProductId);
1470 free(productInfo->pbCompanyName);
1471 free(productInfo->pbProductId);
1472 productInfo->pbCompanyName =
nullptr;
1473 productInfo->pbProductId =
nullptr;
1478static BOOL license_write_product_info(wLog* log,
wStream* s,
1481 WINPR_ASSERT(productInfo);
1483 if (!license_check_stream_capacity(log, s, 8,
"license product info::cbCompanyName"))
1486 Stream_Write_UINT32(s, productInfo->dwVersion);
1487 Stream_Write_UINT32(s, productInfo->cbCompanyName);
1490 if ((productInfo->cbCompanyName < 2) || (productInfo->cbCompanyName % 2 != 0) ||
1491 !productInfo->pbCompanyName)
1493 WLog_Print(log, WLOG_WARN,
"license product info invalid cbCompanyName %" PRIu32,
1494 productInfo->cbCompanyName);
1498 if (!license_check_stream_capacity(log, s, productInfo->cbCompanyName,
1499 "license product info::CompanyName"))
1502 Stream_Write(s, productInfo->pbCompanyName, productInfo->cbCompanyName);
1504 if (!license_check_stream_capacity(log, s, 4,
"license product info::cbProductId"))
1507 Stream_Write_UINT32(s, productInfo->cbProductId);
1509 if ((productInfo->cbProductId < 2) || (productInfo->cbProductId % 2 != 0) ||
1510 !productInfo->pbProductId)
1512 WLog_Print(log, WLOG_WARN,
"license product info invalid cbProductId %" PRIu32,
1513 productInfo->cbProductId);
1517 if (!license_check_stream_capacity(log, s, productInfo->cbProductId,
1518 "license product info::ProductId"))
1521 Stream_Write(s, productInfo->pbProductId, productInfo->cbProductId);
1550 free(productInfo->pbCompanyName);
1551 free(productInfo->pbProductId);
1556BOOL license_read_binary_blob_data(wLog* log,
LICENSE_BLOB* blob, UINT16 wBlobType,
1557 const void* data,
size_t length)
1560 WINPR_ASSERT(length <= UINT16_MAX);
1561 WINPR_ASSERT(data || (length == 0));
1563 blob->length = (UINT16)length;
1565 blob->data =
nullptr;
1567 if ((blob->type != wBlobType) && (blob->type != BB_ANY_BLOB))
1569 WLog_Print(log, WLOG_ERROR,
"license binary blob::type expected %s, got %s",
1570 licencse_blob_type_string(wBlobType), licencse_blob_type_string(blob->type));
1577 if ((blob->type != BB_ANY_BLOB) && (blob->length == 0))
1579 WLog_Print(log, WLOG_DEBUG,
"license binary blob::type %s, length=0, skipping.",
1580 licencse_blob_type_string(blob->type));
1584 blob->type = wBlobType;
1585 blob->data =
nullptr;
1586 if (blob->length > 0)
1587 blob->data = malloc(blob->length);
1590 WLog_Print(log, WLOG_ERROR,
"license binary blob::length=%" PRIu16
", blob::data=%p",
1591 blob->length, (
const void*)blob->data);
1594 memcpy(blob->data, data, blob->length);
1607 UINT16 wBlobType = 0;
1612 if (!license_check_stream_length(log, s, 4,
"license binary blob::type"))
1615 Stream_Read_UINT16(s, wBlobType);
1616 Stream_Read_UINT16(s, length);
1618 if (!license_check_stream_length(log, s, length,
"license binary blob::length"))
1621 if (!license_read_binary_blob_data(log, blob, wBlobType, Stream_Pointer(s), length))
1624 return Stream_SafeSeek(s, length);
1638 if (!Stream_EnsureRemainingCapacity(s, blob->length + 4))
1641 Stream_Write_UINT16(s, blob->type);
1642 Stream_Write_UINT16(s, blob->length);
1644 if (blob->length > 0)
1645 Stream_Write(s, blob->data, blob->length);
1650static BOOL license_write_encrypted_premaster_secret_blob(wLog* log,
wStream* s,
1652 UINT32 ModulusLength)
1654 const UINT32 length = ModulusLength + 8;
1657 WINPR_ASSERT(length <= UINT16_MAX);
1659 if (blob->length > ModulusLength)
1661 WLog_Print(log, WLOG_ERROR,
"invalid blob");
1665 if (!Stream_EnsureRemainingCapacity(s, length + 4))
1667 Stream_Write_UINT16(s, blob->type);
1668 Stream_Write_UINT16(s, (UINT16)length);
1670 if (blob->length > 0)
1671 Stream_Write(s, blob->data, blob->length);
1673 Stream_Zero(s, length - blob->length);
1678static BOOL license_read_encrypted_premaster_secret_blob(wLog* log,
wStream* s,
LICENSE_BLOB* blob,
1679 UINT32* ModulusLength)
1681 if (!license_read_binary_blob(log, s, blob))
1683 WINPR_ASSERT(ModulusLength);
1684 *ModulusLength = blob->length;
1726 UINT32 scopeCount = 0;
1728 WINPR_ASSERT(scopeList);
1730 if (!license_check_stream_length(log, s, 4,
"license scope list"))
1733 Stream_Read_UINT32(s, scopeCount);
1735 if (!license_check_stream_length(log, s, 4ll * scopeCount,
"license scope list::count"))
1738 if (!license_scope_list_resize(scopeList, scopeCount))
1741 for (UINT32 i = 0; i < scopeCount; i++)
1743 if (!license_read_binary_blob(log, s, scopeList->array[i]))
1750BOOL license_write_scope_list(wLog* log,
wStream* s,
const SCOPE_LIST* scopeList)
1752 WINPR_ASSERT(scopeList);
1754 if (!license_check_stream_capacity(log, s, 4,
"license scope list"))
1757 Stream_Write_UINT32(s, scopeList->count);
1759 if (!license_check_stream_capacity(log, s, scopeList->count * 4ull,
1760 "license scope list::count"))
1764 WINPR_ASSERT(scopeList->array || (scopeList->count == 0));
1765 for (UINT32 i = 0; i < scopeList->count; i++)
1769 if (!license_write_binary_blob(s, element))
1788static void license_scope_list_free(
SCOPE_LIST* scopeList, UINT32 count)
1790 WINPR_ASSERT(scopeList);
1791 WINPR_ASSERT(scopeList->array || (scopeList->count == 0));
1793 for (UINT32 x = count; x < scopeList->count; x++)
1795 license_free_binary_blob(scopeList->array[x]);
1796 scopeList->array[x] =
nullptr;
1801 free((
void*)scopeList->array);
1802 scopeList->array =
nullptr;
1806BOOL license_scope_list_resize(
SCOPE_LIST* scopeList, UINT32 count)
1808 license_scope_list_free(scopeList, count);
1815 scopeList->array = tmp;
1818 for (UINT32 x = scopeList->count; x < count; x++)
1820 LICENSE_BLOB* blob = license_new_binary_blob(BB_SCOPE_BLOB);
1823 scopeList->count = x;
1826 scopeList->array[x] = blob;
1829 scopeList->count = count;
1839void license_free_scope_list(
SCOPE_LIST* scopeList)
1844 license_scope_list_free(scopeList, 0);
1848BOOL license_send_license_info(rdpLicense* license,
const LICENSE_BLOB* calBlob,
1849 const BYTE* signature,
size_t signature_length)
1851 WINPR_ASSERT(calBlob);
1852 WINPR_ASSERT(signature);
1853 WINPR_ASSERT(license->certificate);
1855 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1859 UINT16 sec_flags = 0;
1860 wStream* s = license_send_stream_init(license, &sec_flags);
1864 if (!license_check_stream_capacity(license->log, s, 8 +
sizeof(license->ClientRandom),
1865 "license info::ClientRandom"))
1868 Stream_Write_UINT32(s,
1869 license->PreferredKeyExchangeAlg);
1870 Stream_Write_UINT32(s, license->PlatformId);
1873 Stream_Write(s, license->ClientRandom,
sizeof(license->ClientRandom));
1876 if (!license_write_encrypted_premaster_secret_blob(
1877 license->log, s, license->EncryptedPremasterSecret, info->ModulusLength))
1881 if (!license_write_binary_blob(s, calBlob))
1885 if (!license_write_binary_blob(s, license->EncryptedHardwareId))
1889 if (!license_check_stream_capacity(license->log, s, signature_length,
"license info::MACData"))
1891 Stream_Write(s, signature, signature_length);
1893 return license_send(license, s, LICENSE_INFO, sec_flags);
1901static BOOL license_check_preferred_alg(rdpLicense* license, UINT32 PreferredKeyExchangeAlg,
1904 WINPR_ASSERT(license);
1905 WINPR_ASSERT(where);
1907 if (license->PreferredKeyExchangeAlg != PreferredKeyExchangeAlg)
1909 char buffer1[64] = WINPR_C_ARRAY_INIT;
1910 char buffer2[64] = WINPR_C_ARRAY_INIT;
1911 WLog_Print(license->log, WLOG_WARN,
"%s::PreferredKeyExchangeAlg, expected %s, got %s",
1913 license_preferred_key_exchange_alg_string(license->PreferredKeyExchangeAlg,
1914 buffer1,
sizeof(buffer1)),
1915 license_preferred_key_exchange_alg_string(PreferredKeyExchangeAlg, buffer2,
1922BOOL license_read_license_info(rdpLicense* license,
wStream* s)
1925 UINT32 PreferredKeyExchangeAlg = 0;
1927 WINPR_ASSERT(license);
1928 WINPR_ASSERT(license->certificate);
1930 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1935 if (!license_check_stream_length(license->log, s, 8 +
sizeof(license->ClientRandom),
1939 Stream_Read_UINT32(s, PreferredKeyExchangeAlg);
1940 if (!license_check_preferred_alg(license, PreferredKeyExchangeAlg,
"license info"))
1942 Stream_Read_UINT32(s, license->PlatformId);
1945 Stream_Read(s, license->ClientRandom,
sizeof(license->ClientRandom));
1949 UINT32 ModulusLength = 0;
1950 if (!license_read_encrypted_premaster_secret_blob(
1951 license->log, s, license->EncryptedPremasterSecret, &ModulusLength))
1954 if (ModulusLength != info->ModulusLength)
1956 WLog_Print(license->log, WLOG_WARN,
1957 "EncryptedPremasterSecret,::ModulusLength[%" PRIu32
1958 "] != rdpCertInfo::ModulusLength[%" PRIu32
"]",
1959 ModulusLength, info->ModulusLength);
1965 if (!license_read_binary_blob(license->log, s, license->LicenseInfo))
1969 if (!license_read_binary_blob(license->log, s, license->EncryptedHardwareId))
1973 if (!license_check_stream_length(license->log, s,
sizeof(license->MACData),
1974 "license info::MACData"))
1976 Stream_Read(s, license->MACData,
sizeof(license->MACData));
1991BOOL license_read_license_request_packet(rdpLicense* license,
wStream* s)
1993 WINPR_ASSERT(license);
1996 if (!license_check_stream_length(license->log, s,
sizeof(license->ServerRandom),
2000 Stream_Read(s, license->ServerRandom,
sizeof(license->ServerRandom));
2003 if (!license_read_product_info(license->log, s, license->ProductInfo))
2007 if (!license_read_binary_blob(license->log, s, license->KeyExchangeList))
2011 if (!license_read_binary_blob(license->log, s, license->ServerCertificate))
2015 if (!license_read_scope_list(license->log, s, license->ScopeList))
2019 if (!freerdp_certificate_read_server_cert(license->certificate,
2020 license->ServerCertificate->data,
2021 license->ServerCertificate->length))
2024 if (!license_generate_keys(license) || !license_generate_hwid(license) ||
2025 !license_encrypt_premaster_secret(license))
2028#ifdef WITH_DEBUG_LICENSE
2029 WLog_Print(license->log, WLOG_DEBUG,
"ServerRandom:");
2030 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ServerRandom,
2031 sizeof(license->ServerRandom));
2032 license_print_product_info(license->log, license->ProductInfo);
2033 license_print_scope_list(license->log, license->ScopeList);
2038BOOL license_write_license_request_packet(
const rdpLicense* license,
wStream* s)
2040 WINPR_ASSERT(license);
2043 if (!license_check_stream_capacity(license->log, s,
sizeof(license->ServerRandom),
2046 Stream_Write(s, license->ServerRandom,
sizeof(license->ServerRandom));
2049 if (!license_write_product_info(license->log, s, license->ProductInfo))
2053 if (!license_write_binary_blob(s, license->KeyExchangeList))
2057 if (!license_write_binary_blob(s, license->ServerCertificate))
2061 if (!license_write_scope_list(license->log, s, license->ScopeList))
2068static BOOL license_send_license_request_packet(rdpLicense* license)
2070 UINT16 sec_flags = 0;
2071 wStream* s = license_send_stream_init(license, &sec_flags);
2075 if (!license_write_license_request_packet(license, s))
2078 return license_send(license, s, LICENSE_REQUEST, sec_flags);
2092BOOL license_read_platform_challenge_packet(rdpLicense* license,
wStream* s)
2094 BYTE macData[LICENSING_ENCRYPTION_KEY_LENGTH] = WINPR_C_ARRAY_INIT;
2096 WINPR_ASSERT(license);
2098 DEBUG_LICENSE(
"Receiving Platform Challenge Packet");
2100 if (!license_check_stream_length(license->log, s, 4,
"license platform challenge"))
2105 Stream_Seek_UINT32(s);
2108 license->EncryptedPlatformChallenge->type = BB_ANY_BLOB;
2109 if (!license_read_binary_blob(license->log, s, license->EncryptedPlatformChallenge))
2111 license->EncryptedPlatformChallenge->type = BB_ENCRYPTED_DATA_BLOB;
2114 if (!license_check_stream_length(license->log, s,
sizeof(macData),
2115 "license platform challenge::MAC"))
2118 Stream_Read(s, macData,
sizeof(macData));
2119 if (!license_decrypt_and_check_MAC(license, license->EncryptedPlatformChallenge->data,
2120 license->EncryptedPlatformChallenge->length,
2121 license->PlatformChallenge, macData))
2124 WLog_Print(license->log, WLOG_TRACE,
"platform challenge read");
2126#ifdef WITH_DEBUG_LICENSE
2127 WLog_Print(license->log, WLOG_DEBUG,
"EncryptedPlatformChallenge:");
2128 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedPlatformChallenge->data,
2129 license->EncryptedPlatformChallenge->length);
2130 WLog_Print(license->log, WLOG_DEBUG,
"PlatformChallenge:");
2131 winpr_HexLogDump(license->log, WLOG_DEBUG, license->PlatformChallenge->data,
2132 license->PlatformChallenge->length);
2133 WLog_Print(license->log, WLOG_DEBUG,
"MacData:");
2134 winpr_HexLogDump(license->log, WLOG_DEBUG, macData,
sizeof(macData));
2139BOOL license_send_error_alert(rdpLicense* license, UINT32 dwErrorCode, UINT32 dwStateTransition,
2142 UINT16 sec_flags = 0;
2143 wStream* s = license_send_stream_init(license, &sec_flags);
2148 if (!license_check_stream_capacity(license->log, s, 8,
"license error alert"))
2150 Stream_Write_UINT32(s, dwErrorCode);
2151 Stream_Write_UINT32(s, dwStateTransition);
2155 if (!license_write_binary_blob(s, info))
2159 return license_send(license, s, ERROR_ALERT, sec_flags);
2165BOOL license_send_platform_challenge_packet(rdpLicense* license)
2167 UINT16 sec_flags = 0;
2168 wStream* s = license_send_stream_init(license, &sec_flags);
2173 DEBUG_LICENSE(
"Receiving Platform Challenge Packet");
2175 if (!license_check_stream_capacity(license->log, s, 4,
"license platform challenge"))
2181 if (!license_write_binary_blob(s, license->EncryptedPlatformChallenge))
2185 if (!license_check_stream_length(license->log, s,
sizeof(license->MACData),
2186 "license platform challenge::MAC"))
2189 Stream_Write(s, license->MACData,
sizeof(license->MACData));
2191 return license_send(license, s, PLATFORM_CHALLENGE, sec_flags);
2198static BOOL license_read_encrypted_blob(
const rdpLicense* license,
wStream* s,
LICENSE_BLOB* target)
2200 UINT16 wBlobType = 0;
2201 UINT16 wBlobLen = 0;
2203 WINPR_ASSERT(license);
2204 WINPR_ASSERT(target);
2206 if (!license_check_stream_length(license->log, s, 4,
"license encrypted blob"))
2209 Stream_Read_UINT16(s, wBlobType);
2210 if (wBlobType != BB_ENCRYPTED_DATA_BLOB)
2213 license->log, WLOG_WARN,
2214 "expecting BB_ENCRYPTED_DATA_BLOB blob, probably a windows 2003 server, continuing...");
2217 Stream_Read_UINT16(s, wBlobLen);
2219 BYTE* encryptedData = Stream_Pointer(s);
2220 if (!Stream_SafeSeek(s, wBlobLen))
2222 WLog_Print(license->log, WLOG_WARN,
2223 "short license encrypted blob::length, expected %" PRIu16
" bytes, got %" PRIuz,
2224 wBlobLen, Stream_GetRemainingLength(s));
2228 return license_rc4_with_licenseKey(license, encryptedData, wBlobLen, target);
2238BOOL license_read_new_or_upgrade_license_packet(rdpLicense* license,
wStream* s)
2240 UINT32 os_major = 0;
2241 UINT32 os_minor = 0;
2243 UINT32 cbCompanyName = 0;
2244 UINT32 cbProductId = 0;
2245 UINT32 cbLicenseInfo = 0;
2246 wStream sbuffer = WINPR_C_ARRAY_INIT;
2247 wStream* licenseStream =
nullptr;
2249 BYTE computedMac[16] = WINPR_C_ARRAY_INIT;
2250 const BYTE* readMac =
nullptr;
2252 WINPR_ASSERT(license);
2254 DEBUG_LICENSE(
"Receiving Server New/Upgrade License Packet");
2256 LICENSE_BLOB* calBlob = license_new_binary_blob(BB_DATA_BLOB);
2261 if (!license_read_encrypted_blob(license, s, calBlob))
2265 readMac = Stream_Pointer(s);
2266 if (!Stream_SafeSeek(s,
sizeof(computedMac)))
2268 WLog_Print(license->log, WLOG_WARN,
2269 "short license new/upgrade, expected 16 bytes, got %" PRIuz,
2270 Stream_GetRemainingLength(s));
2274 if (!security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), calBlob->data,
2275 calBlob->length, computedMac,
sizeof(computedMac)))
2278 if (memcmp(computedMac, readMac,
sizeof(computedMac)) != 0)
2280 WLog_Print(license->log, WLOG_ERROR,
"new or upgrade license MAC mismatch");
2284 licenseStream = Stream_StaticConstInit(&sbuffer, calBlob->data, calBlob->length);
2287 WLog_Print(license->log, WLOG_ERROR,
2288 "license::blob::data=%p, license::blob::length=%" PRIu16,
2289 (
const void*)calBlob->data, calBlob->length);
2293 if (!license_check_stream_length(license->log, licenseStream, 8,
2294 "license new/upgrade::blob::version"))
2297 Stream_Read_UINT16(licenseStream, os_minor);
2298 Stream_Read_UINT16(licenseStream, os_major);
2300 WLog_Print(license->log, WLOG_DEBUG,
"Version: %" PRIu16
".%" PRIu16, os_major, os_minor);
2303 Stream_Read_UINT32(licenseStream, cbScope);
2304 if (!license_check_stream_length(license->log, licenseStream, cbScope,
2305 "license new/upgrade::blob::scope"))
2308#ifdef WITH_DEBUG_LICENSE
2309 WLog_Print(license->log, WLOG_DEBUG,
"Scope:");
2310 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbScope);
2312 Stream_Seek(licenseStream, cbScope);
2315 if (!license_check_stream_length(license->log, licenseStream, 4,
2316 "license new/upgrade::blob::cbCompanyName"))
2319 Stream_Read_UINT32(licenseStream, cbCompanyName);
2320 if (!license_check_stream_length(license->log, licenseStream, cbCompanyName,
2321 "license new/upgrade::blob::CompanyName"))
2324#ifdef WITH_DEBUG_LICENSE
2325 WLog_Print(license->log, WLOG_DEBUG,
"Company name:");
2326 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbCompanyName);
2328 Stream_Seek(licenseStream, cbCompanyName);
2331 if (!license_check_stream_length(license->log, licenseStream, 4,
2332 "license new/upgrade::blob::cbProductId"))
2335 Stream_Read_UINT32(licenseStream, cbProductId);
2337 if (!license_check_stream_length(license->log, licenseStream, cbProductId,
2338 "license new/upgrade::blob::ProductId"))
2341#ifdef WITH_DEBUG_LICENSE
2342 WLog_Print(license->log, WLOG_DEBUG,
"Product id:");
2343 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbProductId);
2345 Stream_Seek(licenseStream, cbProductId);
2348 if (!license_check_stream_length(license->log, licenseStream, 4,
2349 "license new/upgrade::blob::cbLicenseInfo"))
2352 Stream_Read_UINT32(licenseStream, cbLicenseInfo);
2353 if (!license_check_stream_length(license->log, licenseStream, cbLicenseInfo,
2354 "license new/upgrade::blob::LicenseInfo"))
2357 license->type = LICENSE_TYPE_ISSUED;
2358 license_set_state(license, LICENSE_STATE_COMPLETED);
2361 if (!license->rdp->settings->OldLicenseBehaviour)
2362 ret = saveCal(license->log, license->rdp->settings, Stream_Pointer(licenseStream),
2363 cbLicenseInfo, license->rdp->settings->ClientHostname);
2366 license_free_binary_blob(calBlob);
2377BOOL license_read_error_alert_packet(rdpLicense* license,
wStream* s)
2379 UINT32 dwErrorCode = 0;
2380 UINT32 dwStateTransition = 0;
2382 WINPR_ASSERT(license);
2383 WINPR_ASSERT(license->rdp);
2385 if (!license_check_stream_length(license->log, s, 8ul,
"error alert"))
2388 Stream_Read_UINT32(s, dwErrorCode);
2389 Stream_Read_UINT32(s, dwStateTransition);
2391 if (!license_read_binary_blob(license->log, s, license->ErrorInfo))
2394#ifdef WITH_DEBUG_LICENSE
2395 WLog_Print(license->log, WLOG_DEBUG,
"dwErrorCode: %s, dwStateTransition: %s",
2396 error_codes[dwErrorCode], state_transitions[dwStateTransition]);
2399 if (dwErrorCode == STATUS_VALID_CLIENT)
2401 license->type = LICENSE_TYPE_NONE;
2402 license_set_state(license, LICENSE_STATE_COMPLETED);
2406 switch (dwStateTransition)
2408 case ST_TOTAL_ABORT:
2409 license_set_state(license, LICENSE_STATE_ABORTED);
2411 case ST_NO_TRANSITION:
2412 license_set_state(license, LICENSE_STATE_COMPLETED);
2414 case ST_RESET_PHASE_TO_START:
2415 license_set_state(license, LICENSE_STATE_CONFIGURED);
2417 case ST_RESEND_LAST_MESSAGE:
2433BOOL license_write_new_license_request_packet(
const rdpLicense* license,
wStream* s)
2435 WINPR_ASSERT(license);
2437 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
2441 if (!license_check_stream_capacity(license->log, s, 8 +
sizeof(license->ClientRandom),
2445 Stream_Write_UINT32(s,
2446 license->PreferredKeyExchangeAlg);
2447 Stream_Write_UINT32(s, license->PlatformId);
2448 Stream_Write(s, license->ClientRandom,
2449 sizeof(license->ClientRandom));
2452 !license_write_encrypted_premaster_secret_blob(
2453 license->log, s, license->EncryptedPremasterSecret, info->ModulusLength) ||
2455 !license_write_binary_blob(s, license->ClientUserName) ||
2457 !license_write_binary_blob(s, license->ClientMachineName))
2462 WLog_Print(license->log, WLOG_TRACE,
"new license written");
2464#ifdef WITH_DEBUG_LICENSE
2465 WLog_Print(license->log, WLOG_DEBUG,
"PreferredKeyExchangeAlg: 0x%08" PRIX32
"",
2466 license->PreferredKeyExchangeAlg);
2467 WLog_Print(license->log, WLOG_DEBUG,
"ClientRandom:");
2468 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ClientRandom,
2469 sizeof(license->ClientRandom));
2470 WLog_Print(license->log, WLOG_DEBUG,
"EncryptedPremasterSecret");
2471 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedPremasterSecret->data,
2472 license->EncryptedPremasterSecret->length);
2473 WLog_Print(license->log, WLOG_DEBUG,
"ClientUserName (%" PRIu16
"): %s",
2474 license->ClientUserName->length, (
char*)license->ClientUserName->data);
2475 WLog_Print(license->log, WLOG_DEBUG,
"ClientMachineName (%" PRIu16
"): %s",
2476 license->ClientMachineName->length, (
char*)license->ClientMachineName->data);
2481BOOL license_read_new_license_request_packet(rdpLicense* license,
wStream* s)
2483 UINT32 PreferredKeyExchangeAlg = 0;
2485 WINPR_ASSERT(license);
2487 if (!license_check_stream_length(license->log, s, 8ull +
sizeof(license->ClientRandom),
2488 "new license request"))
2491 Stream_Read_UINT32(s, PreferredKeyExchangeAlg);
2492 if (!license_check_preferred_alg(license, PreferredKeyExchangeAlg,
"new license request"))
2495 Stream_Read_UINT32(s, license->PlatformId);
2496 Stream_Read(s, license->ClientRandom,
2497 sizeof(license->ClientRandom));
2500 UINT32 ModulusLength = 0;
2501 if (!license_read_encrypted_premaster_secret_blob(
2502 license->log, s, license->EncryptedPremasterSecret, &ModulusLength))
2505 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
2507 WLog_Print(license->log, WLOG_WARN,
2508 "Missing license certificate, skipping ModulusLength checks");
2509 else if (ModulusLength != info->ModulusLength)
2511 WLog_Print(license->log, WLOG_WARN,
2512 "EncryptedPremasterSecret expected to be %" PRIu32
" bytes, but read %" PRIu32
2514 info->ModulusLength, ModulusLength);
2519 if (!license_read_binary_blob(license->log, s, license->ClientUserName))
2522 if (!license_read_binary_blob(license->log, s, license->ClientMachineName))
2534BOOL license_answer_license_request(rdpLicense* license)
2536 UINT16 sec_flags = 0;
2538 BYTE* license_data =
nullptr;
2539 size_t license_size = 0;
2541 char* username =
nullptr;
2543 WINPR_ASSERT(license);
2544 WINPR_ASSERT(license->rdp);
2545 WINPR_ASSERT(license->rdp->settings);
2547 if (!license->rdp->settings->OldLicenseBehaviour)
2548 license_data = loadCalFile(license->log, license->rdp->settings,
2549 license->rdp->settings->ClientHostname, &license_size);
2554 BYTE signature[LICENSING_ENCRYPTION_KEY_LENGTH] = WINPR_C_ARRAY_INIT;
2556 DEBUG_LICENSE(
"Sending Saved License Packet");
2558 WINPR_ASSERT(license->EncryptedHardwareId);
2559 license->EncryptedHardwareId->type = BB_ENCRYPTED_DATA_BLOB;
2560 if (!license_encrypt_and_MAC(license, license->HardwareId,
sizeof(license->HardwareId),
2561 license->EncryptedHardwareId, signature,
sizeof(signature)))
2567 calBlob = license_new_binary_blob(BB_DATA_BLOB);
2573 calBlob->data = license_data;
2574 WINPR_ASSERT(license_size <= UINT16_MAX);
2575 calBlob->length = (UINT16)license_size;
2577 status = license_send_license_info(license, calBlob, signature,
sizeof(signature));
2578 license_free_binary_blob(calBlob);
2583 DEBUG_LICENSE(
"Sending New License Packet");
2585 s = license_send_stream_init(license, &sec_flags);
2588 if (license->rdp->settings->Username !=
nullptr)
2589 username = license->rdp->settings->Username;
2591 username =
"username";
2594 WINPR_ASSERT(license->ClientUserName);
2595 const size_t len = strlen(username) + 1;
2596 WINPR_ASSERT(len <= UINT16_MAX);
2598 license->ClientUserName->data = (BYTE*)username;
2599 license->ClientUserName->length = (UINT16)len;
2603 WINPR_ASSERT(license->ClientMachineName);
2604 const size_t len = strlen(license->rdp->settings->ClientHostname) + 1;
2605 WINPR_ASSERT(len <= UINT16_MAX);
2607 license->ClientMachineName->data = (BYTE*)license->rdp->settings->ClientHostname;
2608 license->ClientMachineName->length = (UINT16)len;
2610 status = license_write_new_license_request_packet(license, s);
2612 WINPR_ASSERT(license->ClientUserName);
2613 license->ClientUserName->data =
nullptr;
2614 license->ClientUserName->length = 0;
2616 WINPR_ASSERT(license->ClientMachineName);
2617 license->ClientMachineName->data =
nullptr;
2618 license->ClientMachineName->length = 0;
2626 return license_send(license, s, NEW_LICENSE_REQUEST, sec_flags);
2635BOOL license_send_platform_challenge_response(rdpLicense* license)
2637 wStream* challengeRespData =
nullptr;
2638 BYTE* buffer =
nullptr;
2641 WINPR_ASSERT(license);
2642 WINPR_ASSERT(license->PlatformChallenge);
2643 WINPR_ASSERT(license->MacSaltKey);
2644 WINPR_ASSERT(license->EncryptedPlatformChallenge);
2645 WINPR_ASSERT(license->EncryptedHardwareId);
2647 DEBUG_LICENSE(
"Sending Platform Challenge Response Packet");
2649 license->EncryptedPlatformChallenge->type = BB_DATA_BLOB;
2652 challengeRespData = Stream_New(
nullptr, 8 + license->PlatformChallenge->length);
2653 if (!challengeRespData)
2655 Stream_Write_UINT16(challengeRespData, PLATFORM_CHALLENGE_RESPONSE_VERSION);
2656 Stream_Write_UINT16(challengeRespData, license->ClientType);
2657 Stream_Write_UINT16(challengeRespData, license->LicenseDetailLevel);
2658 Stream_Write_UINT16(challengeRespData, license->PlatformChallenge->length);
2659 Stream_Write(challengeRespData, license->PlatformChallenge->data,
2660 license->PlatformChallenge->length);
2661 Stream_SealLength(challengeRespData);
2664 const size_t length = Stream_Length(challengeRespData) +
sizeof(license->HardwareId);
2665 buffer = (BYTE*)malloc(length);
2668 Stream_Free(challengeRespData, TRUE);
2672 CopyMemory(buffer, Stream_Buffer(challengeRespData), Stream_Length(challengeRespData));
2673 CopyMemory(&buffer[Stream_Length(challengeRespData)], license->HardwareId,
2674 sizeof(license->HardwareId));
2675 status = security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), buffer, length,
2676 license->MACData,
sizeof(license->MACData));
2681 Stream_Free(challengeRespData, TRUE);
2685 license->EncryptedHardwareId->type = BB_ENCRYPTED_DATA_BLOB;
2686 if (!license_rc4_with_licenseKey(license, license->HardwareId,
sizeof(license->HardwareId),
2687 license->EncryptedHardwareId))
2689 Stream_Free(challengeRespData, TRUE);
2693 status = license_rc4_with_licenseKey(license, Stream_Buffer(challengeRespData),
2694 Stream_Length(challengeRespData),
2695 license->EncryptedPlatformChallengeResponse);
2696 Stream_Free(challengeRespData, TRUE);
2700#ifdef WITH_DEBUG_LICENSE
2701 WLog_Print(license->log, WLOG_DEBUG,
"LicensingEncryptionKey:");
2702 winpr_HexLogDump(license->log, WLOG_DEBUG, license->LicensingEncryptionKey, 16);
2703 WLog_Print(license->log, WLOG_DEBUG,
"HardwareId:");
2704 winpr_HexLogDump(license->log, WLOG_DEBUG, license->HardwareId,
sizeof(license->HardwareId));
2705 WLog_Print(license->log, WLOG_DEBUG,
"EncryptedHardwareId:");
2706 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedHardwareId->data,
2707 license->EncryptedHardwareId->length);
2709 UINT16 sec_flags = 0;
2710 wStream* s = license_send_stream_init(license, &sec_flags);
2714 if (license_write_client_platform_challenge_response(license, s))
2715 return license_send(license, s, PLATFORM_CHALLENGE_RESPONSE, sec_flags);
2721BOOL license_read_platform_challenge_response(WINPR_ATTR_UNUSED rdpLicense* license)
2723 WINPR_ASSERT(license);
2724 WINPR_ASSERT(license->PlatformChallenge);
2725 WINPR_ASSERT(license->MacSaltKey);
2726 WINPR_ASSERT(license->EncryptedPlatformChallenge);
2727 WINPR_ASSERT(license->EncryptedHardwareId);
2729 DEBUG_LICENSE(
"Receiving Platform Challenge Response Packet");
2731#if defined(WITH_LICENSE_DECRYPT_CHALLENGE_RESPONSE)
2733 LICENSE_BLOB* dblob = license_new_binary_blob(BB_ANY_BLOB);
2737 wStream sbuffer = WINPR_C_ARRAY_INIT;
2738 UINT16 wVersion = 0;
2739 UINT16 cbChallenge = 0;
2740 const BYTE* pbChallenge =
nullptr;
2741 LICENSE_BLOB* blob = license->EncryptedPlatformChallengeResponse;
2743 if (!license_rc4_with_licenseKey(license, blob->data, blob->length, dblob))
2746 wStream* s = Stream_StaticConstInit(&sbuffer, dblob->data, dblob->length);
2747 if (!license_check_stream_length(s, 8,
"PLATFORM_CHALLENGE_RESPONSE_DATA"))
2750 Stream_Read_UINT16(s, wVersion);
2751 if (wVersion != PLATFORM_CHALLENGE_RESPONSE_VERSION)
2753 WLog_Print(license->log, WLOG_WARN,
2754 "Invalid PLATFORM_CHALLENGE_RESPONSE_DATA::wVersion 0x%04" PRIx16
2755 ", expected 0x04" PRIx16,
2756 wVersion, PLATFORM_CHALLENGE_RESPONSE_VERSION);
2759 Stream_Read_UINT16(s, license->ClientType);
2760 Stream_Read_UINT16(s, license->LicenseDetailLevel);
2761 Stream_Read_UINT16(s, cbChallenge);
2763 if (!license_check_stream_length(s, cbChallenge,
2764 "PLATFORM_CHALLENGE_RESPONSE_DATA::pbChallenge"))
2767 pbChallenge = Stream_Pointer(s);
2768 if (!license_read_binary_blob_data(license->PlatformChallengeResponse, BB_DATA_BLOB,
2769 pbChallenge, cbChallenge))
2771 if (!Stream_SafeSeek(s, cbChallenge))
2776 license_free_binary_blob(dblob);
2783BOOL license_write_client_platform_challenge_response(rdpLicense* license,
wStream* s)
2785 WINPR_ASSERT(license);
2787 if (!license_write_binary_blob(s, license->EncryptedPlatformChallengeResponse))
2789 if (!license_write_binary_blob(s, license->EncryptedHardwareId))
2791 if (!license_check_stream_capacity(license->log, s,
sizeof(license->MACData),
2792 "CLIENT_PLATFORM_CHALLENGE_RESPONSE::MACData"))
2794 Stream_Write(s, license->MACData,
sizeof(license->MACData));
2798BOOL license_read_client_platform_challenge_response(rdpLicense* license,
wStream* s)
2800 WINPR_ASSERT(license);
2802 if (!license_read_binary_blob(license->log, s, license->EncryptedPlatformChallengeResponse))
2804 if (!license_read_binary_blob(license->log, s, license->EncryptedHardwareId))
2806 if (!license_check_stream_length(license->log, s,
sizeof(license->MACData),
2807 "CLIENT_PLATFORM_CHALLENGE_RESPONSE::MACData"))
2809 Stream_Read(s, license->MACData,
sizeof(license->MACData));
2810 return license_read_platform_challenge_response(license);
2822BOOL license_send_valid_client_error_packet(rdpRdp* rdp)
2825 rdpLicense* license = rdp->license;
2826 WINPR_ASSERT(license);
2828 license->state = LICENSE_STATE_COMPLETED;
2829 license->type = LICENSE_TYPE_NONE;
2830 return license_send_error_alert(license, STATUS_VALID_CLIENT, ST_NO_TRANSITION,
2831 license->ErrorInfo);
2840rdpLicense* license_new(rdpRdp* rdp)
2844 rdpLicense* license = (rdpLicense*)calloc(1,
sizeof(rdpLicense));
2847 license->log = WLog_Get(LICENSE_TAG);
2848 WINPR_ASSERT(license->log);
2850 license->PlatformId = PLATFORMID;
2851 license->ClientType = OTHER_PLATFORM_CHALLENGE_TYPE;
2852 license->LicenseDetailLevel = LICENSE_DETAIL_DETAIL;
2853 license->PreferredKeyExchangeAlg = KEY_EXCHANGE_ALG_RSA;
2856 license_set_state(license, LICENSE_STATE_INITIAL);
2857 if (!(license->certificate = freerdp_certificate_new()))
2859 if (!(license->ProductInfo = license_new_product_info()))
2861 if (!(license->ErrorInfo = license_new_binary_blob(BB_ERROR_BLOB)))
2863 if (!(license->LicenseInfo = license_new_binary_blob(BB_DATA_BLOB)))
2865 if (!(license->KeyExchangeList = license_new_binary_blob(BB_KEY_EXCHG_ALG_BLOB)))
2867 if (!(license->ServerCertificate = license_new_binary_blob(BB_CERTIFICATE_BLOB)))
2869 if (!(license->ClientUserName = license_new_binary_blob(BB_CLIENT_USER_NAME_BLOB)))
2871 if (!(license->ClientMachineName = license_new_binary_blob(BB_CLIENT_MACHINE_NAME_BLOB)))
2873 if (!(license->PlatformChallenge = license_new_binary_blob(BB_ANY_BLOB)))
2875 if (!(license->PlatformChallengeResponse = license_new_binary_blob(BB_ANY_BLOB)))
2877 if (!(license->EncryptedPlatformChallenge = license_new_binary_blob(BB_ANY_BLOB)))
2879 if (!(license->EncryptedPlatformChallengeResponse =
2880 license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2882 if (!(license->EncryptedPremasterSecret = license_new_binary_blob(BB_ANY_BLOB)))
2884 if (!(license->EncryptedHardwareId = license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2886 if (!(license->EncryptedLicenseInfo = license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2888 if (!(license->ScopeList = license_new_scope_list()))
2891 if (!license_generate_randoms(license))
2897 WINPR_PRAGMA_DIAG_PUSH
2898 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2899 license_free(license);
2900 WINPR_PRAGMA_DIAG_POP
2909void license_free(rdpLicense* license)
2913 freerdp_certificate_free(license->certificate);
2914 license_free_product_info(license->ProductInfo);
2915 license_free_binary_blob(license->ErrorInfo);
2916 license_free_binary_blob(license->LicenseInfo);
2917 license_free_binary_blob(license->KeyExchangeList);
2918 license_free_binary_blob(license->ServerCertificate);
2919 license_free_binary_blob(license->ClientUserName);
2920 license_free_binary_blob(license->ClientMachineName);
2921 license_free_binary_blob(license->PlatformChallenge);
2922 license_free_binary_blob(license->PlatformChallengeResponse);
2923 license_free_binary_blob(license->EncryptedPlatformChallenge);
2924 license_free_binary_blob(license->EncryptedPlatformChallengeResponse);
2925 license_free_binary_blob(license->EncryptedPremasterSecret);
2926 license_free_binary_blob(license->EncryptedHardwareId);
2927 license_free_binary_blob(license->EncryptedLicenseInfo);
2928 license_free_scope_list(license->ScopeList);
2933LICENSE_STATE license_get_state(
const rdpLicense* license)
2935 WINPR_ASSERT(license);
2936 return license->state;
2939LICENSE_TYPE license_get_type(
const rdpLicense* license)
2941 WINPR_ASSERT(license);
2942 return license->type;
2945void license_set_state(rdpLicense* license, LICENSE_STATE state)
2947 WINPR_ASSERT(license);
2948 license->state = state;
2951 case LICENSE_STATE_COMPLETED:
2953 case LICENSE_STATE_ABORTED:
2955 license->type = LICENSE_TYPE_INVALID;
2960const char* license_get_state_string(LICENSE_STATE state)
2964 case LICENSE_STATE_INITIAL:
2965 return "LICENSE_STATE_INITIAL";
2966 case LICENSE_STATE_CONFIGURED:
2967 return "LICENSE_STATE_CONFIGURED";
2968 case LICENSE_STATE_REQUEST:
2969 return "LICENSE_STATE_REQUEST";
2970 case LICENSE_STATE_NEW_REQUEST:
2971 return "LICENSE_STATE_NEW_REQUEST";
2972 case LICENSE_STATE_PLATFORM_CHALLENGE:
2973 return "LICENSE_STATE_PLATFORM_CHALLENGE";
2974 case LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE:
2975 return "LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE";
2976 case LICENSE_STATE_COMPLETED:
2977 return "LICENSE_STATE_COMPLETED";
2978 case LICENSE_STATE_ABORTED:
2979 return "LICENSE_STATE_ABORTED";
2981 return "LICENSE_STATE_UNKNOWN";
2985BOOL license_server_send_request(rdpLicense* license)
2987 if (!license_ensure_state(license, LICENSE_STATE_CONFIGURED, LICENSE_REQUEST))
2989 if (!license_send_license_request_packet(license))
2991 license_set_state(license, LICENSE_STATE_REQUEST);
2996static BOOL license_set_string(wLog* log,
const char* what,
const char* value, BYTE** bdst,
3000 WINPR_ASSERT(value);
3002 WINPR_ASSERT(dstLen);
3012 *cnv.w = ConvertUtf8ToWCharAlloc(value, &len);
3013 if (!*cnv.w || (len > UINT32_MAX /
sizeof(WCHAR)))
3015 WLog_Print(log, WLOG_ERROR,
"license->ProductInfo: %s == %p || %" PRIuz
" > UINT32_MAX",
3016 what, (
void*)(*cnv.w), len);
3019 *dstLen = (UINT32)(len *
sizeof(WCHAR));
3023BOOL license_server_configure(rdpLicense* license)
3026 UINT32 algs[] = { KEY_EXCHANGE_ALG_RSA };
3028 WINPR_ASSERT(license);
3029 WINPR_ASSERT(license->rdp);
3031 const rdpSettings* settings = license->rdp->settings;
3033 const char* CompanyName =
3036 const char* ProductName =
3038 const UINT32 ProductVersion =
3040 const UINT32 issuerCount =
3044 const char** issuers = WINPR_REINTERPRET_CAST(ptr,
const void*,
const char**);
3046 WINPR_ASSERT(CompanyName);
3047 WINPR_ASSERT(ProductName);
3048 WINPR_ASSERT(ProductVersion > 0);
3049 WINPR_ASSERT(issuers || (issuerCount == 0));
3051 if (!license_ensure_state(license, LICENSE_STATE_INITIAL, LICENSE_REQUEST))
3054 license->ProductInfo->dwVersion = ProductVersion;
3055 if (!license_set_string(license->log,
"pbCompanyName", CompanyName,
3056 &license->ProductInfo->pbCompanyName,
3057 &license->ProductInfo->cbCompanyName))
3060 if (!license_set_string(license->log,
"pbProductId", ProductName,
3061 &license->ProductInfo->pbProductId, &license->ProductInfo->cbProductId))
3064 if (!license_read_binary_blob_data(license->log, license->KeyExchangeList,
3065 BB_KEY_EXCHG_ALG_BLOB, algs,
sizeof(algs)))
3068 if (!freerdp_certificate_read_server_cert(license->certificate, settings->ServerCertificate,
3069 settings->ServerCertificateLength))
3072 s = Stream_New(
nullptr, 1024);
3079 freerdp_certificate_write_server_cert(license->certificate, CERT_CHAIN_VERSION_2, s);
3081 r = license_read_binary_blob_data(license->log, license->ServerCertificate,
3082 BB_CERTIFICATE_BLOB, Stream_Buffer(s),
3083 Stream_GetPosition(s));
3085 Stream_Free(s, TRUE);
3090 if (!license_scope_list_resize(license->ScopeList, issuerCount))
3092 for (
size_t x = 0; x < issuerCount; x++)
3095 const char* name = issuers[x];
3096 const size_t length = strnlen(name, UINT16_MAX) + 1;
3097 if ((length == 0) || (length > UINT16_MAX))
3099 WLog_Print(license->log, WLOG_WARN,
3100 "Invalid issuer at position %" PRIuz
": length 0 < %" PRIuz
" <= %d"
3102 x, length, UINT16_MAX, name);
3105 if (!license_read_binary_blob_data(license->log, blob, BB_SCOPE_BLOB, name, length))
3109 license_set_state(license, LICENSE_STATE_CONFIGURED);
3113rdpLicense* license_get(rdpContext* context)
3115 WINPR_ASSERT(context);
3116 WINPR_ASSERT(context->rdp);
3117 return context->rdp->license;
WINPR_ATTR_NODISCARD FREERDP_API const void * freerdp_settings_get_pointer(const rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id)
Returns a immutable pointer settings value.
WINPR_ATTR_NODISCARD FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.