diff options
-rw-r--r-- | crypto/evp/build.info | 3 | ||||
-rw-r--r-- | crypto/evp/evp_local.h | 4 | ||||
-rw-r--r-- | crypto/evp/keymgmt_lib.c | 23 | ||||
-rw-r--r-- | crypto/evp/keymgmt_meth.c | 24 | ||||
-rw-r--r-- | crypto/evp/pmeth_check.c | 158 | ||||
-rw-r--r-- | crypto/evp/pmeth_gn.c | 71 | ||||
-rw-r--r-- | crypto/evp/pmeth_lib.c | 1 | ||||
-rw-r--r-- | crypto/rsa/rsa_chk.c | 90 | ||||
-rw-r--r-- | crypto/rsa/rsa_sp800_56b_check.c | 8 | ||||
-rw-r--r-- | doc/man3/EVP_PKEY_check.pod | 73 | ||||
-rw-r--r-- | doc/man3/EVP_PKEY_keygen.pod | 26 | ||||
-rw-r--r-- | doc/man7/provider-keymgmt.pod | 26 | ||||
-rw-r--r-- | include/crypto/evp.h | 6 | ||||
-rw-r--r-- | include/crypto/rsa.h | 5 | ||||
-rw-r--r-- | include/openssl/core_numbers.h | 14 | ||||
-rw-r--r-- | include/openssl/evp.h | 2 | ||||
-rw-r--r-- | providers/implementations/keymgmt/rsa_kmgmt.c | 30 | ||||
-rw-r--r-- | test/evp_pkey_provided_test.c | 12 | ||||
-rw-r--r-- | util/libcrypto.num | 2 |
19 files changed, 439 insertions, 139 deletions
diff --git a/crypto/evp/build.info b/crypto/evp/build.info index d3ebac9f4e..7972706f86 100644 --- a/crypto/evp/build.info +++ b/crypto/evp/build.info @@ -1,7 +1,8 @@ LIBS=../../libcrypto $COMMON=digest.c evp_enc.c evp_lib.c evp_fetch.c cmeth_lib.c evp_utils.c \ mac_lib.c mac_meth.c keymgmt_meth.c keymgmt_lib.c kdf_lib.c kdf_meth.c \ - m_sigver.c pmeth_lib.c signature.c p_lib.c pmeth_gn.c exchange.c + m_sigver.c pmeth_lib.c signature.c p_lib.c pmeth_gn.c exchange.c \ + pmeth_check.c SOURCE[../../libcrypto]=$COMMON\ encode.c evp_key.c evp_cnf.c \ diff --git a/crypto/evp/evp_local.h b/crypto/evp/evp_local.h index 894fdf996e..5f34aaeb23 100644 --- a/crypto/evp/evp_local.h +++ b/crypto/evp/evp_local.h @@ -82,6 +82,7 @@ struct evp_keymgmt_st { OSSL_OP_keymgmt_exportdomparam_types_fn *exportdomparam_types; OSSL_OP_keymgmt_get_domparam_params_fn *get_domparam_params; OSSL_OP_keymgmt_gettable_domparam_params_fn *gettable_domparam_params; + OSSL_OP_keymgmt_validate_domparams_fn *validatedomparams; /* Key routines */ OSSL_OP_keymgmt_importkey_fn *importkey; @@ -95,6 +96,9 @@ struct evp_keymgmt_st { OSSL_OP_keymgmt_gettable_key_params_fn *gettable_key_params; OSSL_OP_keymgmt_query_operation_name_fn *query_operation_name; + OSSL_OP_keymgmt_validate_public_fn *validatepublic; + OSSL_OP_keymgmt_validate_private_fn *validateprivate; + OSSL_OP_keymgmt_validate_pairwise_fn *validatepairwise; } /* EVP_KEYMGMT */ ; struct keymgmt_data_st { diff --git a/crypto/evp/keymgmt_lib.c b/crypto/evp/keymgmt_lib.c index 53610d3ef8..1f4b4a104f 100644 --- a/crypto/evp/keymgmt_lib.c +++ b/crypto/evp/keymgmt_lib.c @@ -342,3 +342,26 @@ const OSSL_PARAM *evp_keymgmt_gettable_key_params(const EVP_KEYMGMT *keymgmt) return NULL; return keymgmt->gettable_key_params(); } + +int evp_keymgmt_validate_domparams(const EVP_KEYMGMT *keymgmt, void *provkey) +{ + /* if domainparams are not supported - then pass */ + if (keymgmt->validatedomparams == NULL) + return 1; + return keymgmt->validatedomparams(provkey); +} + +int evp_keymgmt_validate_public(const EVP_KEYMGMT *keymgmt, void *provkey) +{ + return keymgmt->validatepublic(provkey); +} + +int evp_keymgmt_validate_private(const EVP_KEYMGMT *keymgmt, void *provkey) +{ + return keymgmt->validateprivate(provkey); +} + +int evp_keymgmt_validate_pairwise(const EVP_KEYMGMT *keymgmt, void *provkey) +{ + return keymgmt->validatepairwise(provkey); +} diff --git a/crypto/evp/keymgmt_meth.c b/crypto/evp/keymgmt_meth.c index ae1f10e6b3..e9e7f89744 100644 --- a/crypto/evp/keymgmt_meth.c +++ b/crypto/evp/keymgmt_meth.c @@ -144,6 +144,30 @@ static void *keymgmt_from_dispatch(int name_id, keymgmt->query_operation_name = OSSL_get_OP_keymgmt_query_operation_name(fns); break; + case OSSL_FUNC_KEYMGMT_VALIDATE_DOMPARAMS: + if (keymgmt->validatedomparams != NULL) + break; + keymgmt->validatedomparams = + OSSL_get_OP_keymgmt_validate_domparams(fns); + break; + case OSSL_FUNC_KEYMGMT_VALIDATE_PUBLIC: + if (keymgmt->validatepublic != NULL) + break; + keymgmt->validatepublic = + OSSL_get_OP_keymgmt_validate_public(fns); + break; + case OSSL_FUNC_KEYMGMT_VALIDATE_PRIVATE: + if (keymgmt->validateprivate != NULL) + break; + keymgmt->validateprivate = + OSSL_get_OP_keymgmt_validate_private(fns); + break; + case OSSL_FUNC_KEYMGMT_VALIDATE_PAIRWISE: + if (keymgmt->validatepairwise != NULL) + break; + keymgmt->validatepairwise = + OSSL_get_OP_keymgmt_validate_pairwise(fns); + break; } } /* diff --git a/crypto/evp/pmeth_check.c b/crypto/evp/pmeth_check.c new file mode 100644 index 0000000000..19f200a3ce --- /dev/null +++ b/crypto/evp/pmeth_check.c @@ -0,0 +1,158 @@ +/* + * Copyright 2006-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 <stdio.h> +#include <stdlib.h> +#include "internal/cryptlib.h" +#include <openssl/objects.h> +#include <openssl/evp.h> +#include "crypto/bn.h" +#include "crypto/asn1.h" +#include "crypto/evp.h" +#include "evp_local.h" + +int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx) +{ + EVP_PKEY *pkey = ctx->pkey; + void *key; + EVP_KEYMGMT *keymgmt; + + if (pkey == NULL) { + EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK, EVP_R_NO_KEY_SET); + return 0; + } + + keymgmt = pkey->pkeys[0].keymgmt; + key = pkey->pkeys[0].provdata; + + if (key != NULL && keymgmt != NULL) + return evp_keymgmt_validate_public(keymgmt, key); + + /* legacy */ + /* call customized public key check function first */ + if (ctx->pmeth->public_check != NULL) + return ctx->pmeth->public_check(pkey); + + /* use default public key check function in ameth */ + if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL) { + EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK, + EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); + return -2; + } + + return pkey->ameth->pkey_public_check(pkey); +} + +int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx) +{ + EVP_PKEY *pkey = ctx->pkey; + void *key; + EVP_KEYMGMT *keymgmt; + + if (pkey == NULL) { + EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK, EVP_R_NO_KEY_SET); + return 0; + } + + keymgmt = pkey->pkeys[0].keymgmt; + key = pkey->pkeys[0].provdata; + + if (key != NULL && keymgmt != NULL) + return evp_keymgmt_validate_domparams(keymgmt, key); + + /* call customized param check function first */ + if (ctx->pmeth->param_check != NULL) + return ctx->pmeth->param_check(pkey); + + /* legacy */ + /* use default param check function in ameth */ + if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL) { + EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK, + EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); + return -2; + } + + return pkey->ameth->pkey_param_check(pkey); +} + +int EVP_PKEY_private_check(EVP_PKEY_CTX *ctx) +{ + EVP_PKEY *pkey = ctx->pkey; + void *key; + EVP_KEYMGMT *keymgmt; + + if (pkey == NULL) { + EVPerr(0, EVP_R_NO_KEY_SET); + return 0; + } + + keymgmt = pkey->pkeys[0].keymgmt; + key = pkey->pkeys[0].provdata; + + if (key != NULL && keymgmt != NULL) + return evp_keymgmt_validate_private(keymgmt, key); + /* not supported for legacy keys */ + return -2; +} + +int EVP_PKEY_pairwise_check(EVP_PKEY_CTX *ctx) +{ + EVP_PKEY *pkey = ctx->pkey; + void *key; + EVP_KEYMGMT *keymgmt; + + if (pkey == NULL) { + EVPerr(0, EVP_R_NO_KEY_SET); + return 0; + } + + keymgmt = pkey->pkeys[0].keymgmt; + key = pkey->pkeys[0].provdata; + + if (key != NULL && keymgmt != NULL) + return evp_keymgmt_validate_pairwise(keymgmt, key); + /* not supported for legacy keys */ + return -2; +} + +int EVP_PKEY_check(EVP_PKEY_CTX *ctx) +{ + EVP_PKEY *pkey = ctx->pkey; + void *key; + EVP_KEYMGMT *keymgmt; + + if (pkey == NULL) { + EVPerr(EVP_F_EVP_PKEY_CHECK, EVP_R_NO_KEY_SET); + return 0; + } + + keymgmt = pkey->pkeys[0].keymgmt; + key = pkey->pkeys[0].provdata; + + if (key != NULL && keymgmt != NULL) { + return evp_keymgmt_validate_domparams(keymgmt, key) + && evp_keymgmt_validate_public(keymgmt, key) + && evp_keymgmt_validate_private(keymgmt, key) + && evp_keymgmt_validate_pairwise(keymgmt, key); + } + /* legacy */ + /* call customized check function first */ + if (ctx->pmeth->check != NULL) + return ctx->pmeth->check(pkey); + + /* use default check function in ameth */ + if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL) { + EVPerr(EVP_F_EVP_PKEY_CHECK, + EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); + return -2; + } + + return pkey->ameth->pkey_check(pkey); +} + diff --git a/crypto/evp/pmeth_gn.c b/crypto/evp/pmeth_gn.c index 14c5fd4b99..a093337e62 100644 --- a/crypto/evp/pmeth_gn.c +++ b/crypto/evp/pmeth_gn.c @@ -171,75 +171,6 @@ EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e, return mac_key; } -int EVP_PKEY_check(EVP_PKEY_CTX *ctx) -{ - EVP_PKEY *pkey = ctx->pkey; - - if (pkey == NULL) { - EVPerr(EVP_F_EVP_PKEY_CHECK, EVP_R_NO_KEY_SET); - return 0; - } - - /* call customized check function first */ - if (ctx->pmeth->check != NULL) - return ctx->pmeth->check(pkey); - - /* use default check function in ameth */ - if (pkey->ameth == NULL || pkey->ameth->pkey_check == NULL) { - EVPerr(EVP_F_EVP_PKEY_CHECK, - EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); - return -2; - } - - return pkey->ameth->pkey_check(pkey); -} - -int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx) -{ - EVP_PKEY *pkey = ctx->pkey; - - if (pkey == NULL) { - EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK, EVP_R_NO_KEY_SET); - return 0; - } - - /* call customized public key check function first */ - if (ctx->pmeth->public_check != NULL) - return ctx->pmeth->public_check(pkey); - - /* use default public key check function in ameth */ - if (pkey->ameth == NULL || pkey->ameth->pkey_public_check == NULL) { - EVPerr(EVP_F_EVP_PKEY_PUBLIC_CHECK, - EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); - return -2; - } - - return pkey->ameth->pkey_public_check(pkey); -} - -int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx) -{ - EVP_PKEY *pkey = ctx->pkey; - - if (pkey == NULL) { - EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK, EVP_R_NO_KEY_SET); - return 0; - } - - /* call customized param check function first */ - if (ctx->pmeth->param_check != NULL) - return ctx->pmeth->param_check(pkey); - - /* use default param check function in ameth */ - if (pkey->ameth == NULL || pkey->ameth->pkey_param_check == NULL) { - EVPerr(EVP_F_EVP_PKEY_PARAM_CHECK, - EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); - return -2; - } - - return pkey->ameth->pkey_param_check(pkey); -} - #endif /* FIPS_MODE */ /*- All methods below can also be used in FIPS_MODE */ @@ -327,5 +258,3 @@ const OSSL_PARAM *EVP_PKEY_key_fromdata_settable(EVP_PKEY_CTX *ctx) return evp_keymgmt_importdomparam_types(ctx->keymgmt); return NULL; } - - diff --git a/crypto/evp/pmeth_lib.c b/crypto/evp/pmeth_lib.c index 03d6ab4da4..dda9358c4e 100644 --- a/crypto/evp/pmeth_lib.c +++ b/crypto/evp/pmeth_lib.c @@ -379,7 +379,6 @@ EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e) return int_ctx_new(NULL, NULL, e, NULL, NULL, id); } - EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx) { EVP_PKEY_CTX *rctx; diff --git a/crypto/rsa/rsa_chk.c b/crypto/rsa/rsa_chk.c index 9d8049132b..6ba0010c77 100644 --- a/crypto/rsa/rsa_chk.c +++ b/crypto/rsa/rsa_chk.c @@ -1,5 +1,5 @@ /* - * Copyright 1999-2017 The OpenSSL Project Authors. All Rights Reserved. + * Copyright 1999-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 @@ -9,26 +9,12 @@ #include <openssl/bn.h> #include <openssl/err.h> +#include "crypto/rsa.h" #include "rsa_local.h" -int RSA_check_key(const RSA *key) +#ifndef FIPS_MODE +static int rsa_validate_keypair_multiprime(const RSA *key, BN_GENCB *cb) { - return RSA_check_key_ex(key, NULL); -} - -/* - * NOTE: Key validation requires separate checks to be able to be accessed - * individually. These should be visible from the PKEY API.. - * See rsa_sp800_56b_check_public, rsa_sp800_56b_check_private and - * rsa_sp800_56b_check_keypair. - */ -int RSA_check_key_ex(const RSA *key, BN_GENCB *cb) -{ -#ifdef FIPS_MODE - return rsa_sp800_56b_check_public(key) - && rsa_sp800_56b_check_private(key) - && rsa_sp800_56b_check_keypair(key, NULL, -1, RSA_bits(key)); -#else BIGNUM *i, *j, *k, *l, *m; BN_CTX *ctx; int ret = 1, ex_primes = 0, idx; @@ -36,7 +22,7 @@ int RSA_check_key_ex(const RSA *key, BN_GENCB *cb) if (key->p == NULL || key->q == NULL || key->n == NULL || key->e == NULL || key->d == NULL) { - RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_VALUE_MISSING); + RSAerr(0, RSA_R_VALUE_MISSING); return 0; } @@ -45,7 +31,7 @@ int RSA_check_key_ex(const RSA *key, BN_GENCB *cb) ex_primes = sk_RSA_PRIME_INFO_num(key->prime_infos); if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(BN_num_bits(key->n))) { - RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_INVALID_MULTI_PRIME_KEY); + RSAerr(0, RSA_R_INVALID_MULTI_PRIME_KEY); return 0; } } @@ -59,29 +45,29 @@ int RSA_check_key_ex(const RSA *key, BN_GENCB *cb) if (i == NULL || j == NULL || k == NULL || l == NULL || m == NULL || ctx == NULL) { ret = -1; - RSAerr(RSA_F_RSA_CHECK_KEY_EX, ERR_R_MALLOC_FAILURE); + RSAerr(0, ERR_R_MALLOC_FAILURE); goto err; } if (BN_is_one(key->e)) { ret = 0; - RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_BAD_E_VALUE); + RSAerr(0, RSA_R_BAD_E_VALUE); } if (!BN_is_odd(key->e)) { ret = 0; - RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_BAD_E_VALUE); + RSAerr(0, RSA_R_BAD_E_VALUE); } /* p prime? */ if (BN_check_prime(key->p, NULL, cb) != 1) { ret = 0; - RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_P_NOT_PRIME); + RSAerr(0, RSA_R_P_NOT_PRIME); } /* q prime? */ if (BN_check_prime(key->q, NULL, cb) != 1) { ret = 0; - RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_Q_NOT_PRIME); + RSAerr(0, RSA_R_Q_NOT_PRIME); } /* r_i prime? */ @@ -89,7 +75,7 @@ int RSA_check_key_ex(const RSA *key, BN_GENCB *cb) pinfo = sk_RSA_PRIME_INFO_value(key->prime_infos, idx); if (BN_check_prime(pinfo->r, NULL, cb) != 1) { ret = 0; - RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_R_NOT_PRIME); + RSAerr(0, RSA_R_MP_R_NOT_PRIME); } } @@ -108,10 +94,9 @@ int RSA_check_key_ex(const RSA *key, BN_GENCB *cb) if (BN_cmp(i, key->n) != 0) { ret = 0; if (ex_primes) - RSAerr(RSA_F_RSA_CHECK_KEY_EX, - RSA_R_N_DOES_NOT_EQUAL_PRODUCT_OF_PRIMES); + RSAerr(0, RSA_R_N_DOES_NOT_EQUAL_PRODUCT_OF_PRIMES); else - RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_N_DOES_NOT_EQUAL_P_Q); + RSAerr(0, RSA_R_N_DOES_NOT_EQUAL_P_Q); } /* d*e = 1 mod \lambda(n)? */ @@ -159,7 +144,7 @@ int RSA_check_key_ex(const RSA *key, BN_GENCB *cb) if (!BN_is_one(i)) { ret = 0; - RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_D_E_NOT_CONGRUENT_TO_1); + RSAerr(0, RSA_R_D_E_NOT_CONGRUENT_TO_1); } if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL) { @@ -174,7 +159,7 @@ int RSA_check_key_ex(const RSA *key, BN_GENCB *cb) } if (BN_cmp(j, key->dmp1) != 0) { ret = 0; - RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMP1_NOT_CONGRUENT_TO_D); + RSAerr(0, RSA_R_DMP1_NOT_CONGRUENT_TO_D); } /* dmq1 = d mod (q-1)? */ @@ -188,7 +173,7 @@ int RSA_check_key_ex(const RSA *key, BN_GENCB *cb) } if (BN_cmp(j, key->dmq1) != 0) { ret = 0; - RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_DMQ1_NOT_CONGRUENT_TO_D); + RSAerr(0, RSA_R_DMQ1_NOT_CONGRUENT_TO_D); } /* iqmp = q^-1 mod p? */ @@ -198,7 +183,7 @@ int RSA_check_key_ex(const RSA *key, BN_GENCB *cb) } if (BN_cmp(i, key->iqmp) != 0) { ret = 0; - RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_IQMP_NOT_INVERSE_OF_Q); + RSAerr(0, RSA_R_IQMP_NOT_INVERSE_OF_Q); } } @@ -215,7 +200,7 @@ int RSA_check_key_ex(const RSA *key, BN_GENCB *cb) } if (BN_cmp(j, pinfo->d) != 0) { ret = 0; - RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_EXPONENT_NOT_CONGRUENT_TO_D); + RSAerr(0, RSA_R_MP_EXPONENT_NOT_CONGRUENT_TO_D); } /* t_i = R_i ^ -1 mod r_i ? */ if (!BN_mod_inverse(i, pinfo->pp, pinfo->r, ctx)) { @@ -224,7 +209,7 @@ int RSA_check_key_ex(const RSA *key, BN_GENCB *cb) } if (BN_cmp(i, pinfo->t) != 0) { ret = 0; - RSAerr(RSA_F_RSA_CHECK_KEY_EX, RSA_R_MP_COEFFICIENT_NOT_INVERSE_OF_R); + RSAerr(0, RSA_R_MP_COEFFICIENT_NOT_INVERSE_OF_R); } } @@ -236,5 +221,40 @@ int RSA_check_key_ex(const RSA *key, BN_GENCB *cb) BN_free(m); BN_CTX_free(ctx); return ret; +} +#endif /* FIPS_MODE */ + +int rsa_validate_public(const RSA *key) +{ + return rsa_sp800_56b_check_public(key); +} + +int rsa_validate_private(const RSA *key) +{ + return rsa_sp800_56b_check_private(key); +} + +int rsa_validate_pairwise(const RSA *key) +{ +#ifdef FIPS_MODE + return rsa_sp800_56b_check_keypair(key, NULL, -1, RSA_bits(key)); +#else + return rsa_validate_keypair_multiprime(key, NULL); +#endif +} + +int RSA_check_key(const RSA *key) +{ + return RSA_check_key_ex(key, NULL); +} + +int RSA_check_key_ex(const RSA *key, BN_GENCB *cb) +{ +#ifdef FIPS_MODE + return rsa_validate_public(key) + && rsa_validate_private(key) + && rsa_validate_pairwise(key); +#else + return rsa_validate_keypair_multiprime(key, cb); #endif /* FIPS_MODE */ } diff --git a/crypto/rsa/rsa_sp800_56b_check.c b/crypto/rsa/rsa_sp800_56b_check.c index a580b1dc59..93d3103a18 100644 --- a/crypto/rsa/rsa_sp800_56b_check.c +++ b/crypto/rsa/rsa_sp800_56b_check.c @@ -237,13 +237,17 @@ int rsa_get_lcm(BN_CTX *ctx, const BIGNUM *p, const BIGNUM *q, */ int rsa_sp800_56b_check_public(const RSA *rsa) { - int ret = 0, nbits, status; + int ret = 0, status; +#ifdef FIPS_MODE + int nbits; +#endif BN_CTX *ctx = NULL; BIGNUM *gcd = NULL; if (rsa->n == NULL || rsa->e == NULL) return 0; +#ifdef FIPS_MODE /* * (Step a): modulus must be 2048 or 3072 (caveat from SP800-56Br1) * NOTE: changed to allow keys >= 2048 @@ -253,11 +257,11 @@ int rsa_sp800_56b_check_public(const RSA *rsa) RSAerr(RSA_F_RSA_SP800_56B_CHECK_PUBLIC, RSA_R_INVALID_KEY_LENGTH); return 0; } +#endif if (!BN_is_odd(rsa->n)) { RSAerr(RSA_F_RSA_SP800_56B_CHECK_PUBLIC, RSA_R_INVALID_MODULUS); return 0; } - /* (Steps b-c): 2^16 < e < 2^256, n and e must be odd */ if (!rsa_check_public_exponent(rsa->e)) { RSAerr(RSA_F_RSA_SP800_56B_CHECK_PUBLIC, diff --git a/doc/man3/EVP_PKEY_check.pod b/doc/man3/EVP_PKEY_check.pod new file mode 100644 index 0000000000..95b57a3fb8 --- /dev/null +++ b/doc/man3/EVP_PKEY_check.pod @@ -0,0 +1,73 @@ +=pod + +=head1 NAME + +EVP_PKEY_check, EVP_PKEY_param_check, EVP_PKEY_public_check, +EVP_PKEY_private_check, EVP_PKEY_pairwise_check +- key and parameter validation functions + +=head1 SYNOPSIS + + #include <openssl/evp.h> + + int EVP_PKEY_check(EVP_PKEY_CTX *ctx); + int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx); + int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx); + int EVP_PKEY_private_check(EVP_PKEY_CTX *ctx); + int EVP_PKEY_pairwise_check(EVP_PKEY_CTX *ctx); + +=head1 DESCRIPTION + +EVP_PKEY_param_check() validates the domain parameters component of the key +given by B<ctx>. + +EVP_PKEY_public_check() validates the public component of the key given by B<ctx>. + +EVP_PKEY_private_check() validates the private component of the key given by B<ctx>. + +EVP_PKEY_pairwise_check() validates that the public and private components have +the correct mathematical relationship to each other for the key given by B<ctx>. + +EVP_PKEY_check() validates all components of a key given by B<ctx>. + +=head1 NOTES + +Refer to SP800-56A and SP800-56B for rules relating to when these functions +should be called during key establishment. +It is not necessary to call these functions after locally calling an approved key +generation method, but may be required for assurance purposes when receiving +keys from a third party. + +In OpenSSL an EVP_PKEY structure containing a private key also contains the +public key components and parameters (if any). An OpenSSL private key is +equivalent to what some libraries call a "key pair". A private key can be used +in functions which require the use of a public key or parameters. + +=head1 RETURN VALUES + +All functions return 1 for success or others for failure. +They return -2 if the operation is not supported for the specific algorithm. + +=head1 SEE ALSO + +L<EVP_PKEY_CTX_new(3)>, +L<EVP_PKEY_fromdata(3)>, + +=head1 HISTORY + +EVP_PKEY_check(), EVP_PKEY_public_check() and EVP_PKEY_param_check() were added +in OpenSSL 1.1.1. + +EVP_PKEY_private_check() and EVP_PKEY_pairwise_check() were added +in OpenSSL 3.0. + +=head1 COPYRIGHT + +Copyright 2006-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 +L<https://www.openssl.org/source/license.html>. + +=cut diff --git a/doc/man3/EVP_PKEY_keygen.pod b/doc/man3/EVP_PKEY_keygen.pod index 439d7d11fd..e326b82ffa 100644 --- a/doc/man3/EVP_PKEY_keygen.pod +++ b/doc/man3/EVP_PKEY_keygen.pod @@ -6,8 +6,7 @@ EVP_PKEY_keygen_init, EVP_PKEY_keygen, EVP_PKEY_paramgen_init, EVP_PKEY_paramgen, EVP_PKEY_CTX_set_cb, EVP_PKEY_CTX_get_cb, EVP_PKEY_CTX_get_keygen_info, EVP_PKEY_CTX_set_app_data, EVP_PKEY_CTX_get_app_data, -EVP_PKEY_gen_cb, EVP_PKEY_check, EVP_PKEY_public_check, -EVP_PKEY_param_check +EVP_PKEY_gen_cb - key and parameter generation and check functions =head1 SYNOPSIS @@ -29,10 +28,6 @@ EVP_PKEY_param_check void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data); void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx); - int EVP_PKEY_check(EVP_PKEY_CTX *ctx); - int EVP_PKEY_public_check(EVP_PKEY_CTX *ctx); - int EVP_PKEY_param_check(EVP_PKEY_CTX *ctx); - =head1 DESCRIPTION The EVP_PKEY_keygen_init() function initializes a public key algorithm @@ -63,18 +58,6 @@ and retrieve an opaque pointer. This can be used to set some application defined value which can be retrieved in the callback: for example a handle which is used to update a "progress dialog". -EVP_PKEY_check() validates the key-pair given by B<ctx>. This function first tries -to use customized key check method in B<EVP_PKEY_METHOD> if it's present; otherwise -it calls a default one defined in B<EVP_PKEY_ASN1_METHOD>. - -EVP_PKEY_public_check() validates the public component of the key-pair given by B<ctx>. -This function first tries to use customized key check method in B<EVP_PKEY_METHOD> -if it's present; otherwise it calls a default one defined in B<EVP_PKEY_ASN1_METHOD>. - -EVP_PKEY_param_check() validates the algorithm parameters of the key-pair given by B<ctx>. -This function first tries to use customized key check method in B<EVP_PKEY_METHOD> -if it's present; otherwise it calls a default one defined in B<EVP_PKEY_ASN1_METHOD>. - =head1 NOTES After the call to EVP_PKEY_keygen_init() or EVP_PKEY_paramgen_init() algorithm @@ -106,10 +89,6 @@ EVP_PKEY_paramgen() return 1 for success and 0 or a negative value for failure. In particular a return value of -2 indicates the operation is not supported by the public key algorithm. -EVP_PKEY_check(), EVP_PKEY_public_check() and EVP_PKEY_param_check() return 1 -for success or others for failure. They return -2 if the operation is not supported -for the specific algorithm. - =head1 EXAMPLES Generate a 2048 bit RSA key: @@ -191,9 +170,6 @@ L<EVP_PKEY_derive(3)> These functions were added in OpenSSL 1.0.0. -EVP_PKEY_check(), EVP_PKEY_public_check() and EVP_PKEY_param_check() were added -in OpenSSL 1.1.1. - =head1 COPYRIGHT Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved. diff --git a/doc/man7/provider-keymgmt.pod b/doc/man7/provider-keymgmt.pod index adc1978a36..94427de89f 100644 --- a/doc/man7/provider-keymgmt.pod +++ b/doc/man7/provider-keymgmt.pod @@ -30,6 +30,9 @@ provider-keymgmt - The KEYMGMT library E<lt>-E<gt> provider functions int OP_keymgmt_get_domparam_params(void *domparams, OSSL_PARAM params[]); const OSSL_PARAM *OP_keymgmt_gettable_domparam_params(void); + /* Key domain parameter validation */ + int OP_keymgmt_validate_domparams(void *key); + /* Key creation and destruction */ void *OP_keymgmt_importkey(void *provctx, const OSSL_PARAM params[]); void *OP_keymgmt_genkey(void *provctx, @@ -48,6 +51,11 @@ provider-keymgmt - The KEYMGMT library E<lt>-E<gt> provider functions int OP_keymgmt_get_key_params(void *key, OSSL_PARAM params[]); const OSSL_PARAM *OP_keymgmt_gettable_key_params(void); + /* Key validation */ + int OP_keymgmt_validate_public(void *key); + int OP_keymgmt_validate_private(void *key); + int OP_keymgmt_validate_pairwise(void *key); + /* Discovery of supported operations */ const char *OP_keymgmt_query_operation_name(int operation_id); @@ -108,6 +116,11 @@ macros in L<openssl-core_numbers.h(7)>, as follows: OP_keymgmt_query_operation_name OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME + OP_keymgmt_validate_domparams OSSL_FUNC_KEYMGMT_VALIDATE_DOMPARAMS + OP_keymgmt_validate_public OSSL_FUNC_KEYMGMT_VALIDATE_PUBLIC + OP_keymgmt_validate_private OSSL_FUNC_KEYMGMT_VALIDATE_PRIVATE + OP_keymgmt_validate_pairwise OSSL_FUNC_KEYMGMT_VALIDATE_PAIRWISE + =head2 Domain Parameter Functions OP_keymgmt_importdomparams() should create a provider side structure @@ -143,6 +156,9 @@ OP_keymgmt_gettable_domparam_params() should return a constant array of descriptor B<OSSL_PARAM>, for parameters that OP_keymgmt_get_domparam_params() can handle. +OP_keymgmt_validate_domparams() should return a value of 1 if the +domain parameters are valid, or 0 for invalid. + =head2 Key functions OP_keymgmt_importkey() should create a provider side structure @@ -185,6 +201,14 @@ OP_keymgmt_gettable_key_params() should return a constant array of descriptor B<OSSL_PARAM>, for parameters that OP_keymgmt_get_key_params() can handle. +OP_keymgmt_validate_public() should return 1 if the public component of the +key is valid, or 0 if invalid. +OP_keymgmt_validate_private() should return 1 if the private component of the +key is valid, or 0 if invalid. +OP_keymgmt_validate_pairwise() should return 1 if the the pairwise consistency +of the key is valid, or 0 if invalid. + + =head2 Supported operations OP_keymgmt_query_operation_name() should return the name of the @@ -241,7 +265,7 @@ The KEYMGMT interface was introduced in OpenSSL 3.0. =head1 COPYRIGHT -Copyright 2019 The OpenSSL Project Authors. All Rights Reserved. +Copyright 2019-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 diff --git a/include/crypto/evp.h b/include/crypto/evp.h index b3d1f7d21c..5d6f70be78 100644 --- a/include/crypto/evp.h +++ b/include/crypto/evp.h @@ -637,6 +637,12 @@ int evp_keymgmt_get_key_params(const EVP_KEYMGMT *keymgmt, void *provkey, OSSL_PARAM params[]); const OSSL_PARAM *evp_keymgmt_gettable_key_params(const EVP_KEYMGMT *keymgmt); +int evp_keymgmt_validate_domparams(const EVP_KEYMGMT *keymgmt, void *provkey); +int evp_keymgmt_validate_public(const EVP_KEYMGMT *keymgmt, void *provkey); +int evp_keymgmt_validate_private(const EVP_KEYMGMT *keymgmt, void *provkey); +int evp_keymgmt_validate_pairwise(const EVP_KEYMGMT *keymgmt, void *provkey); + + /* Pulling defines out of C source files */ #define EVP_RC4_KEY_SIZE 16 diff --git a/include/crypto/rsa.h b/include/crypto/rsa.h index 29256371f6..c3763d1e1f 100644 --- a/include/crypto/rsa.h +++ b/include/crypto/rsa.h @@ -22,4 +22,9 @@ int rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes, int rsa_padding_check_PKCS1_type_2_TLS(unsigned char *to, size_t tlen, const unsigned char *from, size_t flen, int client_version, int alt_version); + +int rsa_validate_public(const RSA *key); +int rsa_validate_private(const RSA *key); +int rsa_validate_pairwise(const RSA *key); + #endif |