summaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorRichard Levitte <levitte@openssl.org>2019-10-15 21:31:45 +0200
committerRichard Levitte <levitte@openssl.org>2019-10-17 18:07:45 +0200
commit29be60235b9bf86bb38f28349b405bb112250d4b (patch)
tree1a59f70cac8a5ad7b628a01d963bba85dfd4daf9 /crypto
parentc3a4fa4cb849ba300d4691154c0c12ac19d24710 (diff)
New RSA keymgmt implementation to handle import / export of RSA keys
Reviewed-by: Matt Caswell <matt@openssl.org> (Merged from https://github.com/openssl/openssl/pull/10190)
Diffstat (limited to 'crypto')
-rw-r--r--crypto/rsa/rsa_ameth.c127
-rw-r--r--crypto/rsa/rsa_gen.c2
-rw-r--r--crypto/rsa/rsa_lib.c4
-rw-r--r--crypto/rsa/rsa_local.h2
-rw-r--r--crypto/rsa/rsa_sp800_56b_gen.c2
-rw-r--r--crypto/rsa/rsa_x931g.c2
6 files changed, 137 insertions, 2 deletions
diff --git a/crypto/rsa/rsa_ameth.c b/crypto/rsa/rsa_ameth.c
index 69e7c5ea1a..d2f976f681 100644
--- a/crypto/rsa/rsa_ameth.c
+++ b/crypto/rsa/rsa_ameth.c
@@ -13,8 +13,11 @@
#include <openssl/x509.h>
#include <openssl/bn.h>
#include <openssl/cms.h>
+#include <openssl/core_names.h>
+#include "internal/param_build.h"
#include "crypto/asn1.h"
#include "crypto/evp.h"
+#include "crypto/rsa.h"
#include "rsa_local.h"
#ifndef OPENSSL_NO_CMS
@@ -1045,6 +1048,114 @@ static int rsa_pkey_check(const EVP_PKEY *pkey)
return RSA_check_key_ex(pkey->pkey.rsa, NULL);
}
+static size_t rsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
+{
+ return pkey->pkey.rsa->dirty_cnt;
+}
+
+DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
+
+static void *rsa_pkey_export_to(const EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
+ int want_domainparams)
+{
+ RSA *rsa = pk->pkey.rsa;
+ OSSL_PARAM_BLD tmpl;
+ const BIGNUM *n = RSA_get0_n(rsa), *e = RSA_get0_e(rsa);
+ const BIGNUM *d = RSA_get0_d(rsa);
+ STACK_OF(BIGNUM_const) *primes = NULL, *exps = NULL, *coeffs = NULL;
+ int numprimes = 0, numexps = 0, numcoeffs = 0;
+ OSSL_PARAM *params = NULL;
+ void *provkey = NULL;
+
+ /*
+ * There are no domain parameters for RSA keys, or rather, they are
+ * included in the key data itself.
+ */
+ if (want_domainparams)
+ goto err;
+
+ /* Get all the primes and CRT params */
+ if ((primes = sk_BIGNUM_const_new_null()) == NULL
+ || (exps = sk_BIGNUM_const_new_null()) == NULL
+ || (coeffs = sk_BIGNUM_const_new_null()) == NULL)
+ goto err;
+
+ if (!rsa_get0_all_params(rsa, primes, exps, coeffs))
+ goto err;
+
+ /* Public parameters must always be present */
+ if (n == NULL || e == NULL)
+ goto err;
+
+ if (d != NULL) {
+ /* It's a private key, so we should have everything else too */
+ numprimes = sk_BIGNUM_const_num(primes);
+ numexps = sk_BIGNUM_const_num(exps);
+ numcoeffs = sk_BIGNUM_const_num(coeffs);
+
+ if (numprimes < 2 || numexps < 2 || numcoeffs < 1)
+ goto err;
+
+ /*
+ * assert that an OSSL_PARAM_BLD has enough space.
+ * (the current 10 places doesn't have space for multi-primes)
+ */
+ if (!ossl_assert(/* n, e */ 2 + /* d */ 1 + /* numprimes */ 1
+ + numprimes + numexps + numcoeffs
+ <= OSSL_PARAM_BLD_MAX))
+ goto err;
+ }
+
+ ossl_param_bld_init(&tmpl);
+ if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_RSA_N, n)
+ || !ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_RSA_E, e))
+ goto err;
+
+ if (d != NULL) {
+ int i;
+
+ if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_RSA_D, d))
+ goto err;
+
+ for (i = 0; i < numprimes; i++) {
+ const BIGNUM *num = sk_BIGNUM_const_value(primes, i);
+
+ if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_RSA_FACTOR,
+ num))
+ goto err;
+ }
+
+ for (i = 0; i < numexps; i++) {
+ const BIGNUM *num = sk_BIGNUM_const_value(exps, i);
+
+ if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_RSA_EXPONENT,
+ num))
+ goto err;
+ }
+
+ for (i = 0; i < numcoeffs; i++) {
+ const BIGNUM *num = sk_BIGNUM_const_value(coeffs, i);
+
+ if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_RSA_COEFFICIENT,
+ num))
+ goto err;
+ }
+ }
+
+ if ((params = ossl_param_bld_to_param(&tmpl)) == NULL)
+ goto err;
+
+ /* We export, the provider imports */
+ provkey = evp_keymgmt_importkey(keymgmt, params);
+
+ err:
+ sk_BIGNUM_const_free(primes);
+ sk_BIGNUM_const_free(exps);
+ sk_BIGNUM_const_free(coeffs);
+ ossl_param_bld_free(params);
+ return provkey;
+}
+
const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[2] = {
{
EVP_PKEY_RSA,
@@ -1077,7 +1188,13 @@ const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[2] = {
rsa_item_verify,
rsa_item_sign,
rsa_sig_info_set,
- rsa_pkey_check
+ rsa_pkey_check,
+
+ 0, 0,
+ 0, 0, 0, 0,
+
+ rsa_pkey_dirty_cnt,
+ rsa_pkey_export_to
},
{
@@ -1116,5 +1233,11 @@ const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth = {
rsa_item_verify,
rsa_item_sign,
0,
- rsa_pkey_check
+ rsa_pkey_check,
+
+ 0, 0,
+ 0, 0, 0, 0,
+
+ rsa_pkey_dirty_cnt,
+ rsa_pkey_export_to
};
diff --git a/crypto/rsa/rsa_gen.c b/crypto/rsa/rsa_gen.c
index c87b709722..cb2abff6a1 100644
--- a/crypto/rsa/rsa_gen.c
+++ b/crypto/rsa/rsa_gen.c
@@ -108,6 +108,8 @@ static int rsa_builtin_keygen(RSA *rsa, int bits, int primes, BIGNUM *e_value,
for (i = 0; i < primes; i++)
bitsr[i] = (i < rmd) ? quo + 1 : quo;
+ rsa->dirty_cnt++;
+
/* We need the RSA components non-NULL */
if (!rsa->n && ((rsa->n = BN_new()) == NULL))
goto err;
diff --git a/crypto/rsa/rsa_lib.c b/crypto/rsa/rsa_lib.c
index 17ff7e7686..353b9d8725 100644
--- a/crypto/rsa/rsa_lib.c
+++ b/crypto/rsa/rsa_lib.c
@@ -328,6 +328,7 @@ int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
r->d = d;
BN_set_flags(r->d, BN_FLG_CONSTTIME);
}
+ r->dirty_cnt++;
return 1;
}
@@ -351,6 +352,7 @@ int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
r->q = q;
BN_set_flags(r->q, BN_FLG_CONSTTIME);
}
+ r->dirty_cnt++;
return 1;
}
@@ -380,6 +382,7 @@ int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
r->iqmp = iqmp;
BN_set_flags(r->iqmp, BN_FLG_CONSTTIME);
}
+ r->dirty_cnt++;
return 1;
}
@@ -444,6 +447,7 @@ int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
}
r->version = RSA_ASN1_VERSION_MULTI;
+ r->dirty_cnt++;
return 1;
err:
diff --git a/crypto/rsa/rsa_local.h b/crypto/rsa/rsa_local.h
index 204fde29fa..ae71567f7a 100644
--- a/crypto/rsa/rsa_local.h
+++ b/crypto/rsa/rsa_local.h
@@ -66,6 +66,8 @@ struct rsa_st {
BN_BLINDING *blinding;
BN_BLINDING *mt_blinding;
CRYPTO_RWLOCK *lock;
+
+ int dirty_cnt;
};
struct rsa_meth_st {
diff --git a/crypto/rsa/rsa_sp800_56b_gen.c b/crypto/rsa/rsa_sp800_56b_gen.c
index c22b10cfbd..5474aaa4b5 100644
--- a/crypto/rsa/rsa_sp800_56b_gen.c
+++ b/crypto/rsa/rsa_sp800_56b_gen.c
@@ -118,6 +118,7 @@ int rsa_fips186_4_gen_prob_primes(RSA *rsa, BIGNUM *p1, BIGNUM *p2,
continue;
break; /* successfully finished */
}
+ rsa->dirty_cnt++;
ret = 1;
err:
/* Zeroize any internally generated values that are not returned */
@@ -239,6 +240,7 @@ int rsa_sp800_56b_derive_params_from_pq(RSA *rsa, int nbits,
|| BN_mod_inverse(rsa->iqmp, rsa->q, rsa->p, ctx) == NULL)
goto err;
+ rsa->dirty_cnt++;
ret = 1;
err:
if (ret != 1) {
diff --git a/crypto/rsa/rsa_x931g.c b/crypto/rsa/rsa_x931g.c
index 3798d02b55..1f6042a3d2 100644
--- a/crypto/rsa/rsa_x931g.c
+++ b/crypto/rsa/rsa_x931g.c
@@ -131,6 +131,7 @@ int RSA_X931_derive_ex(RSA *rsa, BIGNUM *p1, BIGNUM *p2, BIGNUM *q1,
if (rsa->iqmp == NULL)
goto err;
+ rsa->dirty_cnt++;
ret = 1;
err:
BN_CTX_end(ctx);
@@ -184,6 +185,7 @@ int RSA_X931_generate_key_ex(RSA *rsa, int bits, const BIGNUM *e,
NULL, NULL, NULL, NULL, NULL, NULL, e, cb))
goto error;
+ rsa->dirty_cnt++;
ok = 1;
error: