24 #include <freerdp/config.h>
28 #include <freerdp/license.h>
30 #include <winpr/crt.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 TAG FREERDP_TAG("core.license")
49 #define LICENSE_NULL_CLIENT_RANDOM 1
50 #define LICENSE_NULL_PREMASTER_SECRET 1
53 #define PLATFORM_CHALLENGE_RESPONSE_VERSION 0x0100
56 enum LicenseRequestType
58 LICENSE_REQUEST = 0x01,
59 PLATFORM_CHALLENGE = 0x02,
61 UPGRADE_LICENSE = 0x04,
63 NEW_LICENSE_REQUEST = 0x13,
64 PLATFORM_CHALLENGE_RESPONSE = 0x15,
74 #define LICENSE_PREAMBLE_LENGTH 4
78 #define SERVER_RANDOM_LENGTH 32
79 #define MASTER_SECRET_LENGTH 48
80 #define PREMASTER_SECRET_LENGTH 48
81 #define SESSION_KEY_BLOB_LENGTH 48
82 #define MAC_SALT_KEY_LENGTH 16
83 #define LICENSING_ENCRYPTION_KEY_LENGTH 16
84 #define HWID_PLATFORM_ID_LENGTH 4
86 #define HWID_LENGTH 20
92 #define PREAMBLE_VERSION_3_0 0x03
94 #define EXTENDED_ERROR_MSG_SUPPORTED 0x80
100 BB_DATA_BLOB = 0x0001,
101 BB_RANDOM_BLOB = 0x0002,
102 BB_CERTIFICATE_BLOB = 0x0003,
103 BB_ERROR_BLOB = 0x0004,
104 BB_ENCRYPTED_DATA_BLOB = 0x0009,
105 BB_KEY_EXCHG_ALG_BLOB = 0x000D,
106 BB_SCOPE_BLOB = 0x000E,
107 BB_CLIENT_USER_NAME_BLOB = 0x000F,
108 BB_CLIENT_MACHINE_NAME_BLOB = 0x0010
113 #define KEY_EXCHANGE_ALG_RSA 0x00000001
119 ERR_INVALID_SERVER_CERTIFICATE = 0x00000001,
120 ERR_NO_LICENSE = 0x00000002,
121 ERR_INVALID_MAC = 0x00000003,
122 ERR_INVALID_SCOPE = 0x00000004,
123 ERR_NO_LICENSE_SERVER = 0x00000006,
124 STATUS_VALID_CLIENT = 0x00000007,
125 ERR_INVALID_CLIENT = 0x00000008,
126 ERR_INVALID_PRODUCT_ID = 0x0000000B,
127 ERR_INVALID_MESSAGE_LENGTH = 0x0000000C
134 ST_TOTAL_ABORT = 0x00000001,
135 ST_NO_TRANSITION = 0x00000002,
136 ST_RESET_PHASE_TO_START = 0x00000003,
137 ST_RESEND_LAST_MESSAGE = 0x00000004
144 WIN32_PLATFORM_CHALLENGE_TYPE = 0x0100,
145 WIN16_PLATFORM_CHALLENGE_TYPE = 0x0200,
146 WINCE_PLATFORM_CHALLENGE_TYPE = 0x0300,
147 OTHER_PLATFORM_CHALLENGE_TYPE = 0xFF00
154 LICENSE_DETAIL_SIMPLE = 0x0001,
155 LICENSE_DETAIL_MODERATE = 0x0002,
156 LICENSE_DETAIL_DETAIL = 0x0003
174 CLIENT_OS_ID_WINNT_351 = 0x01000000,
175 CLIENT_OS_ID_WINNT_40 = 0x02000000,
176 CLIENT_OS_ID_WINNT_50 = 0x03000000,
177 CLIENT_OS_ID_WINNT_POST_52 = 0x04000000,
179 CLIENT_IMAGE_ID_MICROSOFT = 0x00010000,
180 CLIENT_IMAGE_ID_CITRIX = 0x00020000,
188 rdpCertificate* certificate;
189 BYTE HardwareId[HWID_LENGTH];
190 BYTE ClientRandom[CLIENT_RANDOM_LENGTH];
191 BYTE ServerRandom[SERVER_RANDOM_LENGTH];
192 BYTE MasterSecret[MASTER_SECRET_LENGTH];
193 BYTE PremasterSecret[PREMASTER_SECRET_LENGTH];
194 BYTE SessionKeyBlob[SESSION_KEY_BLOB_LENGTH];
195 BYTE MacSaltKey[MAC_SALT_KEY_LENGTH];
196 BYTE LicensingEncryptionKey[LICENSING_ENCRYPTION_KEY_LENGTH];
210 BYTE MACData[LICENSING_ENCRYPTION_KEY_LENGTH];
212 UINT32 PacketHeaderLength;
213 UINT32 PreferredKeyExchangeAlg;
216 UINT16 LicenseDetailLevel;
220 static BOOL license_send_error_alert(rdpLicense* license, UINT32 dwErrorCode,
222 static BOOL license_set_state(rdpLicense* license, LICENSE_STATE state);
223 static const char* license_get_state_string(LICENSE_STATE state);
225 static const char* license_preferred_key_exchange_alg_string(UINT32 alg,
char* buffer,
size_t size)
227 const char* name = NULL;
231 case KEY_EXCHANGE_ALG_RSA:
232 name =
"KEY_EXCHANGE_ALG_RSA";
235 name =
"KEY_EXCHANGE_ALG_UNKNOWN";
239 (void)_snprintf(buffer, size,
"%s [0x%08" PRIx32
"]", name, alg);
243 static const char* license_request_type_string(UINT32 type)
247 case LICENSE_REQUEST:
248 return "LICENSE_REQUEST";
249 case PLATFORM_CHALLENGE:
250 return "PLATFORM_CHALLENGE";
252 return "NEW_LICENSE";
253 case UPGRADE_LICENSE:
254 return "UPGRADE_LICENSE";
256 return "LICENSE_INFO";
257 case NEW_LICENSE_REQUEST:
258 return "NEW_LICENSE_REQUEST";
259 case PLATFORM_CHALLENGE_RESPONSE:
260 return "PLATFORM_CHALLENGE_RESPONSE";
262 return "ERROR_ALERT";
264 return "LICENSE_REQUEST_TYPE_UNKNOWN";
268 static const char* licencse_blob_type_string(UINT16 type)
273 return "BB_ANY_BLOB";
275 return "BB_DATA_BLOB";
277 return "BB_RANDOM_BLOB";
278 case BB_CERTIFICATE_BLOB:
279 return "BB_CERTIFICATE_BLOB";
281 return "BB_ERROR_BLOB";
282 case BB_ENCRYPTED_DATA_BLOB:
283 return "BB_ENCRYPTED_DATA_BLOB";
284 case BB_KEY_EXCHG_ALG_BLOB:
285 return "BB_KEY_EXCHG_ALG_BLOB";
287 return "BB_SCOPE_BLOB";
288 case BB_CLIENT_USER_NAME_BLOB:
289 return "BB_CLIENT_USER_NAME_BLOB";
290 case BB_CLIENT_MACHINE_NAME_BLOB:
291 return "BB_CLIENT_MACHINE_NAME_BLOB";
296 static wStream* license_send_stream_init(rdpLicense* license);
298 static void license_generate_randoms(rdpLicense* license);
299 static BOOL license_generate_keys(rdpLicense* license);
300 static BOOL license_generate_hwid(rdpLicense* license);
301 static BOOL license_encrypt_premaster_secret(rdpLicense* license);
307 static LICENSE_BLOB* license_new_binary_blob(UINT16 type);
308 static void license_free_binary_blob(
LICENSE_BLOB* blob);
309 static BOOL license_read_binary_blob_data(
LICENSE_BLOB* blob, UINT16 type,
const void* data,
314 static SCOPE_LIST* license_new_scope_list(
void);
315 static BOOL license_scope_list_resize(
SCOPE_LIST* scopeList, UINT32 count);
316 static void license_free_scope_list(
SCOPE_LIST* scopeList);
320 static BOOL license_read_license_request_packet(rdpLicense* license,
wStream* s);
321 static BOOL license_write_license_request_packet(
const rdpLicense* license,
wStream* s);
323 static BOOL license_read_platform_challenge_packet(rdpLicense* license,
wStream* s);
324 static BOOL license_send_platform_challenge_packet(rdpLicense* license);
325 static BOOL license_read_new_or_upgrade_license_packet(rdpLicense* license,
wStream* s);
326 static BOOL license_read_error_alert_packet(rdpLicense* license,
wStream* s);
328 static BOOL license_write_new_license_request_packet(
const rdpLicense* license,
wStream* s);
329 static BOOL license_read_new_license_request_packet(rdpLicense* license,
wStream* s);
330 static BOOL license_answer_license_request(rdpLicense* license);
332 static BOOL license_send_platform_challenge_response(rdpLicense* license);
333 static BOOL license_read_platform_challenge_response(rdpLicense* license,
wStream* s);
335 static BOOL license_read_client_platform_challenge_response(rdpLicense* license,
wStream* s);
336 static BOOL license_write_client_platform_challenge_response(rdpLicense* license,
wStream* s);
338 static BOOL license_read_server_upgrade_license(rdpLicense* license,
wStream* s);
339 static BOOL license_write_server_upgrade_license(
const rdpLicense* license,
wStream* s);
341 static BOOL license_send_license_info(rdpLicense* license,
const LICENSE_BLOB* calBlob,
342 const BYTE* signature,
size_t signature_length);
343 static BOOL license_read_license_info(rdpLicense* license,
wStream* s);
344 static state_run_t license_client_recv(rdpLicense* license,
wStream* s);
345 static state_run_t license_server_recv(rdpLicense* license,
wStream* s);
347 #define PLATFORMID (CLIENT_OS_ID_WINNT_POST_52 | CLIENT_IMAGE_ID_MICROSOFT)
349 #ifdef WITH_DEBUG_LICENSE
351 static const char* error_codes[] = {
"ERR_UNKNOWN",
352 "ERR_INVALID_SERVER_CERTIFICATE",
357 "ERR_NO_LICENSE_SERVER",
358 "STATUS_VALID_CLIENT",
359 "ERR_INVALID_CLIENT",
362 "ERR_INVALID_PRODUCT_ID",
363 "ERR_INVALID_MESSAGE_LENGTH" };
365 static const char* state_transitions[] = {
"ST_UNKNOWN",
"ST_TOTAL_ABORT",
"ST_NO_TRANSITION",
366 "ST_RESET_PHASE_TO_START",
"ST_RESEND_LAST_MESSAGE" };
370 char* CompanyName = NULL;
371 char* ProductId = NULL;
373 WINPR_ASSERT(productInfo);
374 WINPR_ASSERT(productInfo->pbCompanyName);
375 WINPR_ASSERT(productInfo->pbProductId);
377 CompanyName = ConvertWCharNToUtf8Alloc((
const WCHAR*)productInfo->pbCompanyName,
378 productInfo->cbCompanyName /
sizeof(WCHAR), NULL);
379 ProductId = ConvertWCharNToUtf8Alloc((
const WCHAR*)productInfo->pbProductId,
380 productInfo->cbProductId /
sizeof(WCHAR), NULL);
381 WLog_INFO(TAG,
"ProductInfo:");
382 WLog_INFO(TAG,
"\tdwVersion: 0x%08" PRIX32
"", productInfo->dwVersion);
383 WLog_INFO(TAG,
"\tCompanyName: %s", CompanyName);
384 WLog_INFO(TAG,
"\tProductId: %s", ProductId);
389 static void license_print_scope_list(
const SCOPE_LIST* scopeList)
391 WINPR_ASSERT(scopeList);
393 WLog_INFO(TAG,
"ScopeList (%" PRIu32
"):", scopeList->count);
395 for (UINT32 index = 0; index < scopeList->count; index++)
399 WINPR_ASSERT(scopeList->array);
400 scope = scopeList->array[index];
403 WLog_INFO(TAG,
"\t%s", (
const char*)scope->data);
408 static const char licenseStore[] =
"licenses";
410 static BOOL license_ensure_state(rdpLicense* license, LICENSE_STATE state, UINT32 msg)
412 const LICENSE_STATE cstate = license_get_state(license);
414 WINPR_ASSERT(license);
418 const char* scstate = license_get_state_string(cstate);
419 const char* sstate = license_get_state_string(state);
420 const char* where = license_request_type_string(msg);
422 WLog_WARN(TAG,
"Received [%s], but found invalid licensing state %s, expected %s", where,
429 state_run_t license_recv(rdpLicense* license,
wStream* s)
431 WINPR_ASSERT(license);
432 WINPR_ASSERT(license->rdp);
433 WINPR_ASSERT(license->rdp->settings);
436 return license_server_recv(license, s);
438 return license_client_recv(license, s);
441 static BOOL license_check_stream_length(
wStream* s, SSIZE_T expect,
const char* where)
443 const size_t remain = Stream_GetRemainingLength(s);
449 WLog_WARN(TAG,
"invalid %s, expected value %" PRIdz
" invalid", where, expect);
452 if (remain < (
size_t)expect)
454 WLog_WARN(TAG,
"short %s, expected %" PRIdz
" bytes, got %" PRIuz, where, expect, remain);
460 static BOOL license_check_stream_capacity(
wStream* s,
size_t expect,
const char* where)
464 if (!Stream_CheckAndLogRequiredCapacityEx(TAG, WLOG_WARN, s, expect, 1,
"%s(%s:%" PRIuz
") %s",
465 __func__, __FILE__, (
size_t)__LINE__, where))
471 static BOOL computeCalHash(
const char* hostname,
char* hashStr,
size_t len)
473 WINPR_DIGEST_CTX* sha1 = NULL;
475 BYTE hash[20] = { 0 };
477 WINPR_ASSERT(hostname);
478 WINPR_ASSERT(hashStr);
480 if (len < 2 *
sizeof(hash) + 1)
483 if (!(sha1 = winpr_Digest_New()))
485 if (!winpr_Digest_Init(sha1, WINPR_MD_SHA1))
487 if (!winpr_Digest_Update(sha1, (
const BYTE*)hostname, strlen(hostname)))
489 if (!winpr_Digest_Final(sha1, hash,
sizeof(hash)))
492 for (
size_t i = 0; i <
sizeof(hash); i++, hashStr += 2)
493 (
void)sprintf_s(hashStr, 3,
"%.2x", hash[i]);
498 WLog_ERR(TAG,
"failed to generate SHA1 of hostname '%s'", hostname);
499 winpr_Digest_Free(sha1);
503 static BOOL saveCal(
const rdpSettings* settings,
const BYTE* data,
size_t length,
504 const char* hostname)
506 char hash[41] = { 0 };
508 char* licenseStorePath = NULL;
509 char filename[MAX_PATH] = { 0 };
510 char filenameNew[MAX_PATH] = { 0 };
511 char* filepath = NULL;
512 char* filepathNew = NULL;
519 WINPR_ASSERT(data || (length == 0));
520 WINPR_ASSERT(hostname);
522 if (!winpr_PathFileExists(path))
524 if (!winpr_PathMakePath(path, 0))
526 WLog_ERR(TAG,
"error creating directory '%s'", path);
529 WLog_INFO(TAG,
"creating directory %s", path);
532 if (!(licenseStorePath = GetCombinedPath(path, licenseStore)))
534 WLog_ERR(TAG,
"Failed to get license store path from '%s' + '%s'", path, licenseStore);
538 if (!winpr_PathFileExists(licenseStorePath))
540 if (!winpr_PathMakePath(licenseStorePath, 0))
542 WLog_ERR(TAG,
"error creating directory '%s'", licenseStorePath);
545 WLog_INFO(TAG,
"creating directory %s", licenseStorePath);
548 if (!computeCalHash(hostname, hash,
sizeof(hash)))
550 (void)sprintf_s(filename,
sizeof(filename) - 1,
"%s.cal", hash);
551 (void)sprintf_s(filenameNew,
sizeof(filenameNew) - 1,
"%s.cal.new", hash);
553 if (!(filepath = GetCombinedPath(licenseStorePath, filename)))
555 WLog_ERR(TAG,
"Failed to get license file path from '%s' + '%s'", path, filename);
559 if (!(filepathNew = GetCombinedPath(licenseStorePath, filenameNew)))
561 WLog_ERR(TAG,
"Failed to get license new file path from '%s' + '%s'", path, filenameNew);
565 fp = winpr_fopen(filepathNew,
"wb");
568 WLog_ERR(TAG,
"Failed to open license file '%s'", filepathNew);
572 written = fwrite(data, length, 1, fp);
577 WLog_ERR(TAG,
"Failed to write to license file '%s'", filepathNew);
578 winpr_DeleteFile(filepathNew);
582 ret = winpr_MoveFileEx(filepathNew, filepath, MOVEFILE_REPLACE_EXISTING);
584 WLog_ERR(TAG,
"Failed to move license file '%s' to '%s'", filepathNew, filepath);
589 free(licenseStorePath);
593 static BYTE* loadCalFile(
const rdpSettings* settings,
const char* hostname,
size_t* dataLen)
595 char* licenseStorePath = NULL;
596 char* calPath = NULL;
597 char calFilename[MAX_PATH] = { 0 };
598 char hash[41] = { 0 };
604 WINPR_ASSERT(settings);
605 WINPR_ASSERT(hostname);
606 WINPR_ASSERT(dataLen);
608 if (!computeCalHash(hostname, hash,
sizeof(hash)))
610 WLog_ERR(TAG,
"loadCalFile: unable to compute hostname hash");
614 (void)sprintf_s(calFilename,
sizeof(calFilename) - 1,
"%s.cal", hash);
616 if (!(licenseStorePath = GetCombinedPath(
620 if (!(calPath = GetCombinedPath(licenseStorePath, calFilename)))
623 fp = winpr_fopen(calPath,
"rb");
627 if (_fseeki64(fp, 0, SEEK_END) != 0)
629 length = _ftelli64(fp);
630 if (_fseeki64(fp, 0, SEEK_SET) != 0)
635 ret = (BYTE*)malloc((
size_t)length);
639 status = fread(ret, (
size_t)length, 1, fp);
643 *dataLen = (size_t)length;
647 free(licenseStorePath);
657 free(licenseStorePath);
671 static BOOL license_read_preamble(
wStream* s, BYTE* bMsgType, BYTE* flags, UINT16* wMsgSize)
673 WINPR_ASSERT(bMsgType);
675 WINPR_ASSERT(wMsgSize);
678 if (!license_check_stream_length(s, 4,
"license preamble"))
681 Stream_Read_UINT8(s, *bMsgType);
682 Stream_Read_UINT8(s, *flags);
683 Stream_Read_UINT16(s, *wMsgSize);
684 return license_check_stream_length(s, *wMsgSize - 4ll,
"license preamble::wMsgSize");
697 static BOOL license_write_preamble(
wStream* s, BYTE bMsgType, BYTE flags, UINT16 wMsgSize)
699 if (!Stream_EnsureRemainingCapacity(s, 4))
703 Stream_Write_UINT8(s, bMsgType);
704 Stream_Write_UINT8(s, flags);
705 Stream_Write_UINT16(s, wMsgSize);
717 wStream* license_send_stream_init(rdpLicense* license)
719 WINPR_ASSERT(license);
720 WINPR_ASSERT(license->rdp);
722 const BOOL do_crypt = license->rdp->do_crypt;
724 license->rdp->sec_flags = SEC_LICENSE_PKT;
736 license->rdp->sec_flags |= SEC_LICENSE_ENCRYPT_CS;
737 license->rdp->do_crypt = license->rdp->do_crypt_license;
740 wStream* s = rdp_send_stream_init(license->rdp);
744 license->rdp->do_crypt = do_crypt;
745 license->PacketHeaderLength = (UINT16)Stream_GetPosition(s);
746 if (!Stream_SafeSeek(s, LICENSE_PREAMBLE_LENGTH))
762 static BOOL license_send(rdpLicense* license,
wStream* s, BYTE type)
764 WINPR_ASSERT(license);
765 WINPR_ASSERT(license->rdp);
767 rdpRdp* rdp = license->rdp;
768 WINPR_ASSERT(rdp->settings);
770 DEBUG_LICENSE(
"Sending %s Packet", license_request_type_string(type));
771 const size_t length = Stream_GetPosition(s);
772 WINPR_ASSERT(length >= license->PacketHeaderLength);
773 WINPR_ASSERT(length <= UINT16_MAX + license->PacketHeaderLength);
775 const UINT16 wMsgSize = (UINT16)(length - license->PacketHeaderLength);
776 Stream_SetPosition(s, license->PacketHeaderLength);
777 BYTE flags = PREAMBLE_VERSION_3_0;
784 if (!rdp->settings->ServerMode)
785 flags |= EXTENDED_ERROR_MSG_SUPPORTED;
787 if (!license_write_preamble(s, type, flags, wMsgSize))
790 #ifdef WITH_DEBUG_LICENSE
791 WLog_DBG(TAG,
"Sending %s Packet, length %" PRIu16
"", license_request_type_string(type),
793 winpr_HexDump(TAG, WLOG_DEBUG, Stream_PointerAs(s,
char) - LICENSE_PREAMBLE_LENGTH, wMsgSize);
795 Stream_SetPosition(s, length);
796 const BOOL ret = rdp_send(rdp, s, MCS_GLOBAL_CHANNEL_ID);
801 BOOL license_read_server_upgrade_license(rdpLicense* license,
wStream* s)
803 WINPR_ASSERT(license);
805 if (!license_read_binary_blob(s, license->EncryptedLicenseInfo))
807 if (!license_check_stream_length(s,
sizeof(license->MACData),
808 "SERVER_UPGRADE_LICENSE::MACData"))
810 Stream_Read(s, license->MACData,
sizeof(license->MACData));
814 BOOL license_write_server_upgrade_license(
const rdpLicense* license,
wStream* s)
816 WINPR_ASSERT(license);
818 if (!license_write_binary_blob(s, license->EncryptedLicenseInfo))
820 if (!license_check_stream_capacity(s,
sizeof(license->MACData),
821 "SERVER_UPGRADE_LICENSE::MACData"))
823 Stream_Write(s, license->MACData,
sizeof(license->MACData));
827 static BOOL license_server_send_new_or_upgrade_license(rdpLicense* license, BOOL upgrade)
829 wStream* s = license_send_stream_init(license);
830 const BYTE type = upgrade ? UPGRADE_LICENSE : NEW_LICENSE;
835 if (!license_write_server_upgrade_license(license, s))
838 return license_send(license, s, type);
853 state_run_t license_client_recv(rdpLicense* license,
wStream* s)
858 const size_t length = Stream_GetRemainingLength(s);
860 WINPR_ASSERT(license);
862 if (!license_read_preamble(s, &bMsgType, &flags, &wMsgSize))
863 return STATE_RUN_FAILED;
865 DEBUG_LICENSE(
"Receiving %s Packet", license_request_type_string(bMsgType));
869 case LICENSE_REQUEST:
871 if (license_get_state(license) == LICENSE_STATE_INITIAL)
872 license_set_state(license, LICENSE_STATE_CONFIGURED);
874 if (!license_ensure_state(license, LICENSE_STATE_CONFIGURED, bMsgType))
875 return STATE_RUN_FAILED;
877 if (!license_read_license_request_packet(license, s))
878 return STATE_RUN_FAILED;
880 if (!license_answer_license_request(license))
881 return STATE_RUN_FAILED;
883 license_set_state(license, LICENSE_STATE_NEW_REQUEST);
886 case PLATFORM_CHALLENGE:
887 if (!license_ensure_state(license, LICENSE_STATE_NEW_REQUEST, bMsgType))
888 return STATE_RUN_FAILED;
890 if (!license_read_platform_challenge_packet(license, s))
891 return STATE_RUN_FAILED;
893 if (!license_send_platform_challenge_response(license))
894 return STATE_RUN_FAILED;
895 license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE);
899 case UPGRADE_LICENSE:
900 if (!license_ensure_state(license, LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE, bMsgType))
901 return STATE_RUN_FAILED;
902 if (!license_read_new_or_upgrade_license_packet(license, s))
903 return STATE_RUN_FAILED;
907 if (!license_read_error_alert_packet(license, s))
908 return STATE_RUN_FAILED;
912 WLog_ERR(TAG,
"invalid bMsgType:%" PRIu8
"", bMsgType);
913 return STATE_RUN_FAILED;
916 if (!tpkt_ensure_stream_consumed(s, length))
917 return STATE_RUN_FAILED;
918 return STATE_RUN_SUCCESS;
921 state_run_t license_server_recv(rdpLicense* license,
wStream* s)
923 state_run_t rc = STATE_RUN_FAILED;
927 const size_t length = Stream_GetRemainingLength(s);
929 WINPR_ASSERT(license);
931 if (!license_read_preamble(s, &bMsgType, &flags, &wMsgSize))
934 DEBUG_LICENSE(
"Receiving %s Packet", license_request_type_string(bMsgType));
938 case NEW_LICENSE_REQUEST:
939 if (!license_ensure_state(license, LICENSE_STATE_REQUEST, bMsgType))
941 if (!license_read_new_license_request_packet(license, s))
944 if (!license_send_error_alert(license, ERR_INVALID_MAC, ST_TOTAL_ABORT,
947 if (!license_send_platform_challenge_packet(license))
949 license->update = FALSE;
950 if (!license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE))
954 if (!license_ensure_state(license, LICENSE_STATE_REQUEST, bMsgType))
956 if (!license_read_license_info(license, s))
959 if (!license_send_platform_challenge_packet(license))
961 if (!license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE))
963 license->update = TRUE;
966 case PLATFORM_CHALLENGE_RESPONSE:
967 if (!license_ensure_state(license, LICENSE_STATE_PLATFORM_CHALLENGE, bMsgType))
969 if (!license_read_client_platform_challenge_response(license, s))
975 if (license_send_error_alert(license, ERR_INVALID_CLIENT, ST_TOTAL_ABORT,
981 if (!license_server_send_new_or_upgrade_license(license, license->update))
984 license->type = LICENSE_TYPE_ISSUED;
985 license_set_state(license, LICENSE_STATE_COMPLETED);
987 rc = STATE_RUN_CONTINUE;
992 if (!license_read_error_alert_packet(license, s))
997 WLog_ERR(TAG,
"invalid bMsgType:%" PRIu8
"", bMsgType);
1001 if (!tpkt_ensure_stream_consumed(s, length))
1004 if (!state_run_success(rc))
1005 rc = STATE_RUN_SUCCESS;
1008 if (state_run_failed(rc))
1010 if (flags & EXTENDED_ERROR_MSG_SUPPORTED)
1011 license_send_error_alert(license, ERR_INVALID_CLIENT, ST_TOTAL_ABORT, NULL);
1012 license_set_state(license, LICENSE_STATE_ABORTED);
1018 void license_generate_randoms(rdpLicense* license)
1020 WINPR_ASSERT(license);
1022 #ifdef LICENSE_NULL_CLIENT_RANDOM
1023 ZeroMemory(license->ClientRandom,
sizeof(license->ClientRandom));
1025 winpr_RAND(license->ClientRandom,
sizeof(license->ClientRandom));
1028 winpr_RAND(license->ServerRandom,
sizeof(license->ServerRandom));
1030 #ifdef LICENSE_NULL_PREMASTER_SECRET
1031 ZeroMemory(license->PremasterSecret,
sizeof(license->PremasterSecret));
1033 winpr_RAND(license->PremasterSecret,
sizeof(license->PremasterSecret));
1042 static BOOL license_generate_keys(rdpLicense* license)
1044 WINPR_ASSERT(license);
1048 !security_master_secret(license->PremasterSecret,
sizeof(license->PremasterSecret),
1049 license->ClientRandom,
sizeof(license->ClientRandom),
1050 license->ServerRandom,
sizeof(license->ServerRandom),
1051 license->MasterSecret,
sizeof(license->MasterSecret)) ||
1053 !security_session_key_blob(license->MasterSecret,
sizeof(license->MasterSecret),
1054 license->ClientRandom,
sizeof(license->ClientRandom),
1055 license->ServerRandom,
sizeof(license->ServerRandom),
1056 license->SessionKeyBlob,
sizeof(license->SessionKeyBlob)))
1060 security_mac_salt_key(license->SessionKeyBlob,
sizeof(license->SessionKeyBlob),
1061 license->ClientRandom,
sizeof(license->ClientRandom),
1062 license->ServerRandom,
sizeof(license->ServerRandom), license->MacSaltKey,
1063 sizeof(license->MacSaltKey));
1064 const BOOL ret = security_licensing_encryption_key(
1065 license->SessionKeyBlob,
sizeof(license->SessionKeyBlob), license->ClientRandom,
1066 sizeof(license->ClientRandom), license->ServerRandom,
sizeof(license->ServerRandom),
1067 license->LicensingEncryptionKey,
1068 sizeof(license->LicensingEncryptionKey));
1070 #ifdef WITH_DEBUG_LICENSE
1071 WLog_DBG(TAG,
"ClientRandom:");
1072 winpr_HexDump(TAG, WLOG_DEBUG, license->ClientRandom,
sizeof(license->ClientRandom));
1073 WLog_DBG(TAG,
"ServerRandom:");
1074 winpr_HexDump(TAG, WLOG_DEBUG, license->ServerRandom,
sizeof(license->ServerRandom));
1075 WLog_DBG(TAG,
"PremasterSecret:");
1076 winpr_HexDump(TAG, WLOG_DEBUG, license->PremasterSecret,
sizeof(license->PremasterSecret));
1077 WLog_DBG(TAG,
"MasterSecret:");
1078 winpr_HexDump(TAG, WLOG_DEBUG, license->MasterSecret,
sizeof(license->MasterSecret));
1079 WLog_DBG(TAG,
"SessionKeyBlob:");
1080 winpr_HexDump(TAG, WLOG_DEBUG, license->SessionKeyBlob,
sizeof(license->SessionKeyBlob));
1081 WLog_DBG(TAG,
"MacSaltKey:");
1082 winpr_HexDump(TAG, WLOG_DEBUG, license->MacSaltKey,
sizeof(license->MacSaltKey));
1083 WLog_DBG(TAG,
"LicensingEncryptionKey:");
1084 winpr_HexDump(TAG, WLOG_DEBUG, license->LicensingEncryptionKey,
1085 sizeof(license->LicensingEncryptionKey));
1095 BOOL license_generate_hwid(rdpLicense* license)
1097 const BYTE* hashTarget = NULL;
1098 size_t targetLen = 0;
1099 BYTE macAddress[6] = { 0 };
1101 WINPR_ASSERT(license);
1102 WINPR_ASSERT(license->rdp);
1103 WINPR_ASSERT(license->rdp->settings);
1105 ZeroMemory(license->HardwareId,
sizeof(license->HardwareId));
1107 if (license->rdp->settings->OldLicenseBehaviour)
1109 hashTarget = macAddress;
1110 targetLen =
sizeof(macAddress);
1115 const char* hostname = license->rdp->settings->ClientHostname;
1116 wStream* s = Stream_StaticInit(&buffer, license->HardwareId, 4);
1117 Stream_Write_UINT32(s, license->PlatformId);
1119 hashTarget = (
const BYTE*)hostname;
1120 targetLen = hostname ? strlen(hostname) : 0;
1129 return winpr_Digest_Allow_FIPS(WINPR_MD_MD5, hashTarget, targetLen,
1130 &license->HardwareId[HWID_PLATFORM_ID_LENGTH],
1131 WINPR_MD5_DIGEST_LENGTH);
1134 static BOOL license_get_server_rsa_public_key(rdpLicense* license)
1136 rdpSettings* settings = NULL;
1138 WINPR_ASSERT(license);
1139 WINPR_ASSERT(license->certificate);
1140 WINPR_ASSERT(license->rdp);
1142 settings = license->rdp->settings;
1143 WINPR_ASSERT(settings);
1145 if (license->ServerCertificate->length < 1)
1147 if (!freerdp_certificate_read_server_cert(license->certificate, settings->ServerCertificate,
1148 settings->ServerCertificateLength))
1155 BOOL license_encrypt_premaster_secret(rdpLicense* license)
1157 WINPR_ASSERT(license);
1158 WINPR_ASSERT(license->certificate);
1160 if (!license_get_server_rsa_public_key(license))
1163 WINPR_ASSERT(license->EncryptedPremasterSecret);
1165 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1168 WLog_ERR(TAG,
"info=%p, license->certificate=%p", info, license->certificate);
1172 #ifdef WITH_DEBUG_LICENSE
1173 WLog_DBG(TAG,
"Modulus (%" PRIu32
" bits):", info->ModulusLength * 8);
1174 winpr_HexDump(TAG, WLOG_DEBUG, info->Modulus, info->ModulusLength);
1175 WLog_DBG(TAG,
"Exponent:");
1176 winpr_HexDump(TAG, WLOG_DEBUG, info->exponent,
sizeof(info->exponent));
1179 BYTE* EncryptedPremasterSecret = (BYTE*)calloc(1, info->ModulusLength);
1180 if (!EncryptedPremasterSecret)
1182 WLog_ERR(TAG,
"EncryptedPremasterSecret=%p, info->ModulusLength=%" PRIu32,
1183 EncryptedPremasterSecret, info->ModulusLength);
1187 license->EncryptedPremasterSecret->type = BB_RANDOM_BLOB;
1188 license->EncryptedPremasterSecret->length =
sizeof(license->PremasterSecret);
1189 #ifndef LICENSE_NULL_PREMASTER_SECRET
1191 const SSIZE_T length =
1192 crypto_rsa_public_encrypt(license->PremasterSecret,
sizeof(license->PremasterSecret),
1193 info, EncryptedPremasterSecret, info->ModulusLength);
1194 if ((length < 0) || (length > UINT16_MAX))
1196 WLog_ERR(TAG,
"RSA public encrypt length=%" PRIdz
" < 0 || > %" PRIu16, length,
1200 license->EncryptedPremasterSecret->length = (UINT16)length;
1203 license->EncryptedPremasterSecret->data = EncryptedPremasterSecret;
1207 static BOOL license_rc4_with_licenseKey(
const rdpLicense* license,
const BYTE* input,
size_t len,
1210 WINPR_ASSERT(license);
1211 WINPR_ASSERT(input || (len == 0));
1212 WINPR_ASSERT(target);
1213 WINPR_ASSERT(len <= UINT16_MAX);
1215 WINPR_RC4_CTX* rc4 = winpr_RC4_New_Allow_FIPS(license->LicensingEncryptionKey,
1216 sizeof(license->LicensingEncryptionKey));
1219 WLog_ERR(TAG,
"Failed to allocate RC4");
1223 BYTE* buffer = NULL;
1225 buffer = realloc(target->data, len);
1229 target->data = buffer;
1230 target->length = (UINT16)len;
1232 if (!winpr_RC4_Update(rc4, len, input, buffer))
1235 winpr_RC4_Free(rc4);
1239 WLog_ERR(TAG,
"Failed to create/update RC4: len=%" PRIuz
", buffer=%p", len, buffer);
1240 winpr_RC4_Free(rc4);
1254 static BOOL license_encrypt_and_MAC(rdpLicense* license,
const BYTE* input,
size_t len,
1257 WINPR_ASSERT(license);
1258 return license_rc4_with_licenseKey(license, input, len, target) &&
1259 security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), input, len, mac,
1274 static BOOL license_decrypt_and_check_MAC(rdpLicense* license,
const BYTE* input,
size_t len,
1277 BYTE macData[
sizeof(license->MACData)] = { 0 };
1279 WINPR_ASSERT(license);
1280 WINPR_ASSERT(target);
1284 WLog_DBG(TAG,
"TransportDumpReplay active, skipping...");
1288 if (!license_rc4_with_licenseKey(license, input, len, target))
1291 if (!security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), target->data, len,
1292 macData,
sizeof(macData)))
1295 if (memcmp(packetMac, macData,
sizeof(macData)) != 0)
1297 WLog_ERR(TAG,
"packetMac != expectedMac");
1312 WINPR_ASSERT(productInfo);
1314 if (!license_check_stream_length(s, 8,
"license product info::cbCompanyName"))
1317 Stream_Read_UINT32(s, productInfo->dwVersion);
1318 Stream_Read_UINT32(s, productInfo->cbCompanyName);
1321 if ((productInfo->cbCompanyName < 2) || (productInfo->cbCompanyName % 2 != 0))
1323 WLog_WARN(TAG,
"license product info invalid cbCompanyName %" PRIu32,
1324 productInfo->cbCompanyName);
1328 if (!license_check_stream_length(s, productInfo->cbCompanyName,
1329 "license product info::CompanyName"))
1332 productInfo->pbProductId = NULL;
1333 productInfo->pbCompanyName = (BYTE*)malloc(productInfo->cbCompanyName);
1334 if (!productInfo->pbCompanyName)
1336 Stream_Read(s, productInfo->pbCompanyName, productInfo->cbCompanyName);
1338 if (!license_check_stream_length(s, 4,
"license product info::cbProductId"))
1341 Stream_Read_UINT32(s, productInfo->cbProductId);
1343 if ((productInfo->cbProductId < 2) || (productInfo->cbProductId % 2 != 0))
1345 WLog_WARN(TAG,
"license product info invalid cbProductId %" PRIu32,
1346 productInfo->cbProductId);
1350 if (!license_check_stream_length(s, productInfo->cbProductId,
1351 "license product info::ProductId"))
1354 productInfo->pbProductId = (BYTE*)malloc(productInfo->cbProductId);
1355 if (!productInfo->pbProductId)
1357 Stream_Read(s, productInfo->pbProductId, productInfo->cbProductId);
1361 free(productInfo->pbCompanyName);
1362 free(productInfo->pbProductId);
1363 productInfo->pbCompanyName = NULL;
1364 productInfo->pbProductId = NULL;
1370 WINPR_ASSERT(productInfo);
1372 if (!license_check_stream_capacity(s, 8,
"license product info::cbCompanyName"))
1375 Stream_Write_UINT32(s, productInfo->dwVersion);
1376 Stream_Write_UINT32(s, productInfo->cbCompanyName);
1379 if ((productInfo->cbCompanyName < 2) || (productInfo->cbCompanyName % 2 != 0) ||
1380 !productInfo->pbCompanyName)
1382 WLog_WARN(TAG,
"license product info invalid cbCompanyName %" PRIu32,
1383 productInfo->cbCompanyName);
1387 if (!license_check_stream_capacity(s, productInfo->cbCompanyName,
1388 "license product info::CompanyName"))
1391 Stream_Write(s, productInfo->pbCompanyName, productInfo->cbCompanyName);
1393 if (!license_check_stream_capacity(s, 4,
"license product info::cbProductId"))
1396 Stream_Write_UINT32(s, productInfo->cbProductId);
1398 if ((productInfo->cbProductId < 2) || (productInfo->cbProductId % 2 != 0) ||
1399 !productInfo->pbProductId)
1401 WLog_WARN(TAG,
"license product info invalid cbProductId %" PRIu32,
1402 productInfo->cbProductId);
1406 if (!license_check_stream_capacity(s, productInfo->cbProductId,
1407 "license product info::ProductId"))
1410 Stream_Write(s, productInfo->pbProductId, productInfo->cbProductId);
1439 free(productInfo->pbCompanyName);
1440 free(productInfo->pbProductId);
1445 BOOL license_read_binary_blob_data(
LICENSE_BLOB* blob, UINT16 wBlobType,
const void* data,
1449 WINPR_ASSERT(length <= UINT16_MAX);
1450 WINPR_ASSERT(data || (length == 0));
1452 blob->length = (UINT16)length;
1456 if ((blob->type != wBlobType) && (blob->type != BB_ANY_BLOB))
1458 WLog_ERR(TAG,
"license binary blob::type expected %s, got %s",
1459 licencse_blob_type_string(wBlobType), licencse_blob_type_string(blob->type));
1466 if ((blob->type != BB_ANY_BLOB) && (blob->length == 0))
1468 WLog_WARN(TAG,
"license binary blob::type %s, length=0, skipping.",
1469 licencse_blob_type_string(blob->type));
1473 blob->type = wBlobType;
1475 if (blob->length > 0)
1476 blob->data = malloc(blob->length);
1479 WLog_ERR(TAG,
"license binary blob::length=%" PRIu16
", blob::data=%p", blob->length,
1483 memcpy(blob->data, data, blob->length);
1496 UINT16 wBlobType = 0;
1501 if (!license_check_stream_length(s, 4,
"license binary blob::type"))
1504 Stream_Read_UINT16(s, wBlobType);
1505 Stream_Read_UINT16(s, length);
1507 if (!license_check_stream_length(s, length,
"license binary blob::length"))
1510 if (!license_read_binary_blob_data(blob, wBlobType, Stream_Pointer(s), length))
1513 return Stream_SafeSeek(s, length);
1527 if (!Stream_EnsureRemainingCapacity(s, blob->length + 4))
1530 Stream_Write_UINT16(s, blob->type);
1531 Stream_Write_UINT16(s, blob->length);
1533 if (blob->length > 0)
1534 Stream_Write(s, blob->data, blob->length);
1538 static BOOL license_write_encrypted_premaster_secret_blob(
wStream* s,
const LICENSE_BLOB* blob,
1539 UINT32 ModulusLength)
1541 const UINT32 length = ModulusLength + 8;
1544 WINPR_ASSERT(length <= UINT16_MAX);
1546 if (blob->length > ModulusLength)
1548 WLog_ERR(TAG,
"invalid blob");
1552 if (!Stream_EnsureRemainingCapacity(s, length + 4))
1554 Stream_Write_UINT16(s, blob->type);
1555 Stream_Write_UINT16(s, (UINT16)length);
1557 if (blob->length > 0)
1558 Stream_Write(s, blob->data, blob->length);
1560 Stream_Zero(s, length - blob->length);
1565 UINT32* ModulusLength)
1567 if (!license_read_binary_blob(s, blob))
1569 WINPR_ASSERT(ModulusLength);
1570 *ModulusLength = blob->length;
1612 UINT32 scopeCount = 0;
1614 WINPR_ASSERT(scopeList);
1616 if (!license_check_stream_length(s, 4,
"license scope list"))
1619 Stream_Read_UINT32(s, scopeCount);
1621 if (!license_check_stream_length(s, 4ll * scopeCount,
"license scope list::count"))
1624 if (!license_scope_list_resize(scopeList, scopeCount))
1627 for (UINT32 i = 0; i < scopeCount; i++)
1629 if (!license_read_binary_blob(s, scopeList->array[i]))
1638 WINPR_ASSERT(scopeList);
1640 if (!license_check_stream_capacity(s, 4,
"license scope list"))
1643 Stream_Write_UINT32(s, scopeList->count);
1645 if (!license_check_stream_capacity(s, scopeList->count * 4ull,
"license scope list::count"))
1649 WINPR_ASSERT(scopeList->array || (scopeList->count == 0));
1650 for (UINT32 i = 0; i < scopeList->count; i++)
1654 if (!license_write_binary_blob(s, element))
1673 BOOL license_scope_list_resize(
SCOPE_LIST* scopeList, UINT32 count)
1675 WINPR_ASSERT(scopeList);
1676 WINPR_ASSERT(scopeList->array || (scopeList->count == 0));
1678 for (UINT32 x = count; x < scopeList->count; x++)
1680 license_free_binary_blob(scopeList->array[x]);
1681 scopeList->array[x] = NULL;
1689 scopeList->array = tmp;
1693 free(scopeList->array);
1694 scopeList->array = NULL;
1697 for (UINT32 x = scopeList->count; x < count; x++)
1699 LICENSE_BLOB* blob = license_new_binary_blob(BB_SCOPE_BLOB);
1702 scopeList->count = x;
1705 scopeList->array[x] = blob;
1708 scopeList->count = count;
1718 void license_free_scope_list(
SCOPE_LIST* scopeList)
1723 license_scope_list_resize(scopeList, 0);
1727 BOOL license_send_license_info(rdpLicense* license,
const LICENSE_BLOB* calBlob,
1728 const BYTE* signature,
size_t signature_length)
1730 WINPR_ASSERT(calBlob);
1731 WINPR_ASSERT(signature);
1732 WINPR_ASSERT(license->certificate);
1734 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1738 wStream* s = license_send_stream_init(license);
1742 if (!license_check_stream_capacity(s, 8 +
sizeof(license->ClientRandom),
1743 "license info::ClientRandom"))
1746 Stream_Write_UINT32(s,
1747 license->PreferredKeyExchangeAlg);
1748 Stream_Write_UINT32(s, license->PlatformId);
1751 Stream_Write(s, license->ClientRandom,
sizeof(license->ClientRandom));
1754 if (!license_write_encrypted_premaster_secret_blob(s, license->EncryptedPremasterSecret,
1755 info->ModulusLength))
1759 if (!license_write_binary_blob(s, calBlob))
1763 if (!license_write_binary_blob(s, license->EncryptedHardwareId))
1767 if (!license_check_stream_capacity(s, signature_length,
"license info::MACData"))
1769 Stream_Write(s, signature, signature_length);
1771 return license_send(license, s, LICENSE_INFO);
1778 static BOOL license_check_preferred_alg(rdpLicense* license, UINT32 PreferredKeyExchangeAlg,
1781 WINPR_ASSERT(license);
1782 WINPR_ASSERT(where);
1784 if (license->PreferredKeyExchangeAlg != PreferredKeyExchangeAlg)
1786 char buffer1[64] = { 0 };
1787 char buffer2[64] = { 0 };
1788 WLog_WARN(TAG,
"%s::PreferredKeyExchangeAlg, expected %s, got %s", where,
1789 license_preferred_key_exchange_alg_string(license->PreferredKeyExchangeAlg,
1790 buffer1,
sizeof(buffer1)),
1791 license_preferred_key_exchange_alg_string(PreferredKeyExchangeAlg, buffer2,
1798 BOOL license_read_license_info(rdpLicense* license,
wStream* s)
1801 UINT32 PreferredKeyExchangeAlg = 0;
1803 WINPR_ASSERT(license);
1804 WINPR_ASSERT(license->certificate);
1806 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1811 if (!license_check_stream_length(s, 8 +
sizeof(license->ClientRandom),
"license info"))
1814 Stream_Read_UINT32(s, PreferredKeyExchangeAlg);
1815 if (!license_check_preferred_alg(license, PreferredKeyExchangeAlg,
"license info"))
1817 Stream_Read_UINT32(s, license->PlatformId);
1820 Stream_Read(s, license->ClientRandom,
sizeof(license->ClientRandom));
1823 UINT32 ModulusLength = 0;
1824 if (!license_read_encrypted_premaster_secret_blob(s, license->EncryptedPremasterSecret,
1828 if (ModulusLength != info->ModulusLength)
1831 "EncryptedPremasterSecret,::ModulusLength[%" PRIu32
1832 "] != rdpCertInfo::ModulusLength[%" PRIu32
"]",
1833 ModulusLength, info->ModulusLength);
1837 if (!license_read_binary_blob(s, license->LicenseInfo))
1841 if (!license_read_binary_blob(s, license->EncryptedHardwareId))
1845 if (!license_check_stream_length(s,
sizeof(license->MACData),
"license info::MACData"))
1847 Stream_Read(s, license->MACData,
sizeof(license->MACData));
1862 BOOL license_read_license_request_packet(rdpLicense* license,
wStream* s)
1864 WINPR_ASSERT(license);
1867 if (!license_check_stream_length(s,
sizeof(license->ServerRandom),
"license request"))
1870 Stream_Read(s, license->ServerRandom,
sizeof(license->ServerRandom));
1873 if (!license_read_product_info(s, license->ProductInfo))
1877 if (!license_read_binary_blob(s, license->KeyExchangeList))
1881 if (!license_read_binary_blob(s, license->ServerCertificate))
1885 if (!license_read_scope_list(s, license->ScopeList))
1889 if (!freerdp_certificate_read_server_cert(license->certificate,
1890 license->ServerCertificate->data,
1891 license->ServerCertificate->length))
1894 if (!license_generate_keys(license) || !license_generate_hwid(license) ||
1895 !license_encrypt_premaster_secret(license))
1898 #ifdef WITH_DEBUG_LICENSE
1899 WLog_DBG(TAG,
"ServerRandom:");
1900 winpr_HexDump(TAG, WLOG_DEBUG, license->ServerRandom,
sizeof(license->ServerRandom));
1901 license_print_product_info(license->ProductInfo);
1902 license_print_scope_list(license->ScopeList);
1907 BOOL license_write_license_request_packet(
const rdpLicense* license,
wStream* s)
1909 WINPR_ASSERT(license);
1912 if (!license_check_stream_capacity(s,
sizeof(license->ServerRandom),
"license request"))
1914 Stream_Write(s, license->ServerRandom,
sizeof(license->ServerRandom));
1917 if (!license_write_product_info(s, license->ProductInfo))
1921 if (!license_write_binary_blob(s, license->KeyExchangeList))
1925 if (!license_write_binary_blob(s, license->ServerCertificate))
1929 if (!license_write_scope_list(s, license->ScopeList))
1935 static BOOL license_send_license_request_packet(rdpLicense* license)
1937 wStream* s = license_send_stream_init(license);
1941 if (!license_write_license_request_packet(license, s))
1944 return license_send(license, s, LICENSE_REQUEST);
1958 BOOL license_read_platform_challenge_packet(rdpLicense* license,
wStream* s)
1960 BYTE macData[LICENSING_ENCRYPTION_KEY_LENGTH] = { 0 };
1961 UINT32 ConnectFlags = 0;
1963 WINPR_ASSERT(license);
1965 DEBUG_LICENSE(
"Receiving Platform Challenge Packet");
1967 if (!license_check_stream_length(s, 4,
"license platform challenge"))
1970 Stream_Read_UINT32(s, ConnectFlags);
1973 license->EncryptedPlatformChallenge->type = BB_ANY_BLOB;
1974 if (!license_read_binary_blob(s, license->EncryptedPlatformChallenge))
1976 license->EncryptedPlatformChallenge->type = BB_ENCRYPTED_DATA_BLOB;
1979 if (!license_check_stream_length(s,
sizeof(macData),
"license platform challenge::MAC"))
1982 Stream_Read(s, macData,
sizeof(macData));
1983 if (!license_decrypt_and_check_MAC(license, license->EncryptedPlatformChallenge->data,
1984 license->EncryptedPlatformChallenge->length,
1985 license->PlatformChallenge, macData))
1988 #ifdef WITH_DEBUG_LICENSE
1989 WLog_DBG(TAG,
"ConnectFlags: 0x%08" PRIX32
"", ConnectFlags);
1990 WLog_DBG(TAG,
"EncryptedPlatformChallenge:");
1991 winpr_HexDump(TAG, WLOG_DEBUG, license->EncryptedPlatformChallenge->data,
1992 license->EncryptedPlatformChallenge->length);
1993 WLog_DBG(TAG,
"PlatformChallenge:");
1994 winpr_HexDump(TAG, WLOG_DEBUG, license->PlatformChallenge->data,
1995 license->PlatformChallenge->length);
1996 WLog_DBG(TAG,
"MacData:");
1997 winpr_HexDump(TAG, WLOG_DEBUG, macData,
sizeof(macData));
2002 BOOL license_send_error_alert(rdpLicense* license, UINT32 dwErrorCode, UINT32 dwStateTransition,
2005 wStream* s = license_send_stream_init(license);
2010 if (!license_check_stream_capacity(s, 8,
"license error alert"))
2012 Stream_Write_UINT32(s, dwErrorCode);
2013 Stream_Write_UINT32(s, dwStateTransition);
2017 if (!license_write_binary_blob(s, info))
2021 return license_send(license, s, ERROR_ALERT);
2027 BOOL license_send_platform_challenge_packet(rdpLicense* license)
2029 wStream* s = license_send_stream_init(license);
2034 DEBUG_LICENSE(
"Receiving Platform Challenge Packet");
2036 if (!license_check_stream_capacity(s, 4,
"license platform challenge"))
2042 if (!license_write_binary_blob(s, license->EncryptedPlatformChallenge))
2046 if (!license_check_stream_length(s,
sizeof(license->MACData),
2047 "license platform challenge::MAC"))
2050 Stream_Write(s, license->MACData,
sizeof(license->MACData));
2052 return license_send(license, s, PLATFORM_CHALLENGE);
2058 static BOOL license_read_encrypted_blob(
const rdpLicense* license,
wStream* s,
LICENSE_BLOB* target)
2060 UINT16 wBlobType = 0;
2061 UINT16 wBlobLen = 0;
2063 WINPR_ASSERT(license);
2064 WINPR_ASSERT(target);
2066 if (!license_check_stream_length(s, 4,
"license encrypted blob"))
2069 Stream_Read_UINT16(s, wBlobType);
2070 if (wBlobType != BB_ENCRYPTED_DATA_BLOB)
2074 "expecting BB_ENCRYPTED_DATA_BLOB blob, probably a windows 2003 server, continuing...");
2077 Stream_Read_UINT16(s, wBlobLen);
2079 BYTE* encryptedData = Stream_Pointer(s);
2080 if (!Stream_SafeSeek(s, wBlobLen))
2083 "short license encrypted blob::length, expected %" PRIu16
" bytes, got %" PRIuz,
2084 wBlobLen, Stream_GetRemainingLength(s));
2088 return license_rc4_with_licenseKey(license, encryptedData, wBlobLen, target);
2098 BOOL license_read_new_or_upgrade_license_packet(rdpLicense* license,
wStream* s)
2100 UINT32 os_major = 0;
2101 UINT32 os_minor = 0;
2103 UINT32 cbCompanyName = 0;
2104 UINT32 cbProductId = 0;
2105 UINT32 cbLicenseInfo = 0;
2107 wStream* licenseStream = NULL;
2109 BYTE computedMac[16] = { 0 };
2110 const BYTE* readMac = NULL;
2112 WINPR_ASSERT(license);
2114 DEBUG_LICENSE(
"Receiving Server New/Upgrade License Packet");
2116 LICENSE_BLOB* calBlob = license_new_binary_blob(BB_DATA_BLOB);
2121 if (!license_read_encrypted_blob(license, s, calBlob))
2125 readMac = Stream_Pointer(s);
2126 if (!Stream_SafeSeek(s,
sizeof(computedMac)))
2128 WLog_WARN(TAG,
"short license new/upgrade, expected 16 bytes, got %" PRIuz,
2129 Stream_GetRemainingLength(s));
2133 if (!security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), calBlob->data,
2134 calBlob->length, computedMac,
sizeof(computedMac)))
2137 if (memcmp(computedMac, readMac,
sizeof(computedMac)) != 0)
2139 WLog_ERR(TAG,
"new or upgrade license MAC mismatch");
2143 licenseStream = Stream_StaticConstInit(&sbuffer, calBlob->data, calBlob->length);
2146 WLog_ERR(TAG,
"license::blob::data=%p, license::blob::length=%" PRIu16, calBlob->data,
2151 if (!license_check_stream_length(licenseStream, 8,
"license new/upgrade::blob::version"))
2154 Stream_Read_UINT16(licenseStream, os_minor);
2155 Stream_Read_UINT16(licenseStream, os_major);
2157 WLog_DBG(TAG,
"Version: %" PRIu16
".%" PRIu16, os_major, os_minor);
2160 Stream_Read_UINT32(licenseStream, cbScope);
2161 if (!license_check_stream_length(licenseStream, cbScope,
"license new/upgrade::blob::scope"))
2164 #ifdef WITH_DEBUG_LICENSE
2165 WLog_DBG(TAG,
"Scope:");
2166 winpr_HexDump(TAG, WLOG_DEBUG, Stream_Pointer(licenseStream), cbScope);
2168 Stream_Seek(licenseStream, cbScope);
2171 if (!license_check_stream_length(licenseStream, 4,
"license new/upgrade::blob::cbCompanyName"))
2174 Stream_Read_UINT32(licenseStream, cbCompanyName);
2175 if (!license_check_stream_length(licenseStream, cbCompanyName,
2176 "license new/upgrade::blob::CompanyName"))
2179 #ifdef WITH_DEBUG_LICENSE
2180 WLog_DBG(TAG,
"Company name:");
2181 winpr_HexDump(TAG, WLOG_DEBUG, Stream_Pointer(licenseStream), cbCompanyName);
2183 Stream_Seek(licenseStream, cbCompanyName);
2186 if (!license_check_stream_length(licenseStream, 4,
"license new/upgrade::blob::cbProductId"))
2189 Stream_Read_UINT32(licenseStream, cbProductId);
2191 if (!license_check_stream_length(licenseStream, cbProductId,
2192 "license new/upgrade::blob::ProductId"))
2195 #ifdef WITH_DEBUG_LICENSE
2196 WLog_DBG(TAG,
"Product id:");
2197 winpr_HexDump(TAG, WLOG_DEBUG, Stream_Pointer(licenseStream), cbProductId);
2199 Stream_Seek(licenseStream, cbProductId);
2202 if (!license_check_stream_length(licenseStream, 4,
"license new/upgrade::blob::cbLicenseInfo"))
2205 Stream_Read_UINT32(licenseStream, cbLicenseInfo);
2206 if (!license_check_stream_length(licenseStream, cbLicenseInfo,
2207 "license new/upgrade::blob::LicenseInfo"))
2210 license->type = LICENSE_TYPE_ISSUED;
2211 ret = license_set_state(license, LICENSE_STATE_COMPLETED);
2213 if (!license->rdp->settings->OldLicenseBehaviour)
2214 ret = saveCal(license->rdp->settings, Stream_Pointer(licenseStream), cbLicenseInfo,
2215 license->rdp->settings->ClientHostname);
2218 license_free_binary_blob(calBlob);
2229 BOOL license_read_error_alert_packet(rdpLicense* license,
wStream* s)
2231 UINT32 dwErrorCode = 0;
2232 UINT32 dwStateTransition = 0;
2234 WINPR_ASSERT(license);
2235 WINPR_ASSERT(license->rdp);
2237 if (!license_check_stream_length(s, 8ul,
"error alert"))
2240 Stream_Read_UINT32(s, dwErrorCode);
2241 Stream_Read_UINT32(s, dwStateTransition);
2243 if (!license_read_binary_blob(s, license->ErrorInfo))
2246 #ifdef WITH_DEBUG_LICENSE
2247 WLog_DBG(TAG,
"dwErrorCode: %s, dwStateTransition: %s", error_codes[dwErrorCode],
2248 state_transitions[dwStateTransition]);
2251 if (dwErrorCode == STATUS_VALID_CLIENT)
2253 license->type = LICENSE_TYPE_NONE;
2254 return license_set_state(license, LICENSE_STATE_COMPLETED);
2257 switch (dwStateTransition)
2259 case ST_TOTAL_ABORT:
2260 license_set_state(license, LICENSE_STATE_ABORTED);
2262 case ST_NO_TRANSITION:
2263 license_set_state(license, LICENSE_STATE_COMPLETED);
2265 case ST_RESET_PHASE_TO_START:
2266 license_set_state(license, LICENSE_STATE_CONFIGURED);
2268 case ST_RESEND_LAST_MESSAGE:
2284 BOOL license_write_new_license_request_packet(
const rdpLicense* license,
wStream* s)
2286 WINPR_ASSERT(license);
2288 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
2292 if (!license_check_stream_capacity(s, 8 +
sizeof(license->ClientRandom),
"License Request"))
2295 Stream_Write_UINT32(s,
2296 license->PreferredKeyExchangeAlg);
2297 Stream_Write_UINT32(s, license->PlatformId);
2298 Stream_Write(s, license->ClientRandom,
2299 sizeof(license->ClientRandom));
2302 !license_write_encrypted_premaster_secret_blob(s, license->EncryptedPremasterSecret,
2303 info->ModulusLength) ||
2305 !license_write_binary_blob(s, license->ClientUserName) ||
2307 !license_write_binary_blob(s, license->ClientMachineName))
2312 #ifdef WITH_DEBUG_LICENSE
2313 WLog_DBG(TAG,
"PreferredKeyExchangeAlg: 0x%08" PRIX32
"", license->PreferredKeyExchangeAlg);
2314 WLog_DBG(TAG,
"ClientRandom:");
2315 winpr_HexDump(TAG, WLOG_DEBUG, license->ClientRandom,
sizeof(license->ClientRandom));
2316 WLog_DBG(TAG,
"EncryptedPremasterSecret");
2317 winpr_HexDump(TAG, WLOG_DEBUG, license->EncryptedPremasterSecret->data,
2318 license->EncryptedPremasterSecret->length);
2319 WLog_DBG(TAG,
"ClientUserName (%" PRIu16
"): %s", license->ClientUserName->length,
2320 (
char*)license->ClientUserName->data);
2321 WLog_DBG(TAG,
"ClientMachineName (%" PRIu16
"): %s", license->ClientMachineName->length,
2322 (
char*)license->ClientMachineName->data);
2327 BOOL license_read_new_license_request_packet(rdpLicense* license,
wStream* s)
2329 UINT32 PreferredKeyExchangeAlg = 0;
2331 WINPR_ASSERT(license);
2333 if (!license_check_stream_length(s, 8ull +
sizeof(license->ClientRandom),
2334 "new license request"))
2337 Stream_Read_UINT32(s, PreferredKeyExchangeAlg);
2338 if (!license_check_preferred_alg(license, PreferredKeyExchangeAlg,
"new license request"))
2341 Stream_Read_UINT32(s, license->PlatformId);
2342 Stream_Read(s, license->ClientRandom,
2343 sizeof(license->ClientRandom));
2346 UINT32 ModulusLength = 0;
2347 if (!license_read_encrypted_premaster_secret_blob(s, license->EncryptedPremasterSecret,
2351 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
2353 WLog_WARN(TAG,
"Missing license certificate, skipping ModulusLength checks");
2354 else if (ModulusLength != info->ModulusLength)
2357 "EncryptedPremasterSecret expected to be %" PRIu32
" bytes, but read %" PRIu32
2359 info->ModulusLength, ModulusLength);
2364 if (!license_read_binary_blob(s, license->ClientUserName))
2367 if (!license_read_binary_blob(s, license->ClientMachineName))
2379 BOOL license_answer_license_request(rdpLicense* license)
2382 BYTE* license_data = NULL;
2383 size_t license_size = 0;
2385 char* username = NULL;
2387 WINPR_ASSERT(license);
2388 WINPR_ASSERT(license->rdp);
2389 WINPR_ASSERT(license->rdp->settings);
2391 if (!license->rdp->settings->OldLicenseBehaviour)
2392 license_data = loadCalFile(license->rdp->settings, license->rdp->settings->ClientHostname,
2398 BYTE signature[LICENSING_ENCRYPTION_KEY_LENGTH] = { 0 };
2400 DEBUG_LICENSE(
"Sending Saved License Packet");
2402 WINPR_ASSERT(license->EncryptedHardwareId);
2403 license->EncryptedHardwareId->type = BB_ENCRYPTED_DATA_BLOB;
2404 if (!license_encrypt_and_MAC(license, license->HardwareId,
sizeof(license->HardwareId),
2405 license->EncryptedHardwareId, signature,
sizeof(signature)))
2411 calBlob = license_new_binary_blob(BB_DATA_BLOB);
2417 calBlob->data = license_data;
2418 WINPR_ASSERT(license_size <= UINT16_MAX);
2419 calBlob->length = (UINT16)license_size;
2421 status = license_send_license_info(license, calBlob, signature,
sizeof(signature));
2422 license_free_binary_blob(calBlob);
2427 DEBUG_LICENSE(
"Sending New License Packet");
2429 s = license_send_stream_init(license);
2432 if (license->rdp->settings->Username != NULL)
2433 username = license->rdp->settings->Username;
2435 username =
"username";
2438 WINPR_ASSERT(license->ClientUserName);
2439 const size_t len = strlen(username) + 1;
2440 WINPR_ASSERT(len <= UINT16_MAX);
2442 license->ClientUserName->data = (BYTE*)username;
2443 license->ClientUserName->length = (UINT16)len;
2447 WINPR_ASSERT(license->ClientMachineName);
2448 const size_t len = strlen(license->rdp->settings->ClientHostname) + 1;
2449 WINPR_ASSERT(len <= UINT16_MAX);
2451 license->ClientMachineName->data = (BYTE*)license->rdp->settings->ClientHostname;
2452 license->ClientMachineName->length = (UINT16)len;
2454 status = license_write_new_license_request_packet(license, s);
2456 WINPR_ASSERT(license->ClientUserName);
2457 license->ClientUserName->data = NULL;
2458 license->ClientUserName->length = 0;
2460 WINPR_ASSERT(license->ClientMachineName);
2461 license->ClientMachineName->data = NULL;
2462 license->ClientMachineName->length = 0;
2470 return license_send(license, s, NEW_LICENSE_REQUEST);
2479 BOOL license_send_platform_challenge_response(rdpLicense* license)
2481 wStream* s = license_send_stream_init(license);
2482 wStream* challengeRespData = NULL;
2483 BYTE* buffer = NULL;
2486 WINPR_ASSERT(license);
2487 WINPR_ASSERT(license->PlatformChallenge);
2488 WINPR_ASSERT(license->MacSaltKey);
2489 WINPR_ASSERT(license->EncryptedPlatformChallenge);
2490 WINPR_ASSERT(license->EncryptedHardwareId);
2492 DEBUG_LICENSE(
"Sending Platform Challenge Response Packet");
2494 license->EncryptedPlatformChallenge->type = BB_DATA_BLOB;
2497 challengeRespData = Stream_New(NULL, 8 + license->PlatformChallenge->length);
2498 if (!challengeRespData)
2500 Stream_Write_UINT16(challengeRespData, PLATFORM_CHALLENGE_RESPONSE_VERSION);
2501 Stream_Write_UINT16(challengeRespData, license->ClientType);
2502 Stream_Write_UINT16(challengeRespData, license->LicenseDetailLevel);
2503 Stream_Write_UINT16(challengeRespData, license->PlatformChallenge->length);
2504 Stream_Write(challengeRespData, license->PlatformChallenge->data,
2505 license->PlatformChallenge->length);
2506 Stream_SealLength(challengeRespData);
2509 const size_t length = Stream_Length(challengeRespData) +
sizeof(license->HardwareId);
2510 buffer = (BYTE*)malloc(length);
2513 Stream_Free(challengeRespData, TRUE);
2517 CopyMemory(buffer, Stream_Buffer(challengeRespData), Stream_Length(challengeRespData));
2518 CopyMemory(&buffer[Stream_Length(challengeRespData)], license->HardwareId,
2519 sizeof(license->HardwareId));
2520 status = security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), buffer, length,
2521 license->MACData,
sizeof(license->MACData));
2526 Stream_Free(challengeRespData, TRUE);
2530 license->EncryptedHardwareId->type = BB_ENCRYPTED_DATA_BLOB;
2531 if (!license_rc4_with_licenseKey(license, license->HardwareId,
sizeof(license->HardwareId),
2532 license->EncryptedHardwareId))
2534 Stream_Free(challengeRespData, TRUE);
2538 status = license_rc4_with_licenseKey(license, Stream_Buffer(challengeRespData),
2539 Stream_Length(challengeRespData),
2540 license->EncryptedPlatformChallengeResponse);
2541 Stream_Free(challengeRespData, TRUE);
2545 #ifdef WITH_DEBUG_LICENSE
2546 WLog_DBG(TAG,
"LicensingEncryptionKey:");
2547 winpr_HexDump(TAG, WLOG_DEBUG, license->LicensingEncryptionKey, 16);
2548 WLog_DBG(TAG,
"HardwareId:");
2549 winpr_HexDump(TAG, WLOG_DEBUG, license->HardwareId,
sizeof(license->HardwareId));
2550 WLog_DBG(TAG,
"EncryptedHardwareId:");
2551 winpr_HexDump(TAG, WLOG_DEBUG, license->EncryptedHardwareId->data,
2552 license->EncryptedHardwareId->length);
2554 if (license_write_client_platform_challenge_response(license, s))
2555 return license_send(license, s, PLATFORM_CHALLENGE_RESPONSE);
2561 BOOL license_read_platform_challenge_response(rdpLicense* license,
wStream* s)
2563 UINT16 wVersion = 0;
2564 UINT16 cbChallenge = 0;
2565 const BYTE* pbChallenge = NULL;
2567 WINPR_ASSERT(license);
2568 WINPR_ASSERT(license->PlatformChallenge);
2569 WINPR_ASSERT(license->MacSaltKey);
2570 WINPR_ASSERT(license->EncryptedPlatformChallenge);
2571 WINPR_ASSERT(license->EncryptedHardwareId);
2573 DEBUG_LICENSE(
"Receiving Platform Challenge Response Packet");
2575 if (!license_check_stream_length(s, 8,
"PLATFORM_CHALLENGE_RESPONSE_DATA"))
2578 Stream_Read_UINT16(s, wVersion);
2579 if (wVersion != PLATFORM_CHALLENGE_RESPONSE_VERSION)
2582 "Invalid PLATFORM_CHALLENGE_RESPONSE_DATA::wVersion 0x%04" PRIx16
2583 ", expected 0x04" PRIx16,
2584 wVersion, PLATFORM_CHALLENGE_RESPONSE_VERSION);
2587 Stream_Read_UINT16(s, license->ClientType);
2588 Stream_Read_UINT16(s, license->LicenseDetailLevel);
2589 Stream_Read_UINT16(s, cbChallenge);
2591 if (!license_check_stream_length(s, cbChallenge,
2592 "PLATFORM_CHALLENGE_RESPONSE_DATA::pbChallenge"))
2595 pbChallenge = Stream_Pointer(s);
2596 if (!license_read_binary_blob_data(license->EncryptedPlatformChallengeResponse, BB_DATA_BLOB,
2597 pbChallenge, cbChallenge))
2599 return Stream_SafeSeek(s, cbChallenge);
2602 BOOL license_write_client_platform_challenge_response(rdpLicense* license,
wStream* s)
2604 WINPR_ASSERT(license);
2606 if (!license_write_binary_blob(s, license->EncryptedPlatformChallengeResponse))
2608 if (!license_write_binary_blob(s, license->EncryptedHardwareId))
2610 if (!license_check_stream_capacity(s,
sizeof(license->MACData),
2611 "CLIENT_PLATFORM_CHALLENGE_RESPONSE::MACData"))
2613 Stream_Write(s, license->MACData,
sizeof(license->MACData));
2617 BOOL license_read_client_platform_challenge_response(rdpLicense* license,
wStream* s)
2619 WINPR_ASSERT(license);
2621 if (!license_read_binary_blob(s, license->EncryptedPlatformChallengeResponse))
2623 if (!license_read_binary_blob(s, license->EncryptedHardwareId))
2625 if (!license_check_stream_length(s,
sizeof(license->MACData),
2626 "CLIENT_PLATFORM_CHALLENGE_RESPONSE::MACData"))
2628 Stream_Read(s, license->MACData,
sizeof(license->MACData));
2641 BOOL license_send_valid_client_error_packet(rdpRdp* rdp)
2644 rdpLicense* license = rdp->license;
2645 WINPR_ASSERT(license);
2647 license->state = LICENSE_STATE_COMPLETED;
2648 license->type = LICENSE_TYPE_NONE;
2649 return license_send_error_alert(license, STATUS_VALID_CLIENT, ST_NO_TRANSITION,
2650 license->ErrorInfo);
2659 rdpLicense* license_new(rdpRdp* rdp)
2661 rdpLicense* license = NULL;
2664 license = (rdpLicense*)calloc(1,
sizeof(rdpLicense));
2668 license->PlatformId = PLATFORMID;
2669 license->ClientType = OTHER_PLATFORM_CHALLENGE_TYPE;
2670 license->LicenseDetailLevel = LICENSE_DETAIL_DETAIL;
2671 license->PreferredKeyExchangeAlg = KEY_EXCHANGE_ALG_RSA;
2674 license_set_state(license, LICENSE_STATE_INITIAL);
2675 if (!(license->certificate = freerdp_certificate_new()))
2677 if (!(license->ProductInfo = license_new_product_info()))
2679 if (!(license->ErrorInfo = license_new_binary_blob(BB_ERROR_BLOB)))
2681 if (!(license->LicenseInfo = license_new_binary_blob(BB_DATA_BLOB)))
2683 if (!(license->KeyExchangeList = license_new_binary_blob(BB_KEY_EXCHG_ALG_BLOB)))
2685 if (!(license->ServerCertificate = license_new_binary_blob(BB_CERTIFICATE_BLOB)))
2687 if (!(license->ClientUserName = license_new_binary_blob(BB_CLIENT_USER_NAME_BLOB)))
2689 if (!(license->ClientMachineName = license_new_binary_blob(BB_CLIENT_MACHINE_NAME_BLOB)))
2691 if (!(license->PlatformChallenge = license_new_binary_blob(BB_ANY_BLOB)))
2693 if (!(license->EncryptedPlatformChallenge = license_new_binary_blob(BB_ANY_BLOB)))
2695 if (!(license->EncryptedPlatformChallengeResponse =
2696 license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2698 if (!(license->EncryptedPremasterSecret = license_new_binary_blob(BB_ANY_BLOB)))
2700 if (!(license->EncryptedHardwareId = license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2702 if (!(license->EncryptedLicenseInfo = license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2704 if (!(license->ScopeList = license_new_scope_list()))
2707 license_generate_randoms(license);
2712 WINPR_PRAGMA_DIAG_PUSH
2713 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2714 license_free(license);
2715 WINPR_PRAGMA_DIAG_POP
2724 void license_free(rdpLicense* license)
2728 freerdp_certificate_free(license->certificate);
2729 license_free_product_info(license->ProductInfo);
2730 license_free_binary_blob(license->ErrorInfo);
2731 license_free_binary_blob(license->LicenseInfo);
2732 license_free_binary_blob(license->KeyExchangeList);
2733 license_free_binary_blob(license->ServerCertificate);
2734 license_free_binary_blob(license->ClientUserName);
2735 license_free_binary_blob(license->ClientMachineName);
2736 license_free_binary_blob(license->PlatformChallenge);
2737 license_free_binary_blob(license->EncryptedPlatformChallenge);
2738 license_free_binary_blob(license->EncryptedPlatformChallengeResponse);
2739 license_free_binary_blob(license->EncryptedPremasterSecret);
2740 license_free_binary_blob(license->EncryptedHardwareId);
2741 license_free_binary_blob(license->EncryptedLicenseInfo);
2742 license_free_scope_list(license->ScopeList);
2747 LICENSE_STATE license_get_state(
const rdpLicense* license)
2749 WINPR_ASSERT(license);
2750 return license->state;
2753 LICENSE_TYPE license_get_type(
const rdpLicense* license)
2755 WINPR_ASSERT(license);
2756 return license->type;
2759 BOOL license_set_state(rdpLicense* license, LICENSE_STATE state)
2761 WINPR_ASSERT(license);
2762 license->state = state;
2765 case LICENSE_STATE_COMPLETED:
2767 case LICENSE_STATE_ABORTED:
2769 license->type = LICENSE_TYPE_INVALID;
2776 const char* license_get_state_string(LICENSE_STATE state)
2780 case LICENSE_STATE_INITIAL:
2781 return "LICENSE_STATE_INITIAL";
2782 case LICENSE_STATE_CONFIGURED:
2783 return "LICENSE_STATE_CONFIGURED";
2784 case LICENSE_STATE_REQUEST:
2785 return "LICENSE_STATE_REQUEST";
2786 case LICENSE_STATE_NEW_REQUEST:
2787 return "LICENSE_STATE_NEW_REQUEST";
2788 case LICENSE_STATE_PLATFORM_CHALLENGE:
2789 return "LICENSE_STATE_PLATFORM_CHALLENGE";
2790 case LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE:
2791 return "LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE";
2792 case LICENSE_STATE_COMPLETED:
2793 return "LICENSE_STATE_COMPLETED";
2794 case LICENSE_STATE_ABORTED:
2795 return "LICENSE_STATE_ABORTED";
2797 return "LICENSE_STATE_UNKNOWN";
2801 BOOL license_server_send_request(rdpLicense* license)
2803 if (!license_ensure_state(license, LICENSE_STATE_CONFIGURED, LICENSE_REQUEST))
2805 if (!license_send_license_request_packet(license))
2807 return license_set_state(license, LICENSE_STATE_REQUEST);
2810 static BOOL license_set_string(
const char* what,
const char* value, BYTE** bdst, UINT32* dstLen)
2813 WINPR_ASSERT(value);
2815 WINPR_ASSERT(dstLen);
2825 *cnv.w = ConvertUtf8ToWCharAlloc(value, &len);
2826 if (!*cnv.w || (len > UINT32_MAX /
sizeof(WCHAR)))
2828 WLog_ERR(TAG,
"license->ProductInfo: %s == %p || %" PRIu32
" > UINT32_MAX", what, *cnv.w,
2832 *dstLen = (UINT32)(len *
sizeof(WCHAR));
2836 BOOL license_server_configure(rdpLicense* license)
2839 UINT32 algs[] = { KEY_EXCHANGE_ALG_RSA };
2841 WINPR_ASSERT(license);
2842 WINPR_ASSERT(license->rdp);
2844 const rdpSettings* settings = license->rdp->settings;
2846 const char* CompanyName =
2849 const char* ProductName =
2851 const UINT32 ProductVersion =
2853 const UINT32 issuerCount =
2857 const char** issuers = WINPR_REINTERPRET_CAST(ptr,
const void*,
const char**);
2859 WINPR_ASSERT(CompanyName);
2860 WINPR_ASSERT(ProductName);
2861 WINPR_ASSERT(ProductVersion > 0);
2862 WINPR_ASSERT(issuers || (issuerCount == 0));
2864 if (!license_ensure_state(license, LICENSE_STATE_INITIAL, LICENSE_REQUEST))
2867 license->ProductInfo->dwVersion = ProductVersion;
2868 if (!license_set_string(
"pbCompanyName", CompanyName, &license->ProductInfo->pbCompanyName,
2869 &license->ProductInfo->cbCompanyName))
2872 if (!license_set_string(
"pbProductId", ProductName, &license->ProductInfo->pbProductId,
2873 &license->ProductInfo->cbProductId))
2876 if (!license_read_binary_blob_data(license->KeyExchangeList, BB_KEY_EXCHG_ALG_BLOB, algs,
2880 if (!freerdp_certificate_read_server_cert(license->certificate, settings->ServerCertificate,
2881 settings->ServerCertificateLength))
2884 s = Stream_New(NULL, 1024);
2891 freerdp_certificate_write_server_cert(license->certificate, CERT_CHAIN_VERSION_2, s);
2893 r = license_read_binary_blob_data(license->ServerCertificate, BB_CERTIFICATE_BLOB,
2894 Stream_Buffer(s), Stream_GetPosition(s));
2896 Stream_Free(s, TRUE);
2901 if (!license_scope_list_resize(license->ScopeList, issuerCount))
2903 for (
size_t x = 0; x < issuerCount; x++)
2906 const char* name = issuers[x];
2907 const size_t length = strnlen(name, UINT16_MAX) + 1;
2908 if ((length == 0) || (length > UINT16_MAX))
2911 "%s: Invalid issuer at position %" PRIuz
": length 0 < %" PRIuz
" <= %" PRIu16
2913 x, length, UINT16_MAX, name);
2916 if (!license_read_binary_blob_data(blob, BB_SCOPE_BLOB, name, length))
2920 return license_set_state(license, LICENSE_STATE_CONFIGURED);
2923 rdpLicense* license_get(rdpContext* context)
2925 WINPR_ASSERT(context);
2926 WINPR_ASSERT(context->rdp);
2927 return context->rdp->license;
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API const void * freerdp_settings_get_pointer(const rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id)
Returns a immutable pointer settings value.
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.