25 #include <freerdp/config.h>
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>
37 #include <openssl/pem.h>
38 #include <openssl/rsa.h>
39 #include <openssl/bn.h>
41 #include "privatekey.h"
42 #include "cert_common.h"
44 #include <freerdp/crypto/privatekey.h>
46 #include <openssl/evp.h>
48 #if defined(OPENSSL_VERSION_MAJOR) && (OPENSSL_VERSION_MAJOR >= 3)
49 #include <openssl/core_names.h>
52 #include "x509_utils.h"
54 #include "opensslcompat.h"
56 #define TAG FREERDP_TAG("crypto")
58 struct rdp_private_key
63 BYTE* PrivateExponent;
64 DWORD PrivateExponentLength;
72 static BYTE tssk_modulus[] = { 0x3d, 0x3a, 0x5e, 0xbd, 0x72, 0x43, 0x3e, 0xc9, 0x4d, 0xbb, 0xc1,
73 0x1e, 0x4a, 0xba, 0x5f, 0xcb, 0x3e, 0x88, 0x20, 0x87, 0xef, 0xf5,
74 0xc1, 0xe2, 0xd7, 0xb7, 0x6b, 0x9a, 0xf2, 0x52, 0x45, 0x95, 0xce,
75 0x63, 0x65, 0x6b, 0x58, 0x3a, 0xfe, 0xef, 0x7c, 0xe7, 0xbf, 0xfe,
76 0x3d, 0xf6, 0x5c, 0x7d, 0x6c, 0x5e, 0x06, 0x09, 0x1a, 0xf5, 0x61,
77 0xbb, 0x20, 0x93, 0x09, 0x5f, 0x05, 0x6d, 0xea, 0x87 };
79 static BYTE tssk_privateExponent[] = {
80 0x87, 0xa7, 0x19, 0x32, 0xda, 0x11, 0x87, 0x55, 0x58, 0x00, 0x16, 0x16, 0x25, 0x65, 0x68, 0xf8,
81 0x24, 0x3e, 0xe6, 0xfa, 0xe9, 0x67, 0x49, 0x94, 0xcf, 0x92, 0xcc, 0x33, 0x99, 0xe8, 0x08, 0x60,
82 0x17, 0x9a, 0x12, 0x9f, 0x24, 0xdd, 0xb1, 0x24, 0x99, 0xc7, 0x3a, 0xb8, 0x0a, 0x7b, 0x0d, 0xdd,
83 0x35, 0x07, 0x79, 0x17, 0x0b, 0x51, 0x9b, 0xb3, 0xc7, 0x10, 0x01, 0x13, 0xe7, 0x3f, 0xf3, 0x5f
86 static const rdpPrivateKey tssk = { .PrivateExponent = tssk_privateExponent,
87 .PrivateExponentLength =
sizeof(tssk_privateExponent),
88 .cert = { .Modulus = tssk_modulus,
89 .ModulusLength =
sizeof(tssk_modulus) } };
90 const rdpPrivateKey* priv_key_tssk = &tssk;
92 #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
93 static RSA* evp_pkey_to_rsa(
const rdpPrivateKey* key)
95 if (!freerdp_key_is_rsa(key))
97 WLog_WARN(TAG,
"Key is no RSA key");
103 #
if defined(LIBRESSL_VERSION_NUMBER)
111 const int rc = PEM_write_bio_PrivateKey(bio, key->evp, NULL, NULL, 0, NULL, NULL);
114 rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL);
121 static EVP_PKEY* evp_pkey_utils_from_pem(
const char* data,
size_t len, BOOL fromFile)
123 EVP_PKEY* evp = NULL;
126 bio = BIO_new_file(data,
"rb");
131 bio = BIO_new_mem_buf(data, (
int)len);
136 WLog_ERR(TAG,
"BIO_new failed for private key");
140 evp = PEM_read_bio_PrivateKey(bio, NULL, NULL, 0);
143 WLog_ERR(TAG,
"PEM_read_bio_PrivateKey returned NULL [input length %" PRIuz
"]", len);
148 static BOOL key_read_private(rdpPrivateKey* key)
153 WINPR_ASSERT(key->evp);
156 if (!freerdp_key_is_rsa(key))
159 #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
160 RSA* rsa = evp_pkey_to_rsa(key);
163 char ebuffer[256] = { 0 };
164 WLog_ERR(TAG,
"unable to load RSA key: %s.",
165 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)));
169 switch (RSA_check_key(rsa))
172 WLog_ERR(TAG,
"invalid RSA key");
181 char ebuffer[256] = { 0 };
182 WLog_ERR(TAG,
"unexpected error when checking RSA key: %s.",
183 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)));
188 const BIGNUM* rsa_e = NULL;
189 const BIGNUM* rsa_n = NULL;
190 const BIGNUM* rsa_d = NULL;
192 RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
194 BIGNUM* rsa_e = NULL;
195 BIGNUM* rsa_n = NULL;
196 BIGNUM* rsa_d = NULL;
198 if (!EVP_PKEY_get_bn_param(key->evp, OSSL_PKEY_PARAM_RSA_N, &rsa_n))
200 if (!EVP_PKEY_get_bn_param(key->evp, OSSL_PKEY_PARAM_RSA_E, &rsa_e))
202 if (!EVP_PKEY_get_bn_param(key->evp, OSSL_PKEY_PARAM_RSA_D, &rsa_d))
205 if (BN_num_bytes(rsa_e) > 4)
207 WLog_ERR(TAG,
"RSA public exponent too large");
211 if (!read_bignum(&key->PrivateExponent, &key->PrivateExponentLength, rsa_d, TRUE))
214 if (!cert_info_create(&key->cert, rsa_n, rsa_e))
218 #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
228 rdpPrivateKey* freerdp_key_new_from_pem(
const char* pem)
230 rdpPrivateKey* key = freerdp_key_new();
233 key->evp = evp_pkey_utils_from_pem(pem, strlen(pem), FALSE);
236 if (!key_read_private(key))
240 freerdp_key_free(key);
244 rdpPrivateKey* freerdp_key_new_from_file(
const char* keyfile)
247 rdpPrivateKey* key = freerdp_key_new();
248 if (!key || !keyfile)
251 key->evp = evp_pkey_utils_from_pem(keyfile, strlen(keyfile), TRUE);
254 if (!key_read_private(key))
258 freerdp_key_free(key);
262 rdpPrivateKey* freerdp_key_new(
void)
264 return calloc(1,
sizeof(rdpPrivateKey));
267 rdpPrivateKey* freerdp_key_clone(
const rdpPrivateKey* key)
272 rdpPrivateKey* _key = (rdpPrivateKey*)calloc(1,
sizeof(rdpPrivateKey));
279 _key->evp = key->evp;
282 EVP_PKEY_up_ref(_key->evp);
285 if (key->PrivateExponent)
287 _key->PrivateExponent = (BYTE*)malloc(key->PrivateExponentLength);
289 if (!_key->PrivateExponent)
292 CopyMemory(_key->PrivateExponent, key->PrivateExponent, key->PrivateExponentLength);
293 _key->PrivateExponentLength = key->PrivateExponentLength;
296 if (!cert_info_clone(&_key->cert, &key->cert))
301 WINPR_PRAGMA_DIAG_PUSH
302 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
303 freerdp_key_free(_key);
304 WINPR_PRAGMA_DIAG_POP
308 void freerdp_key_free(rdpPrivateKey* key)
313 EVP_PKEY_free(key->evp);
314 if (key->PrivateExponent)
315 memset(key->PrivateExponent, 0, key->PrivateExponentLength);
316 free(key->PrivateExponent);
317 cert_info_free(&key->cert);
321 const rdpCertInfo* freerdp_key_get_info(
const rdpPrivateKey* key)
324 if (!freerdp_key_is_rsa(key))
329 const BYTE* freerdp_key_get_exponent(
const rdpPrivateKey* key,
size_t* plength)
332 if (!freerdp_key_is_rsa(key))
340 *plength = key->PrivateExponentLength;
341 return key->PrivateExponent;
344 EVP_PKEY* freerdp_key_get_evp_pkey(
const rdpPrivateKey* key)
348 EVP_PKEY* evp = key->evp;
350 EVP_PKEY_up_ref(evp);
354 BOOL freerdp_key_is_rsa(
const rdpPrivateKey* key)
357 if (key == priv_key_tssk)
360 WINPR_ASSERT(key->evp);
361 return (EVP_PKEY_id(key->evp) == EVP_PKEY_RSA);
364 size_t freerdp_key_get_bits(
const rdpPrivateKey* key)
367 #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
368 RSA* rsa = evp_pkey_to_rsa(key);
375 rc = EVP_PKEY_get_bits(key->evp);
381 BOOL freerdp_key_generate(rdpPrivateKey* key,
size_t key_length)
385 #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
387 #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
388 rsa = RSA_generate_key(key_length, RSA_F4, NULL, NULL);
391 BIGNUM* bn = BN_secure_new();
404 BN_set_word(bn, RSA_F4);
405 const int res = RSA_generate_key_ex(rsa, key_length, bn, NULL);
413 EVP_PKEY_free(key->evp);
414 key->evp = EVP_PKEY_new();
416 if (!EVP_PKEY_assign_RSA(key->evp, rsa))
418 EVP_PKEY_free(key->evp);
426 EVP_PKEY_CTX* pctx = EVP_PKEY_CTX_new_from_name(NULL,
"RSA", NULL);
430 if (EVP_PKEY_keygen_init(pctx) != 1)
433 if (key_length > INT_MAX)
436 if (EVP_PKEY_CTX_set_rsa_keygen_bits(pctx, (
int)key_length) != 1)
439 EVP_PKEY_free(key->evp);
442 if (EVP_PKEY_generate(pctx, &key->evp) != 1)
447 EVP_PKEY_CTX_free(pctx);
452 BYTE* freerdp_key_get_param(
const rdpPrivateKey* key,
enum FREERDP_KEY_PARAM param,
size_t* plength)
457 WINPR_ASSERT(plength);
462 #if OPENSSL_VERSION_NUMBER >= 0x30000000L
464 const char* pk = NULL;
467 case FREERDP_KEY_PARAM_RSA_D:
468 pk = OSSL_PKEY_PARAM_RSA_D;
470 case FREERDP_KEY_PARAM_RSA_E:
471 pk = OSSL_PKEY_PARAM_RSA_E;
473 case FREERDP_KEY_PARAM_RSA_N:
474 pk = OSSL_PKEY_PARAM_RSA_N;
480 if (!EVP_PKEY_get_bn_param(key->evp, pk, &bn))
484 const RSA* rsa = EVP_PKEY_get0_RSA(key->evp);
488 const BIGNUM* cbn = NULL;
491 case FREERDP_KEY_PARAM_RSA_D:
492 #if OPENSSL_VERSION_NUMBER >= 0x10101007L
493 cbn = RSA_get0_d(rsa);
496 case FREERDP_KEY_PARAM_RSA_E:
497 #if OPENSSL_VERSION_NUMBER >= 0x10101007L
498 cbn = RSA_get0_e(rsa);
501 case FREERDP_KEY_PARAM_RSA_N:
502 #if OPENSSL_VERSION_NUMBER >= 0x10101007L
503 cbn = RSA_get0_n(rsa);
517 const int length = BN_num_bytes(bn);
521 const size_t alloc_size = (size_t)length + 1ull;
522 buf = calloc(alloc_size,
sizeof(BYTE));
526 const int bnlen = BN_bn2bin(bn, buf);
540 WINPR_DIGEST_CTX* freerdp_key_digest_sign(rdpPrivateKey* key, WINPR_MD_TYPE digest)
542 WINPR_DIGEST_CTX* md_ctx = winpr_Digest_New();
546 if (!winpr_DigestSign_Init(md_ctx, digest, key->evp))
548 winpr_Digest_Free(md_ctx);