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