24#include <freerdp/config.h>
28#include <freerdp/license.h>
31#include <winpr/assert.h>
32#include <winpr/crypto.h>
33#include <winpr/shell.h>
34#include <winpr/path.h>
35#include <winpr/file.h>
37#include <freerdp/log.h>
39#include <freerdp/crypto/certificate.h>
43#include "../crypto/crypto.h"
44#include "../crypto/certificate.h"
46#define LICENSE_TAG FREERDP_TAG("core.license")
53#define PLATFORM_CHALLENGE_RESPONSE_VERSION 0x0100
56enum LicenseRequestType
58 LICENSE_REQUEST = 0x01,
59 PLATFORM_CHALLENGE = 0x02,
61 UPGRADE_LICENSE = 0x04,
63 NEW_LICENSE_REQUEST = 0x13,
64 PLATFORM_CHALLENGE_RESPONSE = 0x15,
75#define LICENSE_PREAMBLE_LENGTH 4
79#define SERVER_RANDOM_LENGTH 32
80#define MASTER_SECRET_LENGTH 48
81#define PREMASTER_SECRET_LENGTH 48
82#define SESSION_KEY_BLOB_LENGTH 48
83#define MAC_SALT_KEY_LENGTH 16
84#define LICENSING_ENCRYPTION_KEY_LENGTH 16
85#define HWID_PLATFORM_ID_LENGTH 4
93#define PREAMBLE_VERSION_3_0 0x03
95#define EXTENDED_ERROR_MSG_SUPPORTED 0x80
100 BB_ANY_BLOB = 0x0000,
101 BB_DATA_BLOB = 0x0001,
102 BB_RANDOM_BLOB = 0x0002,
103 BB_CERTIFICATE_BLOB = 0x0003,
104 BB_ERROR_BLOB = 0x0004,
105 BB_ENCRYPTED_DATA_BLOB = 0x0009,
106 BB_KEY_EXCHG_ALG_BLOB = 0x000D,
107 BB_SCOPE_BLOB = 0x000E,
108 BB_CLIENT_USER_NAME_BLOB = 0x000F,
109 BB_CLIENT_MACHINE_NAME_BLOB = 0x0010
114#define KEY_EXCHANGE_ALG_RSA 0x00000001
120 ERR_INVALID_SERVER_CERTIFICATE = 0x00000001,
121 ERR_NO_LICENSE = 0x00000002,
122 ERR_INVALID_MAC = 0x00000003,
123 ERR_INVALID_SCOPE = 0x00000004,
124 ERR_NO_LICENSE_SERVER = 0x00000006,
125 STATUS_VALID_CLIENT = 0x00000007,
126 ERR_INVALID_CLIENT = 0x00000008,
127 ERR_INVALID_PRODUCT_ID = 0x0000000B,
128 ERR_INVALID_MESSAGE_LENGTH = 0x0000000C
135 ST_TOTAL_ABORT = 0x00000001,
136 ST_NO_TRANSITION = 0x00000002,
137 ST_RESET_PHASE_TO_START = 0x00000003,
138 ST_RESEND_LAST_MESSAGE = 0x00000004
145 WIN32_PLATFORM_CHALLENGE_TYPE = 0x0100,
146 WIN16_PLATFORM_CHALLENGE_TYPE = 0x0200,
147 WINCE_PLATFORM_CHALLENGE_TYPE = 0x0300,
148 OTHER_PLATFORM_CHALLENGE_TYPE = 0xFF00
155 LICENSE_DETAIL_SIMPLE = 0x0001,
156 LICENSE_DETAIL_MODERATE = 0x0002,
157 LICENSE_DETAIL_DETAIL = 0x0003
175 CLIENT_OS_ID_WINNT_351 = 0x01000000,
176 CLIENT_OS_ID_WINNT_40 = 0x02000000,
177 CLIENT_OS_ID_WINNT_50 = 0x03000000,
178 CLIENT_OS_ID_WINNT_POST_52 = 0x04000000,
180 CLIENT_IMAGE_ID_MICROSOFT = 0x00010000,
181 CLIENT_IMAGE_ID_CITRIX = 0x00020000,
189 rdpCertificate* certificate;
190 BYTE HardwareId[HWID_LENGTH];
191 BYTE ClientRandom[CLIENT_RANDOM_LENGTH];
192 BYTE ServerRandom[SERVER_RANDOM_LENGTH];
193 BYTE MasterSecret[MASTER_SECRET_LENGTH];
194 BYTE PremasterSecret[PREMASTER_SECRET_LENGTH];
195 BYTE SessionKeyBlob[SESSION_KEY_BLOB_LENGTH];
196 BYTE MacSaltKey[MAC_SALT_KEY_LENGTH];
197 BYTE LicensingEncryptionKey[LICENSING_ENCRYPTION_KEY_LENGTH];
212 BYTE MACData[LICENSING_ENCRYPTION_KEY_LENGTH];
214 UINT32 PacketHeaderLength;
215 UINT32 PreferredKeyExchangeAlg;
218 UINT16 LicenseDetailLevel;
223static BOOL license_send_error_alert(rdpLicense* license, UINT32 dwErrorCode,
225static BOOL license_set_state(rdpLicense* license, LICENSE_STATE state);
226static const char* license_get_state_string(LICENSE_STATE state);
228static const char* license_preferred_key_exchange_alg_string(UINT32 alg,
char* buffer,
size_t size)
230 const char* name = NULL;
234 case KEY_EXCHANGE_ALG_RSA:
235 name =
"KEY_EXCHANGE_ALG_RSA";
238 name =
"KEY_EXCHANGE_ALG_UNKNOWN";
242 (void)_snprintf(buffer, size,
"%s [0x%08" PRIx32
"]", name, alg);
246static const char* license_request_type_string(UINT32 type)
250 case LICENSE_REQUEST:
251 return "LICENSE_REQUEST";
252 case PLATFORM_CHALLENGE:
253 return "PLATFORM_CHALLENGE";
255 return "NEW_LICENSE";
256 case UPGRADE_LICENSE:
257 return "UPGRADE_LICENSE";
259 return "LICENSE_INFO";
260 case NEW_LICENSE_REQUEST:
261 return "NEW_LICENSE_REQUEST";
262 case PLATFORM_CHALLENGE_RESPONSE:
263 return "PLATFORM_CHALLENGE_RESPONSE";
265 return "ERROR_ALERT";
267 return "LICENSE_REQUEST_TYPE_UNKNOWN";
271static const char* licencse_blob_type_string(UINT16 type)
276 return "BB_ANY_BLOB";
278 return "BB_DATA_BLOB";
280 return "BB_RANDOM_BLOB";
281 case BB_CERTIFICATE_BLOB:
282 return "BB_CERTIFICATE_BLOB";
284 return "BB_ERROR_BLOB";
285 case BB_ENCRYPTED_DATA_BLOB:
286 return "BB_ENCRYPTED_DATA_BLOB";
287 case BB_KEY_EXCHG_ALG_BLOB:
288 return "BB_KEY_EXCHG_ALG_BLOB";
290 return "BB_SCOPE_BLOB";
291 case BB_CLIENT_USER_NAME_BLOB:
292 return "BB_CLIENT_USER_NAME_BLOB";
293 case BB_CLIENT_MACHINE_NAME_BLOB:
294 return "BB_CLIENT_MACHINE_NAME_BLOB";
299static wStream* license_send_stream_init(rdpLicense* license, UINT16* sec_flags);
301static void license_generate_randoms(rdpLicense* license);
302static BOOL license_generate_keys(rdpLicense* license);
303static BOOL license_generate_hwid(rdpLicense* license);
304static BOOL license_encrypt_premaster_secret(rdpLicense* license);
310static LICENSE_BLOB* license_new_binary_blob(UINT16 type);
311static void license_free_binary_blob(
LICENSE_BLOB* blob);
312static BOOL license_read_binary_blob_data(wLog* log,
LICENSE_BLOB* blob, UINT16 type,
313 const void* data,
size_t length);
317static SCOPE_LIST* license_new_scope_list(
void);
318static BOOL license_scope_list_resize(
SCOPE_LIST* scopeList, UINT32 count);
319static void license_free_scope_list(
SCOPE_LIST* scopeList);
320static BOOL license_read_scope_list(wLog* log,
wStream* s,
SCOPE_LIST* scopeList);
321static BOOL license_write_scope_list(wLog* log,
wStream* s,
const SCOPE_LIST* scopeList);
323static BOOL license_read_license_request_packet(rdpLicense* license,
wStream* s);
324static BOOL license_write_license_request_packet(
const rdpLicense* license,
wStream* s);
326static BOOL license_read_platform_challenge_packet(rdpLicense* license,
wStream* s);
327static BOOL license_send_platform_challenge_packet(rdpLicense* license);
328static BOOL license_read_new_or_upgrade_license_packet(rdpLicense* license,
wStream* s);
329static BOOL license_read_error_alert_packet(rdpLicense* license,
wStream* s);
331static BOOL license_write_new_license_request_packet(
const rdpLicense* license,
wStream* s);
332static BOOL license_read_new_license_request_packet(rdpLicense* license,
wStream* s);
333static BOOL license_answer_license_request(rdpLicense* license);
335static BOOL license_send_platform_challenge_response(rdpLicense* license);
336static BOOL license_read_platform_challenge_response(rdpLicense* license);
338static BOOL license_read_client_platform_challenge_response(rdpLicense* license,
wStream* s);
339static BOOL license_write_client_platform_challenge_response(rdpLicense* license,
wStream* s);
341static BOOL license_write_server_upgrade_license(
const rdpLicense* license,
wStream* s);
343static BOOL license_send_license_info(rdpLicense* license,
const LICENSE_BLOB* calBlob,
344 const BYTE* signature,
size_t signature_length);
345static BOOL license_read_license_info(rdpLicense* license,
wStream* s);
346static state_run_t license_client_recv(rdpLicense* license,
wStream* s);
347static state_run_t license_server_recv(rdpLicense* license,
wStream* s);
349#define PLATFORMID (CLIENT_OS_ID_WINNT_POST_52 | CLIENT_IMAGE_ID_MICROSOFT)
351#ifdef WITH_DEBUG_LICENSE
353static const char* error_codes[] = {
"ERR_UNKNOWN",
354 "ERR_INVALID_SERVER_CERTIFICATE",
359 "ERR_NO_LICENSE_SERVER",
360 "STATUS_VALID_CLIENT",
361 "ERR_INVALID_CLIENT",
364 "ERR_INVALID_PRODUCT_ID",
365 "ERR_INVALID_MESSAGE_LENGTH" };
367static const char* state_transitions[] = {
"ST_UNKNOWN",
"ST_TOTAL_ABORT",
"ST_NO_TRANSITION",
368 "ST_RESET_PHASE_TO_START",
"ST_RESEND_LAST_MESSAGE" };
372 char* CompanyName = NULL;
373 char* ProductId = NULL;
375 WINPR_ASSERT(productInfo);
376 WINPR_ASSERT(productInfo->pbCompanyName);
377 WINPR_ASSERT(productInfo->pbProductId);
379 CompanyName = ConvertWCharNToUtf8Alloc((
const WCHAR*)productInfo->pbCompanyName,
380 productInfo->cbCompanyName /
sizeof(WCHAR), NULL);
381 ProductId = ConvertWCharNToUtf8Alloc((
const WCHAR*)productInfo->pbProductId,
382 productInfo->cbProductId /
sizeof(WCHAR), NULL);
383 WLog_Print(log, WLOG_INFO,
"ProductInfo:");
384 WLog_Print(log, WLOG_INFO,
"\tdwVersion: 0x%08" PRIX32
"", productInfo->dwVersion);
385 WLog_Print(log, WLOG_INFO,
"\tCompanyName: %s", CompanyName);
386 WLog_Print(log, WLOG_INFO,
"\tProductId: %s", ProductId);
391static void license_print_scope_list(wLog* log,
const SCOPE_LIST* scopeList)
393 WINPR_ASSERT(scopeList);
395 WLog_Print(log, WLOG_INFO,
"ScopeList (%" PRIu32
"):", scopeList->count);
397 for (UINT32 index = 0; index < scopeList->count; index++)
401 WINPR_ASSERT(scopeList->array);
402 scope = scopeList->array[index];
405 WLog_Print(log, WLOG_INFO,
"\t%s", (
const char*)scope->data);
410static const char licenseStore[] =
"licenses";
412static BOOL license_ensure_state(rdpLicense* license, LICENSE_STATE state, UINT32 msg)
414 const LICENSE_STATE cstate = license_get_state(license);
416 WINPR_ASSERT(license);
420 const char* scstate = license_get_state_string(cstate);
421 const char* sstate = license_get_state_string(state);
422 const char* where = license_request_type_string(msg);
424 WLog_Print(license->log, WLOG_WARN,
425 "Received [%s], but found invalid licensing state %s, expected %s", where,
432state_run_t license_recv(rdpLicense* license,
wStream* s)
434 WINPR_ASSERT(license);
435 WINPR_ASSERT(license->rdp);
436 WINPR_ASSERT(license->rdp->settings);
439 return license_server_recv(license, s);
441 return license_client_recv(license, s);
444static BOOL license_check_stream_length(wLog* log,
wStream* s, SSIZE_T expect,
const char* where)
446 const size_t remain = Stream_GetRemainingLength(s);
452 WLog_Print(log, WLOG_WARN,
"invalid %s, expected value %" PRIdz
" invalid", where, expect);
455 if (remain < (
size_t)expect)
457 WLog_Print(log, WLOG_WARN,
"short %s, expected %" PRIdz
" bytes, got %" PRIuz, where,
464static BOOL license_check_stream_capacity(wLog* log,
wStream* s,
size_t expect,
const char* where)
468 if (!Stream_CheckAndLogRequiredCapacityWLogEx(log, WLOG_WARN, s, expect, 1,
469 "%s(%s:%" PRIuz
") %s", __func__, __FILE__,
470 (
size_t)__LINE__, where))
476static BOOL computeCalHash(wLog* log,
const char* hostname,
char* hashStr,
size_t len)
478 WINPR_DIGEST_CTX* sha1 = NULL;
480 BYTE hash[20] = { 0 };
482 WINPR_ASSERT(hostname);
483 WINPR_ASSERT(hashStr);
485 if (len < 2 *
sizeof(hash) + 1)
488 if (!(sha1 = winpr_Digest_New()))
490 if (!winpr_Digest_Init(sha1, WINPR_MD_SHA1))
492 if (!winpr_Digest_Update(sha1, (
const BYTE*)hostname, strlen(hostname)))
494 if (!winpr_Digest_Final(sha1, hash,
sizeof(hash)))
497 for (
size_t i = 0; i <
sizeof(hash); i++, hashStr += 2)
498 (
void)sprintf_s(hashStr, 3,
"%.2x", hash[i]);
503 WLog_Print(log, WLOG_ERROR,
"failed to generate SHA1 of hostname '%s'", hostname);
504 winpr_Digest_Free(sha1);
508static BOOL saveCal(wLog* log,
const rdpSettings* settings,
const BYTE* data,
size_t length,
509 const char* hostname)
511 char hash[41] = { 0 };
513 char* licenseStorePath = NULL;
514 char filename[MAX_PATH] = { 0 };
515 char filenameNew[MAX_PATH] = { 0 };
516 char* filepath = NULL;
517 char* filepathNew = NULL;
524 WINPR_ASSERT(data || (length == 0));
525 WINPR_ASSERT(hostname);
527 if (!winpr_PathFileExists(path))
529 if (!winpr_PathMakePath(path, 0))
531 WLog_Print(log, WLOG_ERROR,
"error creating directory '%s'", path);
534 WLog_Print(log, WLOG_INFO,
"creating directory %s", path);
537 if (!(licenseStorePath = GetCombinedPath(path, licenseStore)))
539 WLog_Print(log, WLOG_ERROR,
"Failed to get license store path from '%s' + '%s'", path,
544 if (!winpr_PathFileExists(licenseStorePath))
546 if (!winpr_PathMakePath(licenseStorePath, 0))
548 WLog_Print(log, WLOG_ERROR,
"error creating directory '%s'", licenseStorePath);
551 WLog_Print(log, WLOG_INFO,
"creating directory %s", licenseStorePath);
554 if (!computeCalHash(log, hostname, hash,
sizeof(hash)))
556 (void)sprintf_s(filename,
sizeof(filename) - 1,
"%s.cal", hash);
557 (void)sprintf_s(filenameNew,
sizeof(filenameNew) - 1,
"%s.cal.new", hash);
559 if (!(filepath = GetCombinedPath(licenseStorePath, filename)))
561 WLog_Print(log, WLOG_ERROR,
"Failed to get license file path from '%s' + '%s'", path,
566 if (!(filepathNew = GetCombinedPath(licenseStorePath, filenameNew)))
568 WLog_Print(log, WLOG_ERROR,
"Failed to get license new file path from '%s' + '%s'", path,
573 fp = winpr_fopen(filepathNew,
"wb");
576 WLog_Print(log, WLOG_ERROR,
"Failed to open license file '%s'", filepathNew);
580 written = fwrite(data, length, 1, fp);
585 WLog_Print(log, WLOG_ERROR,
"Failed to write to license file '%s'", filepathNew);
586 winpr_DeleteFile(filepathNew);
590 ret = winpr_MoveFileEx(filepathNew, filepath, MOVEFILE_REPLACE_EXISTING);
592 WLog_Print(log, WLOG_ERROR,
"Failed to move license file '%s' to '%s'", filepathNew,
598 free(licenseStorePath);
602static BYTE* loadCalFile(wLog* log,
const rdpSettings* settings,
const char* hostname,
605 char* licenseStorePath = NULL;
606 char* calPath = NULL;
607 char calFilename[MAX_PATH] = { 0 };
608 char hash[41] = { 0 };
614 WINPR_ASSERT(settings);
615 WINPR_ASSERT(hostname);
616 WINPR_ASSERT(dataLen);
618 if (!computeCalHash(log, hostname, hash,
sizeof(hash)))
620 WLog_Print(log, WLOG_ERROR,
"loadCalFile: unable to compute hostname hash");
624 (void)sprintf_s(calFilename,
sizeof(calFilename) - 1,
"%s.cal", hash);
626 if (!(licenseStorePath = GetCombinedPath(
630 if (!(calPath = GetCombinedPath(licenseStorePath, calFilename)))
633 fp = winpr_fopen(calPath,
"rb");
637 if (_fseeki64(fp, 0, SEEK_END) != 0)
639 length = _ftelli64(fp);
640 if (_fseeki64(fp, 0, SEEK_SET) != 0)
645 ret = (BYTE*)malloc((
size_t)length);
649 status = fread(ret, (
size_t)length, 1, fp);
653 *dataLen = (size_t)length;
657 free(licenseStorePath);
667 free(licenseStorePath);
681static BOOL license_read_preamble(wLog* log,
wStream* s, BYTE* bMsgType, BYTE* flags,
684 WINPR_ASSERT(bMsgType);
686 WINPR_ASSERT(wMsgSize);
689 if (!license_check_stream_length(log, s, 4,
"license preamble"))
692 Stream_Read_UINT8(s, *bMsgType);
693 Stream_Read_UINT8(s, *flags);
694 Stream_Read_UINT16(s, *wMsgSize);
695 return license_check_stream_length(log, s, *wMsgSize - 4ll,
"license preamble::wMsgSize");
708static BOOL license_write_preamble(
wStream* s, BYTE bMsgType, BYTE flags, UINT16 wMsgSize)
710 if (!Stream_EnsureRemainingCapacity(s, 4))
714 Stream_Write_UINT8(s, bMsgType);
715 Stream_Write_UINT8(s, flags);
716 Stream_Write_UINT16(s, wMsgSize);
728wStream* license_send_stream_init(rdpLicense* license, UINT16* sec_flags)
730 WINPR_ASSERT(license);
731 WINPR_ASSERT(license->rdp);
732 WINPR_ASSERT(sec_flags);
734 const BOOL do_crypt = license->rdp->do_crypt;
736 *sec_flags = SEC_LICENSE_PKT;
748 *sec_flags |= SEC_LICENSE_ENCRYPT_CS;
749 license->rdp->do_crypt = license->rdp->do_crypt_license;
752 wStream* s = rdp_send_stream_init(license->rdp, sec_flags);
756 license->rdp->do_crypt = do_crypt;
757 license->PacketHeaderLength = (UINT16)Stream_GetPosition(s);
758 if (!Stream_SafeSeek(s, LICENSE_PREAMBLE_LENGTH))
774static BOOL license_send(rdpLicense* license,
wStream* s, BYTE type, UINT16 sec_flags)
776 WINPR_ASSERT(license);
777 WINPR_ASSERT(license->rdp);
779 rdpRdp* rdp = license->rdp;
780 WINPR_ASSERT(rdp->settings);
782 DEBUG_LICENSE(
"Sending %s Packet", license_request_type_string(type));
783 const size_t length = Stream_GetPosition(s);
784 WINPR_ASSERT(length >= license->PacketHeaderLength);
785 WINPR_ASSERT(length <= UINT16_MAX + license->PacketHeaderLength);
787 const UINT16 wMsgSize = (UINT16)(length - license->PacketHeaderLength);
788 Stream_SetPosition(s, license->PacketHeaderLength);
789 BYTE flags = PREAMBLE_VERSION_3_0;
796 if (!rdp->settings->ServerMode)
797 flags |= EXTENDED_ERROR_MSG_SUPPORTED;
799 if (!license_write_preamble(s, type, flags, wMsgSize))
805#ifdef WITH_DEBUG_LICENSE
806 WLog_Print(license->log, WLOG_DEBUG,
"Sending %s Packet, length %" PRIu16
"",
807 license_request_type_string(type), wMsgSize);
808 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_PointerAs(s,
char) - LICENSE_PREAMBLE_LENGTH,
811 Stream_SetPosition(s, length);
812 const BOOL ret = rdp_send(rdp, s, MCS_GLOBAL_CHANNEL_ID, sec_flags);
816BOOL license_write_server_upgrade_license(
const rdpLicense* license,
wStream* s)
818 WINPR_ASSERT(license);
820 if (!license_write_binary_blob(s, license->EncryptedLicenseInfo))
822 if (!license_check_stream_capacity(license->log, s,
sizeof(license->MACData),
823 "SERVER_UPGRADE_LICENSE::MACData"))
825 Stream_Write(s, license->MACData,
sizeof(license->MACData));
829static BOOL license_server_send_new_or_upgrade_license(rdpLicense* license, BOOL upgrade)
831 UINT16 sec_flags = 0;
832 wStream* s = license_send_stream_init(license, &sec_flags);
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, sec_flags);
856static state_run_t license_client_recv_int(rdpLicense* license,
wStream* s)
861 const size_t length = Stream_GetRemainingLength(s);
863 WINPR_ASSERT(license);
865 if (!license_read_preamble(license->log, s, &bMsgType, &flags,
867 return STATE_RUN_FAILED;
869 DEBUG_LICENSE(
"Receiving %s Packet", license_request_type_string(bMsgType));
873 case LICENSE_REQUEST:
875 if (license_get_state(license) == LICENSE_STATE_INITIAL)
876 license_set_state(license, LICENSE_STATE_CONFIGURED);
878 if (!license_ensure_state(license, LICENSE_STATE_CONFIGURED, bMsgType))
879 return STATE_RUN_FAILED;
881 if (!license_read_license_request_packet(license, s))
882 return STATE_RUN_FAILED;
884 if (!license_answer_license_request(license))
885 return STATE_RUN_FAILED;
887 license_set_state(license, LICENSE_STATE_NEW_REQUEST);
890 case PLATFORM_CHALLENGE:
891 if (!license_ensure_state(license, LICENSE_STATE_NEW_REQUEST, bMsgType))
892 return STATE_RUN_FAILED;
894 if (!license_read_platform_challenge_packet(license, s))
895 return STATE_RUN_FAILED;
897 if (!license_send_platform_challenge_response(license))
898 return STATE_RUN_FAILED;
899 license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE);
903 case UPGRADE_LICENSE:
904 if (!license_ensure_state(license, LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE, bMsgType))
905 return STATE_RUN_FAILED;
906 if (!license_read_new_or_upgrade_license_packet(license, s))
907 return STATE_RUN_FAILED;
911 if (!license_read_error_alert_packet(license, s))
912 return STATE_RUN_FAILED;
916 WLog_Print(license->log, WLOG_ERROR,
"invalid bMsgType:%" PRIu8
"", bMsgType);
917 return STATE_RUN_FAILED;
920 if (!tpkt_ensure_stream_consumed(license->log, s, length))
921 return STATE_RUN_FAILED;
922 return STATE_RUN_SUCCESS;
925state_run_t license_client_recv(rdpLicense* license,
wStream* s)
927 state_run_t rc = license_client_recv_int(license, s);
928 if (state_run_failed(rc))
930 freerdp_set_last_error(license->rdp->context, ERROR_CTX_LICENSE_CLIENT_INVALID);
935state_run_t license_server_recv(rdpLicense* license,
wStream* s)
937 state_run_t rc = STATE_RUN_FAILED;
941 const size_t length = Stream_GetRemainingLength(s);
943 WINPR_ASSERT(license);
945 if (!license_read_preamble(license->log, s, &bMsgType, &flags,
949 DEBUG_LICENSE(
"Receiving %s Packet", license_request_type_string(bMsgType));
953 case NEW_LICENSE_REQUEST:
954 if (!license_ensure_state(license, LICENSE_STATE_REQUEST, bMsgType))
956 if (!license_read_new_license_request_packet(license, s))
959 if (!license_send_error_alert(license, ERR_INVALID_MAC, ST_TOTAL_ABORT,
962 if (!license_send_platform_challenge_packet(license))
964 license->update = FALSE;
965 if (!license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE))
969 if (!license_ensure_state(license, LICENSE_STATE_REQUEST, bMsgType))
971 if (!license_read_license_info(license, s))
974 if (!license_send_platform_challenge_packet(license))
976 if (!license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE))
978 license->update = TRUE;
981 case PLATFORM_CHALLENGE_RESPONSE:
982 if (!license_ensure_state(license, LICENSE_STATE_PLATFORM_CHALLENGE, bMsgType))
984 if (!license_read_client_platform_challenge_response(license, s))
990 if (license_send_error_alert(license, ERR_INVALID_CLIENT, ST_TOTAL_ABORT,
996 if (!license_server_send_new_or_upgrade_license(license, license->update))
999 license->type = LICENSE_TYPE_ISSUED;
1000 license_set_state(license, LICENSE_STATE_COMPLETED);
1002 rc = STATE_RUN_CONTINUE;
1007 if (!license_read_error_alert_packet(license, s))
1012 WLog_Print(license->log, WLOG_ERROR,
"invalid bMsgType:%" PRIu8
"", bMsgType);
1016 if (!tpkt_ensure_stream_consumed(license->log, s, length))
1019 if (!state_run_success(rc))
1020 rc = STATE_RUN_SUCCESS;
1023 if (state_run_failed(rc))
1025 if (flags & EXTENDED_ERROR_MSG_SUPPORTED)
1026 license_send_error_alert(license, ERR_INVALID_CLIENT, ST_TOTAL_ABORT, NULL);
1027 license_set_state(license, LICENSE_STATE_ABORTED);
1033void license_generate_randoms(rdpLicense* license)
1035 WINPR_ASSERT(license);
1037#ifdef LICENSE_NULL_CLIENT_RANDOM
1038 ZeroMemory(license->ClientRandom,
sizeof(license->ClientRandom));
1040 winpr_RAND(license->ClientRandom,
sizeof(license->ClientRandom));
1043 winpr_RAND(license->ServerRandom,
sizeof(license->ServerRandom));
1045#ifdef LICENSE_NULL_PREMASTER_SECRET
1046 ZeroMemory(license->PremasterSecret,
sizeof(license->PremasterSecret));
1048 winpr_RAND(license->PremasterSecret,
sizeof(license->PremasterSecret));
1057static BOOL license_generate_keys(rdpLicense* license)
1059 WINPR_ASSERT(license);
1063 !security_master_secret(license->PremasterSecret,
sizeof(license->PremasterSecret),
1064 license->ClientRandom,
sizeof(license->ClientRandom),
1065 license->ServerRandom,
sizeof(license->ServerRandom),
1066 license->MasterSecret,
sizeof(license->MasterSecret)) ||
1068 !security_session_key_blob(license->MasterSecret,
sizeof(license->MasterSecret),
1069 license->ClientRandom,
sizeof(license->ClientRandom),
1070 license->ServerRandom,
sizeof(license->ServerRandom),
1071 license->SessionKeyBlob,
sizeof(license->SessionKeyBlob)))
1075 security_mac_salt_key(license->SessionKeyBlob,
sizeof(license->SessionKeyBlob),
1076 license->ClientRandom,
sizeof(license->ClientRandom),
1077 license->ServerRandom,
sizeof(license->ServerRandom), license->MacSaltKey,
1078 sizeof(license->MacSaltKey));
1079 const BOOL ret = security_licensing_encryption_key(
1080 license->SessionKeyBlob,
sizeof(license->SessionKeyBlob), license->ClientRandom,
1081 sizeof(license->ClientRandom), license->ServerRandom,
sizeof(license->ServerRandom),
1082 license->LicensingEncryptionKey,
1083 sizeof(license->LicensingEncryptionKey));
1085#ifdef WITH_DEBUG_LICENSE
1086 WLog_Print(license->log, WLOG_DEBUG,
"ClientRandom:");
1087 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ClientRandom,
1088 sizeof(license->ClientRandom));
1089 WLog_Print(license->log, WLOG_DEBUG,
"ServerRandom:");
1090 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ServerRandom,
1091 sizeof(license->ServerRandom));
1092 WLog_Print(license->log, WLOG_DEBUG,
"PremasterSecret:");
1093 winpr_HexLogDump(license->log, WLOG_DEBUG, license->PremasterSecret,
1094 sizeof(license->PremasterSecret));
1095 WLog_Print(license->log, WLOG_DEBUG,
"MasterSecret:");
1096 winpr_HexLogDump(license->log, WLOG_DEBUG, license->MasterSecret,
1097 sizeof(license->MasterSecret));
1098 WLog_Print(license->log, WLOG_DEBUG,
"SessionKeyBlob:");
1099 winpr_HexLogDump(license->log, WLOG_DEBUG, license->SessionKeyBlob,
1100 sizeof(license->SessionKeyBlob));
1101 WLog_Print(license->log, WLOG_DEBUG,
"MacSaltKey:");
1102 winpr_HexLogDump(license->log, WLOG_DEBUG, license->MacSaltKey,
sizeof(license->MacSaltKey));
1103 WLog_Print(license->log, WLOG_DEBUG,
"LicensingEncryptionKey:");
1104 winpr_HexLogDump(license->log, WLOG_DEBUG, license->LicensingEncryptionKey,
1105 sizeof(license->LicensingEncryptionKey));
1115BOOL license_generate_hwid(rdpLicense* license)
1117 const BYTE* hashTarget = NULL;
1118 size_t targetLen = 0;
1119 BYTE macAddress[6] = { 0 };
1121 WINPR_ASSERT(license);
1122 WINPR_ASSERT(license->rdp);
1123 WINPR_ASSERT(license->rdp->settings);
1125 ZeroMemory(license->HardwareId,
sizeof(license->HardwareId));
1127 if (license->rdp->settings->OldLicenseBehaviour)
1129 hashTarget = macAddress;
1130 targetLen =
sizeof(macAddress);
1135 const char* hostname = license->rdp->settings->ClientHostname;
1136 wStream* s = Stream_StaticInit(&buffer, license->HardwareId, 4);
1137 Stream_Write_UINT32(s, license->PlatformId);
1139 hashTarget = (
const BYTE*)hostname;
1140 targetLen = hostname ? strlen(hostname) : 0;
1149 return winpr_Digest_Allow_FIPS(WINPR_MD_MD5, hashTarget, targetLen,
1150 &license->HardwareId[HWID_PLATFORM_ID_LENGTH],
1151 WINPR_MD5_DIGEST_LENGTH);
1154static BOOL license_get_server_rsa_public_key(rdpLicense* license)
1156 rdpSettings* settings = NULL;
1158 WINPR_ASSERT(license);
1159 WINPR_ASSERT(license->certificate);
1160 WINPR_ASSERT(license->rdp);
1162 settings = license->rdp->settings;
1163 WINPR_ASSERT(settings);
1165 if (license->ServerCertificate->length < 1)
1167 if (!freerdp_certificate_read_server_cert(license->certificate, settings->ServerCertificate,
1168 settings->ServerCertificateLength))
1175BOOL license_encrypt_premaster_secret(rdpLicense* license)
1177 WINPR_ASSERT(license);
1178 WINPR_ASSERT(license->certificate);
1180 if (!license_get_server_rsa_public_key(license))
1183 WINPR_ASSERT(license->EncryptedPremasterSecret);
1185 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1188 WLog_Print(license->log, WLOG_ERROR,
"info=%p, license->certificate=%p", info,
1189 license->certificate);
1193#ifdef WITH_DEBUG_LICENSE
1194 WLog_Print(license->log, WLOG_DEBUG,
"Modulus (%" PRIu32
" bits):", info->ModulusLength * 8);
1195 winpr_HexLogDump(license->log, WLOG_DEBUG, info->Modulus, info->ModulusLength);
1196 WLog_Print(license->log, WLOG_DEBUG,
"Exponent:");
1197 winpr_HexLogDump(license->log, WLOG_DEBUG, info->exponent,
sizeof(info->exponent));
1200 BYTE* EncryptedPremasterSecret = (BYTE*)calloc(1, info->ModulusLength);
1201 if (!EncryptedPremasterSecret)
1203 WLog_Print(license->log, WLOG_ERROR,
1204 "EncryptedPremasterSecret=%p, info->ModulusLength=%" PRIu32,
1205 EncryptedPremasterSecret, info->ModulusLength);
1209 license->EncryptedPremasterSecret->type = BB_RANDOM_BLOB;
1210 license->EncryptedPremasterSecret->length =
sizeof(license->PremasterSecret);
1211#ifndef LICENSE_NULL_PREMASTER_SECRET
1213 const SSIZE_T length =
1214 crypto_rsa_public_encrypt(license->PremasterSecret,
sizeof(license->PremasterSecret),
1215 info, EncryptedPremasterSecret, info->ModulusLength);
1216 if ((length < 0) || (length > UINT16_MAX))
1218 WLog_Print(license->log, WLOG_ERROR,
1219 "RSA public encrypt length=%" PRIdz
" < 0 || > %" PRIu16, length,
1223 license->EncryptedPremasterSecret->length = (UINT16)length;
1226 license->EncryptedPremasterSecret->data = EncryptedPremasterSecret;
1230static BOOL license_rc4_with_licenseKey(
const rdpLicense* license,
const BYTE* input,
size_t len,
1233 WINPR_ASSERT(license);
1234 WINPR_ASSERT(input || (len == 0));
1235 WINPR_ASSERT(target);
1236 WINPR_ASSERT(len <= UINT16_MAX);
1238 WINPR_RC4_CTX* rc4 = winpr_RC4_New_Allow_FIPS(license->LicensingEncryptionKey,
1239 sizeof(license->LicensingEncryptionKey));
1242 WLog_Print(license->log, WLOG_ERROR,
"Failed to allocate RC4");
1246 BYTE* buffer = NULL;
1248 buffer = realloc(target->data, len);
1252 target->data = buffer;
1253 target->length = (UINT16)len;
1255 if (!winpr_RC4_Update(rc4, len, input, buffer))
1258 winpr_RC4_Free(rc4);
1262 WLog_Print(license->log, WLOG_ERROR,
"Failed to create/update RC4: len=%" PRIuz
", buffer=%p",
1264 winpr_RC4_Free(rc4);
1278static BOOL license_encrypt_and_MAC(rdpLicense* license,
const BYTE* input,
size_t len,
1281 WINPR_ASSERT(license);
1282 return license_rc4_with_licenseKey(license, input, len, target) &&
1283 security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), input, len, mac,
1298static BOOL license_decrypt_and_check_MAC(rdpLicense* license,
const BYTE* input,
size_t len,
1301 BYTE macData[
sizeof(license->MACData)] = { 0 };
1303 WINPR_ASSERT(license);
1304 WINPR_ASSERT(target);
1308 WLog_Print(license->log, WLOG_DEBUG,
"TransportDumpReplay active, skipping...");
1312 if (!license_rc4_with_licenseKey(license, input, len, target))
1315 if (!security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), target->data, len,
1316 macData,
sizeof(macData)))
1319 if (memcmp(packetMac, macData,
sizeof(macData)) != 0)
1321 WLog_Print(license->log, WLOG_ERROR,
"packetMac != expectedMac");
1336 WINPR_ASSERT(productInfo);
1338 if (!license_check_stream_length(log, s, 8,
"license product info::cbCompanyName"))
1341 Stream_Read_UINT32(s, productInfo->dwVersion);
1342 Stream_Read_UINT32(s, productInfo->cbCompanyName);
1345 if ((productInfo->cbCompanyName < 2) || (productInfo->cbCompanyName % 2 != 0))
1347 WLog_Print(log, WLOG_WARN,
"license product info invalid cbCompanyName %" PRIu32,
1348 productInfo->cbCompanyName);
1352 if (!license_check_stream_length(log, s, productInfo->cbCompanyName,
1353 "license product info::CompanyName"))
1356 productInfo->pbProductId = NULL;
1357 productInfo->pbCompanyName = (BYTE*)malloc(productInfo->cbCompanyName);
1358 if (!productInfo->pbCompanyName)
1360 Stream_Read(s, productInfo->pbCompanyName, productInfo->cbCompanyName);
1362 if (!license_check_stream_length(log, s, 4,
"license product info::cbProductId"))
1365 Stream_Read_UINT32(s, productInfo->cbProductId);
1367 if ((productInfo->cbProductId < 2) || (productInfo->cbProductId % 2 != 0))
1369 WLog_Print(log, WLOG_WARN,
"license product info invalid cbProductId %" PRIu32,
1370 productInfo->cbProductId);
1374 if (!license_check_stream_length(log, s, productInfo->cbProductId,
1375 "license product info::ProductId"))
1378 productInfo->pbProductId = (BYTE*)malloc(productInfo->cbProductId);
1379 if (!productInfo->pbProductId)
1381 Stream_Read(s, productInfo->pbProductId, productInfo->cbProductId);
1385 free(productInfo->pbCompanyName);
1386 free(productInfo->pbProductId);
1387 productInfo->pbCompanyName = NULL;
1388 productInfo->pbProductId = NULL;
1392static BOOL license_write_product_info(wLog* log,
wStream* s,
1395 WINPR_ASSERT(productInfo);
1397 if (!license_check_stream_capacity(log, s, 8,
"license product info::cbCompanyName"))
1400 Stream_Write_UINT32(s, productInfo->dwVersion);
1401 Stream_Write_UINT32(s, productInfo->cbCompanyName);
1404 if ((productInfo->cbCompanyName < 2) || (productInfo->cbCompanyName % 2 != 0) ||
1405 !productInfo->pbCompanyName)
1407 WLog_Print(log, WLOG_WARN,
"license product info invalid cbCompanyName %" PRIu32,
1408 productInfo->cbCompanyName);
1412 if (!license_check_stream_capacity(log, s, productInfo->cbCompanyName,
1413 "license product info::CompanyName"))
1416 Stream_Write(s, productInfo->pbCompanyName, productInfo->cbCompanyName);
1418 if (!license_check_stream_capacity(log, s, 4,
"license product info::cbProductId"))
1421 Stream_Write_UINT32(s, productInfo->cbProductId);
1423 if ((productInfo->cbProductId < 2) || (productInfo->cbProductId % 2 != 0) ||
1424 !productInfo->pbProductId)
1426 WLog_Print(log, WLOG_WARN,
"license product info invalid cbProductId %" PRIu32,
1427 productInfo->cbProductId);
1431 if (!license_check_stream_capacity(log, s, productInfo->cbProductId,
1432 "license product info::ProductId"))
1435 Stream_Write(s, productInfo->pbProductId, productInfo->cbProductId);
1464 free(productInfo->pbCompanyName);
1465 free(productInfo->pbProductId);
1470BOOL license_read_binary_blob_data(wLog* log,
LICENSE_BLOB* blob, UINT16 wBlobType,
1471 const void* data,
size_t length)
1474 WINPR_ASSERT(length <= UINT16_MAX);
1475 WINPR_ASSERT(data || (length == 0));
1477 blob->length = (UINT16)length;
1481 if ((blob->type != wBlobType) && (blob->type != BB_ANY_BLOB))
1483 WLog_Print(log, WLOG_ERROR,
"license binary blob::type expected %s, got %s",
1484 licencse_blob_type_string(wBlobType), licencse_blob_type_string(blob->type));
1491 if ((blob->type != BB_ANY_BLOB) && (blob->length == 0))
1493 WLog_Print(log, WLOG_WARN,
"license binary blob::type %s, length=0, skipping.",
1494 licencse_blob_type_string(blob->type));
1498 blob->type = wBlobType;
1500 if (blob->length > 0)
1501 blob->data = malloc(blob->length);
1504 WLog_Print(log, WLOG_ERROR,
"license binary blob::length=%" PRIu16
", blob::data=%p",
1505 blob->length, blob->data);
1508 memcpy(blob->data, data, blob->length);
1521 UINT16 wBlobType = 0;
1526 if (!license_check_stream_length(log, s, 4,
"license binary blob::type"))
1529 Stream_Read_UINT16(s, wBlobType);
1530 Stream_Read_UINT16(s, length);
1532 if (!license_check_stream_length(log, s, length,
"license binary blob::length"))
1535 if (!license_read_binary_blob_data(log, blob, wBlobType, Stream_Pointer(s), length))
1538 return Stream_SafeSeek(s, length);
1552 if (!Stream_EnsureRemainingCapacity(s, blob->length + 4))
1555 Stream_Write_UINT16(s, blob->type);
1556 Stream_Write_UINT16(s, blob->length);
1558 if (blob->length > 0)
1559 Stream_Write(s, blob->data, blob->length);
1563static BOOL license_write_encrypted_premaster_secret_blob(wLog* log,
wStream* s,
1565 UINT32 ModulusLength)
1567 const UINT32 length = ModulusLength + 8;
1570 WINPR_ASSERT(length <= UINT16_MAX);
1572 if (blob->length > ModulusLength)
1574 WLog_Print(log, WLOG_ERROR,
"invalid blob");
1578 if (!Stream_EnsureRemainingCapacity(s, length + 4))
1580 Stream_Write_UINT16(s, blob->type);
1581 Stream_Write_UINT16(s, (UINT16)length);
1583 if (blob->length > 0)
1584 Stream_Write(s, blob->data, blob->length);
1586 Stream_Zero(s, length - blob->length);
1590static BOOL license_read_encrypted_premaster_secret_blob(wLog* log,
wStream* s,
LICENSE_BLOB* blob,
1591 UINT32* ModulusLength)
1593 if (!license_read_binary_blob(log, s, blob))
1595 WINPR_ASSERT(ModulusLength);
1596 *ModulusLength = blob->length;
1638 UINT32 scopeCount = 0;
1640 WINPR_ASSERT(scopeList);
1642 if (!license_check_stream_length(log, s, 4,
"license scope list"))
1645 Stream_Read_UINT32(s, scopeCount);
1647 if (!license_check_stream_length(log, s, 4ll * scopeCount,
"license scope list::count"))
1650 if (!license_scope_list_resize(scopeList, scopeCount))
1653 for (UINT32 i = 0; i < scopeCount; i++)
1655 if (!license_read_binary_blob(log, s, scopeList->array[i]))
1662BOOL license_write_scope_list(wLog* log,
wStream* s,
const SCOPE_LIST* scopeList)
1664 WINPR_ASSERT(scopeList);
1666 if (!license_check_stream_capacity(log, s, 4,
"license scope list"))
1669 Stream_Write_UINT32(s, scopeList->count);
1671 if (!license_check_stream_capacity(log, s, scopeList->count * 4ull,
1672 "license scope list::count"))
1676 WINPR_ASSERT(scopeList->array || (scopeList->count == 0));
1677 for (UINT32 i = 0; i < scopeList->count; i++)
1681 if (!license_write_binary_blob(s, element))
1700BOOL license_scope_list_resize(
SCOPE_LIST* scopeList, UINT32 count)
1702 WINPR_ASSERT(scopeList);
1703 WINPR_ASSERT(scopeList->array || (scopeList->count == 0));
1705 for (UINT32 x = count; x < scopeList->count; x++)
1707 license_free_binary_blob(scopeList->array[x]);
1708 scopeList->array[x] = NULL;
1717 scopeList->array = tmp;
1721 free((
void*)scopeList->array);
1722 scopeList->array = NULL;
1725 for (UINT32 x = scopeList->count; x < count; x++)
1727 LICENSE_BLOB* blob = license_new_binary_blob(BB_SCOPE_BLOB);
1730 scopeList->count = x;
1733 scopeList->array[x] = blob;
1736 scopeList->count = count;
1746void license_free_scope_list(
SCOPE_LIST* scopeList)
1751 license_scope_list_resize(scopeList, 0);
1755BOOL license_send_license_info(rdpLicense* license,
const LICENSE_BLOB* calBlob,
1756 const BYTE* signature,
size_t signature_length)
1758 WINPR_ASSERT(calBlob);
1759 WINPR_ASSERT(signature);
1760 WINPR_ASSERT(license->certificate);
1762 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1766 UINT16 sec_flags = 0;
1767 wStream* s = license_send_stream_init(license, &sec_flags);
1771 if (!license_check_stream_capacity(license->log, s, 8 +
sizeof(license->ClientRandom),
1772 "license info::ClientRandom"))
1775 Stream_Write_UINT32(s,
1776 license->PreferredKeyExchangeAlg);
1777 Stream_Write_UINT32(s, license->PlatformId);
1780 Stream_Write(s, license->ClientRandom,
sizeof(license->ClientRandom));
1783 if (!license_write_encrypted_premaster_secret_blob(
1784 license->log, s, license->EncryptedPremasterSecret, info->ModulusLength))
1788 if (!license_write_binary_blob(s, calBlob))
1792 if (!license_write_binary_blob(s, license->EncryptedHardwareId))
1796 if (!license_check_stream_capacity(license->log, s, signature_length,
"license info::MACData"))
1798 Stream_Write(s, signature, signature_length);
1800 return license_send(license, s, LICENSE_INFO, sec_flags);
1807static BOOL license_check_preferred_alg(rdpLicense* license, UINT32 PreferredKeyExchangeAlg,
1810 WINPR_ASSERT(license);
1811 WINPR_ASSERT(where);
1813 if (license->PreferredKeyExchangeAlg != PreferredKeyExchangeAlg)
1815 char buffer1[64] = { 0 };
1816 char buffer2[64] = { 0 };
1817 WLog_Print(license->log, WLOG_WARN,
"%s::PreferredKeyExchangeAlg, expected %s, got %s",
1819 license_preferred_key_exchange_alg_string(license->PreferredKeyExchangeAlg,
1820 buffer1,
sizeof(buffer1)),
1821 license_preferred_key_exchange_alg_string(PreferredKeyExchangeAlg, buffer2,
1828BOOL license_read_license_info(rdpLicense* license,
wStream* s)
1831 UINT32 PreferredKeyExchangeAlg = 0;
1833 WINPR_ASSERT(license);
1834 WINPR_ASSERT(license->certificate);
1836 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1841 if (!license_check_stream_length(license->log, s, 8 +
sizeof(license->ClientRandom),
1845 Stream_Read_UINT32(s, PreferredKeyExchangeAlg);
1846 if (!license_check_preferred_alg(license, PreferredKeyExchangeAlg,
"license info"))
1848 Stream_Read_UINT32(s, license->PlatformId);
1851 Stream_Read(s, license->ClientRandom,
sizeof(license->ClientRandom));
1854 UINT32 ModulusLength = 0;
1855 if (!license_read_encrypted_premaster_secret_blob(
1856 license->log, s, license->EncryptedPremasterSecret, &ModulusLength))
1859 if (ModulusLength != info->ModulusLength)
1861 WLog_Print(license->log, WLOG_WARN,
1862 "EncryptedPremasterSecret,::ModulusLength[%" PRIu32
1863 "] != rdpCertInfo::ModulusLength[%" PRIu32
"]",
1864 ModulusLength, info->ModulusLength);
1868 if (!license_read_binary_blob(license->log, s, license->LicenseInfo))
1872 if (!license_read_binary_blob(license->log, s, license->EncryptedHardwareId))
1876 if (!license_check_stream_length(license->log, s,
sizeof(license->MACData),
1877 "license info::MACData"))
1879 Stream_Read(s, license->MACData,
sizeof(license->MACData));
1894BOOL license_read_license_request_packet(rdpLicense* license,
wStream* s)
1896 WINPR_ASSERT(license);
1899 if (!license_check_stream_length(license->log, s,
sizeof(license->ServerRandom),
1903 Stream_Read(s, license->ServerRandom,
sizeof(license->ServerRandom));
1906 if (!license_read_product_info(license->log, s, license->ProductInfo))
1910 if (!license_read_binary_blob(license->log, s, license->KeyExchangeList))
1914 if (!license_read_binary_blob(license->log, s, license->ServerCertificate))
1918 if (!license_read_scope_list(license->log, s, license->ScopeList))
1922 if (!freerdp_certificate_read_server_cert(license->certificate,
1923 license->ServerCertificate->data,
1924 license->ServerCertificate->length))
1927 if (!license_generate_keys(license) || !license_generate_hwid(license) ||
1928 !license_encrypt_premaster_secret(license))
1931#ifdef WITH_DEBUG_LICENSE
1932 WLog_Print(license->log, WLOG_DEBUG,
"ServerRandom:");
1933 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ServerRandom,
1934 sizeof(license->ServerRandom));
1935 license_print_product_info(license->log, license->ProductInfo);
1936 license_print_scope_list(license->log, license->ScopeList);
1941BOOL license_write_license_request_packet(
const rdpLicense* license,
wStream* s)
1943 WINPR_ASSERT(license);
1946 if (!license_check_stream_capacity(license->log, s,
sizeof(license->ServerRandom),
1949 Stream_Write(s, license->ServerRandom,
sizeof(license->ServerRandom));
1952 if (!license_write_product_info(license->log, s, license->ProductInfo))
1956 if (!license_write_binary_blob(s, license->KeyExchangeList))
1960 if (!license_write_binary_blob(s, license->ServerCertificate))
1964 if (!license_write_scope_list(license->log, s, license->ScopeList))
1970static BOOL license_send_license_request_packet(rdpLicense* license)
1972 UINT16 sec_flags = 0;
1973 wStream* s = license_send_stream_init(license, &sec_flags);
1977 if (!license_write_license_request_packet(license, s))
1980 return license_send(license, s, LICENSE_REQUEST, sec_flags);
1994BOOL license_read_platform_challenge_packet(rdpLicense* license,
wStream* s)
1996 BYTE macData[LICENSING_ENCRYPTION_KEY_LENGTH] = { 0 };
1998 WINPR_ASSERT(license);
2000 DEBUG_LICENSE(
"Receiving Platform Challenge Packet");
2002 if (!license_check_stream_length(license->log, s, 4,
"license platform challenge"))
2007 Stream_Seek_UINT32(s);
2010 license->EncryptedPlatformChallenge->type = BB_ANY_BLOB;
2011 if (!license_read_binary_blob(license->log, s, license->EncryptedPlatformChallenge))
2013 license->EncryptedPlatformChallenge->type = BB_ENCRYPTED_DATA_BLOB;
2016 if (!license_check_stream_length(license->log, s,
sizeof(macData),
2017 "license platform challenge::MAC"))
2020 Stream_Read(s, macData,
sizeof(macData));
2021 if (!license_decrypt_and_check_MAC(license, license->EncryptedPlatformChallenge->data,
2022 license->EncryptedPlatformChallenge->length,
2023 license->PlatformChallenge, macData))
2026#ifdef WITH_DEBUG_LICENSE
2027 WLog_Print(license->log, WLOG_DEBUG,
"EncryptedPlatformChallenge:");
2028 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedPlatformChallenge->data,
2029 license->EncryptedPlatformChallenge->length);
2030 WLog_Print(license->log, WLOG_DEBUG,
"PlatformChallenge:");
2031 winpr_HexLogDump(license->log, WLOG_DEBUG, license->PlatformChallenge->data,
2032 license->PlatformChallenge->length);
2033 WLog_Print(license->log, WLOG_DEBUG,
"MacData:");
2034 winpr_HexLogDump(license->log, WLOG_DEBUG, macData,
sizeof(macData));
2039BOOL license_send_error_alert(rdpLicense* license, UINT32 dwErrorCode, UINT32 dwStateTransition,
2042 UINT16 sec_flags = 0;
2043 wStream* s = license_send_stream_init(license, &sec_flags);
2048 if (!license_check_stream_capacity(license->log, s, 8,
"license error alert"))
2050 Stream_Write_UINT32(s, dwErrorCode);
2051 Stream_Write_UINT32(s, dwStateTransition);
2055 if (!license_write_binary_blob(s, info))
2059 return license_send(license, s, ERROR_ALERT, sec_flags);
2065BOOL license_send_platform_challenge_packet(rdpLicense* license)
2067 UINT16 sec_flags = 0;
2068 wStream* s = license_send_stream_init(license, &sec_flags);
2073 DEBUG_LICENSE(
"Receiving Platform Challenge Packet");
2075 if (!license_check_stream_capacity(license->log, s, 4,
"license platform challenge"))
2081 if (!license_write_binary_blob(s, license->EncryptedPlatformChallenge))
2085 if (!license_check_stream_length(license->log, s,
sizeof(license->MACData),
2086 "license platform challenge::MAC"))
2089 Stream_Write(s, license->MACData,
sizeof(license->MACData));
2091 return license_send(license, s, PLATFORM_CHALLENGE, sec_flags);
2097static BOOL license_read_encrypted_blob(
const rdpLicense* license,
wStream* s,
LICENSE_BLOB* target)
2099 UINT16 wBlobType = 0;
2100 UINT16 wBlobLen = 0;
2102 WINPR_ASSERT(license);
2103 WINPR_ASSERT(target);
2105 if (!license_check_stream_length(license->log, s, 4,
"license encrypted blob"))
2108 Stream_Read_UINT16(s, wBlobType);
2109 if (wBlobType != BB_ENCRYPTED_DATA_BLOB)
2112 license->log, WLOG_WARN,
2113 "expecting BB_ENCRYPTED_DATA_BLOB blob, probably a windows 2003 server, continuing...");
2116 Stream_Read_UINT16(s, wBlobLen);
2118 BYTE* encryptedData = Stream_Pointer(s);
2119 if (!Stream_SafeSeek(s, wBlobLen))
2121 WLog_Print(license->log, WLOG_WARN,
2122 "short license encrypted blob::length, expected %" PRIu16
" bytes, got %" PRIuz,
2123 wBlobLen, Stream_GetRemainingLength(s));
2127 return license_rc4_with_licenseKey(license, encryptedData, wBlobLen, target);
2137BOOL license_read_new_or_upgrade_license_packet(rdpLicense* license,
wStream* s)
2139 UINT32 os_major = 0;
2140 UINT32 os_minor = 0;
2142 UINT32 cbCompanyName = 0;
2143 UINT32 cbProductId = 0;
2144 UINT32 cbLicenseInfo = 0;
2146 wStream* licenseStream = NULL;
2148 BYTE computedMac[16] = { 0 };
2149 const BYTE* readMac = NULL;
2151 WINPR_ASSERT(license);
2153 DEBUG_LICENSE(
"Receiving Server New/Upgrade License Packet");
2155 LICENSE_BLOB* calBlob = license_new_binary_blob(BB_DATA_BLOB);
2160 if (!license_read_encrypted_blob(license, s, calBlob))
2164 readMac = Stream_Pointer(s);
2165 if (!Stream_SafeSeek(s,
sizeof(computedMac)))
2167 WLog_Print(license->log, WLOG_WARN,
2168 "short license new/upgrade, expected 16 bytes, got %" PRIuz,
2169 Stream_GetRemainingLength(s));
2173 if (!security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), calBlob->data,
2174 calBlob->length, computedMac,
sizeof(computedMac)))
2177 if (memcmp(computedMac, readMac,
sizeof(computedMac)) != 0)
2179 WLog_Print(license->log, WLOG_ERROR,
"new or upgrade license MAC mismatch");
2183 licenseStream = Stream_StaticConstInit(&sbuffer, calBlob->data, calBlob->length);
2186 WLog_Print(license->log, WLOG_ERROR,
2187 "license::blob::data=%p, license::blob::length=%" PRIu16, calBlob->data,
2192 if (!license_check_stream_length(license->log, licenseStream, 8,
2193 "license new/upgrade::blob::version"))
2196 Stream_Read_UINT16(licenseStream, os_minor);
2197 Stream_Read_UINT16(licenseStream, os_major);
2199 WLog_Print(license->log, WLOG_DEBUG,
"Version: %" PRIu16
".%" PRIu16, os_major, os_minor);
2202 Stream_Read_UINT32(licenseStream, cbScope);
2203 if (!license_check_stream_length(license->log, licenseStream, cbScope,
2204 "license new/upgrade::blob::scope"))
2207#ifdef WITH_DEBUG_LICENSE
2208 WLog_Print(license->log, WLOG_DEBUG,
"Scope:");
2209 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbScope);
2211 Stream_Seek(licenseStream, cbScope);
2214 if (!license_check_stream_length(license->log, licenseStream, 4,
2215 "license new/upgrade::blob::cbCompanyName"))
2218 Stream_Read_UINT32(licenseStream, cbCompanyName);
2219 if (!license_check_stream_length(license->log, licenseStream, cbCompanyName,
2220 "license new/upgrade::blob::CompanyName"))
2223#ifdef WITH_DEBUG_LICENSE
2224 WLog_Print(license->log, WLOG_DEBUG,
"Company name:");
2225 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbCompanyName);
2227 Stream_Seek(licenseStream, cbCompanyName);
2230 if (!license_check_stream_length(license->log, licenseStream, 4,
2231 "license new/upgrade::blob::cbProductId"))
2234 Stream_Read_UINT32(licenseStream, cbProductId);
2236 if (!license_check_stream_length(license->log, licenseStream, cbProductId,
2237 "license new/upgrade::blob::ProductId"))
2240#ifdef WITH_DEBUG_LICENSE
2241 WLog_Print(license->log, WLOG_DEBUG,
"Product id:");
2242 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbProductId);
2244 Stream_Seek(licenseStream, cbProductId);
2247 if (!license_check_stream_length(license->log, licenseStream, 4,
2248 "license new/upgrade::blob::cbLicenseInfo"))
2251 Stream_Read_UINT32(licenseStream, cbLicenseInfo);
2252 if (!license_check_stream_length(license->log, licenseStream, cbLicenseInfo,
2253 "license new/upgrade::blob::LicenseInfo"))
2256 license->type = LICENSE_TYPE_ISSUED;
2257 ret = license_set_state(license, LICENSE_STATE_COMPLETED);
2259 if (!license->rdp->settings->OldLicenseBehaviour)
2260 ret = saveCal(license->log, license->rdp->settings, Stream_Pointer(licenseStream),
2261 cbLicenseInfo, license->rdp->settings->ClientHostname);
2264 license_free_binary_blob(calBlob);
2275BOOL license_read_error_alert_packet(rdpLicense* license,
wStream* s)
2277 UINT32 dwErrorCode = 0;
2278 UINT32 dwStateTransition = 0;
2280 WINPR_ASSERT(license);
2281 WINPR_ASSERT(license->rdp);
2283 if (!license_check_stream_length(license->log, s, 8ul,
"error alert"))
2286 Stream_Read_UINT32(s, dwErrorCode);
2287 Stream_Read_UINT32(s, dwStateTransition);
2289 if (!license_read_binary_blob(license->log, s, license->ErrorInfo))
2292#ifdef WITH_DEBUG_LICENSE
2293 WLog_Print(license->log, WLOG_DEBUG,
"dwErrorCode: %s, dwStateTransition: %s",
2294 error_codes[dwErrorCode], state_transitions[dwStateTransition]);
2297 if (dwErrorCode == STATUS_VALID_CLIENT)
2299 license->type = LICENSE_TYPE_NONE;
2300 return license_set_state(license, LICENSE_STATE_COMPLETED);
2303 switch (dwStateTransition)
2305 case ST_TOTAL_ABORT:
2306 license_set_state(license, LICENSE_STATE_ABORTED);
2308 case ST_NO_TRANSITION:
2309 license_set_state(license, LICENSE_STATE_COMPLETED);
2311 case ST_RESET_PHASE_TO_START:
2312 license_set_state(license, LICENSE_STATE_CONFIGURED);
2314 case ST_RESEND_LAST_MESSAGE:
2330BOOL license_write_new_license_request_packet(
const rdpLicense* license,
wStream* s)
2332 WINPR_ASSERT(license);
2334 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
2338 if (!license_check_stream_capacity(license->log, s, 8 +
sizeof(license->ClientRandom),
2342 Stream_Write_UINT32(s,
2343 license->PreferredKeyExchangeAlg);
2344 Stream_Write_UINT32(s, license->PlatformId);
2345 Stream_Write(s, license->ClientRandom,
2346 sizeof(license->ClientRandom));
2349 !license_write_encrypted_premaster_secret_blob(
2350 license->log, s, license->EncryptedPremasterSecret, info->ModulusLength) ||
2352 !license_write_binary_blob(s, license->ClientUserName) ||
2354 !license_write_binary_blob(s, license->ClientMachineName))
2359#ifdef WITH_DEBUG_LICENSE
2360 WLog_Print(license->log, WLOG_DEBUG,
"PreferredKeyExchangeAlg: 0x%08" PRIX32
"",
2361 license->PreferredKeyExchangeAlg);
2362 WLog_Print(license->log, WLOG_DEBUG,
"ClientRandom:");
2363 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ClientRandom,
2364 sizeof(license->ClientRandom));
2365 WLog_Print(license->log, WLOG_DEBUG,
"EncryptedPremasterSecret");
2366 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedPremasterSecret->data,
2367 license->EncryptedPremasterSecret->length);
2368 WLog_Print(license->log, WLOG_DEBUG,
"ClientUserName (%" PRIu16
"): %s",
2369 license->ClientUserName->length, (
char*)license->ClientUserName->data);
2370 WLog_Print(license->log, WLOG_DEBUG,
"ClientMachineName (%" PRIu16
"): %s",
2371 license->ClientMachineName->length, (
char*)license->ClientMachineName->data);
2376BOOL license_read_new_license_request_packet(rdpLicense* license,
wStream* s)
2378 UINT32 PreferredKeyExchangeAlg = 0;
2380 WINPR_ASSERT(license);
2382 if (!license_check_stream_length(license->log, s, 8ull +
sizeof(license->ClientRandom),
2383 "new license request"))
2386 Stream_Read_UINT32(s, PreferredKeyExchangeAlg);
2387 if (!license_check_preferred_alg(license, PreferredKeyExchangeAlg,
"new license request"))
2390 Stream_Read_UINT32(s, license->PlatformId);
2391 Stream_Read(s, license->ClientRandom,
2392 sizeof(license->ClientRandom));
2395 UINT32 ModulusLength = 0;
2396 if (!license_read_encrypted_premaster_secret_blob(
2397 license->log, s, license->EncryptedPremasterSecret, &ModulusLength))
2400 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
2402 WLog_Print(license->log, WLOG_WARN,
2403 "Missing license certificate, skipping ModulusLength checks");
2404 else if (ModulusLength != info->ModulusLength)
2406 WLog_Print(license->log, WLOG_WARN,
2407 "EncryptedPremasterSecret expected to be %" PRIu32
" bytes, but read %" PRIu32
2409 info->ModulusLength, ModulusLength);
2414 if (!license_read_binary_blob(license->log, s, license->ClientUserName))
2417 if (!license_read_binary_blob(license->log, s, license->ClientMachineName))
2429BOOL license_answer_license_request(rdpLicense* license)
2431 UINT16 sec_flags = 0;
2433 BYTE* license_data = NULL;
2434 size_t license_size = 0;
2436 char* username = NULL;
2438 WINPR_ASSERT(license);
2439 WINPR_ASSERT(license->rdp);
2440 WINPR_ASSERT(license->rdp->settings);
2442 if (!license->rdp->settings->OldLicenseBehaviour)
2443 license_data = loadCalFile(license->log, license->rdp->settings,
2444 license->rdp->settings->ClientHostname, &license_size);
2449 BYTE signature[LICENSING_ENCRYPTION_KEY_LENGTH] = { 0 };
2451 DEBUG_LICENSE(
"Sending Saved License Packet");
2453 WINPR_ASSERT(license->EncryptedHardwareId);
2454 license->EncryptedHardwareId->type = BB_ENCRYPTED_DATA_BLOB;
2455 if (!license_encrypt_and_MAC(license, license->HardwareId,
sizeof(license->HardwareId),
2456 license->EncryptedHardwareId, signature,
sizeof(signature)))
2462 calBlob = license_new_binary_blob(BB_DATA_BLOB);
2468 calBlob->data = license_data;
2469 WINPR_ASSERT(license_size <= UINT16_MAX);
2470 calBlob->length = (UINT16)license_size;
2472 status = license_send_license_info(license, calBlob, signature,
sizeof(signature));
2473 license_free_binary_blob(calBlob);
2478 DEBUG_LICENSE(
"Sending New License Packet");
2480 s = license_send_stream_init(license, &sec_flags);
2483 if (license->rdp->settings->Username != NULL)
2484 username = license->rdp->settings->Username;
2486 username =
"username";
2489 WINPR_ASSERT(license->ClientUserName);
2490 const size_t len = strlen(username) + 1;
2491 WINPR_ASSERT(len <= UINT16_MAX);
2493 license->ClientUserName->data = (BYTE*)username;
2494 license->ClientUserName->length = (UINT16)len;
2498 WINPR_ASSERT(license->ClientMachineName);
2499 const size_t len = strlen(license->rdp->settings->ClientHostname) + 1;
2500 WINPR_ASSERT(len <= UINT16_MAX);
2502 license->ClientMachineName->data = (BYTE*)license->rdp->settings->ClientHostname;
2503 license->ClientMachineName->length = (UINT16)len;
2505 status = license_write_new_license_request_packet(license, s);
2507 WINPR_ASSERT(license->ClientUserName);
2508 license->ClientUserName->data = NULL;
2509 license->ClientUserName->length = 0;
2511 WINPR_ASSERT(license->ClientMachineName);
2512 license->ClientMachineName->data = NULL;
2513 license->ClientMachineName->length = 0;
2521 return license_send(license, s, NEW_LICENSE_REQUEST, sec_flags);
2530BOOL license_send_platform_challenge_response(rdpLicense* license)
2532 wStream* challengeRespData = NULL;
2533 BYTE* buffer = NULL;
2536 WINPR_ASSERT(license);
2537 WINPR_ASSERT(license->PlatformChallenge);
2538 WINPR_ASSERT(license->MacSaltKey);
2539 WINPR_ASSERT(license->EncryptedPlatformChallenge);
2540 WINPR_ASSERT(license->EncryptedHardwareId);
2542 DEBUG_LICENSE(
"Sending Platform Challenge Response Packet");
2544 license->EncryptedPlatformChallenge->type = BB_DATA_BLOB;
2547 challengeRespData = Stream_New(NULL, 8 + license->PlatformChallenge->length);
2548 if (!challengeRespData)
2550 Stream_Write_UINT16(challengeRespData, PLATFORM_CHALLENGE_RESPONSE_VERSION);
2551 Stream_Write_UINT16(challengeRespData, license->ClientType);
2552 Stream_Write_UINT16(challengeRespData, license->LicenseDetailLevel);
2553 Stream_Write_UINT16(challengeRespData, license->PlatformChallenge->length);
2554 Stream_Write(challengeRespData, license->PlatformChallenge->data,
2555 license->PlatformChallenge->length);
2556 Stream_SealLength(challengeRespData);
2559 const size_t length = Stream_Length(challengeRespData) +
sizeof(license->HardwareId);
2560 buffer = (BYTE*)malloc(length);
2563 Stream_Free(challengeRespData, TRUE);
2567 CopyMemory(buffer, Stream_Buffer(challengeRespData), Stream_Length(challengeRespData));
2568 CopyMemory(&buffer[Stream_Length(challengeRespData)], license->HardwareId,
2569 sizeof(license->HardwareId));
2570 status = security_mac_data(license->MacSaltKey,
sizeof(license->MacSaltKey), buffer, length,
2571 license->MACData,
sizeof(license->MACData));
2576 Stream_Free(challengeRespData, TRUE);
2580 license->EncryptedHardwareId->type = BB_ENCRYPTED_DATA_BLOB;
2581 if (!license_rc4_with_licenseKey(license, license->HardwareId,
sizeof(license->HardwareId),
2582 license->EncryptedHardwareId))
2584 Stream_Free(challengeRespData, TRUE);
2588 status = license_rc4_with_licenseKey(license, Stream_Buffer(challengeRespData),
2589 Stream_Length(challengeRespData),
2590 license->EncryptedPlatformChallengeResponse);
2591 Stream_Free(challengeRespData, TRUE);
2595#ifdef WITH_DEBUG_LICENSE
2596 WLog_Print(license->log, WLOG_DEBUG,
"LicensingEncryptionKey:");
2597 winpr_HexLogDump(license->log, WLOG_DEBUG, license->LicensingEncryptionKey, 16);
2598 WLog_Print(license->log, WLOG_DEBUG,
"HardwareId:");
2599 winpr_HexLogDump(license->log, WLOG_DEBUG, license->HardwareId,
sizeof(license->HardwareId));
2600 WLog_Print(license->log, WLOG_DEBUG,
"EncryptedHardwareId:");
2601 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedHardwareId->data,
2602 license->EncryptedHardwareId->length);
2604 UINT16 sec_flags = 0;
2605 wStream* s = license_send_stream_init(license, &sec_flags);
2609 if (license_write_client_platform_challenge_response(license, s))
2610 return license_send(license, s, PLATFORM_CHALLENGE_RESPONSE, sec_flags);
2616BOOL license_read_platform_challenge_response(WINPR_ATTR_UNUSED rdpLicense* license)
2618 WINPR_ASSERT(license);
2619 WINPR_ASSERT(license->PlatformChallenge);
2620 WINPR_ASSERT(license->MacSaltKey);
2621 WINPR_ASSERT(license->EncryptedPlatformChallenge);
2622 WINPR_ASSERT(license->EncryptedHardwareId);
2624 DEBUG_LICENSE(
"Receiving Platform Challenge Response Packet");
2626#if defined(WITH_LICENSE_DECRYPT_CHALLENGE_RESPONSE)
2628 LICENSE_BLOB* dblob = license_new_binary_blob(BB_ANY_BLOB);
2633 UINT16 wVersion = 0;
2634 UINT16 cbChallenge = 0;
2635 const BYTE* pbChallenge = NULL;
2636 LICENSE_BLOB* blob = license->EncryptedPlatformChallengeResponse;
2638 if (!license_rc4_with_licenseKey(license, blob->data, blob->length, dblob))
2641 wStream* s = Stream_StaticConstInit(&sbuffer, dblob->data, dblob->length);
2642 if (!license_check_stream_length(s, 8,
"PLATFORM_CHALLENGE_RESPONSE_DATA"))
2645 Stream_Read_UINT16(s, wVersion);
2646 if (wVersion != PLATFORM_CHALLENGE_RESPONSE_VERSION)
2648 WLog_Print(license->log, WLOG_WARN,
2649 "Invalid PLATFORM_CHALLENGE_RESPONSE_DATA::wVersion 0x%04" PRIx16
2650 ", expected 0x04" PRIx16,
2651 wVersion, PLATFORM_CHALLENGE_RESPONSE_VERSION);
2654 Stream_Read_UINT16(s, license->ClientType);
2655 Stream_Read_UINT16(s, license->LicenseDetailLevel);
2656 Stream_Read_UINT16(s, cbChallenge);
2658 if (!license_check_stream_length(s, cbChallenge,
2659 "PLATFORM_CHALLENGE_RESPONSE_DATA::pbChallenge"))
2662 pbChallenge = Stream_Pointer(s);
2663 if (!license_read_binary_blob_data(license->PlatformChallengeResponse, BB_DATA_BLOB,
2664 pbChallenge, cbChallenge))
2666 if (!Stream_SafeSeek(s, cbChallenge))
2671 license_free_binary_blob(dblob);
2678BOOL license_write_client_platform_challenge_response(rdpLicense* license,
wStream* s)
2680 WINPR_ASSERT(license);
2682 if (!license_write_binary_blob(s, license->EncryptedPlatformChallengeResponse))
2684 if (!license_write_binary_blob(s, license->EncryptedHardwareId))
2686 if (!license_check_stream_capacity(license->log, s,
sizeof(license->MACData),
2687 "CLIENT_PLATFORM_CHALLENGE_RESPONSE::MACData"))
2689 Stream_Write(s, license->MACData,
sizeof(license->MACData));
2693BOOL license_read_client_platform_challenge_response(rdpLicense* license,
wStream* s)
2695 WINPR_ASSERT(license);
2697 if (!license_read_binary_blob(license->log, s, license->EncryptedPlatformChallengeResponse))
2699 if (!license_read_binary_blob(license->log, s, license->EncryptedHardwareId))
2701 if (!license_check_stream_length(license->log, s,
sizeof(license->MACData),
2702 "CLIENT_PLATFORM_CHALLENGE_RESPONSE::MACData"))
2704 Stream_Read(s, license->MACData,
sizeof(license->MACData));
2705 return license_read_platform_challenge_response(license);
2717BOOL license_send_valid_client_error_packet(rdpRdp* rdp)
2720 rdpLicense* license = rdp->license;
2721 WINPR_ASSERT(license);
2723 license->state = LICENSE_STATE_COMPLETED;
2724 license->type = LICENSE_TYPE_NONE;
2725 return license_send_error_alert(license, STATUS_VALID_CLIENT, ST_NO_TRANSITION,
2726 license->ErrorInfo);
2735rdpLicense* license_new(rdpRdp* rdp)
2739 rdpLicense* license = (rdpLicense*)calloc(1,
sizeof(rdpLicense));
2742 license->log = WLog_Get(LICENSE_TAG);
2743 WINPR_ASSERT(license->log);
2745 license->PlatformId = PLATFORMID;
2746 license->ClientType = OTHER_PLATFORM_CHALLENGE_TYPE;
2747 license->LicenseDetailLevel = LICENSE_DETAIL_DETAIL;
2748 license->PreferredKeyExchangeAlg = KEY_EXCHANGE_ALG_RSA;
2751 license_set_state(license, LICENSE_STATE_INITIAL);
2752 if (!(license->certificate = freerdp_certificate_new()))
2754 if (!(license->ProductInfo = license_new_product_info()))
2756 if (!(license->ErrorInfo = license_new_binary_blob(BB_ERROR_BLOB)))
2758 if (!(license->LicenseInfo = license_new_binary_blob(BB_DATA_BLOB)))
2760 if (!(license->KeyExchangeList = license_new_binary_blob(BB_KEY_EXCHG_ALG_BLOB)))
2762 if (!(license->ServerCertificate = license_new_binary_blob(BB_CERTIFICATE_BLOB)))
2764 if (!(license->ClientUserName = license_new_binary_blob(BB_CLIENT_USER_NAME_BLOB)))
2766 if (!(license->ClientMachineName = license_new_binary_blob(BB_CLIENT_MACHINE_NAME_BLOB)))
2768 if (!(license->PlatformChallenge = license_new_binary_blob(BB_ANY_BLOB)))
2770 if (!(license->PlatformChallengeResponse = license_new_binary_blob(BB_ANY_BLOB)))
2772 if (!(license->EncryptedPlatformChallenge = license_new_binary_blob(BB_ANY_BLOB)))
2774 if (!(license->EncryptedPlatformChallengeResponse =
2775 license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2777 if (!(license->EncryptedPremasterSecret = license_new_binary_blob(BB_ANY_BLOB)))
2779 if (!(license->EncryptedHardwareId = license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2781 if (!(license->EncryptedLicenseInfo = license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2783 if (!(license->ScopeList = license_new_scope_list()))
2786 license_generate_randoms(license);
2791 WINPR_PRAGMA_DIAG_PUSH
2792 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2793 license_free(license);
2794 WINPR_PRAGMA_DIAG_POP
2803void license_free(rdpLicense* license)
2807 freerdp_certificate_free(license->certificate);
2808 license_free_product_info(license->ProductInfo);
2809 license_free_binary_blob(license->ErrorInfo);
2810 license_free_binary_blob(license->LicenseInfo);
2811 license_free_binary_blob(license->KeyExchangeList);
2812 license_free_binary_blob(license->ServerCertificate);
2813 license_free_binary_blob(license->ClientUserName);
2814 license_free_binary_blob(license->ClientMachineName);
2815 license_free_binary_blob(license->PlatformChallenge);
2816 license_free_binary_blob(license->PlatformChallengeResponse);
2817 license_free_binary_blob(license->EncryptedPlatformChallenge);
2818 license_free_binary_blob(license->EncryptedPlatformChallengeResponse);
2819 license_free_binary_blob(license->EncryptedPremasterSecret);
2820 license_free_binary_blob(license->EncryptedHardwareId);
2821 license_free_binary_blob(license->EncryptedLicenseInfo);
2822 license_free_scope_list(license->ScopeList);
2827LICENSE_STATE license_get_state(
const rdpLicense* license)
2829 WINPR_ASSERT(license);
2830 return license->state;
2833LICENSE_TYPE license_get_type(
const rdpLicense* license)
2835 WINPR_ASSERT(license);
2836 return license->type;
2839BOOL license_set_state(rdpLicense* license, LICENSE_STATE state)
2841 WINPR_ASSERT(license);
2842 license->state = state;
2845 case LICENSE_STATE_COMPLETED:
2847 case LICENSE_STATE_ABORTED:
2849 license->type = LICENSE_TYPE_INVALID;
2856const char* license_get_state_string(LICENSE_STATE state)
2860 case LICENSE_STATE_INITIAL:
2861 return "LICENSE_STATE_INITIAL";
2862 case LICENSE_STATE_CONFIGURED:
2863 return "LICENSE_STATE_CONFIGURED";
2864 case LICENSE_STATE_REQUEST:
2865 return "LICENSE_STATE_REQUEST";
2866 case LICENSE_STATE_NEW_REQUEST:
2867 return "LICENSE_STATE_NEW_REQUEST";
2868 case LICENSE_STATE_PLATFORM_CHALLENGE:
2869 return "LICENSE_STATE_PLATFORM_CHALLENGE";
2870 case LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE:
2871 return "LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE";
2872 case LICENSE_STATE_COMPLETED:
2873 return "LICENSE_STATE_COMPLETED";
2874 case LICENSE_STATE_ABORTED:
2875 return "LICENSE_STATE_ABORTED";
2877 return "LICENSE_STATE_UNKNOWN";
2881BOOL license_server_send_request(rdpLicense* license)
2883 if (!license_ensure_state(license, LICENSE_STATE_CONFIGURED, LICENSE_REQUEST))
2885 if (!license_send_license_request_packet(license))
2887 return license_set_state(license, LICENSE_STATE_REQUEST);
2890static BOOL license_set_string(wLog* log,
const char* what,
const char* value, BYTE** bdst,
2894 WINPR_ASSERT(value);
2896 WINPR_ASSERT(dstLen);
2906 *cnv.w = ConvertUtf8ToWCharAlloc(value, &len);
2907 if (!*cnv.w || (len > UINT32_MAX /
sizeof(WCHAR)))
2909 WLog_Print(log, WLOG_ERROR,
"license->ProductInfo: %s == %p || %" PRIu32
" > UINT32_MAX",
2913 *dstLen = (UINT32)(len *
sizeof(WCHAR));
2917BOOL license_server_configure(rdpLicense* license)
2920 UINT32 algs[] = { KEY_EXCHANGE_ALG_RSA };
2922 WINPR_ASSERT(license);
2923 WINPR_ASSERT(license->rdp);
2925 const rdpSettings* settings = license->rdp->settings;
2927 const char* CompanyName =
2930 const char* ProductName =
2932 const UINT32 ProductVersion =
2934 const UINT32 issuerCount =
2938 const char** issuers = WINPR_REINTERPRET_CAST(ptr,
const void*,
const char**);
2940 WINPR_ASSERT(CompanyName);
2941 WINPR_ASSERT(ProductName);
2942 WINPR_ASSERT(ProductVersion > 0);
2943 WINPR_ASSERT(issuers || (issuerCount == 0));
2945 if (!license_ensure_state(license, LICENSE_STATE_INITIAL, LICENSE_REQUEST))
2948 license->ProductInfo->dwVersion = ProductVersion;
2949 if (!license_set_string(license->log,
"pbCompanyName", CompanyName,
2950 &license->ProductInfo->pbCompanyName,
2951 &license->ProductInfo->cbCompanyName))
2954 if (!license_set_string(license->log,
"pbProductId", ProductName,
2955 &license->ProductInfo->pbProductId, &license->ProductInfo->cbProductId))
2958 if (!license_read_binary_blob_data(license->log, license->KeyExchangeList,
2959 BB_KEY_EXCHG_ALG_BLOB, algs,
sizeof(algs)))
2962 if (!freerdp_certificate_read_server_cert(license->certificate, settings->ServerCertificate,
2963 settings->ServerCertificateLength))
2966 s = Stream_New(NULL, 1024);
2973 freerdp_certificate_write_server_cert(license->certificate, CERT_CHAIN_VERSION_2, s);
2975 r = license_read_binary_blob_data(license->log, license->ServerCertificate,
2976 BB_CERTIFICATE_BLOB, Stream_Buffer(s),
2977 Stream_GetPosition(s));
2979 Stream_Free(s, TRUE);
2984 if (!license_scope_list_resize(license->ScopeList, issuerCount))
2986 for (
size_t x = 0; x < issuerCount; x++)
2989 const char* name = issuers[x];
2990 const size_t length = strnlen(name, UINT16_MAX) + 1;
2991 if ((length == 0) || (length > UINT16_MAX))
2993 WLog_Print(license->log, WLOG_WARN,
2994 "%s: Invalid issuer at position %" PRIuz
": length 0 < %" PRIuz
2995 " <= %" PRIu16
" ['%s']",
2996 x, length, UINT16_MAX, name);
2999 if (!license_read_binary_blob_data(license->log, blob, BB_SCOPE_BLOB, name, length))
3003 return license_set_state(license, LICENSE_STATE_CONFIGURED);
3006rdpLicense* license_get(rdpContext* context)
3008 WINPR_ASSERT(context);
3009 WINPR_ASSERT(context->rdp);
3010 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.