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];
207 LICENSE_PRODUCT_INFO* ProductInfo;
208 LICENSE_BLOB* ErrorInfo;
209 LICENSE_BLOB* LicenseInfo;
210 LICENSE_BLOB* KeyExchangeList;
211 LICENSE_BLOB* ServerCertificate;
212 LICENSE_BLOB* ClientUserName;
213 LICENSE_BLOB* ClientMachineName;
214 LICENSE_BLOB* PlatformChallenge;
215 LICENSE_BLOB* PlatformChallengeResponse;
216 LICENSE_BLOB* EncryptedPremasterSecret;
217 LICENSE_BLOB* EncryptedPlatformChallenge;
218 LICENSE_BLOB* EncryptedPlatformChallengeResponse;
219 LICENSE_BLOB* EncryptedHardwareId;
220 LICENSE_BLOB* EncryptedLicenseInfo;
221 BYTE MACData[LICENSING_ENCRYPTION_KEY_LENGTH];
222 SCOPE_LIST* ScopeList;
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);
486 winpr_zfree(CompanyName);
487 winpr_zfree(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);
508static BOOL license_ensure_state(rdpLicense* license, LICENSE_STATE state, UINT32 msg)
510 const LICENSE_STATE cstate = license_get_state(license);
512 WINPR_ASSERT(license);
516 const char* scstate = license_get_state_string(cstate);
517 const char* sstate = license_get_state_string(state);
518 const char* where = license_request_type_string(msg);
520 WLog_Print(license->log, WLOG_WARN,
521 "Received [%s], but found invalid licensing state %s, expected %s", where,
528state_run_t license_recv(rdpLicense* license,
wStream* s)
530 WINPR_ASSERT(license);
531 WINPR_ASSERT(license->rdp);
532 WINPR_ASSERT(license->rdp->settings);
535 return license_server_recv(license, s);
537 return license_client_recv(license, s);
541static BOOL license_check_stream_length(wLog* log,
wStream* s, UINT64 expect,
const char* where,
544 const size_t remain = Stream_GetRemainingLength(s);
550 WLog_Print(log, WLOG_WARN,
"short %s, expected %" PRIu64
" bytes, got %" PRIuz, where,
554 if ((limit > 0) && (remain > limit))
556 WLog_Print(log, WLOG_WARN,
557 "oversized %s, got %" PRIuz
" bytes, but size is limited to %" PRIu64
".", where,
565static BOOL license_check_stream_capacity(wLog* log,
wStream* s,
size_t expect,
const char* where)
569 return (Stream_CheckAndLogRequiredCapacityWLogEx(log, WLOG_WARN, s, expect, 1,
570 "%s(%s:%" PRIuz
") %s", __func__, __FILE__,
571 (
size_t)__LINE__, where));
575static BOOL computeCalHash(wLog* log,
const char* hostname,
char* hashStr,
size_t len)
577 WINPR_DIGEST_CTX* sha1 =
nullptr;
579 BYTE hash[WINPR_SHA1_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT;
581 WINPR_ASSERT(hostname);
582 WINPR_ASSERT(hashStr);
584 if (len < 2 *
sizeof(hash) + 1)
587 if (!(sha1 = winpr_Digest_New()))
589 if (!winpr_Digest_Init(sha1, WINPR_MD_SHA1))
591 if (!winpr_Digest_Update(sha1, hostname, strlen(hostname)))
593 if (!winpr_Digest_Final(sha1, hash,
sizeof(hash)))
596 winpr_BinToHexStringBuffer(hash,
sizeof(hash), hashStr, len, FALSE);
601 WLog_Print(log, WLOG_ERROR,
"failed to generate SHA1 of hostname '%s'", hostname);
602 winpr_Digest_Free(sha1);
606WINPR_ATTR_MALLOC(winpr_zfree, 1)
607static
char* licenseCanonicalFilePath(wLog* log, const rdpSettings* settings, const
char* hostname,
611 WINPR_ASSERT(settings);
612 WINPR_ASSERT(hostname);
613 WINPR_ASSERT(extension);
615 char hash[MAX_PATH] = WINPR_C_ARRAY_INIT;
617 if (!computeCalHash(log, hostname, hash,
sizeof(hash)))
619 WLog_Print(log, WLOG_ERROR,
"unable to compute hostname hash");
626 WLog_Print(log, WLOG_ERROR,
"No LicenseStorePath available");
630 if (!winpr_PathFileExists(path))
632 if (!winpr_PathMakePath(path,
nullptr))
634 WLog_Print(log, WLOG_ERROR,
"error creating directory '%s'", path);
637 WLog_Print(log, WLOG_INFO,
"creating directory %s", path);
639 return GetCombinedPathV(path,
"%s.%s", hash, extension);
643static BOOL saveCal(wLog* log,
const rdpSettings* settings,
const BYTE* data,
size_t length,
644 const char* hostname)
651 WINPR_ASSERT(data || (length == 0));
652 WINPR_ASSERT(hostname);
654 char* filepath = licenseCanonicalFilePath(log, settings, hostname,
"cal");
655 char* filepathNew = licenseCanonicalFilePath(log, settings, hostname,
"cal.new");
658 WLog_Print(log, WLOG_ERROR,
"Failed to get license file path");
664 WLog_Print(log, WLOG_ERROR,
"Failed to get license new file path");
668 fp = winpr_fopen(filepathNew,
"wb");
671 WLog_Print(log, WLOG_ERROR,
"Failed to open license file '%s'", filepathNew);
675 written = fwrite(data, length, 1, fp);
680 WLog_Print(log, WLOG_ERROR,
"Failed to write to license file '%s'", filepathNew);
681 winpr_DeleteFile(filepathNew);
685 ret = winpr_MoveFileEx(filepathNew, filepath, MOVEFILE_REPLACE_EXISTING);
687 WLog_Print(log, WLOG_ERROR,
"Failed to move license file '%s' to '%s'", filepathNew,
691 winpr_zfree(filepathNew);
692 winpr_zfree(filepath);
696WINPR_ATTR_MALLOC(free, 1)
697static BYTE* loadCalFile(wLog* log, const rdpSettings* settings, const
char* hostname,
705 WINPR_ASSERT(settings);
706 WINPR_ASSERT(hostname);
707 WINPR_ASSERT(dataLen);
709 char* calPath = licenseCanonicalFilePath(log, settings, hostname,
"cal");
713 fp = winpr_fopen(calPath,
"rb");
717 if (_fseeki64(fp, 0, SEEK_END) != 0)
719 length = _ftelli64(fp);
720 if (_fseeki64(fp, 0, SEEK_SET) != 0)
725 if ((UINT64)length > LICENSE_MAX_CAL_LENGTH)
727 WLog_Print(log, WLOG_ERROR,
728 "%s: the file size %" PRId64
" exceeds the CAL size limit of %llu", __func__,
729 length, LICENSE_MAX_CAL_LENGTH);
733 ret = (BYTE*)malloc((
size_t)length);
737 status = fread(ret, (
size_t)length, 1, fp);
741 *dataLen = (size_t)length;
744 winpr_zfree(calPath);
749 winpr_znfree(ret, (
size_t)length);
752 winpr_zfree(calPath);
766static BOOL license_read_preamble(wLog* log,
wStream* s, BYTE* bMsgType, BYTE* flags,
769 WINPR_ASSERT(bMsgType);
771 WINPR_ASSERT(wMsgSize);
774 if (!license_check_stream_length(log, s, 4,
"license preamble", 0))
777 Stream_Read_UINT8(s, *bMsgType);
778 Stream_Read_UINT8(s, *flags);
779 Stream_Read_UINT16(s, *wMsgSize);
782 WLog_Print(log, WLOG_WARN,
783 "invalid license preamble::wMsgSize, expected value >= 4, got %" PRIu32,
787 return license_check_stream_length(log, s, *wMsgSize - 4ull,
"license preamble::wMsgSize", 0);
800static BOOL license_write_preamble(
wStream* s, BYTE bMsgType, BYTE flags, UINT16 wMsgSize)
802 if (!Stream_EnsureRemainingCapacity(s, 4))
806 Stream_Write_UINT8(s, bMsgType);
807 Stream_Write_UINT8(s, flags);
808 Stream_Write_UINT16(s, wMsgSize);
820wStream* license_send_stream_init(rdpLicense* license, UINT16* sec_flags)
822 WINPR_ASSERT(license);
823 WINPR_ASSERT(license->rdp);
824 WINPR_ASSERT(sec_flags);
826 const BOOL do_crypt = license->rdp->do_crypt;
828 *sec_flags = SEC_LICENSE_PKT;
840 *sec_flags |= SEC_LICENSE_ENCRYPT_CS;
841 license->rdp->do_crypt = license->rdp->do_crypt_license;
844 wStream* s = rdp_send_stream_init(license->rdp, sec_flags);
848 license->rdp->do_crypt = do_crypt;
849 license->PacketHeaderLength = (UINT16)Stream_GetPosition(s);
850 if (!Stream_SafeZero(s, LICENSE_PREAMBLE_LENGTH))
866static BOOL license_send(rdpLicense* license,
wStream* s, BYTE type, UINT16 sec_flags)
868 WINPR_ASSERT(license);
869 WINPR_ASSERT(license->rdp);
871 rdpRdp* rdp = license->rdp;
872 WINPR_ASSERT(rdp->settings);
874 DEBUG_LICENSE(
"Sending %s Packet", license_request_type_string(type));
875 const size_t length = Stream_GetPosition(s);
876 if (length < license->PacketHeaderLength)
878 if (length > UINT16_MAX + license->PacketHeaderLength)
881 const UINT16 wMsgSize = (UINT16)(length - license->PacketHeaderLength);
882 if (!Stream_SetPosition(s, license->PacketHeaderLength))
884 BYTE flags = PREAMBLE_VERSION_3_0;
891 if (!rdp->settings->ServerMode)
892 flags |= EXTENDED_ERROR_MSG_SUPPORTED;
894 if (!license_write_preamble(s, type, flags, wMsgSize))
900#ifdef WITH_DEBUG_LICENSE
901 WLog_Print(license->log, WLOG_DEBUG,
"Sending %s Packet, length %" PRIu16
"",
902 license_request_type_string(type), wMsgSize);
903 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_PointerAs(s,
char) - LICENSE_PREAMBLE_LENGTH,
906 if (!Stream_SetPosition(s, length))
908 const BOOL ret = rdp_send(rdp, s, MCS_GLOBAL_CHANNEL_ID, sec_flags);
912BOOL license_write_server_upgrade_license(
const rdpLicense* license,
wStream* s)
914 WINPR_ASSERT(license);
916 if (!license_write_binary_blob(s, license->EncryptedLicenseInfo))
918 if (!license_check_stream_capacity(license->log, s,
sizeof(license->MACData),
919 "SERVER_UPGRADE_LICENSE::MACData"))
921 Stream_Write(s, license->MACData,
sizeof(license->MACData));
926static BOOL license_server_send_new_or_upgrade_license(rdpLicense* license, BOOL upgrade)
928 UINT16 sec_flags = 0;
929 wStream* s = license_send_stream_init(license, &sec_flags);
930 const BYTE type = upgrade ? UPGRADE_LICENSE : NEW_LICENSE;
935 if (!license_write_server_upgrade_license(license, s))
938 return license_send(license, s, type, sec_flags);
953static state_run_t license_client_recv_int(rdpLicense* license,
wStream* s)
958 const size_t length = Stream_GetRemainingLength(s);
960 WINPR_ASSERT(license);
962 if (!license_read_preamble(license->log, s, &bMsgType, &flags,
964 return STATE_RUN_FAILED;
966 DEBUG_LICENSE(
"Receiving %s Packet", license_request_type_string(bMsgType));
970 case LICENSE_REQUEST:
972 if (license_get_state(license) == LICENSE_STATE_INITIAL)
973 license_set_state(license, LICENSE_STATE_CONFIGURED);
975 if (!license_ensure_state(license, LICENSE_STATE_CONFIGURED, bMsgType))
976 return STATE_RUN_FAILED;
978 if (!license_read_license_request_packet(license, s))
979 return STATE_RUN_FAILED;
981 if (!license_answer_license_request(license))
982 return STATE_RUN_FAILED;
984 license_set_state(license, LICENSE_STATE_NEW_REQUEST);
987 case PLATFORM_CHALLENGE:
988 if (!license_ensure_state(license, LICENSE_STATE_NEW_REQUEST, bMsgType))
989 return STATE_RUN_FAILED;
991 if (!license_read_platform_challenge_packet(license, s))
992 return STATE_RUN_FAILED;
994 if (!license_send_platform_challenge_response(license))
995 return STATE_RUN_FAILED;
996 license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE);
1000 case UPGRADE_LICENSE:
1001 if (!license_ensure_state(license, LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE, bMsgType))
1002 return STATE_RUN_FAILED;
1003 if (!license_read_new_or_upgrade_license_packet(license, s))
1004 return STATE_RUN_FAILED;
1008 if (!license_read_error_alert_packet(license, s))
1009 return STATE_RUN_FAILED;
1013 WLog_Print(license->log, WLOG_ERROR,
"invalid bMsgType:%" PRIu8
"", bMsgType);
1014 return STATE_RUN_FAILED;
1017 if (!tpkt_ensure_stream_consumed(license->log, s, length))
1018 return STATE_RUN_FAILED;
1019 return STATE_RUN_SUCCESS;
1022state_run_t license_client_recv(rdpLicense* license,
wStream* s)
1024 state_run_t rc = license_client_recv_int(license, s);
1025 if (state_run_failed(rc))
1027 freerdp_set_last_error(license->rdp->context, ERROR_CTX_LICENSE_CLIENT_INVALID);
1032state_run_t license_server_recv(rdpLicense* license,
wStream* s)
1034 state_run_t rc = STATE_RUN_FAILED;
1037 UINT16 wMsgSize = 0;
1038 const size_t length = Stream_GetRemainingLength(s);
1040 WINPR_ASSERT(license);
1042 if (!license_read_preamble(license->log, s, &bMsgType, &flags,
1046 DEBUG_LICENSE(
"Receiving %s Packet", license_request_type_string(bMsgType));
1050 case NEW_LICENSE_REQUEST:
1051 if (!license_ensure_state(license, LICENSE_STATE_REQUEST, bMsgType))
1053 if (!license_read_new_license_request_packet(license, s))
1056 if (!license_send_error_alert(license, ERR_INVALID_MAC, ST_TOTAL_ABORT,
1057 license->ErrorInfo))
1059 if (!license_send_platform_challenge_packet(license))
1061 license->update = FALSE;
1062 license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE);
1065 if (!license_ensure_state(license, LICENSE_STATE_REQUEST, bMsgType))
1067 if (!license_read_license_info(license, s))
1070 if (!license_send_platform_challenge_packet(license))
1072 license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE);
1073 license->update = TRUE;
1076 case PLATFORM_CHALLENGE_RESPONSE:
1077 if (!license_ensure_state(license, LICENSE_STATE_PLATFORM_CHALLENGE, bMsgType))
1079 if (!license_read_client_platform_challenge_response(license, s))
1085 if (license_send_error_alert(license, ERR_INVALID_CLIENT, ST_TOTAL_ABORT,
1086 license->ErrorInfo))
1091 if (!license_server_send_new_or_upgrade_license(license, license->update))
1094 license->type = LICENSE_TYPE_ISSUED;
1095 license_set_state(license, LICENSE_STATE_COMPLETED);
1097 rc = STATE_RUN_CONTINUE;
1102 if (!license_read_error_alert_packet(license, s))
1107 WLog_Print(license->log, WLOG_ERROR,
"invalid bMsgType:%" PRIu8
"", bMsgType);
1111 if (!tpkt_ensure_stream_consumed(license->log, s, length))
1114 if (!state_run_success(rc))
1115 rc = STATE_RUN_SUCCESS;
1118 if (state_run_failed(rc))
1120 if (flags & EXTENDED_ERROR_MSG_SUPPORTED)
1122 if (!license_send_error_alert(license, ERR_INVALID_CLIENT, ST_TOTAL_ABORT,
nullptr))
1124 WLog_Print(license->log, WLOG_ERROR,
"license_send_error_alert failed");
1127 license_set_state(license, LICENSE_STATE_ABORTED);
1133BOOL license_generate_randoms(rdpLicense* license)
1135 WINPR_ASSERT(license);
1137#ifdef LICENSE_NULL_CLIENT_RANDOM
1138 ZeroMemory(license->ClientRandom,
sizeof(license->ClientRandom));
1140 if (winpr_RAND(license->ClientRandom,
sizeof(license->ClientRandom)) < 0)
1144 if (winpr_RAND(license->ServerRandom,
sizeof(license->ServerRandom)) < 0)
1147#ifdef LICENSE_NULL_PREMASTER_SECRET
1148 ZeroMemory(license->PremasterSecret,
sizeof(license->PremasterSecret));
1150 if (winpr_RAND(license->PremasterSecret,
sizeof(license->PremasterSecret)) <
1162static BOOL license_generate_keys(rdpLicense* license)
1164 WINPR_ASSERT(license);
1168 !security_master_secret(license->PremasterSecret,
sizeof(license->PremasterSecret),
1169 license->ClientRandom,
sizeof(license->ClientRandom),
1170 license->ServerRandom,
sizeof(license->ServerRandom),
1171 license->MasterSecret,
sizeof(license->MasterSecret)) ||
1173 !security_session_key_blob(license->MasterSecret,
sizeof(license->MasterSecret),
1174 license->ClientRandom,
sizeof(license->ClientRandom),
1175 license->ServerRandom,
sizeof(license->ServerRandom),
1176 license->SessionKeyBlob,
sizeof(license->SessionKeyBlob)))
1180 security_mac_salt_key(license->SessionKeyBlob,
sizeof(license->SessionKeyBlob),
1181 license->ClientRandom,
sizeof(license->ClientRandom),
1182 license->ServerRandom,
sizeof(license->ServerRandom), license->MacSaltKey,
1183 sizeof(license->MacSaltKey));
1184 const BOOL ret = security_licensing_encryption_key(
1185 license->SessionKeyBlob,
sizeof(license->SessionKeyBlob), license->ClientRandom,
1186 sizeof(license->ClientRandom), license->ServerRandom,
sizeof(license->ServerRandom),
1187 license->LicensingEncryptionKey,
1188 sizeof(license->LicensingEncryptionKey));
1190 WLog_Print(license->log, WLOG_TRACE,
"license keys %s generated", ret ?
"successfully" :
"NOT");
1192#ifdef WITH_DEBUG_LICENSE
1193 WLog_Print(license->log, WLOG_DEBUG,
"ClientRandom:");
1194 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ClientRandom,
1195 sizeof(license->ClientRandom));
1196 WLog_Print(license->log, WLOG_DEBUG,
"ServerRandom:");
1197 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ServerRandom,
1198 sizeof(license->ServerRandom));
1199 WLog_Print(license->log, WLOG_DEBUG,
"PremasterSecret:");
1200 winpr_HexLogDump(license->log, WLOG_DEBUG, license->PremasterSecret,
1201 sizeof(license->PremasterSecret));
1202 WLog_Print(license->log, WLOG_DEBUG,
"MasterSecret:");
1203 winpr_HexLogDump(license->log, WLOG_DEBUG, license->MasterSecret,
1204 sizeof(license->MasterSecret));
1205 WLog_Print(license->log, WLOG_DEBUG,
"SessionKeyBlob:");
1206 winpr_HexLogDump(license->log, WLOG_DEBUG, license->SessionKeyBlob,
1207 sizeof(license->SessionKeyBlob));
1208 WLog_Print(license->log, WLOG_DEBUG,
"MacSaltKey:");
1209 winpr_HexLogDump(license->log, WLOG_DEBUG, license->MacSaltKey,
sizeof(license->MacSaltKey));
1210 WLog_Print(license->log, WLOG_DEBUG,
"LicensingEncryptionKey:");
1211 winpr_HexLogDump(license->log, WLOG_DEBUG, license->LicensingEncryptionKey,
1212 sizeof(license->LicensingEncryptionKey));
1222BOOL license_generate_hwid(rdpLicense* license)
1224 const BYTE* hashTarget =
nullptr;
1225 size_t targetLen = 0;
1226 BYTE macAddress[6] = WINPR_C_ARRAY_INIT;
1228 WINPR_ASSERT(license);
1229 WINPR_ASSERT(license->rdp);
1230 WINPR_ASSERT(license->rdp->settings);
1232 ZeroMemory(license->HardwareId,
sizeof(license->HardwareId));
1234 if (license->rdp->settings->OldLicenseBehaviour)
1236 hashTarget = macAddress;
1237 targetLen =
sizeof(macAddress);
1241 wStream buffer = WINPR_C_ARRAY_INIT;
1242 const char* hostname = license->rdp->settings->ClientHostname;
1243 wStream* s = Stream_StaticInit(&buffer, license->HardwareId, 4);
1244 Stream_Write_UINT32(s, license->PlatformId);
1246 hashTarget = (
const BYTE*)hostname;
1247 targetLen = hostname ? strlen(hostname) : 0;
1256 return winpr_Digest_Allow_FIPS(WINPR_MD_MD5, hashTarget, targetLen,
1257 &license->HardwareId[HWID_PLATFORM_ID_LENGTH],
1258 WINPR_MD5_DIGEST_LENGTH);
1262static BOOL license_get_server_rsa_public_key(rdpLicense* license)
1264 rdpSettings* settings =
nullptr;
1266 WINPR_ASSERT(license);
1267 WINPR_ASSERT(license->certificate);
1268 WINPR_ASSERT(license->rdp);
1270 settings = license->rdp->settings;
1271 WINPR_ASSERT(settings);
1273 if (license->ServerCertificate->length < 1)
1275 if (!freerdp_certificate_read_server_cert(license->certificate, settings->ServerCertificate,
1276 settings->ServerCertificateLength))
1283BOOL license_encrypt_premaster_secret(rdpLicense* license)
1285 WINPR_ASSERT(license);
1286 WINPR_ASSERT(license->certificate);
1288 if (!license_get_server_rsa_public_key(license))
1291 WINPR_ASSERT(license->EncryptedPremasterSecret);
1293 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1296 WLog_Print(license->log, WLOG_ERROR,
"info=%p, license->certificate=%p", (
const void*)info,
1297 (
void*)license->certificate);
1301#ifdef WITH_DEBUG_LICENSE
1302 WLog_Print(license->log, WLOG_DEBUG,
"Modulus (%" PRIu32
" bits):", info->ModulusLength * 8);
1303 winpr_HexLogDump(license->log, WLOG_DEBUG, info->Modulus, info->ModulusLength);
1304 WLog_Print(license->log, WLOG_DEBUG,
"Exponent:");
1305 winpr_HexLogDump(license->log, WLOG_DEBUG, info->exponent,
sizeof(info->exponent));
1308 BYTE* EncryptedPremasterSecret = (BYTE*)calloc(1, info->ModulusLength);
1309 if (!EncryptedPremasterSecret)
1311 WLog_Print(license->log, WLOG_ERROR,
1312 "EncryptedPremasterSecret=%p, info->ModulusLength=%" PRIu32,
1313 (
const void*)EncryptedPremasterSecret, info->ModulusLength);
1317 license->EncryptedPremasterSecret->type = BB_RANDOM_BLOB;
1318 license->EncryptedPremasterSecret->length =
sizeof(license->PremasterSecret);
1319#ifndef LICENSE_NULL_PREMASTER_SECRET
1321 const SSIZE_T length =
1322 crypto_rsa_public_encrypt(license->PremasterSecret,
sizeof(license->PremasterSecret),
1323 info, EncryptedPremasterSecret, info->ModulusLength);
1324 if ((length < 0) || (length > UINT16_MAX))
1326 WLog_Print(license->log, WLOG_ERROR,
"RSA public encrypt length=%" PRIdz
" < 0 || > %d",
1327 length, UINT16_MAX);
1328 winpr_znfree(EncryptedPremasterSecret, info->ModulusLength);
1331 license->EncryptedPremasterSecret->length = (UINT16)length;
1334 license->EncryptedPremasterSecret->data = EncryptedPremasterSecret;
1339static BOOL license_rc4_with_licenseKey(
const rdpLicense* license,
const BYTE* input,
size_t len,
1342 WINPR_ASSERT(license);
1343 WINPR_ASSERT(input || (len == 0));
1344 WINPR_ASSERT(target);
1345 WINPR_ASSERT(len <= UINT16_MAX);
1347 WINPR_RC4_CTX* rc4 = winpr_RC4_New_Allow_FIPS(license->LicensingEncryptionKey,
1348 sizeof(license->LicensingEncryptionKey));
1351 WLog_Print(license->log, WLOG_ERROR,
"Failed to allocate RC4");
1355 BYTE* buffer =
nullptr;
1357 buffer = realloc(target->data, len);
1361 target->data = buffer;
1362 target->length = (UINT16)len;
1364 if (!winpr_RC4_Update(rc4, len, input, buffer))
1367 winpr_RC4_Free(rc4);
1371 WLog_Print(license->log, WLOG_ERROR,
"Failed to create/update RC4: len=%" PRIuz
", buffer=%p",
1372 len, (
const void*)buffer);
1373 winpr_RC4_Free(rc4);
1389static BOOL license_encrypt_and_MAC(rdpLicense* license,
const BYTE* input,
size_t len,
1392 WINPR_ASSERT(license);
1393 return license_rc4_with_licenseKey(license, input, len, target) &&
1394 security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), input, len, mac,
1410static BOOL license_decrypt_and_check_MAC(rdpLicense* license,
const BYTE* input,
size_t len,
1413 BYTE macData[
sizeof(license->MACData)] = WINPR_C_ARRAY_INIT;
1415 WINPR_ASSERT(license);
1416 WINPR_ASSERT(target);
1420 WLog_Print(license->log, WLOG_DEBUG,
"TransportDumpReplay active, skipping...");
1424 if (!license_rc4_with_licenseKey(license, input, len, target))
1427 if (!security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), target->data, len,
1428 macData,
sizeof(macData)))
1431 if (memcmp(packetMac, macData,
sizeof(macData)) != 0)
1433 WLog_Print(license->log, WLOG_ERROR,
"packetMac != expectedMac");
1447 WINPR_ASSERT(productInfo);
1448 winpr_znfree(productInfo->pbCompanyName, productInfo->cbCompanyName);
1449 winpr_znfree(productInfo->pbProductId, productInfo->cbProductId);
1450 productInfo->pbCompanyName =
nullptr;
1451 productInfo->cbCompanyName = 0;
1452 productInfo->pbProductId =
nullptr;
1453 productInfo->cbProductId = 0;
1458 WINPR_ASSERT(productInfo);
1460 clear_productinfo(productInfo);
1462 if (!license_check_stream_length(log, s, 8,
"license product info::cbCompanyName", 0))
1465 Stream_Read_UINT32(s, productInfo->dwVersion);
1466 const UINT32 cbCompanyName = Stream_Get_UINT32(s);
1469 if ((cbCompanyName < 2) || (cbCompanyName % 2 != 0))
1471 WLog_Print(log, WLOG_WARN,
"license product info invalid cbCompanyName %" PRIu32,
1476 if (!license_check_stream_length(log, s, cbCompanyName,
"license product info::CompanyName",
1477 LICENSE_MAX_COMPANY_NAME_LENGTH))
1480 productInfo->pbCompanyName = (BYTE*)malloc(cbCompanyName);
1481 if (!productInfo->pbCompanyName)
1483 productInfo->cbCompanyName = cbCompanyName;
1484 Stream_Read(s, productInfo->pbCompanyName, productInfo->cbCompanyName);
1486 if (!license_check_stream_length(log, s, 4,
"license product info::cbProductId", 0))
1489 const UINT32 cbProductId = Stream_Get_UINT32(s);
1491 if ((cbProductId < 2) || (cbProductId % 2 != 0))
1493 WLog_Print(log, WLOG_WARN,
"license product info invalid cbProductId %" PRIu32,
1498 if (!license_check_stream_length(log, s, cbProductId,
"license product info::ProductId",
1499 LICENSE_MAX_PRODUCT_ID_LENGTH))
1502 productInfo->pbProductId = (BYTE*)malloc(cbProductId);
1503 if (!productInfo->pbProductId)
1505 productInfo->cbProductId = cbProductId;
1506 Stream_Read(s, productInfo->pbProductId, productInfo->cbProductId);
1510 clear_productinfo(productInfo);
1515static BOOL license_write_product_info(wLog* log,
wStream* s,
1518 WINPR_ASSERT(productInfo);
1520 if (!license_check_stream_capacity(log, s, 8,
"license product info::cbCompanyName"))
1523 Stream_Write_UINT32(s, productInfo->dwVersion);
1524 Stream_Write_UINT32(s, productInfo->cbCompanyName);
1527 if ((productInfo->cbCompanyName < 2) || (productInfo->cbCompanyName % 2 != 0) ||
1528 !productInfo->pbCompanyName)
1530 WLog_Print(log, WLOG_WARN,
"license product info invalid cbCompanyName %" PRIu32,
1531 productInfo->cbCompanyName);
1535 if (!license_check_stream_capacity(log, s, productInfo->cbCompanyName,
1536 "license product info::CompanyName"))
1539 Stream_Write(s, productInfo->pbCompanyName, productInfo->cbCompanyName);
1541 if (!license_check_stream_capacity(log, s, 4,
"license product info::cbProductId"))
1544 Stream_Write_UINT32(s, productInfo->cbProductId);
1546 if ((productInfo->cbProductId < 2) || (productInfo->cbProductId % 2 != 0) ||
1547 !productInfo->pbProductId)
1549 WLog_Print(log, WLOG_WARN,
"license product info invalid cbProductId %" PRIu32,
1550 productInfo->cbProductId);
1554 if (!license_check_stream_capacity(log, s, productInfo->cbProductId,
1555 "license product info::ProductId"))
1558 Stream_Write(s, productInfo->pbProductId, productInfo->cbProductId);
1587 clear_productinfo(productInfo);
1592BOOL license_read_binary_blob_data(wLog* log,
LICENSE_BLOB* blob, UINT16 wBlobType,
1593 const void* data,
size_t length)
1596 WINPR_ASSERT(length <= UINT16_MAX);
1597 WINPR_ASSERT(data || (length == 0));
1599 winpr_znfree(blob->data, blob->length);
1600 blob->length = (UINT16)length;
1601 blob->data =
nullptr;
1603 if ((blob->type != wBlobType) && (blob->type != BB_ANY_BLOB))
1605 WLog_Print(log, WLOG_ERROR,
"license binary blob::type expected %s, got %s",
1606 license_blob_type_string(wBlobType), license_blob_type_string(blob->type));
1613 if ((blob->type != BB_ANY_BLOB) && (blob->length == 0))
1615 WLog_Print(log, WLOG_DEBUG,
"license binary blob::type %s, length=0, skipping.",
1616 license_blob_type_string(blob->type));
1620 blob->type = wBlobType;
1621 blob->data =
nullptr;
1622 if (blob->length > 0)
1623 blob->data = malloc(blob->length);
1626 WLog_Print(log, WLOG_ERROR,
"license binary blob::length=%" PRIu16
", blob::data=%p",
1627 blob->length, (
const void*)blob->data);
1630 memcpy(blob->data, data, blob->length);
1643 UINT16 wBlobType = 0;
1648 if (!license_check_stream_length(log, s, 4,
"license binary blob::type", 0))
1651 Stream_Read_UINT16(s, wBlobType);
1652 Stream_Read_UINT16(s, length);
1654 if (!license_check_stream_length(log, s, length,
"license binary blob::length", 0))
1657 if (!license_read_binary_blob_data(log, blob, wBlobType, Stream_Pointer(s), length))
1660 return Stream_SafeSeek(s, length);
1674 if (!Stream_EnsureRemainingCapacity(s, blob->length + 4ull))
1677 Stream_Write_UINT16(s, blob->type);
1678 Stream_Write_UINT16(s, blob->length);
1680 if (blob->length > 0)
1681 Stream_Write(s, blob->data, blob->length);
1686static BOOL license_write_encrypted_premaster_secret_blob(wLog* log,
wStream* s,
1688 UINT32 ModulusLength)
1690 const UINT32 length = ModulusLength + 8;
1693 WINPR_ASSERT(length <= UINT16_MAX);
1695 if (blob->length > ModulusLength)
1697 WLog_Print(log, WLOG_ERROR,
"invalid blob");
1701 if (!Stream_EnsureRemainingCapacity(s, length + 4ull))
1703 Stream_Write_UINT16(s, blob->type);
1704 Stream_Write_UINT16(s, (UINT16)length);
1706 if (blob->length > 0)
1707 Stream_Write(s, blob->data, blob->length);
1709 Stream_Zero(s, length - blob->length);
1714static BOOL license_read_encrypted_premaster_secret_blob(wLog* log,
wStream* s,
LICENSE_BLOB* blob,
1715 UINT32* ModulusLength)
1717 if (!license_read_binary_blob(log, s, blob))
1719 WINPR_ASSERT(ModulusLength);
1720 *ModulusLength = blob->length;
1748 winpr_znfree(blob->data, blob->length);
1762 UINT32 scopeCount = 0;
1764 WINPR_ASSERT(scopeList);
1766 if (!license_check_stream_length(log, s, 4,
"license scope list", 0))
1769 Stream_Read_UINT32(s, scopeCount);
1771 if (!license_check_stream_length(log, s, 4ull * scopeCount,
"license scope list::count",
1772 LICENSE_MAX_SCOPE_LENGTH))
1775 if (!license_scope_list_resize(scopeList, scopeCount))
1778 for (UINT32 i = 0; i < scopeCount; i++)
1780 if (!license_read_binary_blob(log, s, scopeList->array[i]))
1787BOOL license_write_scope_list(wLog* log,
wStream* s,
const SCOPE_LIST* scopeList)
1789 WINPR_ASSERT(scopeList);
1791 if (!license_check_stream_capacity(log, s, 4,
"license scope list"))
1794 Stream_Write_UINT32(s, scopeList->count);
1796 if (!license_check_stream_capacity(log, s, scopeList->count * 4ull,
1797 "license scope list::count"))
1801 WINPR_ASSERT(scopeList->array || (scopeList->count == 0));
1802 for (UINT32 i = 0; i < scopeList->count; i++)
1806 if (!license_write_binary_blob(s, element))
1825static void license_scope_list_free(
SCOPE_LIST* scopeList, UINT32 count)
1827 WINPR_ASSERT(scopeList);
1828 WINPR_ASSERT(scopeList->array || (scopeList->count == 0));
1830 for (UINT32 x = count; x < scopeList->count; x++)
1832 license_free_binary_blob(scopeList->array[x]);
1833 scopeList->array[x] =
nullptr;
1838 free((
void*)scopeList->array);
1839 scopeList->array =
nullptr;
1843BOOL license_scope_list_resize(
SCOPE_LIST* scopeList, UINT32 count)
1845 license_scope_list_free(scopeList, count);
1852 scopeList->array = tmp;
1855 for (UINT32 x = scopeList->count; x < count; x++)
1857 LICENSE_BLOB* blob = license_new_binary_blob(BB_SCOPE_BLOB);
1860 scopeList->count = x;
1863 scopeList->array[x] = blob;
1866 scopeList->count = count;
1876void license_free_scope_list(
SCOPE_LIST* scopeList)
1881 license_scope_list_free(scopeList, 0);
1885BOOL license_send_license_info(rdpLicense* license,
const LICENSE_BLOB* calBlob,
1886 const BYTE* signature,
size_t signature_length)
1888 WINPR_ASSERT(calBlob);
1889 WINPR_ASSERT(signature);
1890 WINPR_ASSERT(license->certificate);
1892 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1896 UINT16 sec_flags = 0;
1897 wStream* s = license_send_stream_init(license, &sec_flags);
1901 if (!license_check_stream_capacity(license->log, s, 8 +
sizeof(license->ClientRandom),
1902 "license info::ClientRandom"))
1905 Stream_Write_UINT32(s,
1906 license->PreferredKeyExchangeAlg);
1907 Stream_Write_UINT32(s, license->PlatformId);
1910 Stream_Write(s, license->ClientRandom,
sizeof(license->ClientRandom));
1913 if (!license_write_encrypted_premaster_secret_blob(
1914 license->log, s, license->EncryptedPremasterSecret, info->ModulusLength))
1918 if (!license_write_binary_blob(s, calBlob))
1922 if (!license_write_binary_blob(s, license->EncryptedHardwareId))
1926 if (!license_check_stream_capacity(license->log, s, signature_length,
"license info::MACData"))
1928 Stream_Write(s, signature, signature_length);
1930 return license_send(license, s, LICENSE_INFO, sec_flags);
1938static BOOL license_check_preferred_alg(rdpLicense* license, UINT32 PreferredKeyExchangeAlg,
1941 WINPR_ASSERT(license);
1942 WINPR_ASSERT(where);
1944 if (license->PreferredKeyExchangeAlg != PreferredKeyExchangeAlg)
1946 char buffer1[64] = WINPR_C_ARRAY_INIT;
1947 char buffer2[64] = WINPR_C_ARRAY_INIT;
1948 WLog_Print(license->log, WLOG_WARN,
"%s::PreferredKeyExchangeAlg, expected %s, got %s",
1950 license_preferred_key_exchange_alg_string(license->PreferredKeyExchangeAlg,
1951 buffer1,
sizeof(buffer1)),
1952 license_preferred_key_exchange_alg_string(PreferredKeyExchangeAlg, buffer2,
1959BOOL license_read_license_info(rdpLicense* license,
wStream* s)
1962 UINT32 PreferredKeyExchangeAlg = 0;
1964 WINPR_ASSERT(license);
1965 WINPR_ASSERT(license->certificate);
1967 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1972 if (!license_check_stream_length(license->log, s, 8 +
sizeof(license->ClientRandom),
1976 Stream_Read_UINT32(s, PreferredKeyExchangeAlg);
1977 if (!license_check_preferred_alg(license, PreferredKeyExchangeAlg,
"license info"))
1979 Stream_Read_UINT32(s, license->PlatformId);
1982 Stream_Read(s, license->ClientRandom,
sizeof(license->ClientRandom));
1986 UINT32 ModulusLength = 0;
1987 if (!license_read_encrypted_premaster_secret_blob(
1988 license->log, s, license->EncryptedPremasterSecret, &ModulusLength))
1991 if (ModulusLength != info->ModulusLength)
1993 WLog_Print(license->log, WLOG_WARN,
1994 "EncryptedPremasterSecret,::ModulusLength[%" PRIu32
1995 "] != rdpCertInfo::ModulusLength[%" PRIu32
"]",
1996 ModulusLength, info->ModulusLength);
2002 if (!license_read_binary_blob(license->log, s, license->LicenseInfo))
2006 if (!license_read_binary_blob(license->log, s, license->EncryptedHardwareId))
2010 if (!license_check_stream_length(license->log, s,
sizeof(license->MACData),
2011 "license info::MACData", 0))
2013 Stream_Read(s, license->MACData,
sizeof(license->MACData));
2028BOOL license_read_license_request_packet(rdpLicense* license,
wStream* s)
2030 WINPR_ASSERT(license);
2033 if (!license_check_stream_length(license->log, s,
sizeof(license->ServerRandom),
2034 "license request", 0))
2037 Stream_Read(s, license->ServerRandom,
sizeof(license->ServerRandom));
2040 if (!license_read_product_info(license->log, s, license->ProductInfo))
2044 if (!license_read_binary_blob(license->log, s, license->KeyExchangeList))
2048 if (!license_read_binary_blob(license->log, s, license->ServerCertificate))
2052 if (!license_read_scope_list(license->log, s, license->ScopeList))
2056 if (!freerdp_certificate_read_server_cert(license->certificate,
2057 license->ServerCertificate->data,
2058 license->ServerCertificate->length))
2061 if (!license_generate_keys(license) || !license_generate_hwid(license) ||
2062 !license_encrypt_premaster_secret(license))
2065#ifdef WITH_DEBUG_LICENSE
2066 WLog_Print(license->log, WLOG_DEBUG,
"ServerRandom:");
2067 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ServerRandom,
2068 sizeof(license->ServerRandom));
2069 license_print_product_info(license->log, license->ProductInfo);
2070 license_print_scope_list(license->log, license->ScopeList);
2075BOOL license_write_license_request_packet(
const rdpLicense* license,
wStream* s)
2077 WINPR_ASSERT(license);
2080 if (!license_check_stream_capacity(license->log, s,
sizeof(license->ServerRandom),
2083 Stream_Write(s, license->ServerRandom,
sizeof(license->ServerRandom));
2086 if (!license_write_product_info(license->log, s, license->ProductInfo))
2090 if (!license_write_binary_blob(s, license->KeyExchangeList))
2094 if (!license_write_binary_blob(s, license->ServerCertificate))
2098 if (!license_write_scope_list(license->log, s, license->ScopeList))
2105static BOOL license_send_license_request_packet(rdpLicense* license)
2107 UINT16 sec_flags = 0;
2108 wStream* s = license_send_stream_init(license, &sec_flags);
2112 if (!license_write_license_request_packet(license, s))
2115 return license_send(license, s, LICENSE_REQUEST, sec_flags);
2129BOOL license_read_platform_challenge_packet(rdpLicense* license,
wStream* s)
2131 BYTE macData[LICENSING_ENCRYPTION_KEY_LENGTH] = WINPR_C_ARRAY_INIT;
2133 WINPR_ASSERT(license);
2135 DEBUG_LICENSE(
"Receiving Platform Challenge Packet");
2137 if (!license_check_stream_length(license->log, s, 4,
"license platform challenge", 0))
2142 Stream_Seek_UINT32(s);
2145 license->EncryptedPlatformChallenge->type = BB_ANY_BLOB;
2146 if (!license_read_binary_blob(license->log, s, license->EncryptedPlatformChallenge))
2148 license->EncryptedPlatformChallenge->type = BB_ENCRYPTED_DATA_BLOB;
2151 if (!license_check_stream_length(license->log, s,
sizeof(macData),
2152 "license platform challenge::MAC", 0))
2155 Stream_Read(s, macData,
sizeof(macData));
2156 if (!license_decrypt_and_check_MAC(license, license->EncryptedPlatformChallenge->data,
2157 license->EncryptedPlatformChallenge->length,
2158 license->PlatformChallenge, macData))
2161 WLog_Print(license->log, WLOG_TRACE,
"platform challenge read");
2163#ifdef WITH_DEBUG_LICENSE
2164 WLog_Print(license->log, WLOG_DEBUG,
"EncryptedPlatformChallenge:");
2165 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedPlatformChallenge->data,
2166 license->EncryptedPlatformChallenge->length);
2167 WLog_Print(license->log, WLOG_DEBUG,
"PlatformChallenge:");
2168 winpr_HexLogDump(license->log, WLOG_DEBUG, license->PlatformChallenge->data,
2169 license->PlatformChallenge->length);
2170 WLog_Print(license->log, WLOG_DEBUG,
"MacData:");
2171 winpr_HexLogDump(license->log, WLOG_DEBUG, macData,
sizeof(macData));
2176BOOL license_send_error_alert(rdpLicense* license, UINT32 dwErrorCode, UINT32 dwStateTransition,
2179 UINT16 sec_flags = 0;
2180 wStream* s = license_send_stream_init(license, &sec_flags);
2185 if (!license_check_stream_capacity(license->log, s, 8,
"license error alert"))
2187 Stream_Write_UINT32(s, dwErrorCode);
2188 Stream_Write_UINT32(s, dwStateTransition);
2192 if (!license_write_binary_blob(s, info))
2196 return license_send(license, s, ERROR_ALERT, sec_flags);
2202BOOL license_send_platform_challenge_packet(rdpLicense* license)
2204 UINT16 sec_flags = 0;
2205 wStream* s = license_send_stream_init(license, &sec_flags);
2210 DEBUG_LICENSE(
"Receiving Platform Challenge Packet");
2212 if (!license_check_stream_capacity(license->log, s, 4,
"license platform challenge"))
2218 if (!license_write_binary_blob(s, license->EncryptedPlatformChallenge))
2222 if (!license_check_stream_length(license->log, s,
sizeof(license->MACData),
2223 "license platform challenge::MAC", 0))
2226 Stream_Write(s, license->MACData,
sizeof(license->MACData));
2228 return license_send(license, s, PLATFORM_CHALLENGE, sec_flags);
2235static BOOL license_read_encrypted_blob(
const rdpLicense* license,
wStream* s,
LICENSE_BLOB* target)
2237 UINT16 wBlobType = 0;
2238 UINT16 wBlobLen = 0;
2240 WINPR_ASSERT(license);
2241 WINPR_ASSERT(target);
2243 if (!license_check_stream_length(license->log, s, 4,
"license encrypted blob", 0))
2246 Stream_Read_UINT16(s, wBlobType);
2247 if (wBlobType != BB_ENCRYPTED_DATA_BLOB)
2250 license->log, WLOG_WARN,
2251 "expecting BB_ENCRYPTED_DATA_BLOB blob, probably a windows 2003 server, continuing...");
2254 Stream_Read_UINT16(s, wBlobLen);
2256 BYTE* encryptedData = Stream_Pointer(s);
2257 if (!Stream_SafeSeek(s, wBlobLen))
2259 WLog_Print(license->log, WLOG_WARN,
2260 "short license encrypted blob::length, expected %" PRIu16
" bytes, got %" PRIuz,
2261 wBlobLen, Stream_GetRemainingLength(s));
2265 return license_rc4_with_licenseKey(license, encryptedData, wBlobLen, target);
2275BOOL license_read_new_or_upgrade_license_packet(rdpLicense* license,
wStream* s)
2277 UINT32 os_major = 0;
2278 UINT32 os_minor = 0;
2280 UINT32 cbCompanyName = 0;
2281 UINT32 cbProductId = 0;
2282 UINT32 cbLicenseInfo = 0;
2283 wStream sbuffer = WINPR_C_ARRAY_INIT;
2284 wStream* licenseStream =
nullptr;
2286 BYTE computedMac[16] = WINPR_C_ARRAY_INIT;
2287 const BYTE* readMac =
nullptr;
2289 WINPR_ASSERT(license);
2291 DEBUG_LICENSE(
"Receiving Server New/Upgrade License Packet");
2293 LICENSE_BLOB* calBlob = license_new_binary_blob(BB_DATA_BLOB);
2298 if (!license_read_encrypted_blob(license, s, calBlob))
2302 readMac = Stream_Pointer(s);
2303 if (!Stream_SafeSeek(s,
sizeof(computedMac)))
2305 WLog_Print(license->log, WLOG_WARN,
2306 "short license new/upgrade, expected 16 bytes, got %" PRIuz,
2307 Stream_GetRemainingLength(s));
2311 if (!security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), calBlob->data,
2312 calBlob->length, computedMac,
sizeof(computedMac)))
2315 if (memcmp(computedMac, readMac,
sizeof(computedMac)) != 0)
2317 WLog_Print(license->log, WLOG_ERROR,
"new or upgrade license MAC mismatch");
2321 licenseStream = Stream_StaticConstInit(&sbuffer, calBlob->data, calBlob->length);
2324 WLog_Print(license->log, WLOG_ERROR,
2325 "license::blob::data=%p, license::blob::length=%" PRIu16,
2326 (
const void*)calBlob->data, calBlob->length);
2330 if (!license_check_stream_length(license->log, licenseStream, 8,
2331 "license new/upgrade::blob::version", 0))
2334 Stream_Read_UINT16(licenseStream, os_minor);
2335 Stream_Read_UINT16(licenseStream, os_major);
2337 WLog_Print(license->log, WLOG_DEBUG,
"Version: %" PRIu16
".%" PRIu16, os_major, os_minor);
2340 Stream_Read_UINT32(licenseStream, cbScope);
2341 if (!license_check_stream_length(license->log, licenseStream, cbScope,
2342 "license new/upgrade::blob::scope", LICENSE_MAX_SCOPE_LENGTH))
2345#ifdef WITH_DEBUG_LICENSE
2346 WLog_Print(license->log, WLOG_DEBUG,
"Scope:");
2347 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbScope);
2349 Stream_Seek(licenseStream, cbScope);
2352 if (!license_check_stream_length(license->log, licenseStream, 4,
2353 "license new/upgrade::blob::cbCompanyName", 0))
2356 Stream_Read_UINT32(licenseStream, cbCompanyName);
2357 if (!license_check_stream_length(license->log, licenseStream, cbCompanyName,
2358 "license new/upgrade::blob::CompanyName",
2359 LICENSE_MAX_COMPANY_NAME_LENGTH))
2362#ifdef WITH_DEBUG_LICENSE
2363 WLog_Print(license->log, WLOG_DEBUG,
"Company name:");
2364 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbCompanyName);
2366 Stream_Seek(licenseStream, cbCompanyName);
2369 if (!license_check_stream_length(license->log, licenseStream, 4,
2370 "license new/upgrade::blob::cbProductId", 0))
2373 Stream_Read_UINT32(licenseStream, cbProductId);
2375 if (!license_check_stream_length(license->log, licenseStream, cbProductId,
2376 "license new/upgrade::blob::ProductId",
2377 LICENSE_MAX_PRODUCT_ID_LENGTH))
2380#ifdef WITH_DEBUG_LICENSE
2381 WLog_Print(license->log, WLOG_DEBUG,
"Product id:");
2382 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbProductId);
2384 Stream_Seek(licenseStream, cbProductId);
2387 if (!license_check_stream_length(license->log, licenseStream, 4,
2388 "license new/upgrade::blob::cbLicenseInfo", 0))
2391 Stream_Read_UINT32(licenseStream, cbLicenseInfo);
2392 if (!license_check_stream_length(license->log, licenseStream, cbLicenseInfo,
2393 "license new/upgrade::blob::LicenseInfo",
2394 LICENSE_MAX_CAL_LENGTH))
2397 license->type = LICENSE_TYPE_ISSUED;
2398 license_set_state(license, LICENSE_STATE_COMPLETED);
2401 if (!license->rdp->settings->OldLicenseBehaviour)
2402 ret = saveCal(license->log, license->rdp->settings, Stream_Pointer(licenseStream),
2403 cbLicenseInfo, license->rdp->settings->ClientHostname);
2406 license_free_binary_blob(calBlob);
2417BOOL license_read_error_alert_packet(rdpLicense* license,
wStream* s)
2419 UINT32 dwErrorCode = 0;
2420 UINT32 dwStateTransition = 0;
2422 WINPR_ASSERT(license);
2423 WINPR_ASSERT(license->rdp);
2425 if (!license_check_stream_length(license->log, s, 8ul,
"error alert", 0))
2428 Stream_Read_UINT32(s, dwErrorCode);
2429 Stream_Read_UINT32(s, dwStateTransition);
2431 if (!license_read_binary_blob(license->log, s, license->ErrorInfo))
2434#ifdef WITH_DEBUG_LICENSE
2435 WLog_Print(license->log, WLOG_DEBUG,
"dwErrorCode: %s, dwStateTransition: %s",
2436 error_codes(dwErrorCode), state_transitions(dwStateTransition));
2439 if (dwErrorCode == STATUS_VALID_CLIENT)
2441 license->type = LICENSE_TYPE_NONE;
2442 license_set_state(license, LICENSE_STATE_COMPLETED);
2446 switch (dwStateTransition)
2448 case ST_TOTAL_ABORT:
2449 license_set_state(license, LICENSE_STATE_ABORTED);
2451 case ST_NO_TRANSITION:
2452 license_set_state(license, LICENSE_STATE_COMPLETED);
2454 case ST_RESET_PHASE_TO_START:
2455 license_set_state(license, LICENSE_STATE_CONFIGURED);
2457 case ST_RESEND_LAST_MESSAGE:
2473BOOL license_write_new_license_request_packet(
const rdpLicense* license,
wStream* s)
2475 WINPR_ASSERT(license);
2477 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
2481 if (!license_check_stream_capacity(license->log, s, 8 +
sizeof(license->ClientRandom),
2485 Stream_Write_UINT32(s,
2486 license->PreferredKeyExchangeAlg);
2487 Stream_Write_UINT32(s, license->PlatformId);
2488 Stream_Write(s, license->ClientRandom,
2489 sizeof(license->ClientRandom));
2492 !license_write_encrypted_premaster_secret_blob(
2493 license->log, s, license->EncryptedPremasterSecret, info->ModulusLength) ||
2495 !license_write_binary_blob(s, license->ClientUserName) ||
2497 !license_write_binary_blob(s, license->ClientMachineName))
2502 WLog_Print(license->log, WLOG_TRACE,
"new license written");
2504#ifdef WITH_DEBUG_LICENSE
2505 WLog_Print(license->log, WLOG_DEBUG,
"PreferredKeyExchangeAlg: 0x%08" PRIX32
"",
2506 license->PreferredKeyExchangeAlg);
2507 WLog_Print(license->log, WLOG_DEBUG,
"ClientRandom:");
2508 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ClientRandom,
2509 sizeof(license->ClientRandom));
2510 WLog_Print(license->log, WLOG_DEBUG,
"EncryptedPremasterSecret");
2511 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedPremasterSecret->data,
2512 license->EncryptedPremasterSecret->length);
2513 WLog_Print(license->log, WLOG_DEBUG,
"ClientUserName (%" PRIu16
"): %s",
2514 license->ClientUserName->length, (
char*)license->ClientUserName->data);
2515 WLog_Print(license->log, WLOG_DEBUG,
"ClientMachineName (%" PRIu16
"): %s",
2516 license->ClientMachineName->length, (
char*)license->ClientMachineName->data);
2521BOOL license_read_new_license_request_packet(rdpLicense* license,
wStream* s)
2523 UINT32 PreferredKeyExchangeAlg = 0;
2525 WINPR_ASSERT(license);
2527 if (!license_check_stream_length(license->log, s, 8ull +
sizeof(license->ClientRandom),
2528 "new license request", 0))
2531 Stream_Read_UINT32(s, PreferredKeyExchangeAlg);
2532 if (!license_check_preferred_alg(license, PreferredKeyExchangeAlg,
"new license request"))
2535 Stream_Read_UINT32(s, license->PlatformId);
2536 Stream_Read(s, license->ClientRandom,
2537 sizeof(license->ClientRandom));
2540 UINT32 ModulusLength = 0;
2541 if (!license_read_encrypted_premaster_secret_blob(
2542 license->log, s, license->EncryptedPremasterSecret, &ModulusLength))
2545 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
2547 WLog_Print(license->log, WLOG_WARN,
2548 "Missing license certificate, skipping ModulusLength checks");
2549 else if (ModulusLength != info->ModulusLength)
2551 WLog_Print(license->log, WLOG_WARN,
2552 "EncryptedPremasterSecret expected to be %" PRIu32
" bytes, but read %" PRIu32
2554 info->ModulusLength, ModulusLength);
2559 if (!license_read_binary_blob(license->log, s, license->ClientUserName))
2562 if (!license_read_binary_blob(license->log, s, license->ClientMachineName))
2574BOOL license_answer_license_request(rdpLicense* license)
2576 UINT16 sec_flags = 0;
2578 BYTE* license_data =
nullptr;
2579 size_t license_size = 0;
2581 char* username =
nullptr;
2583 WINPR_ASSERT(license);
2584 WINPR_ASSERT(license->rdp);
2585 WINPR_ASSERT(license->rdp->settings);
2587 if (!license->rdp->settings->OldLicenseBehaviour)
2588 license_data = loadCalFile(license->log, license->rdp->settings,
2589 license->rdp->settings->ClientHostname, &license_size);
2594 BYTE signature[LICENSING_ENCRYPTION_KEY_LENGTH] = WINPR_C_ARRAY_INIT;
2596 DEBUG_LICENSE(
"Sending Saved License Packet");
2598 WINPR_ASSERT(license->EncryptedHardwareId);
2599 license->EncryptedHardwareId->type = BB_ENCRYPTED_DATA_BLOB;
2600 if (!license_encrypt_and_MAC(license, license->HardwareId,
sizeof(license->HardwareId),
2601 license->EncryptedHardwareId, signature,
sizeof(signature)))
2603 winpr_znfree(license_data, license_size);
2607 calBlob = license_new_binary_blob(BB_DATA_BLOB);
2610 winpr_znfree(license_data, license_size);
2613 calBlob->data = license_data;
2614 WINPR_ASSERT(license_size <= UINT16_MAX);
2615 calBlob->length = (UINT16)license_size;
2617 status = license_send_license_info(license, calBlob, signature,
sizeof(signature));
2618 license_free_binary_blob(calBlob);
2623 DEBUG_LICENSE(
"Sending New License Packet");
2625 s = license_send_stream_init(license, &sec_flags);
2628 if (license->rdp->settings->Username !=
nullptr)
2629 username = license->rdp->settings->Username;
2631 username =
"username";
2634 WINPR_ASSERT(license->ClientUserName);
2635 const size_t len = strlen(username) + 1;
2636 WINPR_ASSERT(len <= UINT16_MAX);
2638 license->ClientUserName->data = (BYTE*)username;
2639 license->ClientUserName->length = (UINT16)len;
2643 WINPR_ASSERT(license->ClientMachineName);
2644 const size_t len = strlen(license->rdp->settings->ClientHostname) + 1;
2645 WINPR_ASSERT(len <= UINT16_MAX);
2647 license->ClientMachineName->data = (BYTE*)license->rdp->settings->ClientHostname;
2648 license->ClientMachineName->length = (UINT16)len;
2650 status = license_write_new_license_request_packet(license, s);
2652 WINPR_ASSERT(license->ClientUserName);
2653 license->ClientUserName->data =
nullptr;
2654 license->ClientUserName->length = 0;
2656 WINPR_ASSERT(license->ClientMachineName);
2657 license->ClientMachineName->data =
nullptr;
2658 license->ClientMachineName->length = 0;
2666 return license_send(license, s, NEW_LICENSE_REQUEST, sec_flags);
2675BOOL license_send_platform_challenge_response(rdpLicense* license)
2677 wStream* challengeRespData =
nullptr;
2678 BYTE* buffer =
nullptr;
2681 WINPR_ASSERT(license);
2682 WINPR_ASSERT(license->PlatformChallenge);
2683 WINPR_ASSERT(license->MacSaltKey);
2684 WINPR_ASSERT(license->EncryptedPlatformChallenge);
2685 WINPR_ASSERT(license->EncryptedHardwareId);
2687 DEBUG_LICENSE(
"Sending Platform Challenge Response Packet");
2689 license->EncryptedPlatformChallenge->type = BB_DATA_BLOB;
2692 challengeRespData = Stream_New(
nullptr, 8 + license->PlatformChallenge->length);
2693 if (!challengeRespData)
2695 Stream_Write_UINT16(challengeRespData, PLATFORM_CHALLENGE_RESPONSE_VERSION);
2696 Stream_Write_UINT16(challengeRespData, license->ClientType);
2697 Stream_Write_UINT16(challengeRespData, license->LicenseDetailLevel);
2698 Stream_Write_UINT16(challengeRespData, license->PlatformChallenge->length);
2699 Stream_Write(challengeRespData, license->PlatformChallenge->data,
2700 license->PlatformChallenge->length);
2701 Stream_SealLength(challengeRespData);
2704 const size_t length = Stream_Length(challengeRespData) +
sizeof(license->HardwareId);
2705 buffer = (BYTE*)malloc(length);
2708 Stream_Free(challengeRespData, TRUE);
2712 CopyMemory(buffer, Stream_Buffer(challengeRespData), Stream_Length(challengeRespData));
2713 CopyMemory(&buffer[Stream_Length(challengeRespData)], license->HardwareId,
2714 sizeof(license->HardwareId));
2715 status = security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), buffer, length,
2716 license->MACData,
sizeof(license->MACData));
2717 winpr_znfree(buffer, length);
2721 Stream_Free(challengeRespData, TRUE);
2725 license->EncryptedHardwareId->type = BB_ENCRYPTED_DATA_BLOB;
2726 if (!license_rc4_with_licenseKey(license, license->HardwareId,
sizeof(license->HardwareId),
2727 license->EncryptedHardwareId))
2729 Stream_Free(challengeRespData, TRUE);
2733 status = license_rc4_with_licenseKey(license, Stream_Buffer(challengeRespData),
2734 Stream_Length(challengeRespData),
2735 license->EncryptedPlatformChallengeResponse);
2736 Stream_Free(challengeRespData, TRUE);
2740#ifdef WITH_DEBUG_LICENSE
2741 WLog_Print(license->log, WLOG_DEBUG,
"LicensingEncryptionKey:");
2742 winpr_HexLogDump(license->log, WLOG_DEBUG, license->LicensingEncryptionKey, 16);
2743 WLog_Print(license->log, WLOG_DEBUG,
"HardwareId:");
2744 winpr_HexLogDump(license->log, WLOG_DEBUG, license->HardwareId,
sizeof(license->HardwareId));
2745 WLog_Print(license->log, WLOG_DEBUG,
"EncryptedHardwareId:");
2746 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedHardwareId->data,
2747 license->EncryptedHardwareId->length);
2749 UINT16 sec_flags = 0;
2750 wStream* s = license_send_stream_init(license, &sec_flags);
2754 if (license_write_client_platform_challenge_response(license, s))
2755 return license_send(license, s, PLATFORM_CHALLENGE_RESPONSE, sec_flags);
2761BOOL license_read_platform_challenge_response(WINPR_ATTR_UNUSED rdpLicense* license)
2763 WINPR_ASSERT(license);
2764 WINPR_ASSERT(license->PlatformChallenge);
2765 WINPR_ASSERT(license->MacSaltKey);
2766 WINPR_ASSERT(license->EncryptedPlatformChallenge);
2767 WINPR_ASSERT(license->EncryptedHardwareId);
2769 DEBUG_LICENSE(
"Receiving Platform Challenge Response Packet");
2771#if defined(WITH_LICENSE_DECRYPT_CHALLENGE_RESPONSE)
2773 LICENSE_BLOB* dblob = license_new_binary_blob(BB_ANY_BLOB);
2777 wStream sbuffer = WINPR_C_ARRAY_INIT;
2778 UINT16 wVersion = 0;
2779 UINT16 cbChallenge = 0;
2780 const BYTE* pbChallenge =
nullptr;
2781 LICENSE_BLOB* blob = license->EncryptedPlatformChallengeResponse;
2783 if (!license_rc4_with_licenseKey(license, blob->data, blob->length, dblob))
2786 wStream* s = Stream_StaticConstInit(&sbuffer, dblob->data, dblob->length);
2787 if (!license_check_stream_length(s, 8,
"PLATFORM_CHALLENGE_RESPONSE_DATA", 0))
2790 Stream_Read_UINT16(s, wVersion);
2791 if (wVersion != PLATFORM_CHALLENGE_RESPONSE_VERSION)
2793 WLog_Print(license->log, WLOG_WARN,
2794 "Invalid PLATFORM_CHALLENGE_RESPONSE_DATA::wVersion 0x%04" PRIx16
2795 ", expected 0x04" PRIx16,
2796 wVersion, PLATFORM_CHALLENGE_RESPONSE_VERSION);
2799 Stream_Read_UINT16(s, license->ClientType);
2800 Stream_Read_UINT16(s, license->LicenseDetailLevel);
2801 Stream_Read_UINT16(s, cbChallenge);
2803 if (!license_check_stream_length(s, cbChallenge,
2804 "PLATFORM_CHALLENGE_RESPONSE_DATA::pbChallenge", 0))
2807 pbChallenge = Stream_Pointer(s);
2808 if (!license_read_binary_blob_data(license->PlatformChallengeResponse, BB_DATA_BLOB,
2809 pbChallenge, cbChallenge))
2811 if (!Stream_SafeSeek(s, cbChallenge))
2816 license_free_binary_blob(dblob);
2823BOOL license_write_client_platform_challenge_response(rdpLicense* license,
wStream* s)
2825 WINPR_ASSERT(license);
2827 if (!license_write_binary_blob(s, license->EncryptedPlatformChallengeResponse))
2829 if (!license_write_binary_blob(s, license->EncryptedHardwareId))
2831 if (!license_check_stream_capacity(license->log, s,
sizeof(license->MACData),
2832 "CLIENT_PLATFORM_CHALLENGE_RESPONSE::MACData"))
2834 Stream_Write(s, license->MACData,
sizeof(license->MACData));
2838BOOL license_read_client_platform_challenge_response(rdpLicense* license,
wStream* s)
2840 WINPR_ASSERT(license);
2842 if (!license_read_binary_blob(license->log, s, license->EncryptedPlatformChallengeResponse))
2844 if (!license_read_binary_blob(license->log, s, license->EncryptedHardwareId))
2846 if (!license_check_stream_length(license->log, s,
sizeof(license->MACData),
2847 "CLIENT_PLATFORM_CHALLENGE_RESPONSE::MACData", 0))
2849 Stream_Read(s, license->MACData,
sizeof(license->MACData));
2850 return license_read_platform_challenge_response(license);
2862BOOL license_send_valid_client_error_packet(rdpRdp* rdp)
2865 rdpLicense* license = rdp->license;
2866 WINPR_ASSERT(license);
2868 license->state = LICENSE_STATE_COMPLETED;
2869 license->type = LICENSE_TYPE_NONE;
2870 return license_send_error_alert(license, STATUS_VALID_CLIENT, ST_NO_TRANSITION,
2871 license->ErrorInfo);
2880rdpLicense* license_new(rdpRdp* rdp)
2884 rdpLicense* license = (rdpLicense*)calloc(1,
sizeof(rdpLicense));
2887 license->log = WLog_Get(LICENSE_TAG);
2888 WINPR_ASSERT(license->log);
2890 license->PlatformId = PLATFORMID;
2891 license->ClientType = OTHER_PLATFORM_CHALLENGE_TYPE;
2892 license->LicenseDetailLevel = LICENSE_DETAIL_DETAIL;
2893 license->PreferredKeyExchangeAlg = KEY_EXCHANGE_ALG_RSA;
2896 license_set_state(license, LICENSE_STATE_INITIAL);
2897 if (!(license->certificate = freerdp_certificate_new()))
2899 if (!(license->ProductInfo = license_new_product_info()))
2901 if (!(license->ErrorInfo = license_new_binary_blob(BB_ERROR_BLOB)))
2903 if (!(license->LicenseInfo = license_new_binary_blob(BB_DATA_BLOB)))
2905 if (!(license->KeyExchangeList = license_new_binary_blob(BB_KEY_EXCHG_ALG_BLOB)))
2907 if (!(license->ServerCertificate = license_new_binary_blob(BB_CERTIFICATE_BLOB)))
2909 if (!(license->ClientUserName = license_new_binary_blob(BB_CLIENT_USER_NAME_BLOB)))
2911 if (!(license->ClientMachineName = license_new_binary_blob(BB_CLIENT_MACHINE_NAME_BLOB)))
2913 if (!(license->PlatformChallenge = license_new_binary_blob(BB_ANY_BLOB)))
2915 if (!(license->PlatformChallengeResponse = license_new_binary_blob(BB_ANY_BLOB)))
2917 if (!(license->EncryptedPlatformChallenge = license_new_binary_blob(BB_ANY_BLOB)))
2919 if (!(license->EncryptedPlatformChallengeResponse =
2920 license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2922 if (!(license->EncryptedPremasterSecret = license_new_binary_blob(BB_ANY_BLOB)))
2924 if (!(license->EncryptedHardwareId = license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2926 if (!(license->EncryptedLicenseInfo = license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2928 if (!(license->ScopeList = license_new_scope_list()))
2931 if (!license_generate_randoms(license))
2937 WINPR_PRAGMA_DIAG_PUSH
2938 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2939 license_free(license);
2940 WINPR_PRAGMA_DIAG_POP
2949void license_free(rdpLicense* license)
2953 freerdp_certificate_free(license->certificate);
2954 license_free_product_info(license->ProductInfo);
2955 license_free_binary_blob(license->ErrorInfo);
2956 license_free_binary_blob(license->LicenseInfo);
2957 license_free_binary_blob(license->KeyExchangeList);
2958 license_free_binary_blob(license->ServerCertificate);
2959 license_free_binary_blob(license->ClientUserName);
2960 license_free_binary_blob(license->ClientMachineName);
2961 license_free_binary_blob(license->PlatformChallenge);
2962 license_free_binary_blob(license->PlatformChallengeResponse);
2963 license_free_binary_blob(license->EncryptedPlatformChallenge);
2964 license_free_binary_blob(license->EncryptedPlatformChallengeResponse);
2965 license_free_binary_blob(license->EncryptedPremasterSecret);
2966 license_free_binary_blob(license->EncryptedHardwareId);
2967 license_free_binary_blob(license->EncryptedLicenseInfo);
2968 license_free_scope_list(license->ScopeList);
2969 winpr_znfree(license,
sizeof(rdpLicense));
2973LICENSE_STATE license_get_state(
const rdpLicense* license)
2975 WINPR_ASSERT(license);
2976 return license->state;
2979LICENSE_TYPE license_get_type(
const rdpLicense* license)
2981 WINPR_ASSERT(license);
2982 return license->type;
2985void license_set_state(rdpLicense* license, LICENSE_STATE state)
2987 WINPR_ASSERT(license);
2988 license->state = state;
2991 case LICENSE_STATE_COMPLETED:
2993 case LICENSE_STATE_ABORTED:
2995 license->type = LICENSE_TYPE_INVALID;
3000const char* license_get_state_string(LICENSE_STATE state)
3004 case LICENSE_STATE_INITIAL:
3005 return "LICENSE_STATE_INITIAL";
3006 case LICENSE_STATE_CONFIGURED:
3007 return "LICENSE_STATE_CONFIGURED";
3008 case LICENSE_STATE_REQUEST:
3009 return "LICENSE_STATE_REQUEST";
3010 case LICENSE_STATE_NEW_REQUEST:
3011 return "LICENSE_STATE_NEW_REQUEST";
3012 case LICENSE_STATE_PLATFORM_CHALLENGE:
3013 return "LICENSE_STATE_PLATFORM_CHALLENGE";
3014 case LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE:
3015 return "LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE";
3016 case LICENSE_STATE_COMPLETED:
3017 return "LICENSE_STATE_COMPLETED";
3018 case LICENSE_STATE_ABORTED:
3019 return "LICENSE_STATE_ABORTED";
3021 return "LICENSE_STATE_UNKNOWN";
3025BOOL license_server_send_request(rdpLicense* license)
3027 if (!license_ensure_state(license, LICENSE_STATE_CONFIGURED, LICENSE_REQUEST))
3029 if (!license_send_license_request_packet(license))
3031 license_set_state(license, LICENSE_STATE_REQUEST);
3036static BOOL license_set_string(wLog* log,
const char* what,
const char* value, BYTE** bdst,
3040 WINPR_ASSERT(value);
3042 WINPR_ASSERT(dstLen);
3052 *cnv.w = ConvertUtf8ToWCharAlloc(value, &len);
3053 if (!*cnv.w || (len > UINT32_MAX /
sizeof(WCHAR)))
3055 WLog_Print(log, WLOG_ERROR,
"license->ProductInfo: %s == %p || %" PRIuz
" > UINT32_MAX",
3056 what, (
void*)(*cnv.w), len);
3059 *dstLen = (UINT32)(len *
sizeof(WCHAR));
3063BOOL license_server_configure(rdpLicense* license)
3066 UINT32 algs[] = { KEY_EXCHANGE_ALG_RSA };
3068 WINPR_ASSERT(license);
3069 WINPR_ASSERT(license->rdp);
3071 const rdpSettings* settings = license->rdp->settings;
3073 const char* CompanyName =
3076 const char* ProductName =
3078 const UINT32 ProductVersion =
3080 const UINT32 issuerCount =
3084 const char** issuers = WINPR_REINTERPRET_CAST(ptr,
const void*,
const char**);
3086 WINPR_ASSERT(CompanyName);
3087 WINPR_ASSERT(ProductName);
3088 WINPR_ASSERT(ProductVersion > 0);
3089 WINPR_ASSERT(issuers || (issuerCount == 0));
3091 if (!license_ensure_state(license, LICENSE_STATE_INITIAL, LICENSE_REQUEST))
3094 license->ProductInfo->dwVersion = ProductVersion;
3095 if (!license_set_string(license->log,
"pbCompanyName", CompanyName,
3096 &license->ProductInfo->pbCompanyName,
3097 &license->ProductInfo->cbCompanyName))
3100 if (!license_set_string(license->log,
"pbProductId", ProductName,
3101 &license->ProductInfo->pbProductId, &license->ProductInfo->cbProductId))
3104 if (!license_read_binary_blob_data(license->log, license->KeyExchangeList,
3105 BB_KEY_EXCHG_ALG_BLOB, algs,
sizeof(algs)))
3108 if (!freerdp_certificate_read_server_cert(license->certificate, settings->ServerCertificate,
3109 settings->ServerCertificateLength))
3112 s = Stream_New(
nullptr, 1024);
3119 freerdp_certificate_write_server_cert(license->certificate, CERT_CHAIN_VERSION_2, s);
3121 r = license_read_binary_blob_data(license->log, license->ServerCertificate,
3122 BB_CERTIFICATE_BLOB, Stream_Buffer(s),
3123 Stream_GetPosition(s));
3125 Stream_Free(s, TRUE);
3130 if (!license_scope_list_resize(license->ScopeList, issuerCount))
3132 for (
size_t x = 0; x < issuerCount; x++)
3135 const char* name = issuers[x];
3136 const size_t length = strnlen(name, UINT16_MAX) + 1;
3137 if ((length == 0) || (length > UINT16_MAX))
3139 WLog_Print(license->log, WLOG_WARN,
3140 "Invalid issuer at position %" PRIuz
": length 0 < %" PRIuz
" <= %d"
3142 x, length, UINT16_MAX, name);
3145 if (!license_read_binary_blob_data(license->log, blob, BB_SCOPE_BLOB, name, length))
3149 license_set_state(license, LICENSE_STATE_CONFIGURED);
3153rdpLicense* license_get(rdpContext* context)
3155 WINPR_ASSERT(context);
3156 WINPR_ASSERT(context->rdp);
3157 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.