Add RSA key validation to default provider

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/10780)
This commit is contained in:
Shane Lontis 2020-01-29 20:32:32 +10:00
parent a76ce2862b
commit 12603de634
19 changed files with 439 additions and 139 deletions

View File

@ -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 \

View File

@ -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 {

View File

@ -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);
}

View File

@ -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;
}
}
/*

158
crypto/evp/pmeth_check.c Normal file
View File

@ -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);
}

View File

@ -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;
}

View File

@ -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;

View File

@ -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 */
}

View File

@ -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,

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -416,8 +416,8 @@ OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, OP_keymgmt_importkey_types, (void))
OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, OP_keymgmt_exportkey_types, (void))
/* Key information */
#define OSSL_FUNC_KEYMGMT_GET_KEY_PARAMS 27
#define OSSL_FUNC_KEYMGMT_GETTABLE_KEY_PARAMS 28
# define OSSL_FUNC_KEYMGMT_GET_KEY_PARAMS 27
# define OSSL_FUNC_KEYMGMT_GETTABLE_KEY_PARAMS 28
OSSL_CORE_MAKE_FUNC(int, OP_keymgmt_get_key_params,
(void *key, OSSL_PARAM params[]))
OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, OP_keymgmt_gettable_key_params, (void))
@ -427,6 +427,16 @@ OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, OP_keymgmt_gettable_key_params, (void))
OSSL_CORE_MAKE_FUNC(const char *,OP_keymgmt_query_operation_name,
(int operation_id))
/* Key validation */
# define OSSL_FUNC_KEYMGMT_VALIDATE_DOMPARAMS 29
# define OSSL_FUNC_KEYMGMT_VALIDATE_PUBLIC 30
# define OSSL_FUNC_KEYMGMT_VALIDATE_PRIVATE 31
# define OSSL_FUNC_KEYMGMT_VALIDATE_PAIRWISE 32
OSSL_CORE_MAKE_FUNC(int, OP_keymgmt_validate_domparams, (void *key))
OSSL_CORE_MAKE_FUNC(int, OP_keymgmt_validate_public, (void *key))
OSSL_CORE_MAKE_FUNC(int, OP_keymgmt_validate_private, (void *key))
OSSL_CORE_MAKE_FUNC(int, OP_keymgmt_validate_pairwise, (void *key))
/* Key Exchange */
# define OSSL_FUNC_KEYEXCH_NEWCTX 1

View File

@ -1590,6 +1590,8 @@ int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
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);
int EVP_PKEY_private_check(EVP_PKEY_CTX *ctx);
int EVP_PKEY_pairwise_check(EVP_PKEY_CTX *ctx);
void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb);
EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx);

View File

@ -23,6 +23,9 @@
static OSSL_OP_keymgmt_importkey_fn rsa_importkey;
static OSSL_OP_keymgmt_exportkey_fn rsa_exportkey;
static OSSL_OP_keymgmt_get_key_params_fn rsa_get_key_params;
static OSSL_OP_keymgmt_validate_public_fn rsa_validatekey_public;
static OSSL_OP_keymgmt_validate_private_fn rsa_validatekey_private;
static OSSL_OP_keymgmt_validate_pairwise_fn rsa_validatekey_pairwise;
#define RSA_DEFAULT_MD "SHA256"
@ -288,6 +291,27 @@ static int rsa_get_key_params(void *key, OSSL_PARAM params[])
return 1;
}
static int rsa_validatekey_public(void *key)
{
RSA *rsa = key;
return rsa_validate_public(rsa);
}
static int rsa_validatekey_private(void *key)
{
RSA *rsa = key;
return rsa_validate_private(rsa);
}
static int rsa_validatekey_pairwise(void *key)
{
RSA *rsa = key;
return rsa_validate_pairwise(rsa);
}
const OSSL_DISPATCH rsa_keymgmt_functions[] = {
{ OSSL_FUNC_KEYMGMT_IMPORTKEY, (void (*)(void))rsa_importkey },
{ OSSL_FUNC_KEYMGMT_IMPORTKEY_TYPES, (void (*)(void))rsa_importkey_types },
@ -295,5 +319,11 @@ const OSSL_DISPATCH rsa_keymgmt_functions[] = {
{ OSSL_FUNC_KEYMGMT_EXPORTKEY_TYPES, (void (*)(void))rsa_exportkey_types },
{ OSSL_FUNC_KEYMGMT_FREEKEY, (void (*)(void))RSA_free },
{ OSSL_FUNC_KEYMGMT_GET_KEY_PARAMS, (void (*) (void))rsa_get_key_params },
{ OSSL_FUNC_KEYMGMT_VALIDATE_PUBLIC,
(void (*)(void))rsa_validatekey_public },
{ OSSL_FUNC_KEYMGMT_VALIDATE_PRIVATE,
(void (*)(void))rsa_validatekey_private },
{ OSSL_FUNC_KEYMGMT_VALIDATE_PAIRWISE,
(void (*)(void))rsa_validatekey_pairwise },
{ 0, NULL }
};

View File

@ -92,7 +92,7 @@ err:
static int test_fromdata_rsa(void)
{
int ret = 0;
EVP_PKEY_CTX *ctx = NULL;
EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL;
EVP_PKEY *pk = NULL;
/*
* 32-bit RSA key, extracted from this command,
@ -132,11 +132,21 @@ static int test_fromdata_rsa(void)
|| !TEST_int_eq(EVP_PKEY_size(pk), 4))
goto err;
if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")))
goto err;
if (!TEST_true(EVP_PKEY_check(key_ctx))
|| !TEST_true(EVP_PKEY_public_check(key_ctx))
|| !TEST_true(EVP_PKEY_private_check(key_ctx))
|| !TEST_true(EVP_PKEY_pairwise_check(key_ctx)))
goto err;
ret = test_print_key_using_pem(pk)
| test_print_key_using_serializer(pk);
err:
EVP_PKEY_free(pk);
EVP_PKEY_CTX_free(key_ctx);
EVP_PKEY_CTX_free(ctx);
return ret;

View File

@ -4915,3 +4915,5 @@ RAND_bytes_ex ? 3_0_0 EXIST::FUNCTION:
EVP_PKEY_get_default_digest_name ? 3_0_0 EXIST::FUNCTION:
PKCS8_pkey_add1_attr ? 3_0_0 EXIST::FUNCTION:
PKCS8_pkey_add1_attr_by_OBJ ? 3_0_0 EXIST::FUNCTION:
EVP_PKEY_private_check ? 3_0_0 EXIST::FUNCTION:
EVP_PKEY_pairwise_check ? 3_0_0 EXIST::FUNCTION: