25#include <freerdp/config.h>
31#include <winpr/assert.h>
32#include <winpr/wtypes.h>
34#include <winpr/file.h>
35#include <winpr/crypto.h>
37#include <openssl/pem.h>
38#include <openssl/rsa.h>
39#include <openssl/bn.h>
40#include <openssl/err.h>
42#include "privatekey.h"
43#include "cert_common.h"
45#include <freerdp/crypto/privatekey.h>
47#include <openssl/evp.h>
49#if defined(OPENSSL_VERSION_MAJOR) && (OPENSSL_VERSION_MAJOR >= 3)
50#include <openssl/core_names.h>
53#include "x509_utils.h"
55#include "opensslcompat.h"
57#define TAG FREERDP_TAG("crypto")
64 BYTE* PrivateExponent;
65 DWORD PrivateExponentLength;
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 };
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
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;
93#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
94static RSA* evp_pkey_to_rsa(
const rdpPrivateKey* key)
96 if (!freerdp_key_is_rsa(key))
98 WLog_WARN(TAG,
"Key is no RSA key");
104#
if defined(LIBRESSL_VERSION_NUMBER)
112 const int rc = PEM_write_bio_PrivateKey(bio, key->evp, NULL, NULL, 0, NULL, NULL);
115 rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL);
122static EVP_PKEY* evp_pkey_utils_from_pem(
const char* data,
size_t len, BOOL fromFile,
123 const char* password)
125 EVP_PKEY* evp = NULL;
128 bio = BIO_new_file(data,
"rb");
133 bio = BIO_new_mem_buf(data, (
int)len);
138 WLog_ERR(TAG,
"BIO_new failed for private key");
142 evp = PEM_read_bio_PrivateKey(bio, NULL, NULL, WINPR_CAST_CONST_PTR_AWAY(password,
void*));
145 WLog_ERR(TAG,
"PEM_read_bio_PrivateKey returned NULL [input length %" PRIuz
"]", len);
150static BOOL key_read_private(rdpPrivateKey* key)
155 WINPR_ASSERT(key->evp);
158 if (!freerdp_key_is_rsa(key))
161#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
162 RSA* rsa = evp_pkey_to_rsa(key);
165 char ebuffer[256] = { 0 };
166 WLog_ERR(TAG,
"unable to load RSA key: %s.",
167 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)));
171 switch (RSA_check_key(rsa))
174 WLog_ERR(TAG,
"invalid RSA key");
183 char ebuffer[256] = { 0 };
184 WLog_ERR(TAG,
"unexpected error when checking RSA key: %s.",
185 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)));
190 const BIGNUM* rsa_e = NULL;
191 const BIGNUM* rsa_n = NULL;
192 const BIGNUM* rsa_d = NULL;
194 RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
196 BIGNUM* rsa_e = NULL;
197 BIGNUM* rsa_n = NULL;
198 BIGNUM* rsa_d = NULL;
200 if (!EVP_PKEY_get_bn_param(key->evp, OSSL_PKEY_PARAM_RSA_N, &rsa_n))
202 if (!EVP_PKEY_get_bn_param(key->evp, OSSL_PKEY_PARAM_RSA_E, &rsa_e))
204 if (!EVP_PKEY_get_bn_param(key->evp, OSSL_PKEY_PARAM_RSA_D, &rsa_d))
207 if (BN_num_bytes(rsa_e) > 4)
209 WLog_ERR(TAG,
"RSA public exponent too large");
213 if (!read_bignum(&key->PrivateExponent, &key->PrivateExponentLength, rsa_d, TRUE))
216 if (!cert_info_create(&key->cert, rsa_n, rsa_e))
220#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
230rdpPrivateKey* freerdp_key_new_from_pem(
const char* pem)
232 return freerdp_key_new_from_pem_enc(pem, NULL);
235rdpPrivateKey* freerdp_key_new_from_pem_enc(
const char* pem,
const char* password)
237 rdpPrivateKey* key = freerdp_key_new();
240 key->evp = evp_pkey_utils_from_pem(pem, strlen(pem), FALSE, password);
243 if (!key_read_private(key))
247 freerdp_key_free(key);
251rdpPrivateKey* freerdp_key_new_from_file(
const char* keyfile)
253 return freerdp_key_new_from_file_enc(keyfile, NULL);
256rdpPrivateKey* freerdp_key_new_from_file_enc(
const char* keyfile,
const char* password)
258 rdpPrivateKey* key = freerdp_key_new();
259 if (!key || !keyfile)
262 key->evp = evp_pkey_utils_from_pem(keyfile, strlen(keyfile), TRUE, password);
265 if (!key_read_private(key))
269 freerdp_key_free(key);
273rdpPrivateKey* freerdp_key_new(
void)
275 return calloc(1,
sizeof(rdpPrivateKey));
278rdpPrivateKey* freerdp_key_clone(
const rdpPrivateKey* key)
283 rdpPrivateKey* _key = (rdpPrivateKey*)calloc(1,
sizeof(rdpPrivateKey));
290 _key->evp = key->evp;
293 EVP_PKEY_up_ref(_key->evp);
296 if (key->PrivateExponent)
298 _key->PrivateExponent = (BYTE*)malloc(key->PrivateExponentLength);
300 if (!_key->PrivateExponent)
303 CopyMemory(_key->PrivateExponent, key->PrivateExponent, key->PrivateExponentLength);
304 _key->PrivateExponentLength = key->PrivateExponentLength;
307 if (!cert_info_clone(&_key->cert, &key->cert))
312 WINPR_PRAGMA_DIAG_PUSH
313 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
314 freerdp_key_free(_key);
315 WINPR_PRAGMA_DIAG_POP
319void freerdp_key_free(rdpPrivateKey* key)
324 EVP_PKEY_free(key->evp);
325 if (key->PrivateExponent)
326 memset(key->PrivateExponent, 0, key->PrivateExponentLength);
327 free(key->PrivateExponent);
328 cert_info_free(&key->cert);
332const rdpCertInfo* freerdp_key_get_info(
const rdpPrivateKey* key)
335 if (!freerdp_key_is_rsa(key))
340const BYTE* freerdp_key_get_exponent(
const rdpPrivateKey* key,
size_t* plength)
343 if (!freerdp_key_is_rsa(key))
351 *plength = key->PrivateExponentLength;
352 return key->PrivateExponent;
355EVP_PKEY* freerdp_key_get_evp_pkey(
const rdpPrivateKey* key)
359 EVP_PKEY* evp = key->evp;
361 EVP_PKEY_up_ref(evp);
365BOOL freerdp_key_is_rsa(
const rdpPrivateKey* key)
368 if (key == priv_key_tssk)
371 WINPR_ASSERT(key->evp);
372 return (EVP_PKEY_id(key->evp) == EVP_PKEY_RSA);
375size_t freerdp_key_get_bits(
const rdpPrivateKey* key)
378#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
379 RSA* rsa = evp_pkey_to_rsa(key);
386 rc = EVP_PKEY_get_bits(key->evp);
389 return WINPR_ASSERTING_INT_CAST(
size_t, rc);
392BOOL freerdp_key_generate(rdpPrivateKey* key,
const char* type,
size_t count, ...)
398 WLog_ERR(TAG,
"Invalid argument type=%s", type);
401 if (strncmp(
"RSA", type, 4) != 0)
403 WLog_ERR(TAG,
"Argument type=%s is currently not supported, aborting", type);
408 WLog_ERR(TAG,
"Argument type=%s requires count=1, got %" PRIuz
", aborting", type, count);
413 const int key_length = va_arg(ap,
int);
416#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
418#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
419 rsa = RSA_generate_key(key_length, RSA_F4, NULL, NULL);
422 BIGNUM* bn = BN_secure_new();
435 BN_set_word(bn, RSA_F4);
436 const int res = RSA_generate_key_ex(rsa, key_length, bn, NULL);
444 EVP_PKEY_free(key->evp);
445 key->evp = EVP_PKEY_new();
447 if (!EVP_PKEY_assign_RSA(key->evp, rsa))
449 EVP_PKEY_free(key->evp);
457 EVP_PKEY_CTX* pctx = EVP_PKEY_CTX_new_from_name(NULL, type, NULL);
461 if (EVP_PKEY_keygen_init(pctx) != 1)
464 if (EVP_PKEY_CTX_set_rsa_keygen_bits(pctx, key_length) != 1)
467 EVP_PKEY_free(key->evp);
470 if (EVP_PKEY_generate(pctx, &key->evp) != 1)
475 EVP_PKEY_CTX_free(pctx);
480BYTE* freerdp_key_get_param(
const rdpPrivateKey* key,
enum FREERDP_KEY_PARAM param,
size_t* plength)
485 WINPR_ASSERT(plength);
490#if OPENSSL_VERSION_NUMBER >= 0x30000000L
492 const char* pk = NULL;
495 case FREERDP_KEY_PARAM_RSA_D:
496 pk = OSSL_PKEY_PARAM_RSA_D;
498 case FREERDP_KEY_PARAM_RSA_E:
499 pk = OSSL_PKEY_PARAM_RSA_E;
501 case FREERDP_KEY_PARAM_RSA_N:
502 pk = OSSL_PKEY_PARAM_RSA_N;
508 if (!EVP_PKEY_get_bn_param(key->evp, pk, &bn))
512 const RSA* rsa = EVP_PKEY_get0_RSA(key->evp);
516 const BIGNUM* cbn = NULL;
519 case FREERDP_KEY_PARAM_RSA_D:
520#if OPENSSL_VERSION_NUMBER >= 0x10101007L
521 cbn = RSA_get0_d(rsa);
524 case FREERDP_KEY_PARAM_RSA_E:
525#if OPENSSL_VERSION_NUMBER >= 0x10101007L
526 cbn = RSA_get0_e(rsa);
529 case FREERDP_KEY_PARAM_RSA_N:
530#if OPENSSL_VERSION_NUMBER >= 0x10101007L
531 cbn = RSA_get0_n(rsa);
545 const int length = BN_num_bytes(bn);
549 const size_t alloc_size = (size_t)length + 1ull;
550 buf = calloc(alloc_size,
sizeof(BYTE));
554 const int bnlen = BN_bn2bin(bn, buf);
561 *plength = WINPR_ASSERTING_INT_CAST(
size_t, length);
568WINPR_DIGEST_CTX* freerdp_key_digest_sign(rdpPrivateKey* key, WINPR_MD_TYPE digest)
570 WINPR_DIGEST_CTX* md_ctx = winpr_Digest_New();
574 if (!winpr_DigestSign_Init(md_ctx, digest, key->evp))
576 winpr_Digest_Free(md_ctx);
582static BOOL bio_read_pem(BIO* bio,
char** ppem,
size_t* plength)
589 const size_t blocksize = 2048;
591 size_t length = blocksize;
598 while (offset < length)
600 char* tmp = realloc(pem, length + 1);
607 const int status = BIO_read(bio, &pem[offset], (
int)(length - offset));
610 WLog_ERR(TAG,
"failed to read certificate");
617 offset += (size_t)status;
618 if (length - offset > 0)
625 if (offset >= length)
640char* freerdp_key_get_pem(
const rdpPrivateKey* key,
size_t* plen,
const char* password)
651 BIO* bio = BIO_new(BIO_s_mem());
655 WLog_ERR(TAG,
"BIO_new() failure");
661 const EVP_CIPHER* enc = NULL;
663 enc = EVP_aes_256_xts();
665 const int status = PEM_write_bio_PrivateKey(bio, key->evp, enc, NULL, 0, 0,
666 WINPR_CAST_CONST_PTR_AWAY(password,
void*));
669 WLog_ERR(TAG,
"PEM_write_bio_PrivateKey failure: %d", status);
673 (void)bio_read_pem(bio, &pem, plen);