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 = NULL;
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 void license_generate_randoms(rdpLicense* license);
302static BOOL license_generate_keys(rdpLicense* license);
303static BOOL license_generate_hwid(rdpLicense* license);
304static BOOL license_encrypt_premaster_secret(rdpLicense* license);
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 = NULL;
373 char* ProductId = NULL;
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), NULL);
381 ProductId = ConvertWCharNToUtf8Alloc((const WCHAR*)productInfo->pbProductId,
382 productInfo->cbProductId / sizeof(WCHAR), NULL);
383 WLog_Print(log, WLOG_INFO, "ProductInfo:");
384 WLog_Print(log, WLOG_INFO, "\tdwVersion: 0x%08" PRIX32 "", productInfo->dwVersion);
385 WLog_Print(log, WLOG_INFO, "\tCompanyName: %s", CompanyName);
386 WLog_Print(log, WLOG_INFO, "\tProductId: %s", ProductId);
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 = NULL;
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 if (!Stream_CheckAndLogRequiredCapacityWLogEx(log, WLOG_WARN, s, expect, 1,
469 "%s(%s:%" PRIuz ") %s", __func__, __FILE__,
470 (size_t)__LINE__, where))
471 return FALSE;
472
473 return TRUE;
474}
475
476static BOOL computeCalHash(wLog* log, const char* hostname, char* hashStr, size_t len)
477{
478 WINPR_DIGEST_CTX* sha1 = NULL;
479 BOOL ret = FALSE;
480 BYTE hash[20] = { 0 };
481
482 WINPR_ASSERT(hostname);
483 WINPR_ASSERT(hashStr);
484
485 if (len < 2 * sizeof(hash) + 1)
486 return FALSE;
487
488 if (!(sha1 = winpr_Digest_New()))
489 goto out;
490 if (!winpr_Digest_Init(sha1, WINPR_MD_SHA1))
491 goto out;
492 if (!winpr_Digest_Update(sha1, (const BYTE*)hostname, strlen(hostname)))
493 goto out;
494 if (!winpr_Digest_Final(sha1, hash, sizeof(hash)))
495 goto out;
496
497 for (size_t i = 0; i < sizeof(hash); i++, hashStr += 2)
498 (void)sprintf_s(hashStr, 3, "%.2x", hash[i]);
499
500 ret = TRUE;
501out:
502 if (!ret)
503 WLog_Print(log, WLOG_ERROR, "failed to generate SHA1 of hostname '%s'", hostname);
504 winpr_Digest_Free(sha1);
505 return ret;
506}
507
508static BOOL saveCal(wLog* log, const rdpSettings* settings, const BYTE* data, size_t length,
509 const char* hostname)
510{
511 char hash[41] = { 0 };
512 FILE* fp = NULL;
513 char* licenseStorePath = NULL;
514 char filename[MAX_PATH] = { 0 };
515 char filenameNew[MAX_PATH] = { 0 };
516 char* filepath = NULL;
517 char* filepathNew = NULL;
518
519 size_t written = 0;
520 BOOL ret = FALSE;
521 const char* path = freerdp_settings_get_string(settings, FreeRDP_ConfigPath);
522
523 WINPR_ASSERT(path);
524 WINPR_ASSERT(data || (length == 0));
525 WINPR_ASSERT(hostname);
526
527 if (!winpr_PathFileExists(path))
528 {
529 if (!winpr_PathMakePath(path, 0))
530 {
531 WLog_Print(log, WLOG_ERROR, "error creating directory '%s'", path);
532 goto out;
533 }
534 WLog_Print(log, WLOG_INFO, "creating directory %s", path);
535 }
536
537 if (!(licenseStorePath = GetCombinedPath(path, licenseStore)))
538 {
539 WLog_Print(log, WLOG_ERROR, "Failed to get license store path from '%s' + '%s'", path,
540 licenseStore);
541 goto out;
542 }
543
544 if (!winpr_PathFileExists(licenseStorePath))
545 {
546 if (!winpr_PathMakePath(licenseStorePath, 0))
547 {
548 WLog_Print(log, WLOG_ERROR, "error creating directory '%s'", licenseStorePath);
549 goto out;
550 }
551 WLog_Print(log, WLOG_INFO, "creating directory %s", licenseStorePath);
552 }
553
554 if (!computeCalHash(log, hostname, hash, sizeof(hash)))
555 goto out;
556 (void)sprintf_s(filename, sizeof(filename) - 1, "%s.cal", hash);
557 (void)sprintf_s(filenameNew, sizeof(filenameNew) - 1, "%s.cal.new", hash);
558
559 if (!(filepath = GetCombinedPath(licenseStorePath, filename)))
560 {
561 WLog_Print(log, WLOG_ERROR, "Failed to get license file path from '%s' + '%s'", path,
562 filename);
563 goto out;
564 }
565
566 if (!(filepathNew = GetCombinedPath(licenseStorePath, filenameNew)))
567 {
568 WLog_Print(log, WLOG_ERROR, "Failed to get license new file path from '%s' + '%s'", path,
569 filenameNew);
570 goto out;
571 }
572
573 fp = winpr_fopen(filepathNew, "wb");
574 if (!fp)
575 {
576 WLog_Print(log, WLOG_ERROR, "Failed to open license file '%s'", filepathNew);
577 goto out;
578 }
579
580 written = fwrite(data, length, 1, fp);
581 (void)fclose(fp);
582
583 if (written != 1)
584 {
585 WLog_Print(log, WLOG_ERROR, "Failed to write to license file '%s'", filepathNew);
586 winpr_DeleteFile(filepathNew);
587 goto out;
588 }
589
590 ret = winpr_MoveFileEx(filepathNew, filepath, MOVEFILE_REPLACE_EXISTING);
591 if (!ret)
592 WLog_Print(log, WLOG_ERROR, "Failed to move license file '%s' to '%s'", filepathNew,
593 filepath);
594
595out:
596 free(filepathNew);
597 free(filepath);
598 free(licenseStorePath);
599 return ret;
600}
601
602static BYTE* loadCalFile(wLog* log, const rdpSettings* settings, const char* hostname,
603 size_t* dataLen)
604{
605 char* licenseStorePath = NULL;
606 char* calPath = NULL;
607 char calFilename[MAX_PATH] = { 0 };
608 char hash[41] = { 0 };
609 INT64 length = 0;
610 size_t status = 0;
611 FILE* fp = NULL;
612 BYTE* ret = NULL;
613
614 WINPR_ASSERT(settings);
615 WINPR_ASSERT(hostname);
616 WINPR_ASSERT(dataLen);
617
618 if (!computeCalHash(log, hostname, hash, sizeof(hash)))
619 {
620 WLog_Print(log, WLOG_ERROR, "loadCalFile: unable to compute hostname hash");
621 return NULL;
622 }
623
624 (void)sprintf_s(calFilename, sizeof(calFilename) - 1, "%s.cal", hash);
625
626 if (!(licenseStorePath = GetCombinedPath(
627 freerdp_settings_get_string(settings, FreeRDP_ConfigPath), licenseStore)))
628 return NULL;
629
630 if (!(calPath = GetCombinedPath(licenseStorePath, calFilename)))
631 goto error_path;
632
633 fp = winpr_fopen(calPath, "rb");
634 if (!fp)
635 goto error_open;
636
637 if (_fseeki64(fp, 0, SEEK_END) != 0)
638 goto error_malloc;
639 length = _ftelli64(fp);
640 if (_fseeki64(fp, 0, SEEK_SET) != 0)
641 goto error_malloc;
642 if (length < 0)
643 goto error_malloc;
644
645 ret = (BYTE*)malloc((size_t)length);
646 if (!ret)
647 goto error_malloc;
648
649 status = fread(ret, (size_t)length, 1, fp);
650 if (status == 0)
651 goto error_read;
652
653 *dataLen = (size_t)length;
654
655 (void)fclose(fp);
656 free(calPath);
657 free(licenseStorePath);
658 return ret;
659
660error_read:
661 free(ret);
662error_malloc:
663 fclose(fp);
664error_open:
665 free(calPath);
666error_path:
667 free(licenseStorePath);
668 return NULL;
669}
670
681static BOOL license_read_preamble(wLog* log, wStream* s, BYTE* bMsgType, BYTE* flags,
682 UINT16* wMsgSize)
683{
684 WINPR_ASSERT(bMsgType);
685 WINPR_ASSERT(flags);
686 WINPR_ASSERT(wMsgSize);
687
688 /* preamble (4 bytes) */
689 if (!license_check_stream_length(log, s, 4, "license preamble"))
690 return FALSE;
691
692 Stream_Read_UINT8(s, *bMsgType); /* bMsgType (1 byte) */
693 Stream_Read_UINT8(s, *flags); /* flags (1 byte) */
694 Stream_Read_UINT16(s, *wMsgSize); /* wMsgSize (2 bytes) */
695 return license_check_stream_length(log, s, *wMsgSize - 4ll, "license preamble::wMsgSize");
696}
697
708static BOOL license_write_preamble(wStream* s, BYTE bMsgType, BYTE flags, UINT16 wMsgSize)
709{
710 if (!Stream_EnsureRemainingCapacity(s, 4))
711 return FALSE;
712
713 /* preamble (4 bytes) */
714 Stream_Write_UINT8(s, bMsgType); /* bMsgType (1 byte) */
715 Stream_Write_UINT8(s, flags); /* flags (1 byte) */
716 Stream_Write_UINT16(s, wMsgSize); /* wMsgSize (2 bytes) */
717 return TRUE;
718}
719
728wStream* license_send_stream_init(rdpLicense* license, UINT16* sec_flags)
729{
730 WINPR_ASSERT(license);
731 WINPR_ASSERT(license->rdp);
732 WINPR_ASSERT(sec_flags);
733
734 const BOOL do_crypt = license->rdp->do_crypt;
735
736 *sec_flags = SEC_LICENSE_PKT;
737
738 /*
739 * Encryption of licensing packets is optional even if the rdp security
740 * layer is used. If the peer has not indicated that it is capable of
741 * processing encrypted licensing packets (rdp->do_crypt_license) we turn
742 * off encryption (via rdp->do_crypt) before initializing the rdp stream
743 * and re-enable it afterwards.
744 */
745
746 if (do_crypt)
747 {
748 *sec_flags |= SEC_LICENSE_ENCRYPT_CS;
749 license->rdp->do_crypt = license->rdp->do_crypt_license;
750 }
751
752 wStream* s = rdp_send_stream_init(license->rdp, sec_flags);
753 if (!s)
754 return NULL;
755
756 license->rdp->do_crypt = do_crypt;
757 license->PacketHeaderLength = (UINT16)Stream_GetPosition(s);
758 if (!Stream_SafeSeek(s, LICENSE_PREAMBLE_LENGTH))
759 goto fail;
760 return s;
761
762fail:
763 Stream_Release(s);
764 return NULL;
765}
766
774static BOOL license_send(rdpLicense* license, wStream* s, BYTE type, UINT16 sec_flags)
775{
776 WINPR_ASSERT(license);
777 WINPR_ASSERT(license->rdp);
778
779 rdpRdp* rdp = license->rdp;
780 WINPR_ASSERT(rdp->settings);
781
782 DEBUG_LICENSE("Sending %s Packet", license_request_type_string(type));
783 const size_t length = Stream_GetPosition(s);
784 WINPR_ASSERT(length >= license->PacketHeaderLength);
785 WINPR_ASSERT(length <= UINT16_MAX + license->PacketHeaderLength);
786
787 const UINT16 wMsgSize = (UINT16)(length - license->PacketHeaderLength);
788 Stream_SetPosition(s, license->PacketHeaderLength);
789 BYTE flags = PREAMBLE_VERSION_3_0;
790
796 if (!rdp->settings->ServerMode)
797 flags |= EXTENDED_ERROR_MSG_SUPPORTED;
798
799 if (!license_write_preamble(s, type, flags, wMsgSize))
800 {
801 Stream_Release(s);
802 return FALSE;
803 }
804
805#ifdef WITH_DEBUG_LICENSE
806 WLog_Print(license->log, WLOG_DEBUG, "Sending %s Packet, length %" PRIu16 "",
807 license_request_type_string(type), wMsgSize);
808 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_PointerAs(s, char) - LICENSE_PREAMBLE_LENGTH,
809 wMsgSize);
810#endif
811 Stream_SetPosition(s, length);
812 const BOOL ret = rdp_send(rdp, s, MCS_GLOBAL_CHANNEL_ID, sec_flags);
813 return ret;
814}
815
816BOOL license_write_server_upgrade_license(const rdpLicense* license, wStream* s)
817{
818 WINPR_ASSERT(license);
819
820 if (!license_write_binary_blob(s, license->EncryptedLicenseInfo))
821 return FALSE;
822 if (!license_check_stream_capacity(license->log, s, sizeof(license->MACData),
823 "SERVER_UPGRADE_LICENSE::MACData"))
824 return FALSE;
825 Stream_Write(s, license->MACData, sizeof(license->MACData));
826 return TRUE;
827}
828
829static BOOL license_server_send_new_or_upgrade_license(rdpLicense* license, BOOL upgrade)
830{
831 UINT16 sec_flags = 0;
832 wStream* s = license_send_stream_init(license, &sec_flags);
833 const BYTE type = upgrade ? UPGRADE_LICENSE : NEW_LICENSE;
834
835 if (!s)
836 return FALSE;
837
838 if (!license_write_server_upgrade_license(license, s))
839 goto fail;
840
841 return license_send(license, s, type, sec_flags);
842
843fail:
844 Stream_Release(s);
845 return FALSE;
846}
847
856static state_run_t license_client_recv_int(rdpLicense* license, wStream* s)
857{
858 BYTE flags = 0;
859 BYTE bMsgType = 0;
860 UINT16 wMsgSize = 0;
861 const size_t length = Stream_GetRemainingLength(s);
862
863 WINPR_ASSERT(license);
864
865 if (!license_read_preamble(license->log, s, &bMsgType, &flags,
866 &wMsgSize)) /* preamble (4 bytes) */
867 return STATE_RUN_FAILED;
868
869 DEBUG_LICENSE("Receiving %s Packet", license_request_type_string(bMsgType));
870
871 switch (bMsgType)
872 {
873 case LICENSE_REQUEST:
874 /* Client does not require configuration, so skip this state */
875 if (license_get_state(license) == LICENSE_STATE_INITIAL)
876 license_set_state(license, LICENSE_STATE_CONFIGURED);
877
878 if (!license_ensure_state(license, LICENSE_STATE_CONFIGURED, bMsgType))
879 return STATE_RUN_FAILED;
880
881 if (!license_read_license_request_packet(license, s))
882 return STATE_RUN_FAILED;
883
884 if (!license_answer_license_request(license))
885 return STATE_RUN_FAILED;
886
887 license_set_state(license, LICENSE_STATE_NEW_REQUEST);
888 break;
889
890 case PLATFORM_CHALLENGE:
891 if (!license_ensure_state(license, LICENSE_STATE_NEW_REQUEST, bMsgType))
892 return STATE_RUN_FAILED;
893
894 if (!license_read_platform_challenge_packet(license, s))
895 return STATE_RUN_FAILED;
896
897 if (!license_send_platform_challenge_response(license))
898 return STATE_RUN_FAILED;
899 license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE);
900 break;
901
902 case NEW_LICENSE:
903 case UPGRADE_LICENSE:
904 if (!license_ensure_state(license, LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE, bMsgType))
905 return STATE_RUN_FAILED;
906 if (!license_read_new_or_upgrade_license_packet(license, s))
907 return STATE_RUN_FAILED;
908 break;
909
910 case ERROR_ALERT:
911 if (!license_read_error_alert_packet(license, s))
912 return STATE_RUN_FAILED;
913 break;
914
915 default:
916 WLog_Print(license->log, WLOG_ERROR, "invalid bMsgType:%" PRIu8 "", bMsgType);
917 return STATE_RUN_FAILED;
918 }
919
920 if (!tpkt_ensure_stream_consumed(license->log, s, length))
921 return STATE_RUN_FAILED;
922 return STATE_RUN_SUCCESS;
923}
924
925state_run_t license_client_recv(rdpLicense* license, wStream* s)
926{
927 state_run_t rc = license_client_recv_int(license, s);
928 if (state_run_failed(rc))
929 {
930 freerdp_set_last_error(license->rdp->context, ERROR_CTX_LICENSE_CLIENT_INVALID);
931 }
932 return rc;
933}
934
935state_run_t license_server_recv(rdpLicense* license, wStream* s)
936{
937 state_run_t rc = STATE_RUN_FAILED;
938 BYTE flags = 0;
939 BYTE bMsgType = 0;
940 UINT16 wMsgSize = 0;
941 const size_t length = Stream_GetRemainingLength(s);
942
943 WINPR_ASSERT(license);
944
945 if (!license_read_preamble(license->log, s, &bMsgType, &flags,
946 &wMsgSize)) /* preamble (4 bytes) */
947 goto fail;
948
949 DEBUG_LICENSE("Receiving %s Packet", license_request_type_string(bMsgType));
950
951 switch (bMsgType)
952 {
953 case NEW_LICENSE_REQUEST:
954 if (!license_ensure_state(license, LICENSE_STATE_REQUEST, bMsgType))
955 goto fail;
956 if (!license_read_new_license_request_packet(license, s))
957 goto fail;
958 // TODO: Validate if client is allowed
959 if (!license_send_error_alert(license, ERR_INVALID_MAC, ST_TOTAL_ABORT,
960 license->ErrorInfo))
961 goto fail;
962 if (!license_send_platform_challenge_packet(license))
963 goto fail;
964 license->update = FALSE;
965 if (!license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE))
966 goto fail;
967 break;
968 case LICENSE_INFO:
969 if (!license_ensure_state(license, LICENSE_STATE_REQUEST, bMsgType))
970 goto fail;
971 if (!license_read_license_info(license, s))
972 goto fail;
973 // TODO: Validate license info
974 if (!license_send_platform_challenge_packet(license))
975 goto fail;
976 if (!license_set_state(license, LICENSE_STATE_PLATFORM_CHALLENGE))
977 goto fail;
978 license->update = TRUE;
979 break;
980
981 case PLATFORM_CHALLENGE_RESPONSE:
982 if (!license_ensure_state(license, LICENSE_STATE_PLATFORM_CHALLENGE, bMsgType))
983 goto fail;
984 if (!license_read_client_platform_challenge_response(license, s))
985 goto fail;
986
987 // TODO: validate challenge response
988 if (FALSE)
989 {
990 if (license_send_error_alert(license, ERR_INVALID_CLIENT, ST_TOTAL_ABORT,
991 license->ErrorInfo))
992 goto fail;
993 }
994 else
995 {
996 if (!license_server_send_new_or_upgrade_license(license, license->update))
997 goto fail;
998
999 license->type = LICENSE_TYPE_ISSUED;
1000 license_set_state(license, LICENSE_STATE_COMPLETED);
1001
1002 rc = STATE_RUN_CONTINUE; /* License issued, switch state */
1003 }
1004 break;
1005
1006 case ERROR_ALERT:
1007 if (!license_read_error_alert_packet(license, s))
1008 goto fail;
1009 break;
1010
1011 default:
1012 WLog_Print(license->log, WLOG_ERROR, "invalid bMsgType:%" PRIu8 "", bMsgType);
1013 goto fail;
1014 }
1015
1016 if (!tpkt_ensure_stream_consumed(license->log, s, length))
1017 goto fail;
1018
1019 if (!state_run_success(rc))
1020 rc = STATE_RUN_SUCCESS;
1021
1022fail:
1023 if (state_run_failed(rc))
1024 {
1025 if (flags & EXTENDED_ERROR_MSG_SUPPORTED)
1026 license_send_error_alert(license, ERR_INVALID_CLIENT, ST_TOTAL_ABORT, NULL);
1027 license_set_state(license, LICENSE_STATE_ABORTED);
1028 }
1029
1030 return rc;
1031}
1032
1033void license_generate_randoms(rdpLicense* license)
1034{
1035 WINPR_ASSERT(license);
1036
1037#ifdef LICENSE_NULL_CLIENT_RANDOM
1038 ZeroMemory(license->ClientRandom, sizeof(license->ClientRandom)); /* ClientRandom */
1039#else
1040 winpr_RAND(license->ClientRandom, sizeof(license->ClientRandom)); /* ClientRandom */
1041#endif
1042
1043 winpr_RAND(license->ServerRandom, sizeof(license->ServerRandom)); /* ServerRandom */
1044
1045#ifdef LICENSE_NULL_PREMASTER_SECRET
1046 ZeroMemory(license->PremasterSecret, sizeof(license->PremasterSecret)); /* PremasterSecret */
1047#else
1048 winpr_RAND(license->PremasterSecret, sizeof(license->PremasterSecret)); /* PremasterSecret */
1049#endif
1050}
1051
1057static BOOL license_generate_keys(rdpLicense* license)
1058{
1059 WINPR_ASSERT(license);
1060
1061 if (
1062 /* MasterSecret */
1063 !security_master_secret(license->PremasterSecret, sizeof(license->PremasterSecret),
1064 license->ClientRandom, sizeof(license->ClientRandom),
1065 license->ServerRandom, sizeof(license->ServerRandom),
1066 license->MasterSecret, sizeof(license->MasterSecret)) ||
1067 /* SessionKeyBlob */
1068 !security_session_key_blob(license->MasterSecret, sizeof(license->MasterSecret),
1069 license->ClientRandom, sizeof(license->ClientRandom),
1070 license->ServerRandom, sizeof(license->ServerRandom),
1071 license->SessionKeyBlob, sizeof(license->SessionKeyBlob)))
1072 {
1073 return FALSE;
1074 }
1075 security_mac_salt_key(license->SessionKeyBlob, sizeof(license->SessionKeyBlob),
1076 license->ClientRandom, sizeof(license->ClientRandom),
1077 license->ServerRandom, sizeof(license->ServerRandom), license->MacSaltKey,
1078 sizeof(license->MacSaltKey)); /* MacSaltKey */
1079 const BOOL ret = security_licensing_encryption_key(
1080 license->SessionKeyBlob, sizeof(license->SessionKeyBlob), license->ClientRandom,
1081 sizeof(license->ClientRandom), license->ServerRandom, sizeof(license->ServerRandom),
1082 license->LicensingEncryptionKey,
1083 sizeof(license->LicensingEncryptionKey)); /* LicensingEncryptionKey */
1084
1085#ifdef WITH_DEBUG_LICENSE
1086 WLog_Print(license->log, WLOG_DEBUG, "ClientRandom:");
1087 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ClientRandom,
1088 sizeof(license->ClientRandom));
1089 WLog_Print(license->log, WLOG_DEBUG, "ServerRandom:");
1090 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ServerRandom,
1091 sizeof(license->ServerRandom));
1092 WLog_Print(license->log, WLOG_DEBUG, "PremasterSecret:");
1093 winpr_HexLogDump(license->log, WLOG_DEBUG, license->PremasterSecret,
1094 sizeof(license->PremasterSecret));
1095 WLog_Print(license->log, WLOG_DEBUG, "MasterSecret:");
1096 winpr_HexLogDump(license->log, WLOG_DEBUG, license->MasterSecret,
1097 sizeof(license->MasterSecret));
1098 WLog_Print(license->log, WLOG_DEBUG, "SessionKeyBlob:");
1099 winpr_HexLogDump(license->log, WLOG_DEBUG, license->SessionKeyBlob,
1100 sizeof(license->SessionKeyBlob));
1101 WLog_Print(license->log, WLOG_DEBUG, "MacSaltKey:");
1102 winpr_HexLogDump(license->log, WLOG_DEBUG, license->MacSaltKey, sizeof(license->MacSaltKey));
1103 WLog_Print(license->log, WLOG_DEBUG, "LicensingEncryptionKey:");
1104 winpr_HexLogDump(license->log, WLOG_DEBUG, license->LicensingEncryptionKey,
1105 sizeof(license->LicensingEncryptionKey));
1106#endif
1107 return ret;
1108}
1109
1115BOOL license_generate_hwid(rdpLicense* license)
1116{
1117 const BYTE* hashTarget = NULL;
1118 size_t targetLen = 0;
1119 BYTE macAddress[6] = { 0 };
1120
1121 WINPR_ASSERT(license);
1122 WINPR_ASSERT(license->rdp);
1123 WINPR_ASSERT(license->rdp->settings);
1124
1125 ZeroMemory(license->HardwareId, sizeof(license->HardwareId));
1126
1127 if (license->rdp->settings->OldLicenseBehaviour)
1128 {
1129 hashTarget = macAddress;
1130 targetLen = sizeof(macAddress);
1131 }
1132 else
1133 {
1134 wStream buffer = { 0 };
1135 const char* hostname = license->rdp->settings->ClientHostname;
1136 wStream* s = Stream_StaticInit(&buffer, license->HardwareId, 4);
1137 Stream_Write_UINT32(s, license->PlatformId);
1138
1139 hashTarget = (const BYTE*)hostname;
1140 targetLen = hostname ? strlen(hostname) : 0;
1141 }
1142
1143 /* Allow FIPS override for use of MD5 here, really this does not have to be MD5 as we are just
1144 * taking a MD5 hash of the 6 bytes of 0's(macAddress) */
1145 /* and filling in the Data1-Data4 fields of the CLIENT_HARDWARE_ID structure(from MS-RDPELE
1146 * section 2.2.2.3.1). This is for RDP licensing packets */
1147 /* which will already be encrypted under FIPS, so the use of MD5 here is not for sensitive data
1148 * protection. */
1149 return winpr_Digest_Allow_FIPS(WINPR_MD_MD5, hashTarget, targetLen,
1150 &license->HardwareId[HWID_PLATFORM_ID_LENGTH],
1151 WINPR_MD5_DIGEST_LENGTH);
1152}
1153
1154static BOOL license_get_server_rsa_public_key(rdpLicense* license)
1155{
1156 rdpSettings* settings = NULL;
1157
1158 WINPR_ASSERT(license);
1159 WINPR_ASSERT(license->certificate);
1160 WINPR_ASSERT(license->rdp);
1161
1162 settings = license->rdp->settings;
1163 WINPR_ASSERT(settings);
1164
1165 if (license->ServerCertificate->length < 1)
1166 {
1167 if (!freerdp_certificate_read_server_cert(license->certificate, settings->ServerCertificate,
1168 settings->ServerCertificateLength))
1169 return FALSE;
1170 }
1171
1172 return TRUE;
1173}
1174
1175BOOL license_encrypt_premaster_secret(rdpLicense* license)
1176{
1177 WINPR_ASSERT(license);
1178 WINPR_ASSERT(license->certificate);
1179
1180 if (!license_get_server_rsa_public_key(license))
1181 return FALSE;
1182
1183 WINPR_ASSERT(license->EncryptedPremasterSecret);
1184
1185 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1186 if (!info)
1187 {
1188 WLog_Print(license->log, WLOG_ERROR, "info=%p, license->certificate=%p", (const void*)info,
1189 (void*)license->certificate);
1190 return FALSE;
1191 }
1192
1193#ifdef WITH_DEBUG_LICENSE
1194 WLog_Print(license->log, WLOG_DEBUG, "Modulus (%" PRIu32 " bits):", info->ModulusLength * 8);
1195 winpr_HexLogDump(license->log, WLOG_DEBUG, info->Modulus, info->ModulusLength);
1196 WLog_Print(license->log, WLOG_DEBUG, "Exponent:");
1197 winpr_HexLogDump(license->log, WLOG_DEBUG, info->exponent, sizeof(info->exponent));
1198#endif
1199
1200 BYTE* EncryptedPremasterSecret = (BYTE*)calloc(1, info->ModulusLength);
1201 if (!EncryptedPremasterSecret)
1202 {
1203 WLog_Print(license->log, WLOG_ERROR,
1204 "EncryptedPremasterSecret=%p, info->ModulusLength=%" PRIu32,
1205 (const void*)EncryptedPremasterSecret, info->ModulusLength);
1206 return FALSE;
1207 }
1208
1209 license->EncryptedPremasterSecret->type = BB_RANDOM_BLOB;
1210 license->EncryptedPremasterSecret->length = sizeof(license->PremasterSecret);
1211#ifndef LICENSE_NULL_PREMASTER_SECRET
1212 {
1213 const SSIZE_T length =
1214 crypto_rsa_public_encrypt(license->PremasterSecret, sizeof(license->PremasterSecret),
1215 info, EncryptedPremasterSecret, info->ModulusLength);
1216 if ((length < 0) || (length > UINT16_MAX))
1217 {
1218 WLog_Print(license->log, WLOG_ERROR, "RSA public encrypt length=%" PRIdz " < 0 || > %d",
1219 length, UINT16_MAX);
1220 return FALSE;
1221 }
1222 license->EncryptedPremasterSecret->length = (UINT16)length;
1223 }
1224#endif
1225 license->EncryptedPremasterSecret->data = EncryptedPremasterSecret;
1226 return TRUE;
1227}
1228
1229static BOOL license_rc4_with_licenseKey(const rdpLicense* license, const BYTE* input, size_t len,
1230 LICENSE_BLOB* target)
1231{
1232 WINPR_ASSERT(license);
1233 WINPR_ASSERT(input || (len == 0));
1234 WINPR_ASSERT(target);
1235 WINPR_ASSERT(len <= UINT16_MAX);
1236
1237 WINPR_RC4_CTX* rc4 = winpr_RC4_New_Allow_FIPS(license->LicensingEncryptionKey,
1238 sizeof(license->LicensingEncryptionKey));
1239 if (!rc4)
1240 {
1241 WLog_Print(license->log, WLOG_ERROR, "Failed to allocate RC4");
1242 return FALSE;
1243 }
1244
1245 BYTE* buffer = NULL;
1246 if (len > 0)
1247 buffer = realloc(target->data, len);
1248 if (!buffer)
1249 goto error_buffer;
1250
1251 target->data = buffer;
1252 target->length = (UINT16)len;
1253
1254 if (!winpr_RC4_Update(rc4, len, input, buffer))
1255 goto error_buffer;
1256
1257 winpr_RC4_Free(rc4);
1258 return TRUE;
1259
1260error_buffer:
1261 WLog_Print(license->log, WLOG_ERROR, "Failed to create/update RC4: len=%" PRIuz ", buffer=%p",
1262 len, (const void*)buffer);
1263 winpr_RC4_Free(rc4);
1264 return FALSE;
1265}
1266
1277static BOOL license_encrypt_and_MAC(rdpLicense* license, const BYTE* input, size_t len,
1278 LICENSE_BLOB* target, BYTE* mac, size_t mac_length)
1279{
1280 WINPR_ASSERT(license);
1281 return license_rc4_with_licenseKey(license, input, len, target) &&
1282 security_mac_data(license->MacSaltKey, sizeof(license->MacSaltKey), input, len, mac,
1283 mac_length);
1284}
1285
1297static BOOL license_decrypt_and_check_MAC(rdpLicense* license, const BYTE* input, size_t len,
1298 LICENSE_BLOB* target, const BYTE* packetMac)
1299{
1300 BYTE macData[sizeof(license->MACData)] = { 0 };
1301
1302 WINPR_ASSERT(license);
1303 WINPR_ASSERT(target);
1304
1305 if (freerdp_settings_get_bool(license->rdp->settings, FreeRDP_TransportDumpReplay))
1306 {
1307 WLog_Print(license->log, WLOG_DEBUG, "TransportDumpReplay active, skipping...");
1308 return TRUE;
1309 }
1310
1311 if (!license_rc4_with_licenseKey(license, input, len, target))
1312 return FALSE;
1313
1314 if (!security_mac_data(license->MacSaltKey, sizeof(license->MacSaltKey), target->data, len,
1315 macData, sizeof(macData)))
1316 return FALSE;
1317
1318 if (memcmp(packetMac, macData, sizeof(macData)) != 0)
1319 {
1320 WLog_Print(license->log, WLOG_ERROR, "packetMac != expectedMac");
1321 return FALSE;
1322 }
1323 return TRUE;
1324}
1325
1333BOOL license_read_product_info(wLog* log, wStream* s, LICENSE_PRODUCT_INFO* productInfo)
1334{
1335 WINPR_ASSERT(productInfo);
1336
1337 if (!license_check_stream_length(log, s, 8, "license product info::cbCompanyName"))
1338 return FALSE;
1339
1340 Stream_Read_UINT32(s, productInfo->dwVersion); /* dwVersion (4 bytes) */
1341 Stream_Read_UINT32(s, productInfo->cbCompanyName); /* cbCompanyName (4 bytes) */
1342
1343 /* Name must be >0, but there is no upper limit defined, use UINT32_MAX */
1344 if ((productInfo->cbCompanyName < 2) || (productInfo->cbCompanyName % 2 != 0))
1345 {
1346 WLog_Print(log, WLOG_WARN, "license product info invalid cbCompanyName %" PRIu32,
1347 productInfo->cbCompanyName);
1348 return FALSE;
1349 }
1350
1351 if (!license_check_stream_length(log, s, productInfo->cbCompanyName,
1352 "license product info::CompanyName"))
1353 return FALSE;
1354
1355 productInfo->pbProductId = NULL;
1356 productInfo->pbCompanyName = (BYTE*)malloc(productInfo->cbCompanyName);
1357 if (!productInfo->pbCompanyName)
1358 goto out_fail;
1359 Stream_Read(s, productInfo->pbCompanyName, productInfo->cbCompanyName);
1360
1361 if (!license_check_stream_length(log, s, 4, "license product info::cbProductId"))
1362 goto out_fail;
1363
1364 Stream_Read_UINT32(s, productInfo->cbProductId); /* cbProductId (4 bytes) */
1365
1366 if ((productInfo->cbProductId < 2) || (productInfo->cbProductId % 2 != 0))
1367 {
1368 WLog_Print(log, WLOG_WARN, "license product info invalid cbProductId %" PRIu32,
1369 productInfo->cbProductId);
1370 goto out_fail;
1371 }
1372
1373 if (!license_check_stream_length(log, s, productInfo->cbProductId,
1374 "license product info::ProductId"))
1375 goto out_fail;
1376
1377 productInfo->pbProductId = (BYTE*)malloc(productInfo->cbProductId);
1378 if (!productInfo->pbProductId)
1379 goto out_fail;
1380 Stream_Read(s, productInfo->pbProductId, productInfo->cbProductId);
1381 return TRUE;
1382
1383out_fail:
1384 free(productInfo->pbCompanyName);
1385 free(productInfo->pbProductId);
1386 productInfo->pbCompanyName = NULL;
1387 productInfo->pbProductId = NULL;
1388 return FALSE;
1389}
1390
1391static BOOL license_write_product_info(wLog* log, wStream* s,
1392 const LICENSE_PRODUCT_INFO* productInfo)
1393{
1394 WINPR_ASSERT(productInfo);
1395
1396 if (!license_check_stream_capacity(log, s, 8, "license product info::cbCompanyName"))
1397 return FALSE;
1398
1399 Stream_Write_UINT32(s, productInfo->dwVersion); /* dwVersion (4 bytes) */
1400 Stream_Write_UINT32(s, productInfo->cbCompanyName); /* cbCompanyName (4 bytes) */
1401
1402 /* Name must be >0, but there is no upper limit defined, use UINT32_MAX */
1403 if ((productInfo->cbCompanyName < 2) || (productInfo->cbCompanyName % 2 != 0) ||
1404 !productInfo->pbCompanyName)
1405 {
1406 WLog_Print(log, WLOG_WARN, "license product info invalid cbCompanyName %" PRIu32,
1407 productInfo->cbCompanyName);
1408 return FALSE;
1409 }
1410
1411 if (!license_check_stream_capacity(log, s, productInfo->cbCompanyName,
1412 "license product info::CompanyName"))
1413 return FALSE;
1414
1415 Stream_Write(s, productInfo->pbCompanyName, productInfo->cbCompanyName);
1416
1417 if (!license_check_stream_capacity(log, s, 4, "license product info::cbProductId"))
1418 return FALSE;
1419
1420 Stream_Write_UINT32(s, productInfo->cbProductId); /* cbProductId (4 bytes) */
1421
1422 if ((productInfo->cbProductId < 2) || (productInfo->cbProductId % 2 != 0) ||
1423 !productInfo->pbProductId)
1424 {
1425 WLog_Print(log, WLOG_WARN, "license product info invalid cbProductId %" PRIu32,
1426 productInfo->cbProductId);
1427 return FALSE;
1428 }
1429
1430 if (!license_check_stream_capacity(log, s, productInfo->cbProductId,
1431 "license product info::ProductId"))
1432 return FALSE;
1433
1434 Stream_Write(s, productInfo->pbProductId, productInfo->cbProductId);
1435 return TRUE;
1436}
1437
1444LICENSE_PRODUCT_INFO* license_new_product_info(void)
1445{
1446 LICENSE_PRODUCT_INFO* productInfo =
1447 (LICENSE_PRODUCT_INFO*)calloc(1, sizeof(LICENSE_PRODUCT_INFO));
1448 if (!productInfo)
1449 return NULL;
1450 return productInfo;
1451}
1452
1459void license_free_product_info(LICENSE_PRODUCT_INFO* productInfo)
1460{
1461 if (productInfo)
1462 {
1463 free(productInfo->pbCompanyName);
1464 free(productInfo->pbProductId);
1465 free(productInfo);
1466 }
1467}
1468
1469BOOL license_read_binary_blob_data(wLog* log, LICENSE_BLOB* blob, UINT16 wBlobType,
1470 const void* data, size_t length)
1471{
1472 WINPR_ASSERT(blob);
1473 WINPR_ASSERT(length <= UINT16_MAX);
1474 WINPR_ASSERT(data || (length == 0));
1475
1476 blob->length = (UINT16)length;
1477 free(blob->data);
1478 blob->data = NULL;
1479
1480 if ((blob->type != wBlobType) && (blob->type != BB_ANY_BLOB))
1481 {
1482 WLog_Print(log, WLOG_ERROR, "license binary blob::type expected %s, got %s",
1483 licencse_blob_type_string(wBlobType), licencse_blob_type_string(blob->type));
1484 }
1485
1486 /*
1487 * Server can choose to not send data by setting length to 0.
1488 * If so, it may not bother to set the type, so shortcut the warning
1489 */
1490 if ((blob->type != BB_ANY_BLOB) && (blob->length == 0))
1491 {
1492 WLog_Print(log, WLOG_DEBUG, "license binary blob::type %s, length=0, skipping.",
1493 licencse_blob_type_string(blob->type));
1494 return TRUE;
1495 }
1496
1497 blob->type = wBlobType;
1498 blob->data = NULL;
1499 if (blob->length > 0)
1500 blob->data = malloc(blob->length);
1501 if (!blob->data)
1502 {
1503 WLog_Print(log, WLOG_ERROR, "license binary blob::length=%" PRIu16 ", blob::data=%p",
1504 blob->length, (const void*)blob->data);
1505 return FALSE;
1506 }
1507 memcpy(blob->data, data, blob->length); /* blobData */
1508 return TRUE;
1509}
1510
1518BOOL license_read_binary_blob(wLog* log, wStream* s, LICENSE_BLOB* blob)
1519{
1520 UINT16 wBlobType = 0;
1521 UINT16 length = 0;
1522
1523 WINPR_ASSERT(blob);
1524
1525 if (!license_check_stream_length(log, s, 4, "license binary blob::type"))
1526 return FALSE;
1527
1528 Stream_Read_UINT16(s, wBlobType); /* wBlobType (2 bytes) */
1529 Stream_Read_UINT16(s, length); /* wBlobLen (2 bytes) */
1530
1531 if (!license_check_stream_length(log, s, length, "license binary blob::length"))
1532 return FALSE;
1533
1534 if (!license_read_binary_blob_data(log, blob, wBlobType, Stream_Pointer(s), length))
1535 return FALSE;
1536
1537 return Stream_SafeSeek(s, length);
1538}
1539
1547BOOL license_write_binary_blob(wStream* s, const LICENSE_BLOB* blob)
1548{
1549 WINPR_ASSERT(blob);
1550
1551 if (!Stream_EnsureRemainingCapacity(s, blob->length + 4))
1552 return FALSE;
1553
1554 Stream_Write_UINT16(s, blob->type); /* wBlobType (2 bytes) */
1555 Stream_Write_UINT16(s, blob->length); /* wBlobLen (2 bytes) */
1556
1557 if (blob->length > 0)
1558 Stream_Write(s, blob->data, blob->length); /* blobData */
1559 return TRUE;
1560}
1561
1562static BOOL license_write_encrypted_premaster_secret_blob(wLog* log, wStream* s,
1563 const LICENSE_BLOB* blob,
1564 UINT32 ModulusLength)
1565{
1566 const UINT32 length = ModulusLength + 8;
1567
1568 WINPR_ASSERT(blob);
1569 WINPR_ASSERT(length <= UINT16_MAX);
1570
1571 if (blob->length > ModulusLength)
1572 {
1573 WLog_Print(log, WLOG_ERROR, "invalid blob");
1574 return FALSE;
1575 }
1576
1577 if (!Stream_EnsureRemainingCapacity(s, length + 4))
1578 return FALSE;
1579 Stream_Write_UINT16(s, blob->type); /* wBlobType (2 bytes) */
1580 Stream_Write_UINT16(s, (UINT16)length); /* wBlobLen (2 bytes) */
1581
1582 if (blob->length > 0)
1583 Stream_Write(s, blob->data, blob->length); /* blobData */
1584
1585 Stream_Zero(s, length - blob->length);
1586 return TRUE;
1587}
1588
1589static BOOL license_read_encrypted_premaster_secret_blob(wLog* log, wStream* s, LICENSE_BLOB* blob,
1590 UINT32* ModulusLength)
1591{
1592 if (!license_read_binary_blob(log, s, blob))
1593 return FALSE;
1594 WINPR_ASSERT(ModulusLength);
1595 *ModulusLength = blob->length;
1596 return TRUE;
1597}
1598
1605LICENSE_BLOB* license_new_binary_blob(UINT16 type)
1606{
1607 LICENSE_BLOB* blob = (LICENSE_BLOB*)calloc(1, sizeof(LICENSE_BLOB));
1608 if (blob)
1609 blob->type = type;
1610 return blob;
1611}
1612
1619void license_free_binary_blob(LICENSE_BLOB* blob)
1620{
1621 if (blob)
1622 {
1623 free(blob->data);
1624 free(blob);
1625 }
1626}
1627
1635BOOL license_read_scope_list(wLog* log, wStream* s, SCOPE_LIST* scopeList)
1636{
1637 UINT32 scopeCount = 0;
1638
1639 WINPR_ASSERT(scopeList);
1640
1641 if (!license_check_stream_length(log, s, 4, "license scope list"))
1642 return FALSE;
1643
1644 Stream_Read_UINT32(s, scopeCount); /* ScopeCount (4 bytes) */
1645
1646 if (!license_check_stream_length(log, s, 4ll * scopeCount, "license scope list::count"))
1647 return FALSE;
1648
1649 if (!license_scope_list_resize(scopeList, scopeCount))
1650 return FALSE;
1651 /* ScopeArray */
1652 for (UINT32 i = 0; i < scopeCount; i++)
1653 {
1654 if (!license_read_binary_blob(log, s, scopeList->array[i]))
1655 return FALSE;
1656 }
1657
1658 return TRUE;
1659}
1660
1661BOOL license_write_scope_list(wLog* log, wStream* s, const SCOPE_LIST* scopeList)
1662{
1663 WINPR_ASSERT(scopeList);
1664
1665 if (!license_check_stream_capacity(log, s, 4, "license scope list"))
1666 return FALSE;
1667
1668 Stream_Write_UINT32(s, scopeList->count); /* ScopeCount (4 bytes) */
1669
1670 if (!license_check_stream_capacity(log, s, scopeList->count * 4ull,
1671 "license scope list::count"))
1672 return FALSE;
1673
1674 /* ScopeArray */
1675 WINPR_ASSERT(scopeList->array || (scopeList->count == 0));
1676 for (UINT32 i = 0; i < scopeList->count; i++)
1677 {
1678 const LICENSE_BLOB* element = scopeList->array[i];
1679
1680 if (!license_write_binary_blob(s, element))
1681 return FALSE;
1682 }
1683
1684 return TRUE;
1685}
1686
1693SCOPE_LIST* license_new_scope_list(void)
1694{
1695 SCOPE_LIST* list = calloc(1, sizeof(SCOPE_LIST));
1696 return list;
1697}
1698
1699BOOL license_scope_list_resize(SCOPE_LIST* scopeList, UINT32 count)
1700{
1701 WINPR_ASSERT(scopeList);
1702 WINPR_ASSERT(scopeList->array || (scopeList->count == 0));
1703
1704 for (UINT32 x = count; x < scopeList->count; x++)
1705 {
1706 license_free_binary_blob(scopeList->array[x]);
1707 scopeList->array[x] = NULL;
1708 }
1709
1710 if (count > 0)
1711 {
1712 LICENSE_BLOB** tmp =
1713 (LICENSE_BLOB**)realloc((void*)scopeList->array, count * sizeof(LICENSE_BLOB*));
1714 if (!tmp)
1715 return FALSE;
1716 scopeList->array = tmp;
1717 }
1718 else
1719 {
1720 free((void*)scopeList->array);
1721 scopeList->array = NULL;
1722 }
1723
1724 for (UINT32 x = scopeList->count; x < count; x++)
1725 {
1726 LICENSE_BLOB* blob = license_new_binary_blob(BB_SCOPE_BLOB);
1727 if (!blob)
1728 {
1729 scopeList->count = x;
1730 return FALSE;
1731 }
1732 scopeList->array[x] = blob;
1733 }
1734
1735 scopeList->count = count;
1736 return TRUE;
1737}
1738
1745void license_free_scope_list(SCOPE_LIST* scopeList)
1746{
1747 if (!scopeList)
1748 return;
1749
1750 license_scope_list_resize(scopeList, 0);
1751 free(scopeList);
1752}
1753
1754BOOL license_send_license_info(rdpLicense* license, const LICENSE_BLOB* calBlob,
1755 const BYTE* signature, size_t signature_length)
1756{
1757 WINPR_ASSERT(calBlob);
1758 WINPR_ASSERT(signature);
1759 WINPR_ASSERT(license->certificate);
1760
1761 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1762 if (!info)
1763 return FALSE;
1764
1765 UINT16 sec_flags = 0;
1766 wStream* s = license_send_stream_init(license, &sec_flags);
1767 if (!s)
1768 return FALSE;
1769
1770 if (!license_check_stream_capacity(license->log, s, 8 + sizeof(license->ClientRandom),
1771 "license info::ClientRandom"))
1772 goto error;
1773
1774 Stream_Write_UINT32(s,
1775 license->PreferredKeyExchangeAlg); /* PreferredKeyExchangeAlg (4 bytes) */
1776 Stream_Write_UINT32(s, license->PlatformId); /* PlatformId (4 bytes) */
1777
1778 /* ClientRandom (32 bytes) */
1779 Stream_Write(s, license->ClientRandom, sizeof(license->ClientRandom));
1780
1781 /* Licensing Binary Blob with EncryptedPreMasterSecret: */
1782 if (!license_write_encrypted_premaster_secret_blob(
1783 license->log, s, license->EncryptedPremasterSecret, info->ModulusLength))
1784 goto error;
1785
1786 /* Licensing Binary Blob with LicenseInfo: */
1787 if (!license_write_binary_blob(s, calBlob))
1788 goto error;
1789
1790 /* Licensing Binary Blob with EncryptedHWID */
1791 if (!license_write_binary_blob(s, license->EncryptedHardwareId))
1792 goto error;
1793
1794 /* MACData */
1795 if (!license_check_stream_capacity(license->log, s, signature_length, "license info::MACData"))
1796 goto error;
1797 Stream_Write(s, signature, signature_length);
1798
1799 return license_send(license, s, LICENSE_INFO, sec_flags);
1800
1801error:
1802 Stream_Release(s);
1803 return FALSE;
1804}
1805
1806static BOOL license_check_preferred_alg(rdpLicense* license, UINT32 PreferredKeyExchangeAlg,
1807 const char* where)
1808{
1809 WINPR_ASSERT(license);
1810 WINPR_ASSERT(where);
1811
1812 if (license->PreferredKeyExchangeAlg != PreferredKeyExchangeAlg)
1813 {
1814 char buffer1[64] = { 0 };
1815 char buffer2[64] = { 0 };
1816 WLog_Print(license->log, WLOG_WARN, "%s::PreferredKeyExchangeAlg, expected %s, got %s",
1817 where,
1818 license_preferred_key_exchange_alg_string(license->PreferredKeyExchangeAlg,
1819 buffer1, sizeof(buffer1)),
1820 license_preferred_key_exchange_alg_string(PreferredKeyExchangeAlg, buffer2,
1821 sizeof(buffer2)));
1822 return FALSE;
1823 }
1824 return TRUE;
1825}
1826
1827BOOL license_read_license_info(rdpLicense* license, wStream* s)
1828{
1829 BOOL rc = FALSE;
1830 UINT32 PreferredKeyExchangeAlg = 0;
1831
1832 WINPR_ASSERT(license);
1833 WINPR_ASSERT(license->certificate);
1834
1835 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
1836 if (!info)
1837 goto error;
1838
1839 /* ClientRandom (32 bytes) */
1840 if (!license_check_stream_length(license->log, s, 8 + sizeof(license->ClientRandom),
1841 "license info"))
1842 goto error;
1843
1844 Stream_Read_UINT32(s, PreferredKeyExchangeAlg); /* PreferredKeyExchangeAlg (4 bytes) */
1845 if (!license_check_preferred_alg(license, PreferredKeyExchangeAlg, "license info"))
1846 goto error;
1847 Stream_Read_UINT32(s, license->PlatformId); /* PlatformId (4 bytes) */
1848
1849 /* ClientRandom (32 bytes) */
1850 Stream_Read(s, license->ClientRandom, sizeof(license->ClientRandom));
1851
1852 /* Licensing Binary Blob with EncryptedPreMasterSecret: */
1853 UINT32 ModulusLength = 0;
1854 if (!license_read_encrypted_premaster_secret_blob(
1855 license->log, s, license->EncryptedPremasterSecret, &ModulusLength))
1856 goto error;
1857
1858 if (ModulusLength != info->ModulusLength)
1859 {
1860 WLog_Print(license->log, WLOG_WARN,
1861 "EncryptedPremasterSecret,::ModulusLength[%" PRIu32
1862 "] != rdpCertInfo::ModulusLength[%" PRIu32 "]",
1863 ModulusLength, info->ModulusLength);
1864 goto error;
1865 }
1866 /* Licensing Binary Blob with LicenseInfo: */
1867 if (!license_read_binary_blob(license->log, s, license->LicenseInfo))
1868 goto error;
1869
1870 /* Licensing Binary Blob with EncryptedHWID */
1871 if (!license_read_binary_blob(license->log, s, license->EncryptedHardwareId))
1872 goto error;
1873
1874 /* MACData */
1875 if (!license_check_stream_length(license->log, s, sizeof(license->MACData),
1876 "license info::MACData"))
1877 goto error;
1878 Stream_Read(s, license->MACData, sizeof(license->MACData));
1879
1880 rc = TRUE;
1881
1882error:
1883 return rc;
1884}
1885
1893BOOL license_read_license_request_packet(rdpLicense* license, wStream* s)
1894{
1895 WINPR_ASSERT(license);
1896
1897 /* ServerRandom (32 bytes) */
1898 if (!license_check_stream_length(license->log, s, sizeof(license->ServerRandom),
1899 "license request"))
1900 return FALSE;
1901
1902 Stream_Read(s, license->ServerRandom, sizeof(license->ServerRandom));
1903
1904 /* ProductInfo */
1905 if (!license_read_product_info(license->log, s, license->ProductInfo))
1906 return FALSE;
1907
1908 /* KeyExchangeList */
1909 if (!license_read_binary_blob(license->log, s, license->KeyExchangeList))
1910 return FALSE;
1911
1912 /* ServerCertificate */
1913 if (!license_read_binary_blob(license->log, s, license->ServerCertificate))
1914 return FALSE;
1915
1916 /* ScopeList */
1917 if (!license_read_scope_list(license->log, s, license->ScopeList))
1918 return FALSE;
1919
1920 /* Parse Server Certificate */
1921 if (!freerdp_certificate_read_server_cert(license->certificate,
1922 license->ServerCertificate->data,
1923 license->ServerCertificate->length))
1924 return FALSE;
1925
1926 if (!license_generate_keys(license) || !license_generate_hwid(license) ||
1927 !license_encrypt_premaster_secret(license))
1928 return FALSE;
1929
1930#ifdef WITH_DEBUG_LICENSE
1931 WLog_Print(license->log, WLOG_DEBUG, "ServerRandom:");
1932 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ServerRandom,
1933 sizeof(license->ServerRandom));
1934 license_print_product_info(license->log, license->ProductInfo);
1935 license_print_scope_list(license->log, license->ScopeList);
1936#endif
1937 return TRUE;
1938}
1939
1940BOOL license_write_license_request_packet(const rdpLicense* license, wStream* s)
1941{
1942 WINPR_ASSERT(license);
1943
1944 /* ServerRandom (32 bytes) */
1945 if (!license_check_stream_capacity(license->log, s, sizeof(license->ServerRandom),
1946 "license request"))
1947 return FALSE;
1948 Stream_Write(s, license->ServerRandom, sizeof(license->ServerRandom));
1949
1950 /* ProductInfo */
1951 if (!license_write_product_info(license->log, s, license->ProductInfo))
1952 return FALSE;
1953
1954 /* KeyExchangeList */
1955 if (!license_write_binary_blob(s, license->KeyExchangeList))
1956 return FALSE;
1957
1958 /* ServerCertificate */
1959 if (!license_write_binary_blob(s, license->ServerCertificate))
1960 return FALSE;
1961
1962 /* ScopeList */
1963 if (!license_write_scope_list(license->log, s, license->ScopeList))
1964 return FALSE;
1965
1966 return TRUE;
1967}
1968
1969static BOOL license_send_license_request_packet(rdpLicense* license)
1970{
1971 UINT16 sec_flags = 0;
1972 wStream* s = license_send_stream_init(license, &sec_flags);
1973 if (!s)
1974 return FALSE;
1975
1976 if (!license_write_license_request_packet(license, s))
1977 goto fail;
1978
1979 return license_send(license, s, LICENSE_REQUEST, sec_flags);
1980
1981fail:
1982 Stream_Release(s);
1983 return FALSE;
1984}
1985
1986/*
1987 * Read a PLATFORM_CHALLENGE packet.
1988 * msdn{cc241921}
1989 * @param license license module
1990 * @param s stream
1991 */
1992
1993BOOL license_read_platform_challenge_packet(rdpLicense* license, wStream* s)
1994{
1995 BYTE macData[LICENSING_ENCRYPTION_KEY_LENGTH] = { 0 };
1996
1997 WINPR_ASSERT(license);
1998
1999 DEBUG_LICENSE("Receiving Platform Challenge Packet");
2000
2001 if (!license_check_stream_length(license->log, s, 4, "license platform challenge"))
2002 return FALSE;
2003
2004 /* [MS-RDPELE] 2.2.2.4 Server Platform Challenge (SERVER_PLATFORM_CHALLENGE)
2005 * reserved field */
2006 Stream_Seek_UINT32(s); /* ConnectFlags, Reserved (4 bytes) */
2007
2008 /* EncryptedPlatformChallenge */
2009 license->EncryptedPlatformChallenge->type = BB_ANY_BLOB;
2010 if (!license_read_binary_blob(license->log, s, license->EncryptedPlatformChallenge))
2011 return FALSE;
2012 license->EncryptedPlatformChallenge->type = BB_ENCRYPTED_DATA_BLOB;
2013
2014 /* MACData (16 bytes) */
2015 if (!license_check_stream_length(license->log, s, sizeof(macData),
2016 "license platform challenge::MAC"))
2017 return FALSE;
2018
2019 Stream_Read(s, macData, sizeof(macData));
2020 if (!license_decrypt_and_check_MAC(license, license->EncryptedPlatformChallenge->data,
2021 license->EncryptedPlatformChallenge->length,
2022 license->PlatformChallenge, macData))
2023 return FALSE;
2024
2025#ifdef WITH_DEBUG_LICENSE
2026 WLog_Print(license->log, WLOG_DEBUG, "EncryptedPlatformChallenge:");
2027 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedPlatformChallenge->data,
2028 license->EncryptedPlatformChallenge->length);
2029 WLog_Print(license->log, WLOG_DEBUG, "PlatformChallenge:");
2030 winpr_HexLogDump(license->log, WLOG_DEBUG, license->PlatformChallenge->data,
2031 license->PlatformChallenge->length);
2032 WLog_Print(license->log, WLOG_DEBUG, "MacData:");
2033 winpr_HexLogDump(license->log, WLOG_DEBUG, macData, sizeof(macData));
2034#endif
2035 return TRUE;
2036}
2037
2038BOOL license_send_error_alert(rdpLicense* license, UINT32 dwErrorCode, UINT32 dwStateTransition,
2039 const LICENSE_BLOB* info)
2040{
2041 UINT16 sec_flags = 0;
2042 wStream* s = license_send_stream_init(license, &sec_flags);
2043
2044 if (!s)
2045 goto fail;
2046
2047 if (!license_check_stream_capacity(license->log, s, 8, "license error alert"))
2048 goto fail;
2049 Stream_Write_UINT32(s, dwErrorCode);
2050 Stream_Write_UINT32(s, dwStateTransition);
2051
2052 if (info)
2053 {
2054 if (!license_write_binary_blob(s, info))
2055 goto fail;
2056 }
2057
2058 return license_send(license, s, ERROR_ALERT, sec_flags);
2059fail:
2060 Stream_Release(s);
2061 return FALSE;
2062}
2063
2064BOOL license_send_platform_challenge_packet(rdpLicense* license)
2065{
2066 UINT16 sec_flags = 0;
2067 wStream* s = license_send_stream_init(license, &sec_flags);
2068
2069 if (!s)
2070 goto fail;
2071
2072 DEBUG_LICENSE("Receiving Platform Challenge Packet");
2073
2074 if (!license_check_stream_capacity(license->log, s, 4, "license platform challenge"))
2075 goto fail;
2076
2077 Stream_Zero(s, 4); /* ConnectFlags, Reserved (4 bytes) */
2078
2079 /* EncryptedPlatformChallenge */
2080 if (!license_write_binary_blob(s, license->EncryptedPlatformChallenge))
2081 goto fail;
2082
2083 /* MACData (16 bytes) */
2084 if (!license_check_stream_length(license->log, s, sizeof(license->MACData),
2085 "license platform challenge::MAC"))
2086 goto fail;
2087
2088 Stream_Write(s, license->MACData, sizeof(license->MACData));
2089
2090 return license_send(license, s, PLATFORM_CHALLENGE, sec_flags);
2091fail:
2092 Stream_Release(s);
2093 return FALSE;
2094}
2095
2096static BOOL license_read_encrypted_blob(const rdpLicense* license, wStream* s, LICENSE_BLOB* target)
2097{
2098 UINT16 wBlobType = 0;
2099 UINT16 wBlobLen = 0;
2100
2101 WINPR_ASSERT(license);
2102 WINPR_ASSERT(target);
2103
2104 if (!license_check_stream_length(license->log, s, 4, "license encrypted blob"))
2105 return FALSE;
2106
2107 Stream_Read_UINT16(s, wBlobType);
2108 if (wBlobType != BB_ENCRYPTED_DATA_BLOB)
2109 {
2110 WLog_Print(
2111 license->log, WLOG_WARN,
2112 "expecting BB_ENCRYPTED_DATA_BLOB blob, probably a windows 2003 server, continuing...");
2113 }
2114
2115 Stream_Read_UINT16(s, wBlobLen);
2116
2117 BYTE* encryptedData = Stream_Pointer(s);
2118 if (!Stream_SafeSeek(s, wBlobLen))
2119 {
2120 WLog_Print(license->log, WLOG_WARN,
2121 "short license encrypted blob::length, expected %" PRIu16 " bytes, got %" PRIuz,
2122 wBlobLen, Stream_GetRemainingLength(s));
2123 return FALSE;
2124 }
2125
2126 return license_rc4_with_licenseKey(license, encryptedData, wBlobLen, target);
2127}
2128
2136BOOL license_read_new_or_upgrade_license_packet(rdpLicense* license, wStream* s)
2137{
2138 UINT32 os_major = 0;
2139 UINT32 os_minor = 0;
2140 UINT32 cbScope = 0;
2141 UINT32 cbCompanyName = 0;
2142 UINT32 cbProductId = 0;
2143 UINT32 cbLicenseInfo = 0;
2144 wStream sbuffer = { 0 };
2145 wStream* licenseStream = NULL;
2146 BOOL ret = FALSE;
2147 BYTE computedMac[16] = { 0 };
2148 const BYTE* readMac = NULL;
2149
2150 WINPR_ASSERT(license);
2151
2152 DEBUG_LICENSE("Receiving Server New/Upgrade License Packet");
2153
2154 LICENSE_BLOB* calBlob = license_new_binary_blob(BB_DATA_BLOB);
2155 if (!calBlob)
2156 return FALSE;
2157
2158 /* EncryptedLicenseInfo */
2159 if (!license_read_encrypted_blob(license, s, calBlob))
2160 goto fail;
2161
2162 /* compute MAC and check it */
2163 readMac = Stream_Pointer(s);
2164 if (!Stream_SafeSeek(s, sizeof(computedMac)))
2165 {
2166 WLog_Print(license->log, WLOG_WARN,
2167 "short license new/upgrade, expected 16 bytes, got %" PRIuz,
2168 Stream_GetRemainingLength(s));
2169 goto fail;
2170 }
2171
2172 if (!security_mac_data(license->MacSaltKey, sizeof(license->MacSaltKey), calBlob->data,
2173 calBlob->length, computedMac, sizeof(computedMac)))
2174 goto fail;
2175
2176 if (memcmp(computedMac, readMac, sizeof(computedMac)) != 0)
2177 {
2178 WLog_Print(license->log, WLOG_ERROR, "new or upgrade license MAC mismatch");
2179 goto fail;
2180 }
2181
2182 licenseStream = Stream_StaticConstInit(&sbuffer, calBlob->data, calBlob->length);
2183 if (!licenseStream)
2184 {
2185 WLog_Print(license->log, WLOG_ERROR,
2186 "license::blob::data=%p, license::blob::length=%" PRIu16,
2187 (const void*)calBlob->data, calBlob->length);
2188 goto fail;
2189 }
2190
2191 if (!license_check_stream_length(license->log, licenseStream, 8,
2192 "license new/upgrade::blob::version"))
2193 goto fail;
2194
2195 Stream_Read_UINT16(licenseStream, os_minor);
2196 Stream_Read_UINT16(licenseStream, os_major);
2197
2198 WLog_Print(license->log, WLOG_DEBUG, "Version: %" PRIu16 ".%" PRIu16, os_major, os_minor);
2199
2200 /* Scope */
2201 Stream_Read_UINT32(licenseStream, cbScope);
2202 if (!license_check_stream_length(license->log, licenseStream, cbScope,
2203 "license new/upgrade::blob::scope"))
2204 goto fail;
2205
2206#ifdef WITH_DEBUG_LICENSE
2207 WLog_Print(license->log, WLOG_DEBUG, "Scope:");
2208 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbScope);
2209#endif
2210 Stream_Seek(licenseStream, cbScope);
2211
2212 /* CompanyName */
2213 if (!license_check_stream_length(license->log, licenseStream, 4,
2214 "license new/upgrade::blob::cbCompanyName"))
2215 goto fail;
2216
2217 Stream_Read_UINT32(licenseStream, cbCompanyName);
2218 if (!license_check_stream_length(license->log, licenseStream, cbCompanyName,
2219 "license new/upgrade::blob::CompanyName"))
2220 goto fail;
2221
2222#ifdef WITH_DEBUG_LICENSE
2223 WLog_Print(license->log, WLOG_DEBUG, "Company name:");
2224 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbCompanyName);
2225#endif
2226 Stream_Seek(licenseStream, cbCompanyName);
2227
2228 /* productId */
2229 if (!license_check_stream_length(license->log, licenseStream, 4,
2230 "license new/upgrade::blob::cbProductId"))
2231 goto fail;
2232
2233 Stream_Read_UINT32(licenseStream, cbProductId);
2234
2235 if (!license_check_stream_length(license->log, licenseStream, cbProductId,
2236 "license new/upgrade::blob::ProductId"))
2237 goto fail;
2238
2239#ifdef WITH_DEBUG_LICENSE
2240 WLog_Print(license->log, WLOG_DEBUG, "Product id:");
2241 winpr_HexLogDump(license->log, WLOG_DEBUG, Stream_Pointer(licenseStream), cbProductId);
2242#endif
2243 Stream_Seek(licenseStream, cbProductId);
2244
2245 /* licenseInfo */
2246 if (!license_check_stream_length(license->log, licenseStream, 4,
2247 "license new/upgrade::blob::cbLicenseInfo"))
2248 goto fail;
2249
2250 Stream_Read_UINT32(licenseStream, cbLicenseInfo);
2251 if (!license_check_stream_length(license->log, licenseStream, cbLicenseInfo,
2252 "license new/upgrade::blob::LicenseInfo"))
2253 goto fail;
2254
2255 license->type = LICENSE_TYPE_ISSUED;
2256 ret = license_set_state(license, LICENSE_STATE_COMPLETED);
2257
2258 if (!license->rdp->settings->OldLicenseBehaviour)
2259 ret = saveCal(license->log, license->rdp->settings, Stream_Pointer(licenseStream),
2260 cbLicenseInfo, license->rdp->settings->ClientHostname);
2261
2262fail:
2263 license_free_binary_blob(calBlob);
2264 return ret;
2265}
2266
2274BOOL license_read_error_alert_packet(rdpLicense* license, wStream* s)
2275{
2276 UINT32 dwErrorCode = 0;
2277 UINT32 dwStateTransition = 0;
2278
2279 WINPR_ASSERT(license);
2280 WINPR_ASSERT(license->rdp);
2281
2282 if (!license_check_stream_length(license->log, s, 8ul, "error alert"))
2283 return FALSE;
2284
2285 Stream_Read_UINT32(s, dwErrorCode); /* dwErrorCode (4 bytes) */
2286 Stream_Read_UINT32(s, dwStateTransition); /* dwStateTransition (4 bytes) */
2287
2288 if (!license_read_binary_blob(license->log, s, license->ErrorInfo)) /* bbErrorInfo */
2289 return FALSE;
2290
2291#ifdef WITH_DEBUG_LICENSE
2292 WLog_Print(license->log, WLOG_DEBUG, "dwErrorCode: %s, dwStateTransition: %s",
2293 error_codes[dwErrorCode], state_transitions[dwStateTransition]);
2294#endif
2295
2296 if (dwErrorCode == STATUS_VALID_CLIENT)
2297 {
2298 license->type = LICENSE_TYPE_NONE;
2299 return license_set_state(license, LICENSE_STATE_COMPLETED);
2300 }
2301
2302 switch (dwStateTransition)
2303 {
2304 case ST_TOTAL_ABORT:
2305 license_set_state(license, LICENSE_STATE_ABORTED);
2306 break;
2307 case ST_NO_TRANSITION:
2308 license_set_state(license, LICENSE_STATE_COMPLETED);
2309 break;
2310 case ST_RESET_PHASE_TO_START:
2311 license_set_state(license, LICENSE_STATE_CONFIGURED);
2312 break;
2313 case ST_RESEND_LAST_MESSAGE:
2314 break;
2315 default:
2316 break;
2317 }
2318
2319 return TRUE;
2320}
2321
2329BOOL license_write_new_license_request_packet(const rdpLicense* license, wStream* s)
2330{
2331 WINPR_ASSERT(license);
2332
2333 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
2334 if (!info)
2335 return FALSE;
2336
2337 if (!license_check_stream_capacity(license->log, s, 8 + sizeof(license->ClientRandom),
2338 "License Request"))
2339 return FALSE;
2340
2341 Stream_Write_UINT32(s,
2342 license->PreferredKeyExchangeAlg); /* PreferredKeyExchangeAlg (4 bytes) */
2343 Stream_Write_UINT32(s, license->PlatformId); /* PlatformId (4 bytes) */
2344 Stream_Write(s, license->ClientRandom,
2345 sizeof(license->ClientRandom)); /* ClientRandom (32 bytes) */
2346
2347 if (/* EncryptedPremasterSecret */
2348 !license_write_encrypted_premaster_secret_blob(
2349 license->log, s, license->EncryptedPremasterSecret, info->ModulusLength) ||
2350 /* ClientUserName */
2351 !license_write_binary_blob(s, license->ClientUserName) ||
2352 /* ClientMachineName */
2353 !license_write_binary_blob(s, license->ClientMachineName))
2354 {
2355 return FALSE;
2356 }
2357
2358#ifdef WITH_DEBUG_LICENSE
2359 WLog_Print(license->log, WLOG_DEBUG, "PreferredKeyExchangeAlg: 0x%08" PRIX32 "",
2360 license->PreferredKeyExchangeAlg);
2361 WLog_Print(license->log, WLOG_DEBUG, "ClientRandom:");
2362 winpr_HexLogDump(license->log, WLOG_DEBUG, license->ClientRandom,
2363 sizeof(license->ClientRandom));
2364 WLog_Print(license->log, WLOG_DEBUG, "EncryptedPremasterSecret");
2365 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedPremasterSecret->data,
2366 license->EncryptedPremasterSecret->length);
2367 WLog_Print(license->log, WLOG_DEBUG, "ClientUserName (%" PRIu16 "): %s",
2368 license->ClientUserName->length, (char*)license->ClientUserName->data);
2369 WLog_Print(license->log, WLOG_DEBUG, "ClientMachineName (%" PRIu16 "): %s",
2370 license->ClientMachineName->length, (char*)license->ClientMachineName->data);
2371#endif
2372 return TRUE;
2373}
2374
2375BOOL license_read_new_license_request_packet(rdpLicense* license, wStream* s)
2376{
2377 UINT32 PreferredKeyExchangeAlg = 0;
2378
2379 WINPR_ASSERT(license);
2380
2381 if (!license_check_stream_length(license->log, s, 8ull + sizeof(license->ClientRandom),
2382 "new license request"))
2383 return FALSE;
2384
2385 Stream_Read_UINT32(s, PreferredKeyExchangeAlg); /* PreferredKeyExchangeAlg (4 bytes) */
2386 if (!license_check_preferred_alg(license, PreferredKeyExchangeAlg, "new license request"))
2387 return FALSE;
2388
2389 Stream_Read_UINT32(s, license->PlatformId); /* PlatformId (4 bytes) */
2390 Stream_Read(s, license->ClientRandom,
2391 sizeof(license->ClientRandom)); /* ClientRandom (32 bytes) */
2392
2393 /* EncryptedPremasterSecret */
2394 UINT32 ModulusLength = 0;
2395 if (!license_read_encrypted_premaster_secret_blob(
2396 license->log, s, license->EncryptedPremasterSecret, &ModulusLength))
2397 return FALSE;
2398
2399 const rdpCertInfo* info = freerdp_certificate_get_info(license->certificate);
2400 if (!info)
2401 WLog_Print(license->log, WLOG_WARN,
2402 "Missing license certificate, skipping ModulusLength checks");
2403 else if (ModulusLength != info->ModulusLength)
2404 {
2405 WLog_Print(license->log, WLOG_WARN,
2406 "EncryptedPremasterSecret expected to be %" PRIu32 " bytes, but read %" PRIu32
2407 " bytes",
2408 info->ModulusLength, ModulusLength);
2409 return FALSE;
2410 }
2411
2412 /* ClientUserName */
2413 if (!license_read_binary_blob(license->log, s, license->ClientUserName))
2414 return FALSE;
2415 /* ClientMachineName */
2416 if (!license_read_binary_blob(license->log, s, license->ClientMachineName))
2417 return FALSE;
2418
2419 return TRUE;
2420}
2421
2428BOOL license_answer_license_request(rdpLicense* license)
2429{
2430 UINT16 sec_flags = 0;
2431 wStream* s = NULL;
2432 BYTE* license_data = NULL;
2433 size_t license_size = 0;
2434 BOOL status = 0;
2435 char* username = NULL;
2436
2437 WINPR_ASSERT(license);
2438 WINPR_ASSERT(license->rdp);
2439 WINPR_ASSERT(license->rdp->settings);
2440
2441 if (!license->rdp->settings->OldLicenseBehaviour)
2442 license_data = loadCalFile(license->log, license->rdp->settings,
2443 license->rdp->settings->ClientHostname, &license_size);
2444
2445 if (license_data)
2446 {
2447 LICENSE_BLOB* calBlob = NULL;
2448 BYTE signature[LICENSING_ENCRYPTION_KEY_LENGTH] = { 0 };
2449
2450 DEBUG_LICENSE("Sending Saved License Packet");
2451
2452 WINPR_ASSERT(license->EncryptedHardwareId);
2453 license->EncryptedHardwareId->type = BB_ENCRYPTED_DATA_BLOB;
2454 if (!license_encrypt_and_MAC(license, license->HardwareId, sizeof(license->HardwareId),
2455 license->EncryptedHardwareId, signature, sizeof(signature)))
2456 {
2457 free(license_data);
2458 return FALSE;
2459 }
2460
2461 calBlob = license_new_binary_blob(BB_DATA_BLOB);
2462 if (!calBlob)
2463 {
2464 free(license_data);
2465 return FALSE;
2466 }
2467 calBlob->data = license_data;
2468 WINPR_ASSERT(license_size <= UINT16_MAX);
2469 calBlob->length = (UINT16)license_size;
2470
2471 status = license_send_license_info(license, calBlob, signature, sizeof(signature));
2472 license_free_binary_blob(calBlob);
2473
2474 return status;
2475 }
2476
2477 DEBUG_LICENSE("Sending New License Packet");
2478
2479 s = license_send_stream_init(license, &sec_flags);
2480 if (!s)
2481 return FALSE;
2482 if (license->rdp->settings->Username != NULL)
2483 username = license->rdp->settings->Username;
2484 else
2485 username = "username";
2486
2487 {
2488 WINPR_ASSERT(license->ClientUserName);
2489 const size_t len = strlen(username) + 1;
2490 WINPR_ASSERT(len <= UINT16_MAX);
2491
2492 license->ClientUserName->data = (BYTE*)username;
2493 license->ClientUserName->length = (UINT16)len;
2494 }
2495
2496 {
2497 WINPR_ASSERT(license->ClientMachineName);
2498 const size_t len = strlen(license->rdp->settings->ClientHostname) + 1;
2499 WINPR_ASSERT(len <= UINT16_MAX);
2500
2501 license->ClientMachineName->data = (BYTE*)license->rdp->settings->ClientHostname;
2502 license->ClientMachineName->length = (UINT16)len;
2503 }
2504 status = license_write_new_license_request_packet(license, s);
2505
2506 WINPR_ASSERT(license->ClientUserName);
2507 license->ClientUserName->data = NULL;
2508 license->ClientUserName->length = 0;
2509
2510 WINPR_ASSERT(license->ClientMachineName);
2511 license->ClientMachineName->data = NULL;
2512 license->ClientMachineName->length = 0;
2513
2514 if (!status)
2515 {
2516 Stream_Release(s);
2517 return FALSE;
2518 }
2519
2520 return license_send(license, s, NEW_LICENSE_REQUEST, sec_flags);
2521}
2522
2529BOOL license_send_platform_challenge_response(rdpLicense* license)
2530{
2531 wStream* challengeRespData = NULL;
2532 BYTE* buffer = NULL;
2533 BOOL status = 0;
2534
2535 WINPR_ASSERT(license);
2536 WINPR_ASSERT(license->PlatformChallenge);
2537 WINPR_ASSERT(license->MacSaltKey);
2538 WINPR_ASSERT(license->EncryptedPlatformChallenge);
2539 WINPR_ASSERT(license->EncryptedHardwareId);
2540
2541 DEBUG_LICENSE("Sending Platform Challenge Response Packet");
2542
2543 license->EncryptedPlatformChallenge->type = BB_DATA_BLOB;
2544
2545 /* prepare the PLATFORM_CHALLENGE_RESPONSE_DATA */
2546 challengeRespData = Stream_New(NULL, 8 + license->PlatformChallenge->length);
2547 if (!challengeRespData)
2548 return FALSE;
2549 Stream_Write_UINT16(challengeRespData, PLATFORM_CHALLENGE_RESPONSE_VERSION); /* wVersion */
2550 Stream_Write_UINT16(challengeRespData, license->ClientType); /* wClientType */
2551 Stream_Write_UINT16(challengeRespData, license->LicenseDetailLevel); /* wLicenseDetailLevel */
2552 Stream_Write_UINT16(challengeRespData, license->PlatformChallenge->length); /* cbChallenge */
2553 Stream_Write(challengeRespData, license->PlatformChallenge->data,
2554 license->PlatformChallenge->length); /* pbChallenge */
2555 Stream_SealLength(challengeRespData);
2556
2557 /* compute MAC of PLATFORM_CHALLENGE_RESPONSE_DATA + HWID */
2558 const size_t length = Stream_Length(challengeRespData) + sizeof(license->HardwareId);
2559 buffer = (BYTE*)malloc(length);
2560 if (!buffer)
2561 {
2562 Stream_Free(challengeRespData, TRUE);
2563 return FALSE;
2564 }
2565
2566 CopyMemory(buffer, Stream_Buffer(challengeRespData), Stream_Length(challengeRespData));
2567 CopyMemory(&buffer[Stream_Length(challengeRespData)], license->HardwareId,
2568 sizeof(license->HardwareId));
2569 status = security_mac_data(license->MacSaltKey, sizeof(license->MacSaltKey), buffer, length,
2570 license->MACData, sizeof(license->MACData));
2571 free(buffer);
2572
2573 if (!status)
2574 {
2575 Stream_Free(challengeRespData, TRUE);
2576 return FALSE;
2577 }
2578
2579 license->EncryptedHardwareId->type = BB_ENCRYPTED_DATA_BLOB;
2580 if (!license_rc4_with_licenseKey(license, license->HardwareId, sizeof(license->HardwareId),
2581 license->EncryptedHardwareId))
2582 {
2583 Stream_Free(challengeRespData, TRUE);
2584 return FALSE;
2585 }
2586
2587 status = license_rc4_with_licenseKey(license, Stream_Buffer(challengeRespData),
2588 Stream_Length(challengeRespData),
2589 license->EncryptedPlatformChallengeResponse);
2590 Stream_Free(challengeRespData, TRUE);
2591 if (!status)
2592 return FALSE;
2593
2594#ifdef WITH_DEBUG_LICENSE
2595 WLog_Print(license->log, WLOG_DEBUG, "LicensingEncryptionKey:");
2596 winpr_HexLogDump(license->log, WLOG_DEBUG, license->LicensingEncryptionKey, 16);
2597 WLog_Print(license->log, WLOG_DEBUG, "HardwareId:");
2598 winpr_HexLogDump(license->log, WLOG_DEBUG, license->HardwareId, sizeof(license->HardwareId));
2599 WLog_Print(license->log, WLOG_DEBUG, "EncryptedHardwareId:");
2600 winpr_HexLogDump(license->log, WLOG_DEBUG, license->EncryptedHardwareId->data,
2601 license->EncryptedHardwareId->length);
2602#endif
2603 UINT16 sec_flags = 0;
2604 wStream* s = license_send_stream_init(license, &sec_flags);
2605 if (!s)
2606 return FALSE;
2607
2608 if (license_write_client_platform_challenge_response(license, s))
2609 return license_send(license, s, PLATFORM_CHALLENGE_RESPONSE, sec_flags);
2610
2611 Stream_Release(s);
2612 return FALSE;
2613}
2614
2615BOOL license_read_platform_challenge_response(WINPR_ATTR_UNUSED rdpLicense* license)
2616{
2617 WINPR_ASSERT(license);
2618 WINPR_ASSERT(license->PlatformChallenge);
2619 WINPR_ASSERT(license->MacSaltKey);
2620 WINPR_ASSERT(license->EncryptedPlatformChallenge);
2621 WINPR_ASSERT(license->EncryptedHardwareId);
2622
2623 DEBUG_LICENSE("Receiving Platform Challenge Response Packet");
2624
2625#if defined(WITH_LICENSE_DECRYPT_CHALLENGE_RESPONSE)
2626 BOOL rc = FALSE;
2627 LICENSE_BLOB* dblob = license_new_binary_blob(BB_ANY_BLOB);
2628 if (!dblob)
2629 return FALSE;
2630
2631 wStream sbuffer = { 0 };
2632 UINT16 wVersion = 0;
2633 UINT16 cbChallenge = 0;
2634 const BYTE* pbChallenge = NULL;
2635 LICENSE_BLOB* blob = license->EncryptedPlatformChallengeResponse;
2636
2637 if (!license_rc4_with_licenseKey(license, blob->data, blob->length, dblob))
2638 goto fail;
2639
2640 wStream* s = Stream_StaticConstInit(&sbuffer, dblob->data, dblob->length);
2641 if (!license_check_stream_length(s, 8, "PLATFORM_CHALLENGE_RESPONSE_DATA"))
2642 goto fail;
2643
2644 Stream_Read_UINT16(s, wVersion);
2645 if (wVersion != PLATFORM_CHALLENGE_RESPONSE_VERSION)
2646 {
2647 WLog_Print(license->log, WLOG_WARN,
2648 "Invalid PLATFORM_CHALLENGE_RESPONSE_DATA::wVersion 0x%04" PRIx16
2649 ", expected 0x04" PRIx16,
2650 wVersion, PLATFORM_CHALLENGE_RESPONSE_VERSION);
2651 goto fail;
2652 }
2653 Stream_Read_UINT16(s, license->ClientType);
2654 Stream_Read_UINT16(s, license->LicenseDetailLevel);
2655 Stream_Read_UINT16(s, cbChallenge);
2656
2657 if (!license_check_stream_length(s, cbChallenge,
2658 "PLATFORM_CHALLENGE_RESPONSE_DATA::pbChallenge"))
2659 goto fail;
2660
2661 pbChallenge = Stream_Pointer(s);
2662 if (!license_read_binary_blob_data(license->PlatformChallengeResponse, BB_DATA_BLOB,
2663 pbChallenge, cbChallenge))
2664 goto fail;
2665 if (!Stream_SafeSeek(s, cbChallenge))
2666 goto fail;
2667
2668 rc = TRUE;
2669fail:
2670 license_free_binary_blob(dblob);
2671 return rc;
2672#else
2673 return TRUE;
2674#endif
2675}
2676
2677BOOL license_write_client_platform_challenge_response(rdpLicense* license, wStream* s)
2678{
2679 WINPR_ASSERT(license);
2680
2681 if (!license_write_binary_blob(s, license->EncryptedPlatformChallengeResponse))
2682 return FALSE;
2683 if (!license_write_binary_blob(s, license->EncryptedHardwareId))
2684 return FALSE;
2685 if (!license_check_stream_capacity(license->log, s, sizeof(license->MACData),
2686 "CLIENT_PLATFORM_CHALLENGE_RESPONSE::MACData"))
2687 return FALSE;
2688 Stream_Write(s, license->MACData, sizeof(license->MACData));
2689 return TRUE;
2690}
2691
2692BOOL license_read_client_platform_challenge_response(rdpLicense* license, wStream* s)
2693{
2694 WINPR_ASSERT(license);
2695
2696 if (!license_read_binary_blob(license->log, s, license->EncryptedPlatformChallengeResponse))
2697 return FALSE;
2698 if (!license_read_binary_blob(license->log, s, license->EncryptedHardwareId))
2699 return FALSE;
2700 if (!license_check_stream_length(license->log, s, sizeof(license->MACData),
2701 "CLIENT_PLATFORM_CHALLENGE_RESPONSE::MACData"))
2702 return FALSE;
2703 Stream_Read(s, license->MACData, sizeof(license->MACData));
2704 return license_read_platform_challenge_response(license);
2705}
2706
2716BOOL license_send_valid_client_error_packet(rdpRdp* rdp)
2717{
2718 WINPR_ASSERT(rdp);
2719 rdpLicense* license = rdp->license;
2720 WINPR_ASSERT(license);
2721
2722 license->state = LICENSE_STATE_COMPLETED;
2723 license->type = LICENSE_TYPE_NONE;
2724 return license_send_error_alert(license, STATUS_VALID_CLIENT, ST_NO_TRANSITION,
2725 license->ErrorInfo);
2726}
2727
2734rdpLicense* license_new(rdpRdp* rdp)
2735{
2736 WINPR_ASSERT(rdp);
2737
2738 rdpLicense* license = (rdpLicense*)calloc(1, sizeof(rdpLicense));
2739 if (!license)
2740 return NULL;
2741 license->log = WLog_Get(LICENSE_TAG);
2742 WINPR_ASSERT(license->log);
2743
2744 license->PlatformId = PLATFORMID;
2745 license->ClientType = OTHER_PLATFORM_CHALLENGE_TYPE;
2746 license->LicenseDetailLevel = LICENSE_DETAIL_DETAIL;
2747 license->PreferredKeyExchangeAlg = KEY_EXCHANGE_ALG_RSA;
2748 license->rdp = rdp;
2749
2750 license_set_state(license, LICENSE_STATE_INITIAL);
2751 if (!(license->certificate = freerdp_certificate_new()))
2752 goto out_error;
2753 if (!(license->ProductInfo = license_new_product_info()))
2754 goto out_error;
2755 if (!(license->ErrorInfo = license_new_binary_blob(BB_ERROR_BLOB)))
2756 goto out_error;
2757 if (!(license->LicenseInfo = license_new_binary_blob(BB_DATA_BLOB)))
2758 goto out_error;
2759 if (!(license->KeyExchangeList = license_new_binary_blob(BB_KEY_EXCHG_ALG_BLOB)))
2760 goto out_error;
2761 if (!(license->ServerCertificate = license_new_binary_blob(BB_CERTIFICATE_BLOB)))
2762 goto out_error;
2763 if (!(license->ClientUserName = license_new_binary_blob(BB_CLIENT_USER_NAME_BLOB)))
2764 goto out_error;
2765 if (!(license->ClientMachineName = license_new_binary_blob(BB_CLIENT_MACHINE_NAME_BLOB)))
2766 goto out_error;
2767 if (!(license->PlatformChallenge = license_new_binary_blob(BB_ANY_BLOB)))
2768 goto out_error;
2769 if (!(license->PlatformChallengeResponse = license_new_binary_blob(BB_ANY_BLOB)))
2770 goto out_error;
2771 if (!(license->EncryptedPlatformChallenge = license_new_binary_blob(BB_ANY_BLOB)))
2772 goto out_error;
2773 if (!(license->EncryptedPlatformChallengeResponse =
2774 license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2775 goto out_error;
2776 if (!(license->EncryptedPremasterSecret = license_new_binary_blob(BB_ANY_BLOB)))
2777 goto out_error;
2778 if (!(license->EncryptedHardwareId = license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2779 goto out_error;
2780 if (!(license->EncryptedLicenseInfo = license_new_binary_blob(BB_ENCRYPTED_DATA_BLOB)))
2781 goto out_error;
2782 if (!(license->ScopeList = license_new_scope_list()))
2783 goto out_error;
2784
2785 license_generate_randoms(license);
2786
2787 return license;
2788
2789out_error:
2790 WINPR_PRAGMA_DIAG_PUSH
2791 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2792 license_free(license);
2793 WINPR_PRAGMA_DIAG_POP
2794 return NULL;
2795}
2796
2802void license_free(rdpLicense* license)
2803{
2804 if (license)
2805 {
2806 freerdp_certificate_free(license->certificate);
2807 license_free_product_info(license->ProductInfo);
2808 license_free_binary_blob(license->ErrorInfo);
2809 license_free_binary_blob(license->LicenseInfo);
2810 license_free_binary_blob(license->KeyExchangeList);
2811 license_free_binary_blob(license->ServerCertificate);
2812 license_free_binary_blob(license->ClientUserName);
2813 license_free_binary_blob(license->ClientMachineName);
2814 license_free_binary_blob(license->PlatformChallenge);
2815 license_free_binary_blob(license->PlatformChallengeResponse);
2816 license_free_binary_blob(license->EncryptedPlatformChallenge);
2817 license_free_binary_blob(license->EncryptedPlatformChallengeResponse);
2818 license_free_binary_blob(license->EncryptedPremasterSecret);
2819 license_free_binary_blob(license->EncryptedHardwareId);
2820 license_free_binary_blob(license->EncryptedLicenseInfo);
2821 license_free_scope_list(license->ScopeList);
2822 free(license);
2823 }
2824}
2825
2826LICENSE_STATE license_get_state(const rdpLicense* license)
2827{
2828 WINPR_ASSERT(license);
2829 return license->state;
2830}
2831
2832LICENSE_TYPE license_get_type(const rdpLicense* license)
2833{
2834 WINPR_ASSERT(license);
2835 return license->type;
2836}
2837
2838BOOL license_set_state(rdpLicense* license, LICENSE_STATE state)
2839{
2840 WINPR_ASSERT(license);
2841 license->state = state;
2842 switch (state)
2843 {
2844 case LICENSE_STATE_COMPLETED:
2845 break;
2846 case LICENSE_STATE_ABORTED:
2847 default:
2848 license->type = LICENSE_TYPE_INVALID;
2849 break;
2850 }
2851
2852 return TRUE;
2853}
2854
2855const char* license_get_state_string(LICENSE_STATE state)
2856{
2857 switch (state)
2858 {
2859 case LICENSE_STATE_INITIAL:
2860 return "LICENSE_STATE_INITIAL";
2861 case LICENSE_STATE_CONFIGURED:
2862 return "LICENSE_STATE_CONFIGURED";
2863 case LICENSE_STATE_REQUEST:
2864 return "LICENSE_STATE_REQUEST";
2865 case LICENSE_STATE_NEW_REQUEST:
2866 return "LICENSE_STATE_NEW_REQUEST";
2867 case LICENSE_STATE_PLATFORM_CHALLENGE:
2868 return "LICENSE_STATE_PLATFORM_CHALLENGE";
2869 case LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE:
2870 return "LICENSE_STATE_PLATFORM_CHALLENGE_RESPONSE";
2871 case LICENSE_STATE_COMPLETED:
2872 return "LICENSE_STATE_COMPLETED";
2873 case LICENSE_STATE_ABORTED:
2874 return "LICENSE_STATE_ABORTED";
2875 default:
2876 return "LICENSE_STATE_UNKNOWN";
2877 }
2878}
2879
2880BOOL license_server_send_request(rdpLicense* license)
2881{
2882 if (!license_ensure_state(license, LICENSE_STATE_CONFIGURED, LICENSE_REQUEST))
2883 return FALSE;
2884 if (!license_send_license_request_packet(license))
2885 return FALSE;
2886 return license_set_state(license, LICENSE_STATE_REQUEST);
2887}
2888
2889static BOOL license_set_string(wLog* log, const char* what, const char* value, BYTE** bdst,
2890 UINT32* dstLen)
2891{
2892 WINPR_ASSERT(what);
2893 WINPR_ASSERT(value);
2894 WINPR_ASSERT(bdst);
2895 WINPR_ASSERT(dstLen);
2896
2897 union
2898 {
2899 WCHAR** w;
2900 BYTE** b;
2901 } cnv;
2902 cnv.b = bdst;
2903
2904 size_t len = 0;
2905 *cnv.w = ConvertUtf8ToWCharAlloc(value, &len);
2906 if (!*cnv.w || (len > UINT32_MAX / sizeof(WCHAR)))
2907 {
2908 WLog_Print(log, WLOG_ERROR, "license->ProductInfo: %s == %p || %" PRIuz " > UINT32_MAX",
2909 what, (void*)(*cnv.w), len);
2910 return FALSE;
2911 }
2912 *dstLen = (UINT32)(len * sizeof(WCHAR));
2913 return TRUE;
2914}
2915
2916BOOL license_server_configure(rdpLicense* license)
2917{
2918 wStream* s = NULL;
2919 UINT32 algs[] = { KEY_EXCHANGE_ALG_RSA };
2920
2921 WINPR_ASSERT(license);
2922 WINPR_ASSERT(license->rdp);
2923
2924 const rdpSettings* settings = license->rdp->settings;
2925
2926 const char* CompanyName =
2927 freerdp_settings_get_string(settings, FreeRDP_ServerLicenseCompanyName);
2928
2929 const char* ProductName =
2930 freerdp_settings_get_string(settings, FreeRDP_ServerLicenseProductName);
2931 const UINT32 ProductVersion =
2932 freerdp_settings_get_uint32(settings, FreeRDP_ServerLicenseProductVersion);
2933 const UINT32 issuerCount =
2934 freerdp_settings_get_uint32(settings, FreeRDP_ServerLicenseProductIssuersCount);
2935
2936 const void* ptr = freerdp_settings_get_pointer(settings, FreeRDP_ServerLicenseProductIssuers);
2937 const char** issuers = WINPR_REINTERPRET_CAST(ptr, const void*, const char**);
2938
2939 WINPR_ASSERT(CompanyName);
2940 WINPR_ASSERT(ProductName);
2941 WINPR_ASSERT(ProductVersion > 0);
2942 WINPR_ASSERT(issuers || (issuerCount == 0));
2943
2944 if (!license_ensure_state(license, LICENSE_STATE_INITIAL, LICENSE_REQUEST))
2945 return FALSE;
2946
2947 license->ProductInfo->dwVersion = ProductVersion;
2948 if (!license_set_string(license->log, "pbCompanyName", CompanyName,
2949 &license->ProductInfo->pbCompanyName,
2950 &license->ProductInfo->cbCompanyName))
2951 return FALSE;
2952
2953 if (!license_set_string(license->log, "pbProductId", ProductName,
2954 &license->ProductInfo->pbProductId, &license->ProductInfo->cbProductId))
2955 return FALSE;
2956
2957 if (!license_read_binary_blob_data(license->log, license->KeyExchangeList,
2958 BB_KEY_EXCHG_ALG_BLOB, algs, sizeof(algs)))
2959 return FALSE;
2960
2961 if (!freerdp_certificate_read_server_cert(license->certificate, settings->ServerCertificate,
2962 settings->ServerCertificateLength))
2963 return FALSE;
2964
2965 s = Stream_New(NULL, 1024);
2966 if (!s)
2967 return FALSE;
2968 else
2969 {
2970 BOOL r = FALSE;
2971 SSIZE_T res =
2972 freerdp_certificate_write_server_cert(license->certificate, CERT_CHAIN_VERSION_2, s);
2973 if (res >= 0)
2974 r = license_read_binary_blob_data(license->log, license->ServerCertificate,
2975 BB_CERTIFICATE_BLOB, Stream_Buffer(s),
2976 Stream_GetPosition(s));
2977
2978 Stream_Free(s, TRUE);
2979 if (!r)
2980 return FALSE;
2981 }
2982
2983 if (!license_scope_list_resize(license->ScopeList, issuerCount))
2984 return FALSE;
2985 for (size_t x = 0; x < issuerCount; x++)
2986 {
2987 LICENSE_BLOB* blob = license->ScopeList->array[x];
2988 const char* name = issuers[x];
2989 const size_t length = strnlen(name, UINT16_MAX) + 1;
2990 if ((length == 0) || (length > UINT16_MAX))
2991 {
2992 WLog_Print(license->log, WLOG_WARN,
2993 "Invalid issuer at position %" PRIuz ": length 0 < %" PRIuz " <= %d"
2994 " ['%s']",
2995 x, length, UINT16_MAX, name);
2996 return FALSE;
2997 }
2998 if (!license_read_binary_blob_data(license->log, blob, BB_SCOPE_BLOB, name, length))
2999 return FALSE;
3000 }
3001
3002 return license_set_state(license, LICENSE_STATE_CONFIGURED);
3003}
3004
3005rdpLicense* license_get(rdpContext* context)
3006{
3007 WINPR_ASSERT(context);
3008 WINPR_ASSERT(context->rdp);
3009 return context->rdp->license;
3010}
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API const void * freerdp_settings_get_pointer(const rdpSettings *settings, FreeRDP_Settings_Keys_Pointer id)
Returns a immutable pointer settings value.
FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.