FreeRDP
Loading...
Searching...
No Matches
license.c
1/*
2 * FreeRDP: A Remote Desktop Protocol Implementation
3 * RDP Licensing
4 *
5 * Copyright 2011-2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6 * Copyright 2014 Norbert Federa <norbert.federa@thincast.com>
7 * Copyright 2018 David Fort <contact@hardening-consulting.com>
8 * Copyright 2022,2023 Armin Novak <armin.novak@thincast.com>
9 * Copyright 2022,2023 Thincast Technologies GmbH
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23
24#include <freerdp/config.h>
25
26#include "settings.h"
27
28#include <freerdp/license.h>
29
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>
36
37#include <freerdp/log.h>
38
39#include <freerdp/crypto/certificate.h>
40
41#include "license.h"
42
43#include "../crypto/crypto.h"
44#include "../crypto/certificate.h"
45
46#define LICENSE_TAG FREERDP_TAG("core.license")
47
48// #define LICENSE_NULL_CLIENT_RANDOM 1
49// #define LICENSE_NULL_PREMASTER_SECRET 1
50
51// #define WITH_LICENSE_DECRYPT_CHALLENGE_RESPONSE
52
53#define PLATFORM_CHALLENGE_RESPONSE_VERSION 0x0100
54
55/* Upper limits for data exchanged.
56 * In real situations they are not very large, but protocol wise these could be UINT32_MAX.
57 * So limit to something larger than actually used but less than the giant limit.
58 */
59#define LICENSE_MAX_CAL_LENGTH (1ull * UINT16_MAX)
60#define LICENSE_MAX_SCOPE_LENGTH (1ull * UINT16_MAX)
61#define LICENSE_MAX_PRODUCT_ID_LENGTH (1ull * UINT16_MAX)
62#define LICENSE_MAX_COMPANY_NAME_LENGTH (1ull * UINT16_MAX)
63
65enum LicenseRequestType
66{
67 LICENSE_REQUEST = 0x01,
68 PLATFORM_CHALLENGE = 0x02,
69 NEW_LICENSE = 0x03,
70 UPGRADE_LICENSE = 0x04,
71 LICENSE_INFO = 0x12,
72 NEW_LICENSE_REQUEST = 0x13,
73 PLATFORM_CHALLENGE_RESPONSE = 0x15,
74 ERROR_ALERT = 0xFF
75};
76
77/*
78#define LICENSE_PKT_CS_MASK \
79 (LICENSE_INFO | NEW_LICENSE_REQUEST | PLATFORM_CHALLENGE_RESPONSE | ERROR_ALERT)
80#define LICENSE_PKT_SC_MASK \
81 (LICENSE_REQUEST | PLATFORM_CHALLENGE | NEW_LICENSE | UPGRADE_LICENSE | ERROR_ALERT)
82#define LICENSE_PKT_MASK (LICENSE_PKT_CS_MASK | LICENSE_PKT_SC_MASK)
83*/
84#define LICENSE_PREAMBLE_LENGTH 4
85
86/* Cryptographic Lengths */
87
88#define SERVER_RANDOM_LENGTH 32
89#define MASTER_SECRET_LENGTH 48
90#define PREMASTER_SECRET_LENGTH 48
91#define SESSION_KEY_BLOB_LENGTH 48
92#define MAC_SALT_KEY_LENGTH 16
93#define LICENSING_ENCRYPTION_KEY_LENGTH 16
94#define HWID_PLATFORM_ID_LENGTH 4
95// #define HWID_UNIQUE_DATA_LENGTH 16
96#define HWID_LENGTH 20
97// #define LICENSING_PADDING_SIZE 8
98
99/* Preamble Flags */
100
101// #define PREAMBLE_VERSION_2_0 0x02
102#define PREAMBLE_VERSION_3_0 0x03
103// #define LicenseProtocolVersionMask 0x0F
104#define EXTENDED_ERROR_MSG_SUPPORTED 0x80
105
107enum
108{
109 BB_ANY_BLOB = 0x0000,
110 BB_DATA_BLOB = 0x0001,
111 BB_RANDOM_BLOB = 0x0002,
112 BB_CERTIFICATE_BLOB = 0x0003,
113 BB_ERROR_BLOB = 0x0004,
114 BB_ENCRYPTED_DATA_BLOB = 0x0009,
115 BB_KEY_EXCHG_ALG_BLOB = 0x000D,
116 BB_SCOPE_BLOB = 0x000E,
117 BB_CLIENT_USER_NAME_BLOB = 0x000F,
118 BB_CLIENT_MACHINE_NAME_BLOB = 0x0010
119};
120
121/* License Key Exchange Algorithms */
122
123#define KEY_EXCHANGE_ALG_RSA 0x00000001
124
127enum
128{
129 ERR_INVALID_SERVER_CERTIFICATE = 0x00000001,
130 ERR_NO_LICENSE = 0x00000002,
131 ERR_INVALID_MAC = 0x00000003,
132 ERR_INVALID_SCOPE = 0x00000004,
133 ERR_NO_LICENSE_SERVER = 0x00000006,
134 STATUS_VALID_CLIENT = 0x00000007,
135 ERR_INVALID_CLIENT = 0x00000008,
136 ERR_INVALID_PRODUCT_ID = 0x0000000B,
137 ERR_INVALID_MESSAGE_LENGTH = 0x0000000C
138};
139
142enum
143{
144 ST_TOTAL_ABORT = 0x00000001,
145 ST_NO_TRANSITION = 0x00000002,
146 ST_RESET_PHASE_TO_START = 0x00000003,
147 ST_RESEND_LAST_MESSAGE = 0x00000004
148};
149
152enum
153{
154 WIN32_PLATFORM_CHALLENGE_TYPE = 0x0100,
155 WIN16_PLATFORM_CHALLENGE_TYPE = 0x0200,
156 WINCE_PLATFORM_CHALLENGE_TYPE = 0x0300,
157 OTHER_PLATFORM_CHALLENGE_TYPE = 0xFF00
158};
159
162enum
163{
164 LICENSE_DETAIL_SIMPLE = 0x0001,
165 LICENSE_DETAIL_MODERATE = 0x0002,
166 LICENSE_DETAIL_DETAIL = 0x0003
167};
168
169/*
170 * PlatformId:
171 *
172 * The most significant byte of the PlatformId field contains the operating system version of the
173 * client. The second most significant byte of the PlatformId field identifies the ISV that provided
174 * the client image. The remaining two bytes in the PlatformId field are used by the ISV to identify
175 * the build number of the operating system.
176 *
177 * 0x04010000:
178 *
179 * CLIENT_OS_ID_WINNT_POST_52 (0x04000000)
180 * CLIENT_IMAGE_ID_MICROSOFT (0x00010000)
181 */
182enum
183{
184 CLIENT_OS_ID_WINNT_351 = 0x01000000,
185 CLIENT_OS_ID_WINNT_40 = 0x02000000,
186 CLIENT_OS_ID_WINNT_50 = 0x03000000,
187 CLIENT_OS_ID_WINNT_POST_52 = 0x04000000,
188
189 CLIENT_IMAGE_ID_MICROSOFT = 0x00010000,
190 CLIENT_IMAGE_ID_CITRIX = 0x00020000,
191};
192
193struct rdp_license
194{
195 LICENSE_STATE state;
196 LICENSE_TYPE type;
197 rdpRdp* rdp;
198 rdpCertificate* certificate;
199 BYTE HardwareId[HWID_LENGTH];
200 BYTE ClientRandom[CLIENT_RANDOM_LENGTH];
201 BYTE ServerRandom[SERVER_RANDOM_LENGTH];
202 BYTE MasterSecret[MASTER_SECRET_LENGTH];
203 BYTE PremasterSecret[PREMASTER_SECRET_LENGTH];
204 BYTE SessionKeyBlob[SESSION_KEY_BLOB_LENGTH];
205 BYTE MacSaltKey[MAC_SALT_KEY_LENGTH];
206 BYTE LicensingEncryptionKey[LICENSING_ENCRYPTION_KEY_LENGTH];
207 LICENSE_PRODUCT_INFO* ProductInfo;
208 LICENSE_BLOB* ErrorInfo;
209 LICENSE_BLOB* LicenseInfo; /* Client -> Server */
210 LICENSE_BLOB* KeyExchangeList;
211 LICENSE_BLOB* ServerCertificate;
212 LICENSE_BLOB* ClientUserName;
213 LICENSE_BLOB* ClientMachineName;
214 LICENSE_BLOB* PlatformChallenge;
215 LICENSE_BLOB* PlatformChallengeResponse;
216 LICENSE_BLOB* EncryptedPremasterSecret;
217 LICENSE_BLOB* EncryptedPlatformChallenge;
218 LICENSE_BLOB* EncryptedPlatformChallengeResponse;
219 LICENSE_BLOB* EncryptedHardwareId;
220 LICENSE_BLOB* EncryptedLicenseInfo;
221 BYTE MACData[LICENSING_ENCRYPTION_KEY_LENGTH];
222 SCOPE_LIST* ScopeList;
223 UINT32 PacketHeaderLength;
224 UINT32 PreferredKeyExchangeAlg;
225 UINT32 PlatformId;
226 UINT16 ClientType;
227 UINT16 LicenseDetailLevel;
228 BOOL update;
229 wLog* log;
230};
231
232WINPR_ATTR_NODISCARD
233static BOOL license_send_error_alert(rdpLicense* license, UINT32 dwErrorCode,
234 UINT32 dwStateTransition, const LICENSE_BLOB* info);
235
236static void license_set_state(rdpLicense* license, LICENSE_STATE state);
237
238WINPR_ATTR_NODISCARD
239static const char* license_get_state_string(LICENSE_STATE state);
240
241WINPR_ATTR_NODISCARD
242static const char* license_preferred_key_exchange_alg_string(UINT32 alg, char* buffer, size_t size)
243{
244 const char* name = nullptr;
245
246 switch (alg)
247 {
248 case KEY_EXCHANGE_ALG_RSA:
249 name = "KEY_EXCHANGE_ALG_RSA";
250 break;
251 default:
252 name = "KEY_EXCHANGE_ALG_UNKNOWN";
253 break;
254 }
255
256 (void)_snprintf(buffer, size, "%s [0x%08" PRIx32 "]", name, alg);
257 return buffer;
258}
259
260WINPR_ATTR_NODISCARD
261static const char* license_request_type_string(UINT32 type)
262{
263 switch (type)
264 {
265 case LICENSE_REQUEST:
266 return "LICENSE_REQUEST";
267 case PLATFORM_CHALLENGE:
268 return "PLATFORM_CHALLENGE";
269 case NEW_LICENSE:
270 return "NEW_LICENSE";
271 case UPGRADE_LICENSE:
272 return "UPGRADE_LICENSE";
273 case LICENSE_INFO:
274 return "LICENSE_INFO";
275 case NEW_LICENSE_REQUEST:
276 return "NEW_LICENSE_REQUEST";
277 case PLATFORM_CHALLENGE_RESPONSE:
278 return "PLATFORM_CHALLENGE_RESPONSE";
279 case ERROR_ALERT:
280 return "ERROR_ALERT";
281 default:
282 return "LICENSE_REQUEST_TYPE_UNKNOWN";
283 }
284}
285
286WINPR_ATTR_NODISCARD
287static const char* license_blob_type_string(UINT16 type)
288{
289 switch (type)
290 {
291 case BB_ANY_BLOB:
292 return "BB_ANY_BLOB";
293 case BB_DATA_BLOB:
294 return "BB_DATA_BLOB";
295 case BB_RANDOM_BLOB:
296 return "BB_RANDOM_BLOB";
297 case BB_CERTIFICATE_BLOB:
298 return "BB_CERTIFICATE_BLOB";
299 case BB_ERROR_BLOB:
300 return "BB_ERROR_BLOB";
301 case BB_ENCRYPTED_DATA_BLOB:
302 return "BB_ENCRYPTED_DATA_BLOB";
303 case BB_KEY_EXCHG_ALG_BLOB:
304 return "BB_KEY_EXCHG_ALG_BLOB";
305 case BB_SCOPE_BLOB:
306 return "BB_SCOPE_BLOB";
307 case BB_CLIENT_USER_NAME_BLOB:
308 return "BB_CLIENT_USER_NAME_BLOB";
309 case BB_CLIENT_MACHINE_NAME_BLOB:
310 return "BB_CLIENT_MACHINE_NAME_BLOB";
311 default:
312 return "BB_UNKNOWN";
313 }
314}
315
316WINPR_ATTR_NODISCARD
317static wStream* license_send_stream_init(rdpLicense* license, UINT16* sec_flags);
318
319WINPR_ATTR_NODISCARD
320static BOOL license_generate_randoms(rdpLicense* license);
321
322WINPR_ATTR_NODISCARD
323static BOOL license_generate_keys(rdpLicense* license);
324
325WINPR_ATTR_NODISCARD
326static BOOL license_generate_hwid(rdpLicense* license);
327
328WINPR_ATTR_NODISCARD
329static BOOL license_encrypt_premaster_secret(rdpLicense* license);
330
331static void license_free_product_info(LICENSE_PRODUCT_INFO* productInfo);
332
333WINPR_ATTR_MALLOC(license_free_product_info, 1)
334static LICENSE_PRODUCT_INFO* license_new_product_info(void);
335
336WINPR_ATTR_NODISCARD
337static BOOL license_read_product_info(wLog* log, wStream* s, LICENSE_PRODUCT_INFO* productInfo);
338
339static void license_free_binary_blob(LICENSE_BLOB* blob);
340
341WINPR_ATTR_MALLOC(license_free_binary_blob, 1)
342static LICENSE_BLOB* license_new_binary_blob(UINT16 type);
343
344WINPR_ATTR_NODISCARD
345static BOOL license_read_binary_blob_data(wLog* log, LICENSE_BLOB* blob, UINT16 type,
346 const void* data, size_t length);
347
348WINPR_ATTR_NODISCARD
349static BOOL license_read_binary_blob(wLog* log, wStream* s, LICENSE_BLOB* blob);
350
351WINPR_ATTR_NODISCARD
352static BOOL license_write_binary_blob(wStream* s, const LICENSE_BLOB* blob);
353
354static void license_free_scope_list(SCOPE_LIST* scopeList);
355
356WINPR_ATTR_MALLOC(license_free_scope_list, 1)
357static SCOPE_LIST* license_new_scope_list(void);
358
359WINPR_ATTR_NODISCARD
360static BOOL license_scope_list_resize(SCOPE_LIST* scopeList, UINT32 count);
361
362WINPR_ATTR_NODISCARD
363static BOOL license_read_scope_list(wLog* log, wStream* s, SCOPE_LIST* scopeList);
364
365WINPR_ATTR_NODISCARD
366static BOOL license_write_scope_list(wLog* log, wStream* s, const SCOPE_LIST* scopeList);
367
368WINPR_ATTR_NODISCARD
369static BOOL license_read_license_request_packet(rdpLicense* license, wStream* s);
370
371WINPR_ATTR_NODISCARD
372static BOOL license_write_license_request_packet(const rdpLicense* license, wStream* s);
373
374WINPR_ATTR_NODISCARD
375static BOOL license_read_platform_challenge_packet(rdpLicense* license, wStream* s);
376
377WINPR_ATTR_NODISCARD
378static BOOL license_send_platform_challenge_packet(rdpLicense* license);
379
380WINPR_ATTR_NODISCARD
381static BOOL license_read_new_or_upgrade_license_packet(rdpLicense* license, wStream* s);
382
383WINPR_ATTR_NODISCARD
384static BOOL license_read_error_alert_packet(rdpLicense* license, wStream* s);
385
386WINPR_ATTR_NODISCARD
387static BOOL license_write_new_license_request_packet(const rdpLicense* license, wStream* s);
388
389WINPR_ATTR_NODISCARD
390static BOOL license_read_new_license_request_packet(rdpLicense* license, wStream* s);
391
392WINPR_ATTR_NODISCARD
393static BOOL license_answer_license_request(rdpLicense* license);
394
395WINPR_ATTR_NODISCARD
396static BOOL license_send_platform_challenge_response(rdpLicense* license);
397
398WINPR_ATTR_NODISCARD
399static BOOL license_read_platform_challenge_response(rdpLicense* license);
400
401WINPR_ATTR_NODISCARD
402static BOOL license_read_client_platform_challenge_response(rdpLicense* license, wStream* s);
403
404WINPR_ATTR_NODISCARD
405static BOOL license_write_client_platform_challenge_response(rdpLicense* license, wStream* s);
406
407WINPR_ATTR_NODISCARD
408static BOOL license_write_server_upgrade_license(const rdpLicense* license, wStream* s);
409
410WINPR_ATTR_NODISCARD
411static BOOL license_send_license_info(rdpLicense* license, const LICENSE_BLOB* calBlob,
412 const BYTE* signature, size_t signature_length);
413
414WINPR_ATTR_NODISCARD
415static BOOL license_read_license_info(rdpLicense* license, wStream* s);
416
417WINPR_ATTR_NODISCARD
418static state_run_t license_client_recv(rdpLicense* license, wStream* s);
419
420WINPR_ATTR_NODISCARD
421static state_run_t license_server_recv(rdpLicense* license, wStream* s);
422
423#define PLATFORMID (CLIENT_OS_ID_WINNT_POST_52 | CLIENT_IMAGE_ID_MICROSOFT)
424
425#ifdef WITH_DEBUG_LICENSE
426
427WINPR_ATTR_NODISCARD
428static const char* error_codes(UINT32 idx)
429{
430#define EVCASE(x) \
431 case x: \
432 return #x
433 switch (idx)
434 {
435 EVCASE(ERR_INVALID_SERVER_CERTIFICATE);
436 EVCASE(ERR_NO_LICENSE);
437 EVCASE(ERR_INVALID_MAC);
438 EVCASE(ERR_INVALID_SCOPE);
439 EVCASE(ERR_NO_LICENSE_SERVER);
440 EVCASE(STATUS_VALID_CLIENT);
441 EVCASE(ERR_INVALID_CLIENT);
442 EVCASE(ERR_INVALID_PRODUCT_ID);
443 EVCASE(ERR_INVALID_MESSAGE_LENGTH);
444 default:
445 return "ERR_UNKNOWN";
446 }
447}
448
449WINPR_ATTR_NODISCARD
450static const char* state_transitions(UINT32 idx)
451{
452#define EVCASE(x) \
453 case x: \
454 return #x
455
456 switch (idx)
457 {
458 EVCASE(ST_TOTAL_ABORT);
459 EVCASE(ST_NO_TRANSITION);
460 EVCASE(ST_RESET_PHASE_TO_START);
461 EVCASE(ST_RESEND_LAST_MESSAGE);
462 default:
463 return "ST_UNKNOWN";
464 }
465}
466
467static void license_print_product_info(wLog* log, const LICENSE_PRODUCT_INFO* productInfo)
468{
469 char* CompanyName = nullptr;
470 char* ProductId = nullptr;
471
472 WINPR_ASSERT(productInfo);
473 WINPR_ASSERT(productInfo->pbCompanyName);
474 WINPR_ASSERT(productInfo->pbProductId);
475
476 CompanyName =
477 ConvertWCharNToUtf8Alloc(WINPR_PACKED_ALIGN_CAST(const WCHAR*, productInfo->pbCompanyName),
478 productInfo->cbCompanyName / sizeof(WCHAR), nullptr);
479 ProductId =
480 ConvertWCharNToUtf8Alloc(WINPR_PACKED_ALIGN_CAST(const WCHAR*, productInfo->pbProductId),
481 productInfo->cbProductId / sizeof(WCHAR), nullptr);
482 WLog_Print(log, WLOG_INFO, "ProductInfo:");
483 WLog_Print(log, WLOG_INFO, "\tdwVersion: 0x%08" PRIX32 "", productInfo->dwVersion);
484 WLog_Print(log, WLOG_INFO, "\tCompanyName: %s", CompanyName);
485 WLog_Print(log, WLOG_INFO, "\tProductId: %s", ProductId);
486 winpr_zfree(CompanyName);
487 winpr_zfree(ProductId);
488}
489
490static void license_print_scope_list(wLog* log, const SCOPE_LIST* scopeList)
491{
492 WINPR_ASSERT(scopeList);
493
494 WLog_Print(log, WLOG_INFO, "ScopeList (%" PRIu32 "):", scopeList->count);
495
496 for (UINT32 index = 0; index < scopeList->count; index++)
497 {
498 WINPR_ASSERT(scopeList->array);
499 const LICENSE_BLOB* scope = scopeList->array[index];
500 WINPR_ASSERT(scope);
501
502 WLog_Print(log, WLOG_INFO, "\t%.*s", (int)scope->length, (const char*)scope->data);
503 }
504}
505#endif
506
507WINPR_ATTR_NODISCARD
508static BOOL license_ensure_state(rdpLicense* license, LICENSE_STATE state, UINT32 msg)
509{
510 const LICENSE_STATE cstate = license_get_state(license);
511
512 WINPR_ASSERT(license);
513
514 if (cstate != state)
515 {
516 const char* scstate = license_get_state_string(cstate);
517 const char* sstate = license_get_state_string(state);
518 const char* where = license_request_type_string(msg);
519
520 WLog_Print(license->log, WLOG_WARN,
521 "Received [%s], but found invalid licensing state %s, expected %s", where,
522 scstate, sstate);
523 return FALSE;
524 }
525 return TRUE;
526}
527
528state_run_t license_recv(rdpLicense* license, wStream* s)
529{
530 WINPR_ASSERT(license);
531 WINPR_ASSERT(license->rdp);
532 WINPR_ASSERT(license->rdp->settings);
533
534 if (freerdp_settings_get_bool(license->rdp->settings, FreeRDP_ServerMode))
535 return license_server_recv(license, s);
536 else
537 return license_client_recv(license, s);
538}
539
540WINPR_ATTR_NODISCARD
541static BOOL license_check_stream_length(wLog* log, wStream* s, UINT64 expect, const char* where,
542 UINT64 limit)
543{
544 const size_t remain = Stream_GetRemainingLength(s);
545
546 WINPR_ASSERT(where);
547
548 if (remain < expect)
549 {
550 WLog_Print(log, WLOG_WARN, "short %s, expected %" PRIu64 " bytes, got %" PRIuz, where,
551 expect, remain);
552 return FALSE;
553 }
554 if ((limit > 0) && (remain > limit))
555 {
556 WLog_Print(log, WLOG_WARN,
557 "oversized %s, got %" PRIuz " bytes, but size is limited to %" PRIu64 ".", where,
558 remain, limit);
559 return FALSE;
560 }
561 return TRUE;
562}
563
564WINPR_ATTR_NODISCARD
565static BOOL license_check_stream_capacity(wLog* log, wStream* s, size_t expect, const char* where)
566{
567 WINPR_ASSERT(where);
568
569 return (Stream_CheckAndLogRequiredCapacityWLogEx(log, WLOG_WARN, s, expect, 1,
570 "%s(%s:%" PRIuz ") %s", __func__, __FILE__,
571 (size_t)__LINE__, where));
572}
573
574WINPR_ATTR_NODISCARD
575static BOOL computeCalHash(wLog* log, const char* hostname, char* hashStr, size_t len)
576{
577 WINPR_DIGEST_CTX* sha1 = nullptr;
578 BOOL ret = FALSE;
579 BYTE hash[WINPR_SHA1_DIGEST_LENGTH] = WINPR_C_ARRAY_INIT;
580
581 WINPR_ASSERT(hostname);
582 WINPR_ASSERT(hashStr);
583
584 if (len < 2 * sizeof(hash) + 1)
585 return FALSE;
586
587 if (!(sha1 = winpr_Digest_New()))
588 goto out;
589 if (!winpr_Digest_Init(sha1, WINPR_MD_SHA1))
590 goto out;
591 if (!winpr_Digest_Update(sha1, hostname, strlen(hostname)))
592 goto out;
593 if (!winpr_Digest_Final(sha1, hash, sizeof(hash)))
594 goto out;
595
596 winpr_BinToHexStringBuffer(hash, sizeof(hash), hashStr, len, FALSE);
597
598 ret = TRUE;
599out:
600 if (!ret)
601 WLog_Print(log, WLOG_ERROR, "failed to generate SHA1 of hostname '%s'", hostname);
602 winpr_Digest_Free(sha1);
603 return ret;
604}
605
606WINPR_ATTR_MALLOC(winpr_zfree, 1)
607static char* licenseCanonicalFilePath(wLog* log, const rdpSettings* settings, const char* hostname,
608 char* extension)
609{
610 WINPR_ASSERT(log);
611 WINPR_ASSERT(settings);
612 WINPR_ASSERT(hostname);
613 WINPR_ASSERT(extension);
614
615 char hash[MAX_PATH] = WINPR_C_ARRAY_INIT;
616
617 if (!computeCalHash(log, hostname, hash, sizeof(hash)))
618 {
619 WLog_Print(log, WLOG_ERROR, "unable to compute hostname hash");
620 return nullptr;
621 }
622
623 const char* path = freerdp_settings_get_string(settings, FreeRDP_LicenseStorePath);
624 if (!path)
625 {
626 WLog_Print(log, WLOG_ERROR, "No LicenseStorePath available");
627 return nullptr;
628 }
629
630 if (!winpr_PathFileExists(path))
631 {
632 if (!winpr_PathMakePath(path, nullptr))
633 {
634 WLog_Print(log, WLOG_ERROR, "error creating directory '%s'", path);
635 return nullptr;
636 }
637 WLog_Print(log, WLOG_INFO, "creating directory %s", path);
638 }
639 return GetCombinedPathV(path, "%s.%s", hash, extension);
640}
641
642WINPR_ATTR_NODISCARD
643static BOOL saveCal(wLog* log, const rdpSettings* settings, const BYTE* data, size_t length,
644 const char* hostname)
645{
646 FILE* fp = nullptr;
647
648 size_t written = 0;
649 BOOL ret = FALSE;
650
651 WINPR_ASSERT(data || (length == 0));
652 WINPR_ASSERT(hostname);
653
654 char* filepath = licenseCanonicalFilePath(log, settings, hostname, "cal");
655 char* filepathNew = licenseCanonicalFilePath(log, settings, hostname, "cal.new");
656 if (!filepath)
657 {
658 WLog_Print(log, WLOG_ERROR, "Failed to get license file path");
659 goto out;
660 }
661
662 if (!filepathNew)
663 {
664 WLog_Print(log, WLOG_ERROR, "Failed to get license new file path");
665 goto out;
666 }
667
668 fp = winpr_fopen(filepathNew, "wb");
669 if (!fp)
670 {
671 WLog_Print(log, WLOG_ERROR, "Failed to open license file '%s'", filepathNew);
672 goto out;
673 }
674
675 written = fwrite(data, length, 1, fp);
676 (void)fclose(fp);
677
678 if (written != 1)
679 {
680 WLog_Print(log, WLOG_ERROR, "Failed to write to license file '%s'", filepathNew);
681 winpr_DeleteFile(filepathNew);
682 goto out;
683 }
684
685 ret = winpr_MoveFileEx(filepathNew, filepath, MOVEFILE_REPLACE_EXISTING);
686 if (!ret)
687 WLog_Print(log, WLOG_ERROR, "Failed to move license file '%s' to '%s'", filepathNew,
688 filepath);
689
690out:
691 winpr_zfree(filepathNew);
692 winpr_zfree(filepath);
693 return ret;
694}
695
696WINPR_ATTR_MALLOC(free, 1)
697static BYTE* loadCalFile(wLog* log, const rdpSettings* settings, const char* hostname,
698 size_t* dataLen)
699{
700 INT64 length = 0;
701 size_t status = 0;
702 FILE* fp = nullptr;
703 BYTE* ret = nullptr;
704
705 WINPR_ASSERT(settings);
706 WINPR_ASSERT(hostname);
707 WINPR_ASSERT(dataLen);
708
709 char* calPath = licenseCanonicalFilePath(log, settings, hostname, "cal");
710 if (!calPath)
711 goto fail;
712
713 fp = winpr_fopen(calPath, "rb");
714 if (!fp)
715 goto fail;
716
717 if (_fseeki64(fp, 0, SEEK_END) != 0)
718 goto fail;
719 length = _ftelli64(fp);
720 if (_fseeki64(fp, 0, SEEK_SET) != 0)
721 goto fail;
722 if (length < 0)
723 goto fail;
724
725 if ((UINT64)length > LICENSE_MAX_CAL_LENGTH)
726 {
727 WLog_Print(log, WLOG_ERROR,
728 "%s: the file size %" PRId64 " exceeds the CAL size limit of %llu", __func__,
729 length, LICENSE_MAX_CAL_LENGTH);
730 goto fail;
731 }
732
733 ret = (BYTE*)malloc((size_t)length);
734 if (!ret)
735 goto fail;
736
737 status = fread(ret, (size_t)length, 1, fp);
738 if (status == 0)
739 goto fail;
740
741 *dataLen = (size_t)length;
742
743 (void)fclose(fp);
744 winpr_zfree(calPath);
745 return ret;
746
747fail:
748 if (ret)
749 winpr_znfree(ret, (size_t)length);
750 if (fp)
751 (void)fclose(fp);
752 winpr_zfree(calPath);
753 return nullptr;
754}
755
765WINPR_ATTR_NODISCARD
766static BOOL license_read_preamble(wLog* log, wStream* s, BYTE* bMsgType, BYTE* flags,
767 UINT16* wMsgSize)
768{
769 WINPR_ASSERT(bMsgType);
770 WINPR_ASSERT(flags);
771 WINPR_ASSERT(wMsgSize);
772
773 /* preamble (4 bytes) */
774 if (!license_check_stream_length(log, s, 4, "license preamble", 0))
775 return FALSE;
776
777 Stream_Read_UINT8(s, *bMsgType); /* bMsgType (1 byte) */
778 Stream_Read_UINT8(s, *flags); /* flags (1 byte) */
779 Stream_Read_UINT16(s, *wMsgSize); /* wMsgSize (2 bytes) */
780 if (*wMsgSize < 4)
781 {
782 WLog_Print(log, WLOG_WARN,
783 "invalid license preamble::wMsgSize, expected value >= 4, got %" PRIu32,
784 *wMsgSize);
785 return FALSE;
786 }
787 return license_check_stream_length(log, s, *wMsgSize - 4ull, "license preamble::wMsgSize", 0);
788}
789
799WINPR_ATTR_NODISCARD
800static BOOL license_write_preamble(wStream* s, BYTE bMsgType, BYTE flags, UINT16 wMsgSize)
801{
802 if (!Stream_EnsureRemainingCapacity(s, 4))
803 return FALSE;
804
805 /* preamble (4 bytes) */
806 Stream_Write_UINT8(s, bMsgType); /* bMsgType (1 byte) */
807 Stream_Write_UINT8(s, flags); /* flags (1 byte) */
808 Stream_Write_UINT16(s, wMsgSize); /* wMsgSize (2 bytes) */
809 return TRUE;
810}
811
819
820wStream* license_send_stream_init(rdpLicense* license, UINT16* sec_flags)
821{
822 WINPR_ASSERT(license);
823 WINPR_ASSERT(license->rdp);
824 WINPR_ASSERT(sec_flags);
825
826 const BOOL do_crypt = license->rdp->do_crypt;
827
828 *sec_flags = SEC_LICENSE_PKT;
829
830 /*
831 * Encryption of licensing packets is optional even if the rdp security
832 * layer is used. If the peer has not indicated that it is capable of
833 * processing encrypted licensing packets (rdp->do_crypt_license) we turn
834 * off encryption (via rdp->do_crypt) before initializing the rdp stream
835 * and re-enable it afterwards.
836 */
837
838 if (do_crypt)
839 {
840 *sec_flags |= SEC_LICENSE_ENCRYPT_CS;
841 license->rdp->do_crypt = license->rdp->do_crypt_license;
842 }
843
844 wStream* s = rdp_send_stream_init(license->rdp, sec_flags);
845 if (!s)
846 return nullptr;
847
848 license->rdp->do_crypt = do_crypt;
849 license->PacketHeaderLength = (UINT16)Stream_GetPosition(s);
850 if (!Stream_SafeZero(s, LICENSE_PREAMBLE_LENGTH))
851 goto fail;
852 return s;
853
854fail:
855 Stream_Release(s);
856 return nullptr;
857}
858
865WINPR_ATTR_NODISCARD
866static BOOL license_send(rdpLicense* license, wStream* s, BYTE type, UINT16 sec_flags)
867{
868 WINPR_ASSERT(license);
869 WINPR_ASSERT(license->rdp);
870
871 rdpRdp* rdp = license->rdp;
872 WINPR_ASSERT(rdp->settings);
873
874 DEBUG_LICENSE("Sending %s Packet", license_request_type_string(type));
875 const size_t length = Stream_GetPosition(s);
876 if (length < license->PacketHeaderLength)
877 return FALSE;
878 if (length > UINT16_MAX + license->PacketHeaderLength)
879 return FALSE;
880
881 const UINT16 wMsgSize = (UINT16)(length - license->PacketHeaderLength);
882 if (!Stream_SetPosition(s, license->PacketHeaderLength))
883 return FALSE;
884 BYTE flags = PREAMBLE_VERSION_3_0;
885
890
891 if (!rdp->settings->ServerMode)
892 flags |= EXTENDED_ERROR_MSG_SUPPORTED;
893
894 if (!license_write_preamble(s, type, flags, wMsgSize))
895 {
896 Stream_Release(s);
897 return FALSE;
898 }
899
900#ifdef WITH_DEBUG_LICENSE
901 WLog_Print(license->log, WLOG_DEBUG, "Sending %s Packet, length %" PRIu16 "",
902 license_request_type_string(type), wMsgSize);
903 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_PointerAs(s, char) - LICENSE_PREAMBLE_LENGTH,
904 wMsgSize);
905#endif
906 if (!Stream_SetPosition(s, length))
907 return FALSE;
908 const BOOL ret = rdp_send(rdp, s, MCS_GLOBAL_CHANNEL_ID, sec_flags);
909 return ret;
910}
911
912BOOL license_write_server_upgrade_license(const rdpLicense* license, wStream* s)
913{
914 WINPR_ASSERT(license);
915
916 if (!license_write_binary_blob(s, license->EncryptedLicenseInfo))
917 return FALSE;
918 if (!license_check_stream_capacity(license->log, s, sizeof(license->MACData),
919 "SERVER_UPGRADE_LICENSE::MACData"))
920 return FALSE;
921 Stream_Write(s, license->MACData, sizeof(license->MACData));
922 return TRUE;
923}
924
925WINPR_ATTR_NODISCARD
926static BOOL license_server_send_new_or_upgrade_license(rdpLicense* license, BOOL upgrade)
927{
928 UINT16 sec_flags = 0;
929 wStream* s = license_send_stream_init(license, &sec_flags);
930 const BYTE type = upgrade ? UPGRADE_LICENSE : NEW_LICENSE;
931
932 if (!s)
933 return FALSE;
934
935 if (!license_write_server_upgrade_license(license, s))
936 goto fail;
937
938 return license_send(license, s, type, sec_flags);
939
940fail:
941 Stream_Release(s);
942 return FALSE;
943}
944
952WINPR_ATTR_NODISCARD
953static state_run_t license_client_recv_int(rdpLicense* license, wStream* s)
954{
955 BYTE flags = 0;
956 BYTE bMsgType = 0;
957 UINT16 wMsgSize = 0;
958 const size_t length = Stream_GetRemainingLength(s);
959
960 WINPR_ASSERT(license);
961
962 if (!license_read_preamble(license->log, s, &bMsgType, &flags,
963 &wMsgSize)) /* preamble (4 bytes) */
964 return STATE_RUN_FAILED;
965
966 DEBUG_LICENSE("Receiving %s Packet", license_request_type_string(bMsgType));
967
968 switch (bMsgType)
969 {
970 case LICENSE_REQUEST:
971 /* Client does not require configuration, so skip this state */
972 if (license_get_state(license) == LICENSE_STATE_INITIAL)
973 license_set_state(license, LICENSE_STATE_CONFIGURED);
974
975 if (!license_ensure_state(license, LICENSE_STATE_CONFIGURED, bMsgType))
976 return STATE_RUN_FAILED;
977
978 if (!license_read_license_request_packet(license, s))
979 return STATE_RUN_FAILED;
980
981 if (!license_answer_license_request(license))
982 return STATE_RUN_FAILED;
983
984 license_set_state(license, LICENSE_STATE_NEW_REQUEST);
985 break;
986
987 case PLATFORM_CHALLENGE:
988 if (!license_ensure_state(license, LICENSE_STATE_NEW_REQUEST, bMsgType))
989 return STATE_RUN_FAILED;
990
991 if (!license_read_platform_challenge_packet(license, s))
992 return STATE_RUN_FAILED;
993
994 if (!license_send_platform_challenge_response(license))
995 return STATE_RUN_FAILED;
996 license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE);
997 break;
998
999 case NEW_LICENSE:
1000 case UPGRADE_LICENSE:
1001 if (!license_ensure_state(license, LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE, bMsgType))
1002 return STATE_RUN_FAILED;
1003 if (!license_read_new_or_upgrade_license_packet(license, s))
1004 return STATE_RUN_FAILED;
1005 break;
1006
1007 case ERROR_ALERT:
1008 if (!license_read_error_alert_packet(license, s))
1009 return STATE_RUN_FAILED;
1010 break;
1011
1012 default:
1013 WLog_Print(license->log, WLOG_ERROR, "invalid bMsgType:%" PRIu8 "", bMsgType);
1014 return STATE_RUN_FAILED;
1015 }
1016
1017 if (!tpkt_ensure_stream_consumed(license->log, s, length))
1018 return STATE_RUN_FAILED;
1019 return STATE_RUN_SUCCESS;
1020}
1021
1022state_run_t license_client_recv(rdpLicense* license, wStream* s)
1023{
1024 state_run_t rc = license_client_recv_int(license, s);
1025 if (state_run_failed(rc))
1026 {
1027 freerdp_set_last_error(license->rdp->context, ERROR_CTX_LICENSE_CLIENT_INVALID);
1028 }
1029 return rc;
1030}
1031
1032state_run_t license_server_recv(rdpLicense* license, wStream* s)
1033{
1034 state_run_t rc = STATE_RUN_FAILED;
1035 BYTE flags = 0;
1036 BYTE bMsgType = 0;
1037 UINT16 wMsgSize = 0;
1038 const size_t length = Stream_GetRemainingLength(s);
1039
1040 WINPR_ASSERT(license);
1041
1042 if (!license_read_preamble(license->log, s, &bMsgType, &flags,
1043 &wMsgSize)) /* preamble (4 bytes) */
1044 goto fail;
1045
1046 DEBUG_LICENSE("Receiving %s Packet", license_request_type_string(bMsgType));
1047
1048 switch (bMsgType)
1049 {
1050 case NEW_LICENSE_REQUEST:
1051 if (!license_ensure_state(license, LICENSE_STATE_REQUEST, bMsgType))
1052 goto fail;
1053 if (!license_read_new_license_request_packet(license, s))
1054 goto fail;
1055 // TODO: Validate if client is allowed
1056 if (!license_send_error_alert(license, ERR_INVALID_MAC, ST_TOTAL_ABORT,
1057 license->ErrorInfo))
1058 goto fail;
1059 if (!license_send_platform_challenge_packet(license))
1060 goto fail;
1061 license->update = FALSE;
1062 license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE);
1063 break;
1064 case LICENSE_INFO:
1065 if (!license_ensure_state(license, LICENSE_STATE_REQUEST, bMsgType))
1066 goto fail;
1067 if (!license_read_license_info(license, s))
1068 goto fail;
1069 // TODO: Validate license info
1070 if (!license_send_platform_challenge_packet(license))
1071 goto fail;
1072 license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE);
1073 license->update = TRUE;
1074 break;
1075
1076 case PLATFORM_CHALLENGE_RESPONSE:
1077 if (!license_ensure_state(license, LICENSE_STATE_PLATFORM_CHALLENGE, bMsgType))
1078 goto fail;
1079 if (!license_read_client_platform_challenge_response(license, s))
1080 goto fail;
1081
1082 // TODO: validate challenge response
1083 if (FALSE)
1084 {
1085 if (license_send_error_alert(license, ERR_INVALID_CLIENT, ST_TOTAL_ABORT,
1086 license->ErrorInfo))
1087 goto fail;
1088 }
1089 else
1090 {
1091 if (!license_server_send_new_or_upgrade_license(license, license->update))
1092 goto fail;
1093
1094 license->type = LICENSE_TYPE_ISSUED;
1095 license_set_state(license, LICENSE_STATE_COMPLETED);
1096
1097 rc = STATE_RUN_CONTINUE; /* License issued, switch state */
1098 }
1099 break;
1100
1101 case ERROR_ALERT:
1102 if (!license_read_error_alert_packet(license, s))
1103 goto fail;
1104 break;
1105
1106 default:
1107 WLog_Print(license->log, WLOG_ERROR, "invalid bMsgType:%" PRIu8 "", bMsgType);
1108 goto fail;
1109 }
1110
1111 if (!tpkt_ensure_stream_consumed(license->log, s, length))
1112 goto fail;
1113
1114 if (!state_run_success(rc))
1115 rc = STATE_RUN_SUCCESS;
1116
1117fail:
1118 if (state_run_failed(rc))
1119 {
1120 if (flags & EXTENDED_ERROR_MSG_SUPPORTED)
1121 {
1122 if (!license_send_error_alert(license, ERR_INVALID_CLIENT, ST_TOTAL_ABORT, nullptr))
1123 {
1124 WLog_Print(license->log, WLOG_ERROR, "license_send_error_alert failed");
1125 }
1126 }
1127 license_set_state(license, LICENSE_STATE_ABORTED);
1128 }
1129
1130 return rc;
1131}
1132
1133BOOL license_generate_randoms(rdpLicense* license)
1134{
1135 WINPR_ASSERT(license);
1136
1137#ifdef LICENSE_NULL_CLIENT_RANDOM
1138 ZeroMemory(license->ClientRandom, sizeof(license->ClientRandom)); /* ClientRandom */
1139#else
1140 if (winpr_RAND(license->ClientRandom, sizeof(license->ClientRandom)) < 0) /* ClientRandom */
1141 return FALSE;
1142#endif
1143
1144 if (winpr_RAND(license->ServerRandom, sizeof(license->ServerRandom)) < 0) /* ServerRandom */
1145 return FALSE;
1146
1147#ifdef LICENSE_NULL_PREMASTER_SECRET
1148 ZeroMemory(license->PremasterSecret, sizeof(license->PremasterSecret)); /* PremasterSecret */
1149#else
1150 if (winpr_RAND(license->PremasterSecret, sizeof(license->PremasterSecret)) <
1151 0) /* PremasterSecret */
1152 return FALSE;
1153#endif
1154 return TRUE;
1155}
1156
1161WINPR_ATTR_NODISCARD
1162static BOOL license_generate_keys(rdpLicense* license)
1163{
1164 WINPR_ASSERT(license);
1165
1166 if (
1167 /* MasterSecret */
1168 !security_master_secret(license->PremasterSecret, sizeof(license->PremasterSecret),
1169 license->ClientRandom, sizeof(license->ClientRandom),
1170 license->ServerRandom, sizeof(license->ServerRandom),
1171 license->MasterSecret, sizeof(license->MasterSecret)) ||
1172 /* SessionKeyBlob */
1173 !security_session_key_blob(license->MasterSecret, sizeof(license->MasterSecret),
1174 license->ClientRandom, sizeof(license->ClientRandom),
1175 license->ServerRandom, sizeof(license->ServerRandom),
1176 license->SessionKeyBlob, sizeof(license->SessionKeyBlob)))
1177 {
1178 return FALSE;
1179 }
1180 security_mac_salt_key(license->SessionKeyBlob, sizeof(license->SessionKeyBlob),
1181 license->ClientRandom, sizeof(license->ClientRandom),
1182 license->ServerRandom, sizeof(license->ServerRandom), license->MacSaltKey,
1183 sizeof(license->MacSaltKey)); /* MacSaltKey */
1184 const BOOL ret = security_licensing_encryption_key(
1185 license->SessionKeyBlob, sizeof(license->SessionKeyBlob), license->ClientRandom,
1186 sizeof(license->ClientRandom), license->ServerRandom, sizeof(license->ServerRandom),
1187 license->LicensingEncryptionKey,
1188 sizeof(license->LicensingEncryptionKey)); /* LicensingEncryptionKey */
1189
1190 WLog_Print(license->log, WLOG_TRACE, "license keys %s generated", ret ? "successfully" : "NOT");
1191
1192#ifdef WITH_DEBUG_LICENSE
1193 WLog_Print(license->log, WLOG_DEBUG, "ClientRandom:");
1194 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ClientRandom,
1195 sizeof(license->ClientRandom));
1196 WLog_Print(license->log, WLOG_DEBUG, "ServerRandom:");
1197 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ServerRandom,
1198 sizeof(license->ServerRandom));
1199 WLog_Print(license->log, WLOG_DEBUG, "PremasterSecret:");
1200 winpr_HexLogDump(license->log, WLOG_DEBUG, license->PremasterSecret,
1201 sizeof(license->PremasterSecret));
1202 WLog_Print(license->log, WLOG_DEBUG, "MasterSecret:");
1203 winpr_HexLogDump(license->log, WLOG_DEBUG, license->MasterSecret,
1204 sizeof(license->MasterSecret));
1205 WLog_Print(license->log, WLOG_DEBUG, "SessionKeyBlob:");
1206 winpr_HexLogDump(license->log, WLOG_DEBUG, license->SessionKeyBlob,
1207 sizeof(license->SessionKeyBlob));
1208 WLog_Print(license->log, WLOG_DEBUG, "MacSaltKey:");
1209 winpr_HexLogDump(license->log, WLOG_DEBUG, license->MacSaltKey, sizeof(license->MacSaltKey));
1210 WLog_Print(license->log, WLOG_DEBUG, "LicensingEncryptionKey:");
1211 winpr_HexLogDump(license->log, WLOG_DEBUG, license->LicensingEncryptionKey,
1212 sizeof(license->LicensingEncryptionKey));
1213#endif
1214 return ret;
1215}
1216
1221
1222BOOL license_generate_hwid(rdpLicense* license)
1223{
1224 const BYTE* hashTarget = nullptr;
1225 size_t targetLen = 0;
1226 BYTE macAddress[6] = WINPR_C_ARRAY_INIT;
1227
1228 WINPR_ASSERT(license);
1229 WINPR_ASSERT(license->rdp);
1230 WINPR_ASSERT(license->rdp->settings);
1231
1232 ZeroMemory(license->HardwareId, sizeof(license->HardwareId));
1233
1234 if (license->rdp->settings->OldLicenseBehaviour)
1235 {
1236 hashTarget = macAddress;
1237 targetLen = sizeof(macAddress);
1238 }
1239 else
1240 {
1241 wStream buffer = WINPR_C_ARRAY_INIT;
1242 const char* hostname = license->rdp->settings->ClientHostname;
1243 wStream* s = Stream_StaticInit(&buffer, license->HardwareId, 4);
1244 Stream_Write_UINT32(s, license->PlatformId);
1245
1246 hashTarget = (const BYTE*)hostname;
1247 targetLen = hostname ? strlen(hostname) : 0;
1248 }
1249
1250 /* Allow FIPS override for use of MD5 here, really this does not have to be MD5 as we are just
1251 * taking a MD5 hash of the 6 bytes of 0's(macAddress) */
1252 /* and filling in the Data1-Data4 fields of the CLIENT_HARDWARE_ID structure(from MS-RDPELE
1253 * section 2.2.2.3.1). This is for RDP licensing packets */
1254 /* which will already be encrypted under FIPS, so the use of MD5 here is not for sensitive data
1255 * protection. */
1256 return winpr_Digest_Allow_FIPS(WINPR_MD_MD5, hashTarget, targetLen,
1257 &license->HardwareId[HWID_PLATFORM_ID_LENGTH],
1258 WINPR_MD5_DIGEST_LENGTH);
1259}
1260
1261WINPR_ATTR_NODISCARD
1262static BOOL license_get_server_rsa_public_key(rdpLicense* license)
1263{
1264 rdpSettings* settings = nullptr;
1265
1266 WINPR_ASSERT(license);
1267 WINPR_ASSERT(license->certificate);
1268 WINPR_ASSERT(license->rdp);
1269
1270 settings = license->rdp->settings;
1271 WINPR_ASSERT(settings);
1272
1273 if (license->ServerCertificate->length < 1)
1274 {
1275 if (!freerdp_certificate_read_server_cert(license->certificate, settings->ServerCertificate,
1276 settings->ServerCertificateLength))
1277 return FALSE;
1278 }
1279
1280 return TRUE;
1281}
1282
1283BOOL license_encrypt_premaster_secret(rdpLicense* license)
1284{
1285 WINPR_ASSERT(license);
1286 WINPR_ASSERT(license->certificate);
1287
1288 if (!license_get_server_rsa_public_key(license))
1289 return FALSE;
1290
1291 WINPR_ASSERT(license->EncryptedPremasterSecret);
1292
1293 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1294 if (!info)
1295 {
1296 WLog_Print(license->log, WLOG_ERROR, "info=%p, license->certificate=%p", (const void*)info,
1297 (void*)license->certificate);
1298 return FALSE;
1299 }
1300
1301#ifdef WITH_DEBUG_LICENSE
1302 WLog_Print(license->log, WLOG_DEBUG, "Modulus (%" PRIu32 " bits):", info->ModulusLength * 8);
1303 winpr_HexLogDump(license->log, WLOG_DEBUG, info->Modulus, info->ModulusLength);
1304 WLog_Print(license->log, WLOG_DEBUG, "Exponent:");
1305 winpr_HexLogDump(license->log, WLOG_DEBUG, info->exponent, sizeof(info->exponent));
1306#endif
1307
1308 BYTE* EncryptedPremasterSecret = (BYTE*)calloc(1, info->ModulusLength);
1309 if (!EncryptedPremasterSecret)
1310 {
1311 WLog_Print(license->log, WLOG_ERROR,
1312 "EncryptedPremasterSecret=%p, info->ModulusLength=%" PRIu32,
1313 (const void*)EncryptedPremasterSecret, info->ModulusLength);
1314 return FALSE;
1315 }
1316
1317 license->EncryptedPremasterSecret->type = BB_RANDOM_BLOB;
1318 license->EncryptedPremasterSecret->length = sizeof(license->PremasterSecret);
1319#ifndef LICENSE_NULL_PREMASTER_SECRET
1320 {
1321 const SSIZE_T length =
1322 crypto_rsa_public_encrypt(license->PremasterSecret, sizeof(license->PremasterSecret),
1323 info, EncryptedPremasterSecret, info->ModulusLength);
1324 if ((length < 0) || (length > UINT16_MAX))
1325 {
1326 WLog_Print(license->log, WLOG_ERROR, "RSA public encrypt length=%" PRIdz " < 0 || > %d",
1327 length, UINT16_MAX);
1328 winpr_znfree(EncryptedPremasterSecret, info->ModulusLength);
1329 return FALSE;
1330 }
1331 license->EncryptedPremasterSecret->length = (UINT16)length;
1332 }
1333#endif
1334 license->EncryptedPremasterSecret->data = EncryptedPremasterSecret;
1335 return TRUE;
1336}
1337
1338WINPR_ATTR_NODISCARD
1339static BOOL license_rc4_with_licenseKey(const rdpLicense* license, const BYTE* input, size_t len,
1340 LICENSE_BLOB* target)
1341{
1342 WINPR_ASSERT(license);
1343 WINPR_ASSERT(input || (len == 0));
1344 WINPR_ASSERT(target);
1345 WINPR_ASSERT(len <= UINT16_MAX);
1346
1347 WINPR_RC4_CTX* rc4 = winpr_RC4_New_Allow_FIPS(license->LicensingEncryptionKey,
1348 sizeof(license->LicensingEncryptionKey));
1349 if (!rc4)
1350 {
1351 WLog_Print(license->log, WLOG_ERROR, "Failed to allocate RC4");
1352 return FALSE;
1353 }
1354
1355 BYTE* buffer = nullptr;
1356 if (len > 0)
1357 buffer = realloc(target->data, len);
1358 if (!buffer)
1359 goto error_buffer;
1360
1361 target->data = buffer;
1362 target->length = (UINT16)len;
1363
1364 if (!winpr_RC4_Update(rc4, len, input, buffer))
1365 goto error_buffer;
1366
1367 winpr_RC4_Free(rc4);
1368 return TRUE;
1369
1370error_buffer:
1371 WLog_Print(license->log, WLOG_ERROR, "Failed to create/update RC4: len=%" PRIuz ", buffer=%p",
1372 len, (const void*)buffer);
1373 winpr_RC4_Free(rc4);
1374 return FALSE;
1375}
1376
1387
1388WINPR_ATTR_NODISCARD
1389static BOOL license_encrypt_and_MAC(rdpLicense* license, const BYTE* input, size_t len,
1390 LICENSE_BLOB* target, BYTE* mac, size_t mac_length)
1391{
1392 WINPR_ASSERT(license);
1393 return license_rc4_with_licenseKey(license, input, len, target) &&
1394 security_mac_data(license->MacSaltKey, sizeof(license->MacSaltKey), input, len, mac,
1395 mac_length);
1396}
1397
1409WINPR_ATTR_NODISCARD
1410static BOOL license_decrypt_and_check_MAC(rdpLicense* license, const BYTE* input, size_t len,
1411 LICENSE_BLOB* target, const BYTE* packetMac)
1412{
1413 BYTE macData[sizeof(license->MACData)] = WINPR_C_ARRAY_INIT;
1414
1415 WINPR_ASSERT(license);
1416 WINPR_ASSERT(target);
1417
1418 if (freerdp_settings_get_bool(license->rdp->settings, FreeRDP_TransportDumpReplay))
1419 {
1420 WLog_Print(license->log, WLOG_DEBUG, "TransportDumpReplay active, skipping...");
1421 return TRUE;
1422 }
1423
1424 if (!license_rc4_with_licenseKey(license, input, len, target))
1425 return FALSE;
1426
1427 if (!security_mac_data(license->MacSaltKey, sizeof(license->MacSaltKey), target->data, len,
1428 macData, sizeof(macData)))
1429 return FALSE;
1430
1431 if (memcmp(packetMac, macData, sizeof(macData)) != 0)
1432 {
1433 WLog_Print(license->log, WLOG_ERROR, "packetMac != expectedMac");
1434 return FALSE;
1435 }
1436 return TRUE;
1437}
1438
1444
1445static void clear_productinfo(LICENSE_PRODUCT_INFO* productInfo)
1446{
1447 WINPR_ASSERT(productInfo);
1448 winpr_znfree(productInfo->pbCompanyName, productInfo->cbCompanyName);
1449 winpr_znfree(productInfo->pbProductId, productInfo->cbProductId);
1450 productInfo->pbCompanyName = nullptr;
1451 productInfo->cbCompanyName = 0;
1452 productInfo->pbProductId = nullptr;
1453 productInfo->cbProductId = 0;
1454}
1455
1456BOOL license_read_product_info(wLog* log, wStream* s, LICENSE_PRODUCT_INFO* productInfo)
1457{
1458 WINPR_ASSERT(productInfo);
1459
1460 clear_productinfo(productInfo);
1461
1462 if (!license_check_stream_length(log, s, 8, "license product info::cbCompanyName", 0))
1463 return FALSE;
1464
1465 Stream_Read_UINT32(s, productInfo->dwVersion); /* dwVersion (4 bytes) */
1466 const UINT32 cbCompanyName = Stream_Get_UINT32(s); /* cbCompanyName (4 bytes) */
1467
1468 /* Name must be >0, but there is no upper limit defined, use UINT32_MAX */
1469 if ((cbCompanyName < 2) || (cbCompanyName % 2 != 0))
1470 {
1471 WLog_Print(log, WLOG_WARN, "license product info invalid cbCompanyName %" PRIu32,
1472 cbCompanyName);
1473 return FALSE;
1474 }
1475
1476 if (!license_check_stream_length(log, s, cbCompanyName, "license product info::CompanyName",
1477 LICENSE_MAX_COMPANY_NAME_LENGTH))
1478 return FALSE;
1479
1480 productInfo->pbCompanyName = (BYTE*)malloc(cbCompanyName);
1481 if (!productInfo->pbCompanyName)
1482 goto out_fail;
1483 productInfo->cbCompanyName = cbCompanyName;
1484 Stream_Read(s, productInfo->pbCompanyName, productInfo->cbCompanyName);
1485
1486 if (!license_check_stream_length(log, s, 4, "license product info::cbProductId", 0))
1487 goto out_fail;
1488
1489 const UINT32 cbProductId = Stream_Get_UINT32(s); /* cbProductId (4 bytes) */
1490
1491 if ((cbProductId < 2) || (cbProductId % 2 != 0))
1492 {
1493 WLog_Print(log, WLOG_WARN, "license product info invalid cbProductId %" PRIu32,
1494 cbProductId);
1495 goto out_fail;
1496 }
1497
1498 if (!license_check_stream_length(log, s, cbProductId, "license product info::ProductId",
1499 LICENSE_MAX_PRODUCT_ID_LENGTH))
1500 goto out_fail;
1501
1502 productInfo->pbProductId = (BYTE*)malloc(cbProductId);
1503 if (!productInfo->pbProductId)
1504 goto out_fail;
1505 productInfo->cbProductId = cbProductId;
1506 Stream_Read(s, productInfo->pbProductId, productInfo->cbProductId);
1507 return TRUE;
1508
1509out_fail:
1510 clear_productinfo(productInfo);
1511 return FALSE;
1512}
1513
1514WINPR_ATTR_NODISCARD
1515static BOOL license_write_product_info(wLog* log, wStream* s,
1516 const LICENSE_PRODUCT_INFO* productInfo)
1517{
1518 WINPR_ASSERT(productInfo);
1519
1520 if (!license_check_stream_capacity(log, s, 8, "license product info::cbCompanyName"))
1521 return FALSE;
1522
1523 Stream_Write_UINT32(s, productInfo->dwVersion); /* dwVersion (4 bytes) */
1524 Stream_Write_UINT32(s, productInfo->cbCompanyName); /* cbCompanyName (4 bytes) */
1525
1526 /* Name must be >0, but there is no upper limit defined, use UINT32_MAX */
1527 if ((productInfo->cbCompanyName < 2) || (productInfo->cbCompanyName % 2 != 0) ||
1528 !productInfo->pbCompanyName)
1529 {
1530 WLog_Print(log, WLOG_WARN, "license product info invalid cbCompanyName %" PRIu32,
1531 productInfo->cbCompanyName);
1532 return FALSE;
1533 }
1534
1535 if (!license_check_stream_capacity(log, s, productInfo->cbCompanyName,
1536 "license product info::CompanyName"))
1537 return FALSE;
1538
1539 Stream_Write(s, productInfo->pbCompanyName, productInfo->cbCompanyName);
1540
1541 if (!license_check_stream_capacity(log, s, 4, "license product info::cbProductId"))
1542 return FALSE;
1543
1544 Stream_Write_UINT32(s, productInfo->cbProductId); /* cbProductId (4 bytes) */
1545
1546 if ((productInfo->cbProductId < 2) || (productInfo->cbProductId % 2 != 0) ||
1547 !productInfo->pbProductId)
1548 {
1549 WLog_Print(log, WLOG_WARN, "license product info invalid cbProductId %" PRIu32,
1550 productInfo->cbProductId);
1551 return FALSE;
1552 }
1553
1554 if (!license_check_stream_capacity(log, s, productInfo->cbProductId,
1555 "license product info::ProductId"))
1556 return FALSE;
1557
1558 Stream_Write(s, productInfo->pbProductId, productInfo->cbProductId);
1559 return TRUE;
1560}
1561
1567
1568LICENSE_PRODUCT_INFO* license_new_product_info(void)
1569{
1570 LICENSE_PRODUCT_INFO* productInfo =
1571 (LICENSE_PRODUCT_INFO*)calloc(1, sizeof(LICENSE_PRODUCT_INFO));
1572 if (!productInfo)
1573 return nullptr;
1574 return productInfo;
1575}
1576
1582
1583void license_free_product_info(LICENSE_PRODUCT_INFO* productInfo)
1584{
1585 if (productInfo)
1586 {
1587 clear_productinfo(productInfo);
1588 winpr_znfree(productInfo, sizeof(LICENSE_PRODUCT_INFO));
1589 }
1590}
1591
1592BOOL license_read_binary_blob_data(wLog* log, LICENSE_BLOB* blob, UINT16 wBlobType,
1593 const void* data, size_t length)
1594{
1595 WINPR_ASSERT(blob);
1596 WINPR_ASSERT(length <= UINT16_MAX);
1597 WINPR_ASSERT(data || (length == 0));
1598
1599 winpr_znfree(blob->data, blob->length);
1600 blob->length = (UINT16)length;
1601 blob->data = nullptr;
1602
1603 if ((blob->type != wBlobType) && (blob->type != BB_ANY_BLOB))
1604 {
1605 WLog_Print(log, WLOG_ERROR, "license binary blob::type expected %s, got %s",
1606 license_blob_type_string(wBlobType), license_blob_type_string(blob->type));
1607 }
1608
1609 /*
1610 * Server can choose to not send data by setting length to 0.
1611 * If so, it may not bother to set the type, so shortcut the warning
1612 */
1613 if ((blob->type != BB_ANY_BLOB) && (blob->length == 0))
1614 {
1615 WLog_Print(log, WLOG_DEBUG, "license binary blob::type %s, length=0, skipping.",
1616 license_blob_type_string(blob->type));
1617 return TRUE;
1618 }
1619
1620 blob->type = wBlobType;
1621 blob->data = nullptr;
1622 if (blob->length > 0)
1623 blob->data = malloc(blob->length);
1624 if (!blob->data)
1625 {
1626 WLog_Print(log, WLOG_ERROR, "license binary blob::length=%" PRIu16 ", blob::data=%p",
1627 blob->length, (const void*)blob->data);
1628 return FALSE;
1629 }
1630 memcpy(blob->data, data, blob->length); /* blobData */
1631 return TRUE;
1632}
1633
1640
1641BOOL license_read_binary_blob(wLog* log, wStream* s, LICENSE_BLOB* blob)
1642{
1643 UINT16 wBlobType = 0;
1644 UINT16 length = 0;
1645
1646 WINPR_ASSERT(blob);
1647
1648 if (!license_check_stream_length(log, s, 4, "license binary blob::type", 0))
1649 return FALSE;
1650
1651 Stream_Read_UINT16(s, wBlobType); /* wBlobType (2 bytes) */
1652 Stream_Read_UINT16(s, length); /* wBlobLen (2 bytes) */
1653
1654 if (!license_check_stream_length(log, s, length, "license binary blob::length", 0))
1655 return FALSE;
1656
1657 if (!license_read_binary_blob_data(log, blob, wBlobType, Stream_Pointer(s), length))
1658 return FALSE;
1659
1660 return Stream_SafeSeek(s, length);
1661}
1662
1669
1670BOOL license_write_binary_blob(wStream* s, const LICENSE_BLOB* blob)
1671{
1672 WINPR_ASSERT(blob);
1673
1674 if (!Stream_EnsureRemainingCapacity(s, blob->length + 4ull))
1675 return FALSE;
1676
1677 Stream_Write_UINT16(s, blob->type); /* wBlobType (2 bytes) */
1678 Stream_Write_UINT16(s, blob->length); /* wBlobLen (2 bytes) */
1679
1680 if (blob->length > 0)
1681 Stream_Write(s, blob->data, blob->length); /* blobData */
1682 return TRUE;
1683}
1684
1685WINPR_ATTR_NODISCARD
1686static BOOL license_write_encrypted_premaster_secret_blob(wLog* log, wStream* s,
1687 const LICENSE_BLOB* blob,
1688 UINT32 ModulusLength)
1689{
1690 const UINT32 length = ModulusLength + 8;
1691
1692 WINPR_ASSERT(blob);
1693 WINPR_ASSERT(length <= UINT16_MAX);
1694
1695 if (blob->length > ModulusLength)
1696 {
1697 WLog_Print(log, WLOG_ERROR, "invalid blob");
1698 return FALSE;
1699 }
1700
1701 if (!Stream_EnsureRemainingCapacity(s, length + 4ull))
1702 return FALSE;
1703 Stream_Write_UINT16(s, blob->type); /* wBlobType (2 bytes) */
1704 Stream_Write_UINT16(s, (UINT16)length); /* wBlobLen (2 bytes) */
1705
1706 if (blob->length > 0)
1707 Stream_Write(s, blob->data, blob->length); /* blobData */
1708
1709 Stream_Zero(s, length - blob->length);
1710 return TRUE;
1711}
1712
1713WINPR_ATTR_NODISCARD
1714static BOOL license_read_encrypted_premaster_secret_blob(wLog* log, wStream* s, LICENSE_BLOB* blob,
1715 UINT32* ModulusLength)
1716{
1717 if (!license_read_binary_blob(log, s, blob))
1718 return FALSE;
1719 WINPR_ASSERT(ModulusLength);
1720 *ModulusLength = blob->length;
1721 return TRUE;
1722}
1723
1729
1730LICENSE_BLOB* license_new_binary_blob(UINT16 type)
1731{
1732 LICENSE_BLOB* blob = (LICENSE_BLOB*)calloc(1, sizeof(LICENSE_BLOB));
1733 if (blob)
1734 blob->type = type;
1735 return blob;
1736}
1737
1743
1744void license_free_binary_blob(LICENSE_BLOB* blob)
1745{
1746 if (blob)
1747 {
1748 winpr_znfree(blob->data, blob->length);
1749 winpr_znfree(blob, sizeof(LICENSE_BLOB));
1750 }
1751}
1752
1759
1760BOOL license_read_scope_list(wLog* log, wStream* s, SCOPE_LIST* scopeList)
1761{
1762 UINT32 scopeCount = 0;
1763
1764 WINPR_ASSERT(scopeList);
1765
1766 if (!license_check_stream_length(log, s, 4, "license scope list", 0))
1767 return FALSE;
1768
1769 Stream_Read_UINT32(s, scopeCount); /* ScopeCount (4 bytes) */
1770
1771 if (!license_check_stream_length(log, s, 4ull * scopeCount, "license scope list::count",
1772 LICENSE_MAX_SCOPE_LENGTH))
1773 return FALSE;
1774
1775 if (!license_scope_list_resize(scopeList, scopeCount))
1776 return FALSE;
1777 /* ScopeArray */
1778 for (UINT32 i = 0; i < scopeCount; i++)
1779 {
1780 if (!license_read_binary_blob(log, s, scopeList->array[i]))
1781 return FALSE;
1782 }
1783
1784 return TRUE;
1785}
1786
1787BOOL license_write_scope_list(wLog* log, wStream* s, const SCOPE_LIST* scopeList)
1788{
1789 WINPR_ASSERT(scopeList);
1790
1791 if (!license_check_stream_capacity(log, s, 4, "license scope list"))
1792 return FALSE;
1793
1794 Stream_Write_UINT32(s, scopeList->count); /* ScopeCount (4 bytes) */
1795
1796 if (!license_check_stream_capacity(log, s, scopeList->count * 4ull,
1797 "license scope list::count"))
1798 return FALSE;
1799
1800 /* ScopeArray */
1801 WINPR_ASSERT(scopeList->array || (scopeList->count == 0));
1802 for (UINT32 i = 0; i < scopeList->count; i++)
1803 {
1804 const LICENSE_BLOB* element = scopeList->array[i];
1805
1806 if (!license_write_binary_blob(s, element))
1807 return FALSE;
1808 }
1809
1810 return TRUE;
1811}
1812
1818
1819SCOPE_LIST* license_new_scope_list(void)
1820{
1821 SCOPE_LIST* list = calloc(1, sizeof(SCOPE_LIST));
1822 return list;
1823}
1824
1825static void license_scope_list_free(SCOPE_LIST* scopeList, UINT32 count)
1826{
1827 WINPR_ASSERT(scopeList);
1828 WINPR_ASSERT(scopeList->array || (scopeList->count == 0));
1829
1830 for (UINT32 x = count; x < scopeList->count; x++)
1831 {
1832 license_free_binary_blob(scopeList->array[x]);
1833 scopeList->array[x] = nullptr;
1834 }
1835
1836 if (count == 0)
1837 {
1838 free((void*)scopeList->array);
1839 scopeList->array = nullptr;
1840 }
1841}
1842
1843BOOL license_scope_list_resize(SCOPE_LIST* scopeList, UINT32 count)
1844{
1845 license_scope_list_free(scopeList, count);
1846 if (count > 0)
1847 {
1848 LICENSE_BLOB** tmp =
1849 (LICENSE_BLOB**)realloc((void*)scopeList->array, count * sizeof(LICENSE_BLOB*));
1850 if (!tmp)
1851 return FALSE;
1852 scopeList->array = tmp;
1853 }
1854
1855 for (UINT32 x = scopeList->count; x < count; x++)
1856 {
1857 LICENSE_BLOB* blob = license_new_binary_blob(BB_SCOPE_BLOB);
1858 if (!blob)
1859 {
1860 scopeList->count = x;
1861 return FALSE;
1862 }
1863 scopeList->array[x] = blob;
1864 }
1865
1866 scopeList->count = count;
1867 return TRUE;
1868}
1869
1875
1876void license_free_scope_list(SCOPE_LIST* scopeList)
1877{
1878 if (!scopeList)
1879 return;
1880
1881 license_scope_list_free(scopeList, 0);
1882 free(scopeList);
1883}
1884
1885BOOL license_send_license_info(rdpLicense* license, const LICENSE_BLOB* calBlob,
1886 const BYTE* signature, size_t signature_length)
1887{
1888 WINPR_ASSERT(calBlob);
1889 WINPR_ASSERT(signature);
1890 WINPR_ASSERT(license->certificate);
1891
1892 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1893 if (!info)
1894 return FALSE;
1895
1896 UINT16 sec_flags = 0;
1897 wStream* s = license_send_stream_init(license, &sec_flags);
1898 if (!s)
1899 return FALSE;
1900
1901 if (!license_check_stream_capacity(license->log, s, 8 + sizeof(license->ClientRandom),
1902 "license info::ClientRandom"))
1903 goto error;
1904
1905 Stream_Write_UINT32(s,
1906 license->PreferredKeyExchangeAlg); /* PreferredKeyExchangeAlg (4 bytes) */
1907 Stream_Write_UINT32(s, license->PlatformId); /* PlatformId (4 bytes) */
1908
1909 /* ClientRandom (32 bytes) */
1910 Stream_Write(s, license->ClientRandom, sizeof(license->ClientRandom));
1911
1912 /* Licensing Binary Blob with EncryptedPreMasterSecret: */
1913 if (!license_write_encrypted_premaster_secret_blob(
1914 license->log, s, license->EncryptedPremasterSecret, info->ModulusLength))
1915 goto error;
1916
1917 /* Licensing Binary Blob with LicenseInfo: */
1918 if (!license_write_binary_blob(s, calBlob))
1919 goto error;
1920
1921 /* Licensing Binary Blob with EncryptedHWID */
1922 if (!license_write_binary_blob(s, license->EncryptedHardwareId))
1923 goto error;
1924
1925 /* MACData */
1926 if (!license_check_stream_capacity(license->log, s, signature_length, "license info::MACData"))
1927 goto error;
1928 Stream_Write(s, signature, signature_length);
1929
1930 return license_send(license, s, LICENSE_INFO, sec_flags);
1931
1932error:
1933 Stream_Release(s);
1934 return FALSE;
1935}
1936
1937WINPR_ATTR_NODISCARD
1938static BOOL license_check_preferred_alg(rdpLicense* license, UINT32 PreferredKeyExchangeAlg,
1939 const char* where)
1940{
1941 WINPR_ASSERT(license);
1942 WINPR_ASSERT(where);
1943
1944 if (license->PreferredKeyExchangeAlg != PreferredKeyExchangeAlg)
1945 {
1946 char buffer1[64] = WINPR_C_ARRAY_INIT;
1947 char buffer2[64] = WINPR_C_ARRAY_INIT;
1948 WLog_Print(license->log, WLOG_WARN, "%s::PreferredKeyExchangeAlg, expected %s, got %s",
1949 where,
1950 license_preferred_key_exchange_alg_string(license->PreferredKeyExchangeAlg,
1951 buffer1, sizeof(buffer1)),
1952 license_preferred_key_exchange_alg_string(PreferredKeyExchangeAlg, buffer2,
1953 sizeof(buffer2)));
1954 return FALSE;
1955 }
1956 return TRUE;
1957}
1958
1959BOOL license_read_license_info(rdpLicense* license, wStream* s)
1960{
1961 BOOL rc = FALSE;
1962 UINT32 PreferredKeyExchangeAlg = 0;
1963
1964 WINPR_ASSERT(license);
1965 WINPR_ASSERT(license->certificate);
1966
1967 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1968 if (!info)
1969 goto error;
1970
1971 /* ClientRandom (32 bytes) */
1972 if (!license_check_stream_length(license->log, s, 8 + sizeof(license->ClientRandom),
1973 "license info", 0))
1974 goto error;
1975
1976 Stream_Read_UINT32(s, PreferredKeyExchangeAlg); /* PreferredKeyExchangeAlg (4 bytes) */
1977 if (!license_check_preferred_alg(license, PreferredKeyExchangeAlg, "license info"))
1978 goto error;
1979 Stream_Read_UINT32(s, license->PlatformId); /* PlatformId (4 bytes) */
1980
1981 /* ClientRandom (32 bytes) */
1982 Stream_Read(s, license->ClientRandom, sizeof(license->ClientRandom));
1983
1984 /* Licensing Binary Blob with EncryptedPreMasterSecret: */
1985 {
1986 UINT32 ModulusLength = 0;
1987 if (!license_read_encrypted_premaster_secret_blob(
1988 license->log, s, license->EncryptedPremasterSecret, &ModulusLength))
1989 goto error;
1990
1991 if (ModulusLength != info->ModulusLength)
1992 {
1993 WLog_Print(license->log, WLOG_WARN,
1994 "EncryptedPremasterSecret,::ModulusLength[%" PRIu32
1995 "] != rdpCertInfo::ModulusLength[%" PRIu32 "]",
1996 ModulusLength, info->ModulusLength);
1997 goto error;
1998 }
1999 }
2000
2001 /* Licensing Binary Blob with LicenseInfo: */
2002 if (!license_read_binary_blob(license->log, s, license->LicenseInfo))
2003 goto error;
2004
2005 /* Licensing Binary Blob with EncryptedHWID */
2006 if (!license_read_binary_blob(license->log, s, license->EncryptedHardwareId))
2007 goto error;
2008
2009 /* MACData */
2010 if (!license_check_stream_length(license->log, s, sizeof(license->MACData),
2011 "license info::MACData", 0))
2012 goto error;
2013 Stream_Read(s, license->MACData, sizeof(license->MACData));
2014
2015 rc = TRUE;
2016
2017error:
2018 return rc;
2019}
2020
2027
2028BOOL license_read_license_request_packet(rdpLicense* license, wStream* s)
2029{
2030 WINPR_ASSERT(license);
2031
2032 /* ServerRandom (32 bytes) */
2033 if (!license_check_stream_length(license->log, s, sizeof(license->ServerRandom),
2034 "license request", 0))
2035 return FALSE;
2036
2037 Stream_Read(s, license->ServerRandom, sizeof(license->ServerRandom));
2038
2039 /* ProductInfo */
2040 if (!license_read_product_info(license->log, s, license->ProductInfo))
2041 return FALSE;
2042
2043 /* KeyExchangeList */
2044 if (!license_read_binary_blob(license->log, s, license->KeyExchangeList))
2045 return FALSE;
2046
2047 /* ServerCertificate */
2048 if (!license_read_binary_blob(license->log, s, license->ServerCertificate))
2049 return FALSE;
2050
2051 /* ScopeList */
2052 if (!license_read_scope_list(license->log, s, license->ScopeList))
2053 return FALSE;
2054
2055 /* Parse Server Certificate */
2056 if (!freerdp_certificate_read_server_cert(license->certificate,
2057 license->ServerCertificate->data,
2058 license->ServerCertificate->length))
2059 return FALSE;
2060
2061 if (!license_generate_keys(license) || !license_generate_hwid(license) ||
2062 !license_encrypt_premaster_secret(license))
2063 return FALSE;
2064
2065#ifdef WITH_DEBUG_LICENSE
2066 WLog_Print(license->log, WLOG_DEBUG, "ServerRandom:");
2067 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ServerRandom,
2068 sizeof(license->ServerRandom));
2069 license_print_product_info(license->log, license->ProductInfo);
2070 license_print_scope_list(license->log, license->ScopeList);
2071#endif
2072 return TRUE;
2073}
2074
2075BOOL license_write_license_request_packet(const rdpLicense* license, wStream* s)
2076{
2077 WINPR_ASSERT(license);
2078
2079 /* ServerRandom (32 bytes) */
2080 if (!license_check_stream_capacity(license->log, s, sizeof(license->ServerRandom),
2081 "license request"))
2082 return FALSE;
2083 Stream_Write(s, license->ServerRandom, sizeof(license->ServerRandom));
2084
2085 /* ProductInfo */
2086 if (!license_write_product_info(license->log, s, license->ProductInfo))
2087 return FALSE;
2088
2089 /* KeyExchangeList */
2090 if (!license_write_binary_blob(s, license->KeyExchangeList))
2091 return FALSE;
2092
2093 /* ServerCertificate */
2094 if (!license_write_binary_blob(s, license->ServerCertificate))
2095 return FALSE;
2096
2097 /* ScopeList */
2098 if (!license_write_scope_list(license->log, s, license->ScopeList))
2099 return FALSE;
2100
2101 return TRUE;
2102}
2103
2104WINPR_ATTR_NODISCARD
2105static BOOL license_send_license_request_packet(rdpLicense* license)
2106{
2107 UINT16 sec_flags = 0;
2108 wStream* s = license_send_stream_init(license, &sec_flags);
2109 if (!s)
2110 return FALSE;
2111
2112 if (!license_write_license_request_packet(license, s))
2113 goto fail;
2114
2115 return license_send(license, s, LICENSE_REQUEST, sec_flags);
2116
2117fail:
2118 Stream_Release(s);
2119 return FALSE;
2120}
2121
2122/*
2123 * Read a PLATFORM_CHALLENGE packet.
2124 * msdn{cc241921}
2125 * @param license license module
2126 * @param s stream
2127 */
2128
2129BOOL license_read_platform_challenge_packet(rdpLicense* license, wStream* s)
2130{
2131 BYTE macData[LICENSING_ENCRYPTION_KEY_LENGTH] = WINPR_C_ARRAY_INIT;
2132
2133 WINPR_ASSERT(license);
2134
2135 DEBUG_LICENSE("Receiving Platform Challenge Packet");
2136
2137 if (!license_check_stream_length(license->log, s, 4, "license platform challenge", 0))
2138 return FALSE;
2139
2140 /* [MS-RDPELE] 2.2.2.4 Server Platform Challenge (SERVER_PLATFORM_CHALLENGE)
2141 * reserved field */
2142 Stream_Seek_UINT32(s); /* ConnectFlags, Reserved (4 bytes) */
2143
2144 /* EncryptedPlatformChallenge */
2145 license->EncryptedPlatformChallenge->type = BB_ANY_BLOB;
2146 if (!license_read_binary_blob(license->log, s, license->EncryptedPlatformChallenge))
2147 return FALSE;
2148 license->EncryptedPlatformChallenge->type = BB_ENCRYPTED_DATA_BLOB;
2149
2150 /* MACData (16 bytes) */
2151 if (!license_check_stream_length(license->log, s, sizeof(macData),
2152 "license platform challenge::MAC", 0))
2153 return FALSE;
2154
2155 Stream_Read(s, macData, sizeof(macData));
2156 if (!license_decrypt_and_check_MAC(license, license->EncryptedPlatformChallenge->data,
2157 license->EncryptedPlatformChallenge->length,
2158 license->PlatformChallenge, macData))
2159 return FALSE;
2160
2161 WLog_Print(license->log, WLOG_TRACE, "platform challenge read");
2162
2163#ifdef WITH_DEBUG_LICENSE
2164 WLog_Print(license->log, WLOG_DEBUG, "EncryptedPlatformChallenge:");
2165 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedPlatformChallenge->data,
2166 license->EncryptedPlatformChallenge->length);
2167 WLog_Print(license->log, WLOG_DEBUG, "PlatformChallenge:");
2168 winpr_HexLogDump(license->log, WLOG_DEBUG, license->PlatformChallenge->data,
2169 license->PlatformChallenge->length);
2170 WLog_Print(license->log, WLOG_DEBUG, "MacData:");
2171 winpr_HexLogDump(license->log, WLOG_DEBUG, macData, sizeof(macData));
2172#endif
2173 return TRUE;
2174}
2175
2176BOOL license_send_error_alert(rdpLicense* license, UINT32 dwErrorCode, UINT32 dwStateTransition,
2177 const LICENSE_BLOB* info)
2178{
2179 UINT16 sec_flags = 0;
2180 wStream* s = license_send_stream_init(license, &sec_flags);
2181
2182 if (!s)
2183 goto fail;
2184
2185 if (!license_check_stream_capacity(license->log, s, 8, "license error alert"))
2186 goto fail;
2187 Stream_Write_UINT32(s, dwErrorCode);
2188 Stream_Write_UINT32(s, dwStateTransition);
2189
2190 if (info)
2191 {
2192 if (!license_write_binary_blob(s, info))
2193 goto fail;
2194 }
2195
2196 return license_send(license, s, ERROR_ALERT, sec_flags);
2197fail:
2198 Stream_Release(s);
2199 return FALSE;
2200}
2201
2202BOOL license_send_platform_challenge_packet(rdpLicense* license)
2203{
2204 UINT16 sec_flags = 0;
2205 wStream* s = license_send_stream_init(license, &sec_flags);
2206
2207 if (!s)
2208 goto fail;
2209
2210 DEBUG_LICENSE("Receiving Platform Challenge Packet");
2211
2212 if (!license_check_stream_capacity(license->log, s, 4, "license platform challenge"))
2213 goto fail;
2214
2215 Stream_Zero(s, 4); /* ConnectFlags, Reserved (4 bytes) */
2216
2217 /* EncryptedPlatformChallenge */
2218 if (!license_write_binary_blob(s, license->EncryptedPlatformChallenge))
2219 goto fail;
2220
2221 /* MACData (16 bytes) */
2222 if (!license_check_stream_length(license->log, s, sizeof(license->MACData),
2223 "license platform challenge::MAC", 0))
2224 goto fail;
2225
2226 Stream_Write(s, license->MACData, sizeof(license->MACData));
2227
2228 return license_send(license, s, PLATFORM_CHALLENGE, sec_flags);
2229fail:
2230 Stream_Release(s);
2231 return FALSE;
2232}
2233
2234WINPR_ATTR_NODISCARD
2235static BOOL license_read_encrypted_blob(const rdpLicense* license, wStream* s, LICENSE_BLOB* target)
2236{
2237 UINT16 wBlobType = 0;
2238 UINT16 wBlobLen = 0;
2239
2240 WINPR_ASSERT(license);
2241 WINPR_ASSERT(target);
2242
2243 if (!license_check_stream_length(license->log, s, 4, "license encrypted blob", 0))
2244 return FALSE;
2245
2246 Stream_Read_UINT16(s, wBlobType);
2247 if (wBlobType != BB_ENCRYPTED_DATA_BLOB)
2248 {
2249 WLog_Print(
2250 license->log, WLOG_WARN,
2251 "expecting BB_ENCRYPTED_DATA_BLOB blob, probably a windows 2003 server, continuing...");
2252 }
2253
2254 Stream_Read_UINT16(s, wBlobLen);
2255
2256 BYTE* encryptedData = Stream_Pointer(s);
2257 if (!Stream_SafeSeek(s, wBlobLen))
2258 {
2259 WLog_Print(license->log, WLOG_WARN,
2260 "short license encrypted blob::length, expected %" PRIu16 " bytes, got %" PRIuz,
2261 wBlobLen, Stream_GetRemainingLength(s));
2262 return FALSE;
2263 }
2264
2265 return license_rc4_with_licenseKey(license, encryptedData, wBlobLen, target);
2266}
2267
2274
2275BOOL license_read_new_or_upgrade_license_packet(rdpLicense* license, wStream* s)
2276{
2277 UINT32 os_major = 0;
2278 UINT32 os_minor = 0;
2279 UINT32 cbScope = 0;
2280 UINT32 cbCompanyName = 0;
2281 UINT32 cbProductId = 0;
2282 UINT32 cbLicenseInfo = 0;
2283 wStream sbuffer = WINPR_C_ARRAY_INIT;
2284 wStream* licenseStream = nullptr;
2285 BOOL ret = FALSE;
2286 BYTE computedMac[16] = WINPR_C_ARRAY_INIT;
2287 const BYTE* readMac = nullptr;
2288
2289 WINPR_ASSERT(license);
2290
2291 DEBUG_LICENSE("Receiving Server New/Upgrade License Packet");
2292
2293 LICENSE_BLOB* calBlob = license_new_binary_blob(BB_DATA_BLOB);
2294 if (!calBlob)
2295 return FALSE;
2296
2297 /* EncryptedLicenseInfo */
2298 if (!license_read_encrypted_blob(license, s, calBlob))
2299 goto fail;
2300
2301 /* compute MAC and check it */
2302 readMac = Stream_Pointer(s);
2303 if (!Stream_SafeSeek(s, sizeof(computedMac)))
2304 {
2305 WLog_Print(license->log, WLOG_WARN,
2306 "short license new/upgrade, expected 16 bytes, got %" PRIuz,
2307 Stream_GetRemainingLength(s));
2308 goto fail;
2309 }
2310
2311 if (!security_mac_data(license->MacSaltKey, sizeof(license->MacSaltKey), calBlob->data,
2312 calBlob->length, computedMac, sizeof(computedMac)))
2313 goto fail;
2314
2315 if (memcmp(computedMac, readMac, sizeof(computedMac)) != 0)
2316 {
2317 WLog_Print(license->log, WLOG_ERROR, "new or upgrade license MAC mismatch");
2318 goto fail;
2319 }
2320
2321 licenseStream = Stream_StaticConstInit(&sbuffer, calBlob->data, calBlob->length);
2322 if (!licenseStream)
2323 {
2324 WLog_Print(license->log, WLOG_ERROR,
2325 "license::blob::data=%p, license::blob::length=%" PRIu16,
2326 (const void*)calBlob->data, calBlob->length);
2327 goto fail;
2328 }
2329
2330 if (!license_check_stream_length(license->log, licenseStream, 8,
2331 "license new/upgrade::blob::version", 0))
2332 goto fail;
2333
2334 Stream_Read_UINT16(licenseStream, os_minor);
2335 Stream_Read_UINT16(licenseStream, os_major);
2336
2337 WLog_Print(license->log, WLOG_DEBUG, "Version: %" PRIu16 ".%" PRIu16, os_major, os_minor);
2338
2339 /* Scope */
2340 Stream_Read_UINT32(licenseStream, cbScope);
2341 if (!license_check_stream_length(license->log, licenseStream, cbScope,
2342 "license new/upgrade::blob::scope", LICENSE_MAX_SCOPE_LENGTH))
2343 goto fail;
2344
2345#ifdef WITH_DEBUG_LICENSE
2346 WLog_Print(license->log, WLOG_DEBUG, "Scope:");
2347 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbScope);
2348#endif
2349 Stream_Seek(licenseStream, cbScope);
2350
2351 /* CompanyName */
2352 if (!license_check_stream_length(license->log, licenseStream, 4,
2353 "license new/upgrade::blob::cbCompanyName", 0))
2354 goto fail;
2355
2356 Stream_Read_UINT32(licenseStream, cbCompanyName);
2357 if (!license_check_stream_length(license->log, licenseStream, cbCompanyName,
2358 "license new/upgrade::blob::CompanyName",
2359 LICENSE_MAX_COMPANY_NAME_LENGTH))
2360 goto fail;
2361
2362#ifdef WITH_DEBUG_LICENSE
2363 WLog_Print(license->log, WLOG_DEBUG, "Company name:");
2364 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbCompanyName);
2365#endif
2366 Stream_Seek(licenseStream, cbCompanyName);
2367
2368 /* productId */
2369 if (!license_check_stream_length(license->log, licenseStream, 4,
2370 "license new/upgrade::blob::cbProductId", 0))
2371 goto fail;
2372
2373 Stream_Read_UINT32(licenseStream, cbProductId);
2374
2375 if (!license_check_stream_length(license->log, licenseStream, cbProductId,
2376 "license new/upgrade::blob::ProductId",
2377 LICENSE_MAX_PRODUCT_ID_LENGTH))
2378 goto fail;
2379
2380#ifdef WITH_DEBUG_LICENSE
2381 WLog_Print(license->log, WLOG_DEBUG, "Product id:");
2382 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbProductId);
2383#endif
2384 Stream_Seek(licenseStream, cbProductId);
2385
2386 /* licenseInfo */
2387 if (!license_check_stream_length(license->log, licenseStream, 4,
2388 "license new/upgrade::blob::cbLicenseInfo", 0))
2389 goto fail;
2390
2391 Stream_Read_UINT32(licenseStream, cbLicenseInfo);
2392 if (!license_check_stream_length(license->log, licenseStream, cbLicenseInfo,
2393 "license new/upgrade::blob::LicenseInfo",
2394 LICENSE_MAX_CAL_LENGTH))
2395 goto fail;
2396
2397 license->type = LICENSE_TYPE_ISSUED;
2398 license_set_state(license, LICENSE_STATE_COMPLETED);
2399 ret = TRUE;
2400
2401 if (!license->rdp->settings->OldLicenseBehaviour)
2402 ret = saveCal(license->log, license->rdp->settings, Stream_Pointer(licenseStream),
2403 cbLicenseInfo, license->rdp->settings->ClientHostname);
2404
2405fail:
2406 license_free_binary_blob(calBlob);
2407 return ret;
2408}
2409
2416
2417BOOL license_read_error_alert_packet(rdpLicense* license, wStream* s)
2418{
2419 UINT32 dwErrorCode = 0;
2420 UINT32 dwStateTransition = 0;
2421
2422 WINPR_ASSERT(license);
2423 WINPR_ASSERT(license->rdp);
2424
2425 if (!license_check_stream_length(license->log, s, 8ul, "error alert", 0))
2426 return FALSE;
2427
2428 Stream_Read_UINT32(s, dwErrorCode); /* dwErrorCode (4 bytes) */
2429 Stream_Read_UINT32(s, dwStateTransition); /* dwStateTransition (4 bytes) */
2430
2431 if (!license_read_binary_blob(license->log, s, license->ErrorInfo)) /* bbErrorInfo */
2432 return FALSE;
2433
2434#ifdef WITH_DEBUG_LICENSE
2435 WLog_Print(license->log, WLOG_DEBUG, "dwErrorCode: %s, dwStateTransition: %s",
2436 error_codes(dwErrorCode), state_transitions(dwStateTransition));
2437#endif
2438
2439 if (dwErrorCode == STATUS_VALID_CLIENT)
2440 {
2441 license->type = LICENSE_TYPE_NONE;
2442 license_set_state(license, LICENSE_STATE_COMPLETED);
2443 return TRUE;
2444 }
2445
2446 switch (dwStateTransition)
2447 {
2448 case ST_TOTAL_ABORT:
2449 license_set_state(license, LICENSE_STATE_ABORTED);
2450 break;
2451 case ST_NO_TRANSITION:
2452 license_set_state(license, LICENSE_STATE_COMPLETED);
2453 break;
2454 case ST_RESET_PHASE_TO_START:
2455 license_set_state(license, LICENSE_STATE_CONFIGURED);
2456 break;
2457 case ST_RESEND_LAST_MESSAGE:
2458 break;
2459 default:
2460 break;
2461 }
2462
2463 return TRUE;
2464}
2465
2472
2473BOOL license_write_new_license_request_packet(const rdpLicense* license, wStream* s)
2474{
2475 WINPR_ASSERT(license);
2476
2477 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
2478 if (!info)
2479 return FALSE;
2480
2481 if (!license_check_stream_capacity(license->log, s, 8 + sizeof(license->ClientRandom),
2482 "License Request"))
2483 return FALSE;
2484
2485 Stream_Write_UINT32(s,
2486 license->PreferredKeyExchangeAlg); /* PreferredKeyExchangeAlg (4 bytes) */
2487 Stream_Write_UINT32(s, license->PlatformId); /* PlatformId (4 bytes) */
2488 Stream_Write(s, license->ClientRandom,
2489 sizeof(license->ClientRandom)); /* ClientRandom (32 bytes) */
2490
2491 if (/* EncryptedPremasterSecret */
2492 !license_write_encrypted_premaster_secret_blob(
2493 license->log, s, license->EncryptedPremasterSecret, info->ModulusLength) ||
2494 /* ClientUserName */
2495 !license_write_binary_blob(s, license->ClientUserName) ||
2496 /* ClientMachineName */
2497 !license_write_binary_blob(s, license->ClientMachineName))
2498 {
2499 return FALSE;
2500 }
2501
2502 WLog_Print(license->log, WLOG_TRACE, "new license written");
2503
2504#ifdef WITH_DEBUG_LICENSE
2505 WLog_Print(license->log, WLOG_DEBUG, "PreferredKeyExchangeAlg: 0x%08" PRIX32 "",
2506 license->PreferredKeyExchangeAlg);
2507 WLog_Print(license->log, WLOG_DEBUG, "ClientRandom:");
2508 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ClientRandom,
2509 sizeof(license->ClientRandom));
2510 WLog_Print(license->log, WLOG_DEBUG, "EncryptedPremasterSecret");
2511 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedPremasterSecret->data,
2512 license->EncryptedPremasterSecret->length);
2513 WLog_Print(license->log, WLOG_DEBUG, "ClientUserName (%" PRIu16 "): %s",
2514 license->ClientUserName->length, (char*)license->ClientUserName->data);
2515 WLog_Print(license->log, WLOG_DEBUG, "ClientMachineName (%" PRIu16 "): %s",
2516 license->ClientMachineName->length, (char*)license->ClientMachineName->data);
2517#endif
2518 return TRUE;
2519}
2520
2521BOOL license_read_new_license_request_packet(rdpLicense* license, wStream* s)
2522{
2523 UINT32 PreferredKeyExchangeAlg = 0;
2524
2525 WINPR_ASSERT(license);
2526
2527 if (!license_check_stream_length(license->log, s, 8ull + sizeof(license->ClientRandom),
2528 "new license request", 0))
2529 return FALSE;
2530
2531 Stream_Read_UINT32(s, PreferredKeyExchangeAlg); /* PreferredKeyExchangeAlg (4 bytes) */
2532 if (!license_check_preferred_alg(license, PreferredKeyExchangeAlg, "new license request"))
2533 return FALSE;
2534
2535 Stream_Read_UINT32(s, license->PlatformId); /* PlatformId (4 bytes) */
2536 Stream_Read(s, license->ClientRandom,
2537 sizeof(license->ClientRandom)); /* ClientRandom (32 bytes) */
2538
2539 /* EncryptedPremasterSecret */
2540 UINT32 ModulusLength = 0;
2541 if (!license_read_encrypted_premaster_secret_blob(
2542 license->log, s, license->EncryptedPremasterSecret, &ModulusLength))
2543 return FALSE;
2544
2545 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
2546 if (!info)
2547 WLog_Print(license->log, WLOG_WARN,
2548 "Missing license certificate, skipping ModulusLength checks");
2549 else if (ModulusLength != info->ModulusLength)
2550 {
2551 WLog_Print(license->log, WLOG_WARN,
2552 "EncryptedPremasterSecret expected to be %" PRIu32 " bytes, but read %" PRIu32
2553 " bytes",
2554 info->ModulusLength, ModulusLength);
2555 return FALSE;
2556 }
2557
2558 /* ClientUserName */
2559 if (!license_read_binary_blob(license->log, s, license->ClientUserName))
2560 return FALSE;
2561 /* ClientMachineName */
2562 if (!license_read_binary_blob(license->log, s, license->ClientMachineName))
2563 return FALSE;
2564
2565 return TRUE;
2566}
2567
2573
2574BOOL license_answer_license_request(rdpLicense* license)
2575{
2576 UINT16 sec_flags = 0;
2577 wStream* s = nullptr;
2578 BYTE* license_data = nullptr;
2579 size_t license_size = 0;
2580 BOOL status = 0;
2581 char* username = nullptr;
2582
2583 WINPR_ASSERT(license);
2584 WINPR_ASSERT(license->rdp);
2585 WINPR_ASSERT(license->rdp->settings);
2586
2587 if (!license->rdp->settings->OldLicenseBehaviour)
2588 license_data = loadCalFile(license->log, license->rdp->settings,
2589 license->rdp->settings->ClientHostname, &license_size);
2590
2591 if (license_data)
2592 {
2593 LICENSE_BLOB* calBlob = nullptr;
2594 BYTE signature[LICENSING_ENCRYPTION_KEY_LENGTH] = WINPR_C_ARRAY_INIT;
2595
2596 DEBUG_LICENSE("Sending Saved License Packet");
2597
2598 WINPR_ASSERT(license->EncryptedHardwareId);
2599 license->EncryptedHardwareId->type = BB_ENCRYPTED_DATA_BLOB;
2600 if (!license_encrypt_and_MAC(license, license->HardwareId, sizeof(license->HardwareId),
2601 license->EncryptedHardwareId, signature, sizeof(signature)))
2602 {
2603 winpr_znfree(license_data, license_size);
2604 return FALSE;
2605 }
2606
2607 calBlob = license_new_binary_blob(BB_DATA_BLOB);
2608 if (!calBlob)
2609 {
2610 winpr_znfree(license_data, license_size);
2611 return FALSE;
2612 }
2613 calBlob->data = license_data;
2614 WINPR_ASSERT(license_size <= UINT16_MAX);
2615 calBlob->length = (UINT16)license_size;
2616
2617 status = license_send_license_info(license, calBlob, signature, sizeof(signature));
2618 license_free_binary_blob(calBlob);
2619
2620 return status;
2621 }
2622
2623 DEBUG_LICENSE("Sending New License Packet");
2624
2625 s = license_send_stream_init(license, &sec_flags);
2626 if (!s)
2627 return FALSE;
2628 if (license->rdp->settings->Username != nullptr)
2629 username = license->rdp->settings->Username;
2630 else
2631 username = "username";
2632
2633 {
2634 WINPR_ASSERT(license->ClientUserName);
2635 const size_t len = strlen(username) + 1;
2636 WINPR_ASSERT(len <= UINT16_MAX);
2637
2638 license->ClientUserName->data = (BYTE*)username;
2639 license->ClientUserName->length = (UINT16)len;
2640 }
2641
2642 {
2643 WINPR_ASSERT(license->ClientMachineName);
2644 const size_t len = strlen(license->rdp->settings->ClientHostname) + 1;
2645 WINPR_ASSERT(len <= UINT16_MAX);
2646
2647 license->ClientMachineName->data = (BYTE*)license->rdp->settings->ClientHostname;
2648 license->ClientMachineName->length = (UINT16)len;
2649 }
2650 status = license_write_new_license_request_packet(license, s);
2651
2652 WINPR_ASSERT(license->ClientUserName);
2653 license->ClientUserName->data = nullptr;
2654 license->ClientUserName->length = 0;
2655
2656 WINPR_ASSERT(license->ClientMachineName);
2657 license->ClientMachineName->data = nullptr;
2658 license->ClientMachineName->length = 0;
2659
2660 if (!status)
2661 {
2662 Stream_Release(s);
2663 return FALSE;
2664 }
2665
2666 return license_send(license, s, NEW_LICENSE_REQUEST, sec_flags);
2667}
2668
2674
2675BOOL license_send_platform_challenge_response(rdpLicense* license)
2676{
2677 wStream* challengeRespData = nullptr;
2678 BYTE* buffer = nullptr;
2679 BOOL status = 0;
2680
2681 WINPR_ASSERT(license);
2682 WINPR_ASSERT(license->PlatformChallenge);
2683 WINPR_ASSERT(license->MacSaltKey);
2684 WINPR_ASSERT(license->EncryptedPlatformChallenge);
2685 WINPR_ASSERT(license->EncryptedHardwareId);
2686
2687 DEBUG_LICENSE("Sending Platform Challenge Response Packet");
2688
2689 license->EncryptedPlatformChallenge->type = BB_DATA_BLOB;
2690
2691 /* prepare the PLATFORM_CHALLENGE_RESPONSE_DATA */
2692 challengeRespData = Stream_New(nullptr, 8 + license->PlatformChallenge->length);
2693 if (!challengeRespData)
2694 return FALSE;
2695 Stream_Write_UINT16(challengeRespData, PLATFORM_CHALLENGE_RESPONSE_VERSION); /* wVersion */
2696 Stream_Write_UINT16(challengeRespData, license->ClientType); /* wClientType */
2697 Stream_Write_UINT16(challengeRespData, license->LicenseDetailLevel); /* wLicenseDetailLevel */
2698 Stream_Write_UINT16(challengeRespData, license->PlatformChallenge->length); /* cbChallenge */
2699 Stream_Write(challengeRespData, license->PlatformChallenge->data,
2700 license->PlatformChallenge->length); /* pbChallenge */
2701 Stream_SealLength(challengeRespData);
2702
2703 /* compute MAC of PLATFORM_CHALLENGE_RESPONSE_DATA + HWID */
2704 const size_t length = Stream_Length(challengeRespData) + sizeof(license->HardwareId);
2705 buffer = (BYTE*)malloc(length);
2706 if (!buffer)
2707 {
2708 Stream_Free(challengeRespData, TRUE);
2709 return FALSE;
2710 }
2711
2712 CopyMemory(buffer, Stream_Buffer(challengeRespData), Stream_Length(challengeRespData));
2713 CopyMemory(&buffer[Stream_Length(challengeRespData)], license->HardwareId,
2714 sizeof(license->HardwareId));
2715 status = security_mac_data(license->MacSaltKey, sizeof(license->MacSaltKey), buffer, length,
2716 license->MACData, sizeof(license->MACData));
2717 winpr_znfree(buffer, length);
2718
2719 if (!status)
2720 {
2721 Stream_Free(challengeRespData, TRUE);
2722 return FALSE;
2723 }
2724
2725 license->EncryptedHardwareId->type = BB_ENCRYPTED_DATA_BLOB;
2726 if (!license_rc4_with_licenseKey(license, license->HardwareId, sizeof(license->HardwareId),
2727 license->EncryptedHardwareId))
2728 {
2729 Stream_Free(challengeRespData, TRUE);
2730 return FALSE;
2731 }
2732
2733 status = license_rc4_with_licenseKey(license, Stream_Buffer(challengeRespData),
2734 Stream_Length(challengeRespData),
2735 license->EncryptedPlatformChallengeResponse);
2736 Stream_Free(challengeRespData, TRUE);
2737 if (!status)
2738 return FALSE;
2739
2740#ifdef WITH_DEBUG_LICENSE
2741 WLog_Print(license->log, WLOG_DEBUG, "LicensingEncryptionKey:");
2742 winpr_HexLogDump(license->log, WLOG_DEBUG, license->LicensingEncryptionKey, 16);
2743 WLog_Print(license->log, WLOG_DEBUG, "HardwareId:");
2744 winpr_HexLogDump(license->log, WLOG_DEBUG, license->HardwareId, sizeof(license->HardwareId));
2745 WLog_Print(license->log, WLOG_DEBUG, "EncryptedHardwareId:");
2746 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedHardwareId->data,
2747 license->EncryptedHardwareId->length);
2748#endif
2749 UINT16 sec_flags = 0;
2750 wStream* s = license_send_stream_init(license, &sec_flags);
2751 if (!s)
2752 return FALSE;
2753
2754 if (license_write_client_platform_challenge_response(license, s))
2755 return license_send(license, s, PLATFORM_CHALLENGE_RESPONSE, sec_flags);
2756
2757 Stream_Release(s);
2758 return FALSE;
2759}
2760
2761BOOL license_read_platform_challenge_response(WINPR_ATTR_UNUSED rdpLicense* license)
2762{
2763 WINPR_ASSERT(license);
2764 WINPR_ASSERT(license->PlatformChallenge);
2765 WINPR_ASSERT(license->MacSaltKey);
2766 WINPR_ASSERT(license->EncryptedPlatformChallenge);
2767 WINPR_ASSERT(license->EncryptedHardwareId);
2768
2769 DEBUG_LICENSE("Receiving Platform Challenge Response Packet");
2770
2771#if defined(WITH_LICENSE_DECRYPT_CHALLENGE_RESPONSE)
2772 BOOL rc = FALSE;
2773 LICENSE_BLOB* dblob = license_new_binary_blob(BB_ANY_BLOB);
2774 if (!dblob)
2775 return FALSE;
2776
2777 wStream sbuffer = WINPR_C_ARRAY_INIT;
2778 UINT16 wVersion = 0;
2779 UINT16 cbChallenge = 0;
2780 const BYTE* pbChallenge = nullptr;
2781 LICENSE_BLOB* blob = license->EncryptedPlatformChallengeResponse;
2782
2783 if (!license_rc4_with_licenseKey(license, blob->data, blob->length, dblob))
2784 goto fail;
2785
2786 wStream* s = Stream_StaticConstInit(&sbuffer, dblob->data, dblob->length);
2787 if (!license_check_stream_length(s, 8, "PLATFORM_CHALLENGE_RESPONSE_DATA", 0))
2788 goto fail;
2789
2790 Stream_Read_UINT16(s, wVersion);
2791 if (wVersion != PLATFORM_CHALLENGE_RESPONSE_VERSION)
2792 {
2793 WLog_Print(license->log, WLOG_WARN,
2794 "Invalid PLATFORM_CHALLENGE_RESPONSE_DATA::wVersion 0x%04" PRIx16
2795 ", expected 0x04" PRIx16,
2796 wVersion, PLATFORM_CHALLENGE_RESPONSE_VERSION);
2797 goto fail;
2798 }
2799 Stream_Read_UINT16(s, license->ClientType);
2800 Stream_Read_UINT16(s, license->LicenseDetailLevel);
2801 Stream_Read_UINT16(s, cbChallenge);
2802
2803 if (!license_check_stream_length(s, cbChallenge,
2804 "PLATFORM_CHALLENGE_RESPONSE_DATA::pbChallenge", 0))
2805 goto fail;
2806
2807 pbChallenge = Stream_Pointer(s);
2808 if (!license_read_binary_blob_data(license->PlatformChallengeResponse, BB_DATA_BLOB,
2809 pbChallenge, cbChallenge))
2810 goto fail;
2811 if (!Stream_SafeSeek(s, cbChallenge))
2812 goto fail;
2813
2814 rc = TRUE;
2815fail:
2816 license_free_binary_blob(dblob);
2817 return rc;
2818#else
2819 return TRUE;
2820#endif
2821}
2822
2823BOOL license_write_client_platform_challenge_response(rdpLicense* license, wStream* s)
2824{
2825 WINPR_ASSERT(license);
2826
2827 if (!license_write_binary_blob(s, license->EncryptedPlatformChallengeResponse))
2828 return FALSE;
2829 if (!license_write_binary_blob(s, license->EncryptedHardwareId))
2830 return FALSE;
2831 if (!license_check_stream_capacity(license->log, s, sizeof(license->MACData),
2832 "CLIENT_PLATFORM_CHALLENGE_RESPONSE::MACData"))
2833 return FALSE;
2834 Stream_Write(s, license->MACData, sizeof(license->MACData));
2835 return TRUE;
2836}
2837
2838BOOL license_read_client_platform_challenge_response(rdpLicense* license, wStream* s)
2839{
2840 WINPR_ASSERT(license);
2841
2842 if (!license_read_binary_blob(license->log, s, license->EncryptedPlatformChallengeResponse))
2843 return FALSE;
2844 if (!license_read_binary_blob(license->log, s, license->EncryptedHardwareId))
2845 return FALSE;
2846 if (!license_check_stream_length(license->log, s, sizeof(license->MACData),
2847 "CLIENT_PLATFORM_CHALLENGE_RESPONSE::MACData", 0))
2848 return FALSE;
2849 Stream_Read(s, license->MACData, sizeof(license->MACData));
2850 return license_read_platform_challenge_response(license);
2851}
2852
2861
2862BOOL license_send_valid_client_error_packet(rdpRdp* rdp)
2863{
2864 WINPR_ASSERT(rdp);
2865 rdpLicense* license = rdp->license;
2866 WINPR_ASSERT(license);
2867
2868 license->state = LICENSE_STATE_COMPLETED;
2869 license->type = LICENSE_TYPE_NONE;
2870 return license_send_error_alert(license, STATUS_VALID_CLIENT, ST_NO_TRANSITION,
2871 license->ErrorInfo);
2872}
2873
2879
2880rdpLicense* license_new(rdpRdp* rdp)
2881{
2882 WINPR_ASSERT(rdp);
2883
2884 rdpLicense* license = (rdpLicense*)calloc(1, sizeof(rdpLicense));
2885 if (!license)
2886 return nullptr;
2887 license->log = WLog_Get(LICENSE_TAG);
2888 WINPR_ASSERT(license->log);
2889
2890 license->PlatformId = PLATFORMID;
2891 license->ClientType = OTHER_PLATFORM_CHALLENGE_TYPE;
2892 license->LicenseDetailLevel = LICENSE_DETAIL_DETAIL;
2893 license->PreferredKeyExchangeAlg = KEY_EXCHANGE_ALG_RSA;
2894 license->rdp = rdp;
2895
2896 license_set_state(license, LICENSE_STATE_INITIAL);
2897 if (!(license->certificate = freerdp_certificate_new()))
2898 goto out_error;
2899 if (!(license->ProductInfo = license_new_product_info()))
2900 goto out_error;
2901 if (!(license->ErrorInfo = license_new_binary_blob(BB_ERROR_BLOB)))
2902 goto out_error;
2903 if (!(license->LicenseInfo = license_new_binary_blob(BB_DATA_BLOB)))
2904 goto out_error;
2905 if (!(license->KeyExchangeList = license_new_binary_blob(BB_KEY_EXCHG_ALG_BLOB)))
2906 goto out_error;
2907 if (!(license->ServerCertificate = license_new_binary_blob(BB_CERTIFICATE_BLOB)))
2908 goto out_error;
2909 if (!(license->ClientUserName = license_new_binary_blob(BB_CLIENT_USER_NAME_BLOB)))
2910 goto out_error;
2911 if (!(license->ClientMachineName = license_new_binary_blob(BB_CLIENT_MACHINE_NAME_BLOB)))
2912 goto out_error;
2913 if (!(license->PlatformChallenge = license_new_binary_blob(BB_ANY_BLOB)))
2914 goto out_error;
2915 if (!(license->PlatformChallengeResponse = license_new_binary_blob(BB_ANY_BLOB)))
2916 goto out_error;
2917 if (!(license->EncryptedPlatformChallenge = license_new_binary_blob(BB_ANY_BLOB)))
2918 goto out_error;
2919 if (!(license->EncryptedPlatformChallengeResponse =
2920 license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2921 goto out_error;
2922 if (!(license->EncryptedPremasterSecret = license_new_binary_blob(BB_ANY_BLOB)))
2923 goto out_error;
2924 if (!(license->EncryptedHardwareId = license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2925 goto out_error;
2926 if (!(license->EncryptedLicenseInfo = license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2927 goto out_error;
2928 if (!(license->ScopeList = license_new_scope_list()))
2929 goto out_error;
2930
2931 if (!license_generate_randoms(license))
2932 goto out_error;
2933
2934 return license;
2935
2936out_error:
2937 WINPR_PRAGMA_DIAG_PUSH
2938 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2939 license_free(license);
2940 WINPR_PRAGMA_DIAG_POP
2941 return nullptr;
2942}
2943
2948
2949void license_free(rdpLicense* license)
2950{
2951 if (license)
2952 {
2953 freerdp_certificate_free(license->certificate);
2954 license_free_product_info(license->ProductInfo);
2955 license_free_binary_blob(license->ErrorInfo);
2956 license_free_binary_blob(license->LicenseInfo);
2957 license_free_binary_blob(license->KeyExchangeList);
2958 license_free_binary_blob(license->ServerCertificate);
2959 license_free_binary_blob(license->ClientUserName);
2960 license_free_binary_blob(license->ClientMachineName);
2961 license_free_binary_blob(license->PlatformChallenge);
2962 license_free_binary_blob(license->PlatformChallengeResponse);
2963 license_free_binary_blob(license->EncryptedPlatformChallenge);
2964 license_free_binary_blob(license->EncryptedPlatformChallengeResponse);
2965 license_free_binary_blob(license->EncryptedPremasterSecret);
2966 license_free_binary_blob(license->EncryptedHardwareId);
2967 license_free_binary_blob(license->EncryptedLicenseInfo);
2968 license_free_scope_list(license->ScopeList);
2969 winpr_znfree(license, sizeof(rdpLicense));
2970 }
2971}
2972
2973LICENSE_STATE license_get_state(const rdpLicense* license)
2974{
2975 WINPR_ASSERT(license);
2976 return license->state;
2977}
2978
2979LICENSE_TYPE license_get_type(const rdpLicense* license)
2980{
2981 WINPR_ASSERT(license);
2982 return license->type;
2983}
2984
2985void license_set_state(rdpLicense* license, LICENSE_STATE state)
2986{
2987 WINPR_ASSERT(license);
2988 license->state = state;
2989 switch (state)
2990 {
2991 case LICENSE_STATE_COMPLETED:
2992 break;
2993 case LICENSE_STATE_ABORTED:
2994 default:
2995 license->type = LICENSE_TYPE_INVALID;
2996 break;
2997 }
2998}
2999
3000const char* license_get_state_string(LICENSE_STATE state)
3001{
3002 switch (state)
3003 {
3004 case LICENSE_STATE_INITIAL:
3005 return "LICENSE_STATE_INITIAL";
3006 case LICENSE_STATE_CONFIGURED:
3007 return "LICENSE_STATE_CONFIGURED";
3008 case LICENSE_STATE_REQUEST:
3009 return "LICENSE_STATE_REQUEST";
3010 case LICENSE_STATE_NEW_REQUEST:
3011 return "LICENSE_STATE_NEW_REQUEST";
3012 case LICENSE_STATE_PLATFORM_CHALLENGE:
3013 return "LICENSE_STATE_PLATFORM_CHALLENGE";
3014 case LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE:
3015 return "LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE";
3016 case LICENSE_STATE_COMPLETED:
3017 return "LICENSE_STATE_COMPLETED";
3018 case LICENSE_STATE_ABORTED:
3019 return "LICENSE_STATE_ABORTED";
3020 default:
3021 return "LICENSE_STATE_UNKNOWN";
3022 }
3023}
3024
3025BOOL license_server_send_request(rdpLicense* license)
3026{
3027 if (!license_ensure_state(license, LICENSE_STATE_CONFIGURED, LICENSE_REQUEST))
3028 return FALSE;
3029 if (!license_send_license_request_packet(license))
3030 return FALSE;
3031 license_set_state(license, LICENSE_STATE_REQUEST);
3032 return TRUE;
3033}
3034
3035WINPR_ATTR_NODISCARD
3036static BOOL license_set_string(wLog* log, const char* what, const char* value, BYTE** bdst,
3037 UINT32* dstLen)
3038{
3039 WINPR_ASSERT(what);
3040 WINPR_ASSERT(value);
3041 WINPR_ASSERT(bdst);
3042 WINPR_ASSERT(dstLen);
3043
3044 union
3045 {
3046 WCHAR** w;
3047 BYTE** b;
3048 } cnv;
3049 cnv.b = bdst;
3050
3051 size_t len = 0;
3052 *cnv.w = ConvertUtf8ToWCharAlloc(value, &len);
3053 if (!*cnv.w || (len > UINT32_MAX / sizeof(WCHAR)))
3054 {
3055 WLog_Print(log, WLOG_ERROR, "license->ProductInfo: %s == %p || %" PRIuz " > UINT32_MAX",
3056 what, (void*)(*cnv.w), len);
3057 return FALSE;
3058 }
3059 *dstLen = (UINT32)(len * sizeof(WCHAR));
3060 return TRUE;
3061}
3062
3063BOOL license_server_configure(rdpLicense* license)
3064{
3065 wStream* s = nullptr;
3066 UINT32 algs[] = { KEY_EXCHANGE_ALG_RSA };
3067
3068 WINPR_ASSERT(license);
3069 WINPR_ASSERT(license->rdp);
3070
3071 const rdpSettings* settings = license->rdp->settings;
3072
3073 const char* CompanyName =
3074 freerdp_settings_get_string(settings, FreeRDP_ServerLicenseCompanyName);
3075
3076 const char* ProductName =
3077 freerdp_settings_get_string(settings, FreeRDP_ServerLicenseProductName);
3078 const UINT32 ProductVersion =
3079 freerdp_settings_get_uint32(settings, FreeRDP_ServerLicenseProductVersion);
3080 const UINT32 issuerCount =
3081 freerdp_settings_get_uint32(settings, FreeRDP_ServerLicenseProductIssuersCount);
3082
3083 const void* ptr = freerdp_settings_get_pointer(settings, FreeRDP_ServerLicenseProductIssuers);
3084 const char** issuers = WINPR_REINTERPRET_CAST(ptr, const void*, const char**);
3085
3086 WINPR_ASSERT(CompanyName);
3087 WINPR_ASSERT(ProductName);
3088 WINPR_ASSERT(ProductVersion > 0);
3089 WINPR_ASSERT(issuers || (issuerCount == 0));
3090
3091 if (!license_ensure_state(license, LICENSE_STATE_INITIAL, LICENSE_REQUEST))
3092 return FALSE;
3093
3094 license->ProductInfo->dwVersion = ProductVersion;
3095 if (!license_set_string(license->log, "pbCompanyName", CompanyName,
3096 &license->ProductInfo->pbCompanyName,
3097 &license->ProductInfo->cbCompanyName))
3098 return FALSE;
3099
3100 if (!license_set_string(license->log, "pbProductId", ProductName,
3101 &license->ProductInfo->pbProductId, &license->ProductInfo->cbProductId))
3102 return FALSE;
3103
3104 if (!license_read_binary_blob_data(license->log, license->KeyExchangeList,
3105 BB_KEY_EXCHG_ALG_BLOB, algs, sizeof(algs)))
3106 return FALSE;
3107
3108 if (!freerdp_certificate_read_server_cert(license->certificate, settings->ServerCertificate,
3109 settings->ServerCertificateLength))
3110 return FALSE;
3111
3112 s = Stream_New(nullptr, 1024);
3113 if (!s)
3114 return FALSE;
3115 else
3116 {
3117 BOOL r = FALSE;
3118 SSIZE_T res =
3119 freerdp_certificate_write_server_cert(license->certificate, CERT_CHAIN_VERSION_2, s);
3120 if (res >= 0)
3121 r = license_read_binary_blob_data(license->log, license->ServerCertificate,
3122 BB_CERTIFICATE_BLOB, Stream_Buffer(s),
3123 Stream_GetPosition(s));
3124
3125 Stream_Free(s, TRUE);
3126 if (!r)
3127 return FALSE;
3128 }
3129
3130 if (!license_scope_list_resize(license->ScopeList, issuerCount))
3131 return FALSE;
3132 for (size_t x = 0; x < issuerCount; x++)
3133 {
3134 LICENSE_BLOB* blob = license->ScopeList->array[x];
3135 const char* name = issuers[x];
3136 const size_t length = strnlen(name, UINT16_MAX) + 1;
3137 if ((length == 0) || (length > UINT16_MAX))
3138 {
3139 WLog_Print(license->log, WLOG_WARN,
3140 "Invalid issuer at position %" PRIuz ": length 0 < %" PRIuz " <= %d"
3141 " ['%s']",
3142 x, length, UINT16_MAX, name);
3143 return FALSE;
3144 }
3145 if (!license_read_binary_blob_data(license->log, blob, BB_SCOPE_BLOB, name, length))
3146 return FALSE;
3147 }
3148
3149 license_set_state(license, LICENSE_STATE_CONFIGURED);
3150 return TRUE;
3151}
3152
3153rdpLicense* license_get(rdpContext* context)
3154{
3155 WINPR_ASSERT(context);
3156 WINPR_ASSERT(context->rdp);
3157 return context->rdp->license;
3158}
WINPR_ATTR_NODISCARD FREERDP_API const void * freerdp_settings_get_pointer(const rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id)
Returns a immutable pointer settings value.
WINPR_ATTR_NODISCARD FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.