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* license_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);
445 ConvertWCharNToUtf8Alloc(WINPR_PACKED_ALIGN_CAST(
const WCHAR*, productInfo->pbCompanyName),
446 productInfo->cbCompanyName /
sizeof(WCHAR),
nullptr);
448 ConvertWCharNToUtf8Alloc(WINPR_PACKED_ALIGN_CAST(
const WCHAR*, productInfo->pbProductId),
449 productInfo->cbProductId /
sizeof(WCHAR),
nullptr);
450 WLog_Print(log, WLOG_INFO,
"ProductInfo:");
451 WLog_Print(log, WLOG_INFO,
"\tdwVersion: 0x%08" PRIX32
"", productInfo->dwVersion);
452 WLog_Print(log, WLOG_INFO,
"\tCompanyName: %s", CompanyName);
453 WLog_Print(log, WLOG_INFO,
"\tProductId: %s", ProductId);
458static void license_print_scope_list(wLog* log,
const SCOPE_LIST* scopeList)
460 WINPR_ASSERT(scopeList);
462 WLog_Print(log, WLOG_INFO,
"ScopeList (%" PRIu32
"):", scopeList->count);
464 for (UINT32 index = 0; index < scopeList->count; index++)
468 WINPR_ASSERT(scopeList->array);
469 scope = scopeList->array[index];
472 WLog_Print(log, WLOG_INFO,
"\t%s", (
const char*)scope->data);
477static const char licenseStore[] =
"licenses";
480static BOOL license_ensure_state(rdpLicense* license, LICENSE_STATE state, UINT32 msg)
482 const LICENSE_STATE cstate = license_get_state(license);
484 WINPR_ASSERT(license);
488 const char* scstate = license_get_state_string(cstate);
489 const char* sstate = license_get_state_string(state);
490 const char* where = license_request_type_string(msg);
492 WLog_Print(license->log, WLOG_WARN,
493 "Received [%s], but found invalid licensing state %s, expected %s", where,
500state_run_t license_recv(rdpLicense* license,
wStream* s)
502 WINPR_ASSERT(license);
503 WINPR_ASSERT(license->rdp);
504 WINPR_ASSERT(license->rdp->settings);
507 return license_server_recv(license, s);
509 return license_client_recv(license, s);
513static BOOL license_check_stream_length(wLog* log,
wStream* s, UINT64 expect,
const char* where)
515 const size_t remain = Stream_GetRemainingLength(s);
521 WLog_Print(log, WLOG_WARN,
"short %s, expected %" PRIu64
" bytes, got %" PRIuz, where,
529static BOOL license_check_stream_capacity(wLog* log,
wStream* s,
size_t expect,
const char* where)
533 return (Stream_CheckAndLogRequiredCapacityWLogEx(log, WLOG_WARN, s, expect, 1,
534 "%s(%s:%" PRIuz
") %s", __func__, __FILE__,
535 (
size_t)__LINE__, where));
539static BOOL computeCalHash(wLog* log,
const char* hostname,
char* hashStr,
size_t len)
541 WINPR_DIGEST_CTX* sha1 =
nullptr;
543 BYTE hash[20] = WINPR_C_ARRAY_INIT;
545 WINPR_ASSERT(hostname);
546 WINPR_ASSERT(hashStr);
548 if (len < 2 *
sizeof(hash) + 1)
551 if (!(sha1 = winpr_Digest_New()))
553 if (!winpr_Digest_Init(sha1, WINPR_MD_SHA1))
555 if (!winpr_Digest_Update(sha1, (
const BYTE*)hostname, strlen(hostname)))
557 if (!winpr_Digest_Final(sha1, hash,
sizeof(hash)))
560 for (
size_t i = 0; i <
sizeof(hash); i++, hashStr += 2)
561 (
void)sprintf_s(hashStr, 3,
"%.2x", hash[i]);
566 WLog_Print(log, WLOG_ERROR,
"failed to generate SHA1 of hostname '%s'", hostname);
567 winpr_Digest_Free(sha1);
572static BOOL saveCal(wLog* log,
const rdpSettings* settings,
const BYTE* data,
size_t length,
573 const char* hostname)
575 char hash[41] = WINPR_C_ARRAY_INIT;
577 char* licenseStorePath =
nullptr;
578 char filename[MAX_PATH] = WINPR_C_ARRAY_INIT;
579 char filenameNew[MAX_PATH] = WINPR_C_ARRAY_INIT;
580 char* filepath =
nullptr;
581 char* filepathNew =
nullptr;
588 WINPR_ASSERT(data || (length == 0));
589 WINPR_ASSERT(hostname);
591 if (!winpr_PathFileExists(path))
593 if (!winpr_PathMakePath(path,
nullptr))
595 WLog_Print(log, WLOG_ERROR,
"error creating directory '%s'", path);
598 WLog_Print(log, WLOG_INFO,
"creating directory %s", path);
601 if (!(licenseStorePath = GetCombinedPath(path, licenseStore)))
603 WLog_Print(log, WLOG_ERROR,
"Failed to get license store path from '%s' + '%s'", path,
608 if (!winpr_PathFileExists(licenseStorePath))
610 if (!winpr_PathMakePath(licenseStorePath,
nullptr))
612 WLog_Print(log, WLOG_ERROR,
"error creating directory '%s'", licenseStorePath);
615 WLog_Print(log, WLOG_INFO,
"creating directory %s", licenseStorePath);
618 if (!computeCalHash(log, hostname, hash,
sizeof(hash)))
620 (void)sprintf_s(filename,
sizeof(filename) - 1,
"%s.cal", hash);
621 (void)sprintf_s(filenameNew,
sizeof(filenameNew) - 1,
"%s.cal.new", hash);
623 if (!(filepath = GetCombinedPath(licenseStorePath, filename)))
625 WLog_Print(log, WLOG_ERROR,
"Failed to get license file path from '%s' + '%s'", path,
630 if (!(filepathNew = GetCombinedPath(licenseStorePath, filenameNew)))
632 WLog_Print(log, WLOG_ERROR,
"Failed to get license new file path from '%s' + '%s'", path,
637 fp = winpr_fopen(filepathNew,
"wb");
640 WLog_Print(log, WLOG_ERROR,
"Failed to open license file '%s'", filepathNew);
644 written = fwrite(data, length, 1, fp);
649 WLog_Print(log, WLOG_ERROR,
"Failed to write to license file '%s'", filepathNew);
650 winpr_DeleteFile(filepathNew);
654 ret = winpr_MoveFileEx(filepathNew, filepath, MOVEFILE_REPLACE_EXISTING);
656 WLog_Print(log, WLOG_ERROR,
"Failed to move license file '%s' to '%s'", filepathNew,
662 free(licenseStorePath);
666WINPR_ATTR_MALLOC(free, 1)
667static BYTE* loadCalFile(wLog* log, const rdpSettings* settings, const
char* hostname,
670 char* licenseStorePath =
nullptr;
671 char* calPath =
nullptr;
672 char calFilename[MAX_PATH] = WINPR_C_ARRAY_INIT;
673 char hash[41] = WINPR_C_ARRAY_INIT;
679 WINPR_ASSERT(settings);
680 WINPR_ASSERT(hostname);
681 WINPR_ASSERT(dataLen);
683 if (!computeCalHash(log, hostname, hash,
sizeof(hash)))
685 WLog_Print(log, WLOG_ERROR,
"loadCalFile: unable to compute hostname hash");
689 (void)sprintf_s(calFilename,
sizeof(calFilename) - 1,
"%s.cal", hash);
691 if (!(licenseStorePath = GetCombinedPath(
695 if (!(calPath = GetCombinedPath(licenseStorePath, calFilename)))
698 fp = winpr_fopen(calPath,
"rb");
702 if (_fseeki64(fp, 0, SEEK_END) != 0)
704 length = _ftelli64(fp);
705 if (_fseeki64(fp, 0, SEEK_SET) != 0)
710 ret = (BYTE*)malloc((
size_t)length);
714 status = fread(ret, (
size_t)length, 1, fp);
718 *dataLen = (size_t)length;
722 free(licenseStorePath);
732 free(licenseStorePath);
746static BOOL license_read_preamble(wLog* log,
wStream* s, BYTE* bMsgType, BYTE* flags,
749 WINPR_ASSERT(bMsgType);
751 WINPR_ASSERT(wMsgSize);
754 if (!license_check_stream_length(log, s, 4,
"license preamble"))
757 Stream_Read_UINT8(s, *bMsgType);
758 Stream_Read_UINT8(s, *flags);
759 Stream_Read_UINT16(s, *wMsgSize);
762 WLog_Print(log, WLOG_WARN,
763 "invalid license preamble::wMsgSize, expected value >= 4, got %" PRIu32,
767 return license_check_stream_length(log, s, *wMsgSize - 4ull,
"license preamble::wMsgSize");
780static BOOL license_write_preamble(
wStream* s, BYTE bMsgType, BYTE flags, UINT16 wMsgSize)
782 if (!Stream_EnsureRemainingCapacity(s, 4))
786 Stream_Write_UINT8(s, bMsgType);
787 Stream_Write_UINT8(s, flags);
788 Stream_Write_UINT16(s, wMsgSize);
800wStream* license_send_stream_init(rdpLicense* license, UINT16* sec_flags)
802 WINPR_ASSERT(license);
803 WINPR_ASSERT(license->rdp);
804 WINPR_ASSERT(sec_flags);
806 const BOOL do_crypt = license->rdp->do_crypt;
808 *sec_flags = SEC_LICENSE_PKT;
820 *sec_flags |= SEC_LICENSE_ENCRYPT_CS;
821 license->rdp->do_crypt = license->rdp->do_crypt_license;
824 wStream* s = rdp_send_stream_init(license->rdp, sec_flags);
828 license->rdp->do_crypt = do_crypt;
829 license->PacketHeaderLength = (UINT16)Stream_GetPosition(s);
830 if (!Stream_SafeZero(s, LICENSE_PREAMBLE_LENGTH))
846static BOOL license_send(rdpLicense* license,
wStream* s, BYTE type, UINT16 sec_flags)
848 WINPR_ASSERT(license);
849 WINPR_ASSERT(license->rdp);
851 rdpRdp* rdp = license->rdp;
852 WINPR_ASSERT(rdp->settings);
854 DEBUG_LICENSE(
"Sending %s Packet", license_request_type_string(type));
855 const size_t length = Stream_GetPosition(s);
856 WINPR_ASSERT(length >= license->PacketHeaderLength);
857 WINPR_ASSERT(length <= UINT16_MAX + license->PacketHeaderLength);
859 const UINT16 wMsgSize = (UINT16)(length - license->PacketHeaderLength);
860 if (!Stream_SetPosition(s, license->PacketHeaderLength))
862 BYTE flags = PREAMBLE_VERSION_3_0;
869 if (!rdp->settings->ServerMode)
870 flags |= EXTENDED_ERROR_MSG_SUPPORTED;
872 if (!license_write_preamble(s, type, flags, wMsgSize))
878#ifdef WITH_DEBUG_LICENSE
879 WLog_Print(license->log, WLOG_DEBUG,
"Sending %s Packet, length %" PRIu16
"",
880 license_request_type_string(type), wMsgSize);
881 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_PointerAs(s,
char) - LICENSE_PREAMBLE_LENGTH,
884 if (!Stream_SetPosition(s, length))
886 const BOOL ret = rdp_send(rdp, s, MCS_GLOBAL_CHANNEL_ID, sec_flags);
890BOOL license_write_server_upgrade_license(
const rdpLicense* license,
wStream* s)
892 WINPR_ASSERT(license);
894 if (!license_write_binary_blob(s, license->EncryptedLicenseInfo))
896 if (!license_check_stream_capacity(license->log, s,
sizeof(license->MACData),
897 "SERVER_UPGRADE_LICENSE::MACData"))
899 Stream_Write(s, license->MACData,
sizeof(license->MACData));
904static BOOL license_server_send_new_or_upgrade_license(rdpLicense* license, BOOL upgrade)
906 UINT16 sec_flags = 0;
907 wStream* s = license_send_stream_init(license, &sec_flags);
908 const BYTE type = upgrade ? UPGRADE_LICENSE : NEW_LICENSE;
913 if (!license_write_server_upgrade_license(license, s))
916 return license_send(license, s, type, sec_flags);
931static state_run_t license_client_recv_int(rdpLicense* license,
wStream* s)
936 const size_t length = Stream_GetRemainingLength(s);
938 WINPR_ASSERT(license);
940 if (!license_read_preamble(license->log, s, &bMsgType, &flags,
942 return STATE_RUN_FAILED;
944 DEBUG_LICENSE(
"Receiving %s Packet", license_request_type_string(bMsgType));
948 case LICENSE_REQUEST:
950 if (license_get_state(license) == LICENSE_STATE_INITIAL)
951 license_set_state(license, LICENSE_STATE_CONFIGURED);
953 if (!license_ensure_state(license, LICENSE_STATE_CONFIGURED, bMsgType))
954 return STATE_RUN_FAILED;
956 if (!license_read_license_request_packet(license, s))
957 return STATE_RUN_FAILED;
959 if (!license_answer_license_request(license))
960 return STATE_RUN_FAILED;
962 license_set_state(license, LICENSE_STATE_NEW_REQUEST);
965 case PLATFORM_CHALLENGE:
966 if (!license_ensure_state(license, LICENSE_STATE_NEW_REQUEST, bMsgType))
967 return STATE_RUN_FAILED;
969 if (!license_read_platform_challenge_packet(license, s))
970 return STATE_RUN_FAILED;
972 if (!license_send_platform_challenge_response(license))
973 return STATE_RUN_FAILED;
974 license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE);
978 case UPGRADE_LICENSE:
979 if (!license_ensure_state(license, LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE, bMsgType))
980 return STATE_RUN_FAILED;
981 if (!license_read_new_or_upgrade_license_packet(license, s))
982 return STATE_RUN_FAILED;
986 if (!license_read_error_alert_packet(license, s))
987 return STATE_RUN_FAILED;
991 WLog_Print(license->log, WLOG_ERROR,
"invalid bMsgType:%" PRIu8
"", bMsgType);
992 return STATE_RUN_FAILED;
995 if (!tpkt_ensure_stream_consumed(license->log, s, length))
996 return STATE_RUN_FAILED;
997 return STATE_RUN_SUCCESS;
1000state_run_t license_client_recv(rdpLicense* license,
wStream* s)
1002 state_run_t rc = license_client_recv_int(license, s);
1003 if (state_run_failed(rc))
1005 freerdp_set_last_error(license->rdp->context, ERROR_CTX_LICENSE_CLIENT_INVALID);
1010state_run_t license_server_recv(rdpLicense* license,
wStream* s)
1012 state_run_t rc = STATE_RUN_FAILED;
1015 UINT16 wMsgSize = 0;
1016 const size_t length = Stream_GetRemainingLength(s);
1018 WINPR_ASSERT(license);
1020 if (!license_read_preamble(license->log, s, &bMsgType, &flags,
1024 DEBUG_LICENSE(
"Receiving %s Packet", license_request_type_string(bMsgType));
1028 case NEW_LICENSE_REQUEST:
1029 if (!license_ensure_state(license, LICENSE_STATE_REQUEST, bMsgType))
1031 if (!license_read_new_license_request_packet(license, s))
1034 if (!license_send_error_alert(license, ERR_INVALID_MAC, ST_TOTAL_ABORT,
1035 license->ErrorInfo))
1037 if (!license_send_platform_challenge_packet(license))
1039 license->update = FALSE;
1040 license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE);
1043 if (!license_ensure_state(license, LICENSE_STATE_REQUEST, bMsgType))
1045 if (!license_read_license_info(license, s))
1048 if (!license_send_platform_challenge_packet(license))
1050 license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE);
1051 license->update = TRUE;
1054 case PLATFORM_CHALLENGE_RESPONSE:
1055 if (!license_ensure_state(license, LICENSE_STATE_PLATFORM_CHALLENGE, bMsgType))
1057 if (!license_read_client_platform_challenge_response(license, s))
1063 if (license_send_error_alert(license, ERR_INVALID_CLIENT, ST_TOTAL_ABORT,
1064 license->ErrorInfo))
1069 if (!license_server_send_new_or_upgrade_license(license, license->update))
1072 license->type = LICENSE_TYPE_ISSUED;
1073 license_set_state(license, LICENSE_STATE_COMPLETED);
1075 rc = STATE_RUN_CONTINUE;
1080 if (!license_read_error_alert_packet(license, s))
1085 WLog_Print(license->log, WLOG_ERROR,
"invalid bMsgType:%" PRIu8
"", bMsgType);
1089 if (!tpkt_ensure_stream_consumed(license->log, s, length))
1092 if (!state_run_success(rc))
1093 rc = STATE_RUN_SUCCESS;
1096 if (state_run_failed(rc))
1098 if (flags & EXTENDED_ERROR_MSG_SUPPORTED)
1100 if (!license_send_error_alert(license, ERR_INVALID_CLIENT, ST_TOTAL_ABORT,
nullptr))
1102 WLog_Print(license->log, WLOG_ERROR,
"license_send_error_alert failed");
1105 license_set_state(license, LICENSE_STATE_ABORTED);
1111BOOL license_generate_randoms(rdpLicense* license)
1113 WINPR_ASSERT(license);
1115#ifdef LICENSE_NULL_CLIENT_RANDOM
1116 ZeroMemory(license->ClientRandom,
sizeof(license->ClientRandom));
1118 if (winpr_RAND(license->ClientRandom,
sizeof(license->ClientRandom)) < 0)
1122 if (winpr_RAND(license->ServerRandom,
sizeof(license->ServerRandom)) < 0)
1125#ifdef LICENSE_NULL_PREMASTER_SECRET
1126 ZeroMemory(license->PremasterSecret,
sizeof(license->PremasterSecret));
1128 if (winpr_RAND(license->PremasterSecret,
sizeof(license->PremasterSecret)) <
1140static BOOL license_generate_keys(rdpLicense* license)
1142 WINPR_ASSERT(license);
1146 !security_master_secret(license->PremasterSecret,
sizeof(license->PremasterSecret),
1147 license->ClientRandom,
sizeof(license->ClientRandom),
1148 license->ServerRandom,
sizeof(license->ServerRandom),
1149 license->MasterSecret,
sizeof(license->MasterSecret)) ||
1151 !security_session_key_blob(license->MasterSecret,
sizeof(license->MasterSecret),
1152 license->ClientRandom,
sizeof(license->ClientRandom),
1153 license->ServerRandom,
sizeof(license->ServerRandom),
1154 license->SessionKeyBlob,
sizeof(license->SessionKeyBlob)))
1158 security_mac_salt_key(license->SessionKeyBlob,
sizeof(license->SessionKeyBlob),
1159 license->ClientRandom,
sizeof(license->ClientRandom),
1160 license->ServerRandom,
sizeof(license->ServerRandom), license->MacSaltKey,
1161 sizeof(license->MacSaltKey));
1162 const BOOL ret = security_licensing_encryption_key(
1163 license->SessionKeyBlob,
sizeof(license->SessionKeyBlob), license->ClientRandom,
1164 sizeof(license->ClientRandom), license->ServerRandom,
sizeof(license->ServerRandom),
1165 license->LicensingEncryptionKey,
1166 sizeof(license->LicensingEncryptionKey));
1168 WLog_Print(license->log, WLOG_TRACE,
"license keys %s generated", ret ?
"successfully" :
"NOT");
1170#ifdef WITH_DEBUG_LICENSE
1171 WLog_Print(license->log, WLOG_DEBUG,
"ClientRandom:");
1172 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ClientRandom,
1173 sizeof(license->ClientRandom));
1174 WLog_Print(license->log, WLOG_DEBUG,
"ServerRandom:");
1175 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ServerRandom,
1176 sizeof(license->ServerRandom));
1177 WLog_Print(license->log, WLOG_DEBUG,
"PremasterSecret:");
1178 winpr_HexLogDump(license->log, WLOG_DEBUG, license->PremasterSecret,
1179 sizeof(license->PremasterSecret));
1180 WLog_Print(license->log, WLOG_DEBUG,
"MasterSecret:");
1181 winpr_HexLogDump(license->log, WLOG_DEBUG, license->MasterSecret,
1182 sizeof(license->MasterSecret));
1183 WLog_Print(license->log, WLOG_DEBUG,
"SessionKeyBlob:");
1184 winpr_HexLogDump(license->log, WLOG_DEBUG, license->SessionKeyBlob,
1185 sizeof(license->SessionKeyBlob));
1186 WLog_Print(license->log, WLOG_DEBUG,
"MacSaltKey:");
1187 winpr_HexLogDump(license->log, WLOG_DEBUG, license->MacSaltKey,
sizeof(license->MacSaltKey));
1188 WLog_Print(license->log, WLOG_DEBUG,
"LicensingEncryptionKey:");
1189 winpr_HexLogDump(license->log, WLOG_DEBUG, license->LicensingEncryptionKey,
1190 sizeof(license->LicensingEncryptionKey));
1200BOOL license_generate_hwid(rdpLicense* license)
1202 const BYTE* hashTarget =
nullptr;
1203 size_t targetLen = 0;
1204 BYTE macAddress[6] = WINPR_C_ARRAY_INIT;
1206 WINPR_ASSERT(license);
1207 WINPR_ASSERT(license->rdp);
1208 WINPR_ASSERT(license->rdp->settings);
1210 ZeroMemory(license->HardwareId,
sizeof(license->HardwareId));
1212 if (license->rdp->settings->OldLicenseBehaviour)
1214 hashTarget = macAddress;
1215 targetLen =
sizeof(macAddress);
1219 wStream buffer = WINPR_C_ARRAY_INIT;
1220 const char* hostname = license->rdp->settings->ClientHostname;
1221 wStream* s = Stream_StaticInit(&buffer, license->HardwareId, 4);
1222 Stream_Write_UINT32(s, license->PlatformId);
1224 hashTarget = (
const BYTE*)hostname;
1225 targetLen = hostname ? strlen(hostname) : 0;
1234 return winpr_Digest_Allow_FIPS(WINPR_MD_MD5, hashTarget, targetLen,
1235 &license->HardwareId[HWID_PLATFORM_ID_LENGTH],
1236 WINPR_MD5_DIGEST_LENGTH);
1240static BOOL license_get_server_rsa_public_key(rdpLicense* license)
1242 rdpSettings* settings =
nullptr;
1244 WINPR_ASSERT(license);
1245 WINPR_ASSERT(license->certificate);
1246 WINPR_ASSERT(license->rdp);
1248 settings = license->rdp->settings;
1249 WINPR_ASSERT(settings);
1251 if (license->ServerCertificate->length < 1)
1253 if (!freerdp_certificate_read_server_cert(license->certificate, settings->ServerCertificate,
1254 settings->ServerCertificateLength))
1261BOOL license_encrypt_premaster_secret(rdpLicense* license)
1263 WINPR_ASSERT(license);
1264 WINPR_ASSERT(license->certificate);
1266 if (!license_get_server_rsa_public_key(license))
1269 WINPR_ASSERT(license->EncryptedPremasterSecret);
1271 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1274 WLog_Print(license->log, WLOG_ERROR,
"info=%p, license->certificate=%p", (
const void*)info,
1275 (
void*)license->certificate);
1279#ifdef WITH_DEBUG_LICENSE
1280 WLog_Print(license->log, WLOG_DEBUG,
"Modulus (%" PRIu32
" bits):", info->ModulusLength * 8);
1281 winpr_HexLogDump(license->log, WLOG_DEBUG, info->Modulus, info->ModulusLength);
1282 WLog_Print(license->log, WLOG_DEBUG,
"Exponent:");
1283 winpr_HexLogDump(license->log, WLOG_DEBUG, info->exponent,
sizeof(info->exponent));
1286 BYTE* EncryptedPremasterSecret = (BYTE*)calloc(1, info->ModulusLength);
1287 if (!EncryptedPremasterSecret)
1289 WLog_Print(license->log, WLOG_ERROR,
1290 "EncryptedPremasterSecret=%p, info->ModulusLength=%" PRIu32,
1291 (
const void*)EncryptedPremasterSecret, info->ModulusLength);
1295 license->EncryptedPremasterSecret->type = BB_RANDOM_BLOB;
1296 license->EncryptedPremasterSecret->length =
sizeof(license->PremasterSecret);
1297#ifndef LICENSE_NULL_PREMASTER_SECRET
1299 const SSIZE_T length =
1300 crypto_rsa_public_encrypt(license->PremasterSecret,
sizeof(license->PremasterSecret),
1301 info, EncryptedPremasterSecret, info->ModulusLength);
1302 if ((length < 0) || (length > UINT16_MAX))
1304 WLog_Print(license->log, WLOG_ERROR,
"RSA public encrypt length=%" PRIdz
" < 0 || > %d",
1305 length, UINT16_MAX);
1308 license->EncryptedPremasterSecret->length = (UINT16)length;
1311 license->EncryptedPremasterSecret->data = EncryptedPremasterSecret;
1316static BOOL license_rc4_with_licenseKey(
const rdpLicense* license,
const BYTE* input,
size_t len,
1319 WINPR_ASSERT(license);
1320 WINPR_ASSERT(input || (len == 0));
1321 WINPR_ASSERT(target);
1322 WINPR_ASSERT(len <= UINT16_MAX);
1324 WINPR_RC4_CTX* rc4 = winpr_RC4_New_Allow_FIPS(license->LicensingEncryptionKey,
1325 sizeof(license->LicensingEncryptionKey));
1328 WLog_Print(license->log, WLOG_ERROR,
"Failed to allocate RC4");
1332 BYTE* buffer =
nullptr;
1334 buffer = realloc(target->data, len);
1338 target->data = buffer;
1339 target->length = (UINT16)len;
1341 if (!winpr_RC4_Update(rc4, len, input, buffer))
1344 winpr_RC4_Free(rc4);
1348 WLog_Print(license->log, WLOG_ERROR,
"Failed to create/update RC4: len=%" PRIuz
", buffer=%p",
1349 len, (
const void*)buffer);
1350 winpr_RC4_Free(rc4);
1366static BOOL license_encrypt_and_MAC(rdpLicense* license,
const BYTE* input,
size_t len,
1369 WINPR_ASSERT(license);
1370 return license_rc4_with_licenseKey(license, input, len, target) &&
1371 security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), input, len, mac,
1387static BOOL license_decrypt_and_check_MAC(rdpLicense* license,
const BYTE* input,
size_t len,
1390 BYTE macData[
sizeof(license->MACData)] = WINPR_C_ARRAY_INIT;
1392 WINPR_ASSERT(license);
1393 WINPR_ASSERT(target);
1397 WLog_Print(license->log, WLOG_DEBUG,
"TransportDumpReplay active, skipping...");
1401 if (!license_rc4_with_licenseKey(license, input, len, target))
1404 if (!security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), target->data, len,
1405 macData,
sizeof(macData)))
1408 if (memcmp(packetMac, macData,
sizeof(macData)) != 0)
1410 WLog_Print(license->log, WLOG_ERROR,
"packetMac != expectedMac");
1425 WINPR_ASSERT(productInfo);
1427 if (!license_check_stream_length(log, s, 8,
"license product info::cbCompanyName"))
1430 Stream_Read_UINT32(s, productInfo->dwVersion);
1431 Stream_Read_UINT32(s, productInfo->cbCompanyName);
1434 if ((productInfo->cbCompanyName < 2) || (productInfo->cbCompanyName % 2 != 0))
1436 WLog_Print(log, WLOG_WARN,
"license product info invalid cbCompanyName %" PRIu32,
1437 productInfo->cbCompanyName);
1441 if (!license_check_stream_length(log, s, productInfo->cbCompanyName,
1442 "license product info::CompanyName"))
1445 productInfo->pbProductId =
nullptr;
1446 productInfo->pbCompanyName = (BYTE*)malloc(productInfo->cbCompanyName);
1447 if (!productInfo->pbCompanyName)
1449 Stream_Read(s, productInfo->pbCompanyName, productInfo->cbCompanyName);
1451 if (!license_check_stream_length(log, s, 4,
"license product info::cbProductId"))
1454 Stream_Read_UINT32(s, productInfo->cbProductId);
1456 if ((productInfo->cbProductId < 2) || (productInfo->cbProductId % 2 != 0))
1458 WLog_Print(log, WLOG_WARN,
"license product info invalid cbProductId %" PRIu32,
1459 productInfo->cbProductId);
1463 if (!license_check_stream_length(log, s, productInfo->cbProductId,
1464 "license product info::ProductId"))
1467 productInfo->pbProductId = (BYTE*)malloc(productInfo->cbProductId);
1468 if (!productInfo->pbProductId)
1470 Stream_Read(s, productInfo->pbProductId, productInfo->cbProductId);
1474 free(productInfo->pbCompanyName);
1475 free(productInfo->pbProductId);
1476 productInfo->pbCompanyName =
nullptr;
1477 productInfo->pbProductId =
nullptr;
1482static BOOL license_write_product_info(wLog* log,
wStream* s,
1485 WINPR_ASSERT(productInfo);
1487 if (!license_check_stream_capacity(log, s, 8,
"license product info::cbCompanyName"))
1490 Stream_Write_UINT32(s, productInfo->dwVersion);
1491 Stream_Write_UINT32(s, productInfo->cbCompanyName);
1494 if ((productInfo->cbCompanyName < 2) || (productInfo->cbCompanyName % 2 != 0) ||
1495 !productInfo->pbCompanyName)
1497 WLog_Print(log, WLOG_WARN,
"license product info invalid cbCompanyName %" PRIu32,
1498 productInfo->cbCompanyName);
1502 if (!license_check_stream_capacity(log, s, productInfo->cbCompanyName,
1503 "license product info::CompanyName"))
1506 Stream_Write(s, productInfo->pbCompanyName, productInfo->cbCompanyName);
1508 if (!license_check_stream_capacity(log, s, 4,
"license product info::cbProductId"))
1511 Stream_Write_UINT32(s, productInfo->cbProductId);
1513 if ((productInfo->cbProductId < 2) || (productInfo->cbProductId % 2 != 0) ||
1514 !productInfo->pbProductId)
1516 WLog_Print(log, WLOG_WARN,
"license product info invalid cbProductId %" PRIu32,
1517 productInfo->cbProductId);
1521 if (!license_check_stream_capacity(log, s, productInfo->cbProductId,
1522 "license product info::ProductId"))
1525 Stream_Write(s, productInfo->pbProductId, productInfo->cbProductId);
1554 free(productInfo->pbCompanyName);
1555 free(productInfo->pbProductId);
1560BOOL license_read_binary_blob_data(wLog* log,
LICENSE_BLOB* blob, UINT16 wBlobType,
1561 const void* data,
size_t length)
1564 WINPR_ASSERT(length <= UINT16_MAX);
1565 WINPR_ASSERT(data || (length == 0));
1567 blob->length = (UINT16)length;
1569 blob->data =
nullptr;
1571 if ((blob->type != wBlobType) && (blob->type != BB_ANY_BLOB))
1573 WLog_Print(log, WLOG_ERROR,
"license binary blob::type expected %s, got %s",
1574 license_blob_type_string(wBlobType), license_blob_type_string(blob->type));
1581 if ((blob->type != BB_ANY_BLOB) && (blob->length == 0))
1583 WLog_Print(log, WLOG_DEBUG,
"license binary blob::type %s, length=0, skipping.",
1584 license_blob_type_string(blob->type));
1588 blob->type = wBlobType;
1589 blob->data =
nullptr;
1590 if (blob->length > 0)
1591 blob->data = malloc(blob->length);
1594 WLog_Print(log, WLOG_ERROR,
"license binary blob::length=%" PRIu16
", blob::data=%p",
1595 blob->length, (
const void*)blob->data);
1598 memcpy(blob->data, data, blob->length);
1611 UINT16 wBlobType = 0;
1616 if (!license_check_stream_length(log, s, 4,
"license binary blob::type"))
1619 Stream_Read_UINT16(s, wBlobType);
1620 Stream_Read_UINT16(s, length);
1622 if (!license_check_stream_length(log, s, length,
"license binary blob::length"))
1625 if (!license_read_binary_blob_data(log, blob, wBlobType, Stream_Pointer(s), length))
1628 return Stream_SafeSeek(s, length);
1642 if (!Stream_EnsureRemainingCapacity(s, blob->length + 4))
1645 Stream_Write_UINT16(s, blob->type);
1646 Stream_Write_UINT16(s, blob->length);
1648 if (blob->length > 0)
1649 Stream_Write(s, blob->data, blob->length);
1654static BOOL license_write_encrypted_premaster_secret_blob(wLog* log,
wStream* s,
1656 UINT32 ModulusLength)
1658 const UINT32 length = ModulusLength + 8;
1661 WINPR_ASSERT(length <= UINT16_MAX);
1663 if (blob->length > ModulusLength)
1665 WLog_Print(log, WLOG_ERROR,
"invalid blob");
1669 if (!Stream_EnsureRemainingCapacity(s, length + 4))
1671 Stream_Write_UINT16(s, blob->type);
1672 Stream_Write_UINT16(s, (UINT16)length);
1674 if (blob->length > 0)
1675 Stream_Write(s, blob->data, blob->length);
1677 Stream_Zero(s, length - blob->length);
1682static BOOL license_read_encrypted_premaster_secret_blob(wLog* log,
wStream* s,
LICENSE_BLOB* blob,
1683 UINT32* ModulusLength)
1685 if (!license_read_binary_blob(log, s, blob))
1687 WINPR_ASSERT(ModulusLength);
1688 *ModulusLength = blob->length;
1730 UINT32 scopeCount = 0;
1732 WINPR_ASSERT(scopeList);
1734 if (!license_check_stream_length(log, s, 4,
"license scope list"))
1737 Stream_Read_UINT32(s, scopeCount);
1739 if (!license_check_stream_length(log, s, 4ull * scopeCount,
"license scope list::count"))
1742 if (!license_scope_list_resize(scopeList, scopeCount))
1745 for (UINT32 i = 0; i < scopeCount; i++)
1747 if (!license_read_binary_blob(log, s, scopeList->array[i]))
1754BOOL license_write_scope_list(wLog* log,
wStream* s,
const SCOPE_LIST* scopeList)
1756 WINPR_ASSERT(scopeList);
1758 if (!license_check_stream_capacity(log, s, 4,
"license scope list"))
1761 Stream_Write_UINT32(s, scopeList->count);
1763 if (!license_check_stream_capacity(log, s, scopeList->count * 4ull,
1764 "license scope list::count"))
1768 WINPR_ASSERT(scopeList->array || (scopeList->count == 0));
1769 for (UINT32 i = 0; i < scopeList->count; i++)
1773 if (!license_write_binary_blob(s, element))
1792static void license_scope_list_free(
SCOPE_LIST* scopeList, UINT32 count)
1794 WINPR_ASSERT(scopeList);
1795 WINPR_ASSERT(scopeList->array || (scopeList->count == 0));
1797 for (UINT32 x = count; x < scopeList->count; x++)
1799 license_free_binary_blob(scopeList->array[x]);
1800 scopeList->array[x] =
nullptr;
1805 free((
void*)scopeList->array);
1806 scopeList->array =
nullptr;
1810BOOL license_scope_list_resize(
SCOPE_LIST* scopeList, UINT32 count)
1812 license_scope_list_free(scopeList, count);
1819 scopeList->array = tmp;
1822 for (UINT32 x = scopeList->count; x < count; x++)
1824 LICENSE_BLOB* blob = license_new_binary_blob(BB_SCOPE_BLOB);
1827 scopeList->count = x;
1830 scopeList->array[x] = blob;
1833 scopeList->count = count;
1843void license_free_scope_list(
SCOPE_LIST* scopeList)
1848 license_scope_list_free(scopeList, 0);
1852BOOL license_send_license_info(rdpLicense* license,
const LICENSE_BLOB* calBlob,
1853 const BYTE* signature,
size_t signature_length)
1855 WINPR_ASSERT(calBlob);
1856 WINPR_ASSERT(signature);
1857 WINPR_ASSERT(license->certificate);
1859 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1863 UINT16 sec_flags = 0;
1864 wStream* s = license_send_stream_init(license, &sec_flags);
1868 if (!license_check_stream_capacity(license->log, s, 8 +
sizeof(license->ClientRandom),
1869 "license info::ClientRandom"))
1872 Stream_Write_UINT32(s,
1873 license->PreferredKeyExchangeAlg);
1874 Stream_Write_UINT32(s, license->PlatformId);
1877 Stream_Write(s, license->ClientRandom,
sizeof(license->ClientRandom));
1880 if (!license_write_encrypted_premaster_secret_blob(
1881 license->log, s, license->EncryptedPremasterSecret, info->ModulusLength))
1885 if (!license_write_binary_blob(s, calBlob))
1889 if (!license_write_binary_blob(s, license->EncryptedHardwareId))
1893 if (!license_check_stream_capacity(license->log, s, signature_length,
"license info::MACData"))
1895 Stream_Write(s, signature, signature_length);
1897 return license_send(license, s, LICENSE_INFO, sec_flags);
1905static BOOL license_check_preferred_alg(rdpLicense* license, UINT32 PreferredKeyExchangeAlg,
1908 WINPR_ASSERT(license);
1909 WINPR_ASSERT(where);
1911 if (license->PreferredKeyExchangeAlg != PreferredKeyExchangeAlg)
1913 char buffer1[64] = WINPR_C_ARRAY_INIT;
1914 char buffer2[64] = WINPR_C_ARRAY_INIT;
1915 WLog_Print(license->log, WLOG_WARN,
"%s::PreferredKeyExchangeAlg, expected %s, got %s",
1917 license_preferred_key_exchange_alg_string(license->PreferredKeyExchangeAlg,
1918 buffer1,
sizeof(buffer1)),
1919 license_preferred_key_exchange_alg_string(PreferredKeyExchangeAlg, buffer2,
1926BOOL license_read_license_info(rdpLicense* license,
wStream* s)
1929 UINT32 PreferredKeyExchangeAlg = 0;
1931 WINPR_ASSERT(license);
1932 WINPR_ASSERT(license->certificate);
1934 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1939 if (!license_check_stream_length(license->log, s, 8 +
sizeof(license->ClientRandom),
1943 Stream_Read_UINT32(s, PreferredKeyExchangeAlg);
1944 if (!license_check_preferred_alg(license, PreferredKeyExchangeAlg,
"license info"))
1946 Stream_Read_UINT32(s, license->PlatformId);
1949 Stream_Read(s, license->ClientRandom,
sizeof(license->ClientRandom));
1953 UINT32 ModulusLength = 0;
1954 if (!license_read_encrypted_premaster_secret_blob(
1955 license->log, s, license->EncryptedPremasterSecret, &ModulusLength))
1958 if (ModulusLength != info->ModulusLength)
1960 WLog_Print(license->log, WLOG_WARN,
1961 "EncryptedPremasterSecret,::ModulusLength[%" PRIu32
1962 "] != rdpCertInfo::ModulusLength[%" PRIu32
"]",
1963 ModulusLength, info->ModulusLength);
1969 if (!license_read_binary_blob(license->log, s, license->LicenseInfo))
1973 if (!license_read_binary_blob(license->log, s, license->EncryptedHardwareId))
1977 if (!license_check_stream_length(license->log, s,
sizeof(license->MACData),
1978 "license info::MACData"))
1980 Stream_Read(s, license->MACData,
sizeof(license->MACData));
1995BOOL license_read_license_request_packet(rdpLicense* license,
wStream* s)
1997 WINPR_ASSERT(license);
2000 if (!license_check_stream_length(license->log, s,
sizeof(license->ServerRandom),
2004 Stream_Read(s, license->ServerRandom,
sizeof(license->ServerRandom));
2007 if (!license_read_product_info(license->log, s, license->ProductInfo))
2011 if (!license_read_binary_blob(license->log, s, license->KeyExchangeList))
2015 if (!license_read_binary_blob(license->log, s, license->ServerCertificate))
2019 if (!license_read_scope_list(license->log, s, license->ScopeList))
2023 if (!freerdp_certificate_read_server_cert(license->certificate,
2024 license->ServerCertificate->data,
2025 license->ServerCertificate->length))
2028 if (!license_generate_keys(license) || !license_generate_hwid(license) ||
2029 !license_encrypt_premaster_secret(license))
2032#ifdef WITH_DEBUG_LICENSE
2033 WLog_Print(license->log, WLOG_DEBUG,
"ServerRandom:");
2034 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ServerRandom,
2035 sizeof(license->ServerRandom));
2036 license_print_product_info(license->log, license->ProductInfo);
2037 license_print_scope_list(license->log, license->ScopeList);
2042BOOL license_write_license_request_packet(
const rdpLicense* license,
wStream* s)
2044 WINPR_ASSERT(license);
2047 if (!license_check_stream_capacity(license->log, s,
sizeof(license->ServerRandom),
2050 Stream_Write(s, license->ServerRandom,
sizeof(license->ServerRandom));
2053 if (!license_write_product_info(license->log, s, license->ProductInfo))
2057 if (!license_write_binary_blob(s, license->KeyExchangeList))
2061 if (!license_write_binary_blob(s, license->ServerCertificate))
2065 if (!license_write_scope_list(license->log, s, license->ScopeList))
2072static BOOL license_send_license_request_packet(rdpLicense* license)
2074 UINT16 sec_flags = 0;
2075 wStream* s = license_send_stream_init(license, &sec_flags);
2079 if (!license_write_license_request_packet(license, s))
2082 return license_send(license, s, LICENSE_REQUEST, sec_flags);
2096BOOL license_read_platform_challenge_packet(rdpLicense* license,
wStream* s)
2098 BYTE macData[LICENSING_ENCRYPTION_KEY_LENGTH] = WINPR_C_ARRAY_INIT;
2100 WINPR_ASSERT(license);
2102 DEBUG_LICENSE(
"Receiving Platform Challenge Packet");
2104 if (!license_check_stream_length(license->log, s, 4,
"license platform challenge"))
2109 Stream_Seek_UINT32(s);
2112 license->EncryptedPlatformChallenge->type = BB_ANY_BLOB;
2113 if (!license_read_binary_blob(license->log, s, license->EncryptedPlatformChallenge))
2115 license->EncryptedPlatformChallenge->type = BB_ENCRYPTED_DATA_BLOB;
2118 if (!license_check_stream_length(license->log, s,
sizeof(macData),
2119 "license platform challenge::MAC"))
2122 Stream_Read(s, macData,
sizeof(macData));
2123 if (!license_decrypt_and_check_MAC(license, license->EncryptedPlatformChallenge->data,
2124 license->EncryptedPlatformChallenge->length,
2125 license->PlatformChallenge, macData))
2128 WLog_Print(license->log, WLOG_TRACE,
"platform challenge read");
2130#ifdef WITH_DEBUG_LICENSE
2131 WLog_Print(license->log, WLOG_DEBUG,
"EncryptedPlatformChallenge:");
2132 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedPlatformChallenge->data,
2133 license->EncryptedPlatformChallenge->length);
2134 WLog_Print(license->log, WLOG_DEBUG,
"PlatformChallenge:");
2135 winpr_HexLogDump(license->log, WLOG_DEBUG, license->PlatformChallenge->data,
2136 license->PlatformChallenge->length);
2137 WLog_Print(license->log, WLOG_DEBUG,
"MacData:");
2138 winpr_HexLogDump(license->log, WLOG_DEBUG, macData,
sizeof(macData));
2143BOOL license_send_error_alert(rdpLicense* license, UINT32 dwErrorCode, UINT32 dwStateTransition,
2146 UINT16 sec_flags = 0;
2147 wStream* s = license_send_stream_init(license, &sec_flags);
2152 if (!license_check_stream_capacity(license->log, s, 8,
"license error alert"))
2154 Stream_Write_UINT32(s, dwErrorCode);
2155 Stream_Write_UINT32(s, dwStateTransition);
2159 if (!license_write_binary_blob(s, info))
2163 return license_send(license, s, ERROR_ALERT, sec_flags);
2169BOOL license_send_platform_challenge_packet(rdpLicense* license)
2171 UINT16 sec_flags = 0;
2172 wStream* s = license_send_stream_init(license, &sec_flags);
2177 DEBUG_LICENSE(
"Receiving Platform Challenge Packet");
2179 if (!license_check_stream_capacity(license->log, s, 4,
"license platform challenge"))
2185 if (!license_write_binary_blob(s, license->EncryptedPlatformChallenge))
2189 if (!license_check_stream_length(license->log, s,
sizeof(license->MACData),
2190 "license platform challenge::MAC"))
2193 Stream_Write(s, license->MACData,
sizeof(license->MACData));
2195 return license_send(license, s, PLATFORM_CHALLENGE, sec_flags);
2202static BOOL license_read_encrypted_blob(
const rdpLicense* license,
wStream* s,
LICENSE_BLOB* target)
2204 UINT16 wBlobType = 0;
2205 UINT16 wBlobLen = 0;
2207 WINPR_ASSERT(license);
2208 WINPR_ASSERT(target);
2210 if (!license_check_stream_length(license->log, s, 4,
"license encrypted blob"))
2213 Stream_Read_UINT16(s, wBlobType);
2214 if (wBlobType != BB_ENCRYPTED_DATA_BLOB)
2217 license->log, WLOG_WARN,
2218 "expecting BB_ENCRYPTED_DATA_BLOB blob, probably a windows 2003 server, continuing...");
2221 Stream_Read_UINT16(s, wBlobLen);
2223 BYTE* encryptedData = Stream_Pointer(s);
2224 if (!Stream_SafeSeek(s, wBlobLen))
2226 WLog_Print(license->log, WLOG_WARN,
2227 "short license encrypted blob::length, expected %" PRIu16
" bytes, got %" PRIuz,
2228 wBlobLen, Stream_GetRemainingLength(s));
2232 return license_rc4_with_licenseKey(license, encryptedData, wBlobLen, target);
2242BOOL license_read_new_or_upgrade_license_packet(rdpLicense* license,
wStream* s)
2244 UINT32 os_major = 0;
2245 UINT32 os_minor = 0;
2247 UINT32 cbCompanyName = 0;
2248 UINT32 cbProductId = 0;
2249 UINT32 cbLicenseInfo = 0;
2250 wStream sbuffer = WINPR_C_ARRAY_INIT;
2251 wStream* licenseStream =
nullptr;
2253 BYTE computedMac[16] = WINPR_C_ARRAY_INIT;
2254 const BYTE* readMac =
nullptr;
2256 WINPR_ASSERT(license);
2258 DEBUG_LICENSE(
"Receiving Server New/Upgrade License Packet");
2260 LICENSE_BLOB* calBlob = license_new_binary_blob(BB_DATA_BLOB);
2265 if (!license_read_encrypted_blob(license, s, calBlob))
2269 readMac = Stream_Pointer(s);
2270 if (!Stream_SafeSeek(s,
sizeof(computedMac)))
2272 WLog_Print(license->log, WLOG_WARN,
2273 "short license new/upgrade, expected 16 bytes, got %" PRIuz,
2274 Stream_GetRemainingLength(s));
2278 if (!security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), calBlob->data,
2279 calBlob->length, computedMac,
sizeof(computedMac)))
2282 if (memcmp(computedMac, readMac,
sizeof(computedMac)) != 0)
2284 WLog_Print(license->log, WLOG_ERROR,
"new or upgrade license MAC mismatch");
2288 licenseStream = Stream_StaticConstInit(&sbuffer, calBlob->data, calBlob->length);
2291 WLog_Print(license->log, WLOG_ERROR,
2292 "license::blob::data=%p, license::blob::length=%" PRIu16,
2293 (
const void*)calBlob->data, calBlob->length);
2297 if (!license_check_stream_length(license->log, licenseStream, 8,
2298 "license new/upgrade::blob::version"))
2301 Stream_Read_UINT16(licenseStream, os_minor);
2302 Stream_Read_UINT16(licenseStream, os_major);
2304 WLog_Print(license->log, WLOG_DEBUG,
"Version: %" PRIu16
".%" PRIu16, os_major, os_minor);
2307 Stream_Read_UINT32(licenseStream, cbScope);
2308 if (!license_check_stream_length(license->log, licenseStream, cbScope,
2309 "license new/upgrade::blob::scope"))
2312#ifdef WITH_DEBUG_LICENSE
2313 WLog_Print(license->log, WLOG_DEBUG,
"Scope:");
2314 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbScope);
2316 Stream_Seek(licenseStream, cbScope);
2319 if (!license_check_stream_length(license->log, licenseStream, 4,
2320 "license new/upgrade::blob::cbCompanyName"))
2323 Stream_Read_UINT32(licenseStream, cbCompanyName);
2324 if (!license_check_stream_length(license->log, licenseStream, cbCompanyName,
2325 "license new/upgrade::blob::CompanyName"))
2328#ifdef WITH_DEBUG_LICENSE
2329 WLog_Print(license->log, WLOG_DEBUG,
"Company name:");
2330 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbCompanyName);
2332 Stream_Seek(licenseStream, cbCompanyName);
2335 if (!license_check_stream_length(license->log, licenseStream, 4,
2336 "license new/upgrade::blob::cbProductId"))
2339 Stream_Read_UINT32(licenseStream, cbProductId);
2341 if (!license_check_stream_length(license->log, licenseStream, cbProductId,
2342 "license new/upgrade::blob::ProductId"))
2345#ifdef WITH_DEBUG_LICENSE
2346 WLog_Print(license->log, WLOG_DEBUG,
"Product id:");
2347 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbProductId);
2349 Stream_Seek(licenseStream, cbProductId);
2352 if (!license_check_stream_length(license->log, licenseStream, 4,
2353 "license new/upgrade::blob::cbLicenseInfo"))
2356 Stream_Read_UINT32(licenseStream, cbLicenseInfo);
2357 if (!license_check_stream_length(license->log, licenseStream, cbLicenseInfo,
2358 "license new/upgrade::blob::LicenseInfo"))
2361 license->type = LICENSE_TYPE_ISSUED;
2362 license_set_state(license, LICENSE_STATE_COMPLETED);
2365 if (!license->rdp->settings->OldLicenseBehaviour)
2366 ret = saveCal(license->log, license->rdp->settings, Stream_Pointer(licenseStream),
2367 cbLicenseInfo, license->rdp->settings->ClientHostname);
2370 license_free_binary_blob(calBlob);
2381BOOL license_read_error_alert_packet(rdpLicense* license,
wStream* s)
2383 UINT32 dwErrorCode = 0;
2384 UINT32 dwStateTransition = 0;
2386 WINPR_ASSERT(license);
2387 WINPR_ASSERT(license->rdp);
2389 if (!license_check_stream_length(license->log, s, 8ul,
"error alert"))
2392 Stream_Read_UINT32(s, dwErrorCode);
2393 Stream_Read_UINT32(s, dwStateTransition);
2395 if (!license_read_binary_blob(license->log, s, license->ErrorInfo))
2398#ifdef WITH_DEBUG_LICENSE
2399 WLog_Print(license->log, WLOG_DEBUG,
"dwErrorCode: %s, dwStateTransition: %s",
2400 error_codes[dwErrorCode], state_transitions[dwStateTransition]);
2403 if (dwErrorCode == STATUS_VALID_CLIENT)
2405 license->type = LICENSE_TYPE_NONE;
2406 license_set_state(license, LICENSE_STATE_COMPLETED);
2410 switch (dwStateTransition)
2412 case ST_TOTAL_ABORT:
2413 license_set_state(license, LICENSE_STATE_ABORTED);
2415 case ST_NO_TRANSITION:
2416 license_set_state(license, LICENSE_STATE_COMPLETED);
2418 case ST_RESET_PHASE_TO_START:
2419 license_set_state(license, LICENSE_STATE_CONFIGURED);
2421 case ST_RESEND_LAST_MESSAGE:
2437BOOL license_write_new_license_request_packet(
const rdpLicense* license,
wStream* s)
2439 WINPR_ASSERT(license);
2441 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
2445 if (!license_check_stream_capacity(license->log, s, 8 +
sizeof(license->ClientRandom),
2449 Stream_Write_UINT32(s,
2450 license->PreferredKeyExchangeAlg);
2451 Stream_Write_UINT32(s, license->PlatformId);
2452 Stream_Write(s, license->ClientRandom,
2453 sizeof(license->ClientRandom));
2456 !license_write_encrypted_premaster_secret_blob(
2457 license->log, s, license->EncryptedPremasterSecret, info->ModulusLength) ||
2459 !license_write_binary_blob(s, license->ClientUserName) ||
2461 !license_write_binary_blob(s, license->ClientMachineName))
2466 WLog_Print(license->log, WLOG_TRACE,
"new license written");
2468#ifdef WITH_DEBUG_LICENSE
2469 WLog_Print(license->log, WLOG_DEBUG,
"PreferredKeyExchangeAlg: 0x%08" PRIX32
"",
2470 license->PreferredKeyExchangeAlg);
2471 WLog_Print(license->log, WLOG_DEBUG,
"ClientRandom:");
2472 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ClientRandom,
2473 sizeof(license->ClientRandom));
2474 WLog_Print(license->log, WLOG_DEBUG,
"EncryptedPremasterSecret");
2475 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedPremasterSecret->data,
2476 license->EncryptedPremasterSecret->length);
2477 WLog_Print(license->log, WLOG_DEBUG,
"ClientUserName (%" PRIu16
"): %s",
2478 license->ClientUserName->length, (
char*)license->ClientUserName->data);
2479 WLog_Print(license->log, WLOG_DEBUG,
"ClientMachineName (%" PRIu16
"): %s",
2480 license->ClientMachineName->length, (
char*)license->ClientMachineName->data);
2485BOOL license_read_new_license_request_packet(rdpLicense* license,
wStream* s)
2487 UINT32 PreferredKeyExchangeAlg = 0;
2489 WINPR_ASSERT(license);
2491 if (!license_check_stream_length(license->log, s, 8ull +
sizeof(license->ClientRandom),
2492 "new license request"))
2495 Stream_Read_UINT32(s, PreferredKeyExchangeAlg);
2496 if (!license_check_preferred_alg(license, PreferredKeyExchangeAlg,
"new license request"))
2499 Stream_Read_UINT32(s, license->PlatformId);
2500 Stream_Read(s, license->ClientRandom,
2501 sizeof(license->ClientRandom));
2504 UINT32 ModulusLength = 0;
2505 if (!license_read_encrypted_premaster_secret_blob(
2506 license->log, s, license->EncryptedPremasterSecret, &ModulusLength))
2509 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
2511 WLog_Print(license->log, WLOG_WARN,
2512 "Missing license certificate, skipping ModulusLength checks");
2513 else if (ModulusLength != info->ModulusLength)
2515 WLog_Print(license->log, WLOG_WARN,
2516 "EncryptedPremasterSecret expected to be %" PRIu32
" bytes, but read %" PRIu32
2518 info->ModulusLength, ModulusLength);
2523 if (!license_read_binary_blob(license->log, s, license->ClientUserName))
2526 if (!license_read_binary_blob(license->log, s, license->ClientMachineName))
2538BOOL license_answer_license_request(rdpLicense* license)
2540 UINT16 sec_flags = 0;
2542 BYTE* license_data =
nullptr;
2543 size_t license_size = 0;
2545 char* username =
nullptr;
2547 WINPR_ASSERT(license);
2548 WINPR_ASSERT(license->rdp);
2549 WINPR_ASSERT(license->rdp->settings);
2551 if (!license->rdp->settings->OldLicenseBehaviour)
2552 license_data = loadCalFile(license->log, license->rdp->settings,
2553 license->rdp->settings->ClientHostname, &license_size);
2558 BYTE signature[LICENSING_ENCRYPTION_KEY_LENGTH] = WINPR_C_ARRAY_INIT;
2560 DEBUG_LICENSE(
"Sending Saved License Packet");
2562 WINPR_ASSERT(license->EncryptedHardwareId);
2563 license->EncryptedHardwareId->type = BB_ENCRYPTED_DATA_BLOB;
2564 if (!license_encrypt_and_MAC(license, license->HardwareId,
sizeof(license->HardwareId),
2565 license->EncryptedHardwareId, signature,
sizeof(signature)))
2571 calBlob = license_new_binary_blob(BB_DATA_BLOB);
2577 calBlob->data = license_data;
2578 WINPR_ASSERT(license_size <= UINT16_MAX);
2579 calBlob->length = (UINT16)license_size;
2581 status = license_send_license_info(license, calBlob, signature,
sizeof(signature));
2582 license_free_binary_blob(calBlob);
2587 DEBUG_LICENSE(
"Sending New License Packet");
2589 s = license_send_stream_init(license, &sec_flags);
2592 if (license->rdp->settings->Username !=
nullptr)
2593 username = license->rdp->settings->Username;
2595 username =
"username";
2598 WINPR_ASSERT(license->ClientUserName);
2599 const size_t len = strlen(username) + 1;
2600 WINPR_ASSERT(len <= UINT16_MAX);
2602 license->ClientUserName->data = (BYTE*)username;
2603 license->ClientUserName->length = (UINT16)len;
2607 WINPR_ASSERT(license->ClientMachineName);
2608 const size_t len = strlen(license->rdp->settings->ClientHostname) + 1;
2609 WINPR_ASSERT(len <= UINT16_MAX);
2611 license->ClientMachineName->data = (BYTE*)license->rdp->settings->ClientHostname;
2612 license->ClientMachineName->length = (UINT16)len;
2614 status = license_write_new_license_request_packet(license, s);
2616 WINPR_ASSERT(license->ClientUserName);
2617 license->ClientUserName->data =
nullptr;
2618 license->ClientUserName->length = 0;
2620 WINPR_ASSERT(license->ClientMachineName);
2621 license->ClientMachineName->data =
nullptr;
2622 license->ClientMachineName->length = 0;
2630 return license_send(license, s, NEW_LICENSE_REQUEST, sec_flags);
2639BOOL license_send_platform_challenge_response(rdpLicense* license)
2641 wStream* challengeRespData =
nullptr;
2642 BYTE* buffer =
nullptr;
2645 WINPR_ASSERT(license);
2646 WINPR_ASSERT(license->PlatformChallenge);
2647 WINPR_ASSERT(license->MacSaltKey);
2648 WINPR_ASSERT(license->EncryptedPlatformChallenge);
2649 WINPR_ASSERT(license->EncryptedHardwareId);
2651 DEBUG_LICENSE(
"Sending Platform Challenge Response Packet");
2653 license->EncryptedPlatformChallenge->type = BB_DATA_BLOB;
2656 challengeRespData = Stream_New(
nullptr, 8 + license->PlatformChallenge->length);
2657 if (!challengeRespData)
2659 Stream_Write_UINT16(challengeRespData, PLATFORM_CHALLENGE_RESPONSE_VERSION);
2660 Stream_Write_UINT16(challengeRespData, license->ClientType);
2661 Stream_Write_UINT16(challengeRespData, license->LicenseDetailLevel);
2662 Stream_Write_UINT16(challengeRespData, license->PlatformChallenge->length);
2663 Stream_Write(challengeRespData, license->PlatformChallenge->data,
2664 license->PlatformChallenge->length);
2665 Stream_SealLength(challengeRespData);
2668 const size_t length = Stream_Length(challengeRespData) +
sizeof(license->HardwareId);
2669 buffer = (BYTE*)malloc(length);
2672 Stream_Free(challengeRespData, TRUE);
2676 CopyMemory(buffer, Stream_Buffer(challengeRespData), Stream_Length(challengeRespData));
2677 CopyMemory(&buffer[Stream_Length(challengeRespData)], license->HardwareId,
2678 sizeof(license->HardwareId));
2679 status = security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), buffer, length,
2680 license->MACData,
sizeof(license->MACData));
2685 Stream_Free(challengeRespData, TRUE);
2689 license->EncryptedHardwareId->type = BB_ENCRYPTED_DATA_BLOB;
2690 if (!license_rc4_with_licenseKey(license, license->HardwareId,
sizeof(license->HardwareId),
2691 license->EncryptedHardwareId))
2693 Stream_Free(challengeRespData, TRUE);
2697 status = license_rc4_with_licenseKey(license, Stream_Buffer(challengeRespData),
2698 Stream_Length(challengeRespData),
2699 license->EncryptedPlatformChallengeResponse);
2700 Stream_Free(challengeRespData, TRUE);
2704#ifdef WITH_DEBUG_LICENSE
2705 WLog_Print(license->log, WLOG_DEBUG,
"LicensingEncryptionKey:");
2706 winpr_HexLogDump(license->log, WLOG_DEBUG, license->LicensingEncryptionKey, 16);
2707 WLog_Print(license->log, WLOG_DEBUG,
"HardwareId:");
2708 winpr_HexLogDump(license->log, WLOG_DEBUG, license->HardwareId,
sizeof(license->HardwareId));
2709 WLog_Print(license->log, WLOG_DEBUG,
"EncryptedHardwareId:");
2710 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedHardwareId->data,
2711 license->EncryptedHardwareId->length);
2713 UINT16 sec_flags = 0;
2714 wStream* s = license_send_stream_init(license, &sec_flags);
2718 if (license_write_client_platform_challenge_response(license, s))
2719 return license_send(license, s, PLATFORM_CHALLENGE_RESPONSE, sec_flags);
2725BOOL license_read_platform_challenge_response(WINPR_ATTR_UNUSED rdpLicense* license)
2727 WINPR_ASSERT(license);
2728 WINPR_ASSERT(license->PlatformChallenge);
2729 WINPR_ASSERT(license->MacSaltKey);
2730 WINPR_ASSERT(license->EncryptedPlatformChallenge);
2731 WINPR_ASSERT(license->EncryptedHardwareId);
2733 DEBUG_LICENSE(
"Receiving Platform Challenge Response Packet");
2735#if defined(WITH_LICENSE_DECRYPT_CHALLENGE_RESPONSE)
2737 LICENSE_BLOB* dblob = license_new_binary_blob(BB_ANY_BLOB);
2741 wStream sbuffer = WINPR_C_ARRAY_INIT;
2742 UINT16 wVersion = 0;
2743 UINT16 cbChallenge = 0;
2744 const BYTE* pbChallenge =
nullptr;
2745 LICENSE_BLOB* blob = license->EncryptedPlatformChallengeResponse;
2747 if (!license_rc4_with_licenseKey(license, blob->data, blob->length, dblob))
2750 wStream* s = Stream_StaticConstInit(&sbuffer, dblob->data, dblob->length);
2751 if (!license_check_stream_length(s, 8,
"PLATFORM_CHALLENGE_RESPONSE_DATA"))
2754 Stream_Read_UINT16(s, wVersion);
2755 if (wVersion != PLATFORM_CHALLENGE_RESPONSE_VERSION)
2757 WLog_Print(license->log, WLOG_WARN,
2758 "Invalid PLATFORM_CHALLENGE_RESPONSE_DATA::wVersion 0x%04" PRIx16
2759 ", expected 0x04" PRIx16,
2760 wVersion, PLATFORM_CHALLENGE_RESPONSE_VERSION);
2763 Stream_Read_UINT16(s, license->ClientType);
2764 Stream_Read_UINT16(s, license->LicenseDetailLevel);
2765 Stream_Read_UINT16(s, cbChallenge);
2767 if (!license_check_stream_length(s, cbChallenge,
2768 "PLATFORM_CHALLENGE_RESPONSE_DATA::pbChallenge"))
2771 pbChallenge = Stream_Pointer(s);
2772 if (!license_read_binary_blob_data(license->PlatformChallengeResponse, BB_DATA_BLOB,
2773 pbChallenge, cbChallenge))
2775 if (!Stream_SafeSeek(s, cbChallenge))
2780 license_free_binary_blob(dblob);
2787BOOL license_write_client_platform_challenge_response(rdpLicense* license,
wStream* s)
2789 WINPR_ASSERT(license);
2791 if (!license_write_binary_blob(s, license->EncryptedPlatformChallengeResponse))
2793 if (!license_write_binary_blob(s, license->EncryptedHardwareId))
2795 if (!license_check_stream_capacity(license->log, s,
sizeof(license->MACData),
2796 "CLIENT_PLATFORM_CHALLENGE_RESPONSE::MACData"))
2798 Stream_Write(s, license->MACData,
sizeof(license->MACData));
2802BOOL license_read_client_platform_challenge_response(rdpLicense* license,
wStream* s)
2804 WINPR_ASSERT(license);
2806 if (!license_read_binary_blob(license->log, s, license->EncryptedPlatformChallengeResponse))
2808 if (!license_read_binary_blob(license->log, s, license->EncryptedHardwareId))
2810 if (!license_check_stream_length(license->log, s,
sizeof(license->MACData),
2811 "CLIENT_PLATFORM_CHALLENGE_RESPONSE::MACData"))
2813 Stream_Read(s, license->MACData,
sizeof(license->MACData));
2814 return license_read_platform_challenge_response(license);
2826BOOL license_send_valid_client_error_packet(rdpRdp* rdp)
2829 rdpLicense* license = rdp->license;
2830 WINPR_ASSERT(license);
2832 license->state = LICENSE_STATE_COMPLETED;
2833 license->type = LICENSE_TYPE_NONE;
2834 return license_send_error_alert(license, STATUS_VALID_CLIENT, ST_NO_TRANSITION,
2835 license->ErrorInfo);
2844rdpLicense* license_new(rdpRdp* rdp)
2848 rdpLicense* license = (rdpLicense*)calloc(1,
sizeof(rdpLicense));
2851 license->log = WLog_Get(LICENSE_TAG);
2852 WINPR_ASSERT(license->log);
2854 license->PlatformId = PLATFORMID;
2855 license->ClientType = OTHER_PLATFORM_CHALLENGE_TYPE;
2856 license->LicenseDetailLevel = LICENSE_DETAIL_DETAIL;
2857 license->PreferredKeyExchangeAlg = KEY_EXCHANGE_ALG_RSA;
2860 license_set_state(license, LICENSE_STATE_INITIAL);
2861 if (!(license->certificate = freerdp_certificate_new()))
2863 if (!(license->ProductInfo = license_new_product_info()))
2865 if (!(license->ErrorInfo = license_new_binary_blob(BB_ERROR_BLOB)))
2867 if (!(license->LicenseInfo = license_new_binary_blob(BB_DATA_BLOB)))
2869 if (!(license->KeyExchangeList = license_new_binary_blob(BB_KEY_EXCHG_ALG_BLOB)))
2871 if (!(license->ServerCertificate = license_new_binary_blob(BB_CERTIFICATE_BLOB)))
2873 if (!(license->ClientUserName = license_new_binary_blob(BB_CLIENT_USER_NAME_BLOB)))
2875 if (!(license->ClientMachineName = license_new_binary_blob(BB_CLIENT_MACHINE_NAME_BLOB)))
2877 if (!(license->PlatformChallenge = license_new_binary_blob(BB_ANY_BLOB)))
2879 if (!(license->PlatformChallengeResponse = license_new_binary_blob(BB_ANY_BLOB)))
2881 if (!(license->EncryptedPlatformChallenge = license_new_binary_blob(BB_ANY_BLOB)))
2883 if (!(license->EncryptedPlatformChallengeResponse =
2884 license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2886 if (!(license->EncryptedPremasterSecret = license_new_binary_blob(BB_ANY_BLOB)))
2888 if (!(license->EncryptedHardwareId = license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2890 if (!(license->EncryptedLicenseInfo = license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2892 if (!(license->ScopeList = license_new_scope_list()))
2895 if (!license_generate_randoms(license))
2901 WINPR_PRAGMA_DIAG_PUSH
2902 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2903 license_free(license);
2904 WINPR_PRAGMA_DIAG_POP
2913void license_free(rdpLicense* license)
2917 freerdp_certificate_free(license->certificate);
2918 license_free_product_info(license->ProductInfo);
2919 license_free_binary_blob(license->ErrorInfo);
2920 license_free_binary_blob(license->LicenseInfo);
2921 license_free_binary_blob(license->KeyExchangeList);
2922 license_free_binary_blob(license->ServerCertificate);
2923 license_free_binary_blob(license->ClientUserName);
2924 license_free_binary_blob(license->ClientMachineName);
2925 license_free_binary_blob(license->PlatformChallenge);
2926 license_free_binary_blob(license->PlatformChallengeResponse);
2927 license_free_binary_blob(license->EncryptedPlatformChallenge);
2928 license_free_binary_blob(license->EncryptedPlatformChallengeResponse);
2929 license_free_binary_blob(license->EncryptedPremasterSecret);
2930 license_free_binary_blob(license->EncryptedHardwareId);
2931 license_free_binary_blob(license->EncryptedLicenseInfo);
2932 license_free_scope_list(license->ScopeList);
2937LICENSE_STATE license_get_state(
const rdpLicense* license)
2939 WINPR_ASSERT(license);
2940 return license->state;
2943LICENSE_TYPE license_get_type(
const rdpLicense* license)
2945 WINPR_ASSERT(license);
2946 return license->type;
2949void license_set_state(rdpLicense* license, LICENSE_STATE state)
2951 WINPR_ASSERT(license);
2952 license->state = state;
2955 case LICENSE_STATE_COMPLETED:
2957 case LICENSE_STATE_ABORTED:
2959 license->type = LICENSE_TYPE_INVALID;
2964const char* license_get_state_string(LICENSE_STATE state)
2968 case LICENSE_STATE_INITIAL:
2969 return "LICENSE_STATE_INITIAL";
2970 case LICENSE_STATE_CONFIGURED:
2971 return "LICENSE_STATE_CONFIGURED";
2972 case LICENSE_STATE_REQUEST:
2973 return "LICENSE_STATE_REQUEST";
2974 case LICENSE_STATE_NEW_REQUEST:
2975 return "LICENSE_STATE_NEW_REQUEST";
2976 case LICENSE_STATE_PLATFORM_CHALLENGE:
2977 return "LICENSE_STATE_PLATFORM_CHALLENGE";
2978 case LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE:
2979 return "LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE";
2980 case LICENSE_STATE_COMPLETED:
2981 return "LICENSE_STATE_COMPLETED";
2982 case LICENSE_STATE_ABORTED:
2983 return "LICENSE_STATE_ABORTED";
2985 return "LICENSE_STATE_UNKNOWN";
2989BOOL license_server_send_request(rdpLicense* license)
2991 if (!license_ensure_state(license, LICENSE_STATE_CONFIGURED, LICENSE_REQUEST))
2993 if (!license_send_license_request_packet(license))
2995 license_set_state(license, LICENSE_STATE_REQUEST);
3000static BOOL license_set_string(wLog* log,
const char* what,
const char* value, BYTE** bdst,
3004 WINPR_ASSERT(value);
3006 WINPR_ASSERT(dstLen);
3016 *cnv.w = ConvertUtf8ToWCharAlloc(value, &len);
3017 if (!*cnv.w || (len > UINT32_MAX /
sizeof(WCHAR)))
3019 WLog_Print(log, WLOG_ERROR,
"license->ProductInfo: %s == %p || %" PRIuz
" > UINT32_MAX",
3020 what, (
void*)(*cnv.w), len);
3023 *dstLen = (UINT32)(len *
sizeof(WCHAR));
3027BOOL license_server_configure(rdpLicense* license)
3030 UINT32 algs[] = { KEY_EXCHANGE_ALG_RSA };
3032 WINPR_ASSERT(license);
3033 WINPR_ASSERT(license->rdp);
3035 const rdpSettings* settings = license->rdp->settings;
3037 const char* CompanyName =
3040 const char* ProductName =
3042 const UINT32 ProductVersion =
3044 const UINT32 issuerCount =
3048 const char** issuers = WINPR_REINTERPRET_CAST(ptr,
const void*,
const char**);
3050 WINPR_ASSERT(CompanyName);
3051 WINPR_ASSERT(ProductName);
3052 WINPR_ASSERT(ProductVersion > 0);
3053 WINPR_ASSERT(issuers || (issuerCount == 0));
3055 if (!license_ensure_state(license, LICENSE_STATE_INITIAL, LICENSE_REQUEST))
3058 license->ProductInfo->dwVersion = ProductVersion;
3059 if (!license_set_string(license->log,
"pbCompanyName", CompanyName,
3060 &license->ProductInfo->pbCompanyName,
3061 &license->ProductInfo->cbCompanyName))
3064 if (!license_set_string(license->log,
"pbProductId", ProductName,
3065 &license->ProductInfo->pbProductId, &license->ProductInfo->cbProductId))
3068 if (!license_read_binary_blob_data(license->log, license->KeyExchangeList,
3069 BB_KEY_EXCHG_ALG_BLOB, algs,
sizeof(algs)))
3072 if (!freerdp_certificate_read_server_cert(license->certificate, settings->ServerCertificate,
3073 settings->ServerCertificateLength))
3076 s = Stream_New(
nullptr, 1024);
3083 freerdp_certificate_write_server_cert(license->certificate, CERT_CHAIN_VERSION_2, s);
3085 r = license_read_binary_blob_data(license->log, license->ServerCertificate,
3086 BB_CERTIFICATE_BLOB, Stream_Buffer(s),
3087 Stream_GetPosition(s));
3089 Stream_Free(s, TRUE);
3094 if (!license_scope_list_resize(license->ScopeList, issuerCount))
3096 for (
size_t x = 0; x < issuerCount; x++)
3099 const char* name = issuers[x];
3100 const size_t length = strnlen(name, UINT16_MAX) + 1;
3101 if ((length == 0) || (length > UINT16_MAX))
3103 WLog_Print(license->log, WLOG_WARN,
3104 "Invalid issuer at position %" PRIuz
": length 0 < %" PRIuz
" <= %d"
3106 x, length, UINT16_MAX, name);
3109 if (!license_read_binary_blob_data(license->log, blob, BB_SCOPE_BLOB, name, length))
3113 license_set_state(license, LICENSE_STATE_CONFIGURED);
3117rdpLicense* license_get(rdpContext* context)
3119 WINPR_ASSERT(context);
3120 WINPR_ASSERT(context->rdp);
3121 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.