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
59#define LICENSE_MAX_CAL_LENGTH (1ull * UINT16_MAX)
60#define LICENSE_MAX_SCOPE_LENGTH (1ull * UINT16_MAX)
61#define LICENSE_MAX_PRODUCT_ID_LENGTH (1ull * UINT16_MAX)
62#define LICENSE_MAX_COMPANY_NAME_LENGTH (1ull * UINT16_MAX)
65enum LicenseRequestType
67 LICENSE_REQUEST = 0x01,
68 PLATFORM_CHALLENGE = 0x02,
70 UPGRADE_LICENSE = 0x04,
72 NEW_LICENSE_REQUEST = 0x13,
73 PLATFORM_CHALLENGE_RESPONSE = 0x15,
84#define LICENSE_PREAMBLE_LENGTH 4
88#define SERVER_RANDOM_LENGTH 32
89#define MASTER_SECRET_LENGTH 48
90#define PREMASTER_SECRET_LENGTH 48
91#define SESSION_KEY_BLOB_LENGTH 48
92#define MAC_SALT_KEY_LENGTH 16
93#define LICENSING_ENCRYPTION_KEY_LENGTH 16
94#define HWID_PLATFORM_ID_LENGTH 4
102#define PREAMBLE_VERSION_3_0 0x03
104#define EXTENDED_ERROR_MSG_SUPPORTED 0x80
109 BB_ANY_BLOB = 0x0000,
110 BB_DATA_BLOB = 0x0001,
111 BB_RANDOM_BLOB = 0x0002,
112 BB_CERTIFICATE_BLOB = 0x0003,
113 BB_ERROR_BLOB = 0x0004,
114 BB_ENCRYPTED_DATA_BLOB = 0x0009,
115 BB_KEY_EXCHG_ALG_BLOB = 0x000D,
116 BB_SCOPE_BLOB = 0x000E,
117 BB_CLIENT_USER_NAME_BLOB = 0x000F,
118 BB_CLIENT_MACHINE_NAME_BLOB = 0x0010
123#define KEY_EXCHANGE_ALG_RSA 0x00000001
129 ERR_INVALID_SERVER_CERTIFICATE = 0x00000001,
130 ERR_NO_LICENSE = 0x00000002,
131 ERR_INVALID_MAC = 0x00000003,
132 ERR_INVALID_SCOPE = 0x00000004,
133 ERR_NO_LICENSE_SERVER = 0x00000006,
134 STATUS_VALID_CLIENT = 0x00000007,
135 ERR_INVALID_CLIENT = 0x00000008,
136 ERR_INVALID_PRODUCT_ID = 0x0000000B,
137 ERR_INVALID_MESSAGE_LENGTH = 0x0000000C
144 ST_TOTAL_ABORT = 0x00000001,
145 ST_NO_TRANSITION = 0x00000002,
146 ST_RESET_PHASE_TO_START = 0x00000003,
147 ST_RESEND_LAST_MESSAGE = 0x00000004
154 WIN32_PLATFORM_CHALLENGE_TYPE = 0x0100,
155 WIN16_PLATFORM_CHALLENGE_TYPE = 0x0200,
156 WINCE_PLATFORM_CHALLENGE_TYPE = 0x0300,
157 OTHER_PLATFORM_CHALLENGE_TYPE = 0xFF00
164 LICENSE_DETAIL_SIMPLE = 0x0001,
165 LICENSE_DETAIL_MODERATE = 0x0002,
166 LICENSE_DETAIL_DETAIL = 0x0003
184 CLIENT_OS_ID_WINNT_351 = 0x01000000,
185 CLIENT_OS_ID_WINNT_40 = 0x02000000,
186 CLIENT_OS_ID_WINNT_50 = 0x03000000,
187 CLIENT_OS_ID_WINNT_POST_52 = 0x04000000,
189 CLIENT_IMAGE_ID_MICROSOFT = 0x00010000,
190 CLIENT_IMAGE_ID_CITRIX = 0x00020000,
198 rdpCertificate* certificate;
199 BYTE HardwareId[HWID_LENGTH];
200 BYTE ClientRandom[CLIENT_RANDOM_LENGTH];
201 BYTE ServerRandom[SERVER_RANDOM_LENGTH];
202 BYTE MasterSecret[MASTER_SECRET_LENGTH];
203 BYTE PremasterSecret[PREMASTER_SECRET_LENGTH];
204 BYTE SessionKeyBlob[SESSION_KEY_BLOB_LENGTH];
205 BYTE MacSaltKey[MAC_SALT_KEY_LENGTH];
206 BYTE LicensingEncryptionKey[LICENSING_ENCRYPTION_KEY_LENGTH];
221 BYTE MACData[LICENSING_ENCRYPTION_KEY_LENGTH];
223 UINT32 PacketHeaderLength;
224 UINT32 PreferredKeyExchangeAlg;
227 UINT16 LicenseDetailLevel;
233static BOOL license_send_error_alert(rdpLicense* license, UINT32 dwErrorCode,
236static void license_set_state(rdpLicense* license, LICENSE_STATE state);
239static const char* license_get_state_string(LICENSE_STATE state);
242static const char* license_preferred_key_exchange_alg_string(UINT32 alg,
char* buffer,
size_t size)
244 const char* name =
nullptr;
248 case KEY_EXCHANGE_ALG_RSA:
249 name =
"KEY_EXCHANGE_ALG_RSA";
252 name =
"KEY_EXCHANGE_ALG_UNKNOWN";
256 (void)_snprintf(buffer, size,
"%s [0x%08" PRIx32
"]", name, alg);
261static const char* license_request_type_string(UINT32 type)
265 case LICENSE_REQUEST:
266 return "LICENSE_REQUEST";
267 case PLATFORM_CHALLENGE:
268 return "PLATFORM_CHALLENGE";
270 return "NEW_LICENSE";
271 case UPGRADE_LICENSE:
272 return "UPGRADE_LICENSE";
274 return "LICENSE_INFO";
275 case NEW_LICENSE_REQUEST:
276 return "NEW_LICENSE_REQUEST";
277 case PLATFORM_CHALLENGE_RESPONSE:
278 return "PLATFORM_CHALLENGE_RESPONSE";
280 return "ERROR_ALERT";
282 return "LICENSE_REQUEST_TYPE_UNKNOWN";
287static const char* license_blob_type_string(UINT16 type)
292 return "BB_ANY_BLOB";
294 return "BB_DATA_BLOB";
296 return "BB_RANDOM_BLOB";
297 case BB_CERTIFICATE_BLOB:
298 return "BB_CERTIFICATE_BLOB";
300 return "BB_ERROR_BLOB";
301 case BB_ENCRYPTED_DATA_BLOB:
302 return "BB_ENCRYPTED_DATA_BLOB";
303 case BB_KEY_EXCHG_ALG_BLOB:
304 return "BB_KEY_EXCHG_ALG_BLOB";
306 return "BB_SCOPE_BLOB";
307 case BB_CLIENT_USER_NAME_BLOB:
308 return "BB_CLIENT_USER_NAME_BLOB";
309 case BB_CLIENT_MACHINE_NAME_BLOB:
310 return "BB_CLIENT_MACHINE_NAME_BLOB";
317static wStream* license_send_stream_init(rdpLicense* license, UINT16* sec_flags);
320static BOOL license_generate_randoms(rdpLicense* license);
323static BOOL license_generate_keys(rdpLicense* license);
326static BOOL license_generate_hwid(rdpLicense* license);
329static BOOL license_encrypt_premaster_secret(rdpLicense* license);
333WINPR_ATTR_MALLOC(license_free_product_info, 1)
339static
void license_free_binary_blob(
LICENSE_BLOB* blob);
341WINPR_ATTR_MALLOC(license_free_binary_blob, 1)
342static
LICENSE_BLOB* license_new_binary_blob(UINT16 type);
345static BOOL license_read_binary_blob_data(wLog* log,
LICENSE_BLOB* blob, UINT16 type,
346 const
void* data,
size_t length);
354static
void license_free_scope_list(
SCOPE_LIST* scopeList);
356WINPR_ATTR_MALLOC(license_free_scope_list, 1)
357static
SCOPE_LIST* license_new_scope_list(
void);
360static BOOL license_scope_list_resize(
SCOPE_LIST* scopeList, UINT32 count);
363static BOOL license_read_scope_list(wLog* log,
wStream* s,
SCOPE_LIST* scopeList);
366static BOOL license_write_scope_list(wLog* log,
wStream* s, const
SCOPE_LIST* scopeList);
369static BOOL license_read_license_request_packet(rdpLicense* license,
wStream* s);
372static BOOL license_write_license_request_packet(const rdpLicense* license,
wStream* s);
375static BOOL license_read_platform_challenge_packet(rdpLicense* license,
wStream* s);
378static BOOL license_send_platform_challenge_packet(rdpLicense* license);
381static BOOL license_read_new_or_upgrade_license_packet(rdpLicense* license,
wStream* s);
384static BOOL license_read_error_alert_packet(rdpLicense* license,
wStream* s);
387static BOOL license_write_new_license_request_packet(const rdpLicense* license,
wStream* s);
390static BOOL license_read_new_license_request_packet(rdpLicense* license,
wStream* s);
393static BOOL license_answer_license_request(rdpLicense* license);
396static BOOL license_send_platform_challenge_response(rdpLicense* license);
399static BOOL license_read_platform_challenge_response(rdpLicense* license);
402static BOOL license_read_client_platform_challenge_response(rdpLicense* license,
wStream* s);
405static BOOL license_write_client_platform_challenge_response(rdpLicense* license,
wStream* s);
408static BOOL license_write_server_upgrade_license(const rdpLicense* license,
wStream* s);
411static BOOL license_send_license_info(rdpLicense* license, const
LICENSE_BLOB* calBlob,
412 const BYTE* signature,
size_t signature_length);
415static BOOL license_read_license_info(rdpLicense* license,
wStream* s);
418static state_run_t license_client_recv(rdpLicense* license,
wStream* s);
421static state_run_t license_server_recv(rdpLicense* license,
wStream* s);
423#define PLATFORMID (CLIENT_OS_ID_WINNT_POST_52 | CLIENT_IMAGE_ID_MICROSOFT)
425#ifdef WITH_DEBUG_LICENSE
428static const char* error_codes(UINT32 idx)
435 EVCASE(ERR_INVALID_SERVER_CERTIFICATE);
436 EVCASE(ERR_NO_LICENSE);
437 EVCASE(ERR_INVALID_MAC);
438 EVCASE(ERR_INVALID_SCOPE);
439 EVCASE(ERR_NO_LICENSE_SERVER);
440 EVCASE(STATUS_VALID_CLIENT);
441 EVCASE(ERR_INVALID_CLIENT);
442 EVCASE(ERR_INVALID_PRODUCT_ID);
443 EVCASE(ERR_INVALID_MESSAGE_LENGTH);
445 return "ERR_UNKNOWN";
450static const char* state_transitions(UINT32 idx)
458 EVCASE(ST_TOTAL_ABORT);
459 EVCASE(ST_NO_TRANSITION);
460 EVCASE(ST_RESET_PHASE_TO_START);
461 EVCASE(ST_RESEND_LAST_MESSAGE);
469 char* CompanyName =
nullptr;
470 char* ProductId =
nullptr;
472 WINPR_ASSERT(productInfo);
473 WINPR_ASSERT(productInfo->pbCompanyName);
474 WINPR_ASSERT(productInfo->pbProductId);
477 ConvertWCharNToUtf8Alloc(WINPR_PACKED_ALIGN_CAST(
const WCHAR*, productInfo->pbCompanyName),
478 productInfo->cbCompanyName /
sizeof(WCHAR),
nullptr);
480 ConvertWCharNToUtf8Alloc(WINPR_PACKED_ALIGN_CAST(
const WCHAR*, productInfo->pbProductId),
481 productInfo->cbProductId /
sizeof(WCHAR),
nullptr);
482 WLog_Print(log, WLOG_INFO,
"ProductInfo:");
483 WLog_Print(log, WLOG_INFO,
"\tdwVersion: 0x%08" PRIX32
"", productInfo->dwVersion);
484 WLog_Print(log, WLOG_INFO,
"\tCompanyName: %s", CompanyName);
485 WLog_Print(log, WLOG_INFO,
"\tProductId: %s", ProductId);
490static void license_print_scope_list(wLog* log,
const SCOPE_LIST* scopeList)
492 WINPR_ASSERT(scopeList);
494 WLog_Print(log, WLOG_INFO,
"ScopeList (%" PRIu32
"):", scopeList->count);
496 for (UINT32 index = 0; index < scopeList->count; index++)
498 WINPR_ASSERT(scopeList->array);
502 WLog_Print(log, WLOG_INFO,
"\t%.*s", (
int)scope->length, (
const char*)scope->data);
507static const char licenseStore[] =
"licenses";
510static BOOL license_ensure_state(rdpLicense* license, LICENSE_STATE state, UINT32 msg)
512 const LICENSE_STATE cstate = license_get_state(license);
514 WINPR_ASSERT(license);
518 const char* scstate = license_get_state_string(cstate);
519 const char* sstate = license_get_state_string(state);
520 const char* where = license_request_type_string(msg);
522 WLog_Print(license->log, WLOG_WARN,
523 "Received [%s], but found invalid licensing state %s, expected %s", where,
530state_run_t license_recv(rdpLicense* license,
wStream* s)
532 WINPR_ASSERT(license);
533 WINPR_ASSERT(license->rdp);
534 WINPR_ASSERT(license->rdp->settings);
537 return license_server_recv(license, s);
539 return license_client_recv(license, s);
543static BOOL license_check_stream_length(wLog* log,
wStream* s, UINT64 expect,
const char* where,
546 const size_t remain = Stream_GetRemainingLength(s);
552 WLog_Print(log, WLOG_WARN,
"short %s, expected %" PRIu64
" bytes, got %" PRIuz, where,
556 if ((limit > 0) && (remain > limit))
558 WLog_Print(log, WLOG_WARN,
559 "oversized %s, got %" PRIuz
" bytes, but size is limited to %" PRIu64
".", where,
567static BOOL license_check_stream_capacity(wLog* log,
wStream* s,
size_t expect,
const char* where)
571 return (Stream_CheckAndLogRequiredCapacityWLogEx(log, WLOG_WARN, s, expect, 1,
572 "%s(%s:%" PRIuz
") %s", __func__, __FILE__,
573 (
size_t)__LINE__, where));
577static BOOL computeCalHash(wLog* log,
const char* hostname,
char* hashStr,
size_t len)
579 WINPR_DIGEST_CTX* sha1 =
nullptr;
581 BYTE hash[20] = WINPR_C_ARRAY_INIT;
583 WINPR_ASSERT(hostname);
584 WINPR_ASSERT(hashStr);
586 if (len < 2 *
sizeof(hash) + 1)
589 if (!(sha1 = winpr_Digest_New()))
591 if (!winpr_Digest_Init(sha1, WINPR_MD_SHA1))
593 if (!winpr_Digest_Update(sha1, (
const BYTE*)hostname, strlen(hostname)))
595 if (!winpr_Digest_Final(sha1, hash,
sizeof(hash)))
598 for (
size_t i = 0; i <
sizeof(hash); i++, hashStr += 2)
599 (
void)sprintf_s(hashStr, 3,
"%.2x", hash[i]);
604 WLog_Print(log, WLOG_ERROR,
"failed to generate SHA1 of hostname '%s'", hostname);
605 winpr_Digest_Free(sha1);
610static BOOL saveCal(wLog* log,
const rdpSettings* settings,
const BYTE* data,
size_t length,
611 const char* hostname)
613 char hash[41] = WINPR_C_ARRAY_INIT;
615 char* licenseStorePath =
nullptr;
616 char filename[MAX_PATH] = WINPR_C_ARRAY_INIT;
617 char filenameNew[MAX_PATH] = WINPR_C_ARRAY_INIT;
618 char* filepath =
nullptr;
619 char* filepathNew =
nullptr;
626 WINPR_ASSERT(data || (length == 0));
627 WINPR_ASSERT(hostname);
629 if (!winpr_PathFileExists(path))
631 if (!winpr_PathMakePath(path,
nullptr))
633 WLog_Print(log, WLOG_ERROR,
"error creating directory '%s'", path);
636 WLog_Print(log, WLOG_INFO,
"creating directory %s", path);
639 if (!(licenseStorePath = GetCombinedPath(path, licenseStore)))
641 WLog_Print(log, WLOG_ERROR,
"Failed to get license store path from '%s' + '%s'", path,
646 if (!winpr_PathFileExists(licenseStorePath))
648 if (!winpr_PathMakePath(licenseStorePath,
nullptr))
650 WLog_Print(log, WLOG_ERROR,
"error creating directory '%s'", licenseStorePath);
653 WLog_Print(log, WLOG_INFO,
"creating directory %s", licenseStorePath);
656 if (!computeCalHash(log, hostname, hash,
sizeof(hash)))
658 (void)sprintf_s(filename,
sizeof(filename) - 1,
"%s.cal", hash);
659 (void)sprintf_s(filenameNew,
sizeof(filenameNew) - 1,
"%s.cal.new", hash);
661 if (!(filepath = GetCombinedPath(licenseStorePath, filename)))
663 WLog_Print(log, WLOG_ERROR,
"Failed to get license file path from '%s' + '%s'", path,
668 if (!(filepathNew = GetCombinedPath(licenseStorePath, filenameNew)))
670 WLog_Print(log, WLOG_ERROR,
"Failed to get license new file path from '%s' + '%s'", path,
675 fp = winpr_fopen(filepathNew,
"wb");
678 WLog_Print(log, WLOG_ERROR,
"Failed to open license file '%s'", filepathNew);
682 written = fwrite(data, length, 1, fp);
687 WLog_Print(log, WLOG_ERROR,
"Failed to write to license file '%s'", filepathNew);
688 winpr_DeleteFile(filepathNew);
692 ret = winpr_MoveFileEx(filepathNew, filepath, MOVEFILE_REPLACE_EXISTING);
694 WLog_Print(log, WLOG_ERROR,
"Failed to move license file '%s' to '%s'", filepathNew,
700 free(licenseStorePath);
704WINPR_ATTR_MALLOC(free, 1)
705static BYTE* loadCalFile(wLog* log, const rdpSettings* settings, const
char* hostname,
708 char* licenseStorePath =
nullptr;
709 char calFilename[MAX_PATH] = WINPR_C_ARRAY_INIT;
710 char hash[41] = WINPR_C_ARRAY_INIT;
716 WINPR_ASSERT(settings);
717 WINPR_ASSERT(hostname);
718 WINPR_ASSERT(dataLen);
720 if (!computeCalHash(log, hostname, hash,
sizeof(hash)))
722 WLog_Print(log, WLOG_ERROR,
"%s: unable to compute hostname hash", __func__);
726 (void)sprintf_s(calFilename,
sizeof(calFilename) - 1,
"%s.cal", hash);
728 if (!(licenseStorePath = GetCombinedPath(
732 char* calPath = GetCombinedPath(licenseStorePath, calFilename);
736 fp = winpr_fopen(calPath,
"rb");
740 if (_fseeki64(fp, 0, SEEK_END) != 0)
742 length = _ftelli64(fp);
743 if (_fseeki64(fp, 0, SEEK_SET) != 0)
748 if ((UINT64)length > LICENSE_MAX_CAL_LENGTH)
750 WLog_Print(log, WLOG_ERROR,
751 "%s: the file size %" PRId64
" exceeds the CAL size limit of %llu", __func__,
752 length, LICENSE_MAX_CAL_LENGTH);
756 ret = (BYTE*)malloc((
size_t)length);
760 status = fread(ret, (
size_t)length, 1, fp);
764 *dataLen = (size_t)length;
768 free(licenseStorePath);
776 free(licenseStorePath);
790static BOOL license_read_preamble(wLog* log,
wStream* s, BYTE* bMsgType, BYTE* flags,
793 WINPR_ASSERT(bMsgType);
795 WINPR_ASSERT(wMsgSize);
798 if (!license_check_stream_length(log, s, 4,
"license preamble", 0))
801 Stream_Read_UINT8(s, *bMsgType);
802 Stream_Read_UINT8(s, *flags);
803 Stream_Read_UINT16(s, *wMsgSize);
806 WLog_Print(log, WLOG_WARN,
807 "invalid license preamble::wMsgSize, expected value >= 4, got %" PRIu32,
811 return license_check_stream_length(log, s, *wMsgSize - 4ull,
"license preamble::wMsgSize", 0);
824static BOOL license_write_preamble(
wStream* s, BYTE bMsgType, BYTE flags, UINT16 wMsgSize)
826 if (!Stream_EnsureRemainingCapacity(s, 4))
830 Stream_Write_UINT8(s, bMsgType);
831 Stream_Write_UINT8(s, flags);
832 Stream_Write_UINT16(s, wMsgSize);
844wStream* license_send_stream_init(rdpLicense* license, UINT16* sec_flags)
846 WINPR_ASSERT(license);
847 WINPR_ASSERT(license->rdp);
848 WINPR_ASSERT(sec_flags);
850 const BOOL do_crypt = license->rdp->do_crypt;
852 *sec_flags = SEC_LICENSE_PKT;
864 *sec_flags |= SEC_LICENSE_ENCRYPT_CS;
865 license->rdp->do_crypt = license->rdp->do_crypt_license;
868 wStream* s = rdp_send_stream_init(license->rdp, sec_flags);
872 license->rdp->do_crypt = do_crypt;
873 license->PacketHeaderLength = (UINT16)Stream_GetPosition(s);
874 if (!Stream_SafeZero(s, LICENSE_PREAMBLE_LENGTH))
890static BOOL license_send(rdpLicense* license,
wStream* s, BYTE type, UINT16 sec_flags)
892 WINPR_ASSERT(license);
893 WINPR_ASSERT(license->rdp);
895 rdpRdp* rdp = license->rdp;
896 WINPR_ASSERT(rdp->settings);
898 DEBUG_LICENSE(
"Sending %s Packet", license_request_type_string(type));
899 const size_t length = Stream_GetPosition(s);
900 WINPR_ASSERT(length >= license->PacketHeaderLength);
901 WINPR_ASSERT(length <= UINT16_MAX + license->PacketHeaderLength);
903 const UINT16 wMsgSize = (UINT16)(length - license->PacketHeaderLength);
904 if (!Stream_SetPosition(s, license->PacketHeaderLength))
906 BYTE flags = PREAMBLE_VERSION_3_0;
913 if (!rdp->settings->ServerMode)
914 flags |= EXTENDED_ERROR_MSG_SUPPORTED;
916 if (!license_write_preamble(s, type, flags, wMsgSize))
922#ifdef WITH_DEBUG_LICENSE
923 WLog_Print(license->log, WLOG_DEBUG,
"Sending %s Packet, length %" PRIu16
"",
924 license_request_type_string(type), wMsgSize);
925 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_PointerAs(s,
char) - LICENSE_PREAMBLE_LENGTH,
928 if (!Stream_SetPosition(s, length))
930 const BOOL ret = rdp_send(rdp, s, MCS_GLOBAL_CHANNEL_ID, sec_flags);
934BOOL license_write_server_upgrade_license(
const rdpLicense* license,
wStream* s)
936 WINPR_ASSERT(license);
938 if (!license_write_binary_blob(s, license->EncryptedLicenseInfo))
940 if (!license_check_stream_capacity(license->log, s,
sizeof(license->MACData),
941 "SERVER_UPGRADE_LICENSE::MACData"))
943 Stream_Write(s, license->MACData,
sizeof(license->MACData));
948static BOOL license_server_send_new_or_upgrade_license(rdpLicense* license, BOOL upgrade)
950 UINT16 sec_flags = 0;
951 wStream* s = license_send_stream_init(license, &sec_flags);
952 const BYTE type = upgrade ? UPGRADE_LICENSE : NEW_LICENSE;
957 if (!license_write_server_upgrade_license(license, s))
960 return license_send(license, s, type, sec_flags);
975static state_run_t license_client_recv_int(rdpLicense* license,
wStream* s)
980 const size_t length = Stream_GetRemainingLength(s);
982 WINPR_ASSERT(license);
984 if (!license_read_preamble(license->log, s, &bMsgType, &flags,
986 return STATE_RUN_FAILED;
988 DEBUG_LICENSE(
"Receiving %s Packet", license_request_type_string(bMsgType));
992 case LICENSE_REQUEST:
994 if (license_get_state(license) == LICENSE_STATE_INITIAL)
995 license_set_state(license, LICENSE_STATE_CONFIGURED);
997 if (!license_ensure_state(license, LICENSE_STATE_CONFIGURED, bMsgType))
998 return STATE_RUN_FAILED;
1000 if (!license_read_license_request_packet(license, s))
1001 return STATE_RUN_FAILED;
1003 if (!license_answer_license_request(license))
1004 return STATE_RUN_FAILED;
1006 license_set_state(license, LICENSE_STATE_NEW_REQUEST);
1009 case PLATFORM_CHALLENGE:
1010 if (!license_ensure_state(license, LICENSE_STATE_NEW_REQUEST, bMsgType))
1011 return STATE_RUN_FAILED;
1013 if (!license_read_platform_challenge_packet(license, s))
1014 return STATE_RUN_FAILED;
1016 if (!license_send_platform_challenge_response(license))
1017 return STATE_RUN_FAILED;
1018 license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE);
1022 case UPGRADE_LICENSE:
1023 if (!license_ensure_state(license, LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE, bMsgType))
1024 return STATE_RUN_FAILED;
1025 if (!license_read_new_or_upgrade_license_packet(license, s))
1026 return STATE_RUN_FAILED;
1030 if (!license_read_error_alert_packet(license, s))
1031 return STATE_RUN_FAILED;
1035 WLog_Print(license->log, WLOG_ERROR,
"invalid bMsgType:%" PRIu8
"", bMsgType);
1036 return STATE_RUN_FAILED;
1039 if (!tpkt_ensure_stream_consumed(license->log, s, length))
1040 return STATE_RUN_FAILED;
1041 return STATE_RUN_SUCCESS;
1044state_run_t license_client_recv(rdpLicense* license,
wStream* s)
1046 state_run_t rc = license_client_recv_int(license, s);
1047 if (state_run_failed(rc))
1049 freerdp_set_last_error(license->rdp->context, ERROR_CTX_LICENSE_CLIENT_INVALID);
1054state_run_t license_server_recv(rdpLicense* license,
wStream* s)
1056 state_run_t rc = STATE_RUN_FAILED;
1059 UINT16 wMsgSize = 0;
1060 const size_t length = Stream_GetRemainingLength(s);
1062 WINPR_ASSERT(license);
1064 if (!license_read_preamble(license->log, s, &bMsgType, &flags,
1068 DEBUG_LICENSE(
"Receiving %s Packet", license_request_type_string(bMsgType));
1072 case NEW_LICENSE_REQUEST:
1073 if (!license_ensure_state(license, LICENSE_STATE_REQUEST, bMsgType))
1075 if (!license_read_new_license_request_packet(license, s))
1078 if (!license_send_error_alert(license, ERR_INVALID_MAC, ST_TOTAL_ABORT,
1079 license->ErrorInfo))
1081 if (!license_send_platform_challenge_packet(license))
1083 license->update = FALSE;
1084 license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE);
1087 if (!license_ensure_state(license, LICENSE_STATE_REQUEST, bMsgType))
1089 if (!license_read_license_info(license, s))
1092 if (!license_send_platform_challenge_packet(license))
1094 license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE);
1095 license->update = TRUE;
1098 case PLATFORM_CHALLENGE_RESPONSE:
1099 if (!license_ensure_state(license, LICENSE_STATE_PLATFORM_CHALLENGE, bMsgType))
1101 if (!license_read_client_platform_challenge_response(license, s))
1107 if (license_send_error_alert(license, ERR_INVALID_CLIENT, ST_TOTAL_ABORT,
1108 license->ErrorInfo))
1113 if (!license_server_send_new_or_upgrade_license(license, license->update))
1116 license->type = LICENSE_TYPE_ISSUED;
1117 license_set_state(license, LICENSE_STATE_COMPLETED);
1119 rc = STATE_RUN_CONTINUE;
1124 if (!license_read_error_alert_packet(license, s))
1129 WLog_Print(license->log, WLOG_ERROR,
"invalid bMsgType:%" PRIu8
"", bMsgType);
1133 if (!tpkt_ensure_stream_consumed(license->log, s, length))
1136 if (!state_run_success(rc))
1137 rc = STATE_RUN_SUCCESS;
1140 if (state_run_failed(rc))
1142 if (flags & EXTENDED_ERROR_MSG_SUPPORTED)
1144 if (!license_send_error_alert(license, ERR_INVALID_CLIENT, ST_TOTAL_ABORT,
nullptr))
1146 WLog_Print(license->log, WLOG_ERROR,
"license_send_error_alert failed");
1149 license_set_state(license, LICENSE_STATE_ABORTED);
1155BOOL license_generate_randoms(rdpLicense* license)
1157 WINPR_ASSERT(license);
1159#ifdef LICENSE_NULL_CLIENT_RANDOM
1160 ZeroMemory(license->ClientRandom,
sizeof(license->ClientRandom));
1162 if (winpr_RAND(license->ClientRandom,
sizeof(license->ClientRandom)) < 0)
1166 if (winpr_RAND(license->ServerRandom,
sizeof(license->ServerRandom)) < 0)
1169#ifdef LICENSE_NULL_PREMASTER_SECRET
1170 ZeroMemory(license->PremasterSecret,
sizeof(license->PremasterSecret));
1172 if (winpr_RAND(license->PremasterSecret,
sizeof(license->PremasterSecret)) <
1184static BOOL license_generate_keys(rdpLicense* license)
1186 WINPR_ASSERT(license);
1190 !security_master_secret(license->PremasterSecret,
sizeof(license->PremasterSecret),
1191 license->ClientRandom,
sizeof(license->ClientRandom),
1192 license->ServerRandom,
sizeof(license->ServerRandom),
1193 license->MasterSecret,
sizeof(license->MasterSecret)) ||
1195 !security_session_key_blob(license->MasterSecret,
sizeof(license->MasterSecret),
1196 license->ClientRandom,
sizeof(license->ClientRandom),
1197 license->ServerRandom,
sizeof(license->ServerRandom),
1198 license->SessionKeyBlob,
sizeof(license->SessionKeyBlob)))
1202 security_mac_salt_key(license->SessionKeyBlob,
sizeof(license->SessionKeyBlob),
1203 license->ClientRandom,
sizeof(license->ClientRandom),
1204 license->ServerRandom,
sizeof(license->ServerRandom), license->MacSaltKey,
1205 sizeof(license->MacSaltKey));
1206 const BOOL ret = security_licensing_encryption_key(
1207 license->SessionKeyBlob,
sizeof(license->SessionKeyBlob), license->ClientRandom,
1208 sizeof(license->ClientRandom), license->ServerRandom,
sizeof(license->ServerRandom),
1209 license->LicensingEncryptionKey,
1210 sizeof(license->LicensingEncryptionKey));
1212 WLog_Print(license->log, WLOG_TRACE,
"license keys %s generated", ret ?
"successfully" :
"NOT");
1214#ifdef WITH_DEBUG_LICENSE
1215 WLog_Print(license->log, WLOG_DEBUG,
"ClientRandom:");
1216 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ClientRandom,
1217 sizeof(license->ClientRandom));
1218 WLog_Print(license->log, WLOG_DEBUG,
"ServerRandom:");
1219 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ServerRandom,
1220 sizeof(license->ServerRandom));
1221 WLog_Print(license->log, WLOG_DEBUG,
"PremasterSecret:");
1222 winpr_HexLogDump(license->log, WLOG_DEBUG, license->PremasterSecret,
1223 sizeof(license->PremasterSecret));
1224 WLog_Print(license->log, WLOG_DEBUG,
"MasterSecret:");
1225 winpr_HexLogDump(license->log, WLOG_DEBUG, license->MasterSecret,
1226 sizeof(license->MasterSecret));
1227 WLog_Print(license->log, WLOG_DEBUG,
"SessionKeyBlob:");
1228 winpr_HexLogDump(license->log, WLOG_DEBUG, license->SessionKeyBlob,
1229 sizeof(license->SessionKeyBlob));
1230 WLog_Print(license->log, WLOG_DEBUG,
"MacSaltKey:");
1231 winpr_HexLogDump(license->log, WLOG_DEBUG, license->MacSaltKey,
sizeof(license->MacSaltKey));
1232 WLog_Print(license->log, WLOG_DEBUG,
"LicensingEncryptionKey:");
1233 winpr_HexLogDump(license->log, WLOG_DEBUG, license->LicensingEncryptionKey,
1234 sizeof(license->LicensingEncryptionKey));
1244BOOL license_generate_hwid(rdpLicense* license)
1246 const BYTE* hashTarget =
nullptr;
1247 size_t targetLen = 0;
1248 BYTE macAddress[6] = WINPR_C_ARRAY_INIT;
1250 WINPR_ASSERT(license);
1251 WINPR_ASSERT(license->rdp);
1252 WINPR_ASSERT(license->rdp->settings);
1254 ZeroMemory(license->HardwareId,
sizeof(license->HardwareId));
1256 if (license->rdp->settings->OldLicenseBehaviour)
1258 hashTarget = macAddress;
1259 targetLen =
sizeof(macAddress);
1263 wStream buffer = WINPR_C_ARRAY_INIT;
1264 const char* hostname = license->rdp->settings->ClientHostname;
1265 wStream* s = Stream_StaticInit(&buffer, license->HardwareId, 4);
1266 Stream_Write_UINT32(s, license->PlatformId);
1268 hashTarget = (
const BYTE*)hostname;
1269 targetLen = hostname ? strlen(hostname) : 0;
1278 return winpr_Digest_Allow_FIPS(WINPR_MD_MD5, hashTarget, targetLen,
1279 &license->HardwareId[HWID_PLATFORM_ID_LENGTH],
1280 WINPR_MD5_DIGEST_LENGTH);
1284static BOOL license_get_server_rsa_public_key(rdpLicense* license)
1286 rdpSettings* settings =
nullptr;
1288 WINPR_ASSERT(license);
1289 WINPR_ASSERT(license->certificate);
1290 WINPR_ASSERT(license->rdp);
1292 settings = license->rdp->settings;
1293 WINPR_ASSERT(settings);
1295 if (license->ServerCertificate->length < 1)
1297 if (!freerdp_certificate_read_server_cert(license->certificate, settings->ServerCertificate,
1298 settings->ServerCertificateLength))
1305BOOL license_encrypt_premaster_secret(rdpLicense* license)
1307 WINPR_ASSERT(license);
1308 WINPR_ASSERT(license->certificate);
1310 if (!license_get_server_rsa_public_key(license))
1313 WINPR_ASSERT(license->EncryptedPremasterSecret);
1315 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1318 WLog_Print(license->log, WLOG_ERROR,
"info=%p, license->certificate=%p", (
const void*)info,
1319 (
void*)license->certificate);
1323#ifdef WITH_DEBUG_LICENSE
1324 WLog_Print(license->log, WLOG_DEBUG,
"Modulus (%" PRIu32
" bits):", info->ModulusLength * 8);
1325 winpr_HexLogDump(license->log, WLOG_DEBUG, info->Modulus, info->ModulusLength);
1326 WLog_Print(license->log, WLOG_DEBUG,
"Exponent:");
1327 winpr_HexLogDump(license->log, WLOG_DEBUG, info->exponent,
sizeof(info->exponent));
1330 BYTE* EncryptedPremasterSecret = (BYTE*)calloc(1, info->ModulusLength);
1331 if (!EncryptedPremasterSecret)
1333 WLog_Print(license->log, WLOG_ERROR,
1334 "EncryptedPremasterSecret=%p, info->ModulusLength=%" PRIu32,
1335 (
const void*)EncryptedPremasterSecret, info->ModulusLength);
1339 license->EncryptedPremasterSecret->type = BB_RANDOM_BLOB;
1340 license->EncryptedPremasterSecret->length =
sizeof(license->PremasterSecret);
1341#ifndef LICENSE_NULL_PREMASTER_SECRET
1343 const SSIZE_T length =
1344 crypto_rsa_public_encrypt(license->PremasterSecret,
sizeof(license->PremasterSecret),
1345 info, EncryptedPremasterSecret, info->ModulusLength);
1346 if ((length < 0) || (length > UINT16_MAX))
1348 WLog_Print(license->log, WLOG_ERROR,
"RSA public encrypt length=%" PRIdz
" < 0 || > %d",
1349 length, UINT16_MAX);
1352 license->EncryptedPremasterSecret->length = (UINT16)length;
1355 license->EncryptedPremasterSecret->data = EncryptedPremasterSecret;
1360static BOOL license_rc4_with_licenseKey(
const rdpLicense* license,
const BYTE* input,
size_t len,
1363 WINPR_ASSERT(license);
1364 WINPR_ASSERT(input || (len == 0));
1365 WINPR_ASSERT(target);
1366 WINPR_ASSERT(len <= UINT16_MAX);
1368 WINPR_RC4_CTX* rc4 = winpr_RC4_New_Allow_FIPS(license->LicensingEncryptionKey,
1369 sizeof(license->LicensingEncryptionKey));
1372 WLog_Print(license->log, WLOG_ERROR,
"Failed to allocate RC4");
1376 BYTE* buffer =
nullptr;
1378 buffer = realloc(target->data, len);
1382 target->data = buffer;
1383 target->length = (UINT16)len;
1385 if (!winpr_RC4_Update(rc4, len, input, buffer))
1388 winpr_RC4_Free(rc4);
1392 WLog_Print(license->log, WLOG_ERROR,
"Failed to create/update RC4: len=%" PRIuz
", buffer=%p",
1393 len, (
const void*)buffer);
1394 winpr_RC4_Free(rc4);
1410static BOOL license_encrypt_and_MAC(rdpLicense* license,
const BYTE* input,
size_t len,
1413 WINPR_ASSERT(license);
1414 return license_rc4_with_licenseKey(license, input, len, target) &&
1415 security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), input, len, mac,
1431static BOOL license_decrypt_and_check_MAC(rdpLicense* license,
const BYTE* input,
size_t len,
1434 BYTE macData[
sizeof(license->MACData)] = WINPR_C_ARRAY_INIT;
1436 WINPR_ASSERT(license);
1437 WINPR_ASSERT(target);
1441 WLog_Print(license->log, WLOG_DEBUG,
"TransportDumpReplay active, skipping...");
1445 if (!license_rc4_with_licenseKey(license, input, len, target))
1448 if (!security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), target->data, len,
1449 macData,
sizeof(macData)))
1452 if (memcmp(packetMac, macData,
sizeof(macData)) != 0)
1454 WLog_Print(license->log, WLOG_ERROR,
"packetMac != expectedMac");
1469 WINPR_ASSERT(productInfo);
1471 if (!license_check_stream_length(log, s, 8,
"license product info::cbCompanyName", 0))
1474 Stream_Read_UINT32(s, productInfo->dwVersion);
1475 Stream_Read_UINT32(s, productInfo->cbCompanyName);
1478 if ((productInfo->cbCompanyName < 2) || (productInfo->cbCompanyName % 2 != 0))
1480 WLog_Print(log, WLOG_WARN,
"license product info invalid cbCompanyName %" PRIu32,
1481 productInfo->cbCompanyName);
1485 if (!license_check_stream_length(log, s, productInfo->cbCompanyName,
1486 "license product info::CompanyName",
1487 LICENSE_MAX_COMPANY_NAME_LENGTH))
1490 productInfo->pbProductId =
nullptr;
1491 productInfo->pbCompanyName = (BYTE*)malloc(productInfo->cbCompanyName);
1492 if (!productInfo->pbCompanyName)
1494 Stream_Read(s, productInfo->pbCompanyName, productInfo->cbCompanyName);
1496 if (!license_check_stream_length(log, s, 4,
"license product info::cbProductId", 0))
1499 Stream_Read_UINT32(s, productInfo->cbProductId);
1501 if ((productInfo->cbProductId < 2) || (productInfo->cbProductId % 2 != 0))
1503 WLog_Print(log, WLOG_WARN,
"license product info invalid cbProductId %" PRIu32,
1504 productInfo->cbProductId);
1508 if (!license_check_stream_length(log, s, productInfo->cbProductId,
1509 "license product info::ProductId",
1510 LICENSE_MAX_PRODUCT_ID_LENGTH))
1513 productInfo->pbProductId = (BYTE*)malloc(productInfo->cbProductId);
1514 if (!productInfo->pbProductId)
1516 Stream_Read(s, productInfo->pbProductId, productInfo->cbProductId);
1520 free(productInfo->pbCompanyName);
1521 free(productInfo->pbProductId);
1522 productInfo->pbCompanyName =
nullptr;
1523 productInfo->pbProductId =
nullptr;
1528static BOOL license_write_product_info(wLog* log,
wStream* s,
1531 WINPR_ASSERT(productInfo);
1533 if (!license_check_stream_capacity(log, s, 8,
"license product info::cbCompanyName"))
1536 Stream_Write_UINT32(s, productInfo->dwVersion);
1537 Stream_Write_UINT32(s, productInfo->cbCompanyName);
1540 if ((productInfo->cbCompanyName < 2) || (productInfo->cbCompanyName % 2 != 0) ||
1541 !productInfo->pbCompanyName)
1543 WLog_Print(log, WLOG_WARN,
"license product info invalid cbCompanyName %" PRIu32,
1544 productInfo->cbCompanyName);
1548 if (!license_check_stream_capacity(log, s, productInfo->cbCompanyName,
1549 "license product info::CompanyName"))
1552 Stream_Write(s, productInfo->pbCompanyName, productInfo->cbCompanyName);
1554 if (!license_check_stream_capacity(log, s, 4,
"license product info::cbProductId"))
1557 Stream_Write_UINT32(s, productInfo->cbProductId);
1559 if ((productInfo->cbProductId < 2) || (productInfo->cbProductId % 2 != 0) ||
1560 !productInfo->pbProductId)
1562 WLog_Print(log, WLOG_WARN,
"license product info invalid cbProductId %" PRIu32,
1563 productInfo->cbProductId);
1567 if (!license_check_stream_capacity(log, s, productInfo->cbProductId,
1568 "license product info::ProductId"))
1571 Stream_Write(s, productInfo->pbProductId, productInfo->cbProductId);
1600 free(productInfo->pbCompanyName);
1601 free(productInfo->pbProductId);
1606BOOL license_read_binary_blob_data(wLog* log,
LICENSE_BLOB* blob, UINT16 wBlobType,
1607 const void* data,
size_t length)
1610 WINPR_ASSERT(length <= UINT16_MAX);
1611 WINPR_ASSERT(data || (length == 0));
1613 blob->length = (UINT16)length;
1615 blob->data =
nullptr;
1617 if ((blob->type != wBlobType) && (blob->type != BB_ANY_BLOB))
1619 WLog_Print(log, WLOG_ERROR,
"license binary blob::type expected %s, got %s",
1620 license_blob_type_string(wBlobType), license_blob_type_string(blob->type));
1627 if ((blob->type != BB_ANY_BLOB) && (blob->length == 0))
1629 WLog_Print(log, WLOG_DEBUG,
"license binary blob::type %s, length=0, skipping.",
1630 license_blob_type_string(blob->type));
1634 blob->type = wBlobType;
1635 blob->data =
nullptr;
1636 if (blob->length > 0)
1637 blob->data = malloc(blob->length);
1640 WLog_Print(log, WLOG_ERROR,
"license binary blob::length=%" PRIu16
", blob::data=%p",
1641 blob->length, (
const void*)blob->data);
1644 memcpy(blob->data, data, blob->length);
1657 UINT16 wBlobType = 0;
1662 if (!license_check_stream_length(log, s, 4,
"license binary blob::type", 0))
1665 Stream_Read_UINT16(s, wBlobType);
1666 Stream_Read_UINT16(s, length);
1668 if (!license_check_stream_length(log, s, length,
"license binary blob::length", 0))
1671 if (!license_read_binary_blob_data(log, blob, wBlobType, Stream_Pointer(s), length))
1674 return Stream_SafeSeek(s, length);
1688 if (!Stream_EnsureRemainingCapacity(s, blob->length + 4ull))
1691 Stream_Write_UINT16(s, blob->type);
1692 Stream_Write_UINT16(s, blob->length);
1694 if (blob->length > 0)
1695 Stream_Write(s, blob->data, blob->length);
1700static BOOL license_write_encrypted_premaster_secret_blob(wLog* log,
wStream* s,
1702 UINT32 ModulusLength)
1704 const UINT32 length = ModulusLength + 8;
1707 WINPR_ASSERT(length <= UINT16_MAX);
1709 if (blob->length > ModulusLength)
1711 WLog_Print(log, WLOG_ERROR,
"invalid blob");
1715 if (!Stream_EnsureRemainingCapacity(s, length + 4ull))
1717 Stream_Write_UINT16(s, blob->type);
1718 Stream_Write_UINT16(s, (UINT16)length);
1720 if (blob->length > 0)
1721 Stream_Write(s, blob->data, blob->length);
1723 Stream_Zero(s, length - blob->length);
1728static BOOL license_read_encrypted_premaster_secret_blob(wLog* log,
wStream* s,
LICENSE_BLOB* blob,
1729 UINT32* ModulusLength)
1731 if (!license_read_binary_blob(log, s, blob))
1733 WINPR_ASSERT(ModulusLength);
1734 *ModulusLength = blob->length;
1776 UINT32 scopeCount = 0;
1778 WINPR_ASSERT(scopeList);
1780 if (!license_check_stream_length(log, s, 4,
"license scope list", 0))
1783 Stream_Read_UINT32(s, scopeCount);
1785 if (!license_check_stream_length(log, s, 4ull * scopeCount,
"license scope list::count",
1786 LICENSE_MAX_SCOPE_LENGTH))
1789 if (!license_scope_list_resize(scopeList, scopeCount))
1792 for (UINT32 i = 0; i < scopeCount; i++)
1794 if (!license_read_binary_blob(log, s, scopeList->array[i]))
1801BOOL license_write_scope_list(wLog* log,
wStream* s,
const SCOPE_LIST* scopeList)
1803 WINPR_ASSERT(scopeList);
1805 if (!license_check_stream_capacity(log, s, 4,
"license scope list"))
1808 Stream_Write_UINT32(s, scopeList->count);
1810 if (!license_check_stream_capacity(log, s, scopeList->count * 4ull,
1811 "license scope list::count"))
1815 WINPR_ASSERT(scopeList->array || (scopeList->count == 0));
1816 for (UINT32 i = 0; i < scopeList->count; i++)
1820 if (!license_write_binary_blob(s, element))
1839static void license_scope_list_free(
SCOPE_LIST* scopeList, UINT32 count)
1841 WINPR_ASSERT(scopeList);
1842 WINPR_ASSERT(scopeList->array || (scopeList->count == 0));
1844 for (UINT32 x = count; x < scopeList->count; x++)
1846 license_free_binary_blob(scopeList->array[x]);
1847 scopeList->array[x] =
nullptr;
1852 free((
void*)scopeList->array);
1853 scopeList->array =
nullptr;
1857BOOL license_scope_list_resize(
SCOPE_LIST* scopeList, UINT32 count)
1859 license_scope_list_free(scopeList, count);
1866 scopeList->array = tmp;
1869 for (UINT32 x = scopeList->count; x < count; x++)
1871 LICENSE_BLOB* blob = license_new_binary_blob(BB_SCOPE_BLOB);
1874 scopeList->count = x;
1877 scopeList->array[x] = blob;
1880 scopeList->count = count;
1890void license_free_scope_list(
SCOPE_LIST* scopeList)
1895 license_scope_list_free(scopeList, 0);
1899BOOL license_send_license_info(rdpLicense* license,
const LICENSE_BLOB* calBlob,
1900 const BYTE* signature,
size_t signature_length)
1902 WINPR_ASSERT(calBlob);
1903 WINPR_ASSERT(signature);
1904 WINPR_ASSERT(license->certificate);
1906 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1910 UINT16 sec_flags = 0;
1911 wStream* s = license_send_stream_init(license, &sec_flags);
1915 if (!license_check_stream_capacity(license->log, s, 8 +
sizeof(license->ClientRandom),
1916 "license info::ClientRandom"))
1919 Stream_Write_UINT32(s,
1920 license->PreferredKeyExchangeAlg);
1921 Stream_Write_UINT32(s, license->PlatformId);
1924 Stream_Write(s, license->ClientRandom,
sizeof(license->ClientRandom));
1927 if (!license_write_encrypted_premaster_secret_blob(
1928 license->log, s, license->EncryptedPremasterSecret, info->ModulusLength))
1932 if (!license_write_binary_blob(s, calBlob))
1936 if (!license_write_binary_blob(s, license->EncryptedHardwareId))
1940 if (!license_check_stream_capacity(license->log, s, signature_length,
"license info::MACData"))
1942 Stream_Write(s, signature, signature_length);
1944 return license_send(license, s, LICENSE_INFO, sec_flags);
1952static BOOL license_check_preferred_alg(rdpLicense* license, UINT32 PreferredKeyExchangeAlg,
1955 WINPR_ASSERT(license);
1956 WINPR_ASSERT(where);
1958 if (license->PreferredKeyExchangeAlg != PreferredKeyExchangeAlg)
1960 char buffer1[64] = WINPR_C_ARRAY_INIT;
1961 char buffer2[64] = WINPR_C_ARRAY_INIT;
1962 WLog_Print(license->log, WLOG_WARN,
"%s::PreferredKeyExchangeAlg, expected %s, got %s",
1964 license_preferred_key_exchange_alg_string(license->PreferredKeyExchangeAlg,
1965 buffer1,
sizeof(buffer1)),
1966 license_preferred_key_exchange_alg_string(PreferredKeyExchangeAlg, buffer2,
1973BOOL license_read_license_info(rdpLicense* license,
wStream* s)
1976 UINT32 PreferredKeyExchangeAlg = 0;
1978 WINPR_ASSERT(license);
1979 WINPR_ASSERT(license->certificate);
1981 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1986 if (!license_check_stream_length(license->log, s, 8 +
sizeof(license->ClientRandom),
1990 Stream_Read_UINT32(s, PreferredKeyExchangeAlg);
1991 if (!license_check_preferred_alg(license, PreferredKeyExchangeAlg,
"license info"))
1993 Stream_Read_UINT32(s, license->PlatformId);
1996 Stream_Read(s, license->ClientRandom,
sizeof(license->ClientRandom));
2000 UINT32 ModulusLength = 0;
2001 if (!license_read_encrypted_premaster_secret_blob(
2002 license->log, s, license->EncryptedPremasterSecret, &ModulusLength))
2005 if (ModulusLength != info->ModulusLength)
2007 WLog_Print(license->log, WLOG_WARN,
2008 "EncryptedPremasterSecret,::ModulusLength[%" PRIu32
2009 "] != rdpCertInfo::ModulusLength[%" PRIu32
"]",
2010 ModulusLength, info->ModulusLength);
2016 if (!license_read_binary_blob(license->log, s, license->LicenseInfo))
2020 if (!license_read_binary_blob(license->log, s, license->EncryptedHardwareId))
2024 if (!license_check_stream_length(license->log, s,
sizeof(license->MACData),
2025 "license info::MACData", 0))
2027 Stream_Read(s, license->MACData,
sizeof(license->MACData));
2042BOOL license_read_license_request_packet(rdpLicense* license,
wStream* s)
2044 WINPR_ASSERT(license);
2047 if (!license_check_stream_length(license->log, s,
sizeof(license->ServerRandom),
2048 "license request", 0))
2051 Stream_Read(s, license->ServerRandom,
sizeof(license->ServerRandom));
2054 if (!license_read_product_info(license->log, s, license->ProductInfo))
2058 if (!license_read_binary_blob(license->log, s, license->KeyExchangeList))
2062 if (!license_read_binary_blob(license->log, s, license->ServerCertificate))
2066 if (!license_read_scope_list(license->log, s, license->ScopeList))
2070 if (!freerdp_certificate_read_server_cert(license->certificate,
2071 license->ServerCertificate->data,
2072 license->ServerCertificate->length))
2075 if (!license_generate_keys(license) || !license_generate_hwid(license) ||
2076 !license_encrypt_premaster_secret(license))
2079#ifdef WITH_DEBUG_LICENSE
2080 WLog_Print(license->log, WLOG_DEBUG,
"ServerRandom:");
2081 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ServerRandom,
2082 sizeof(license->ServerRandom));
2083 license_print_product_info(license->log, license->ProductInfo);
2084 license_print_scope_list(license->log, license->ScopeList);
2089BOOL license_write_license_request_packet(
const rdpLicense* license,
wStream* s)
2091 WINPR_ASSERT(license);
2094 if (!license_check_stream_capacity(license->log, s,
sizeof(license->ServerRandom),
2097 Stream_Write(s, license->ServerRandom,
sizeof(license->ServerRandom));
2100 if (!license_write_product_info(license->log, s, license->ProductInfo))
2104 if (!license_write_binary_blob(s, license->KeyExchangeList))
2108 if (!license_write_binary_blob(s, license->ServerCertificate))
2112 if (!license_write_scope_list(license->log, s, license->ScopeList))
2119static BOOL license_send_license_request_packet(rdpLicense* license)
2121 UINT16 sec_flags = 0;
2122 wStream* s = license_send_stream_init(license, &sec_flags);
2126 if (!license_write_license_request_packet(license, s))
2129 return license_send(license, s, LICENSE_REQUEST, sec_flags);
2143BOOL license_read_platform_challenge_packet(rdpLicense* license,
wStream* s)
2145 BYTE macData[LICENSING_ENCRYPTION_KEY_LENGTH] = WINPR_C_ARRAY_INIT;
2147 WINPR_ASSERT(license);
2149 DEBUG_LICENSE(
"Receiving Platform Challenge Packet");
2151 if (!license_check_stream_length(license->log, s, 4,
"license platform challenge", 0))
2156 Stream_Seek_UINT32(s);
2159 license->EncryptedPlatformChallenge->type = BB_ANY_BLOB;
2160 if (!license_read_binary_blob(license->log, s, license->EncryptedPlatformChallenge))
2162 license->EncryptedPlatformChallenge->type = BB_ENCRYPTED_DATA_BLOB;
2165 if (!license_check_stream_length(license->log, s,
sizeof(macData),
2166 "license platform challenge::MAC", 0))
2169 Stream_Read(s, macData,
sizeof(macData));
2170 if (!license_decrypt_and_check_MAC(license, license->EncryptedPlatformChallenge->data,
2171 license->EncryptedPlatformChallenge->length,
2172 license->PlatformChallenge, macData))
2175 WLog_Print(license->log, WLOG_TRACE,
"platform challenge read");
2177#ifdef WITH_DEBUG_LICENSE
2178 WLog_Print(license->log, WLOG_DEBUG,
"EncryptedPlatformChallenge:");
2179 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedPlatformChallenge->data,
2180 license->EncryptedPlatformChallenge->length);
2181 WLog_Print(license->log, WLOG_DEBUG,
"PlatformChallenge:");
2182 winpr_HexLogDump(license->log, WLOG_DEBUG, license->PlatformChallenge->data,
2183 license->PlatformChallenge->length);
2184 WLog_Print(license->log, WLOG_DEBUG,
"MacData:");
2185 winpr_HexLogDump(license->log, WLOG_DEBUG, macData,
sizeof(macData));
2190BOOL license_send_error_alert(rdpLicense* license, UINT32 dwErrorCode, UINT32 dwStateTransition,
2193 UINT16 sec_flags = 0;
2194 wStream* s = license_send_stream_init(license, &sec_flags);
2199 if (!license_check_stream_capacity(license->log, s, 8,
"license error alert"))
2201 Stream_Write_UINT32(s, dwErrorCode);
2202 Stream_Write_UINT32(s, dwStateTransition);
2206 if (!license_write_binary_blob(s, info))
2210 return license_send(license, s, ERROR_ALERT, sec_flags);
2216BOOL license_send_platform_challenge_packet(rdpLicense* license)
2218 UINT16 sec_flags = 0;
2219 wStream* s = license_send_stream_init(license, &sec_flags);
2224 DEBUG_LICENSE(
"Receiving Platform Challenge Packet");
2226 if (!license_check_stream_capacity(license->log, s, 4,
"license platform challenge"))
2232 if (!license_write_binary_blob(s, license->EncryptedPlatformChallenge))
2236 if (!license_check_stream_length(license->log, s,
sizeof(license->MACData),
2237 "license platform challenge::MAC", 0))
2240 Stream_Write(s, license->MACData,
sizeof(license->MACData));
2242 return license_send(license, s, PLATFORM_CHALLENGE, sec_flags);
2249static BOOL license_read_encrypted_blob(
const rdpLicense* license,
wStream* s,
LICENSE_BLOB* target)
2251 UINT16 wBlobType = 0;
2252 UINT16 wBlobLen = 0;
2254 WINPR_ASSERT(license);
2255 WINPR_ASSERT(target);
2257 if (!license_check_stream_length(license->log, s, 4,
"license encrypted blob", 0))
2260 Stream_Read_UINT16(s, wBlobType);
2261 if (wBlobType != BB_ENCRYPTED_DATA_BLOB)
2264 license->log, WLOG_WARN,
2265 "expecting BB_ENCRYPTED_DATA_BLOB blob, probably a windows 2003 server, continuing...");
2268 Stream_Read_UINT16(s, wBlobLen);
2270 BYTE* encryptedData = Stream_Pointer(s);
2271 if (!Stream_SafeSeek(s, wBlobLen))
2273 WLog_Print(license->log, WLOG_WARN,
2274 "short license encrypted blob::length, expected %" PRIu16
" bytes, got %" PRIuz,
2275 wBlobLen, Stream_GetRemainingLength(s));
2279 return license_rc4_with_licenseKey(license, encryptedData, wBlobLen, target);
2289BOOL license_read_new_or_upgrade_license_packet(rdpLicense* license,
wStream* s)
2291 UINT32 os_major = 0;
2292 UINT32 os_minor = 0;
2294 UINT32 cbCompanyName = 0;
2295 UINT32 cbProductId = 0;
2296 UINT32 cbLicenseInfo = 0;
2297 wStream sbuffer = WINPR_C_ARRAY_INIT;
2298 wStream* licenseStream =
nullptr;
2300 BYTE computedMac[16] = WINPR_C_ARRAY_INIT;
2301 const BYTE* readMac =
nullptr;
2303 WINPR_ASSERT(license);
2305 DEBUG_LICENSE(
"Receiving Server New/Upgrade License Packet");
2307 LICENSE_BLOB* calBlob = license_new_binary_blob(BB_DATA_BLOB);
2312 if (!license_read_encrypted_blob(license, s, calBlob))
2316 readMac = Stream_Pointer(s);
2317 if (!Stream_SafeSeek(s,
sizeof(computedMac)))
2319 WLog_Print(license->log, WLOG_WARN,
2320 "short license new/upgrade, expected 16 bytes, got %" PRIuz,
2321 Stream_GetRemainingLength(s));
2325 if (!security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), calBlob->data,
2326 calBlob->length, computedMac,
sizeof(computedMac)))
2329 if (memcmp(computedMac, readMac,
sizeof(computedMac)) != 0)
2331 WLog_Print(license->log, WLOG_ERROR,
"new or upgrade license MAC mismatch");
2335 licenseStream = Stream_StaticConstInit(&sbuffer, calBlob->data, calBlob->length);
2338 WLog_Print(license->log, WLOG_ERROR,
2339 "license::blob::data=%p, license::blob::length=%" PRIu16,
2340 (
const void*)calBlob->data, calBlob->length);
2344 if (!license_check_stream_length(license->log, licenseStream, 8,
2345 "license new/upgrade::blob::version", 0))
2348 Stream_Read_UINT16(licenseStream, os_minor);
2349 Stream_Read_UINT16(licenseStream, os_major);
2351 WLog_Print(license->log, WLOG_DEBUG,
"Version: %" PRIu16
".%" PRIu16, os_major, os_minor);
2354 Stream_Read_UINT32(licenseStream, cbScope);
2355 if (!license_check_stream_length(license->log, licenseStream, cbScope,
2356 "license new/upgrade::blob::scope", LICENSE_MAX_SCOPE_LENGTH))
2359#ifdef WITH_DEBUG_LICENSE
2360 WLog_Print(license->log, WLOG_DEBUG,
"Scope:");
2361 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbScope);
2363 Stream_Seek(licenseStream, cbScope);
2366 if (!license_check_stream_length(license->log, licenseStream, 4,
2367 "license new/upgrade::blob::cbCompanyName", 0))
2370 Stream_Read_UINT32(licenseStream, cbCompanyName);
2371 if (!license_check_stream_length(license->log, licenseStream, cbCompanyName,
2372 "license new/upgrade::blob::CompanyName",
2373 LICENSE_MAX_COMPANY_NAME_LENGTH))
2376#ifdef WITH_DEBUG_LICENSE
2377 WLog_Print(license->log, WLOG_DEBUG,
"Company name:");
2378 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbCompanyName);
2380 Stream_Seek(licenseStream, cbCompanyName);
2383 if (!license_check_stream_length(license->log, licenseStream, 4,
2384 "license new/upgrade::blob::cbProductId", 0))
2387 Stream_Read_UINT32(licenseStream, cbProductId);
2389 if (!license_check_stream_length(license->log, licenseStream, cbProductId,
2390 "license new/upgrade::blob::ProductId",
2391 LICENSE_MAX_PRODUCT_ID_LENGTH))
2394#ifdef WITH_DEBUG_LICENSE
2395 WLog_Print(license->log, WLOG_DEBUG,
"Product id:");
2396 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbProductId);
2398 Stream_Seek(licenseStream, cbProductId);
2401 if (!license_check_stream_length(license->log, licenseStream, 4,
2402 "license new/upgrade::blob::cbLicenseInfo", 0))
2405 Stream_Read_UINT32(licenseStream, cbLicenseInfo);
2406 if (!license_check_stream_length(license->log, licenseStream, cbLicenseInfo,
2407 "license new/upgrade::blob::LicenseInfo",
2408 LICENSE_MAX_CAL_LENGTH))
2411 license->type = LICENSE_TYPE_ISSUED;
2412 license_set_state(license, LICENSE_STATE_COMPLETED);
2415 if (!license->rdp->settings->OldLicenseBehaviour)
2416 ret = saveCal(license->log, license->rdp->settings, Stream_Pointer(licenseStream),
2417 cbLicenseInfo, license->rdp->settings->ClientHostname);
2420 license_free_binary_blob(calBlob);
2431BOOL license_read_error_alert_packet(rdpLicense* license,
wStream* s)
2433 UINT32 dwErrorCode = 0;
2434 UINT32 dwStateTransition = 0;
2436 WINPR_ASSERT(license);
2437 WINPR_ASSERT(license->rdp);
2439 if (!license_check_stream_length(license->log, s, 8ul,
"error alert", 0))
2442 Stream_Read_UINT32(s, dwErrorCode);
2443 Stream_Read_UINT32(s, dwStateTransition);
2445 if (!license_read_binary_blob(license->log, s, license->ErrorInfo))
2448#ifdef WITH_DEBUG_LICENSE
2449 WLog_Print(license->log, WLOG_DEBUG,
"dwErrorCode: %s, dwStateTransition: %s",
2450 error_codes(dwErrorCode), state_transitions(dwStateTransition));
2453 if (dwErrorCode == STATUS_VALID_CLIENT)
2455 license->type = LICENSE_TYPE_NONE;
2456 license_set_state(license, LICENSE_STATE_COMPLETED);
2460 switch (dwStateTransition)
2462 case ST_TOTAL_ABORT:
2463 license_set_state(license, LICENSE_STATE_ABORTED);
2465 case ST_NO_TRANSITION:
2466 license_set_state(license, LICENSE_STATE_COMPLETED);
2468 case ST_RESET_PHASE_TO_START:
2469 license_set_state(license, LICENSE_STATE_CONFIGURED);
2471 case ST_RESEND_LAST_MESSAGE:
2487BOOL license_write_new_license_request_packet(
const rdpLicense* license,
wStream* s)
2489 WINPR_ASSERT(license);
2491 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
2495 if (!license_check_stream_capacity(license->log, s, 8 +
sizeof(license->ClientRandom),
2499 Stream_Write_UINT32(s,
2500 license->PreferredKeyExchangeAlg);
2501 Stream_Write_UINT32(s, license->PlatformId);
2502 Stream_Write(s, license->ClientRandom,
2503 sizeof(license->ClientRandom));
2506 !license_write_encrypted_premaster_secret_blob(
2507 license->log, s, license->EncryptedPremasterSecret, info->ModulusLength) ||
2509 !license_write_binary_blob(s, license->ClientUserName) ||
2511 !license_write_binary_blob(s, license->ClientMachineName))
2516 WLog_Print(license->log, WLOG_TRACE,
"new license written");
2518#ifdef WITH_DEBUG_LICENSE
2519 WLog_Print(license->log, WLOG_DEBUG,
"PreferredKeyExchangeAlg: 0x%08" PRIX32
"",
2520 license->PreferredKeyExchangeAlg);
2521 WLog_Print(license->log, WLOG_DEBUG,
"ClientRandom:");
2522 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ClientRandom,
2523 sizeof(license->ClientRandom));
2524 WLog_Print(license->log, WLOG_DEBUG,
"EncryptedPremasterSecret");
2525 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedPremasterSecret->data,
2526 license->EncryptedPremasterSecret->length);
2527 WLog_Print(license->log, WLOG_DEBUG,
"ClientUserName (%" PRIu16
"): %s",
2528 license->ClientUserName->length, (
char*)license->ClientUserName->data);
2529 WLog_Print(license->log, WLOG_DEBUG,
"ClientMachineName (%" PRIu16
"): %s",
2530 license->ClientMachineName->length, (
char*)license->ClientMachineName->data);
2535BOOL license_read_new_license_request_packet(rdpLicense* license,
wStream* s)
2537 UINT32 PreferredKeyExchangeAlg = 0;
2539 WINPR_ASSERT(license);
2541 if (!license_check_stream_length(license->log, s, 8ull +
sizeof(license->ClientRandom),
2542 "new license request", 0))
2545 Stream_Read_UINT32(s, PreferredKeyExchangeAlg);
2546 if (!license_check_preferred_alg(license, PreferredKeyExchangeAlg,
"new license request"))
2549 Stream_Read_UINT32(s, license->PlatformId);
2550 Stream_Read(s, license->ClientRandom,
2551 sizeof(license->ClientRandom));
2554 UINT32 ModulusLength = 0;
2555 if (!license_read_encrypted_premaster_secret_blob(
2556 license->log, s, license->EncryptedPremasterSecret, &ModulusLength))
2559 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
2561 WLog_Print(license->log, WLOG_WARN,
2562 "Missing license certificate, skipping ModulusLength checks");
2563 else if (ModulusLength != info->ModulusLength)
2565 WLog_Print(license->log, WLOG_WARN,
2566 "EncryptedPremasterSecret expected to be %" PRIu32
" bytes, but read %" PRIu32
2568 info->ModulusLength, ModulusLength);
2573 if (!license_read_binary_blob(license->log, s, license->ClientUserName))
2576 if (!license_read_binary_blob(license->log, s, license->ClientMachineName))
2588BOOL license_answer_license_request(rdpLicense* license)
2590 UINT16 sec_flags = 0;
2592 BYTE* license_data =
nullptr;
2593 size_t license_size = 0;
2595 char* username =
nullptr;
2597 WINPR_ASSERT(license);
2598 WINPR_ASSERT(license->rdp);
2599 WINPR_ASSERT(license->rdp->settings);
2601 if (!license->rdp->settings->OldLicenseBehaviour)
2602 license_data = loadCalFile(license->log, license->rdp->settings,
2603 license->rdp->settings->ClientHostname, &license_size);
2608 BYTE signature[LICENSING_ENCRYPTION_KEY_LENGTH] = WINPR_C_ARRAY_INIT;
2610 DEBUG_LICENSE(
"Sending Saved License Packet");
2612 WINPR_ASSERT(license->EncryptedHardwareId);
2613 license->EncryptedHardwareId->type = BB_ENCRYPTED_DATA_BLOB;
2614 if (!license_encrypt_and_MAC(license, license->HardwareId,
sizeof(license->HardwareId),
2615 license->EncryptedHardwareId, signature,
sizeof(signature)))
2621 calBlob = license_new_binary_blob(BB_DATA_BLOB);
2627 calBlob->data = license_data;
2628 WINPR_ASSERT(license_size <= UINT16_MAX);
2629 calBlob->length = (UINT16)license_size;
2631 status = license_send_license_info(license, calBlob, signature,
sizeof(signature));
2632 license_free_binary_blob(calBlob);
2637 DEBUG_LICENSE(
"Sending New License Packet");
2639 s = license_send_stream_init(license, &sec_flags);
2642 if (license->rdp->settings->Username !=
nullptr)
2643 username = license->rdp->settings->Username;
2645 username =
"username";
2648 WINPR_ASSERT(license->ClientUserName);
2649 const size_t len = strlen(username) + 1;
2650 WINPR_ASSERT(len <= UINT16_MAX);
2652 license->ClientUserName->data = (BYTE*)username;
2653 license->ClientUserName->length = (UINT16)len;
2657 WINPR_ASSERT(license->ClientMachineName);
2658 const size_t len = strlen(license->rdp->settings->ClientHostname) + 1;
2659 WINPR_ASSERT(len <= UINT16_MAX);
2661 license->ClientMachineName->data = (BYTE*)license->rdp->settings->ClientHostname;
2662 license->ClientMachineName->length = (UINT16)len;
2664 status = license_write_new_license_request_packet(license, s);
2666 WINPR_ASSERT(license->ClientUserName);
2667 license->ClientUserName->data =
nullptr;
2668 license->ClientUserName->length = 0;
2670 WINPR_ASSERT(license->ClientMachineName);
2671 license->ClientMachineName->data =
nullptr;
2672 license->ClientMachineName->length = 0;
2680 return license_send(license, s, NEW_LICENSE_REQUEST, sec_flags);
2689BOOL license_send_platform_challenge_response(rdpLicense* license)
2691 wStream* challengeRespData =
nullptr;
2692 BYTE* buffer =
nullptr;
2695 WINPR_ASSERT(license);
2696 WINPR_ASSERT(license->PlatformChallenge);
2697 WINPR_ASSERT(license->MacSaltKey);
2698 WINPR_ASSERT(license->EncryptedPlatformChallenge);
2699 WINPR_ASSERT(license->EncryptedHardwareId);
2701 DEBUG_LICENSE(
"Sending Platform Challenge Response Packet");
2703 license->EncryptedPlatformChallenge->type = BB_DATA_BLOB;
2706 challengeRespData = Stream_New(
nullptr, 8 + license->PlatformChallenge->length);
2707 if (!challengeRespData)
2709 Stream_Write_UINT16(challengeRespData, PLATFORM_CHALLENGE_RESPONSE_VERSION);
2710 Stream_Write_UINT16(challengeRespData, license->ClientType);
2711 Stream_Write_UINT16(challengeRespData, license->LicenseDetailLevel);
2712 Stream_Write_UINT16(challengeRespData, license->PlatformChallenge->length);
2713 Stream_Write(challengeRespData, license->PlatformChallenge->data,
2714 license->PlatformChallenge->length);
2715 Stream_SealLength(challengeRespData);
2718 const size_t length = Stream_Length(challengeRespData) +
sizeof(license->HardwareId);
2719 buffer = (BYTE*)malloc(length);
2722 Stream_Free(challengeRespData, TRUE);
2726 CopyMemory(buffer, Stream_Buffer(challengeRespData), Stream_Length(challengeRespData));
2727 CopyMemory(&buffer[Stream_Length(challengeRespData)], license->HardwareId,
2728 sizeof(license->HardwareId));
2729 status = security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), buffer, length,
2730 license->MACData,
sizeof(license->MACData));
2735 Stream_Free(challengeRespData, TRUE);
2739 license->EncryptedHardwareId->type = BB_ENCRYPTED_DATA_BLOB;
2740 if (!license_rc4_with_licenseKey(license, license->HardwareId,
sizeof(license->HardwareId),
2741 license->EncryptedHardwareId))
2743 Stream_Free(challengeRespData, TRUE);
2747 status = license_rc4_with_licenseKey(license, Stream_Buffer(challengeRespData),
2748 Stream_Length(challengeRespData),
2749 license->EncryptedPlatformChallengeResponse);
2750 Stream_Free(challengeRespData, TRUE);
2754#ifdef WITH_DEBUG_LICENSE
2755 WLog_Print(license->log, WLOG_DEBUG,
"LicensingEncryptionKey:");
2756 winpr_HexLogDump(license->log, WLOG_DEBUG, license->LicensingEncryptionKey, 16);
2757 WLog_Print(license->log, WLOG_DEBUG,
"HardwareId:");
2758 winpr_HexLogDump(license->log, WLOG_DEBUG, license->HardwareId,
sizeof(license->HardwareId));
2759 WLog_Print(license->log, WLOG_DEBUG,
"EncryptedHardwareId:");
2760 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedHardwareId->data,
2761 license->EncryptedHardwareId->length);
2763 UINT16 sec_flags = 0;
2764 wStream* s = license_send_stream_init(license, &sec_flags);
2768 if (license_write_client_platform_challenge_response(license, s))
2769 return license_send(license, s, PLATFORM_CHALLENGE_RESPONSE, sec_flags);
2775BOOL license_read_platform_challenge_response(WINPR_ATTR_UNUSED rdpLicense* license)
2777 WINPR_ASSERT(license);
2778 WINPR_ASSERT(license->PlatformChallenge);
2779 WINPR_ASSERT(license->MacSaltKey);
2780 WINPR_ASSERT(license->EncryptedPlatformChallenge);
2781 WINPR_ASSERT(license->EncryptedHardwareId);
2783 DEBUG_LICENSE(
"Receiving Platform Challenge Response Packet");
2785#if defined(WITH_LICENSE_DECRYPT_CHALLENGE_RESPONSE)
2787 LICENSE_BLOB* dblob = license_new_binary_blob(BB_ANY_BLOB);
2791 wStream sbuffer = WINPR_C_ARRAY_INIT;
2792 UINT16 wVersion = 0;
2793 UINT16 cbChallenge = 0;
2794 const BYTE* pbChallenge =
nullptr;
2795 LICENSE_BLOB* blob = license->EncryptedPlatformChallengeResponse;
2797 if (!license_rc4_with_licenseKey(license, blob->data, blob->length, dblob))
2800 wStream* s = Stream_StaticConstInit(&sbuffer, dblob->data, dblob->length);
2801 if (!license_check_stream_length(s, 8,
"PLATFORM_CHALLENGE_RESPONSE_DATA", 0))
2804 Stream_Read_UINT16(s, wVersion);
2805 if (wVersion != PLATFORM_CHALLENGE_RESPONSE_VERSION)
2807 WLog_Print(license->log, WLOG_WARN,
2808 "Invalid PLATFORM_CHALLENGE_RESPONSE_DATA::wVersion 0x%04" PRIx16
2809 ", expected 0x04" PRIx16,
2810 wVersion, PLATFORM_CHALLENGE_RESPONSE_VERSION);
2813 Stream_Read_UINT16(s, license->ClientType);
2814 Stream_Read_UINT16(s, license->LicenseDetailLevel);
2815 Stream_Read_UINT16(s, cbChallenge);
2817 if (!license_check_stream_length(s, cbChallenge,
2818 "PLATFORM_CHALLENGE_RESPONSE_DATA::pbChallenge", 0))
2821 pbChallenge = Stream_Pointer(s);
2822 if (!license_read_binary_blob_data(license->PlatformChallengeResponse, BB_DATA_BLOB,
2823 pbChallenge, cbChallenge))
2825 if (!Stream_SafeSeek(s, cbChallenge))
2830 license_free_binary_blob(dblob);
2837BOOL license_write_client_platform_challenge_response(rdpLicense* license,
wStream* s)
2839 WINPR_ASSERT(license);
2841 if (!license_write_binary_blob(s, license->EncryptedPlatformChallengeResponse))
2843 if (!license_write_binary_blob(s, license->EncryptedHardwareId))
2845 if (!license_check_stream_capacity(license->log, s,
sizeof(license->MACData),
2846 "CLIENT_PLATFORM_CHALLENGE_RESPONSE::MACData"))
2848 Stream_Write(s, license->MACData,
sizeof(license->MACData));
2852BOOL license_read_client_platform_challenge_response(rdpLicense* license,
wStream* s)
2854 WINPR_ASSERT(license);
2856 if (!license_read_binary_blob(license->log, s, license->EncryptedPlatformChallengeResponse))
2858 if (!license_read_binary_blob(license->log, s, license->EncryptedHardwareId))
2860 if (!license_check_stream_length(license->log, s,
sizeof(license->MACData),
2861 "CLIENT_PLATFORM_CHALLENGE_RESPONSE::MACData", 0))
2863 Stream_Read(s, license->MACData,
sizeof(license->MACData));
2864 return license_read_platform_challenge_response(license);
2876BOOL license_send_valid_client_error_packet(rdpRdp* rdp)
2879 rdpLicense* license = rdp->license;
2880 WINPR_ASSERT(license);
2882 license->state = LICENSE_STATE_COMPLETED;
2883 license->type = LICENSE_TYPE_NONE;
2884 return license_send_error_alert(license, STATUS_VALID_CLIENT, ST_NO_TRANSITION,
2885 license->ErrorInfo);
2894rdpLicense* license_new(rdpRdp* rdp)
2898 rdpLicense* license = (rdpLicense*)calloc(1,
sizeof(rdpLicense));
2901 license->log = WLog_Get(LICENSE_TAG);
2902 WINPR_ASSERT(license->log);
2904 license->PlatformId = PLATFORMID;
2905 license->ClientType = OTHER_PLATFORM_CHALLENGE_TYPE;
2906 license->LicenseDetailLevel = LICENSE_DETAIL_DETAIL;
2907 license->PreferredKeyExchangeAlg = KEY_EXCHANGE_ALG_RSA;
2910 license_set_state(license, LICENSE_STATE_INITIAL);
2911 if (!(license->certificate = freerdp_certificate_new()))
2913 if (!(license->ProductInfo = license_new_product_info()))
2915 if (!(license->ErrorInfo = license_new_binary_blob(BB_ERROR_BLOB)))
2917 if (!(license->LicenseInfo = license_new_binary_blob(BB_DATA_BLOB)))
2919 if (!(license->KeyExchangeList = license_new_binary_blob(BB_KEY_EXCHG_ALG_BLOB)))
2921 if (!(license->ServerCertificate = license_new_binary_blob(BB_CERTIFICATE_BLOB)))
2923 if (!(license->ClientUserName = license_new_binary_blob(BB_CLIENT_USER_NAME_BLOB)))
2925 if (!(license->ClientMachineName = license_new_binary_blob(BB_CLIENT_MACHINE_NAME_BLOB)))
2927 if (!(license->PlatformChallenge = license_new_binary_blob(BB_ANY_BLOB)))
2929 if (!(license->PlatformChallengeResponse = license_new_binary_blob(BB_ANY_BLOB)))
2931 if (!(license->EncryptedPlatformChallenge = license_new_binary_blob(BB_ANY_BLOB)))
2933 if (!(license->EncryptedPlatformChallengeResponse =
2934 license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2936 if (!(license->EncryptedPremasterSecret = license_new_binary_blob(BB_ANY_BLOB)))
2938 if (!(license->EncryptedHardwareId = license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2940 if (!(license->EncryptedLicenseInfo = license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2942 if (!(license->ScopeList = license_new_scope_list()))
2945 if (!license_generate_randoms(license))
2951 WINPR_PRAGMA_DIAG_PUSH
2952 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2953 license_free(license);
2954 WINPR_PRAGMA_DIAG_POP
2963void license_free(rdpLicense* license)
2967 freerdp_certificate_free(license->certificate);
2968 license_free_product_info(license->ProductInfo);
2969 license_free_binary_blob(license->ErrorInfo);
2970 license_free_binary_blob(license->LicenseInfo);
2971 license_free_binary_blob(license->KeyExchangeList);
2972 license_free_binary_blob(license->ServerCertificate);
2973 license_free_binary_blob(license->ClientUserName);
2974 license_free_binary_blob(license->ClientMachineName);
2975 license_free_binary_blob(license->PlatformChallenge);
2976 license_free_binary_blob(license->PlatformChallengeResponse);
2977 license_free_binary_blob(license->EncryptedPlatformChallenge);
2978 license_free_binary_blob(license->EncryptedPlatformChallengeResponse);
2979 license_free_binary_blob(license->EncryptedPremasterSecret);
2980 license_free_binary_blob(license->EncryptedHardwareId);
2981 license_free_binary_blob(license->EncryptedLicenseInfo);
2982 license_free_scope_list(license->ScopeList);
2987LICENSE_STATE license_get_state(
const rdpLicense* license)
2989 WINPR_ASSERT(license);
2990 return license->state;
2993LICENSE_TYPE license_get_type(
const rdpLicense* license)
2995 WINPR_ASSERT(license);
2996 return license->type;
2999void license_set_state(rdpLicense* license, LICENSE_STATE state)
3001 WINPR_ASSERT(license);
3002 license->state = state;
3005 case LICENSE_STATE_COMPLETED:
3007 case LICENSE_STATE_ABORTED:
3009 license->type = LICENSE_TYPE_INVALID;
3014const char* license_get_state_string(LICENSE_STATE state)
3018 case LICENSE_STATE_INITIAL:
3019 return "LICENSE_STATE_INITIAL";
3020 case LICENSE_STATE_CONFIGURED:
3021 return "LICENSE_STATE_CONFIGURED";
3022 case LICENSE_STATE_REQUEST:
3023 return "LICENSE_STATE_REQUEST";
3024 case LICENSE_STATE_NEW_REQUEST:
3025 return "LICENSE_STATE_NEW_REQUEST";
3026 case LICENSE_STATE_PLATFORM_CHALLENGE:
3027 return "LICENSE_STATE_PLATFORM_CHALLENGE";
3028 case LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE:
3029 return "LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE";
3030 case LICENSE_STATE_COMPLETED:
3031 return "LICENSE_STATE_COMPLETED";
3032 case LICENSE_STATE_ABORTED:
3033 return "LICENSE_STATE_ABORTED";
3035 return "LICENSE_STATE_UNKNOWN";
3039BOOL license_server_send_request(rdpLicense* license)
3041 if (!license_ensure_state(license, LICENSE_STATE_CONFIGURED, LICENSE_REQUEST))
3043 if (!license_send_license_request_packet(license))
3045 license_set_state(license, LICENSE_STATE_REQUEST);
3050static BOOL license_set_string(wLog* log,
const char* what,
const char* value, BYTE** bdst,
3054 WINPR_ASSERT(value);
3056 WINPR_ASSERT(dstLen);
3066 *cnv.w = ConvertUtf8ToWCharAlloc(value, &len);
3067 if (!*cnv.w || (len > UINT32_MAX /
sizeof(WCHAR)))
3069 WLog_Print(log, WLOG_ERROR,
"license->ProductInfo: %s == %p || %" PRIuz
" > UINT32_MAX",
3070 what, (
void*)(*cnv.w), len);
3073 *dstLen = (UINT32)(len *
sizeof(WCHAR));
3077BOOL license_server_configure(rdpLicense* license)
3080 UINT32 algs[] = { KEY_EXCHANGE_ALG_RSA };
3082 WINPR_ASSERT(license);
3083 WINPR_ASSERT(license->rdp);
3085 const rdpSettings* settings = license->rdp->settings;
3087 const char* CompanyName =
3090 const char* ProductName =
3092 const UINT32 ProductVersion =
3094 const UINT32 issuerCount =
3098 const char** issuers = WINPR_REINTERPRET_CAST(ptr,
const void*,
const char**);
3100 WINPR_ASSERT(CompanyName);
3101 WINPR_ASSERT(ProductName);
3102 WINPR_ASSERT(ProductVersion > 0);
3103 WINPR_ASSERT(issuers || (issuerCount == 0));
3105 if (!license_ensure_state(license, LICENSE_STATE_INITIAL, LICENSE_REQUEST))
3108 license->ProductInfo->dwVersion = ProductVersion;
3109 if (!license_set_string(license->log,
"pbCompanyName", CompanyName,
3110 &license->ProductInfo->pbCompanyName,
3111 &license->ProductInfo->cbCompanyName))
3114 if (!license_set_string(license->log,
"pbProductId", ProductName,
3115 &license->ProductInfo->pbProductId, &license->ProductInfo->cbProductId))
3118 if (!license_read_binary_blob_data(license->log, license->KeyExchangeList,
3119 BB_KEY_EXCHG_ALG_BLOB, algs,
sizeof(algs)))
3122 if (!freerdp_certificate_read_server_cert(license->certificate, settings->ServerCertificate,
3123 settings->ServerCertificateLength))
3126 s = Stream_New(
nullptr, 1024);
3133 freerdp_certificate_write_server_cert(license->certificate, CERT_CHAIN_VERSION_2, s);
3135 r = license_read_binary_blob_data(license->log, license->ServerCertificate,
3136 BB_CERTIFICATE_BLOB, Stream_Buffer(s),
3137 Stream_GetPosition(s));
3139 Stream_Free(s, TRUE);
3144 if (!license_scope_list_resize(license->ScopeList, issuerCount))
3146 for (
size_t x = 0; x < issuerCount; x++)
3149 const char* name = issuers[x];
3150 const size_t length = strnlen(name, UINT16_MAX) + 1;
3151 if ((length == 0) || (length > UINT16_MAX))
3153 WLog_Print(license->log, WLOG_WARN,
3154 "Invalid issuer at position %" PRIuz
": length 0 < %" PRIuz
" <= %d"
3156 x, length, UINT16_MAX, name);
3159 if (!license_read_binary_blob_data(license->log, blob, BB_SCOPE_BLOB, name, length))
3163 license_set_state(license, LICENSE_STATE_CONFIGURED);
3167rdpLicense* license_get(rdpContext* context)
3169 WINPR_ASSERT(context);
3170 WINPR_ASSERT(context->rdp);
3171 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.