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 int pem_pwd_cb(char* buf, int size, WINPR_ATTR_UNUSED int rwflag, void* userdata)
123{
124 const char* pwd = userdata;
125 if (!pwd || (size < 0))
126 return -1;
127 if (size == 0)
128 return 0;
129
130 size_t len = strlen(pwd);
131 if (len >= WINPR_ASSERTING_INT_CAST(size_t, size))
132 len = WINPR_ASSERTING_INT_CAST(size_t, size) - 1;
133 memcpy(buf, pwd, len);
134 buf[len] = '\0';
135 return WINPR_ASSERTING_INT_CAST(int, len);
136}
137
138static EVP_PKEY* evp_pkey_utils_from_pem(const char* data, size_t len, BOOL fromFile,
139 const char* password)
140{
141 EVP_PKEY* evp = nullptr;
142 BIO* bio = nullptr;
143 if (fromFile)
144 bio = BIO_new_file(data, "rb");
145 else
146 {
147 if (len > INT_MAX)
148 return nullptr;
149 bio = BIO_new_mem_buf(data, (int)len);
150 }
151
152 if (!bio)
153 {
154 WLog_ERR(TAG, "BIO_new failed for private key");
155 return nullptr;
156 }
157
158 evp = PEM_read_bio_PrivateKey(bio, nullptr, pem_pwd_cb,
159 WINPR_CAST_CONST_PTR_AWAY(password, void*));
160 BIO_free_all(bio);
161 if (!evp)
162 WLog_ERR(TAG, "PEM_read_bio_PrivateKey returned nullptr [input length %" PRIuz "]", len);
163
164 return evp;
165}
166
167static BOOL key_read_private(rdpPrivateKey* key)
168{
169 BOOL rc = FALSE;
170
171 WINPR_ASSERT(key);
172 WINPR_ASSERT(key->evp);
173
174 /* The key is not an RSA key, that means we just return success. */
175 if (!freerdp_key_is_rsa(key))
176 return TRUE;
177
178#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
179 RSA* rsa = evp_pkey_to_rsa(key);
180 if (!rsa)
181 {
182 char ebuffer[256] = WINPR_C_ARRAY_INIT;
183 WLog_ERR(TAG, "unable to load RSA key: %s.",
184 winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
185 goto fail;
186 }
187
188 switch (RSA_check_key(rsa))
189 {
190 case 0:
191 WLog_ERR(TAG, "invalid RSA key");
192 goto fail;
193
194 case 1:
195 /* Valid key. */
196 break;
197
198 default:
199 {
200 char ebuffer[256] = WINPR_C_ARRAY_INIT;
201 WLog_ERR(TAG, "unexpected error when checking RSA key: %s.",
202 winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
203 goto fail;
204 }
205 }
206
207 const BIGNUM* rsa_e = nullptr;
208 const BIGNUM* rsa_n = nullptr;
209 const BIGNUM* rsa_d = nullptr;
210
211 RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
212#else
213 BIGNUM* rsa_e = nullptr;
214 BIGNUM* rsa_n = nullptr;
215 BIGNUM* rsa_d = nullptr;
216
217 if (!EVP_PKEY_get_bn_param(key->evp, OSSL_PKEY_PARAM_RSA_N, &rsa_n))
218 goto fail;
219 if (!EVP_PKEY_get_bn_param(key->evp, OSSL_PKEY_PARAM_RSA_E, &rsa_e))
220 goto fail;
221 if (!EVP_PKEY_get_bn_param(key->evp, OSSL_PKEY_PARAM_RSA_D, &rsa_d))
222 goto fail;
223#endif
224 if (BN_num_bytes(rsa_e) > 4)
225 {
226 WLog_ERR(TAG, "RSA public exponent too large");
227 goto fail;
228 }
229
230 if (!read_bignum(&key->PrivateExponent, &key->PrivateExponentLength, rsa_d, TRUE))
231 goto fail;
232
233 if (!cert_info_create(&key->cert, rsa_n, rsa_e))
234 goto fail;
235 rc = TRUE;
236fail:
237#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
238 RSA_free(rsa);
239#else
240 BN_free(rsa_d);
241 BN_free(rsa_e);
242 BN_free(rsa_n);
243#endif
244 return rc;
245}
246
247rdpPrivateKey* freerdp_key_new_from_pem(const char* pem)
248{
249 return freerdp_key_new_from_pem_enc(pem, nullptr);
250}
251
252rdpPrivateKey* freerdp_key_new_from_pem_enc(const char* pem, const char* password)
253{
254 rdpPrivateKey* key = freerdp_key_new();
255 if (!key || !pem)
256 goto fail;
257
258 key->evp = evp_pkey_utils_from_pem(pem, strlen(pem), FALSE, password);
259 if (!key->evp)
260 goto fail;
261 if (!key_read_private(key))
262 goto fail;
263 return key;
264fail:
265 freerdp_key_free(key);
266 return nullptr;
267}
268
269rdpPrivateKey* freerdp_key_new_from_file(const char* keyfile)
270{
271 return freerdp_key_new_from_file_enc(keyfile, nullptr);
272}
273
274rdpPrivateKey* freerdp_key_new_from_file_enc(const char* keyfile, const char* password)
275{
276 rdpPrivateKey* key = freerdp_key_new();
277 if (!key || !keyfile)
278 goto fail;
279
280 key->evp = evp_pkey_utils_from_pem(keyfile, strlen(keyfile), TRUE, password);
281 if (!key->evp)
282 goto fail;
283 if (!key_read_private(key))
284 goto fail;
285 return key;
286fail:
287 freerdp_key_free(key);
288 return nullptr;
289}
290
291rdpPrivateKey* freerdp_key_new(void)
292{
293 return calloc(1, sizeof(rdpPrivateKey));
294}
295
296rdpPrivateKey* freerdp_key_clone(const rdpPrivateKey* key)
297{
298 if (!key)
299 return nullptr;
300
301 rdpPrivateKey* _key = (rdpPrivateKey*)calloc(1, sizeof(rdpPrivateKey));
302
303 if (!_key)
304 return nullptr;
305
306 if (key->evp)
307 {
308 _key->evp = key->evp;
309 if (!_key->evp)
310 goto out_fail;
311 EVP_PKEY_up_ref(_key->evp);
312 }
313
314 if (key->PrivateExponent)
315 {
316 _key->PrivateExponent = (BYTE*)malloc(key->PrivateExponentLength);
317
318 if (!_key->PrivateExponent)
319 goto out_fail;
320
321 CopyMemory(_key->PrivateExponent, key->PrivateExponent, key->PrivateExponentLength);
322 _key->PrivateExponentLength = key->PrivateExponentLength;
323 }
324
325 if (!cert_info_clone(&_key->cert, &key->cert))
326 goto out_fail;
327
328 return _key;
329out_fail:
330 WINPR_PRAGMA_DIAG_PUSH
331 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
332 freerdp_key_free(_key);
333 WINPR_PRAGMA_DIAG_POP
334 return nullptr;
335}
336
337void freerdp_key_free(rdpPrivateKey* key)
338{
339 if (!key)
340 return;
341
342 EVP_PKEY_free(key->evp);
343 if (key->PrivateExponent)
344 memset(key->PrivateExponent, 0, key->PrivateExponentLength);
345 free(key->PrivateExponent);
346 cert_info_free(&key->cert);
347 free(key);
348}
349
350const rdpCertInfo* freerdp_key_get_info(const rdpPrivateKey* key)
351{
352 WINPR_ASSERT(key);
353 if (!freerdp_key_is_rsa(key))
354 return nullptr;
355 return &key->cert;
356}
357
358const BYTE* freerdp_key_get_exponent(const rdpPrivateKey* key, size_t* plength)
359{
360 WINPR_ASSERT(key);
361 if (!freerdp_key_is_rsa(key))
362 {
363 if (plength)
364 *plength = 0;
365 return nullptr;
366 }
367
368 if (plength)
369 *plength = key->PrivateExponentLength;
370 return key->PrivateExponent;
371}
372
373EVP_PKEY* freerdp_key_get_evp_pkey(const rdpPrivateKey* key)
374{
375 WINPR_ASSERT(key);
376
377 EVP_PKEY* evp = key->evp;
378 WINPR_ASSERT(evp);
379 EVP_PKEY_up_ref(evp);
380 return evp;
381}
382
383BOOL freerdp_key_is_rsa(const rdpPrivateKey* key)
384{
385 WINPR_ASSERT(key);
386 if (key == priv_key_tssk)
387 return TRUE;
388
389 WINPR_ASSERT(key->evp);
390 return (EVP_PKEY_id(key->evp) == EVP_PKEY_RSA);
391}
392
393size_t freerdp_key_get_bits(const rdpPrivateKey* key)
394{
395 int rc = -1;
396#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
397 RSA* rsa = evp_pkey_to_rsa(key);
398 if (rsa)
399 {
400 rc = RSA_bits(rsa);
401 RSA_free(rsa);
402 }
403#else
404 rc = EVP_PKEY_get_bits(key->evp);
405#endif
406
407 return WINPR_ASSERTING_INT_CAST(size_t, rc);
408}
409
410BOOL freerdp_key_generate(rdpPrivateKey* key, const char* type, size_t count, ...)
411{
412 BOOL rc = FALSE;
413
414 if (!type)
415 {
416 WLog_ERR(TAG, "Invalid argument type=%s", type);
417 return FALSE;
418 }
419 if (strncmp("RSA", type, 4) != 0)
420 {
421 WLog_ERR(TAG, "Argument type=%s is currently not supported, aborting", type);
422 return FALSE;
423 }
424 if (count != 1)
425 {
426 WLog_ERR(TAG, "Argument type=%s requires count=1, got %" PRIuz ", aborting", type, count);
427 return FALSE;
428 }
429 va_list ap = WINPR_C_ARRAY_INIT;
430 va_start(ap, count);
431 const int key_length = va_arg(ap, int);
432 va_end(ap);
433
434#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
435 RSA* rsa = nullptr;
436#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
437 rsa = RSA_generate_key(key_length, RSA_F4, nullptr, nullptr);
438#else
439 {
440 BIGNUM* bn = BN_secure_new();
441
442 if (!bn)
443 return FALSE;
444
445 rsa = RSA_new();
446
447 if (!rsa)
448 {
449 BN_clear_free(bn);
450 return FALSE;
451 }
452
453 BN_set_word(bn, RSA_F4);
454 const int res = RSA_generate_key_ex(rsa, key_length, bn, nullptr);
455 BN_clear_free(bn);
456
457 if (res != 1)
458 return FALSE;
459 }
460#endif
461
462 EVP_PKEY_free(key->evp);
463 key->evp = EVP_PKEY_new();
464
465 if (!EVP_PKEY_assign_RSA(key->evp, rsa))
466 {
467 EVP_PKEY_free(key->evp);
468 key->evp = nullptr;
469 RSA_free(rsa);
470 return FALSE;
471 }
472
473 rc = TRUE;
474#else
475 EVP_PKEY_CTX* pctx = EVP_PKEY_CTX_new_from_name(nullptr, type, nullptr);
476 if (!pctx)
477 return FALSE;
478
479 if (EVP_PKEY_keygen_init(pctx) != 1)
480 goto fail;
481
482 if (EVP_PKEY_CTX_set_rsa_keygen_bits(pctx, key_length) != 1)
483 goto fail;
484
485 EVP_PKEY_free(key->evp);
486 key->evp = nullptr;
487
488 if (EVP_PKEY_generate(pctx, &key->evp) != 1)
489 goto fail;
490
491 rc = TRUE;
492fail:
493 EVP_PKEY_CTX_free(pctx);
494#endif
495 return rc;
496}
497
498BYTE* freerdp_key_get_param(const rdpPrivateKey* key, enum FREERDP_KEY_PARAM param, size_t* plength)
499{
500 BYTE* buf = nullptr;
501
502 WINPR_ASSERT(key);
503 WINPR_ASSERT(plength);
504
505 *plength = 0;
506
507 BIGNUM* bn = nullptr;
508#if OPENSSL_VERSION_NUMBER >= 0x30000000L
509
510 const char* pk = nullptr;
511 switch (param)
512 {
513 case FREERDP_KEY_PARAM_RSA_D:
514 pk = OSSL_PKEY_PARAM_RSA_D;
515 break;
516 case FREERDP_KEY_PARAM_RSA_E:
517 pk = OSSL_PKEY_PARAM_RSA_E;
518 break;
519 case FREERDP_KEY_PARAM_RSA_N:
520 pk = OSSL_PKEY_PARAM_RSA_N;
521 break;
522 default:
523 return nullptr;
524 }
525
526 if (!EVP_PKEY_get_bn_param(key->evp, pk, &bn))
527 return nullptr;
528#else
529 {
530 const RSA* rsa = EVP_PKEY_get0_RSA(key->evp);
531 if (!rsa)
532 return nullptr;
533
534 const BIGNUM* cbn = nullptr;
535 switch (param)
536 {
537 case FREERDP_KEY_PARAM_RSA_D:
538#if OPENSSL_VERSION_NUMBER >= 0x10101007L
539 cbn = RSA_get0_d(rsa);
540#endif
541 break;
542 case FREERDP_KEY_PARAM_RSA_E:
543#if OPENSSL_VERSION_NUMBER >= 0x10101007L
544 cbn = RSA_get0_e(rsa);
545#endif
546 break;
547 case FREERDP_KEY_PARAM_RSA_N:
548#if OPENSSL_VERSION_NUMBER >= 0x10101007L
549 cbn = RSA_get0_n(rsa);
550#endif
551 break;
552 default:
553 return nullptr;
554 }
555 if (!cbn)
556 return nullptr;
557 bn = BN_dup(cbn);
558 if (!bn)
559 return nullptr;
560 }
561#endif
562
563 const int length = BN_num_bytes(bn);
564 if (length < 0)
565 goto fail;
566
567 {
568 const size_t alloc_size = (size_t)length + 1ull;
569 buf = calloc(alloc_size, sizeof(BYTE));
570 }
571
572 if (!buf)
573 goto fail;
574
575 {
576 const int bnlen = BN_bn2bin(bn, buf);
577 if (bnlen != length)
578 {
579 free(buf);
580 buf = nullptr;
581 }
582 else
583 *plength = WINPR_ASSERTING_INT_CAST(size_t, length);
584 }
585
586fail:
587 BN_free(bn);
588 return buf;
589}
590
591WINPR_DIGEST_CTX* freerdp_key_digest_sign(rdpPrivateKey* key, WINPR_MD_TYPE digest)
592{
593 WINPR_DIGEST_CTX* md_ctx = winpr_Digest_New();
594 if (!md_ctx)
595 return nullptr;
596
597 if (!winpr_DigestSign_Init(md_ctx, digest, key->evp))
598 {
599 winpr_Digest_Free(md_ctx);
600 return nullptr;
601 }
602 return md_ctx;
603}
604
605static BOOL bio_read_pem(BIO* bio, char** ppem, size_t* plength)
606{
607 BOOL rc = FALSE;
608
609 WINPR_ASSERT(bio);
610 WINPR_ASSERT(ppem);
611
612 const size_t blocksize = 2048;
613 size_t offset = 0;
614 size_t length = blocksize;
615 char* pem = nullptr;
616
617 *ppem = nullptr;
618 if (plength)
619 *plength = 0;
620
621 while (offset < length)
622 {
623 char* tmp = realloc(pem, length + 1);
624 if (!tmp)
625 goto fail;
626 pem = tmp;
627
628 ERR_clear_error();
629
630 const int status = BIO_read(bio, &pem[offset], (int)(length - offset));
631 if (status < 0)
632 {
633 WLog_ERR(TAG, "failed to read certificate");
634 goto fail;
635 }
636
637 if (status == 0)
638 break;
639
640 offset += (size_t)status;
641 if (length - offset > 0)
642 break;
643 length += blocksize;
644 }
645
646 if (pem)
647 {
648 if (offset >= length)
649 goto fail;
650 pem[offset] = '\0';
651 }
652 *ppem = pem;
653 if (plength)
654 *plength = offset;
655 rc = TRUE;
656fail:
657 if (!rc)
658 free(pem);
659
660 return rc;
661}
662
663char* freerdp_key_get_pem(const rdpPrivateKey* key, size_t* plen, const char* password)
664{
665 WINPR_ASSERT(key);
666
667 if (!key->evp)
668 return nullptr;
669
674 BIO* bio = BIO_new(BIO_s_mem());
675
676 if (!bio)
677 {
678 WLog_ERR(TAG, "BIO_new() failure");
679 return nullptr;
680 }
681
682 char* pem = nullptr;
683
684 const EVP_CIPHER* enc = nullptr;
685 if (password)
686 enc = EVP_aes_256_xts();
687
688 const int status = PEM_write_bio_PrivateKey(bio, key->evp, enc, nullptr, 0, nullptr,
689 WINPR_CAST_CONST_PTR_AWAY(password, void*));
690 if (status < 0)
691 {
692 WLog_ERR(TAG, "PEM_write_bio_PrivateKey failure: %d", status);
693 goto fail;
694 }
695
696 (void)bio_read_pem(bio, &pem, plen);
697
698fail:
699 BIO_free_all(bio);
700 return pem;
701}