diff options
author | Shane Lontis <shane.lontis@oracle.com> | 2020-04-01 15:51:18 +1000 |
---|---|---|
committer | Shane Lontis <shane.lontis@oracle.com> | 2020-04-01 15:51:18 +1000 |
commit | 96ebe52e897dea29664683e138877fb5eb995e4d (patch) | |
tree | b32e8fa99b2b4eb53e214b7fa196c8ec8d465777 /crypto/rsa | |
parent | f4c88073091592b1ff92ba12c894488ff7d03ece (diff) |
Add EVP_PKEY_gettable_params support for accessing EVP_PKEY key data fields
Currently only RSA, EC and ECX are supported (DH and DSA need to be added to the keygen
PR's seperately because the fields supported have changed significantly).
The API's require the keys to be provider based.
Made the keymanagement export and get_params functions share the same code by supplying
support functions that work for both a OSSL_PARAM_BLD as well as a OSSL_PARAM[].
This approach means that complex code is not required to build an
empty OSSL_PARAM[] with the correct sized fields before then doing a second
pass to populate the array.
The RSA factor arrays have been changed to use unique key names to simplify the interface
needed by the user.
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/11365)
Diffstat (limited to 'crypto/rsa')
-rw-r--r-- | crypto/rsa/build.info | 3 | ||||
-rw-r--r-- | crypto/rsa/rsa_ameth.c | 17 | ||||
-rw-r--r-- | crypto/rsa/rsa_backend.c | 22 | ||||
-rw-r--r-- | crypto/rsa/rsa_mp_names.c | 76 |
4 files changed, 98 insertions, 20 deletions
diff --git a/crypto/rsa/build.info b/crypto/rsa/build.info index c1d1a3769b..7921202739 100644 --- a/crypto/rsa/build.info +++ b/crypto/rsa/build.info @@ -2,7 +2,8 @@ LIBS=../../libcrypto $COMMON=rsa_ossl.c rsa_gen.c rsa_lib.c rsa_sign.c rsa_aid.c rsa_pk1.c \ rsa_none.c rsa_oaep.c rsa_chk.c rsa_pss.c rsa_x931.c rsa_crpt.c \ - rsa_x931g.c rsa_sp800_56b_gen.c rsa_sp800_56b_check.c rsa_backend.c + rsa_x931g.c rsa_sp800_56b_gen.c rsa_sp800_56b_check.c rsa_backend.c \ + rsa_mp_names.c SOURCE[../../libcrypto]=$COMMON\ rsa_saos.c rsa_err.c rsa_asn1.c rsa_depr.c rsa_ameth.c rsa_prn.c \ diff --git a/crypto/rsa/rsa_ameth.c b/crypto/rsa/rsa_ameth.c index ec8df4a718..fb378ae039 100644 --- a/crypto/rsa/rsa_ameth.c +++ b/crypto/rsa/rsa_ameth.c @@ -20,7 +20,7 @@ #include <openssl/bn.h> #include <openssl/cms.h> #include <openssl/core_names.h> -#include "openssl/param_build.h" +#include <openssl/param_build.h> #include "crypto/asn1.h" #include "crypto/evp.h" #include "crypto/rsa.h" @@ -1142,27 +1142,24 @@ static int rsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata, goto err; selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY; - for (i = 0; i < numprimes; i++) { + for (i = 0; i < numprimes && rsa_mp_factor_names[i] != NULL; i++) { const BIGNUM *num = sk_BIGNUM_const_value(primes, i); - if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_FACTOR, - num)) + if (!OSSL_PARAM_BLD_push_BN(tmpl, rsa_mp_factor_names[i], num)) goto err; } - for (i = 0; i < numexps; i++) { + for (i = 0; i < numexps && rsa_mp_exp_names[i] != NULL; i++) { const BIGNUM *num = sk_BIGNUM_const_value(exps, i); - if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_EXPONENT, - num)) + if (!OSSL_PARAM_BLD_push_BN(tmpl, rsa_mp_exp_names[i], num)) goto err; } - for (i = 0; i < numcoeffs; i++) { + for (i = 0; i < numcoeffs && rsa_mp_coeff_names[i] != NULL; i++) { const BIGNUM *num = sk_BIGNUM_const_value(coeffs, i); - if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_COEFFICIENT, - num)) + if (!OSSL_PARAM_BLD_push_BN(tmpl, rsa_mp_coeff_names[i], num)) goto err; } } diff --git a/crypto/rsa/rsa_backend.c b/crypto/rsa/rsa_backend.c index f68d38cc1a..57a539c051 100644 --- a/crypto/rsa/rsa_backend.c +++ b/crypto/rsa/rsa_backend.c @@ -20,19 +20,23 @@ DEFINE_STACK_OF(BIGNUM) static int collect_numbers(STACK_OF(BIGNUM) *numbers, - const OSSL_PARAM params[], const char *key) + const OSSL_PARAM params[], const char *names[]) { const OSSL_PARAM *p = NULL; + int i; if (numbers == NULL) return 0; - for (p = params; (p = OSSL_PARAM_locate_const(p, key)) != NULL; p++) { - BIGNUM *tmp = NULL; + for (i = 0; names[i] != NULL; i++){ + p = OSSL_PARAM_locate_const(params, names[i]); + if (p != NULL) { + BIGNUM *tmp = NULL; - if (!OSSL_PARAM_get_BN(p, &tmp) - || sk_BIGNUM_push(numbers, tmp) == 0) - return 0; + if (!OSSL_PARAM_get_BN(p, &tmp) + || sk_BIGNUM_push(numbers, tmp) == 0) + return 0; + } } return 1; @@ -65,11 +69,11 @@ int rsa_fromdata(RSA *rsa, const OSSL_PARAM params[]) if (is_private) { if (!collect_numbers(factors = sk_BIGNUM_new_null(), params, - OSSL_PKEY_PARAM_RSA_FACTOR) + rsa_mp_factor_names) || !collect_numbers(exps = sk_BIGNUM_new_null(), params, - OSSL_PKEY_PARAM_RSA_EXPONENT) + rsa_mp_exp_names) || !collect_numbers(coeffs = sk_BIGNUM_new_null(), params, - OSSL_PKEY_PARAM_RSA_COEFFICIENT)) + rsa_mp_coeff_names)) goto err; /* It's ok if this private key just has n, e and d */ diff --git a/crypto/rsa/rsa_mp_names.c b/crypto/rsa/rsa_mp_names.c new file mode 100644 index 0000000000..e69321a4b7 --- /dev/null +++ b/crypto/rsa/rsa_mp_names.c @@ -0,0 +1,76 @@ +/* + * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#include <openssl/core_names.h> +#include "crypto/rsa.h" + +/* + * The following tables are constants used during RSA parameter building + * operations. It is easier to point to one of these fixed strings than have + * to dynamically add and generate the names on the fly. + */ + +/* + * A fixed table of names for the RSA prime factors starting with + * P,Q and up to 8 additional primes. + */ +const char *rsa_mp_factor_names[] = { + OSSL_PKEY_PARAM_RSA_FACTOR1, + OSSL_PKEY_PARAM_RSA_FACTOR2, +#ifndef FIPS_MODE + OSSL_PKEY_PARAM_RSA_FACTOR3, + OSSL_PKEY_PARAM_RSA_FACTOR4, + OSSL_PKEY_PARAM_RSA_FACTOR5, + OSSL_PKEY_PARAM_RSA_FACTOR6, + OSSL_PKEY_PARAM_RSA_FACTOR7, + OSSL_PKEY_PARAM_RSA_FACTOR8, + OSSL_PKEY_PARAM_RSA_FACTOR9, + OSSL_PKEY_PARAM_RSA_FACTOR10, +#endif + NULL +}; + +/* + * A fixed table of names for the RSA exponents starting with + * DP,DQ and up to 8 additional exponents. + */ +const char *rsa_mp_exp_names[] = { + OSSL_PKEY_PARAM_RSA_EXPONENT1, + OSSL_PKEY_PARAM_RSA_EXPONENT2, +#ifndef FIPS_MODE + OSSL_PKEY_PARAM_RSA_EXPONENT3, + OSSL_PKEY_PARAM_RSA_EXPONENT4, + OSSL_PKEY_PARAM_RSA_EXPONENT5, + OSSL_PKEY_PARAM_RSA_EXPONENT6, + OSSL_PKEY_PARAM_RSA_EXPONENT7, + OSSL_PKEY_PARAM_RSA_EXPONENT8, + OSSL_PKEY_PARAM_RSA_EXPONENT9, + OSSL_PKEY_PARAM_RSA_EXPONENT10, +#endif + NULL +}; + +/* + * A fixed table of names for the RSA coefficients starting with + * QINV and up to 8 additional exponents. + */ +const char *rsa_mp_coeff_names[] = { + OSSL_PKEY_PARAM_RSA_COEFFICIENT1, + OSSL_PKEY_PARAM_RSA_COEFFICIENT2, +#ifndef FIPS_MODE + OSSL_PKEY_PARAM_RSA_COEFFICIENT3, + OSSL_PKEY_PARAM_RSA_COEFFICIENT4, + OSSL_PKEY_PARAM_RSA_COEFFICIENT5, + OSSL_PKEY_PARAM_RSA_COEFFICIENT6, + OSSL_PKEY_PARAM_RSA_COEFFICIENT7, + OSSL_PKEY_PARAM_RSA_COEFFICIENT8, + OSSL_PKEY_PARAM_RSA_COEFFICIENT9, +#endif + NULL +}; |