Latest update.

This commit is contained in:
2020-01-17 19:45:12 +09:00
parent 016df9f433
commit 0f7abb4eb6
1100 changed files with 47785 additions and 16292 deletions
+122 -2
View File
@@ -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,111 @@ 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. */
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 +1185,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 +1230,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
};
+1 -1
View File
@@ -13,7 +13,7 @@
*/
#include <openssl/opensslconf.h>
#if OPENSSL_API_0_9_8
#ifdef OPENSSL_NO_DEPRECATED_0_9_8
NON_EMPTY_TRANSLATION_UNIT
#else
+2
View File
@@ -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;
+522 -2
View File
@@ -9,12 +9,14 @@
#include <stdio.h>
#include <openssl/crypto.h>
#include <openssl/core_names.h>
#include <openssl/engine.h>
#include <openssl/evp.h>
#include "internal/cryptlib.h"
#include "internal/refcount.h"
#include "crypto/bn.h"
#include <openssl/engine.h>
#include <openssl/evp.h>
#include "crypto/evp.h"
#include "crypto/rsa.h"
#include "rsa_local.h"
RSA *RSA_new(void)
@@ -86,9 +88,11 @@ RSA *RSA_new_method(ENGINE *engine)
#endif
ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
#ifndef FIPS_MODE
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
goto err;
}
#endif
if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
@@ -121,7 +125,9 @@ void RSA_free(RSA *r)
ENGINE_finish(r->engine);
#endif
#ifndef FIPS_MODE
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
#endif
CRYPTO_THREAD_lock_free(r->lock);
@@ -153,6 +159,7 @@ int RSA_up_ref(RSA *r)
return i > 1 ? 1 : 0;
}
#ifndef FIPS_MODE
int RSA_set_ex_data(RSA *r, int idx, void *arg)
{
return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
@@ -162,6 +169,7 @@ void *RSA_get_ex_data(const RSA *r, int idx)
{
return CRYPTO_get_ex_data(&r->ex_data, idx);
}
#endif
/*
* Define a scaling constant for our fixed point arithmetic.
@@ -327,6 +335,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;
}
@@ -350,6 +359,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;
}
@@ -379,6 +389,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;
}
@@ -443,6 +454,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:
@@ -578,6 +590,11 @@ const BIGNUM *RSA_get0_iqmp(const RSA *r)
return r->iqmp;
}
const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *r)
{
return r->pss;
}
void RSA_clear_flags(RSA *r, int flags)
{
r->flags &= ~flags;
@@ -613,3 +630,506 @@ int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
return -1;
return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
}
DEFINE_STACK_OF(BIGNUM)
int rsa_set0_all_params(RSA *r, const STACK_OF(BIGNUM) *primes,
const STACK_OF(BIGNUM) *exps,
const STACK_OF(BIGNUM) *coeffs)
{
STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
int pnum;
if (primes == NULL || exps == NULL || coeffs == NULL)
return 0;
pnum = sk_BIGNUM_num(primes);
if (pnum < 2
|| pnum != sk_BIGNUM_num(exps)
|| pnum != sk_BIGNUM_num(coeffs) + 1)
return 0;
if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
sk_BIGNUM_value(primes, 1))
|| !RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
sk_BIGNUM_value(exps, 1),
sk_BIGNUM_value(coeffs, 0)))
return 0;
old_infos = r->prime_infos;
if (pnum > 2) {
int i;
prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
if (prime_infos == NULL)
return 0;
for (i = 2; i < pnum; i++) {
BIGNUM *prime = sk_BIGNUM_value(primes, i);
BIGNUM *exp = sk_BIGNUM_value(exps, i);
BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1);
RSA_PRIME_INFO *pinfo = NULL;
if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
goto err;
/* Using rsa_multip_info_new() is wasteful, so allocate directly */
if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL) {
ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
goto err;
}
pinfo->r = prime;
pinfo->d = exp;
pinfo->t = coeff;
BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
(void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
}
r->prime_infos = prime_infos;
if (!rsa_multip_calc_product(r)) {
r->prime_infos = old_infos;
goto err;
}
}
if (old_infos != NULL) {
/*
* This is hard to deal with, since the old infos could
* also be set by this function and r, d, t should not
* be freed in that case. So currently, stay consistent
* with other *set0* functions: just free it...
*/
sk_RSA_PRIME_INFO_pop_free(old_infos, rsa_multip_info_free);
}
r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
r->dirty_cnt++;
return 1;
err:
/* r, d, t should not be freed */
sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
return 0;
}
DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
int rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
STACK_OF(BIGNUM_const) *exps,
STACK_OF(BIGNUM_const) *coeffs)
{
RSA_PRIME_INFO *pinfo;
int i, pnum;
if (r == NULL)
return 0;
pnum = RSA_get_multi_prime_extra_count(r);
sk_BIGNUM_const_push(primes, RSA_get0_p(r));
sk_BIGNUM_const_push(primes, RSA_get0_q(r));
sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
for (i = 0; i < pnum; i++) {
pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
sk_BIGNUM_const_push(primes, pinfo->r);
sk_BIGNUM_const_push(exps, pinfo->d);
sk_BIGNUM_const_push(coeffs, pinfo->t);
}
return 1;
}
int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode)
{
OSSL_PARAM pad_params[2], *p = pad_params;
if (ctx == NULL) {
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
/* Uses the same return values as EVP_PKEY_CTX_ctrl */
return -2;
}
/* If key type not RSA or RSA-PSS return error */
if (ctx->pmeth != NULL
&& ctx->pmeth->pkey_id != EVP_PKEY_RSA
&& ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
return -1;
/* TODO(3.0): Remove this eventually when no more legacy */
if (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
|| ctx->op.ciph.ciphprovctx == NULL)
return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PADDING,
pad_mode, NULL);
*p++ = OSSL_PARAM_construct_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, &pad_mode);
*p++ = OSSL_PARAM_construct_end();
return EVP_PKEY_CTX_set_params(ctx, pad_params);
}
int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode)
{
OSSL_PARAM pad_params[2], *p = pad_params;
if (ctx == NULL || pad_mode == NULL) {
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
/* Uses the same return values as EVP_PKEY_CTX_ctrl */
return -2;
}
/* If key type not RSA or RSA-PSS return error */
if (ctx->pmeth != NULL
&& ctx->pmeth->pkey_id != EVP_PKEY_RSA
&& ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
return -1;
/* TODO(3.0): Remove this eventually when no more legacy */
if (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
|| ctx->op.ciph.ciphprovctx == NULL)
return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET_RSA_PADDING, 0,
pad_mode);
*p++ = OSSL_PARAM_construct_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, pad_mode);
*p++ = OSSL_PARAM_construct_end();
if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
return 0;
return 1;
}
int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
{
const char *name;
if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
/* Uses the same return values as EVP_PKEY_CTX_ctrl */
return -2;
}
/* If key type not RSA return error */
if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
return -1;
/* TODO(3.0): Remove this eventually when no more legacy */
if (ctx->op.ciph.ciphprovctx == NULL)
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
name = (md == NULL) ? "" : EVP_MD_name(md);
return EVP_PKEY_CTX_set_rsa_oaep_md_name(ctx, name, NULL);
}
int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
const char *mdprops)
{
OSSL_PARAM rsa_params[3], *p = rsa_params;
if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
/* Uses the same return values as EVP_PKEY_CTX_ctrl */
return -2;
}
/* If key type not RSA return error */
if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
return -1;
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
/*
* Cast away the const. This is read
* only so should be safe
*/
(char *)mdname,
strlen(mdname) + 1);
if (mdprops != NULL) {
*p++ = OSSL_PARAM_construct_utf8_string(
OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS,
/*
* Cast away the const. This is read
* only so should be safe
*/
(char *)mdprops,
strlen(mdprops) + 1);
}
*p++ = OSSL_PARAM_construct_end();
return EVP_PKEY_CTX_set_params(ctx, rsa_params);
}
int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name,
size_t namelen)
{
OSSL_PARAM rsa_params[2], *p = rsa_params;
if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
/* Uses the same return values as EVP_PKEY_CTX_ctrl */
return -2;
}
/* If key type not RSA return error */
if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
return -1;
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
name, namelen);
*p++ = OSSL_PARAM_construct_end();
if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
return -1;
return 1;
}
int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
{
/* 80 should be big enough */
char name[80] = "";
if (ctx == NULL || md == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
/* Uses the same return values as EVP_PKEY_CTX_ctrl */
return -2;
}
/* If key type not RSA return error */
if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
return -1;
/* TODO(3.0): Remove this eventually when no more legacy */
if (ctx->op.ciph.ciphprovctx == NULL)
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md);
if (EVP_PKEY_CTX_get_rsa_oaep_md_name(ctx, name, sizeof(name)) <= 0)
return -1;
/* May be NULL meaning "unknown" */
*md = EVP_get_digestbyname(name);
return 1;
}
int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
{
const char *name;
if (ctx == NULL
|| (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
&& !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
/* Uses the same return values as EVP_PKEY_CTX_ctrl */
return -2;
}
/* If key type not RSA return error */
if (ctx->pmeth != NULL
&& ctx->pmeth->pkey_id != EVP_PKEY_RSA
&& ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
return -1;
/* TODO(3.0): Remove this eventually when no more legacy */
if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
&& ctx->op.ciph.ciphprovctx == NULL)
|| (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
&& ctx->op.sig.sigprovctx == NULL))
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)md);
name = (md == NULL) ? "" : EVP_MD_name(md);
return EVP_PKEY_CTX_set_rsa_mgf1_md_name(ctx, name, NULL);
}
int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
const char *mdprops)
{
OSSL_PARAM rsa_params[3], *p = rsa_params;
if (ctx == NULL
|| mdname == NULL
|| (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
&& !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
/* Uses the same return values as EVP_PKEY_CTX_ctrl */
return -2;
}
/* If key type not RSA return error */
if (ctx->pmeth != NULL
&& ctx->pmeth->pkey_id != EVP_PKEY_RSA
&& ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
return -1;
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST,
/*
* Cast away the const. This is read
* only so should be safe
*/
(char *)mdname,
strlen(mdname) + 1);
if (mdprops != NULL) {
*p++ = OSSL_PARAM_construct_utf8_string(
OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS,
/*
* Cast away the const. This is read
* only so should be safe
*/
(char *)mdprops,
strlen(mdprops) + 1);
}
*p++ = OSSL_PARAM_construct_end();
return EVP_PKEY_CTX_set_params(ctx, rsa_params);
}
int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name,
size_t namelen)
{
OSSL_PARAM rsa_params[2], *p = rsa_params;
if (ctx == NULL
|| (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
&& !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
/* Uses the same return values as EVP_PKEY_CTX_ctrl */
return -2;
}
/* If key type not RSA or RSA-PSS return error */
if (ctx->pmeth != NULL
&& ctx->pmeth->pkey_id != EVP_PKEY_RSA
&& ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
return -1;
*p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST,
name, namelen);
*p++ = OSSL_PARAM_construct_end();
if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
return -1;
return 1;
}
int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
{
/* 80 should be big enough */
char name[80] = "";
if (ctx == NULL
|| (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
&& !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
/* Uses the same return values as EVP_PKEY_CTX_ctrl */
return -2;
}
/* If key type not RSA or RSA-PSS return error */
if (ctx->pmeth != NULL
&& ctx->pmeth->pkey_id != EVP_PKEY_RSA
&& ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
return -1;
/* TODO(3.0): Remove this eventually when no more legacy */
if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
&& ctx->op.ciph.ciphprovctx == NULL)
|| (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
&& ctx->op.sig.sigprovctx == NULL))
return EVP_PKEY_CTX_ctrl(ctx, -1,
EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)md);
if (EVP_PKEY_CTX_get_rsa_mgf1_md_name(ctx, name, sizeof(name)) <= 0)
return -1;
/* May be NULL meaning "unknown" */
*md = EVP_get_digestbyname(name);
return 1;
}
int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen)
{
OSSL_PARAM rsa_params[2], *p = rsa_params;
if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
/* Uses the same return values as EVP_PKEY_CTX_ctrl */
return -2;
}
/* If key type not RSA return error */
if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
return -1;
/* TODO(3.0): Remove this eventually when no more legacy */
if (ctx->op.ciph.ciphprovctx == NULL)
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
EVP_PKEY_CTRL_RSA_OAEP_LABEL, llen,
(void *)label);
*p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
/*
* Cast away the const. This is read
* only so should be safe
*/
(void *)label,
(size_t)llen);
*p++ = OSSL_PARAM_construct_end();
if (!EVP_PKEY_CTX_set_params(ctx, rsa_params))
return 0;
OPENSSL_free(label);
return 1;
}
int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label)
{
OSSL_PARAM rsa_params[3], *p = rsa_params;
size_t labellen;
if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
/* Uses the same return values as EVP_PKEY_CTX_ctrl */
return -2;
}
/* If key type not RSA return error */
if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
return -1;
/* TODO(3.0): Remove this eventually when no more legacy */
if (ctx->op.ciph.ciphprovctx == NULL)
return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0,
(void *)label);
*p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
(void **)label, 0);
*p++ = OSSL_PARAM_construct_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN,
&labellen);
*p++ = OSSL_PARAM_construct_end();
if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
return -1;
if (labellen > INT_MAX)
return -1;
return (int)labellen;
}
+4
View File
@@ -50,8 +50,10 @@ struct rsa_st {
STACK_OF(RSA_PRIME_INFO) *prime_infos;
/* If a PSS only key this contains the parameter restrictions */
RSA_PSS_PARAMS *pss;
#ifndef FIPS_MODE
/* be careful using this if the RSA structure is shared */
CRYPTO_EX_DATA ex_data;
#endif
CRYPTO_REF_COUNT references;
int flags;
/* Used to cache montgomery values */
@@ -66,6 +68,8 @@ struct rsa_st {
BN_BLINDING *blinding;
BN_BLINDING *mt_blinding;
CRYPTO_RWLOCK *lock;
int dirty_cnt;
};
struct rsa_meth_st {
+124 -1
View File
@@ -10,10 +10,13 @@
#include "internal/constant_time.h"
#include <stdio.h>
#include "internal/cryptlib.h"
#include <openssl/bn.h>
#include <openssl/rsa.h>
#include <openssl/rand.h>
/* Just for the SSL_MAX_MASTER_KEY_LENGTH value */
#include <openssl/ssl.h>
#include "internal/cryptlib.h"
#include "crypto/rsa.h"
int RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen,
const unsigned char *from, int flen)
@@ -253,3 +256,123 @@ int RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen,
return constant_time_select_int(good, mlen, -1);
}
/*
* rsa_padding_check_PKCS1_type_2_TLS() checks and removes the PKCS1 type 2
* padding from a decrypted RSA message in a TLS signature. The result is stored
* in the buffer pointed to by |to| which should be |tlen| bytes long. |tlen|
* must be at least SSL_MAX_MASTER_KEY_LENGTH. The original decrypted message
* should be stored in |from| which must be |flen| bytes in length and padded
* such that |flen == RSA_size()|. The TLS protocol version that the client
* originally requested should be passed in |client_version|. Some buggy clients
* can exist which use the negotiated version instead of the originally
* requested protocol version. If it is necessary to work around this bug then
* the negotiated protocol version can be passed in |alt_version|, otherwise 0
* should be passed.
*
* If the passed message is publicly invalid or some other error that can be
* treated in non-constant time occurs then -1 is returned. On success the
* length of the decrypted data is returned. This will always be
* SSL_MAX_MASTER_KEY_LENGTH. If an error occurs that should be treated in
* constant time then this function will appear to return successfully, but the
* decrypted data will be randomly generated (as per
* https://tools.ietf.org/html/rfc5246#section-7.4.7.1).
*/
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)
{
unsigned int i, good, version_good;
unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
/*
* If these checks fail then either the message in publicly invalid, or
* we've been called incorrectly. We can fail immediately.
*/
if (flen < RSA_PKCS1_PADDING_SIZE + SSL_MAX_MASTER_KEY_LENGTH
|| tlen < SSL_MAX_MASTER_KEY_LENGTH) {
ERR_raise(ERR_LIB_RSA, RSA_R_PKCS_DECODING_ERROR);
return -1;
}
/*
* Generate a random premaster secret to use in the event that we fail
* to decrypt.
*/
if (RAND_priv_bytes(rand_premaster_secret,
sizeof(rand_premaster_secret)) <= 0) {
ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);
return -1;
}
good = constant_time_is_zero(from[0]);
good &= constant_time_eq(from[1], 2);
/* Check we have the expected padding data */
for (i = 2; i < flen - SSL_MAX_MASTER_KEY_LENGTH - 1; i++)
good &= ~constant_time_is_zero_8(from[i]);
good &= constant_time_is_zero_8(from[flen - SSL_MAX_MASTER_KEY_LENGTH - 1]);
/*
* If the version in the decrypted pre-master secret is correct then
* version_good will be 0xff, otherwise it'll be zero. The
* Klima-Pokorny-Rosa extension of Bleichenbacher's attack
* (http://eprint.iacr.org/2003/052/) exploits the version number
* check as a "bad version oracle". Thus version checks are done in
* constant time and are treated like any other decryption error.
*/
version_good =
constant_time_eq(from[flen - SSL_MAX_MASTER_KEY_LENGTH],
(client_version >> 8) & 0xff);
version_good &=
constant_time_eq(from[flen - SSL_MAX_MASTER_KEY_LENGTH + 1],
client_version & 0xff);
/*
* The premaster secret must contain the same version number as the
* ClientHello to detect version rollback attacks (strangely, the
* protocol does not offer such protection for DH ciphersuites).
* However, buggy clients exist that send the negotiated protocol
* version instead if the server does not support the requested
* protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set then we tolerate
* such clients. In that case alt_version will be non-zero and set to
* the negotiated version.
*/
if (alt_version > 0) {
unsigned int workaround_good;
workaround_good =
constant_time_eq(from[flen - SSL_MAX_MASTER_KEY_LENGTH],
(alt_version >> 8) & 0xff);
workaround_good &=
constant_time_eq(from[flen - SSL_MAX_MASTER_KEY_LENGTH + 1],
alt_version & 0xff);
version_good |= workaround_good;
}
good &= version_good;
/*
* Now copy the result over to the to buffer if good, or random data if
* not good.
*/
for (i = 0; i < SSL_MAX_MASTER_KEY_LENGTH; i++) {
to[i] =
constant_time_select_8(good,
from[flen - SSL_MAX_MASTER_KEY_LENGTH + i],
rand_premaster_secret[i]);
}
/*
* We must not leak whether a decryption failure occurs because of
* Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
* section 7.4.7.1). The code follows that advice of the TLS RFC and
* generates a random premaster secret for the case that the decrypt
* fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
* So, whether we actually succeeded or not, return success.
*/
return SSL_MAX_MASTER_KEY_LENGTH;
}
+185 -49
View File
@@ -1,5 +1,5 @@
/*
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
* Copyright 1995-2019 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
@@ -14,68 +14,199 @@
#include <openssl/objects.h>
#include <openssl/x509.h>
#include "crypto/x509.h"
#ifndef OPENSSL_NO_MD2
# include <openssl/md2.h> /* uses MD2_DIGEST_LENGTH */
#endif
#ifndef OPENSSL_NO_MD5
# include <openssl/md5.h> /* uses MD5_DIGEST_LENGTH */
#endif
#ifndef OPENSSL_NO_MDC2
# include <openssl/mdc2.h> /* uses MDC2_DIGEST_LENGTH */
#endif
#include <openssl/sha.h> /* uses SHA???_DIGEST_LENGTH */
#include "rsa_local.h"
/*
* The general purpose ASN1 code is not available inside the FIPS provider.
* To remove the dependency RSASSA-PKCS1-v1_5 DigestInfo encodings can be
* treated as a special case by pregenerating the required ASN1 encoding.
* This encoding will also be shared by the default provider.
*
* The EMSA-PKCS1-v1_5 encoding method includes an ASN.1 value of type
* DigestInfo, where the type DigestInfo has the syntax
*
* DigestInfo ::= SEQUENCE {
* digestAlgorithm DigestAlgorithm,
* digest OCTET STRING
* }
*
* DigestAlgorithm ::= AlgorithmIdentifier {
* {PKCS1-v1-5DigestAlgorithms}
* }
*
* The AlgorithmIdentifier is a sequence containing the digest OID and
* parameters (a value of type NULL).
*
* The ENCODE_DIGESTINFO_SHA() and ENCODE_DIGESTINFO_MD() macros define an
* initialized array containing the DER encoded DigestInfo for the specified
* SHA or MD digest. The content of the OCTET STRING is not included.
* |name| is the digest name.
* |n| is last byte in the encoded OID for the digest.
* |sz| is the digest length in bytes. It must not be greater than 110.
*/
#define ASN1_SEQUENCE 0x30
#define ASN1_OCTET_STRING 0x04
#define ASN1_NULL 0x05
#define ASN1_OID 0x06
/* SHA OIDs are of the form: (2 16 840 1 101 3 4 2 |n|) */
#define ENCODE_DIGESTINFO_SHA(name, n, sz) \
static const unsigned char digestinfo_##name##_der[] = { \
ASN1_SEQUENCE, 0x11 + sz, \
ASN1_SEQUENCE, 0x0d, \
ASN1_OID, 0x09, 2 * 40 + 16, 0x86, 0x48, 1, 101, 3, 4, 2, n, \
ASN1_NULL, 0x00, \
ASN1_OCTET_STRING, sz \
};
/* MD2 and MD5 OIDs are of the form: (1 2 840 113549 2 |n|) */
#define ENCODE_DIGESTINFO_MD(name, n, sz) \
static const unsigned char digestinfo_##name##_der[] = { \
ASN1_SEQUENCE, 0x10 + sz, \
ASN1_SEQUENCE, 0x0c, \
ASN1_OID, 0x08, 1 * 40 + 2, 0x86, 0x48, 0x86, 0xf7, 0x0d, 2, n, \
ASN1_NULL, 0x00, \
ASN1_OCTET_STRING, sz \
};
#ifndef FIPS_MODE
# ifndef OPENSSL_NO_MD2
ENCODE_DIGESTINFO_MD(md2, 0x02, MD2_DIGEST_LENGTH)
# endif
# ifndef OPENSSL_NO_MD5
ENCODE_DIGESTINFO_MD(md5, 0x05, MD5_DIGEST_LENGTH)
# endif
# ifndef OPENSSL_NO_MDC2
/* MDC-2 (2 5 8 3 101) */
static const unsigned char digestinfo_mdc2_der[] = {
ASN1_SEQUENCE, 0x0c + MDC2_DIGEST_LENGTH,
ASN1_SEQUENCE, 0x08,
ASN1_OID, 0x04, 2 * 40 + 5, 8, 3, 101,
ASN1_NULL, 0x00,
ASN1_OCTET_STRING, MDC2_DIGEST_LENGTH
};
# endif
/* SHA-1 (1 3 14 3 2 26) */
static const unsigned char digestinfo_sha1_der[] = {
ASN1_SEQUENCE, 0x0d + SHA_DIGEST_LENGTH,
ASN1_SEQUENCE, 0x09,
ASN1_OID, 0x05, 1 * 40 + 3, 14, 3, 2, 26,
ASN1_NULL, 0x00,
ASN1_OCTET_STRING, SHA_DIGEST_LENGTH
};
#endif /* FIPS_MODE */
ENCODE_DIGESTINFO_SHA(sha256, 0x01, SHA256_DIGEST_LENGTH)
ENCODE_DIGESTINFO_SHA(sha384, 0x02, SHA384_DIGEST_LENGTH)
ENCODE_DIGESTINFO_SHA(sha512, 0x03, SHA512_DIGEST_LENGTH)
ENCODE_DIGESTINFO_SHA(sha224, 0x04, SHA224_DIGEST_LENGTH)
ENCODE_DIGESTINFO_SHA(sha512_224, 0x05, SHA224_DIGEST_LENGTH)
ENCODE_DIGESTINFO_SHA(sha512_256, 0x06, SHA256_DIGEST_LENGTH)
ENCODE_DIGESTINFO_SHA(sha3_224, 0x07, SHA224_DIGEST_LENGTH)
ENCODE_DIGESTINFO_SHA(sha3_256, 0x08, SHA256_DIGEST_LENGTH)
ENCODE_DIGESTINFO_SHA(sha3_384, 0x09, SHA384_DIGEST_LENGTH)
ENCODE_DIGESTINFO_SHA(sha3_512, 0x0a, SHA512_DIGEST_LENGTH)
#define MD_CASE(name) \
case NID_##name: \
*len = sizeof(digestinfo_##name##_der); \
return digestinfo_##name##_der;
static const unsigned char *digestinfo_encoding(int nid, size_t *len)
{
switch (nid) {
#ifndef FIPS_MODE
# ifndef OPENSSL_NO_MDC2
MD_CASE(mdc2)
# endif
# ifndef OPENSSL_NO_MD2
MD_CASE(md2)
# endif
# ifndef OPENSSL_NO_MD5
MD_CASE(md5)
# endif
MD_CASE(sha1)
#endif /* FIPS_MODE */
MD_CASE(sha224)
MD_CASE(sha256)
MD_CASE(sha384)
MD_CASE(sha512)
MD_CASE(sha512_224)
MD_CASE(sha512_256)
MD_CASE(sha3_224)
MD_CASE(sha3_256)
MD_CASE(sha3_384)
MD_CASE(sha3_512)
default:
return NULL;
}
}
/* Size of an SSL signature: MD5+SHA1 */
#define SSL_SIG_LENGTH 36
/*
* encode_pkcs1 encodes a DigestInfo prefix of hash |type| and digest |m|, as
* Encodes a DigestInfo prefix of hash |type| and digest |m|, as
* described in EMSA-PKCS1-v1_5-ENCODE, RFC 3447 section 9.2 step 2. This
* encodes the DigestInfo (T and tLen) but does not add the padding.
*
* On success, it returns one and sets |*out| to a newly allocated buffer
* containing the result and |*out_len| to its length. The caller must free
* |*out| with |OPENSSL_free|. Otherwise, it returns zero.
* |*out| with OPENSSL_free(). Otherwise, it returns zero.
*/
static int encode_pkcs1(unsigned char **out, int *out_len, int type,
const unsigned char *m, unsigned int m_len)
static int encode_pkcs1(unsigned char **out, size_t *out_len, int type,
const unsigned char *m, size_t m_len)
{
X509_SIG sig;
X509_ALGOR algor;
ASN1_TYPE parameter;
ASN1_OCTET_STRING digest;
uint8_t *der = NULL;
int len;
size_t di_prefix_len, dig_info_len;
const unsigned char *di_prefix;
unsigned char *dig_info;
sig.algor = &algor;
sig.algor->algorithm = OBJ_nid2obj(type);
if (sig.algor->algorithm == NULL) {
if (type == NID_undef) {
RSAerr(RSA_F_ENCODE_PKCS1, RSA_R_UNKNOWN_ALGORITHM_TYPE);
return 0;
}
if (OBJ_length(sig.algor->algorithm) == 0) {
di_prefix = digestinfo_encoding(type, &di_prefix_len);
if (di_prefix == NULL) {
RSAerr(RSA_F_ENCODE_PKCS1,
RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
return 0;
}
parameter.type = V_ASN1_NULL;
parameter.value.ptr = NULL;
sig.algor->parameter = &parameter;
sig.digest = &digest;
sig.digest->data = (unsigned char *)m;
sig.digest->length = m_len;
len = i2d_X509_SIG(&sig, &der);
if (len < 0)
dig_info_len = di_prefix_len + m_len;
dig_info = OPENSSL_malloc(dig_info_len);
if (dig_info == NULL) {
RSAerr(RSA_F_ENCODE_PKCS1, ERR_R_MALLOC_FAILURE);
return 0;
}
memcpy(dig_info, di_prefix, di_prefix_len);
memcpy(dig_info + di_prefix_len, m, m_len);
*out = der;
*out_len = len;
*out = dig_info;
*out_len = dig_info_len;
return 1;
}
int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
unsigned char *sigret, unsigned int *siglen, RSA *rsa)
{
int encrypt_len, encoded_len = 0, ret = 0;
int encrypt_len, ret = 0;
size_t encoded_len = 0;
unsigned char *tmps = NULL;
const unsigned char *encoded = NULL;
if (rsa->meth->rsa_sign) {
if (rsa->meth->rsa_sign != NULL)
return rsa->meth->rsa_sign(type, m, m_len, sigret, siglen, rsa);
}
/* Compute the encoded digest. */
if (type == NID_md5_sha1) {
@@ -96,11 +227,11 @@ int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
encoded = tmps;
}
if (encoded_len > RSA_size(rsa) - RSA_PKCS1_PADDING_SIZE) {
if (encoded_len + RSA_PKCS1_PADDING_SIZE > (size_t)RSA_size(rsa)) {
RSAerr(RSA_F_RSA_SIGN, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
goto err;
}
encrypt_len = RSA_private_encrypt(encoded_len, encoded, sigret, rsa,
encrypt_len = RSA_private_encrypt((int)encoded_len, encoded, sigret, rsa,
RSA_PKCS1_PADDING);
if (encrypt_len <= 0)
goto err;
@@ -109,23 +240,25 @@ int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
ret = 1;
err:
OPENSSL_clear_free(tmps, (size_t)encoded_len);
OPENSSL_clear_free(tmps, encoded_len);
return ret;
}
/*
* int_rsa_verify verifies an RSA signature in |sigbuf| using |rsa|. It may be
* called in two modes. If |rm| is NULL, it verifies the signature for digest
* |m|. Otherwise, it recovers the digest from the signature, writing the digest
* to |rm| and the length to |*prm_len|. |type| is the NID of the digest
* algorithm to use. It returns one on successful verification and zero
* otherwise.
* Verify an RSA signature in |sigbuf| using |rsa|.
* |type| is the NID of the digest algorithm to use.
* If |rm| is NULL, it verifies the signature for digest |m|, otherwise
* it recovers the digest from the signature, writing the digest to |rm| and
* the length to |*prm_len|.
*
* It returns one on successful verification or zero otherwise.
*/
int int_rsa_verify(int type, const unsigned char *m, unsigned int m_len,
unsigned char *rm, size_t *prm_len,
const unsigned char *sigbuf, size_t siglen, RSA *rsa)
{
int decrypt_len, ret = 0, encoded_len = 0;
int len, ret = 0;
size_t decrypt_len, encoded_len = 0;
unsigned char *decrypt_buf = NULL, *encoded = NULL;
if (siglen != (size_t)RSA_size(rsa)) {
@@ -140,10 +273,11 @@ int int_rsa_verify(int type, const unsigned char *m, unsigned int m_len,
goto err;
}
decrypt_len = RSA_public_decrypt((int)siglen, sigbuf, decrypt_buf, rsa,
RSA_PKCS1_PADDING);
if (decrypt_len <= 0)
len = RSA_public_decrypt((int)siglen, sigbuf, decrypt_buf, rsa,
RSA_PKCS1_PADDING);
if (len <= 0)
goto err;
decrypt_len = len;
if (type == NID_md5_sha1) {
/*
@@ -203,8 +337,11 @@ int int_rsa_verify(int type, const unsigned char *m, unsigned int m_len,
goto err;
}
m_len = EVP_MD_size(md);
if (m_len > (size_t)decrypt_len) {
len = EVP_MD_size(md);
if (len <= 0)
goto err;
m_len = (unsigned int)len;
if (m_len > decrypt_len) {
RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_INVALID_DIGEST_LENGTH);
goto err;
}
@@ -216,7 +353,7 @@ int int_rsa_verify(int type, const unsigned char *m, unsigned int m_len,
goto err;
if (encoded_len != decrypt_len
|| memcmp(encoded, decrypt_buf, encoded_len) != 0) {
|| memcmp(encoded, decrypt_buf, encoded_len) != 0) {
RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
goto err;
}
@@ -231,7 +368,7 @@ int int_rsa_verify(int type, const unsigned char *m, unsigned int m_len,
ret = 1;
err:
OPENSSL_clear_free(encoded, (size_t)encoded_len);
OPENSSL_clear_free(encoded, encoded_len);
OPENSSL_clear_free(decrypt_buf, siglen);
return ret;
}
@@ -240,9 +377,8 @@ int RSA_verify(int type, const unsigned char *m, unsigned int m_len,
const unsigned char *sigbuf, unsigned int siglen, RSA *rsa)
{
if (rsa->meth->rsa_verify) {
if (rsa->meth->rsa_verify != NULL)
return rsa->meth->rsa_verify(type, m, m_len, sigbuf, siglen, rsa);
}
return int_rsa_verify(type, m, m_len, NULL, NULL, sigbuf, siglen, rsa);
}
+15 -12
View File
@@ -75,38 +75,41 @@ int rsa_check_crt_components(const RSA *rsa, BN_CTX *ctx)
* See SP800-5bBr1 6.4.1.2.1 Part 5 (c) & (g) - used for both p and q.
*
* (√2)(2^(nbits/2 - 1) = (√2/2)(2^(nbits/2))
* √2/2 = 0.707106781186547524400 = 0.B504F333F9DE6484597D8
* 0.B504F334 gives an approximation to 11 decimal places.
* The range is then from
* 0xB504F334_0000.......................000 to
* 0xFFFFFFFF_FFFF.......................FFF
*/
int rsa_check_prime_factor_range(const BIGNUM *p, int nbits, BN_CTX *ctx)
{
int ret = 0;
BIGNUM *tmp, *low;
BIGNUM *low;
int shift;
nbits >>= 1;
shift = nbits - BN_num_bits(&bn_inv_sqrt_2);
/* Upper bound check */
if (BN_num_bits(p) != nbits)
return 0;
BN_CTX_start(ctx);
tmp = BN_CTX_get(ctx);
low = BN_CTX_get(ctx);
if (low == NULL)
goto err;
/* set low = (√2)(2^(nbits/2 - 1) */
if (low == NULL || !BN_set_word(tmp, 0xB504F334))
if (!BN_copy(low, &bn_inv_sqrt_2))
goto err;
if (nbits >= 32) {
if (!BN_lshift(low, tmp, nbits - 32))
if (shift >= 0) {
/*
* We don't have all the bits. bn_inv_sqrt_2 contains a rounded up
* value, so there is a very low probability that we'll reject a valid
* value.
*/
if (!BN_lshift(low, low, shift))
goto err;
} else if (!BN_rshift(low, tmp, 32 - nbits)) {
} else if (!BN_rshift(low, low, -shift)) {
goto err;
}
if (BN_cmp(p, low) < 0)
if (BN_cmp(p, low) <= 0)
goto err;
ret = 1;
err:
+2
View File
@@ -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) {
+2
View File
@@ -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: