Latest update.
This commit is contained in:
+45
-33
@@ -859,6 +859,7 @@ static int rsa_sig_info_set(X509_SIG_INFO *siginf, const X509_ALGOR *sigalg,
|
||||
uint32_t flags;
|
||||
const EVP_MD *mgf1md = NULL, *md = NULL;
|
||||
RSA_PSS_PARAMS *pss;
|
||||
int secbits;
|
||||
|
||||
/* Sanity check: make sure it is PSS */
|
||||
if (OBJ_obj2nid(sigalg->algorithm) != EVP_PKEY_RSA_PSS)
|
||||
@@ -878,7 +879,24 @@ static int rsa_sig_info_set(X509_SIG_INFO *siginf, const X509_ALGOR *sigalg,
|
||||
else
|
||||
flags = 0;
|
||||
/* Note: security bits half number of digest bits */
|
||||
X509_SIG_INFO_set(siginf, mdnid, EVP_PKEY_RSA_PSS, EVP_MD_size(md) * 4,
|
||||
secbits = EVP_MD_size(md) * 4;
|
||||
/*
|
||||
* SHA1 and MD5 are known to be broken. Reduce security bits so that
|
||||
* they're no longer accepted at security level 1. The real values don't
|
||||
* really matter as long as they're lower than 80, which is our security
|
||||
* level 1.
|
||||
* https://eprint.iacr.org/2020/014 puts a chosen-prefix attack for SHA1 at
|
||||
* 2^63.4
|
||||
* https://documents.epfl.ch/users/l/le/lenstra/public/papers/lat.pdf
|
||||
* puts a chosen-prefix attack for MD5 at 2^39.
|
||||
*/
|
||||
if (mdnid == NID_sha1)
|
||||
secbits = 64;
|
||||
else if (mdnid == NID_md5_sha1)
|
||||
secbits = 68;
|
||||
else if (mdnid == NID_md5)
|
||||
secbits = 39;
|
||||
X509_SIG_INFO_set(siginf, mdnid, EVP_PKEY_RSA_PSS, secbits,
|
||||
flags);
|
||||
rv = 1;
|
||||
err:
|
||||
@@ -1056,40 +1074,42 @@ static size_t rsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
|
||||
|
||||
DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
|
||||
|
||||
static void *rsa_pkey_export_to(const EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
|
||||
int want_domainparams)
|
||||
static int rsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
|
||||
EVP_KEYMGMT *to_keymgmt)
|
||||
{
|
||||
RSA *rsa = pk->pkey.rsa;
|
||||
RSA *rsa = from->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;
|
||||
int rv = 0;
|
||||
|
||||
/* Public parameters must always be present */
|
||||
if (n == NULL || e == NULL)
|
||||
goto err;
|
||||
|
||||
ossl_param_bld_init(&tmpl);
|
||||
|
||||
/* |e| and |n| are always present */
|
||||
if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_RSA_E, e))
|
||||
goto err;
|
||||
if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_RSA_N, n))
|
||||
goto err;
|
||||
|
||||
if (d != NULL) {
|
||||
/* It's a private key, so we should have everything else too */
|
||||
int i;
|
||||
|
||||
/* 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;
|
||||
|
||||
numprimes = sk_BIGNUM_const_num(primes);
|
||||
numexps = sk_BIGNUM_const_num(exps);
|
||||
numcoeffs = sk_BIGNUM_const_num(coeffs);
|
||||
@@ -1102,15 +1122,6 @@ static void *rsa_pkey_export_to(const EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
|
||||
+ 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;
|
||||
@@ -1144,14 +1155,15 @@ static void *rsa_pkey_export_to(const EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
|
||||
goto err;
|
||||
|
||||
/* We export, the provider imports */
|
||||
provkey = evp_keymgmt_importkey(keymgmt, params);
|
||||
rv = evp_keymgmt_import(to_keymgmt, to_keydata, OSSL_KEYMGMT_SELECT_ALL,
|
||||
params);
|
||||
|
||||
err:
|
||||
sk_BIGNUM_const_free(primes);
|
||||
sk_BIGNUM_const_free(exps);
|
||||
sk_BIGNUM_const_free(coeffs);
|
||||
ossl_param_bld_free(params);
|
||||
return provkey;
|
||||
return rv;
|
||||
}
|
||||
|
||||
const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[2] = {
|
||||
|
||||
+55
-35
@@ -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 */
|
||||
}
|
||||
@@ -851,8 +851,7 @@ int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
|
||||
* Cast away the const. This is read
|
||||
* only so should be safe
|
||||
*/
|
||||
(char *)mdname,
|
||||
strlen(mdname) + 1);
|
||||
(char *)mdname, 0);
|
||||
if (mdprops != NULL) {
|
||||
*p++ = OSSL_PARAM_construct_utf8_string(
|
||||
OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS,
|
||||
@@ -860,8 +859,7 @@ int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
|
||||
* Cast away the const. This is read
|
||||
* only so should be safe
|
||||
*/
|
||||
(char *)mdprops,
|
||||
strlen(mdprops) + 1);
|
||||
(char *)mdprops, 0);
|
||||
}
|
||||
*p++ = OSSL_PARAM_construct_end();
|
||||
|
||||
@@ -979,8 +977,7 @@ int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
|
||||
* Cast away the const. This is read
|
||||
* only so should be safe
|
||||
*/
|
||||
(char *)mdname,
|
||||
strlen(mdname) + 1);
|
||||
(char *)mdname, 0);
|
||||
if (mdprops != NULL) {
|
||||
*p++ = OSSL_PARAM_construct_utf8_string(
|
||||
OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS,
|
||||
@@ -988,8 +985,7 @@ int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
|
||||
* Cast away the const. This is read
|
||||
* only so should be safe
|
||||
*/
|
||||
(char *)mdprops,
|
||||
strlen(mdprops) + 1);
|
||||
(char *)mdprops, 0);
|
||||
}
|
||||
*p++ = OSSL_PARAM_construct_end();
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user