FreeRDP
Loading...
Searching...
No Matches
privatekey.c
1
25#include <freerdp/config.h>
26
27#include <errno.h>
28#include <stdio.h>
29#include <string.h>
30
31#include <winpr/assert.h>
32#include <winpr/wtypes.h>
33#include <winpr/crt.h>
34#include <winpr/file.h>
35#include <winpr/crypto.h>
36
37#include <openssl/pem.h>
38#include <openssl/rsa.h>
39#include <openssl/bn.h>
40#include <openssl/err.h>
41
42#include "privatekey.h"
43#include "cert_common.h"
44
45#include <freerdp/crypto/privatekey.h>
46
47#include <openssl/evp.h>
48
49#if defined(OPENSSL_VERSION_MAJOR) && (OPENSSL_VERSION_MAJOR >= 3)
50#include <openssl/core_names.h>
51#endif
52
53#include "x509_utils.h"
54#include "crypto.h"
55#include "opensslcompat.h"
56
57#define TAG FREERDP_TAG("crypto")
58
59struct rdp_private_key
60{
61 EVP_PKEY* evp;
62
63 rdpCertInfo cert;
64 BYTE* PrivateExponent;
65 DWORD PrivateExponentLength;
66};
67
68/*
69 * Terminal Services Signing Keys.
70 * Yes, Terminal Services Private Key is publicly available.
71 */
72
73static BYTE tssk_modulus[] = { 0x3d, 0x3a, 0x5e, 0xbd, 0x72, 0x43, 0x3e, 0xc9, 0x4d, 0xbb, 0xc1,
74 0x1e, 0x4a, 0xba, 0x5f, 0xcb, 0x3e, 0x88, 0x20, 0x87, 0xef, 0xf5,
75 0xc1, 0xe2, 0xd7, 0xb7, 0x6b, 0x9a, 0xf2, 0x52, 0x45, 0x95, 0xce,
76 0x63, 0x65, 0x6b, 0x58, 0x3a, 0xfe, 0xef, 0x7c, 0xe7, 0xbf, 0xfe,
77 0x3d, 0xf6, 0x5c, 0x7d, 0x6c, 0x5e, 0x06, 0x09, 0x1a, 0xf5, 0x61,
78 0xbb, 0x20, 0x93, 0x09, 0x5f, 0x05, 0x6d, 0xea, 0x87 };
79
80static BYTE tssk_privateExponent[] = {
81 0x87, 0xa7, 0x19, 0x32, 0xda, 0x11, 0x87, 0x55, 0x58, 0x00, 0x16, 0x16, 0x25, 0x65, 0x68, 0xf8,
82 0x24, 0x3e, 0xe6, 0xfa, 0xe9, 0x67, 0x49, 0x94, 0xcf, 0x92, 0xcc, 0x33, 0x99, 0xe8, 0x08, 0x60,
83 0x17, 0x9a, 0x12, 0x9f, 0x24, 0xdd, 0xb1, 0x24, 0x99, 0xc7, 0x3a, 0xb8, 0x0a, 0x7b, 0x0d, 0xdd,
84 0x35, 0x07, 0x79, 0x17, 0x0b, 0x51, 0x9b, 0xb3, 0xc7, 0x10, 0x01, 0x13, 0xe7, 0x3f, 0xf3, 0x5f
85};
86
87static const rdpPrivateKey tssk = { .PrivateExponent = tssk_privateExponent,
88 .PrivateExponentLength = sizeof(tssk_privateExponent),
89 .cert = { .Modulus = tssk_modulus,
90 .ModulusLength = sizeof(tssk_modulus) } };
91const rdpPrivateKey* priv_key_tssk = &tssk;
92
93#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
94static RSA* evp_pkey_to_rsa(const rdpPrivateKey* key)
95{
96 if (!freerdp_key_is_rsa(key))
97 {
98 WLog_WARN(TAG, "Key is no RSA key");
99 return nullptr;
100 }
101
102 RSA* rsa = nullptr;
103 BIO* bio = BIO_new(
104#if defined(LIBRESSL_VERSION_NUMBER)
105 BIO_s_mem()
106#else
107 BIO_s_secmem()
108#endif
109 );
110 if (!bio)
111 return nullptr;
112 const int rc = PEM_write_bio_PrivateKey(bio, key->evp, nullptr, nullptr, 0, nullptr, nullptr);
113 if (rc != 1)
114 goto fail;
115 rsa = PEM_read_bio_RSAPrivateKey(bio, nullptr, nullptr, nullptr);
116fail:
117 BIO_free_all(bio);
118 return rsa;
119}
120#endif
121
122static EVP_PKEY* evp_pkey_utils_from_pem(const char* data, size_t len, BOOL fromFile,
123 const char* password)
124{
125 EVP_PKEY* evp = nullptr;
126 BIO* bio = nullptr;
127 if (fromFile)
128 bio = BIO_new_file(data, "rb");
129 else
130 {
131 if (len > INT_MAX)
132 return nullptr;
133 bio = BIO_new_mem_buf(data, (int)len);
134 }
135
136 if (!bio)
137 {
138 WLog_ERR(TAG, "BIO_new failed for private key");
139 return nullptr;
140 }
141
142 evp =
143 PEM_read_bio_PrivateKey(bio, nullptr, nullptr, WINPR_CAST_CONST_PTR_AWAY(password, void*));
144 BIO_free_all(bio);
145 if (!evp)
146 WLog_ERR(TAG, "PEM_read_bio_PrivateKey returned nullptr [input length %" PRIuz "]", len);
147
148 return evp;
149}
150
151static BOOL key_read_private(rdpPrivateKey* key)
152{
153 BOOL rc = FALSE;
154
155 WINPR_ASSERT(key);
156 WINPR_ASSERT(key->evp);
157
158 /* The key is not an RSA key, that means we just return success. */
159 if (!freerdp_key_is_rsa(key))
160 return TRUE;
161
162#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
163 RSA* rsa = evp_pkey_to_rsa(key);
164 if (!rsa)
165 {
166 char ebuffer[256] = WINPR_C_ARRAY_INIT;
167 WLog_ERR(TAG, "unable to load RSA key: %s.",
168 winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
169 goto fail;
170 }
171
172 switch (RSA_check_key(rsa))
173 {
174 case 0:
175 WLog_ERR(TAG, "invalid RSA key");
176 goto fail;
177
178 case 1:
179 /* Valid key. */
180 break;
181
182 default:
183 {
184 char ebuffer[256] = WINPR_C_ARRAY_INIT;
185 WLog_ERR(TAG, "unexpected error when checking RSA key: %s.",
186 winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
187 goto fail;
188 }
189 }
190
191 const BIGNUM* rsa_e = nullptr;
192 const BIGNUM* rsa_n = nullptr;
193 const BIGNUM* rsa_d = nullptr;
194
195 RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
196#else
197 BIGNUM* rsa_e = nullptr;
198 BIGNUM* rsa_n = nullptr;
199 BIGNUM* rsa_d = nullptr;
200
201 if (!EVP_PKEY_get_bn_param(key->evp, OSSL_PKEY_PARAM_RSA_N, &rsa_n))
202 goto fail;
203 if (!EVP_PKEY_get_bn_param(key->evp, OSSL_PKEY_PARAM_RSA_E, &rsa_e))
204 goto fail;
205 if (!EVP_PKEY_get_bn_param(key->evp, OSSL_PKEY_PARAM_RSA_D, &rsa_d))
206 goto fail;
207#endif
208 if (BN_num_bytes(rsa_e) > 4)
209 {
210 WLog_ERR(TAG, "RSA public exponent too large");
211 goto fail;
212 }
213
214 if (!read_bignum(&key->PrivateExponent, &key->PrivateExponentLength, rsa_d, TRUE))
215 goto fail;
216
217 if (!cert_info_create(&key->cert, rsa_n, rsa_e))
218 goto fail;
219 rc = TRUE;
220fail:
221#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
222 RSA_free(rsa);
223#else
224 BN_free(rsa_d);
225 BN_free(rsa_e);
226 BN_free(rsa_n);
227#endif
228 return rc;
229}
230
231rdpPrivateKey* freerdp_key_new_from_pem(const char* pem)
232{
233 return freerdp_key_new_from_pem_enc(pem, nullptr);
234}
235
236rdpPrivateKey* freerdp_key_new_from_pem_enc(const char* pem, const char* password)
237{
238 rdpPrivateKey* key = freerdp_key_new();
239 if (!key || !pem)
240 goto fail;
241 key->evp = evp_pkey_utils_from_pem(pem, strlen(pem), FALSE, password);
242 if (!key->evp)
243 goto fail;
244 if (!key_read_private(key))
245 goto fail;
246 return key;
247fail:
248 freerdp_key_free(key);
249 return nullptr;
250}
251
252rdpPrivateKey* freerdp_key_new_from_file(const char* keyfile)
253{
254 return freerdp_key_new_from_file_enc(keyfile, nullptr);
255}
256
257rdpPrivateKey* freerdp_key_new_from_file_enc(const char* keyfile, const char* password)
258{
259 rdpPrivateKey* key = freerdp_key_new();
260 if (!key || !keyfile)
261 goto fail;
262
263 key->evp = evp_pkey_utils_from_pem(keyfile, strlen(keyfile), TRUE, password);
264 if (!key->evp)
265 goto fail;
266 if (!key_read_private(key))
267 goto fail;
268 return key;
269fail:
270 freerdp_key_free(key);
271 return nullptr;
272}
273
274rdpPrivateKey* freerdp_key_new(void)
275{
276 return calloc(1, sizeof(rdpPrivateKey));
277}
278
279rdpPrivateKey* freerdp_key_clone(const rdpPrivateKey* key)
280{
281 if (!key)
282 return nullptr;
283
284 rdpPrivateKey* _key = (rdpPrivateKey*)calloc(1, sizeof(rdpPrivateKey));
285
286 if (!_key)
287 return nullptr;
288
289 if (key->evp)
290 {
291 _key->evp = key->evp;
292 if (!_key->evp)
293 goto out_fail;
294 EVP_PKEY_up_ref(_key->evp);
295 }
296
297 if (key->PrivateExponent)
298 {
299 _key->PrivateExponent = (BYTE*)malloc(key->PrivateExponentLength);
300
301 if (!_key->PrivateExponent)
302 goto out_fail;
303
304 CopyMemory(_key->PrivateExponent, key->PrivateExponent, key->PrivateExponentLength);
305 _key->PrivateExponentLength = key->PrivateExponentLength;
306 }
307
308 if (!cert_info_clone(&_key->cert, &key->cert))
309 goto out_fail;
310
311 return _key;
312out_fail:
313 WINPR_PRAGMA_DIAG_PUSH
314 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
315 freerdp_key_free(_key);
316 WINPR_PRAGMA_DIAG_POP
317 return nullptr;
318}
319
320void freerdp_key_free(rdpPrivateKey* key)
321{
322 if (!key)
323 return;
324
325 EVP_PKEY_free(key->evp);
326 if (key->PrivateExponent)
327 memset(key->PrivateExponent, 0, key->PrivateExponentLength);
328 free(key->PrivateExponent);
329 cert_info_free(&key->cert);
330 free(key);
331}
332
333const rdpCertInfo* freerdp_key_get_info(const rdpPrivateKey* key)
334{
335 WINPR_ASSERT(key);
336 if (!freerdp_key_is_rsa(key))
337 return nullptr;
338 return &key->cert;
339}
340
341const BYTE* freerdp_key_get_exponent(const rdpPrivateKey* key, size_t* plength)
342{
343 WINPR_ASSERT(key);
344 if (!freerdp_key_is_rsa(key))
345 {
346 if (plength)
347 *plength = 0;
348 return nullptr;
349 }
350
351 if (plength)
352 *plength = key->PrivateExponentLength;
353 return key->PrivateExponent;
354}
355
356EVP_PKEY* freerdp_key_get_evp_pkey(const rdpPrivateKey* key)
357{
358 WINPR_ASSERT(key);
359
360 EVP_PKEY* evp = key->evp;
361 WINPR_ASSERT(evp);
362 EVP_PKEY_up_ref(evp);
363 return evp;
364}
365
366BOOL freerdp_key_is_rsa(const rdpPrivateKey* key)
367{
368 WINPR_ASSERT(key);
369 if (key == priv_key_tssk)
370 return TRUE;
371
372 WINPR_ASSERT(key->evp);
373 return (EVP_PKEY_id(key->evp) == EVP_PKEY_RSA);
374}
375
376size_t freerdp_key_get_bits(const rdpPrivateKey* key)
377{
378 int rc = -1;
379#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
380 RSA* rsa = evp_pkey_to_rsa(key);
381 if (rsa)
382 {
383 rc = RSA_bits(rsa);
384 RSA_free(rsa);
385 }
386#else
387 rc = EVP_PKEY_get_bits(key->evp);
388#endif
389
390 return WINPR_ASSERTING_INT_CAST(size_t, rc);
391}
392
393BOOL freerdp_key_generate(rdpPrivateKey* key, const char* type, size_t count, ...)
394{
395 BOOL rc = FALSE;
396
397 if (!type)
398 {
399 WLog_ERR(TAG, "Invalid argument type=%s", type);
400 return FALSE;
401 }
402 if (strncmp("RSA", type, 4) != 0)
403 {
404 WLog_ERR(TAG, "Argument type=%s is currently not supported, aborting", type);
405 return FALSE;
406 }
407 if (count != 1)
408 {
409 WLog_ERR(TAG, "Argument type=%s requires count=1, got %" PRIuz ", aborting", type, count);
410 return FALSE;
411 }
412 va_list ap = WINPR_C_ARRAY_INIT;
413 va_start(ap, count);
414 const int key_length = va_arg(ap, int);
415 va_end(ap);
416
417#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
418 RSA* rsa = nullptr;
419#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
420 rsa = RSA_generate_key(key_length, RSA_F4, nullptr, nullptr);
421#else
422 {
423 BIGNUM* bn = BN_secure_new();
424
425 if (!bn)
426 return FALSE;
427
428 rsa = RSA_new();
429
430 if (!rsa)
431 {
432 BN_clear_free(bn);
433 return FALSE;
434 }
435
436 BN_set_word(bn, RSA_F4);
437 const int res = RSA_generate_key_ex(rsa, key_length, bn, nullptr);
438 BN_clear_free(bn);
439
440 if (res != 1)
441 return FALSE;
442 }
443#endif
444
445 EVP_PKEY_free(key->evp);
446 key->evp = EVP_PKEY_new();
447
448 if (!EVP_PKEY_assign_RSA(key->evp, rsa))
449 {
450 EVP_PKEY_free(key->evp);
451 key->evp = nullptr;
452 RSA_free(rsa);
453 return FALSE;
454 }
455
456 rc = TRUE;
457#else
458 EVP_PKEY_CTX* pctx = EVP_PKEY_CTX_new_from_name(nullptr, type, nullptr);
459 if (!pctx)
460 return FALSE;
461
462 if (EVP_PKEY_keygen_init(pctx) != 1)
463 goto fail;
464
465 if (EVP_PKEY_CTX_set_rsa_keygen_bits(pctx, key_length) != 1)
466 goto fail;
467
468 EVP_PKEY_free(key->evp);
469 key->evp = nullptr;
470
471 if (EVP_PKEY_generate(pctx, &key->evp) != 1)
472 goto fail;
473
474 rc = TRUE;
475fail:
476 EVP_PKEY_CTX_free(pctx);
477#endif
478 return rc;
479}
480
481BYTE* freerdp_key_get_param(const rdpPrivateKey* key, enum FREERDP_KEY_PARAM param, size_t* plength)
482{
483 BYTE* buf = nullptr;
484
485 WINPR_ASSERT(key);
486 WINPR_ASSERT(plength);
487
488 *plength = 0;
489
490 BIGNUM* bn = nullptr;
491#if OPENSSL_VERSION_NUMBER >= 0x30000000L
492
493 const char* pk = nullptr;
494 switch (param)
495 {
496 case FREERDP_KEY_PARAM_RSA_D:
497 pk = OSSL_PKEY_PARAM_RSA_D;
498 break;
499 case FREERDP_KEY_PARAM_RSA_E:
500 pk = OSSL_PKEY_PARAM_RSA_E;
501 break;
502 case FREERDP_KEY_PARAM_RSA_N:
503 pk = OSSL_PKEY_PARAM_RSA_N;
504 break;
505 default:
506 return nullptr;
507 }
508
509 if (!EVP_PKEY_get_bn_param(key->evp, pk, &bn))
510 return nullptr;
511#else
512 {
513 const RSA* rsa = EVP_PKEY_get0_RSA(key->evp);
514 if (!rsa)
515 return nullptr;
516
517 const BIGNUM* cbn = nullptr;
518 switch (param)
519 {
520 case FREERDP_KEY_PARAM_RSA_D:
521#if OPENSSL_VERSION_NUMBER >= 0x10101007L
522 cbn = RSA_get0_d(rsa);
523#endif
524 break;
525 case FREERDP_KEY_PARAM_RSA_E:
526#if OPENSSL_VERSION_NUMBER >= 0x10101007L
527 cbn = RSA_get0_e(rsa);
528#endif
529 break;
530 case FREERDP_KEY_PARAM_RSA_N:
531#if OPENSSL_VERSION_NUMBER >= 0x10101007L
532 cbn = RSA_get0_n(rsa);
533#endif
534 break;
535 default:
536 return nullptr;
537 }
538 if (!cbn)
539 return nullptr;
540 bn = BN_dup(cbn);
541 if (!bn)
542 return nullptr;
543 }
544#endif
545
546 const int length = BN_num_bytes(bn);
547 if (length < 0)
548 goto fail;
549
550 {
551 const size_t alloc_size = (size_t)length + 1ull;
552 buf = calloc(alloc_size, sizeof(BYTE));
553 }
554
555 if (!buf)
556 goto fail;
557
558 {
559 const int bnlen = BN_bn2bin(bn, buf);
560 if (bnlen != length)
561 {
562 free(buf);
563 buf = nullptr;
564 }
565 else
566 *plength = WINPR_ASSERTING_INT_CAST(size_t, length);
567 }
568
569fail:
570 BN_free(bn);
571 return buf;
572}
573
574WINPR_DIGEST_CTX* freerdp_key_digest_sign(rdpPrivateKey* key, WINPR_MD_TYPE digest)
575{
576 WINPR_DIGEST_CTX* md_ctx = winpr_Digest_New();
577 if (!md_ctx)
578 return nullptr;
579
580 if (!winpr_DigestSign_Init(md_ctx, digest, key->evp))
581 {
582 winpr_Digest_Free(md_ctx);
583 return nullptr;
584 }
585 return md_ctx;
586}
587
588static BOOL bio_read_pem(BIO* bio, char** ppem, size_t* plength)
589{
590 BOOL rc = FALSE;
591
592 WINPR_ASSERT(bio);
593 WINPR_ASSERT(ppem);
594
595 const size_t blocksize = 2048;
596 size_t offset = 0;
597 size_t length = blocksize;
598 char* pem = nullptr;
599
600 *ppem = nullptr;
601 if (plength)
602 *plength = 0;
603
604 while (offset < length)
605 {
606 char* tmp = realloc(pem, length + 1);
607 if (!tmp)
608 goto fail;
609 pem = tmp;
610
611 ERR_clear_error();
612
613 const int status = BIO_read(bio, &pem[offset], (int)(length - offset));
614 if (status < 0)
615 {
616 WLog_ERR(TAG, "failed to read certificate");
617 goto fail;
618 }
619
620 if (status == 0)
621 break;
622
623 offset += (size_t)status;
624 if (length - offset > 0)
625 break;
626 length += blocksize;
627 }
628
629 if (pem)
630 {
631 if (offset >= length)
632 goto fail;
633 pem[offset] = '\0';
634 }
635 *ppem = pem;
636 if (plength)
637 *plength = offset;
638 rc = TRUE;
639fail:
640 if (!rc)
641 free(pem);
642
643 return rc;
644}
645
646char* freerdp_key_get_pem(const rdpPrivateKey* key, size_t* plen, const char* password)
647{
648 WINPR_ASSERT(key);
649
650 if (!key->evp)
651 return nullptr;
652
657 BIO* bio = BIO_new(BIO_s_mem());
658
659 if (!bio)
660 {
661 WLog_ERR(TAG, "BIO_new() failure");
662 return nullptr;
663 }
664
665 char* pem = nullptr;
666
667 const EVP_CIPHER* enc = nullptr;
668 if (password)
669 enc = EVP_aes_256_xts();
670
671 const int status = PEM_write_bio_PrivateKey(bio, key->evp, enc, nullptr, 0, nullptr,
672 WINPR_CAST_CONST_PTR_AWAY(password, void*));
673 if (status < 0)
674 {
675 WLog_ERR(TAG, "PEM_write_bio_PrivateKey failure: %d", status);
676 goto fail;
677 }
678
679 (void)bio_read_pem(bio, &pem, plen);
680
681fail:
682 BIO_free_all(bio);
683 return pem;
684}