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))
793 #ifdef WITH_DEBUG_LICENSE
794 WLog_DBG(TAG,
"Sending %s Packet, length %" PRIu16
"", license_request_type_string(type),
796 winpr_HexDump(TAG, WLOG_DEBUG, Stream_PointerAs(s,
char) - LICENSE_PREAMBLE_LENGTH, wMsgSize);
798 Stream_SetPosition(s, length);
799 const BOOL ret = rdp_send(rdp, s, MCS_GLOBAL_CHANNEL_ID);
804 BOOL license_read_server_upgrade_license(rdpLicense* license,
wStream* s)
806 WINPR_ASSERT(license);
808 if (!license_read_binary_blob(s, license->EncryptedLicenseInfo))
810 if (!license_check_stream_length(s,
sizeof(license->MACData),
811 "SERVER_UPGRADE_LICENSE::MACData"))
813 Stream_Read(s, license->MACData,
sizeof(license->MACData));
817 BOOL license_write_server_upgrade_license(
const rdpLicense* license,
wStream* s)
819 WINPR_ASSERT(license);
821 if (!license_write_binary_blob(s, license->EncryptedLicenseInfo))
823 if (!license_check_stream_capacity(s,
sizeof(license->MACData),
824 "SERVER_UPGRADE_LICENSE::MACData"))
826 Stream_Write(s, license->MACData,
sizeof(license->MACData));
830 static BOOL license_server_send_new_or_upgrade_license(rdpLicense* license, BOOL upgrade)
832 wStream* s = license_send_stream_init(license);
833 const BYTE type = upgrade ? UPGRADE_LICENSE : NEW_LICENSE;
838 if (!license_write_server_upgrade_license(license, s))
841 return license_send(license, s, type);
856 state_run_t license_client_recv(rdpLicense* license,
wStream* s)
861 const size_t length = Stream_GetRemainingLength(s);
863 WINPR_ASSERT(license);
865 if (!license_read_preamble(s, &bMsgType, &flags, &wMsgSize))
866 return STATE_RUN_FAILED;
868 DEBUG_LICENSE(
"Receiving %s Packet", license_request_type_string(bMsgType));
872 case LICENSE_REQUEST:
874 if (license_get_state(license) == LICENSE_STATE_INITIAL)
875 license_set_state(license, LICENSE_STATE_CONFIGURED);
877 if (!license_ensure_state(license, LICENSE_STATE_CONFIGURED, bMsgType))
878 return STATE_RUN_FAILED;
880 if (!license_read_license_request_packet(license, s))
881 return STATE_RUN_FAILED;
883 if (!license_answer_license_request(license))
884 return STATE_RUN_FAILED;
886 license_set_state(license, LICENSE_STATE_NEW_REQUEST);
889 case PLATFORM_CHALLENGE:
890 if (!license_ensure_state(license, LICENSE_STATE_NEW_REQUEST, bMsgType))
891 return STATE_RUN_FAILED;
893 if (!license_read_platform_challenge_packet(license, s))
894 return STATE_RUN_FAILED;
896 if (!license_send_platform_challenge_response(license))
897 return STATE_RUN_FAILED;
898 license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE);
902 case UPGRADE_LICENSE:
903 if (!license_ensure_state(license, LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE, bMsgType))
904 return STATE_RUN_FAILED;
905 if (!license_read_new_or_upgrade_license_packet(license, s))
906 return STATE_RUN_FAILED;
910 if (!license_read_error_alert_packet(license, s))
911 return STATE_RUN_FAILED;
915 WLog_ERR(TAG,
"invalid bMsgType:%" PRIu8
"", bMsgType);
916 return STATE_RUN_FAILED;
919 if (!tpkt_ensure_stream_consumed(s, length))
920 return STATE_RUN_FAILED;
921 return STATE_RUN_SUCCESS;
924 state_run_t license_server_recv(rdpLicense* license,
wStream* s)
926 state_run_t rc = STATE_RUN_FAILED;
930 const size_t length = Stream_GetRemainingLength(s);
932 WINPR_ASSERT(license);
934 if (!license_read_preamble(s, &bMsgType, &flags, &wMsgSize))
937 DEBUG_LICENSE(
"Receiving %s Packet", license_request_type_string(bMsgType));
941 case NEW_LICENSE_REQUEST:
942 if (!license_ensure_state(license, LICENSE_STATE_REQUEST, bMsgType))
944 if (!license_read_new_license_request_packet(license, s))
947 if (!license_send_error_alert(license, ERR_INVALID_MAC, ST_TOTAL_ABORT,
950 if (!license_send_platform_challenge_packet(license))
952 license->update = FALSE;
953 if (!license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE))
957 if (!license_ensure_state(license, LICENSE_STATE_REQUEST, bMsgType))
959 if (!license_read_license_info(license, s))
962 if (!license_send_platform_challenge_packet(license))
964 if (!license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE))
966 license->update = TRUE;
969 case PLATFORM_CHALLENGE_RESPONSE:
970 if (!license_ensure_state(license, LICENSE_STATE_PLATFORM_CHALLENGE, bMsgType))
972 if (!license_read_client_platform_challenge_response(license, s))
978 if (license_send_error_alert(license, ERR_INVALID_CLIENT, ST_TOTAL_ABORT,
984 if (!license_server_send_new_or_upgrade_license(license, license->update))
987 license->type = LICENSE_TYPE_ISSUED;
988 license_set_state(license, LICENSE_STATE_COMPLETED);
990 rc = STATE_RUN_CONTINUE;
995 if (!license_read_error_alert_packet(license, s))
1000 WLog_ERR(TAG,
"invalid bMsgType:%" PRIu8
"", bMsgType);
1004 if (!tpkt_ensure_stream_consumed(s, length))
1007 if (!state_run_success(rc))
1008 rc = STATE_RUN_SUCCESS;
1011 if (state_run_failed(rc))
1013 if (flags & EXTENDED_ERROR_MSG_SUPPORTED)
1014 license_send_error_alert(license, ERR_INVALID_CLIENT, ST_TOTAL_ABORT, NULL);
1015 license_set_state(license, LICENSE_STATE_ABORTED);
1021 void license_generate_randoms(rdpLicense* license)
1023 WINPR_ASSERT(license);
1025 #ifdef LICENSE_NULL_CLIENT_RANDOM
1026 ZeroMemory(license->ClientRandom,
sizeof(license->ClientRandom));
1028 winpr_RAND(license->ClientRandom,
sizeof(license->ClientRandom));
1031 winpr_RAND(license->ServerRandom,
sizeof(license->ServerRandom));
1033 #ifdef LICENSE_NULL_PREMASTER_SECRET
1034 ZeroMemory(license->PremasterSecret,
sizeof(license->PremasterSecret));
1036 winpr_RAND(license->PremasterSecret,
sizeof(license->PremasterSecret));
1045 static BOOL license_generate_keys(rdpLicense* license)
1047 WINPR_ASSERT(license);
1051 !security_master_secret(license->PremasterSecret,
sizeof(license->PremasterSecret),
1052 license->ClientRandom,
sizeof(license->ClientRandom),
1053 license->ServerRandom,
sizeof(license->ServerRandom),
1054 license->MasterSecret,
sizeof(license->MasterSecret)) ||
1056 !security_session_key_blob(license->MasterSecret,
sizeof(license->MasterSecret),
1057 license->ClientRandom,
sizeof(license->ClientRandom),
1058 license->ServerRandom,
sizeof(license->ServerRandom),
1059 license->SessionKeyBlob,
sizeof(license->SessionKeyBlob)))
1063 security_mac_salt_key(license->SessionKeyBlob,
sizeof(license->SessionKeyBlob),
1064 license->ClientRandom,
sizeof(license->ClientRandom),
1065 license->ServerRandom,
sizeof(license->ServerRandom), license->MacSaltKey,
1066 sizeof(license->MacSaltKey));
1067 const BOOL ret = security_licensing_encryption_key(
1068 license->SessionKeyBlob,
sizeof(license->SessionKeyBlob), license->ClientRandom,
1069 sizeof(license->ClientRandom), license->ServerRandom,
sizeof(license->ServerRandom),
1070 license->LicensingEncryptionKey,
1071 sizeof(license->LicensingEncryptionKey));
1073 #ifdef WITH_DEBUG_LICENSE
1074 WLog_DBG(TAG,
"ClientRandom:");
1075 winpr_HexDump(TAG, WLOG_DEBUG, license->ClientRandom,
sizeof(license->ClientRandom));
1076 WLog_DBG(TAG,
"ServerRandom:");
1077 winpr_HexDump(TAG, WLOG_DEBUG, license->ServerRandom,
sizeof(license->ServerRandom));
1078 WLog_DBG(TAG,
"PremasterSecret:");
1079 winpr_HexDump(TAG, WLOG_DEBUG, license->PremasterSecret,
sizeof(license->PremasterSecret));
1080 WLog_DBG(TAG,
"MasterSecret:");
1081 winpr_HexDump(TAG, WLOG_DEBUG, license->MasterSecret,
sizeof(license->MasterSecret));
1082 WLog_DBG(TAG,
"SessionKeyBlob:");
1083 winpr_HexDump(TAG, WLOG_DEBUG, license->SessionKeyBlob,
sizeof(license->SessionKeyBlob));
1084 WLog_DBG(TAG,
"MacSaltKey:");
1085 winpr_HexDump(TAG, WLOG_DEBUG, license->MacSaltKey,
sizeof(license->MacSaltKey));
1086 WLog_DBG(TAG,
"LicensingEncryptionKey:");
1087 winpr_HexDump(TAG, WLOG_DEBUG, license->LicensingEncryptionKey,
1088 sizeof(license->LicensingEncryptionKey));
1098 BOOL license_generate_hwid(rdpLicense* license)
1100 const BYTE* hashTarget = NULL;
1101 size_t targetLen = 0;
1102 BYTE macAddress[6] = { 0 };
1104 WINPR_ASSERT(license);
1105 WINPR_ASSERT(license->rdp);
1106 WINPR_ASSERT(license->rdp->settings);
1108 ZeroMemory(license->HardwareId,
sizeof(license->HardwareId));
1110 if (license->rdp->settings->OldLicenseBehaviour)
1112 hashTarget = macAddress;
1113 targetLen =
sizeof(macAddress);
1118 const char* hostname = license->rdp->settings->ClientHostname;
1119 wStream* s = Stream_StaticInit(&buffer, license->HardwareId, 4);
1120 Stream_Write_UINT32(s, license->PlatformId);
1122 hashTarget = (
const BYTE*)hostname;
1123 targetLen = hostname ? strlen(hostname) : 0;
1132 return winpr_Digest_Allow_FIPS(WINPR_MD_MD5, hashTarget, targetLen,
1133 &license->HardwareId[HWID_PLATFORM_ID_LENGTH],
1134 WINPR_MD5_DIGEST_LENGTH);
1137 static BOOL license_get_server_rsa_public_key(rdpLicense* license)
1139 rdpSettings* settings = NULL;
1141 WINPR_ASSERT(license);
1142 WINPR_ASSERT(license->certificate);
1143 WINPR_ASSERT(license->rdp);
1145 settings = license->rdp->settings;
1146 WINPR_ASSERT(settings);
1148 if (license->ServerCertificate->length < 1)
1150 if (!freerdp_certificate_read_server_cert(license->certificate, settings->ServerCertificate,
1151 settings->ServerCertificateLength))
1158 BOOL license_encrypt_premaster_secret(rdpLicense* license)
1160 WINPR_ASSERT(license);
1161 WINPR_ASSERT(license->certificate);
1163 if (!license_get_server_rsa_public_key(license))
1166 WINPR_ASSERT(license->EncryptedPremasterSecret);
1168 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1171 WLog_ERR(TAG,
"info=%p, license->certificate=%p", info, license->certificate);
1175 #ifdef WITH_DEBUG_LICENSE
1176 WLog_DBG(TAG,
"Modulus (%" PRIu32
" bits):", info->ModulusLength * 8);
1177 winpr_HexDump(TAG, WLOG_DEBUG, info->Modulus, info->ModulusLength);
1178 WLog_DBG(TAG,
"Exponent:");
1179 winpr_HexDump(TAG, WLOG_DEBUG, info->exponent,
sizeof(info->exponent));
1182 BYTE* EncryptedPremasterSecret = (BYTE*)calloc(1, info->ModulusLength);
1183 if (!EncryptedPremasterSecret)
1185 WLog_ERR(TAG,
"EncryptedPremasterSecret=%p, info->ModulusLength=%" PRIu32,
1186 EncryptedPremasterSecret, info->ModulusLength);
1190 license->EncryptedPremasterSecret->type = BB_RANDOM_BLOB;
1191 license->EncryptedPremasterSecret->length =
sizeof(license->PremasterSecret);
1192 #ifndef LICENSE_NULL_PREMASTER_SECRET
1194 const SSIZE_T length =
1195 crypto_rsa_public_encrypt(license->PremasterSecret,
sizeof(license->PremasterSecret),
1196 info, EncryptedPremasterSecret, info->ModulusLength);
1197 if ((length < 0) || (length > UINT16_MAX))
1199 WLog_ERR(TAG,
"RSA public encrypt length=%" PRIdz
" < 0 || > %" PRIu16, length,
1203 license->EncryptedPremasterSecret->length = (UINT16)length;
1206 license->EncryptedPremasterSecret->data = EncryptedPremasterSecret;
1210 static BOOL license_rc4_with_licenseKey(
const rdpLicense* license,
const BYTE* input,
size_t len,
1213 WINPR_ASSERT(license);
1214 WINPR_ASSERT(input || (len == 0));
1215 WINPR_ASSERT(target);
1216 WINPR_ASSERT(len <= UINT16_MAX);
1218 WINPR_RC4_CTX* rc4 = winpr_RC4_New_Allow_FIPS(license->LicensingEncryptionKey,
1219 sizeof(license->LicensingEncryptionKey));
1222 WLog_ERR(TAG,
"Failed to allocate RC4");
1226 BYTE* buffer = NULL;
1228 buffer = realloc(target->data, len);
1232 target->data = buffer;
1233 target->length = (UINT16)len;
1235 if (!winpr_RC4_Update(rc4, len, input, buffer))
1238 winpr_RC4_Free(rc4);
1242 WLog_ERR(TAG,
"Failed to create/update RC4: len=%" PRIuz
", buffer=%p", len, buffer);
1243 winpr_RC4_Free(rc4);
1257 static BOOL license_encrypt_and_MAC(rdpLicense* license,
const BYTE* input,
size_t len,
1260 WINPR_ASSERT(license);
1261 return license_rc4_with_licenseKey(license, input, len, target) &&
1262 security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), input, len, mac,
1277 static BOOL license_decrypt_and_check_MAC(rdpLicense* license,
const BYTE* input,
size_t len,
1280 BYTE macData[
sizeof(license->MACData)] = { 0 };
1282 WINPR_ASSERT(license);
1283 WINPR_ASSERT(target);
1287 WLog_DBG(TAG,
"TransportDumpReplay active, skipping...");
1291 if (!license_rc4_with_licenseKey(license, input, len, target))
1294 if (!security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), target->data, len,
1295 macData,
sizeof(macData)))
1298 if (memcmp(packetMac, macData,
sizeof(macData)) != 0)
1300 WLog_ERR(TAG,
"packetMac != expectedMac");
1315 WINPR_ASSERT(productInfo);
1317 if (!license_check_stream_length(s, 8,
"license product info::cbCompanyName"))
1320 Stream_Read_UINT32(s, productInfo->dwVersion);
1321 Stream_Read_UINT32(s, productInfo->cbCompanyName);
1324 if ((productInfo->cbCompanyName < 2) || (productInfo->cbCompanyName % 2 != 0))
1326 WLog_WARN(TAG,
"license product info invalid cbCompanyName %" PRIu32,
1327 productInfo->cbCompanyName);
1331 if (!license_check_stream_length(s, productInfo->cbCompanyName,
1332 "license product info::CompanyName"))
1335 productInfo->pbProductId = NULL;
1336 productInfo->pbCompanyName = (BYTE*)malloc(productInfo->cbCompanyName);
1337 if (!productInfo->pbCompanyName)
1339 Stream_Read(s, productInfo->pbCompanyName, productInfo->cbCompanyName);
1341 if (!license_check_stream_length(s, 4,
"license product info::cbProductId"))
1344 Stream_Read_UINT32(s, productInfo->cbProductId);
1346 if ((productInfo->cbProductId < 2) || (productInfo->cbProductId % 2 != 0))
1348 WLog_WARN(TAG,
"license product info invalid cbProductId %" PRIu32,
1349 productInfo->cbProductId);
1353 if (!license_check_stream_length(s, productInfo->cbProductId,
1354 "license product info::ProductId"))
1357 productInfo->pbProductId = (BYTE*)malloc(productInfo->cbProductId);
1358 if (!productInfo->pbProductId)
1360 Stream_Read(s, productInfo->pbProductId, productInfo->cbProductId);
1364 free(productInfo->pbCompanyName);
1365 free(productInfo->pbProductId);
1366 productInfo->pbCompanyName = NULL;
1367 productInfo->pbProductId = NULL;
1373 WINPR_ASSERT(productInfo);
1375 if (!license_check_stream_capacity(s, 8,
"license product info::cbCompanyName"))
1378 Stream_Write_UINT32(s, productInfo->dwVersion);
1379 Stream_Write_UINT32(s, productInfo->cbCompanyName);
1382 if ((productInfo->cbCompanyName < 2) || (productInfo->cbCompanyName % 2 != 0) ||
1383 !productInfo->pbCompanyName)
1385 WLog_WARN(TAG,
"license product info invalid cbCompanyName %" PRIu32,
1386 productInfo->cbCompanyName);
1390 if (!license_check_stream_capacity(s, productInfo->cbCompanyName,
1391 "license product info::CompanyName"))
1394 Stream_Write(s, productInfo->pbCompanyName, productInfo->cbCompanyName);
1396 if (!license_check_stream_capacity(s, 4,
"license product info::cbProductId"))
1399 Stream_Write_UINT32(s, productInfo->cbProductId);
1401 if ((productInfo->cbProductId < 2) || (productInfo->cbProductId % 2 != 0) ||
1402 !productInfo->pbProductId)
1404 WLog_WARN(TAG,
"license product info invalid cbProductId %" PRIu32,
1405 productInfo->cbProductId);
1409 if (!license_check_stream_capacity(s, productInfo->cbProductId,
1410 "license product info::ProductId"))
1413 Stream_Write(s, productInfo->pbProductId, productInfo->cbProductId);
1442 free(productInfo->pbCompanyName);
1443 free(productInfo->pbProductId);
1448 BOOL license_read_binary_blob_data(
LICENSE_BLOB* blob, UINT16 wBlobType,
const void* data,
1452 WINPR_ASSERT(length <= UINT16_MAX);
1453 WINPR_ASSERT(data || (length == 0));
1455 blob->length = (UINT16)length;
1459 if ((blob->type != wBlobType) && (blob->type != BB_ANY_BLOB))
1461 WLog_ERR(TAG,
"license binary blob::type expected %s, got %s",
1462 licencse_blob_type_string(wBlobType), licencse_blob_type_string(blob->type));
1469 if ((blob->type != BB_ANY_BLOB) && (blob->length == 0))
1471 WLog_WARN(TAG,
"license binary blob::type %s, length=0, skipping.",
1472 licencse_blob_type_string(blob->type));
1476 blob->type = wBlobType;
1478 if (blob->length > 0)
1479 blob->data = malloc(blob->length);
1482 WLog_ERR(TAG,
"license binary blob::length=%" PRIu16
", blob::data=%p", blob->length,
1486 memcpy(blob->data, data, blob->length);
1499 UINT16 wBlobType = 0;
1504 if (!license_check_stream_length(s, 4,
"license binary blob::type"))
1507 Stream_Read_UINT16(s, wBlobType);
1508 Stream_Read_UINT16(s, length);
1510 if (!license_check_stream_length(s, length,
"license binary blob::length"))
1513 if (!license_read_binary_blob_data(blob, wBlobType, Stream_Pointer(s), length))
1516 return Stream_SafeSeek(s, length);
1530 if (!Stream_EnsureRemainingCapacity(s, blob->length + 4))
1533 Stream_Write_UINT16(s, blob->type);
1534 Stream_Write_UINT16(s, blob->length);
1536 if (blob->length > 0)
1537 Stream_Write(s, blob->data, blob->length);
1541 static BOOL license_write_encrypted_premaster_secret_blob(
wStream* s,
const LICENSE_BLOB* blob,
1542 UINT32 ModulusLength)
1544 const UINT32 length = ModulusLength + 8;
1547 WINPR_ASSERT(length <= UINT16_MAX);
1549 if (blob->length > ModulusLength)
1551 WLog_ERR(TAG,
"invalid blob");
1555 if (!Stream_EnsureRemainingCapacity(s, length + 4))
1557 Stream_Write_UINT16(s, blob->type);
1558 Stream_Write_UINT16(s, (UINT16)length);
1560 if (blob->length > 0)
1561 Stream_Write(s, blob->data, blob->length);
1563 Stream_Zero(s, length - blob->length);
1568 UINT32* ModulusLength)
1570 if (!license_read_binary_blob(s, blob))
1572 WINPR_ASSERT(ModulusLength);
1573 *ModulusLength = blob->length;
1615 UINT32 scopeCount = 0;
1617 WINPR_ASSERT(scopeList);
1619 if (!license_check_stream_length(s, 4,
"license scope list"))
1622 Stream_Read_UINT32(s, scopeCount);
1624 if (!license_check_stream_length(s, 4ll * scopeCount,
"license scope list::count"))
1627 if (!license_scope_list_resize(scopeList, scopeCount))
1630 for (UINT32 i = 0; i < scopeCount; i++)
1632 if (!license_read_binary_blob(s, scopeList->array[i]))
1641 WINPR_ASSERT(scopeList);
1643 if (!license_check_stream_capacity(s, 4,
"license scope list"))
1646 Stream_Write_UINT32(s, scopeList->count);
1648 if (!license_check_stream_capacity(s, scopeList->count * 4ull,
"license scope list::count"))
1652 WINPR_ASSERT(scopeList->array || (scopeList->count == 0));
1653 for (UINT32 i = 0; i < scopeList->count; i++)
1657 if (!license_write_binary_blob(s, element))
1676 BOOL license_scope_list_resize(
SCOPE_LIST* scopeList, UINT32 count)
1678 WINPR_ASSERT(scopeList);
1679 WINPR_ASSERT(scopeList->array || (scopeList->count == 0));
1681 for (UINT32 x = count; x < scopeList->count; x++)
1683 license_free_binary_blob(scopeList->array[x]);
1684 scopeList->array[x] = NULL;
1693 scopeList->array = tmp;
1697 free((
void*)scopeList->array);
1698 scopeList->array = NULL;
1701 for (UINT32 x = scopeList->count; x < count; x++)
1703 LICENSE_BLOB* blob = license_new_binary_blob(BB_SCOPE_BLOB);
1706 scopeList->count = x;
1709 scopeList->array[x] = blob;
1712 scopeList->count = count;
1722 void license_free_scope_list(
SCOPE_LIST* scopeList)
1727 license_scope_list_resize(scopeList, 0);
1731 BOOL license_send_license_info(rdpLicense* license,
const LICENSE_BLOB* calBlob,
1732 const BYTE* signature,
size_t signature_length)
1734 WINPR_ASSERT(calBlob);
1735 WINPR_ASSERT(signature);
1736 WINPR_ASSERT(license->certificate);
1738 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1742 wStream* s = license_send_stream_init(license);
1746 if (!license_check_stream_capacity(s, 8 +
sizeof(license->ClientRandom),
1747 "license info::ClientRandom"))
1750 Stream_Write_UINT32(s,
1751 license->PreferredKeyExchangeAlg);
1752 Stream_Write_UINT32(s, license->PlatformId);
1755 Stream_Write(s, license->ClientRandom,
sizeof(license->ClientRandom));
1758 if (!license_write_encrypted_premaster_secret_blob(s, license->EncryptedPremasterSecret,
1759 info->ModulusLength))
1763 if (!license_write_binary_blob(s, calBlob))
1767 if (!license_write_binary_blob(s, license->EncryptedHardwareId))
1771 if (!license_check_stream_capacity(s, signature_length,
"license info::MACData"))
1773 Stream_Write(s, signature, signature_length);
1775 return license_send(license, s, LICENSE_INFO);
1782 static BOOL license_check_preferred_alg(rdpLicense* license, UINT32 PreferredKeyExchangeAlg,
1785 WINPR_ASSERT(license);
1786 WINPR_ASSERT(where);
1788 if (license->PreferredKeyExchangeAlg != PreferredKeyExchangeAlg)
1790 char buffer1[64] = { 0 };
1791 char buffer2[64] = { 0 };
1792 WLog_WARN(TAG,
"%s::PreferredKeyExchangeAlg, expected %s, got %s", where,
1793 license_preferred_key_exchange_alg_string(license->PreferredKeyExchangeAlg,
1794 buffer1,
sizeof(buffer1)),
1795 license_preferred_key_exchange_alg_string(PreferredKeyExchangeAlg, buffer2,
1802 BOOL license_read_license_info(rdpLicense* license,
wStream* s)
1805 UINT32 PreferredKeyExchangeAlg = 0;
1807 WINPR_ASSERT(license);
1808 WINPR_ASSERT(license->certificate);
1810 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1815 if (!license_check_stream_length(s, 8 +
sizeof(license->ClientRandom),
"license info"))
1818 Stream_Read_UINT32(s, PreferredKeyExchangeAlg);
1819 if (!license_check_preferred_alg(license, PreferredKeyExchangeAlg,
"license info"))
1821 Stream_Read_UINT32(s, license->PlatformId);
1824 Stream_Read(s, license->ClientRandom,
sizeof(license->ClientRandom));
1827 UINT32 ModulusLength = 0;
1828 if (!license_read_encrypted_premaster_secret_blob(s, license->EncryptedPremasterSecret,
1832 if (ModulusLength != info->ModulusLength)
1835 "EncryptedPremasterSecret,::ModulusLength[%" PRIu32
1836 "] != rdpCertInfo::ModulusLength[%" PRIu32
"]",
1837 ModulusLength, info->ModulusLength);
1841 if (!license_read_binary_blob(s, license->LicenseInfo))
1845 if (!license_read_binary_blob(s, license->EncryptedHardwareId))
1849 if (!license_check_stream_length(s,
sizeof(license->MACData),
"license info::MACData"))
1851 Stream_Read(s, license->MACData,
sizeof(license->MACData));
1866 BOOL license_read_license_request_packet(rdpLicense* license,
wStream* s)
1868 WINPR_ASSERT(license);
1871 if (!license_check_stream_length(s,
sizeof(license->ServerRandom),
"license request"))
1874 Stream_Read(s, license->ServerRandom,
sizeof(license->ServerRandom));
1877 if (!license_read_product_info(s, license->ProductInfo))
1881 if (!license_read_binary_blob(s, license->KeyExchangeList))
1885 if (!license_read_binary_blob(s, license->ServerCertificate))
1889 if (!license_read_scope_list(s, license->ScopeList))
1893 if (!freerdp_certificate_read_server_cert(license->certificate,
1894 license->ServerCertificate->data,
1895 license->ServerCertificate->length))
1898 if (!license_generate_keys(license) || !license_generate_hwid(license) ||
1899 !license_encrypt_premaster_secret(license))
1902 #ifdef WITH_DEBUG_LICENSE
1903 WLog_DBG(TAG,
"ServerRandom:");
1904 winpr_HexDump(TAG, WLOG_DEBUG, license->ServerRandom,
sizeof(license->ServerRandom));
1905 license_print_product_info(license->ProductInfo);
1906 license_print_scope_list(license->ScopeList);
1911 BOOL license_write_license_request_packet(
const rdpLicense* license,
wStream* s)
1913 WINPR_ASSERT(license);
1916 if (!license_check_stream_capacity(s,
sizeof(license->ServerRandom),
"license request"))
1918 Stream_Write(s, license->ServerRandom,
sizeof(license->ServerRandom));
1921 if (!license_write_product_info(s, license->ProductInfo))
1925 if (!license_write_binary_blob(s, license->KeyExchangeList))
1929 if (!license_write_binary_blob(s, license->ServerCertificate))
1933 if (!license_write_scope_list(s, license->ScopeList))
1939 static BOOL license_send_license_request_packet(rdpLicense* license)
1941 wStream* s = license_send_stream_init(license);
1945 if (!license_write_license_request_packet(license, s))
1948 return license_send(license, s, LICENSE_REQUEST);
1962 BOOL license_read_platform_challenge_packet(rdpLicense* license,
wStream* s)
1964 BYTE macData[LICENSING_ENCRYPTION_KEY_LENGTH] = { 0 };
1965 UINT32 ConnectFlags = 0;
1967 WINPR_ASSERT(license);
1969 DEBUG_LICENSE(
"Receiving Platform Challenge Packet");
1971 if (!license_check_stream_length(s, 4,
"license platform challenge"))
1974 Stream_Read_UINT32(s, ConnectFlags);
1977 license->EncryptedPlatformChallenge->type = BB_ANY_BLOB;
1978 if (!license_read_binary_blob(s, license->EncryptedPlatformChallenge))
1980 license->EncryptedPlatformChallenge->type = BB_ENCRYPTED_DATA_BLOB;
1983 if (!license_check_stream_length(s,
sizeof(macData),
"license platform challenge::MAC"))
1986 Stream_Read(s, macData,
sizeof(macData));
1987 if (!license_decrypt_and_check_MAC(license, license->EncryptedPlatformChallenge->data,
1988 license->EncryptedPlatformChallenge->length,
1989 license->PlatformChallenge, macData))
1992 #ifdef WITH_DEBUG_LICENSE
1993 WLog_DBG(TAG,
"ConnectFlags: 0x%08" PRIX32
"", ConnectFlags);
1994 WLog_DBG(TAG,
"EncryptedPlatformChallenge:");
1995 winpr_HexDump(TAG, WLOG_DEBUG, license->EncryptedPlatformChallenge->data,
1996 license->EncryptedPlatformChallenge->length);
1997 WLog_DBG(TAG,
"PlatformChallenge:");
1998 winpr_HexDump(TAG, WLOG_DEBUG, license->PlatformChallenge->data,
1999 license->PlatformChallenge->length);
2000 WLog_DBG(TAG,
"MacData:");
2001 winpr_HexDump(TAG, WLOG_DEBUG, macData,
sizeof(macData));
2006 BOOL license_send_error_alert(rdpLicense* license, UINT32 dwErrorCode, UINT32 dwStateTransition,
2009 wStream* s = license_send_stream_init(license);
2014 if (!license_check_stream_capacity(s, 8,
"license error alert"))
2016 Stream_Write_UINT32(s, dwErrorCode);
2017 Stream_Write_UINT32(s, dwStateTransition);
2021 if (!license_write_binary_blob(s, info))
2025 return license_send(license, s, ERROR_ALERT);
2031 BOOL license_send_platform_challenge_packet(rdpLicense* license)
2033 wStream* s = license_send_stream_init(license);
2038 DEBUG_LICENSE(
"Receiving Platform Challenge Packet");
2040 if (!license_check_stream_capacity(s, 4,
"license platform challenge"))
2046 if (!license_write_binary_blob(s, license->EncryptedPlatformChallenge))
2050 if (!license_check_stream_length(s,
sizeof(license->MACData),
2051 "license platform challenge::MAC"))
2054 Stream_Write(s, license->MACData,
sizeof(license->MACData));
2056 return license_send(license, s, PLATFORM_CHALLENGE);
2062 static BOOL license_read_encrypted_blob(
const rdpLicense* license,
wStream* s,
LICENSE_BLOB* target)
2064 UINT16 wBlobType = 0;
2065 UINT16 wBlobLen = 0;
2067 WINPR_ASSERT(license);
2068 WINPR_ASSERT(target);
2070 if (!license_check_stream_length(s, 4,
"license encrypted blob"))
2073 Stream_Read_UINT16(s, wBlobType);
2074 if (wBlobType != BB_ENCRYPTED_DATA_BLOB)
2078 "expecting BB_ENCRYPTED_DATA_BLOB blob, probably a windows 2003 server, continuing...");
2081 Stream_Read_UINT16(s, wBlobLen);
2083 BYTE* encryptedData = Stream_Pointer(s);
2084 if (!Stream_SafeSeek(s, wBlobLen))
2087 "short license encrypted blob::length, expected %" PRIu16
" bytes, got %" PRIuz,
2088 wBlobLen, Stream_GetRemainingLength(s));
2092 return license_rc4_with_licenseKey(license, encryptedData, wBlobLen, target);
2102 BOOL license_read_new_or_upgrade_license_packet(rdpLicense* license,
wStream* s)
2104 UINT32 os_major = 0;
2105 UINT32 os_minor = 0;
2107 UINT32 cbCompanyName = 0;
2108 UINT32 cbProductId = 0;
2109 UINT32 cbLicenseInfo = 0;
2111 wStream* licenseStream = NULL;
2113 BYTE computedMac[16] = { 0 };
2114 const BYTE* readMac = NULL;
2116 WINPR_ASSERT(license);
2118 DEBUG_LICENSE(
"Receiving Server New/Upgrade License Packet");
2120 LICENSE_BLOB* calBlob = license_new_binary_blob(BB_DATA_BLOB);
2125 if (!license_read_encrypted_blob(license, s, calBlob))
2129 readMac = Stream_Pointer(s);
2130 if (!Stream_SafeSeek(s,
sizeof(computedMac)))
2132 WLog_WARN(TAG,
"short license new/upgrade, expected 16 bytes, got %" PRIuz,
2133 Stream_GetRemainingLength(s));
2137 if (!security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), calBlob->data,
2138 calBlob->length, computedMac,
sizeof(computedMac)))
2141 if (memcmp(computedMac, readMac,
sizeof(computedMac)) != 0)
2143 WLog_ERR(TAG,
"new or upgrade license MAC mismatch");
2147 licenseStream = Stream_StaticConstInit(&sbuffer, calBlob->data, calBlob->length);
2150 WLog_ERR(TAG,
"license::blob::data=%p, license::blob::length=%" PRIu16, calBlob->data,
2155 if (!license_check_stream_length(licenseStream, 8,
"license new/upgrade::blob::version"))
2158 Stream_Read_UINT16(licenseStream, os_minor);
2159 Stream_Read_UINT16(licenseStream, os_major);
2161 WLog_DBG(TAG,
"Version: %" PRIu16
".%" PRIu16, os_major, os_minor);
2164 Stream_Read_UINT32(licenseStream, cbScope);
2165 if (!license_check_stream_length(licenseStream, cbScope,
"license new/upgrade::blob::scope"))
2168 #ifdef WITH_DEBUG_LICENSE
2169 WLog_DBG(TAG,
"Scope:");
2170 winpr_HexDump(TAG, WLOG_DEBUG, Stream_Pointer(licenseStream), cbScope);
2172 Stream_Seek(licenseStream, cbScope);
2175 if (!license_check_stream_length(licenseStream, 4,
"license new/upgrade::blob::cbCompanyName"))
2178 Stream_Read_UINT32(licenseStream, cbCompanyName);
2179 if (!license_check_stream_length(licenseStream, cbCompanyName,
2180 "license new/upgrade::blob::CompanyName"))
2183 #ifdef WITH_DEBUG_LICENSE
2184 WLog_DBG(TAG,
"Company name:");
2185 winpr_HexDump(TAG, WLOG_DEBUG, Stream_Pointer(licenseStream), cbCompanyName);
2187 Stream_Seek(licenseStream, cbCompanyName);
2190 if (!license_check_stream_length(licenseStream, 4,
"license new/upgrade::blob::cbProductId"))
2193 Stream_Read_UINT32(licenseStream, cbProductId);
2195 if (!license_check_stream_length(licenseStream, cbProductId,
2196 "license new/upgrade::blob::ProductId"))
2199 #ifdef WITH_DEBUG_LICENSE
2200 WLog_DBG(TAG,
"Product id:");
2201 winpr_HexDump(TAG, WLOG_DEBUG, Stream_Pointer(licenseStream), cbProductId);
2203 Stream_Seek(licenseStream, cbProductId);
2206 if (!license_check_stream_length(licenseStream, 4,
"license new/upgrade::blob::cbLicenseInfo"))
2209 Stream_Read_UINT32(licenseStream, cbLicenseInfo);
2210 if (!license_check_stream_length(licenseStream, cbLicenseInfo,
2211 "license new/upgrade::blob::LicenseInfo"))
2214 license->type = LICENSE_TYPE_ISSUED;
2215 ret = license_set_state(license, LICENSE_STATE_COMPLETED);
2217 if (!license->rdp->settings->OldLicenseBehaviour)
2218 ret = saveCal(license->rdp->settings, Stream_Pointer(licenseStream), cbLicenseInfo,
2219 license->rdp->settings->ClientHostname);
2222 license_free_binary_blob(calBlob);
2233 BOOL license_read_error_alert_packet(rdpLicense* license,
wStream* s)
2235 UINT32 dwErrorCode = 0;
2236 UINT32 dwStateTransition = 0;
2238 WINPR_ASSERT(license);
2239 WINPR_ASSERT(license->rdp);
2241 if (!license_check_stream_length(s, 8ul,
"error alert"))
2244 Stream_Read_UINT32(s, dwErrorCode);
2245 Stream_Read_UINT32(s, dwStateTransition);
2247 if (!license_read_binary_blob(s, license->ErrorInfo))
2250 #ifdef WITH_DEBUG_LICENSE
2251 WLog_DBG(TAG,
"dwErrorCode: %s, dwStateTransition: %s", error_codes[dwErrorCode],
2252 state_transitions[dwStateTransition]);
2255 if (dwErrorCode == STATUS_VALID_CLIENT)
2257 license->type = LICENSE_TYPE_NONE;
2258 return license_set_state(license, LICENSE_STATE_COMPLETED);
2261 switch (dwStateTransition)
2263 case ST_TOTAL_ABORT:
2264 license_set_state(license, LICENSE_STATE_ABORTED);
2266 case ST_NO_TRANSITION:
2267 license_set_state(license, LICENSE_STATE_COMPLETED);
2269 case ST_RESET_PHASE_TO_START:
2270 license_set_state(license, LICENSE_STATE_CONFIGURED);
2272 case ST_RESEND_LAST_MESSAGE:
2288 BOOL license_write_new_license_request_packet(
const rdpLicense* license,
wStream* s)
2290 WINPR_ASSERT(license);
2292 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
2296 if (!license_check_stream_capacity(s, 8 +
sizeof(license->ClientRandom),
"License Request"))
2299 Stream_Write_UINT32(s,
2300 license->PreferredKeyExchangeAlg);
2301 Stream_Write_UINT32(s, license->PlatformId);
2302 Stream_Write(s, license->ClientRandom,
2303 sizeof(license->ClientRandom));
2306 !license_write_encrypted_premaster_secret_blob(s, license->EncryptedPremasterSecret,
2307 info->ModulusLength) ||
2309 !license_write_binary_blob(s, license->ClientUserName) ||
2311 !license_write_binary_blob(s, license->ClientMachineName))
2316 #ifdef WITH_DEBUG_LICENSE
2317 WLog_DBG(TAG,
"PreferredKeyExchangeAlg: 0x%08" PRIX32
"", license->PreferredKeyExchangeAlg);
2318 WLog_DBG(TAG,
"ClientRandom:");
2319 winpr_HexDump(TAG, WLOG_DEBUG, license->ClientRandom,
sizeof(license->ClientRandom));
2320 WLog_DBG(TAG,
"EncryptedPremasterSecret");
2321 winpr_HexDump(TAG, WLOG_DEBUG, license->EncryptedPremasterSecret->data,
2322 license->EncryptedPremasterSecret->length);
2323 WLog_DBG(TAG,
"ClientUserName (%" PRIu16
"): %s", license->ClientUserName->length,
2324 (
char*)license->ClientUserName->data);
2325 WLog_DBG(TAG,
"ClientMachineName (%" PRIu16
"): %s", license->ClientMachineName->length,
2326 (
char*)license->ClientMachineName->data);
2331 BOOL license_read_new_license_request_packet(rdpLicense* license,
wStream* s)
2333 UINT32 PreferredKeyExchangeAlg = 0;
2335 WINPR_ASSERT(license);
2337 if (!license_check_stream_length(s, 8ull +
sizeof(license->ClientRandom),
2338 "new license request"))
2341 Stream_Read_UINT32(s, PreferredKeyExchangeAlg);
2342 if (!license_check_preferred_alg(license, PreferredKeyExchangeAlg,
"new license request"))
2345 Stream_Read_UINT32(s, license->PlatformId);
2346 Stream_Read(s, license->ClientRandom,
2347 sizeof(license->ClientRandom));
2350 UINT32 ModulusLength = 0;
2351 if (!license_read_encrypted_premaster_secret_blob(s, license->EncryptedPremasterSecret,
2355 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
2357 WLog_WARN(TAG,
"Missing license certificate, skipping ModulusLength checks");
2358 else if (ModulusLength != info->ModulusLength)
2361 "EncryptedPremasterSecret expected to be %" PRIu32
" bytes, but read %" PRIu32
2363 info->ModulusLength, ModulusLength);
2368 if (!license_read_binary_blob(s, license->ClientUserName))
2371 if (!license_read_binary_blob(s, license->ClientMachineName))
2383 BOOL license_answer_license_request(rdpLicense* license)
2386 BYTE* license_data = NULL;
2387 size_t license_size = 0;
2389 char* username = NULL;
2391 WINPR_ASSERT(license);
2392 WINPR_ASSERT(license->rdp);
2393 WINPR_ASSERT(license->rdp->settings);
2395 if (!license->rdp->settings->OldLicenseBehaviour)
2396 license_data = loadCalFile(license->rdp->settings, license->rdp->settings->ClientHostname,
2402 BYTE signature[LICENSING_ENCRYPTION_KEY_LENGTH] = { 0 };
2404 DEBUG_LICENSE(
"Sending Saved License Packet");
2406 WINPR_ASSERT(license->EncryptedHardwareId);
2407 license->EncryptedHardwareId->type = BB_ENCRYPTED_DATA_BLOB;
2408 if (!license_encrypt_and_MAC(license, license->HardwareId,
sizeof(license->HardwareId),
2409 license->EncryptedHardwareId, signature,
sizeof(signature)))
2415 calBlob = license_new_binary_blob(BB_DATA_BLOB);
2421 calBlob->data = license_data;
2422 WINPR_ASSERT(license_size <= UINT16_MAX);
2423 calBlob->length = (UINT16)license_size;
2425 status = license_send_license_info(license, calBlob, signature,
sizeof(signature));
2426 license_free_binary_blob(calBlob);
2431 DEBUG_LICENSE(
"Sending New License Packet");
2433 s = license_send_stream_init(license);
2436 if (license->rdp->settings->Username != NULL)
2437 username = license->rdp->settings->Username;
2439 username =
"username";
2442 WINPR_ASSERT(license->ClientUserName);
2443 const size_t len = strlen(username) + 1;
2444 WINPR_ASSERT(len <= UINT16_MAX);
2446 license->ClientUserName->data = (BYTE*)username;
2447 license->ClientUserName->length = (UINT16)len;
2451 WINPR_ASSERT(license->ClientMachineName);
2452 const size_t len = strlen(license->rdp->settings->ClientHostname) + 1;
2453 WINPR_ASSERT(len <= UINT16_MAX);
2455 license->ClientMachineName->data = (BYTE*)license->rdp->settings->ClientHostname;
2456 license->ClientMachineName->length = (UINT16)len;
2458 status = license_write_new_license_request_packet(license, s);
2460 WINPR_ASSERT(license->ClientUserName);
2461 license->ClientUserName->data = NULL;
2462 license->ClientUserName->length = 0;
2464 WINPR_ASSERT(license->ClientMachineName);
2465 license->ClientMachineName->data = NULL;
2466 license->ClientMachineName->length = 0;
2474 return license_send(license, s, NEW_LICENSE_REQUEST);
2483 BOOL license_send_platform_challenge_response(rdpLicense* license)
2485 wStream* challengeRespData = NULL;
2486 BYTE* buffer = NULL;
2489 WINPR_ASSERT(license);
2490 WINPR_ASSERT(license->PlatformChallenge);
2491 WINPR_ASSERT(license->MacSaltKey);
2492 WINPR_ASSERT(license->EncryptedPlatformChallenge);
2493 WINPR_ASSERT(license->EncryptedHardwareId);
2495 DEBUG_LICENSE(
"Sending Platform Challenge Response Packet");
2497 license->EncryptedPlatformChallenge->type = BB_DATA_BLOB;
2500 challengeRespData = Stream_New(NULL, 8 + license->PlatformChallenge->length);
2501 if (!challengeRespData)
2503 Stream_Write_UINT16(challengeRespData, PLATFORM_CHALLENGE_RESPONSE_VERSION);
2504 Stream_Write_UINT16(challengeRespData, license->ClientType);
2505 Stream_Write_UINT16(challengeRespData, license->LicenseDetailLevel);
2506 Stream_Write_UINT16(challengeRespData, license->PlatformChallenge->length);
2507 Stream_Write(challengeRespData, license->PlatformChallenge->data,
2508 license->PlatformChallenge->length);
2509 Stream_SealLength(challengeRespData);
2512 const size_t length = Stream_Length(challengeRespData) +
sizeof(license->HardwareId);
2513 buffer = (BYTE*)malloc(length);
2516 Stream_Free(challengeRespData, TRUE);
2520 CopyMemory(buffer, Stream_Buffer(challengeRespData), Stream_Length(challengeRespData));
2521 CopyMemory(&buffer[Stream_Length(challengeRespData)], license->HardwareId,
2522 sizeof(license->HardwareId));
2523 status = security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), buffer, length,
2524 license->MACData,
sizeof(license->MACData));
2529 Stream_Free(challengeRespData, TRUE);
2533 license->EncryptedHardwareId->type = BB_ENCRYPTED_DATA_BLOB;
2534 if (!license_rc4_with_licenseKey(license, license->HardwareId,
sizeof(license->HardwareId),
2535 license->EncryptedHardwareId))
2537 Stream_Free(challengeRespData, TRUE);
2541 status = license_rc4_with_licenseKey(license, Stream_Buffer(challengeRespData),
2542 Stream_Length(challengeRespData),
2543 license->EncryptedPlatformChallengeResponse);
2544 Stream_Free(challengeRespData, TRUE);
2548 #ifdef WITH_DEBUG_LICENSE
2549 WLog_DBG(TAG,
"LicensingEncryptionKey:");
2550 winpr_HexDump(TAG, WLOG_DEBUG, license->LicensingEncryptionKey, 16);
2551 WLog_DBG(TAG,
"HardwareId:");
2552 winpr_HexDump(TAG, WLOG_DEBUG, license->HardwareId,
sizeof(license->HardwareId));
2553 WLog_DBG(TAG,
"EncryptedHardwareId:");
2554 winpr_HexDump(TAG, WLOG_DEBUG, license->EncryptedHardwareId->data,
2555 license->EncryptedHardwareId->length);
2557 wStream* s = license_send_stream_init(license);
2561 if (license_write_client_platform_challenge_response(license, s))
2562 return license_send(license, s, PLATFORM_CHALLENGE_RESPONSE);
2568 BOOL license_read_platform_challenge_response(rdpLicense* license,
wStream* s)
2570 UINT16 wVersion = 0;
2571 UINT16 cbChallenge = 0;
2572 const BYTE* pbChallenge = NULL;
2574 WINPR_ASSERT(license);
2575 WINPR_ASSERT(license->PlatformChallenge);
2576 WINPR_ASSERT(license->MacSaltKey);
2577 WINPR_ASSERT(license->EncryptedPlatformChallenge);
2578 WINPR_ASSERT(license->EncryptedHardwareId);
2580 DEBUG_LICENSE(
"Receiving Platform Challenge Response Packet");
2582 if (!license_check_stream_length(s, 8,
"PLATFORM_CHALLENGE_RESPONSE_DATA"))
2585 Stream_Read_UINT16(s, wVersion);
2586 if (wVersion != PLATFORM_CHALLENGE_RESPONSE_VERSION)
2589 "Invalid PLATFORM_CHALLENGE_RESPONSE_DATA::wVersion 0x%04" PRIx16
2590 ", expected 0x04" PRIx16,
2591 wVersion, PLATFORM_CHALLENGE_RESPONSE_VERSION);
2594 Stream_Read_UINT16(s, license->ClientType);
2595 Stream_Read_UINT16(s, license->LicenseDetailLevel);
2596 Stream_Read_UINT16(s, cbChallenge);
2598 if (!license_check_stream_length(s, cbChallenge,
2599 "PLATFORM_CHALLENGE_RESPONSE_DATA::pbChallenge"))
2602 pbChallenge = Stream_Pointer(s);
2603 if (!license_read_binary_blob_data(license->EncryptedPlatformChallengeResponse, BB_DATA_BLOB,
2604 pbChallenge, cbChallenge))
2606 return Stream_SafeSeek(s, cbChallenge);
2609 BOOL license_write_client_platform_challenge_response(rdpLicense* license,
wStream* s)
2611 WINPR_ASSERT(license);
2613 if (!license_write_binary_blob(s, license->EncryptedPlatformChallengeResponse))
2615 if (!license_write_binary_blob(s, license->EncryptedHardwareId))
2617 if (!license_check_stream_capacity(s,
sizeof(license->MACData),
2618 "CLIENT_PLATFORM_CHALLENGE_RESPONSE::MACData"))
2620 Stream_Write(s, license->MACData,
sizeof(license->MACData));
2624 BOOL license_read_client_platform_challenge_response(rdpLicense* license,
wStream* s)
2626 WINPR_ASSERT(license);
2628 if (!license_read_binary_blob(s, license->EncryptedPlatformChallengeResponse))
2630 if (!license_read_binary_blob(s, license->EncryptedHardwareId))
2632 if (!license_check_stream_length(s,
sizeof(license->MACData),
2633 "CLIENT_PLATFORM_CHALLENGE_RESPONSE::MACData"))
2635 Stream_Read(s, license->MACData,
sizeof(license->MACData));
2648 BOOL license_send_valid_client_error_packet(rdpRdp* rdp)
2651 rdpLicense* license = rdp->license;
2652 WINPR_ASSERT(license);
2654 license->state = LICENSE_STATE_COMPLETED;
2655 license->type = LICENSE_TYPE_NONE;
2656 return license_send_error_alert(license, STATUS_VALID_CLIENT, ST_NO_TRANSITION,
2657 license->ErrorInfo);
2666 rdpLicense* license_new(rdpRdp* rdp)
2668 rdpLicense* license = NULL;
2671 license = (rdpLicense*)calloc(1,
sizeof(rdpLicense));
2675 license->PlatformId = PLATFORMID;
2676 license->ClientType = OTHER_PLATFORM_CHALLENGE_TYPE;
2677 license->LicenseDetailLevel = LICENSE_DETAIL_DETAIL;
2678 license->PreferredKeyExchangeAlg = KEY_EXCHANGE_ALG_RSA;
2681 license_set_state(license, LICENSE_STATE_INITIAL);
2682 if (!(license->certificate = freerdp_certificate_new()))
2684 if (!(license->ProductInfo = license_new_product_info()))
2686 if (!(license->ErrorInfo = license_new_binary_blob(BB_ERROR_BLOB)))
2688 if (!(license->LicenseInfo = license_new_binary_blob(BB_DATA_BLOB)))
2690 if (!(license->KeyExchangeList = license_new_binary_blob(BB_KEY_EXCHG_ALG_BLOB)))
2692 if (!(license->ServerCertificate = license_new_binary_blob(BB_CERTIFICATE_BLOB)))
2694 if (!(license->ClientUserName = license_new_binary_blob(BB_CLIENT_USER_NAME_BLOB)))
2696 if (!(license->ClientMachineName = license_new_binary_blob(BB_CLIENT_MACHINE_NAME_BLOB)))
2698 if (!(license->PlatformChallenge = license_new_binary_blob(BB_ANY_BLOB)))
2700 if (!(license->EncryptedPlatformChallenge = license_new_binary_blob(BB_ANY_BLOB)))
2702 if (!(license->EncryptedPlatformChallengeResponse =
2703 license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2705 if (!(license->EncryptedPremasterSecret = license_new_binary_blob(BB_ANY_BLOB)))
2707 if (!(license->EncryptedHardwareId = license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2709 if (!(license->EncryptedLicenseInfo = license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2711 if (!(license->ScopeList = license_new_scope_list()))
2714 license_generate_randoms(license);
2719 WINPR_PRAGMA_DIAG_PUSH
2720 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2721 license_free(license);
2722 WINPR_PRAGMA_DIAG_POP
2731 void license_free(rdpLicense* license)
2735 freerdp_certificate_free(license->certificate);
2736 license_free_product_info(license->ProductInfo);
2737 license_free_binary_blob(license->ErrorInfo);
2738 license_free_binary_blob(license->LicenseInfo);
2739 license_free_binary_blob(license->KeyExchangeList);
2740 license_free_binary_blob(license->ServerCertificate);
2741 license_free_binary_blob(license->ClientUserName);
2742 license_free_binary_blob(license->ClientMachineName);
2743 license_free_binary_blob(license->PlatformChallenge);
2744 license_free_binary_blob(license->EncryptedPlatformChallenge);
2745 license_free_binary_blob(license->EncryptedPlatformChallengeResponse);
2746 license_free_binary_blob(license->EncryptedPremasterSecret);
2747 license_free_binary_blob(license->EncryptedHardwareId);
2748 license_free_binary_blob(license->EncryptedLicenseInfo);
2749 license_free_scope_list(license->ScopeList);
2754 LICENSE_STATE license_get_state(
const rdpLicense* license)
2756 WINPR_ASSERT(license);
2757 return license->state;
2760 LICENSE_TYPE license_get_type(
const rdpLicense* license)
2762 WINPR_ASSERT(license);
2763 return license->type;
2766 BOOL license_set_state(rdpLicense* license, LICENSE_STATE state)
2768 WINPR_ASSERT(license);
2769 license->state = state;
2772 case LICENSE_STATE_COMPLETED:
2774 case LICENSE_STATE_ABORTED:
2776 license->type = LICENSE_TYPE_INVALID;
2783 const char* license_get_state_string(LICENSE_STATE state)
2787 case LICENSE_STATE_INITIAL:
2788 return "LICENSE_STATE_INITIAL";
2789 case LICENSE_STATE_CONFIGURED:
2790 return "LICENSE_STATE_CONFIGURED";
2791 case LICENSE_STATE_REQUEST:
2792 return "LICENSE_STATE_REQUEST";
2793 case LICENSE_STATE_NEW_REQUEST:
2794 return "LICENSE_STATE_NEW_REQUEST";
2795 case LICENSE_STATE_PLATFORM_CHALLENGE:
2796 return "LICENSE_STATE_PLATFORM_CHALLENGE";
2797 case LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE:
2798 return "LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE";
2799 case LICENSE_STATE_COMPLETED:
2800 return "LICENSE_STATE_COMPLETED";
2801 case LICENSE_STATE_ABORTED:
2802 return "LICENSE_STATE_ABORTED";
2804 return "LICENSE_STATE_UNKNOWN";
2808 BOOL license_server_send_request(rdpLicense* license)
2810 if (!license_ensure_state(license, LICENSE_STATE_CONFIGURED, LICENSE_REQUEST))
2812 if (!license_send_license_request_packet(license))
2814 return license_set_state(license, LICENSE_STATE_REQUEST);
2817 static BOOL license_set_string(
const char* what,
const char* value, BYTE** bdst, UINT32* dstLen)
2820 WINPR_ASSERT(value);
2822 WINPR_ASSERT(dstLen);
2832 *cnv.w = ConvertUtf8ToWCharAlloc(value, &len);
2833 if (!*cnv.w || (len > UINT32_MAX /
sizeof(WCHAR)))
2835 WLog_ERR(TAG,
"license->ProductInfo: %s == %p || %" PRIu32
" > UINT32_MAX", what, *cnv.w,
2839 *dstLen = (UINT32)(len *
sizeof(WCHAR));
2843 BOOL license_server_configure(rdpLicense* license)
2846 UINT32 algs[] = { KEY_EXCHANGE_ALG_RSA };
2848 WINPR_ASSERT(license);
2849 WINPR_ASSERT(license->rdp);
2851 const rdpSettings* settings = license->rdp->settings;
2853 const char* CompanyName =
2856 const char* ProductName =
2858 const UINT32 ProductVersion =
2860 const UINT32 issuerCount =
2864 const char** issuers = WINPR_REINTERPRET_CAST(ptr,
const void*,
const char**);
2866 WINPR_ASSERT(CompanyName);
2867 WINPR_ASSERT(ProductName);
2868 WINPR_ASSERT(ProductVersion > 0);
2869 WINPR_ASSERT(issuers || (issuerCount == 0));
2871 if (!license_ensure_state(license, LICENSE_STATE_INITIAL, LICENSE_REQUEST))
2874 license->ProductInfo->dwVersion = ProductVersion;
2875 if (!license_set_string(
"pbCompanyName", CompanyName, &license->ProductInfo->pbCompanyName,
2876 &license->ProductInfo->cbCompanyName))
2879 if (!license_set_string(
"pbProductId", ProductName, &license->ProductInfo->pbProductId,
2880 &license->ProductInfo->cbProductId))
2883 if (!license_read_binary_blob_data(license->KeyExchangeList, BB_KEY_EXCHG_ALG_BLOB, algs,
2887 if (!freerdp_certificate_read_server_cert(license->certificate, settings->ServerCertificate,
2888 settings->ServerCertificateLength))
2891 s = Stream_New(NULL, 1024);
2898 freerdp_certificate_write_server_cert(license->certificate, CERT_CHAIN_VERSION_2, s);
2900 r = license_read_binary_blob_data(license->ServerCertificate, BB_CERTIFICATE_BLOB,
2901 Stream_Buffer(s), Stream_GetPosition(s));
2903 Stream_Free(s, TRUE);
2908 if (!license_scope_list_resize(license->ScopeList, issuerCount))
2910 for (
size_t x = 0; x < issuerCount; x++)
2913 const char* name = issuers[x];
2914 const size_t length = strnlen(name, UINT16_MAX) + 1;
2915 if ((length == 0) || (length > UINT16_MAX))
2918 "%s: Invalid issuer at position %" PRIuz
": length 0 < %" PRIuz
" <= %" PRIu16
2920 x, length, UINT16_MAX, name);
2923 if (!license_read_binary_blob_data(blob, BB_SCOPE_BLOB, name, length))
2927 return license_set_state(license, LICENSE_STATE_CONFIGURED);
2930 rdpLicense* license_get(rdpContext* context)
2932 WINPR_ASSERT(context);
2933 WINPR_ASSERT(context->rdp);
2934 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.