| 1 |
From: Joris Vink <[email protected]> |
| 2 |
Date: Wed, 28 Dec 2022 15:43:56 +0100 |
| 3 |
Subject: Change how we handle EVP_PKEY for privsep. |
| 4 |
|
| 5 |
We used to just call EVP_PKEY_get1_RSA() and set the domain |
| 6 |
and RSA_METHOD on that. |
| 7 |
|
| 8 |
But with OpenSSL 3, the EVP_PKEY_get1_RSA() function returns a cached |
| 9 |
copy of the internal provider struct and any changes we make are not |
| 10 |
reflected back. So we can't use it to set the domain and custom method. |
| 11 |
|
| 12 |
Instead just create our own EVP_PKEY from scratch, coupled with an |
| 13 |
RSA key that contains just n and e from the public key. |
| 14 |
|
| 15 |
Works with both 1.1.x and 3.0.x. |
| 16 |
--- |
| 17 |
src/tls_openssl.c | 70 ++++++++++++++++++++++++++++++++++++++++++------------- |
| 18 |
1 file changed, 54 insertions(+), 16 deletions(-) |
| 19 |
|
| 20 |
diff --git a/src/tls_openssl.c b/src/tls_openssl.c |
| 21 |
index af14f75..05af9c3 100644 |
| 22 |
--- a/src/tls_openssl.c |
| 23 |
+++ b/src/tls_openssl.c |
| 24 |
@@ -48,9 +48,16 @@ |
| 25 |
|
| 26 |
#define TLS_SESSION_ID "kore_tls_sessionid" |
| 27 |
|
| 28 |
+/* Helper for weird API designs (looking at you OpenSSL). */ |
| 29 |
+union deconst { |
| 30 |
+ void *p; |
| 31 |
+ const void *cp; |
| 32 |
+}; |
| 33 |
+ |
| 34 |
static int tls_domain_x509_verify(int, X509_STORE_CTX *); |
| 35 |
static X509 *tls_domain_load_certificate_chain(SSL_CTX *, |
| 36 |
const void *, size_t); |
| 37 |
+static EVP_PKEY *tls_privsep_private_key(EVP_PKEY *, struct kore_domain *); |
| 38 |
|
| 39 |
static int tls_sni_cb(SSL *, int *, void *); |
| 40 |
static void tls_info_callback(const SSL *, int, int); |
| 41 |
@@ -69,9 +76,8 @@ static int tls_keymgr_rsa_finish(RSA *); |
| 42 |
static int tls_keymgr_rsa_privenc(int, const unsigned char *, |
| 43 |
unsigned char *, RSA *, int); |
| 44 |
|
| 45 |
-static RSA_METHOD *keymgr_rsa_meth = NULL; |
| 46 |
- |
| 47 |
static DH *dh_params = NULL; |
| 48 |
+static RSA_METHOD *keymgr_rsa_meth = NULL; |
| 49 |
static int tls_version = KORE_TLS_VERSION_BOTH; |
| 50 |
static char *tls_cipher_list = KORE_DEFAULT_CIPHER_LIST; |
| 51 |
|
| 52 |
@@ -100,14 +106,6 @@ kore_tls_init(void) |
| 53 |
SSL_load_error_strings(); |
| 54 |
ERR_load_crypto_strings(); |
| 55 |
|
| 56 |
- if ((keymgr_rsa_meth = RSA_meth_new("kore RSA keymgr method", |
| 57 |
- RSA_METHOD_FLAG_NO_CHECK)) == NULL) |
| 58 |
- fatal("failed to setup RSA method"); |
| 59 |
- |
| 60 |
- RSA_meth_set_init(keymgr_rsa_meth, tls_keymgr_rsa_init); |
| 61 |
- RSA_meth_set_finish(keymgr_rsa_meth, tls_keymgr_rsa_finish); |
| 62 |
- RSA_meth_set_priv_enc(keymgr_rsa_meth, tls_keymgr_rsa_privenc); |
| 63 |
- |
| 64 |
kore_log(LOG_NOTICE, "TLS backend %s", OPENSSL_VERSION_TEXT); |
| 65 |
#if !defined(TLS1_3_VERSION) |
| 66 |
if (!kore_quiet) { |
| 67 |
@@ -187,6 +185,14 @@ kore_tls_keymgr_init(void) |
| 68 |
if ((meth = RSA_get_default_method()) == NULL) |
| 69 |
fatal("failed to obtain RSA method"); |
| 70 |
|
| 71 |
+ if ((keymgr_rsa_meth = RSA_meth_new("kore RSA keymgr method", |
| 72 |
+ RSA_METHOD_FLAG_NO_CHECK)) == NULL) |
| 73 |
+ fatal("failed to setup RSA method"); |
| 74 |
+ |
| 75 |
+ RSA_meth_set_init(keymgr_rsa_meth, tls_keymgr_rsa_init); |
| 76 |
+ RSA_meth_set_finish(keymgr_rsa_meth, tls_keymgr_rsa_finish); |
| 77 |
+ RSA_meth_set_priv_enc(keymgr_rsa_meth, tls_keymgr_rsa_privenc); |
| 78 |
+ |
| 79 |
RSA_meth_set_pub_enc(keymgr_rsa_meth, RSA_meth_get_pub_enc(meth)); |
| 80 |
RSA_meth_set_pub_dec(keymgr_rsa_meth, RSA_meth_get_pub_dec(meth)); |
| 81 |
RSA_meth_set_bn_mod_exp(keymgr_rsa_meth, RSA_meth_get_bn_mod_exp(meth)); |
| 82 |
@@ -199,9 +205,8 @@ kore_tls_domain_setup(struct kore_domain *dom, int type, |
| 83 |
const void *data, size_t datalen) |
| 84 |
{ |
| 85 |
const u_int8_t *ptr; |
| 86 |
- RSA *rsa; |
| 87 |
- X509 *x509; |
| 88 |
EVP_PKEY *pkey; |
| 89 |
+ X509 *x509; |
| 90 |
STACK_OF(X509_NAME) *certs; |
| 91 |
const SSL_METHOD *method; |
| 92 |
|
| 93 |
@@ -278,10 +283,7 @@ kore_tls_domain_setup(struct kore_domain *dom, int type, |
| 94 |
|
| 95 |
switch (EVP_PKEY_id(pkey)) { |
| 96 |
case EVP_PKEY_RSA: |
| 97 |
- if ((rsa = EVP_PKEY_get1_RSA(pkey)) == NULL) |
| 98 |
- fatalx("no RSA public key present"); |
| 99 |
- RSA_set_app_data(rsa, dom); |
| 100 |
- RSA_set_method(rsa, keymgr_rsa_meth); |
| 101 |
+ pkey = tls_privsep_private_key(pkey, dom); |
| 102 |
break; |
| 103 |
default: |
| 104 |
fatalx("unknown public key in certificate"); |
| 105 |
@@ -825,6 +827,7 @@ static int |
| 106 |
tls_keymgr_rsa_init(RSA *rsa) |
| 107 |
{ |
| 108 |
if (rsa != NULL) { |
| 109 |
+ kore_log(LOG_INFO, "Set the flags on %p", (void *)rsa); |
| 110 |
RSA_set_flags(rsa, RSA_flags(rsa) | |
| 111 |
RSA_FLAG_EXT_PKEY | RSA_METHOD_FLAG_NO_CHECK); |
| 112 |
return (1); |
| 113 |
@@ -1058,6 +1061,41 @@ tls_domain_load_certificate_chain(SSL_CTX *ctx, const void *data, size_t len) |
| 114 |
return (x); |
| 115 |
} |
| 116 |
|
| 117 |
+static EVP_PKEY * |
| 118 |
+tls_privsep_private_key(EVP_PKEY *pub, struct kore_domain *dom) |
| 119 |
+{ |
| 120 |
+ EVP_PKEY *pkey; |
| 121 |
+ RSA *rsa_new; |
| 122 |
+ const RSA *rsa_cur; |
| 123 |
+ const BIGNUM *e_cur, *n_cur; |
| 124 |
+ BIGNUM *e_new, *n_new; |
| 125 |
+ |
| 126 |
+ if ((pkey = EVP_PKEY_new()) == NULL) |
| 127 |
+ fatalx("failed to create new EVP_PKEY data structure"); |
| 128 |
+ |
| 129 |
+ if ((rsa_new = RSA_new()) == NULL) |
| 130 |
+ fatalx("failed to create new RSA data structure"); |
| 131 |
+ |
| 132 |
+ if ((rsa_cur = EVP_PKEY_get0_RSA(pub)) == NULL) |
| 133 |
+ fatalx("no RSA public key present"); |
| 134 |
+ |
| 135 |
+ RSA_get0_key(rsa_cur, &n_cur, &e_cur, NULL); |
| 136 |
+ |
| 137 |
+ if ((n_new = BN_dup(n_cur)) == NULL) |
| 138 |
+ fatalx("BN_dup failed"); |
| 139 |
+ |
| 140 |
+ if ((e_new = BN_dup(e_cur)) == NULL) |
| 141 |
+ fatalx("BN_dup failed"); |
| 142 |
+ |
| 143 |
+ RSA_set_app_data(rsa_new, dom); |
| 144 |
+ RSA_set_method(rsa_new, keymgr_rsa_meth); |
| 145 |
+ RSA_set0_key(rsa_new, n_new, e_new, NULL); |
| 146 |
+ |
| 147 |
+ EVP_PKEY_set1_RSA(pkey, rsa_new); |
| 148 |
+ |
| 149 |
+ return (pkey); |
| 150 |
+} |
| 151 |
+ |
| 152 |
#if defined(KORE_USE_ACME) |
| 153 |
static int |
| 154 |
tls_acme_alpn(SSL *ssl, const unsigned char **out, unsigned char *outlen, |