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,
nullptr,
nullptr, 0,
nullptr,
nullptr);
115 rsa = PEM_read_bio_RSAPrivateKey(bio,
nullptr,
nullptr,
nullptr);
122static EVP_PKEY* evp_pkey_utils_from_pem(
const char* data,
size_t len, BOOL fromFile,
123 const char* password)
125 EVP_PKEY* evp =
nullptr;
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");
143 PEM_read_bio_PrivateKey(bio,
nullptr,
nullptr, WINPR_CAST_CONST_PTR_AWAY(password,
void*));
146 WLog_ERR(TAG,
"PEM_read_bio_PrivateKey returned nullptr [input length %" PRIuz
"]", len);
151static BOOL key_read_private(rdpPrivateKey* key)
156 WINPR_ASSERT(key->evp);
159 if (!freerdp_key_is_rsa(key))
162#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
163 RSA* rsa = evp_pkey_to_rsa(key);
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)));
172 switch (RSA_check_key(rsa))
175 WLog_ERR(TAG,
"invalid RSA key");
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)));
191 const BIGNUM* rsa_e =
nullptr;
192 const BIGNUM* rsa_n =
nullptr;
193 const BIGNUM* rsa_d =
nullptr;
195 RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
197 BIGNUM* rsa_e =
nullptr;
198 BIGNUM* rsa_n =
nullptr;
199 BIGNUM* rsa_d =
nullptr;
201 if (!EVP_PKEY_get_bn_param(key->evp, OSSL_PKEY_PARAM_RSA_N, &rsa_n))
203 if (!EVP_PKEY_get_bn_param(key->evp, OSSL_PKEY_PARAM_RSA_E, &rsa_e))
205 if (!EVP_PKEY_get_bn_param(key->evp, OSSL_PKEY_PARAM_RSA_D, &rsa_d))
208 if (BN_num_bytes(rsa_e) > 4)
210 WLog_ERR(TAG,
"RSA public exponent too large");
214 if (!read_bignum(&key->PrivateExponent, &key->PrivateExponentLength, rsa_d, TRUE))
217 if (!cert_info_create(&key->cert, rsa_n, rsa_e))
221#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
231rdpPrivateKey* freerdp_key_new_from_pem(
const char* pem)
233 return freerdp_key_new_from_pem_enc(pem,
nullptr);
236rdpPrivateKey* freerdp_key_new_from_pem_enc(
const char* pem,
const char* password)
238 rdpPrivateKey* key = freerdp_key_new();
241 key->evp = evp_pkey_utils_from_pem(pem, strlen(pem), FALSE, password);
244 if (!key_read_private(key))
248 freerdp_key_free(key);
252rdpPrivateKey* freerdp_key_new_from_file(
const char* keyfile)
254 return freerdp_key_new_from_file_enc(keyfile,
nullptr);
257rdpPrivateKey* freerdp_key_new_from_file_enc(
const char* keyfile,
const char* password)
259 rdpPrivateKey* key = freerdp_key_new();
260 if (!key || !keyfile)
263 key->evp = evp_pkey_utils_from_pem(keyfile, strlen(keyfile), TRUE, password);
266 if (!key_read_private(key))
270 freerdp_key_free(key);
274rdpPrivateKey* freerdp_key_new(
void)
276 return calloc(1,
sizeof(rdpPrivateKey));
279rdpPrivateKey* freerdp_key_clone(
const rdpPrivateKey* key)
284 rdpPrivateKey* _key = (rdpPrivateKey*)calloc(1,
sizeof(rdpPrivateKey));
291 _key->evp = key->evp;
294 EVP_PKEY_up_ref(_key->evp);
297 if (key->PrivateExponent)
299 _key->PrivateExponent = (BYTE*)malloc(key->PrivateExponentLength);
301 if (!_key->PrivateExponent)
304 CopyMemory(_key->PrivateExponent, key->PrivateExponent, key->PrivateExponentLength);
305 _key->PrivateExponentLength = key->PrivateExponentLength;
308 if (!cert_info_clone(&_key->cert, &key->cert))
313 WINPR_PRAGMA_DIAG_PUSH
314 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
315 freerdp_key_free(_key);
316 WINPR_PRAGMA_DIAG_POP
320void freerdp_key_free(rdpPrivateKey* key)
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);
333const rdpCertInfo* freerdp_key_get_info(
const rdpPrivateKey* key)
336 if (!freerdp_key_is_rsa(key))
341const BYTE* freerdp_key_get_exponent(
const rdpPrivateKey* key,
size_t* plength)
344 if (!freerdp_key_is_rsa(key))
352 *plength = key->PrivateExponentLength;
353 return key->PrivateExponent;
356EVP_PKEY* freerdp_key_get_evp_pkey(
const rdpPrivateKey* key)
360 EVP_PKEY* evp = key->evp;
362 EVP_PKEY_up_ref(evp);
366BOOL freerdp_key_is_rsa(
const rdpPrivateKey* key)
369 if (key == priv_key_tssk)
372 WINPR_ASSERT(key->evp);
373 return (EVP_PKEY_id(key->evp) == EVP_PKEY_RSA);
376size_t freerdp_key_get_bits(
const rdpPrivateKey* key)
379#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
380 RSA* rsa = evp_pkey_to_rsa(key);
387 rc = EVP_PKEY_get_bits(key->evp);
390 return WINPR_ASSERTING_INT_CAST(
size_t, rc);
393BOOL freerdp_key_generate(rdpPrivateKey* key,
const char* type,
size_t count, ...)
399 WLog_ERR(TAG,
"Invalid argument type=%s", type);
402 if (strncmp(
"RSA", type, 4) != 0)
404 WLog_ERR(TAG,
"Argument type=%s is currently not supported, aborting", type);
409 WLog_ERR(TAG,
"Argument type=%s requires count=1, got %" PRIuz
", aborting", type, count);
412 va_list ap = WINPR_C_ARRAY_INIT;
414 const int key_length = va_arg(ap,
int);
417#if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
419#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
420 rsa = RSA_generate_key(key_length, RSA_F4,
nullptr,
nullptr);
423 BIGNUM* bn = BN_secure_new();
436 BN_set_word(bn, RSA_F4);
437 const int res = RSA_generate_key_ex(rsa, key_length, bn,
nullptr);
445 EVP_PKEY_free(key->evp);
446 key->evp = EVP_PKEY_new();
448 if (!EVP_PKEY_assign_RSA(key->evp, rsa))
450 EVP_PKEY_free(key->evp);
458 EVP_PKEY_CTX* pctx = EVP_PKEY_CTX_new_from_name(
nullptr, type,
nullptr);
462 if (EVP_PKEY_keygen_init(pctx) != 1)
465 if (EVP_PKEY_CTX_set_rsa_keygen_bits(pctx, key_length) != 1)
468 EVP_PKEY_free(key->evp);
471 if (EVP_PKEY_generate(pctx, &key->evp) != 1)
476 EVP_PKEY_CTX_free(pctx);
481BYTE* freerdp_key_get_param(
const rdpPrivateKey* key,
enum FREERDP_KEY_PARAM param,
size_t* plength)
486 WINPR_ASSERT(plength);
490 BIGNUM* bn =
nullptr;
491#if OPENSSL_VERSION_NUMBER >= 0x30000000L
493 const char* pk =
nullptr;
496 case FREERDP_KEY_PARAM_RSA_D:
497 pk = OSSL_PKEY_PARAM_RSA_D;
499 case FREERDP_KEY_PARAM_RSA_E:
500 pk = OSSL_PKEY_PARAM_RSA_E;
502 case FREERDP_KEY_PARAM_RSA_N:
503 pk = OSSL_PKEY_PARAM_RSA_N;
509 if (!EVP_PKEY_get_bn_param(key->evp, pk, &bn))
513 const RSA* rsa = EVP_PKEY_get0_RSA(key->evp);
517 const BIGNUM* cbn =
nullptr;
520 case FREERDP_KEY_PARAM_RSA_D:
521#if OPENSSL_VERSION_NUMBER >= 0x10101007L
522 cbn = RSA_get0_d(rsa);
525 case FREERDP_KEY_PARAM_RSA_E:
526#if OPENSSL_VERSION_NUMBER >= 0x10101007L
527 cbn = RSA_get0_e(rsa);
530 case FREERDP_KEY_PARAM_RSA_N:
531#if OPENSSL_VERSION_NUMBER >= 0x10101007L
532 cbn = RSA_get0_n(rsa);
546 const int length = BN_num_bytes(bn);
551 const size_t alloc_size = (size_t)length + 1ull;
552 buf = calloc(alloc_size,
sizeof(BYTE));
559 const int bnlen = BN_bn2bin(bn, buf);
566 *plength = WINPR_ASSERTING_INT_CAST(
size_t, length);
574WINPR_DIGEST_CTX* freerdp_key_digest_sign(rdpPrivateKey* key, WINPR_MD_TYPE digest)
576 WINPR_DIGEST_CTX* md_ctx = winpr_Digest_New();
580 if (!winpr_DigestSign_Init(md_ctx, digest, key->evp))
582 winpr_Digest_Free(md_ctx);
588static BOOL bio_read_pem(BIO* bio,
char** ppem,
size_t* plength)
595 const size_t blocksize = 2048;
597 size_t length = blocksize;
604 while (offset < length)
606 char* tmp = realloc(pem, length + 1);
613 const int status = BIO_read(bio, &pem[offset], (
int)(length - offset));
616 WLog_ERR(TAG,
"failed to read certificate");
623 offset += (size_t)status;
624 if (length - offset > 0)
631 if (offset >= length)
646char* freerdp_key_get_pem(
const rdpPrivateKey* key,
size_t* plen,
const char* password)
657 BIO* bio = BIO_new(BIO_s_mem());
661 WLog_ERR(TAG,
"BIO_new() failure");
667 const EVP_CIPHER* enc =
nullptr;
669 enc = EVP_aes_256_xts();
671 const int status = PEM_write_bio_PrivateKey(bio, key->evp, enc,
nullptr, 0,
nullptr,
672 WINPR_CAST_CONST_PTR_AWAY(password,
void*));
675 WLog_ERR(TAG,
"PEM_write_bio_PrivateKey failure: %d", status);
679 (void)bio_read_pem(bio, &pem, plen);