2019-12-01 22:01:50 +08:00
|
|
|
/*
|
2022-05-03 18:52:38 +08:00
|
|
|
* Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
|
2019-12-01 22:01:50 +08:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RSA low level APIs are deprecated for public use, but still ok for
|
|
|
|
* internal use.
|
|
|
|
*/
|
|
|
|
#include "internal/deprecated.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <openssl/crypto.h>
|
2020-06-21 07:21:19 +08:00
|
|
|
#include <openssl/core_dispatch.h>
|
2019-12-01 22:01:50 +08:00
|
|
|
#include <openssl/core_names.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/rsa.h>
|
|
|
|
#include <openssl/params.h>
|
|
|
|
#include <openssl/evp.h>
|
2021-02-06 00:40:42 +08:00
|
|
|
#include <openssl/proverr.h>
|
2019-12-01 22:01:50 +08:00
|
|
|
#include "internal/cryptlib.h"
|
|
|
|
#include "internal/nelem.h"
|
|
|
|
#include "internal/sizes.h"
|
|
|
|
#include "crypto/rsa.h"
|
signature: add FIPS error state handling
The functions that check for the provider being runnable are: newctx, dupctx,
sign init, sign, verify init, verify, verify recover init, verify recover,
digest sign init, digest sign final, digest verify init and digest verify final.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/12801)
2020-09-07 11:44:17 +08:00
|
|
|
#include "prov/providercommon.h"
|
2019-12-01 22:01:50 +08:00
|
|
|
#include "prov/implementations.h"
|
|
|
|
#include "prov/provider_ctx.h"
|
2020-03-31 23:15:17 +08:00
|
|
|
#include "prov/der_rsa.h"
|
2020-09-04 15:55:28 +08:00
|
|
|
#include "prov/securitycheck.h"
|
2020-08-29 10:25:54 +08:00
|
|
|
|
|
|
|
#define RSA_DEFAULT_DIGEST_NAME OSSL_DIGEST_NAME_SHA1
|
2019-12-01 22:01:50 +08:00
|
|
|
|
2020-06-21 07:19:16 +08:00
|
|
|
static OSSL_FUNC_signature_newctx_fn rsa_newctx;
|
|
|
|
static OSSL_FUNC_signature_sign_init_fn rsa_sign_init;
|
|
|
|
static OSSL_FUNC_signature_verify_init_fn rsa_verify_init;
|
|
|
|
static OSSL_FUNC_signature_verify_recover_init_fn rsa_verify_recover_init;
|
|
|
|
static OSSL_FUNC_signature_sign_fn rsa_sign;
|
|
|
|
static OSSL_FUNC_signature_verify_fn rsa_verify;
|
|
|
|
static OSSL_FUNC_signature_verify_recover_fn rsa_verify_recover;
|
|
|
|
static OSSL_FUNC_signature_digest_sign_init_fn rsa_digest_sign_init;
|
|
|
|
static OSSL_FUNC_signature_digest_sign_update_fn rsa_digest_signverify_update;
|
|
|
|
static OSSL_FUNC_signature_digest_sign_final_fn rsa_digest_sign_final;
|
|
|
|
static OSSL_FUNC_signature_digest_verify_init_fn rsa_digest_verify_init;
|
|
|
|
static OSSL_FUNC_signature_digest_verify_update_fn rsa_digest_signverify_update;
|
|
|
|
static OSSL_FUNC_signature_digest_verify_final_fn rsa_digest_verify_final;
|
|
|
|
static OSSL_FUNC_signature_freectx_fn rsa_freectx;
|
|
|
|
static OSSL_FUNC_signature_dupctx_fn rsa_dupctx;
|
|
|
|
static OSSL_FUNC_signature_get_ctx_params_fn rsa_get_ctx_params;
|
|
|
|
static OSSL_FUNC_signature_gettable_ctx_params_fn rsa_gettable_ctx_params;
|
|
|
|
static OSSL_FUNC_signature_set_ctx_params_fn rsa_set_ctx_params;
|
|
|
|
static OSSL_FUNC_signature_settable_ctx_params_fn rsa_settable_ctx_params;
|
|
|
|
static OSSL_FUNC_signature_get_ctx_md_params_fn rsa_get_ctx_md_params;
|
|
|
|
static OSSL_FUNC_signature_gettable_ctx_md_params_fn rsa_gettable_ctx_md_params;
|
|
|
|
static OSSL_FUNC_signature_set_ctx_md_params_fn rsa_set_ctx_md_params;
|
|
|
|
static OSSL_FUNC_signature_settable_ctx_md_params_fn rsa_settable_ctx_md_params;
|
2019-12-01 22:01:50 +08:00
|
|
|
|
|
|
|
static OSSL_ITEM padding_item[] = {
|
2020-05-26 11:53:07 +08:00
|
|
|
{ RSA_PKCS1_PADDING, OSSL_PKEY_RSA_PAD_MODE_PKCSV15 },
|
|
|
|
{ RSA_NO_PADDING, OSSL_PKEY_RSA_PAD_MODE_NONE },
|
|
|
|
{ RSA_X931_PADDING, OSSL_PKEY_RSA_PAD_MODE_X931 },
|
|
|
|
{ RSA_PKCS1_PSS_PADDING, OSSL_PKEY_RSA_PAD_MODE_PSS },
|
2019-12-01 22:01:50 +08:00
|
|
|
{ 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* What's passed as an actual key is defined by the KEYMGMT interface.
|
|
|
|
* We happen to know that our KEYMGMT simply passes RSA structures, so
|
|
|
|
* we use that here too.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct {
|
2020-10-15 17:55:50 +08:00
|
|
|
OSSL_LIB_CTX *libctx;
|
2020-05-07 03:44:58 +08:00
|
|
|
char *propq;
|
2019-12-01 22:01:50 +08:00
|
|
|
RSA *rsa;
|
2020-05-07 14:51:09 +08:00
|
|
|
int operation;
|
2019-12-01 22:01:50 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Flag to determine if the hash function can be changed (1) or not (0)
|
|
|
|
* Because it's dangerous to change during a DigestSign or DigestVerify
|
|
|
|
* operation, this flag is cleared by their Init function, and set again
|
|
|
|
* by their Final function.
|
|
|
|
*/
|
|
|
|
unsigned int flag_allow_md : 1;
|
2021-01-30 00:02:32 +08:00
|
|
|
unsigned int mgf1_md_set : 1;
|
2019-12-01 22:01:50 +08:00
|
|
|
|
|
|
|
/* main digest */
|
|
|
|
EVP_MD *md;
|
|
|
|
EVP_MD_CTX *mdctx;
|
|
|
|
int mdnid;
|
|
|
|
char mdname[OSSL_MAX_NAME_SIZE]; /* Purely informational */
|
|
|
|
|
|
|
|
/* RSA padding mode */
|
|
|
|
int pad_mode;
|
|
|
|
/* message digest for MGF1 */
|
|
|
|
EVP_MD *mgf1_md;
|
2021-01-30 00:02:32 +08:00
|
|
|
int mgf1_mdnid;
|
2019-12-01 22:01:50 +08:00
|
|
|
char mgf1_mdname[OSSL_MAX_NAME_SIZE]; /* Purely informational */
|
|
|
|
/* PSS salt length */
|
|
|
|
int saltlen;
|
|
|
|
/* Minimum salt length or -1 if no PSS parameter restriction */
|
|
|
|
int min_saltlen;
|
|
|
|
|
|
|
|
/* Temp buffer */
|
|
|
|
unsigned char *tbuf;
|
|
|
|
|
|
|
|
} PROV_RSA_CTX;
|
|
|
|
|
2021-01-30 00:02:32 +08:00
|
|
|
/* True if PSS parameters are restricted */
|
|
|
|
#define rsa_pss_restricted(prsactx) (prsactx->min_saltlen != -1)
|
|
|
|
|
2019-12-01 22:01:50 +08:00
|
|
|
static size_t rsa_get_md_size(const PROV_RSA_CTX *prsactx)
|
|
|
|
{
|
|
|
|
if (prsactx->md != NULL)
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
return EVP_MD_get_size(prsactx->md);
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:02:32 +08:00
|
|
|
static int rsa_check_padding(const PROV_RSA_CTX *prsactx,
|
|
|
|
const char *mdname, const char *mgf1_mdname,
|
|
|
|
int mdnid)
|
2019-12-01 22:01:50 +08:00
|
|
|
{
|
2021-10-26 15:16:18 +08:00
|
|
|
switch (prsactx->pad_mode) {
|
2021-01-30 00:02:32 +08:00
|
|
|
case RSA_NO_PADDING:
|
2021-07-13 21:28:24 +08:00
|
|
|
if (mdname != NULL || mdnid != NID_undef) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2021-01-30 00:02:32 +08:00
|
|
|
case RSA_X931_PADDING:
|
|
|
|
if (RSA_X931_hash_id(mdnid) == -1) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_X931_DIGEST);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RSA_PKCS1_PSS_PADDING:
|
|
|
|
if (rsa_pss_restricted(prsactx))
|
|
|
|
if ((mdname != NULL && !EVP_MD_is_a(prsactx->md, mdname))
|
|
|
|
|| (mgf1_mdname != NULL
|
|
|
|
&& !EVP_MD_is_a(prsactx->mgf1_md, mgf1_mdname))) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2019-12-01 22:01:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:02:32 +08:00
|
|
|
static int rsa_check_parameters(PROV_RSA_CTX *prsactx, int min_saltlen)
|
2020-05-03 12:02:52 +08:00
|
|
|
{
|
|
|
|
if (prsactx->pad_mode == RSA_PKCS1_PSS_PADDING) {
|
|
|
|
int max_saltlen;
|
|
|
|
|
|
|
|
/* See if minimum salt length exceeds maximum possible */
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
max_saltlen = RSA_size(prsactx->rsa) - EVP_MD_get_size(prsactx->md);
|
2020-05-03 12:02:52 +08:00
|
|
|
if ((RSA_bits(prsactx->rsa) & 0x7) == 1)
|
|
|
|
max_saltlen--;
|
2021-01-30 00:02:32 +08:00
|
|
|
if (min_saltlen < 0 || min_saltlen > max_saltlen) {
|
2020-05-03 12:02:52 +08:00
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-30 00:02:32 +08:00
|
|
|
prsactx->min_saltlen = min_saltlen;
|
2020-05-03 12:02:52 +08:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-05-07 03:44:58 +08:00
|
|
|
static void *rsa_newctx(void *provctx, const char *propq)
|
2019-12-01 22:01:50 +08:00
|
|
|
{
|
2020-05-03 12:02:52 +08:00
|
|
|
PROV_RSA_CTX *prsactx = NULL;
|
|
|
|
char *propq_copy = NULL;
|
2019-12-01 22:01:50 +08:00
|
|
|
|
signature: add FIPS error state handling
The functions that check for the provider being runnable are: newctx, dupctx,
sign init, sign, verify init, verify, verify recover init, verify recover,
digest sign init, digest sign final, digest verify init and digest verify final.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/12801)
2020-09-07 11:44:17 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return NULL;
|
|
|
|
|
2020-05-03 12:02:52 +08:00
|
|
|
if ((prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX))) == NULL
|
|
|
|
|| (propq != NULL
|
|
|
|
&& (propq_copy = OPENSSL_strdup(propq)) == NULL)) {
|
|
|
|
OPENSSL_free(prsactx);
|
2019-12-01 22:01:50 +08:00
|
|
|
return NULL;
|
2020-05-03 12:02:52 +08:00
|
|
|
}
|
2019-12-01 22:01:50 +08:00
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
prsactx->libctx = PROV_LIBCTX_OF(provctx);
|
2019-12-01 22:01:50 +08:00
|
|
|
prsactx->flag_allow_md = 1;
|
2020-05-03 12:02:52 +08:00
|
|
|
prsactx->propq = propq_copy;
|
signature: Clamp PSS salt len to MD len
FIPS 186-4 section 5 "The RSA Digital Signature Algorithm", subsection
5.5 "PKCS #1" says: "For RSASSA-PSS […] the length (in bytes) of the
salt (sLen) shall satisfy 0 <= sLen <= hLen, where hLen is the length of
the hash function output block (in bytes)."
Introduce a new option RSA_PSS_SALTLEN_AUTO_DIGEST_MAX and make it the
default. The new value will behave like RSA_PSS_SALTLEN_AUTO, but will
not use more than the digest length when signing, so that FIPS 186-4 is
not violated. This value has two advantages when compared with
RSA_PSS_SALTLEN_DIGEST: (1) It will continue to do auto-detection when
verifying signatures for maximum compatibility, where
RSA_PSS_SALTLEN_DIGEST would fail for other digest sizes. (2) It will
work for combinations where the maximum salt length is smaller than the
digest size, which typically happens with large digest sizes (e.g.,
SHA-512) and small RSA keys.
J.-S. Coron shows in "Optimal Security Proofs for PSS and Other
Signature Schemes. Advances in Cryptology – Eurocrypt 2002, volume 2332
of Lecture Notes in Computer Science, pp. 272 – 287. Springer Verlag,
2002." that longer salts than the output size of modern hash functions
do not increase security: "For example,for an application in which at
most one billion signatures will be generated, k0 = 30 bits of random
salt are actually sufficient to guarantee the same level of security as
RSA, and taking a larger salt does not increase the security level."
Signed-off-by: Clemens Lang <cllang@redhat.com>
Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/19724)
2022-11-18 19:35:33 +08:00
|
|
|
/* Maximum up to digest length for sign, auto for verify */
|
|
|
|
prsactx->saltlen = RSA_PSS_SALTLEN_AUTO_DIGEST_MAX;
|
2021-11-19 22:16:53 +08:00
|
|
|
prsactx->min_saltlen = -1;
|
2019-12-01 22:01:50 +08:00
|
|
|
return prsactx;
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:02:32 +08:00
|
|
|
static int rsa_pss_compute_saltlen(PROV_RSA_CTX *ctx)
|
|
|
|
{
|
|
|
|
int saltlen = ctx->saltlen;
|
signature: Clamp PSS salt len to MD len
FIPS 186-4 section 5 "The RSA Digital Signature Algorithm", subsection
5.5 "PKCS #1" says: "For RSASSA-PSS […] the length (in bytes) of the
salt (sLen) shall satisfy 0 <= sLen <= hLen, where hLen is the length of
the hash function output block (in bytes)."
Introduce a new option RSA_PSS_SALTLEN_AUTO_DIGEST_MAX and make it the
default. The new value will behave like RSA_PSS_SALTLEN_AUTO, but will
not use more than the digest length when signing, so that FIPS 186-4 is
not violated. This value has two advantages when compared with
RSA_PSS_SALTLEN_DIGEST: (1) It will continue to do auto-detection when
verifying signatures for maximum compatibility, where
RSA_PSS_SALTLEN_DIGEST would fail for other digest sizes. (2) It will
work for combinations where the maximum salt length is smaller than the
digest size, which typically happens with large digest sizes (e.g.,
SHA-512) and small RSA keys.
J.-S. Coron shows in "Optimal Security Proofs for PSS and Other
Signature Schemes. Advances in Cryptology – Eurocrypt 2002, volume 2332
of Lecture Notes in Computer Science, pp. 272 – 287. Springer Verlag,
2002." that longer salts than the output size of modern hash functions
do not increase security: "For example,for an application in which at
most one billion signatures will be generated, k0 = 30 bits of random
salt are actually sufficient to guarantee the same level of security as
RSA, and taking a larger salt does not increase the security level."
Signed-off-by: Clemens Lang <cllang@redhat.com>
Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/19724)
2022-11-18 19:35:33 +08:00
|
|
|
int saltlenMax = -1;
|
|
|
|
|
|
|
|
/* FIPS 186-4 section 5 "The RSA Digital Signature Algorithm", subsection
|
|
|
|
* 5.5 "PKCS #1" says: "For RSASSA-PSS […] the length (in bytes) of the
|
|
|
|
* salt (sLen) shall satisfy 0 <= sLen <= hLen, where hLen is the length of
|
|
|
|
* the hash function output block (in bytes)."
|
|
|
|
*
|
|
|
|
* Provide a way to use at most the digest length, so that the default does
|
|
|
|
* not violate FIPS 186-4. */
|
2021-01-30 00:02:32 +08:00
|
|
|
if (saltlen == RSA_PSS_SALTLEN_DIGEST) {
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
saltlen = EVP_MD_get_size(ctx->md);
|
signature: Clamp PSS salt len to MD len
FIPS 186-4 section 5 "The RSA Digital Signature Algorithm", subsection
5.5 "PKCS #1" says: "For RSASSA-PSS […] the length (in bytes) of the
salt (sLen) shall satisfy 0 <= sLen <= hLen, where hLen is the length of
the hash function output block (in bytes)."
Introduce a new option RSA_PSS_SALTLEN_AUTO_DIGEST_MAX and make it the
default. The new value will behave like RSA_PSS_SALTLEN_AUTO, but will
not use more than the digest length when signing, so that FIPS 186-4 is
not violated. This value has two advantages when compared with
RSA_PSS_SALTLEN_DIGEST: (1) It will continue to do auto-detection when
verifying signatures for maximum compatibility, where
RSA_PSS_SALTLEN_DIGEST would fail for other digest sizes. (2) It will
work for combinations where the maximum salt length is smaller than the
digest size, which typically happens with large digest sizes (e.g.,
SHA-512) and small RSA keys.
J.-S. Coron shows in "Optimal Security Proofs for PSS and Other
Signature Schemes. Advances in Cryptology – Eurocrypt 2002, volume 2332
of Lecture Notes in Computer Science, pp. 272 – 287. Springer Verlag,
2002." that longer salts than the output size of modern hash functions
do not increase security: "For example,for an application in which at
most one billion signatures will be generated, k0 = 30 bits of random
salt are actually sufficient to guarantee the same level of security as
RSA, and taking a larger salt does not increase the security level."
Signed-off-by: Clemens Lang <cllang@redhat.com>
Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/19724)
2022-11-18 19:35:33 +08:00
|
|
|
} else if (saltlen == RSA_PSS_SALTLEN_AUTO_DIGEST_MAX) {
|
|
|
|
saltlen = RSA_PSS_SALTLEN_MAX;
|
|
|
|
saltlenMax = EVP_MD_get_size(ctx->md);
|
|
|
|
}
|
|
|
|
if (saltlen == RSA_PSS_SALTLEN_MAX || saltlen == RSA_PSS_SALTLEN_AUTO) {
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
saltlen = RSA_size(ctx->rsa) - EVP_MD_get_size(ctx->md) - 2;
|
2021-01-30 00:02:32 +08:00
|
|
|
if ((RSA_bits(ctx->rsa) & 0x7) == 1)
|
|
|
|
saltlen--;
|
signature: Clamp PSS salt len to MD len
FIPS 186-4 section 5 "The RSA Digital Signature Algorithm", subsection
5.5 "PKCS #1" says: "For RSASSA-PSS […] the length (in bytes) of the
salt (sLen) shall satisfy 0 <= sLen <= hLen, where hLen is the length of
the hash function output block (in bytes)."
Introduce a new option RSA_PSS_SALTLEN_AUTO_DIGEST_MAX and make it the
default. The new value will behave like RSA_PSS_SALTLEN_AUTO, but will
not use more than the digest length when signing, so that FIPS 186-4 is
not violated. This value has two advantages when compared with
RSA_PSS_SALTLEN_DIGEST: (1) It will continue to do auto-detection when
verifying signatures for maximum compatibility, where
RSA_PSS_SALTLEN_DIGEST would fail for other digest sizes. (2) It will
work for combinations where the maximum salt length is smaller than the
digest size, which typically happens with large digest sizes (e.g.,
SHA-512) and small RSA keys.
J.-S. Coron shows in "Optimal Security Proofs for PSS and Other
Signature Schemes. Advances in Cryptology – Eurocrypt 2002, volume 2332
of Lecture Notes in Computer Science, pp. 272 – 287. Springer Verlag,
2002." that longer salts than the output size of modern hash functions
do not increase security: "For example,for an application in which at
most one billion signatures will be generated, k0 = 30 bits of random
salt are actually sufficient to guarantee the same level of security as
RSA, and taking a larger salt does not increase the security level."
Signed-off-by: Clemens Lang <cllang@redhat.com>
Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/19724)
2022-11-18 19:35:33 +08:00
|
|
|
if (saltlenMax >= 0 && saltlen > saltlenMax)
|
|
|
|
saltlen = saltlenMax;
|
2021-01-30 00:02:32 +08:00
|
|
|
}
|
|
|
|
if (saltlen < 0) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
|
|
|
|
return -1;
|
|
|
|
} else if (saltlen < ctx->min_saltlen) {
|
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_PSS_SALTLEN_TOO_SMALL,
|
|
|
|
"minimum salt length: %d, actual salt length: %d",
|
|
|
|
ctx->min_saltlen, saltlen);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return saltlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char *rsa_generate_signature_aid(PROV_RSA_CTX *ctx,
|
|
|
|
unsigned char *aid_buf,
|
|
|
|
size_t buf_len,
|
|
|
|
size_t *aid_len)
|
|
|
|
{
|
|
|
|
WPACKET pkt;
|
|
|
|
unsigned char *aid = NULL;
|
|
|
|
int saltlen;
|
|
|
|
RSA_PSS_PARAMS_30 pss_params;
|
2021-04-14 22:35:28 +08:00
|
|
|
int ret;
|
2021-01-30 00:02:32 +08:00
|
|
|
|
|
|
|
if (!WPACKET_init_der(&pkt, aid_buf, buf_len)) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_CRYPTO_LIB);
|
2021-01-30 00:02:32 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-10-26 15:16:18 +08:00
|
|
|
switch (ctx->pad_mode) {
|
2021-04-14 22:35:28 +08:00
|
|
|
case RSA_PKCS1_PADDING:
|
|
|
|
ret = ossl_DER_w_algorithmIdentifier_MDWithRSAEncryption(&pkt, -1,
|
|
|
|
ctx->mdnid);
|
|
|
|
|
|
|
|
if (ret > 0) {
|
2021-01-30 00:02:32 +08:00
|
|
|
break;
|
2021-04-14 22:35:28 +08:00
|
|
|
} else if (ret == 0) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
ERR_raise_data(ERR_LIB_PROV, ERR_R_UNSUPPORTED,
|
|
|
|
"Algorithm ID generation - md NID: %d",
|
|
|
|
ctx->mdnid);
|
|
|
|
goto cleanup;
|
|
|
|
case RSA_PKCS1_PSS_PADDING:
|
|
|
|
saltlen = rsa_pss_compute_saltlen(ctx);
|
|
|
|
if (saltlen < 0)
|
2021-01-30 00:02:32 +08:00
|
|
|
goto cleanup;
|
2021-04-14 22:35:28 +08:00
|
|
|
if (!ossl_rsa_pss_params_30_set_defaults(&pss_params)
|
|
|
|
|| !ossl_rsa_pss_params_30_set_hashalg(&pss_params, ctx->mdnid)
|
|
|
|
|| !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params,
|
|
|
|
ctx->mgf1_mdnid)
|
|
|
|
|| !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
|
|
|
|
|| !ossl_DER_w_algorithmIdentifier_RSA_PSS(&pkt, -1,
|
|
|
|
RSA_FLAG_TYPE_RSASSAPSS,
|
|
|
|
&pss_params)) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERR_raise_data(ERR_LIB_PROV, ERR_R_UNSUPPORTED,
|
|
|
|
"Algorithm ID generation - pad mode: %d",
|
|
|
|
ctx->pad_mode);
|
|
|
|
goto cleanup;
|
2021-01-30 00:02:32 +08:00
|
|
|
}
|
|
|
|
if (WPACKET_finish(&pkt)) {
|
|
|
|
WPACKET_get_total_written(&pkt, aid_len);
|
|
|
|
aid = WPACKET_get_curr(&pkt);
|
|
|
|
}
|
|
|
|
cleanup:
|
|
|
|
WPACKET_cleanup(&pkt);
|
|
|
|
return aid;
|
|
|
|
}
|
2019-12-01 22:01:50 +08:00
|
|
|
|
|
|
|
static int rsa_setup_md(PROV_RSA_CTX *ctx, const char *mdname,
|
|
|
|
const char *mdprops)
|
|
|
|
{
|
2020-05-07 03:44:58 +08:00
|
|
|
if (mdprops == NULL)
|
|
|
|
mdprops = ctx->propq;
|
|
|
|
|
2019-12-01 22:01:50 +08:00
|
|
|
if (mdname != NULL) {
|
2020-08-29 10:25:54 +08:00
|
|
|
EVP_MD *md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);
|
2020-08-29 16:03:17 +08:00
|
|
|
int sha1_allowed = (ctx->operation != EVP_PKEY_OP_SIGN);
|
2021-04-10 00:26:34 +08:00
|
|
|
int md_nid = ossl_digest_rsa_sign_get_md_nid(ctx->libctx, md,
|
|
|
|
sha1_allowed);
|
2020-05-20 01:36:44 +08:00
|
|
|
size_t mdname_len = strlen(mdname);
|
2019-12-01 22:01:50 +08:00
|
|
|
|
2020-03-31 23:15:17 +08:00
|
|
|
if (md == NULL
|
2021-05-10 22:51:39 +08:00
|
|
|
|| md_nid <= 0
|
2021-01-30 00:02:32 +08:00
|
|
|
|| !rsa_check_padding(ctx, mdname, NULL, md_nid)
|
2020-05-20 01:36:44 +08:00
|
|
|
|| mdname_len >= sizeof(ctx->mdname)) {
|
2020-05-03 12:02:52 +08:00
|
|
|
if (md == NULL)
|
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
|
|
|
|
"%s could not be fetched", mdname);
|
2021-05-10 22:51:39 +08:00
|
|
|
if (md_nid <= 0)
|
2020-05-03 12:02:52 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
|
|
|
|
"digest=%s", mdname);
|
2020-05-20 01:36:44 +08:00
|
|
|
if (mdname_len >= sizeof(ctx->mdname))
|
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
|
|
|
|
"%s exceeds name buffer length", mdname);
|
2019-12-01 22:01:50 +08:00
|
|
|
EVP_MD_free(md);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-14 21:41:22 +08:00
|
|
|
if (!ctx->flag_allow_md) {
|
|
|
|
if (ctx->mdname[0] != '\0' && !EVP_MD_is_a(md, ctx->mdname)) {
|
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
|
|
|
|
"digest %s != %s", mdname, ctx->mdname);
|
|
|
|
EVP_MD_free(md);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EVP_MD_free(md);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:02:32 +08:00
|
|
|
if (!ctx->mgf1_md_set) {
|
|
|
|
if (!EVP_MD_up_ref(md)) {
|
|
|
|
EVP_MD_free(md);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EVP_MD_free(ctx->mgf1_md);
|
|
|
|
ctx->mgf1_md = md;
|
|
|
|
ctx->mgf1_mdnid = md_nid;
|
|
|
|
OPENSSL_strlcpy(ctx->mgf1_mdname, mdname, sizeof(ctx->mgf1_mdname));
|
|
|
|
}
|
|
|
|
|
2019-12-01 22:01:50 +08:00
|
|
|
EVP_MD_CTX_free(ctx->mdctx);
|
|
|
|
EVP_MD_free(ctx->md);
|
|
|
|
|
2020-03-31 23:15:17 +08:00
|
|
|
ctx->mdctx = NULL;
|
2019-12-01 22:01:50 +08:00
|
|
|
ctx->md = md;
|
|
|
|
ctx->mdnid = md_nid;
|
|
|
|
OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rsa_setup_mgf1_md(PROV_RSA_CTX *ctx, const char *mdname,
|
2020-05-07 03:44:58 +08:00
|
|
|
const char *mdprops)
|
2019-12-01 22:01:50 +08:00
|
|
|
{
|
2020-05-20 01:36:44 +08:00
|
|
|
size_t len;
|
2020-08-29 10:25:54 +08:00
|
|
|
EVP_MD *md = NULL;
|
2021-01-30 00:02:32 +08:00
|
|
|
int mdnid;
|
2020-05-20 01:36:44 +08:00
|
|
|
|
2020-05-07 03:44:58 +08:00
|
|
|
if (mdprops == NULL)
|
|
|
|
mdprops = ctx->propq;
|
|
|
|
|
2020-08-29 10:25:54 +08:00
|
|
|
if ((md = EVP_MD_fetch(ctx->libctx, mdname, mdprops)) == NULL) {
|
2020-05-03 12:02:52 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
|
|
|
|
"%s could not be fetched", mdname);
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
2020-05-03 12:02:52 +08:00
|
|
|
}
|
2020-08-29 16:03:17 +08:00
|
|
|
/* The default for mgf1 is SHA1 - so allow SHA1 */
|
2021-05-10 22:51:39 +08:00
|
|
|
if ((mdnid = ossl_digest_rsa_sign_get_md_nid(ctx->libctx, md, 1)) <= 0
|
2021-01-30 00:02:32 +08:00
|
|
|
|| !rsa_check_padding(ctx, NULL, mdname, mdnid)) {
|
2021-05-10 22:51:39 +08:00
|
|
|
if (mdnid <= 0)
|
2021-01-30 00:02:32 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
|
|
|
|
"digest=%s", mdname);
|
2020-08-29 10:25:54 +08:00
|
|
|
EVP_MD_free(md);
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-20 01:36:44 +08:00
|
|
|
len = OPENSSL_strlcpy(ctx->mgf1_mdname, mdname, sizeof(ctx->mgf1_mdname));
|
|
|
|
if (len >= sizeof(ctx->mgf1_mdname)) {
|
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
|
|
|
|
"%s exceeds name buffer length", mdname);
|
2021-01-30 00:02:32 +08:00
|
|
|
EVP_MD_free(md);
|
2020-05-20 01:36:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2019-12-01 22:01:50 +08:00
|
|
|
|
2021-01-30 00:02:32 +08:00
|
|
|
EVP_MD_free(ctx->mgf1_md);
|
|
|
|
ctx->mgf1_md = md;
|
|
|
|
ctx->mgf1_mdnid = mdnid;
|
|
|
|
ctx->mgf1_md_set = 1;
|
2019-12-01 22:01:50 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-03-02 20:43:36 +08:00
|
|
|
static int rsa_signverify_init(void *vprsactx, void *vrsa,
|
|
|
|
const OSSL_PARAM params[], int operation)
|
2020-05-03 12:02:52 +08:00
|
|
|
{
|
|
|
|
PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
|
|
|
|
|
2021-11-04 22:38:51 +08:00
|
|
|
if (!ossl_prov_is_running() || prsactx == NULL)
|
signature: add FIPS error state handling
The functions that check for the provider being runnable are: newctx, dupctx,
sign init, sign, verify init, verify, verify recover init, verify recover,
digest sign init, digest sign final, digest verify init and digest verify final.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/12801)
2020-09-07 11:44:17 +08:00
|
|
|
return 0;
|
|
|
|
|
2021-11-04 22:38:51 +08:00
|
|
|
if (vrsa == NULL && prsactx->rsa == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
|
2020-05-03 12:02:52 +08:00
|
|
|
return 0;
|
2021-11-04 22:38:51 +08:00
|
|
|
}
|
2020-05-03 12:02:52 +08:00
|
|
|
|
2021-11-04 22:38:51 +08:00
|
|
|
if (vrsa != NULL) {
|
|
|
|
if (!ossl_rsa_check_key(prsactx->libctx, vrsa, operation))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!RSA_up_ref(vrsa))
|
|
|
|
return 0;
|
|
|
|
RSA_free(prsactx->rsa);
|
|
|
|
prsactx->rsa = vrsa;
|
|
|
|
}
|
2021-04-01 23:14:43 +08:00
|
|
|
|
2020-05-03 12:02:52 +08:00
|
|
|
prsactx->operation = operation;
|
|
|
|
|
signature: Clamp PSS salt len to MD len
FIPS 186-4 section 5 "The RSA Digital Signature Algorithm", subsection
5.5 "PKCS #1" says: "For RSASSA-PSS […] the length (in bytes) of the
salt (sLen) shall satisfy 0 <= sLen <= hLen, where hLen is the length of
the hash function output block (in bytes)."
Introduce a new option RSA_PSS_SALTLEN_AUTO_DIGEST_MAX and make it the
default. The new value will behave like RSA_PSS_SALTLEN_AUTO, but will
not use more than the digest length when signing, so that FIPS 186-4 is
not violated. This value has two advantages when compared with
RSA_PSS_SALTLEN_DIGEST: (1) It will continue to do auto-detection when
verifying signatures for maximum compatibility, where
RSA_PSS_SALTLEN_DIGEST would fail for other digest sizes. (2) It will
work for combinations where the maximum salt length is smaller than the
digest size, which typically happens with large digest sizes (e.g.,
SHA-512) and small RSA keys.
J.-S. Coron shows in "Optimal Security Proofs for PSS and Other
Signature Schemes. Advances in Cryptology – Eurocrypt 2002, volume 2332
of Lecture Notes in Computer Science, pp. 272 – 287. Springer Verlag,
2002." that longer salts than the output size of modern hash functions
do not increase security: "For example,for an application in which at
most one billion signatures will be generated, k0 = 30 bits of random
salt are actually sufficient to guarantee the same level of security as
RSA, and taking a larger salt does not increase the security level."
Signed-off-by: Clemens Lang <cllang@redhat.com>
Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/19724)
2022-11-18 19:35:33 +08:00
|
|
|
/* Maximize up to digest length for sign, auto for verify */
|
|
|
|
prsactx->saltlen = RSA_PSS_SALTLEN_AUTO_DIGEST_MAX;
|
2020-05-03 12:02:52 +08:00
|
|
|
prsactx->min_saltlen = -1;
|
|
|
|
|
|
|
|
switch (RSA_test_flags(prsactx->rsa, RSA_FLAG_TYPE_MASK)) {
|
|
|
|
case RSA_FLAG_TYPE_RSA:
|
|
|
|
prsactx->pad_mode = RSA_PKCS1_PADDING;
|
|
|
|
break;
|
|
|
|
case RSA_FLAG_TYPE_RSASSAPSS:
|
|
|
|
prsactx->pad_mode = RSA_PKCS1_PSS_PADDING;
|
|
|
|
|
|
|
|
{
|
|
|
|
const RSA_PSS_PARAMS_30 *pss =
|
rsa: add ossl_ prefix to internal rsa_ calls.
The functions being:
rsa_check_crt_components, rsa_check_key, rsa_check_pminusq_diff,
rsa_check_prime_factor, rsa_check_prime_factor_range,
rsa_check_private_exponent, rsa_check_public_exponent,
rsa_digestinfo_encoding, rsa_fips186_4_gen_prob_primes, rsa_fromdata,
rsa_get0_all_params, rsa_get0_libctx, rsa_get0_pss_params_30,
rsa_get_lcm, rsa_mgf_nid2name, rsa_mp_coeff_names, rsa_mp_exp_names,
rsa_mp_factor_names, rsa_new_with_ctx, rsa_oaeppss_md2nid,
rsa_oaeppss_nid2name, rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx,
rsa_padding_add_PKCS1_type_2_with_libctx,
rsa_padding_add_SSLv23_with_libctx, rsa_padding_check_PKCS1_type_2_TLS,
rsa_pkey_method, rsa_pss_params_30_copy, rsa_pss_params_30_fromdata,
rsa_pss_params_30_hashalg, rsa_pss_params_30_is_unrestricted,
rsa_pss_params_30_maskgenalg, rsa_pss_params_30_maskgenhashalg,
rsa_pss_params_30_saltlen, rsa_pss_params_30_set_defaults,
rsa_pss_params_30_set_hashalg, rsa_pss_params_30_set_maskgenalg,
rsa_pss_params_30_set_maskgenhashalg, rsa_pss_params_30_set_saltlen,
rsa_pss_params_30_set_trailerfield, rsa_pss_params_30_todata,
rsa_pss_params_30_trailerfield, rsa_pss_pkey_method, rsa_set0_all_params,
rsa_sp800_56b_check_keypair, rsa_sp800_56b_check_private,
rsa_sp800_56b_check_public, rsa_sp800_56b_derive_params_from_pq,
rsa_sp800_56b_generate_key, rsa_sp800_56b_pairwise_test,
rsa_sp800_56b_validate_strength, rsa_todata, rsa_validate_pairwise,
rsa_validate_private and rsa_validate_public.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13040)
2020-09-30 12:20:14 +08:00
|
|
|
ossl_rsa_get0_pss_params_30(prsactx->rsa);
|
2020-05-03 12:02:52 +08:00
|
|
|
|
rsa: add ossl_ prefix to internal rsa_ calls.
The functions being:
rsa_check_crt_components, rsa_check_key, rsa_check_pminusq_diff,
rsa_check_prime_factor, rsa_check_prime_factor_range,
rsa_check_private_exponent, rsa_check_public_exponent,
rsa_digestinfo_encoding, rsa_fips186_4_gen_prob_primes, rsa_fromdata,
rsa_get0_all_params, rsa_get0_libctx, rsa_get0_pss_params_30,
rsa_get_lcm, rsa_mgf_nid2name, rsa_mp_coeff_names, rsa_mp_exp_names,
rsa_mp_factor_names, rsa_new_with_ctx, rsa_oaeppss_md2nid,
rsa_oaeppss_nid2name, rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx,
rsa_padding_add_PKCS1_type_2_with_libctx,
rsa_padding_add_SSLv23_with_libctx, rsa_padding_check_PKCS1_type_2_TLS,
rsa_pkey_method, rsa_pss_params_30_copy, rsa_pss_params_30_fromdata,
rsa_pss_params_30_hashalg, rsa_pss_params_30_is_unrestricted,
rsa_pss_params_30_maskgenalg, rsa_pss_params_30_maskgenhashalg,
rsa_pss_params_30_saltlen, rsa_pss_params_30_set_defaults,
rsa_pss_params_30_set_hashalg, rsa_pss_params_30_set_maskgenalg,
rsa_pss_params_30_set_maskgenhashalg, rsa_pss_params_30_set_saltlen,
rsa_pss_params_30_set_trailerfield, rsa_pss_params_30_todata,
rsa_pss_params_30_trailerfield, rsa_pss_pkey_method, rsa_set0_all_params,
rsa_sp800_56b_check_keypair, rsa_sp800_56b_check_private,
rsa_sp800_56b_check_public, rsa_sp800_56b_derive_params_from_pq,
rsa_sp800_56b_generate_key, rsa_sp800_56b_pairwise_test,
rsa_sp800_56b_validate_strength, rsa_todata, rsa_validate_pairwise,
rsa_validate_private and rsa_validate_public.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13040)
2020-09-30 12:20:14 +08:00
|
|
|
if (!ossl_rsa_pss_params_30_is_unrestricted(pss)) {
|
|
|
|
int md_nid = ossl_rsa_pss_params_30_hashalg(pss);
|
|
|
|
int mgf1md_nid = ossl_rsa_pss_params_30_maskgenhashalg(pss);
|
|
|
|
int min_saltlen = ossl_rsa_pss_params_30_saltlen(pss);
|
2020-05-03 12:02:52 +08:00
|
|
|
const char *mdname, *mgf1mdname;
|
2020-05-20 01:36:44 +08:00
|
|
|
size_t len;
|
2020-05-03 12:02:52 +08:00
|
|
|
|
rsa: add ossl_ prefix to internal rsa_ calls.
The functions being:
rsa_check_crt_components, rsa_check_key, rsa_check_pminusq_diff,
rsa_check_prime_factor, rsa_check_prime_factor_range,
rsa_check_private_exponent, rsa_check_public_exponent,
rsa_digestinfo_encoding, rsa_fips186_4_gen_prob_primes, rsa_fromdata,
rsa_get0_all_params, rsa_get0_libctx, rsa_get0_pss_params_30,
rsa_get_lcm, rsa_mgf_nid2name, rsa_mp_coeff_names, rsa_mp_exp_names,
rsa_mp_factor_names, rsa_new_with_ctx, rsa_oaeppss_md2nid,
rsa_oaeppss_nid2name, rsa_padding_add_PKCS1_OAEP_mgf1_with_libctx,
rsa_padding_add_PKCS1_type_2_with_libctx,
rsa_padding_add_SSLv23_with_libctx, rsa_padding_check_PKCS1_type_2_TLS,
rsa_pkey_method, rsa_pss_params_30_copy, rsa_pss_params_30_fromdata,
rsa_pss_params_30_hashalg, rsa_pss_params_30_is_unrestricted,
rsa_pss_params_30_maskgenalg, rsa_pss_params_30_maskgenhashalg,
rsa_pss_params_30_saltlen, rsa_pss_params_30_set_defaults,
rsa_pss_params_30_set_hashalg, rsa_pss_params_30_set_maskgenalg,
rsa_pss_params_30_set_maskgenhashalg, rsa_pss_params_30_set_saltlen,
rsa_pss_params_30_set_trailerfield, rsa_pss_params_30_todata,
rsa_pss_params_30_trailerfield, rsa_pss_pkey_method, rsa_set0_all_params,
rsa_sp800_56b_check_keypair, rsa_sp800_56b_check_private,
rsa_sp800_56b_check_public, rsa_sp800_56b_derive_params_from_pq,
rsa_sp800_56b_generate_key, rsa_sp800_56b_pairwise_test,
rsa_sp800_56b_validate_strength, rsa_todata, rsa_validate_pairwise,
rsa_validate_private and rsa_validate_public.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13040)
2020-09-30 12:20:14 +08:00
|
|
|
mdname = ossl_rsa_oaeppss_nid2name(md_nid);
|
|
|
|
mgf1mdname = ossl_rsa_oaeppss_nid2name(mgf1md_nid);
|
2020-05-03 12:02:52 +08:00
|
|
|
|
|
|
|
if (mdname == NULL) {
|
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
|
|
|
|
"PSS restrictions lack hash algorithm");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (mgf1mdname == NULL) {
|
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
|
|
|
|
"PSS restrictions lack MGF1 hash algorithm");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-20 01:36:44 +08:00
|
|
|
len = OPENSSL_strlcpy(prsactx->mdname, mdname,
|
|
|
|
sizeof(prsactx->mdname));
|
|
|
|
if (len >= sizeof(prsactx->mdname)) {
|
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
|
|
|
|
"hash algorithm name too long");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
len = OPENSSL_strlcpy(prsactx->mgf1_mdname, mgf1mdname,
|
|
|
|
sizeof(prsactx->mgf1_mdname));
|
|
|
|
if (len >= sizeof(prsactx->mgf1_mdname)) {
|
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
|
|
|
|
"MGF1 hash algorithm name too long");
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-03 12:02:52 +08:00
|
|
|
prsactx->saltlen = min_saltlen;
|
|
|
|
|
2021-01-30 00:02:32 +08:00
|
|
|
/* call rsa_setup_mgf1_md before rsa_setup_md to avoid duplication */
|
2021-11-19 22:16:53 +08:00
|
|
|
if (!rsa_setup_mgf1_md(prsactx, mgf1mdname, prsactx->propq)
|
|
|
|
|| !rsa_setup_md(prsactx, mdname, prsactx->propq)
|
|
|
|
|| !rsa_check_parameters(prsactx, min_saltlen))
|
|
|
|
return 0;
|
2020-05-03 12:02:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERR_raise(ERR_LIB_RSA, PROV_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-11-19 22:16:53 +08:00
|
|
|
if (!rsa_set_ctx_params(prsactx, params))
|
|
|
|
return 0;
|
|
|
|
|
2020-05-03 12:02:52 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-12-01 22:01:50 +08:00
|
|
|
static int setup_tbuf(PROV_RSA_CTX *ctx)
|
|
|
|
{
|
|
|
|
if (ctx->tbuf != NULL)
|
|
|
|
return 1;
|
2022-09-29 19:57:34 +08:00
|
|
|
if ((ctx->tbuf = OPENSSL_malloc(RSA_size(ctx->rsa))) == NULL)
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clean_tbuf(PROV_RSA_CTX *ctx)
|
|
|
|
{
|
|
|
|
if (ctx->tbuf != NULL)
|
|
|
|
OPENSSL_cleanse(ctx->tbuf, RSA_size(ctx->rsa));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_tbuf(PROV_RSA_CTX *ctx)
|
|
|
|
{
|
2020-05-03 12:02:52 +08:00
|
|
|
clean_tbuf(ctx);
|
|
|
|
OPENSSL_free(ctx->tbuf);
|
2019-12-01 22:01:50 +08:00
|
|
|
ctx->tbuf = NULL;
|
|
|
|
}
|
|
|
|
|
2021-03-02 20:43:36 +08:00
|
|
|
static int rsa_sign_init(void *vprsactx, void *vrsa, const OSSL_PARAM params[])
|
2020-05-07 14:51:09 +08:00
|
|
|
{
|
signature: add FIPS error state handling
The functions that check for the provider being runnable are: newctx, dupctx,
sign init, sign, verify init, verify, verify recover init, verify recover,
digest sign init, digest sign final, digest verify init and digest verify final.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/12801)
2020-09-07 11:44:17 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return 0;
|
2021-03-02 20:43:36 +08:00
|
|
|
return rsa_signverify_init(vprsactx, vrsa, params, EVP_PKEY_OP_SIGN);
|
2020-05-07 14:51:09 +08:00
|
|
|
}
|
|
|
|
|
2019-12-01 22:01:50 +08:00
|
|
|
static int rsa_sign(void *vprsactx, unsigned char *sig, size_t *siglen,
|
|
|
|
size_t sigsize, const unsigned char *tbs, size_t tbslen)
|
|
|
|
{
|
|
|
|
PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
|
|
|
|
int ret;
|
|
|
|
size_t rsasize = RSA_size(prsactx->rsa);
|
|
|
|
size_t mdsize = rsa_get_md_size(prsactx);
|
|
|
|
|
signature: add FIPS error state handling
The functions that check for the provider being runnable are: newctx, dupctx,
sign init, sign, verify init, verify, verify recover init, verify recover,
digest sign init, digest sign final, digest verify init and digest verify final.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/12801)
2020-09-07 11:44:17 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return 0;
|
|
|
|
|
2019-12-01 22:01:50 +08:00
|
|
|
if (sig == NULL) {
|
|
|
|
*siglen = rsasize;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-05-03 12:02:52 +08:00
|
|
|
if (sigsize < rsasize) {
|
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SIGNATURE_SIZE,
|
|
|
|
"is %zu, should be at least %zu", sigsize, rsasize);
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
2020-05-03 12:02:52 +08:00
|
|
|
}
|
2019-12-01 22:01:50 +08:00
|
|
|
|
|
|
|
if (mdsize != 0) {
|
|
|
|
if (tbslen != mdsize) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE
|
2019-12-01 22:01:50 +08:00
|
|
|
if (EVP_MD_is_a(prsactx->md, OSSL_DIGEST_NAME_MDC2)) {
|
|
|
|
unsigned int sltmp;
|
|
|
|
|
|
|
|
if (prsactx->pad_mode != RSA_PKCS1_PADDING) {
|
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
|
|
|
|
"only PKCS#1 padding supported with MDC2");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ret = RSA_sign_ASN1_OCTET_STRING(0, tbs, tbslen, sig, &sltmp,
|
|
|
|
prsactx->rsa);
|
|
|
|
|
|
|
|
if (ret <= 0) {
|
2021-02-11 19:55:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ret = sltmp;
|
|
|
|
goto end;
|
|
|
|
}
|
2019-12-04 16:54:35 +08:00
|
|
|
#endif
|
2019-12-01 22:01:50 +08:00
|
|
|
switch (prsactx->pad_mode) {
|
|
|
|
case RSA_X931_PADDING:
|
|
|
|
if ((size_t)RSA_size(prsactx->rsa) < tbslen + 1) {
|
2020-05-03 12:02:52 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_KEY_SIZE_TOO_SMALL,
|
|
|
|
"RSA key size = %d, expected minimum = %d",
|
|
|
|
RSA_size(prsactx->rsa), tbslen + 1);
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!setup_tbuf(prsactx)) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_PROV_LIB);
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy(prsactx->tbuf, tbs, tbslen);
|
|
|
|
prsactx->tbuf[tbslen] = RSA_X931_hash_id(prsactx->mdnid);
|
|
|
|
ret = RSA_private_encrypt(tbslen + 1, prsactx->tbuf,
|
|
|
|
sig, prsactx->rsa, RSA_X931_PADDING);
|
|
|
|
clean_tbuf(prsactx);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RSA_PKCS1_PADDING:
|
|
|
|
{
|
|
|
|
unsigned int sltmp;
|
|
|
|
|
|
|
|
ret = RSA_sign(prsactx->mdnid, tbs, tbslen, sig, &sltmp,
|
|
|
|
prsactx->rsa);
|
|
|
|
if (ret <= 0) {
|
2021-02-11 19:55:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ret = sltmp;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RSA_PKCS1_PSS_PADDING:
|
|
|
|
/* Check PSS restrictions */
|
|
|
|
if (rsa_pss_restricted(prsactx)) {
|
|
|
|
switch (prsactx->saltlen) {
|
|
|
|
case RSA_PSS_SALTLEN_DIGEST:
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
if (prsactx->min_saltlen > EVP_MD_get_size(prsactx->md)) {
|
2020-05-03 12:02:52 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROV,
|
|
|
|
PROV_R_PSS_SALTLEN_TOO_SMALL,
|
|
|
|
"minimum salt length set to %d, "
|
|
|
|
"but the digest only gives %d",
|
|
|
|
prsactx->min_saltlen,
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
EVP_MD_get_size(prsactx->md));
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* FALLTHRU */
|
|
|
|
default:
|
|
|
|
if (prsactx->saltlen >= 0
|
|
|
|
&& prsactx->saltlen < prsactx->min_saltlen) {
|
2020-05-03 12:02:52 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROV,
|
|
|
|
PROV_R_PSS_SALTLEN_TOO_SMALL,
|
|
|
|
"minimum salt length set to %d, but the"
|
|
|
|
"actual salt length is only set to %d",
|
|
|
|
prsactx->min_saltlen,
|
|
|
|
prsactx->saltlen);
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!setup_tbuf(prsactx))
|
|
|
|
return 0;
|
|
|
|
if (!RSA_padding_add_PKCS1_PSS_mgf1(prsactx->rsa,
|
|
|
|
prsactx->tbuf, tbs,
|
|
|
|
prsactx->md, prsactx->mgf1_md,
|
|
|
|
prsactx->saltlen)) {
|
2021-02-11 19:55:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ret = RSA_private_encrypt(RSA_size(prsactx->rsa), prsactx->tbuf,
|
|
|
|
sig, prsactx->rsa, RSA_NO_PADDING);
|
|
|
|
clean_tbuf(prsactx);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
|
|
|
|
"Only X.931, PKCS#1 v1.5 or PSS padding allowed");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = RSA_private_encrypt(tbslen, tbs, sig, prsactx->rsa,
|
|
|
|
prsactx->pad_mode);
|
|
|
|
}
|
|
|
|
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE
|
2019-12-01 22:01:50 +08:00
|
|
|
end:
|
|
|
|
#endif
|
|
|
|
if (ret <= 0) {
|
2021-02-11 19:55:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*siglen = ret;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-03-02 20:43:36 +08:00
|
|
|
static int rsa_verify_recover_init(void *vprsactx, void *vrsa,
|
|
|
|
const OSSL_PARAM params[])
|
2020-05-07 14:51:09 +08:00
|
|
|
{
|
signature: add FIPS error state handling
The functions that check for the provider being runnable are: newctx, dupctx,
sign init, sign, verify init, verify, verify recover init, verify recover,
digest sign init, digest sign final, digest verify init and digest verify final.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/12801)
2020-09-07 11:44:17 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return 0;
|
2021-03-02 20:43:36 +08:00
|
|
|
return rsa_signverify_init(vprsactx, vrsa, params,
|
|
|
|
EVP_PKEY_OP_VERIFYRECOVER);
|
2020-05-07 14:51:09 +08:00
|
|
|
}
|
|
|
|
|
2019-12-01 22:01:50 +08:00
|
|
|
static int rsa_verify_recover(void *vprsactx,
|
|
|
|
unsigned char *rout,
|
|
|
|
size_t *routlen,
|
|
|
|
size_t routsize,
|
|
|
|
const unsigned char *sig,
|
|
|
|
size_t siglen)
|
|
|
|
{
|
|
|
|
PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
|
|
|
|
int ret;
|
|
|
|
|
signature: add FIPS error state handling
The functions that check for the provider being runnable are: newctx, dupctx,
sign init, sign, verify init, verify, verify recover init, verify recover,
digest sign init, digest sign final, digest verify init and digest verify final.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/12801)
2020-09-07 11:44:17 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return 0;
|
|
|
|
|
2019-12-01 22:01:50 +08:00
|
|
|
if (rout == NULL) {
|
|
|
|
*routlen = RSA_size(prsactx->rsa);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prsactx->md != NULL) {
|
|
|
|
switch (prsactx->pad_mode) {
|
|
|
|
case RSA_X931_PADDING:
|
|
|
|
if (!setup_tbuf(prsactx))
|
|
|
|
return 0;
|
|
|
|
ret = RSA_public_decrypt(siglen, sig, prsactx->tbuf, prsactx->rsa,
|
|
|
|
RSA_X931_PADDING);
|
|
|
|
if (ret < 1) {
|
2021-02-11 19:55:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ret--;
|
|
|
|
if (prsactx->tbuf[ret] != RSA_X931_hash_id(prsactx->mdnid)) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_ALGORITHM_MISMATCH);
|
|
|
|
return 0;
|
|
|
|
}
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
if (ret != EVP_MD_get_size(prsactx->md)) {
|
2019-12-01 22:01:50 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH,
|
|
|
|
"Should be %d, but got %d",
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
EVP_MD_get_size(prsactx->md), ret);
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
*routlen = ret;
|
2020-06-17 09:33:16 +08:00
|
|
|
if (rout != prsactx->tbuf) {
|
|
|
|
if (routsize < (size_t)ret) {
|
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL,
|
|
|
|
"buffer size is %d, should be %d",
|
|
|
|
routsize, ret);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memcpy(rout, prsactx->tbuf, ret);
|
2019-12-01 22:01:50 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RSA_PKCS1_PADDING:
|
|
|
|
{
|
|
|
|
size_t sltmp;
|
|
|
|
|
2021-03-09 08:14:45 +08:00
|
|
|
ret = ossl_rsa_verify(prsactx->mdnid, NULL, 0, rout, &sltmp,
|
|
|
|
sig, siglen, prsactx->rsa);
|
2019-12-01 22:01:50 +08:00
|
|
|
if (ret <= 0) {
|
2021-02-11 19:55:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ret = sltmp;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
|
|
|
|
"Only X.931 or PKCS#1 v1.5 padding allowed");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = RSA_public_decrypt(siglen, sig, rout, prsactx->rsa,
|
|
|
|
prsactx->pad_mode);
|
|
|
|
if (ret < 0) {
|
2021-02-11 19:55:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*routlen = ret;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-03-02 20:43:36 +08:00
|
|
|
static int rsa_verify_init(void *vprsactx, void *vrsa,
|
|
|
|
const OSSL_PARAM params[])
|
2020-05-07 14:51:09 +08:00
|
|
|
{
|
signature: add FIPS error state handling
The functions that check for the provider being runnable are: newctx, dupctx,
sign init, sign, verify init, verify, verify recover init, verify recover,
digest sign init, digest sign final, digest verify init and digest verify final.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/12801)
2020-09-07 11:44:17 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return 0;
|
2021-03-02 20:43:36 +08:00
|
|
|
return rsa_signverify_init(vprsactx, vrsa, params, EVP_PKEY_OP_VERIFY);
|
2020-05-07 14:51:09 +08:00
|
|
|
}
|
|
|
|
|
2019-12-01 22:01:50 +08:00
|
|
|
static int rsa_verify(void *vprsactx, const unsigned char *sig, size_t siglen,
|
|
|
|
const unsigned char *tbs, size_t tbslen)
|
|
|
|
{
|
|
|
|
PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
|
|
|
|
size_t rslen;
|
|
|
|
|
signature: add FIPS error state handling
The functions that check for the provider being runnable are: newctx, dupctx,
sign init, sign, verify init, verify, verify recover init, verify recover,
digest sign init, digest sign final, digest verify init and digest verify final.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/12801)
2020-09-07 11:44:17 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return 0;
|
2019-12-01 22:01:50 +08:00
|
|
|
if (prsactx->md != NULL) {
|
|
|
|
switch (prsactx->pad_mode) {
|
|
|
|
case RSA_PKCS1_PADDING:
|
|
|
|
if (!RSA_verify(prsactx->mdnid, tbs, tbslen, sig, siglen,
|
|
|
|
prsactx->rsa)) {
|
2021-02-11 19:55:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
case RSA_X931_PADDING:
|
2020-06-17 09:33:16 +08:00
|
|
|
if (!setup_tbuf(prsactx))
|
|
|
|
return 0;
|
|
|
|
if (rsa_verify_recover(prsactx, prsactx->tbuf, &rslen, 0,
|
|
|
|
sig, siglen) <= 0)
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case RSA_PKCS1_PSS_PADDING:
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t mdsize;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to check this for the RSA_verify_PKCS1_PSS_mgf1()
|
|
|
|
* call
|
|
|
|
*/
|
|
|
|
mdsize = rsa_get_md_size(prsactx);
|
|
|
|
if (tbslen != mdsize) {
|
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_LENGTH,
|
|
|
|
"Should be %d, but got %d",
|
|
|
|
mdsize, tbslen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!setup_tbuf(prsactx))
|
|
|
|
return 0;
|
|
|
|
ret = RSA_public_decrypt(siglen, sig, prsactx->tbuf,
|
|
|
|
prsactx->rsa, RSA_NO_PADDING);
|
|
|
|
if (ret <= 0) {
|
2021-02-11 19:55:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ret = RSA_verify_PKCS1_PSS_mgf1(prsactx->rsa, tbs,
|
|
|
|
prsactx->md, prsactx->mgf1_md,
|
|
|
|
prsactx->tbuf,
|
|
|
|
prsactx->saltlen);
|
|
|
|
if (ret <= 0) {
|
2021-02-11 19:55:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_PADDING_MODE,
|
|
|
|
"Only X.931, PKCS#1 v1.5 or PSS padding allowed");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
2023-03-08 18:17:31 +08:00
|
|
|
int ret;
|
|
|
|
|
2019-12-01 22:01:50 +08:00
|
|
|
if (!setup_tbuf(prsactx))
|
|
|
|
return 0;
|
2023-03-08 18:17:31 +08:00
|
|
|
ret = RSA_public_decrypt(siglen, sig, prsactx->tbuf, prsactx->rsa,
|
|
|
|
prsactx->pad_mode);
|
|
|
|
if (ret <= 0) {
|
2021-02-11 19:55:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_RSA_LIB);
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2023-03-08 18:17:31 +08:00
|
|
|
rslen = (size_t)ret;
|
2019-12-01 22:01:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((rslen != tbslen) || memcmp(tbs, prsactx->tbuf, rslen))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rsa_digest_signverify_init(void *vprsactx, const char *mdname,
|
2021-03-02 20:43:36 +08:00
|
|
|
void *vrsa, const OSSL_PARAM params[],
|
|
|
|
int operation)
|
2019-12-01 22:01:50 +08:00
|
|
|
{
|
|
|
|
PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
|
|
|
|
|
signature: add FIPS error state handling
The functions that check for the provider being runnable are: newctx, dupctx,
sign init, sign, verify init, verify, verify recover init, verify recover,
digest sign init, digest sign final, digest verify init and digest verify final.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/12801)
2020-09-07 11:44:17 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return 0;
|
|
|
|
|
2021-03-02 20:43:36 +08:00
|
|
|
if (!rsa_signverify_init(vprsactx, vrsa, params, operation))
|
2021-01-30 00:02:32 +08:00
|
|
|
return 0;
|
2021-11-04 22:38:51 +08:00
|
|
|
|
2021-01-30 00:02:32 +08:00
|
|
|
if (mdname != NULL
|
|
|
|
/* was rsa_setup_md already called in rsa_signverify_init()? */
|
2022-04-12 18:30:08 +08:00
|
|
|
&& (mdname[0] == '\0' || OPENSSL_strcasecmp(prsactx->mdname, mdname) != 0)
|
2021-01-30 00:02:32 +08:00
|
|
|
&& !rsa_setup_md(prsactx, mdname, prsactx->propq))
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
|
2021-07-14 21:41:22 +08:00
|
|
|
prsactx->flag_allow_md = 0;
|
2021-11-04 22:38:51 +08:00
|
|
|
|
2020-05-03 12:02:52 +08:00
|
|
|
if (prsactx->mdctx == NULL) {
|
2021-11-04 22:38:51 +08:00
|
|
|
prsactx->mdctx = EVP_MD_CTX_new();
|
|
|
|
if (prsactx->mdctx == NULL)
|
|
|
|
goto error;
|
2020-05-03 12:02:52 +08:00
|
|
|
}
|
2019-12-01 22:01:50 +08:00
|
|
|
|
2021-03-02 20:43:36 +08:00
|
|
|
if (!EVP_DigestInit_ex2(prsactx->mdctx, prsactx->md, params))
|
2019-12-01 22:01:50 +08:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
error:
|
|
|
|
EVP_MD_CTX_free(prsactx->mdctx);
|
|
|
|
prsactx->mdctx = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-07 14:51:09 +08:00
|
|
|
static int rsa_digest_signverify_update(void *vprsactx,
|
|
|
|
const unsigned char *data,
|
|
|
|
size_t datalen)
|
2019-12-01 22:01:50 +08:00
|
|
|
{
|
|
|
|
PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
|
|
|
|
|
|
|
|
if (prsactx == NULL || prsactx->mdctx == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return EVP_DigestUpdate(prsactx->mdctx, data, datalen);
|
|
|
|
}
|
|
|
|
|
2020-05-07 14:51:09 +08:00
|
|
|
static int rsa_digest_sign_init(void *vprsactx, const char *mdname,
|
2021-03-02 20:43:36 +08:00
|
|
|
void *vrsa, const OSSL_PARAM params[])
|
2020-05-07 14:51:09 +08:00
|
|
|
{
|
signature: add FIPS error state handling
The functions that check for the provider being runnable are: newctx, dupctx,
sign init, sign, verify init, verify, verify recover init, verify recover,
digest sign init, digest sign final, digest verify init and digest verify final.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/12801)
2020-09-07 11:44:17 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return 0;
|
2020-05-03 12:02:52 +08:00
|
|
|
return rsa_digest_signverify_init(vprsactx, mdname, vrsa,
|
2021-03-02 20:43:36 +08:00
|
|
|
params, EVP_PKEY_OP_SIGN);
|
2020-05-07 14:51:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rsa_digest_sign_final(void *vprsactx, unsigned char *sig,
|
|
|
|
size_t *siglen, size_t sigsize)
|
2019-12-01 22:01:50 +08:00
|
|
|
{
|
|
|
|
PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
|
|
|
|
unsigned char digest[EVP_MAX_MD_SIZE];
|
|
|
|
unsigned int dlen = 0;
|
|
|
|
|
signature: add FIPS error state handling
The functions that check for the provider being runnable are: newctx, dupctx,
sign init, sign, verify init, verify, verify recover init, verify recover,
digest sign init, digest sign final, digest verify init and digest verify final.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/12801)
2020-09-07 11:44:17 +08:00
|
|
|
if (!ossl_prov_is_running() || prsactx == NULL)
|
2020-08-11 14:33:19 +08:00
|
|
|
return 0;
|
2019-12-01 22:01:50 +08:00
|
|
|
prsactx->flag_allow_md = 1;
|
2020-08-11 14:33:19 +08:00
|
|
|
if (prsactx->mdctx == NULL)
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
/*
|
|
|
|
* If sig is NULL then we're just finding out the sig size. Other fields
|
|
|
|
* are ignored. Defer to rsa_sign.
|
|
|
|
*/
|
|
|
|
if (sig != NULL) {
|
|
|
|
/*
|
2020-08-29 10:25:54 +08:00
|
|
|
* The digests used here are all known (see rsa_get_md_nid()), so they
|
|
|
|
* should not exceed the internal buffer size of EVP_MAX_MD_SIZE.
|
2019-12-01 22:01:50 +08:00
|
|
|
*/
|
|
|
|
if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rsa_sign(vprsactx, sig, siglen, sigsize, digest, (size_t)dlen);
|
|
|
|
}
|
|
|
|
|
2020-05-07 14:51:09 +08:00
|
|
|
static int rsa_digest_verify_init(void *vprsactx, const char *mdname,
|
2021-03-02 20:43:36 +08:00
|
|
|
void *vrsa, const OSSL_PARAM params[])
|
2020-05-07 14:51:09 +08:00
|
|
|
{
|
signature: add FIPS error state handling
The functions that check for the provider being runnable are: newctx, dupctx,
sign init, sign, verify init, verify, verify recover init, verify recover,
digest sign init, digest sign final, digest verify init and digest verify final.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/12801)
2020-09-07 11:44:17 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return 0;
|
2020-05-03 12:02:52 +08:00
|
|
|
return rsa_digest_signverify_init(vprsactx, mdname, vrsa,
|
2021-03-02 20:43:36 +08:00
|
|
|
params, EVP_PKEY_OP_VERIFY);
|
2020-05-07 14:51:09 +08:00
|
|
|
}
|
2019-12-01 22:01:50 +08:00
|
|
|
|
|
|
|
int rsa_digest_verify_final(void *vprsactx, const unsigned char *sig,
|
|
|
|
size_t siglen)
|
|
|
|
{
|
|
|
|
PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
|
|
|
|
unsigned char digest[EVP_MAX_MD_SIZE];
|
|
|
|
unsigned int dlen = 0;
|
|
|
|
|
signature: add FIPS error state handling
The functions that check for the provider being runnable are: newctx, dupctx,
sign init, sign, verify init, verify, verify recover init, verify recover,
digest sign init, digest sign final, digest verify init and digest verify final.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/12801)
2020-09-07 11:44:17 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return 0;
|
|
|
|
|
2020-08-11 14:50:10 +08:00
|
|
|
if (prsactx == NULL)
|
|
|
|
return 0;
|
2019-12-01 22:01:50 +08:00
|
|
|
prsactx->flag_allow_md = 1;
|
2020-08-11 14:50:10 +08:00
|
|
|
if (prsactx->mdctx == NULL)
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
2020-08-29 10:25:54 +08:00
|
|
|
* The digests used here are all known (see rsa_get_md_nid()), so they
|
|
|
|
* should not exceed the internal buffer size of EVP_MAX_MD_SIZE.
|
2019-12-01 22:01:50 +08:00
|
|
|
*/
|
|
|
|
if (!EVP_DigestFinal_ex(prsactx->mdctx, digest, &dlen))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return rsa_verify(vprsactx, sig, siglen, digest, (size_t)dlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rsa_freectx(void *vprsactx)
|
|
|
|
{
|
|
|
|
PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
|
|
|
|
|
|
|
|
if (prsactx == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
EVP_MD_CTX_free(prsactx->mdctx);
|
|
|
|
EVP_MD_free(prsactx->md);
|
|
|
|
EVP_MD_free(prsactx->mgf1_md);
|
2020-05-07 03:44:58 +08:00
|
|
|
OPENSSL_free(prsactx->propq);
|
2019-12-01 22:01:50 +08:00
|
|
|
free_tbuf(prsactx);
|
2020-08-11 14:28:00 +08:00
|
|
|
RSA_free(prsactx->rsa);
|
2019-12-01 22:01:50 +08:00
|
|
|
|
2020-08-11 14:23:28 +08:00
|
|
|
OPENSSL_clear_free(prsactx, sizeof(*prsactx));
|
2019-12-01 22:01:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *rsa_dupctx(void *vprsactx)
|
|
|
|
{
|
|
|
|
PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
|
|
|
|
PROV_RSA_CTX *dstctx;
|
|
|
|
|
signature: add FIPS error state handling
The functions that check for the provider being runnable are: newctx, dupctx,
sign init, sign, verify init, verify, verify recover init, verify recover,
digest sign init, digest sign final, digest verify init and digest verify final.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/12801)
2020-09-07 11:44:17 +08:00
|
|
|
if (!ossl_prov_is_running())
|
|
|
|
return NULL;
|
|
|
|
|
2019-12-01 22:01:50 +08:00
|
|
|
dstctx = OPENSSL_zalloc(sizeof(*srcctx));
|
2022-09-29 19:57:34 +08:00
|
|
|
if (dstctx == NULL)
|
2019-12-01 22:01:50 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
*dstctx = *srcctx;
|
|
|
|
dstctx->rsa = NULL;
|
|
|
|
dstctx->md = NULL;
|
|
|
|
dstctx->mdctx = NULL;
|
|
|
|
dstctx->tbuf = NULL;
|
2020-11-26 13:06:34 +08:00
|
|
|
dstctx->propq = NULL;
|
2019-12-01 22:01:50 +08:00
|
|
|
|
|
|
|
if (srcctx->rsa != NULL && !RSA_up_ref(srcctx->rsa))
|
|
|
|
goto err;
|
|
|
|
dstctx->rsa = srcctx->rsa;
|
|
|
|
|
|
|
|
if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
|
|
|
|
goto err;
|
|
|
|
dstctx->md = srcctx->md;
|
|
|
|
|
|
|
|
if (srcctx->mgf1_md != NULL && !EVP_MD_up_ref(srcctx->mgf1_md))
|
|
|
|
goto err;
|
|
|
|
dstctx->mgf1_md = srcctx->mgf1_md;
|
|
|
|
|
|
|
|
if (srcctx->mdctx != NULL) {
|
|
|
|
dstctx->mdctx = EVP_MD_CTX_new();
|
|
|
|
if (dstctx->mdctx == NULL
|
|
|
|
|| !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2020-11-26 13:06:34 +08:00
|
|
|
if (srcctx->propq != NULL) {
|
|
|
|
dstctx->propq = OPENSSL_strdup(srcctx->propq);
|
|
|
|
if (dstctx->propq == NULL)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2019-12-01 22:01:50 +08:00
|
|
|
return dstctx;
|
|
|
|
err:
|
|
|
|
rsa_freectx(dstctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
|
|
|
|
{
|
|
|
|
PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
|
|
|
|
OSSL_PARAM *p;
|
|
|
|
|
2021-03-02 20:43:36 +08:00
|
|
|
if (prsactx == NULL)
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
|
2021-01-30 00:02:32 +08:00
|
|
|
if (p != NULL) {
|
|
|
|
/* The Algorithm Identifier of the combined signature algorithm */
|
|
|
|
unsigned char aid_buf[128];
|
|
|
|
unsigned char *aid;
|
|
|
|
size_t aid_len;
|
|
|
|
|
|
|
|
aid = rsa_generate_signature_aid(prsactx, aid_buf,
|
|
|
|
sizeof(aid_buf), &aid_len);
|
|
|
|
if (aid == NULL || !OSSL_PARAM_set_octet_string(p, aid, aid_len))
|
|
|
|
return 0;
|
|
|
|
}
|
2019-12-01 22:01:50 +08:00
|
|
|
|
|
|
|
p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_PAD_MODE);
|
|
|
|
if (p != NULL)
|
|
|
|
switch (p->data_type) {
|
|
|
|
case OSSL_PARAM_INTEGER:
|
|
|
|
if (!OSSL_PARAM_set_int(p, prsactx->pad_mode))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case OSSL_PARAM_UTF8_STRING:
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const char *word = NULL;
|
|
|
|
|
|
|
|
for (i = 0; padding_item[i].id != 0; i++) {
|
|
|
|
if (prsactx->pad_mode == (int)padding_item[i].id) {
|
|
|
|
word = padding_item[i].ptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (word != NULL) {
|
|
|
|
if (!OSSL_PARAM_set_utf8_string(p, word))
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
|
|
|
|
if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->mdname))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_MGF1_DIGEST);
|
|
|
|
if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->mgf1_mdname))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_PSS_SALTLEN);
|
|
|
|
if (p != NULL) {
|
|
|
|
if (p->data_type == OSSL_PARAM_INTEGER) {
|
|
|
|
if (!OSSL_PARAM_set_int(p, prsactx->saltlen))
|
|
|
|
return 0;
|
|
|
|
} else if (p->data_type == OSSL_PARAM_UTF8_STRING) {
|
2020-05-26 11:53:07 +08:00
|
|
|
const char *value = NULL;
|
|
|
|
|
2019-12-01 22:01:50 +08:00
|
|
|
switch (prsactx->saltlen) {
|
|
|
|
case RSA_PSS_SALTLEN_DIGEST:
|
2020-05-26 11:53:07 +08:00
|
|
|
value = OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST;
|
2019-12-01 22:01:50 +08:00
|
|
|
break;
|
|
|
|
case RSA_PSS_SALTLEN_MAX:
|
2020-05-26 11:53:07 +08:00
|
|
|
value = OSSL_PKEY_RSA_PSS_SALT_LEN_MAX;
|
2019-12-01 22:01:50 +08:00
|
|
|
break;
|
|
|
|
case RSA_PSS_SALTLEN_AUTO:
|
2020-05-26 11:53:07 +08:00
|
|
|
value = OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO;
|
2019-12-01 22:01:50 +08:00
|
|
|
break;
|
signature: Clamp PSS salt len to MD len
FIPS 186-4 section 5 "The RSA Digital Signature Algorithm", subsection
5.5 "PKCS #1" says: "For RSASSA-PSS […] the length (in bytes) of the
salt (sLen) shall satisfy 0 <= sLen <= hLen, where hLen is the length of
the hash function output block (in bytes)."
Introduce a new option RSA_PSS_SALTLEN_AUTO_DIGEST_MAX and make it the
default. The new value will behave like RSA_PSS_SALTLEN_AUTO, but will
not use more than the digest length when signing, so that FIPS 186-4 is
not violated. This value has two advantages when compared with
RSA_PSS_SALTLEN_DIGEST: (1) It will continue to do auto-detection when
verifying signatures for maximum compatibility, where
RSA_PSS_SALTLEN_DIGEST would fail for other digest sizes. (2) It will
work for combinations where the maximum salt length is smaller than the
digest size, which typically happens with large digest sizes (e.g.,
SHA-512) and small RSA keys.
J.-S. Coron shows in "Optimal Security Proofs for PSS and Other
Signature Schemes. Advances in Cryptology – Eurocrypt 2002, volume 2332
of Lecture Notes in Computer Science, pp. 272 – 287. Springer Verlag,
2002." that longer salts than the output size of modern hash functions
do not increase security: "For example,for an application in which at
most one billion signatures will be generated, k0 = 30 bits of random
salt are actually sufficient to guarantee the same level of security as
RSA, and taking a larger salt does not increase the security level."
Signed-off-by: Clemens Lang <cllang@redhat.com>
Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/19724)
2022-11-18 19:35:33 +08:00
|
|
|
case RSA_PSS_SALTLEN_AUTO_DIGEST_MAX:
|
|
|
|
value = OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO_DIGEST_MAX;
|
|
|
|
break;
|
2019-12-01 22:01:50 +08:00
|
|
|
default:
|
2020-05-26 11:53:07 +08:00
|
|
|
{
|
|
|
|
int len = BIO_snprintf(p->data, p->data_size, "%d",
|
|
|
|
prsactx->saltlen);
|
|
|
|
|
|
|
|
if (len <= 0)
|
|
|
|
return 0;
|
|
|
|
p->return_size = len;
|
|
|
|
break;
|
|
|
|
}
|
2019-12-01 22:01:50 +08:00
|
|
|
}
|
2020-05-26 11:53:07 +08:00
|
|
|
if (value != NULL
|
|
|
|
&& !OSSL_PARAM_set_utf8_string(p, value))
|
|
|
|
return 0;
|
2019-12-01 22:01:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const OSSL_PARAM known_gettable_ctx_params[] = {
|
|
|
|
OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
|
|
|
|
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
|
|
|
|
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
|
|
|
|
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, NULL, 0),
|
|
|
|
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
|
|
|
|
OSSL_PARAM_END
|
|
|
|
};
|
|
|
|
|
2021-02-27 01:02:36 +08:00
|
|
|
static const OSSL_PARAM *rsa_gettable_ctx_params(ossl_unused void *vprsactx,
|
|
|
|
ossl_unused void *provctx)
|
2019-12-01 22:01:50 +08:00
|
|
|
{
|
|
|
|
return known_gettable_ctx_params;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
|
|
|
|
{
|
|
|
|
PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
|
|
|
|
const OSSL_PARAM *p;
|
2021-02-12 07:52:54 +08:00
|
|
|
int pad_mode;
|
|
|
|
int saltlen;
|
2021-01-30 00:02:32 +08:00
|
|
|
char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = NULL;
|
|
|
|
char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = NULL;
|
|
|
|
char mgf1mdname[OSSL_MAX_NAME_SIZE] = "", *pmgf1mdname = NULL;
|
|
|
|
char mgf1mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmgf1mdprops = NULL;
|
2019-12-01 22:01:50 +08:00
|
|
|
|
2021-03-02 20:43:36 +08:00
|
|
|
if (prsactx == NULL)
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
2021-03-02 20:43:36 +08:00
|
|
|
if (params == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2021-02-12 07:52:54 +08:00
|
|
|
pad_mode = prsactx->pad_mode;
|
|
|
|
saltlen = prsactx->saltlen;
|
2019-12-01 22:01:50 +08:00
|
|
|
|
|
|
|
p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
|
|
|
|
if (p != NULL) {
|
|
|
|
const OSSL_PARAM *propsp =
|
|
|
|
OSSL_PARAM_locate_const(params,
|
|
|
|
OSSL_SIGNATURE_PARAM_PROPERTIES);
|
|
|
|
|
2021-01-30 00:02:32 +08:00
|
|
|
pmdname = mdname;
|
2019-12-01 22:01:50 +08:00
|
|
|
if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname)))
|
|
|
|
return 0;
|
2020-05-07 03:44:58 +08:00
|
|
|
|
2021-01-30 00:02:32 +08:00
|
|
|
if (propsp != NULL) {
|
|
|
|
pmdprops = mdprops;
|
|
|
|
if (!OSSL_PARAM_get_utf8_string(propsp,
|
|
|
|
&pmdprops, sizeof(mdprops)))
|
|
|
|
return 0;
|
2019-12-01 22:01:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_PAD_MODE);
|
|
|
|
if (p != NULL) {
|
2020-05-07 14:51:09 +08:00
|
|
|
const char *err_extra_text = NULL;
|
2019-12-01 22:01:50 +08:00
|
|
|
|
|
|
|
switch (p->data_type) {
|
|
|
|
case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
|
|
|
|
if (!OSSL_PARAM_get_int(p, &pad_mode))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case OSSL_PARAM_UTF8_STRING:
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (p->data == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; padding_item[i].id != 0; i++) {
|
|
|
|
if (strcmp(p->data, padding_item[i].ptr) == 0) {
|
|
|
|
pad_mode = padding_item[i].id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (pad_mode) {
|
|
|
|
case RSA_PKCS1_OAEP_PADDING:
|
|
|
|
/*
|
|
|
|
* OAEP padding is for asymmetric cipher only so is not compatible
|
|
|
|
* with signature use.
|
|
|
|
*/
|
2020-05-07 14:51:09 +08:00
|
|
|
err_extra_text = "OAEP padding not allowed for signing / verifying";
|
|
|
|
goto bad_pad;
|
2019-12-01 22:01:50 +08:00
|
|
|
case RSA_PKCS1_PSS_PADDING:
|
2020-05-07 14:51:09 +08:00
|
|
|
if ((prsactx->operation
|
|
|
|
& (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_VERIFY)) == 0) {
|
|
|
|
err_extra_text =
|
|
|
|
"PSS padding only allowed for sign and verify operations";
|
|
|
|
goto bad_pad;
|
|
|
|
}
|
|
|
|
break;
|
2019-12-01 22:01:50 +08:00
|
|
|
case RSA_PKCS1_PADDING:
|
2020-05-07 14:51:09 +08:00
|
|
|
err_extra_text = "PKCS#1 padding not allowed with RSA-PSS";
|
|
|
|
goto cont;
|
2019-12-01 22:01:50 +08:00
|
|
|
case RSA_NO_PADDING:
|
2020-05-07 14:51:09 +08:00
|
|
|
err_extra_text = "No padding not allowed with RSA-PSS";
|
|
|
|
goto cont;
|
2019-12-01 22:01:50 +08:00
|
|
|
case RSA_X931_PADDING:
|
2020-05-07 14:51:09 +08:00
|
|
|
err_extra_text = "X.931 padding not allowed with RSA-PSS";
|
2019-12-01 22:01:50 +08:00
|
|
|
cont:
|
2020-05-03 12:02:52 +08:00
|
|
|
if (RSA_test_flags(prsactx->rsa,
|
|
|
|
RSA_FLAG_TYPE_MASK) == RSA_FLAG_TYPE_RSA)
|
2020-05-07 14:51:09 +08:00
|
|
|
break;
|
|
|
|
/* FALLTHRU */
|
2019-12-01 22:01:50 +08:00
|
|
|
default:
|
2020-05-07 14:51:09 +08:00
|
|
|
bad_pad:
|
|
|
|
if (err_extra_text == NULL)
|
|
|
|
ERR_raise(ERR_LIB_PROV,
|
|
|
|
PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
|
|
|
|
else
|
|
|
|
ERR_raise_data(ERR_LIB_PROV,
|
|
|
|
PROV_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE,
|
|
|
|
err_extra_text);
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_PSS_SALTLEN);
|
|
|
|
if (p != NULL) {
|
2021-01-30 00:02:32 +08:00
|
|
|
if (pad_mode != RSA_PKCS1_PSS_PADDING) {
|
2019-12-01 22:01:50 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_NOT_SUPPORTED,
|
|
|
|
"PSS saltlen can only be specified if "
|
|
|
|
"PSS padding has been specified first");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (p->data_type) {
|
|
|
|
case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
|
|
|
|
if (!OSSL_PARAM_get_int(p, &saltlen))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case OSSL_PARAM_UTF8_STRING:
|
2020-05-26 11:53:07 +08:00
|
|
|
if (strcmp(p->data, OSSL_PKEY_RSA_PSS_SALT_LEN_DIGEST) == 0)
|
2019-12-01 22:01:50 +08:00
|
|
|
saltlen = RSA_PSS_SALTLEN_DIGEST;
|
2020-05-26 11:53:07 +08:00
|
|
|
else if (strcmp(p->data, OSSL_PKEY_RSA_PSS_SALT_LEN_MAX) == 0)
|
2019-12-01 22:01:50 +08:00
|
|
|
saltlen = RSA_PSS_SALTLEN_MAX;
|
2020-05-26 11:53:07 +08:00
|
|
|
else if (strcmp(p->data, OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO) == 0)
|
2019-12-01 22:01:50 +08:00
|
|
|
saltlen = RSA_PSS_SALTLEN_AUTO;
|
signature: Clamp PSS salt len to MD len
FIPS 186-4 section 5 "The RSA Digital Signature Algorithm", subsection
5.5 "PKCS #1" says: "For RSASSA-PSS […] the length (in bytes) of the
salt (sLen) shall satisfy 0 <= sLen <= hLen, where hLen is the length of
the hash function output block (in bytes)."
Introduce a new option RSA_PSS_SALTLEN_AUTO_DIGEST_MAX and make it the
default. The new value will behave like RSA_PSS_SALTLEN_AUTO, but will
not use more than the digest length when signing, so that FIPS 186-4 is
not violated. This value has two advantages when compared with
RSA_PSS_SALTLEN_DIGEST: (1) It will continue to do auto-detection when
verifying signatures for maximum compatibility, where
RSA_PSS_SALTLEN_DIGEST would fail for other digest sizes. (2) It will
work for combinations where the maximum salt length is smaller than the
digest size, which typically happens with large digest sizes (e.g.,
SHA-512) and small RSA keys.
J.-S. Coron shows in "Optimal Security Proofs for PSS and Other
Signature Schemes. Advances in Cryptology – Eurocrypt 2002, volume 2332
of Lecture Notes in Computer Science, pp. 272 – 287. Springer Verlag,
2002." that longer salts than the output size of modern hash functions
do not increase security: "For example,for an application in which at
most one billion signatures will be generated, k0 = 30 bits of random
salt are actually sufficient to guarantee the same level of security as
RSA, and taking a larger salt does not increase the security level."
Signed-off-by: Clemens Lang <cllang@redhat.com>
Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/19724)
2022-11-18 19:35:33 +08:00
|
|
|
else if (strcmp(p->data, OSSL_PKEY_RSA_PSS_SALT_LEN_AUTO_DIGEST_MAX) == 0)
|
|
|
|
saltlen = RSA_PSS_SALTLEN_AUTO_DIGEST_MAX;
|
2019-12-01 22:01:50 +08:00
|
|
|
else
|
|
|
|
saltlen = atoi(p->data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
signature: Clamp PSS salt len to MD len
FIPS 186-4 section 5 "The RSA Digital Signature Algorithm", subsection
5.5 "PKCS #1" says: "For RSASSA-PSS […] the length (in bytes) of the
salt (sLen) shall satisfy 0 <= sLen <= hLen, where hLen is the length of
the hash function output block (in bytes)."
Introduce a new option RSA_PSS_SALTLEN_AUTO_DIGEST_MAX and make it the
default. The new value will behave like RSA_PSS_SALTLEN_AUTO, but will
not use more than the digest length when signing, so that FIPS 186-4 is
not violated. This value has two advantages when compared with
RSA_PSS_SALTLEN_DIGEST: (1) It will continue to do auto-detection when
verifying signatures for maximum compatibility, where
RSA_PSS_SALTLEN_DIGEST would fail for other digest sizes. (2) It will
work for combinations where the maximum salt length is smaller than the
digest size, which typically happens with large digest sizes (e.g.,
SHA-512) and small RSA keys.
J.-S. Coron shows in "Optimal Security Proofs for PSS and Other
Signature Schemes. Advances in Cryptology – Eurocrypt 2002, volume 2332
of Lecture Notes in Computer Science, pp. 272 – 287. Springer Verlag,
2002." that longer salts than the output size of modern hash functions
do not increase security: "For example,for an application in which at
most one billion signatures will be generated, k0 = 30 bits of random
salt are actually sufficient to guarantee the same level of security as
RSA, and taking a larger salt does not increase the security level."
Signed-off-by: Clemens Lang <cllang@redhat.com>
Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/19724)
2022-11-18 19:35:33 +08:00
|
|
|
* RSA_PSS_SALTLEN_AUTO_DIGEST_MAX seems curiously named in this check.
|
|
|
|
* Contrary to what it's name suggests, it's the currently lowest
|
|
|
|
* saltlen number possible.
|
2019-12-01 22:01:50 +08:00
|
|
|
*/
|
signature: Clamp PSS salt len to MD len
FIPS 186-4 section 5 "The RSA Digital Signature Algorithm", subsection
5.5 "PKCS #1" says: "For RSASSA-PSS […] the length (in bytes) of the
salt (sLen) shall satisfy 0 <= sLen <= hLen, where hLen is the length of
the hash function output block (in bytes)."
Introduce a new option RSA_PSS_SALTLEN_AUTO_DIGEST_MAX and make it the
default. The new value will behave like RSA_PSS_SALTLEN_AUTO, but will
not use more than the digest length when signing, so that FIPS 186-4 is
not violated. This value has two advantages when compared with
RSA_PSS_SALTLEN_DIGEST: (1) It will continue to do auto-detection when
verifying signatures for maximum compatibility, where
RSA_PSS_SALTLEN_DIGEST would fail for other digest sizes. (2) It will
work for combinations where the maximum salt length is smaller than the
digest size, which typically happens with large digest sizes (e.g.,
SHA-512) and small RSA keys.
J.-S. Coron shows in "Optimal Security Proofs for PSS and Other
Signature Schemes. Advances in Cryptology – Eurocrypt 2002, volume 2332
of Lecture Notes in Computer Science, pp. 272 – 287. Springer Verlag,
2002." that longer salts than the output size of modern hash functions
do not increase security: "For example,for an application in which at
most one billion signatures will be generated, k0 = 30 bits of random
salt are actually sufficient to guarantee the same level of security as
RSA, and taking a larger salt does not increase the security level."
Signed-off-by: Clemens Lang <cllang@redhat.com>
Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/19724)
2022-11-18 19:35:33 +08:00
|
|
|
if (saltlen < RSA_PSS_SALTLEN_AUTO_DIGEST_MAX) {
|
2021-02-06 01:51:37 +08:00
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH);
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-07 14:51:09 +08:00
|
|
|
if (rsa_pss_restricted(prsactx)) {
|
2020-08-05 14:01:59 +08:00
|
|
|
switch (saltlen) {
|
2020-05-07 14:51:09 +08:00
|
|
|
case RSA_PSS_SALTLEN_AUTO:
|
signature: Clamp PSS salt len to MD len
FIPS 186-4 section 5 "The RSA Digital Signature Algorithm", subsection
5.5 "PKCS #1" says: "For RSASSA-PSS […] the length (in bytes) of the
salt (sLen) shall satisfy 0 <= sLen <= hLen, where hLen is the length of
the hash function output block (in bytes)."
Introduce a new option RSA_PSS_SALTLEN_AUTO_DIGEST_MAX and make it the
default. The new value will behave like RSA_PSS_SALTLEN_AUTO, but will
not use more than the digest length when signing, so that FIPS 186-4 is
not violated. This value has two advantages when compared with
RSA_PSS_SALTLEN_DIGEST: (1) It will continue to do auto-detection when
verifying signatures for maximum compatibility, where
RSA_PSS_SALTLEN_DIGEST would fail for other digest sizes. (2) It will
work for combinations where the maximum salt length is smaller than the
digest size, which typically happens with large digest sizes (e.g.,
SHA-512) and small RSA keys.
J.-S. Coron shows in "Optimal Security Proofs for PSS and Other
Signature Schemes. Advances in Cryptology – Eurocrypt 2002, volume 2332
of Lecture Notes in Computer Science, pp. 272 – 287. Springer Verlag,
2002." that longer salts than the output size of modern hash functions
do not increase security: "For example,for an application in which at
most one billion signatures will be generated, k0 = 30 bits of random
salt are actually sufficient to guarantee the same level of security as
RSA, and taking a larger salt does not increase the security level."
Signed-off-by: Clemens Lang <cllang@redhat.com>
Reviewed-by: Dmitry Belyavskiy <beldmit@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/19724)
2022-11-18 19:35:33 +08:00
|
|
|
case RSA_PSS_SALTLEN_AUTO_DIGEST_MAX:
|
2020-05-07 14:51:09 +08:00
|
|
|
if (prsactx->operation == EVP_PKEY_OP_VERIFY) {
|
2021-02-06 01:51:37 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_SALT_LENGTH,
|
|
|
|
"Cannot use autodetected salt length");
|
2020-05-07 14:51:09 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RSA_PSS_SALTLEN_DIGEST:
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
if (prsactx->min_saltlen > EVP_MD_get_size(prsactx->md)) {
|
2020-05-07 14:51:09 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PROV,
|
|
|
|
PROV_R_PSS_SALTLEN_TOO_SMALL,
|
|
|
|
"Should be more than %d, but would be "
|
|
|
|
"set to match digest size (%d)",
|
|
|
|
prsactx->min_saltlen,
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
EVP_MD_get_size(prsactx->md));
|
2020-05-07 14:51:09 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2020-08-05 14:01:59 +08:00
|
|
|
break;
|
2020-05-07 14:51:09 +08:00
|
|
|
default:
|
|
|
|
if (saltlen >= 0 && saltlen < prsactx->min_saltlen) {
|
|
|
|
ERR_raise_data(ERR_LIB_PROV,
|
|
|
|
PROV_R_PSS_SALTLEN_TOO_SMALL,
|
|
|
|
"Should be more than %d, "
|
|
|
|
"but would be set to %d",
|
|
|
|
prsactx->min_saltlen, saltlen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-01 22:01:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_MGF1_DIGEST);
|
|
|
|
if (p != NULL) {
|
|
|
|
const OSSL_PARAM *propsp =
|
|
|
|
OSSL_PARAM_locate_const(params,
|
|
|
|
OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES);
|
|
|
|
|
2021-01-30 00:02:32 +08:00
|
|
|
pmgf1mdname = mgf1mdname;
|
|
|
|
if (!OSSL_PARAM_get_utf8_string(p, &pmgf1mdname, sizeof(mgf1mdname)))
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
2020-05-07 03:44:58 +08:00
|
|
|
|
2021-01-30 00:02:32 +08:00
|
|
|
if (propsp != NULL) {
|
|
|
|
pmgf1mdprops = mgf1mdprops;
|
|
|
|
if (!OSSL_PARAM_get_utf8_string(propsp,
|
|
|
|
&pmgf1mdprops, sizeof(mgf1mdprops)))
|
|
|
|
return 0;
|
|
|
|
}
|
2019-12-01 22:01:50 +08:00
|
|
|
|
2021-01-30 00:02:32 +08:00
|
|
|
if (pad_mode != RSA_PKCS1_PSS_PADDING) {
|
2019-12-01 22:01:50 +08:00
|
|
|
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MGF1_MD);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-30 00:02:32 +08:00
|
|
|
}
|
2019-12-01 22:01:50 +08:00
|
|
|
|
2021-01-30 00:02:32 +08:00
|
|
|
prsactx->saltlen = saltlen;
|
|
|
|
prsactx->pad_mode = pad_mode;
|
|
|
|
|
|
|
|
if (prsactx->md == NULL && pmdname == NULL
|
|
|
|
&& pad_mode == RSA_PKCS1_PSS_PADDING)
|
|
|
|
pmdname = RSA_DEFAULT_DIGEST_NAME;
|
2019-12-01 22:01:50 +08:00
|
|
|
|
2021-01-30 00:02:32 +08:00
|
|
|
if (pmgf1mdname != NULL
|
|
|
|
&& !rsa_setup_mgf1_md(prsactx, pmgf1mdname, pmgf1mdprops))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (pmdname != NULL) {
|
|
|
|
if (!rsa_setup_md(prsactx, pmdname, pmdprops))
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
if (!rsa_check_padding(prsactx, NULL, NULL, prsactx->mdnid))
|
2019-12-01 22:01:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-02-27 01:02:36 +08:00
|
|
|
static const OSSL_PARAM settable_ctx_params[] = {
|
2019-12-01 22:01:50 +08:00
|
|
|
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
|
|
|
|
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0),
|
2021-02-27 01:02:36 +08:00
|
|
|
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
|
2019-12-01 22:01:50 +08:00
|
|
|
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, NULL, 0),
|
|
|
|
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES, NULL, 0),
|
|
|
|
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
|
|
|
|
OSSL_PARAM_END
|
|
|
|
};
|
|
|
|
|
2021-02-27 01:02:36 +08:00
|
|
|
static const OSSL_PARAM settable_ctx_params_no_digest[] = {
|
|
|
|
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, NULL, 0),
|
|
|
|
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, NULL, 0),
|
|
|
|
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_PROPERTIES, NULL, 0),
|
|
|
|
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, NULL, 0),
|
|
|
|
OSSL_PARAM_END
|
|
|
|
};
|
|
|
|
|
|
|
|
static const OSSL_PARAM *rsa_settable_ctx_params(void *vprsactx,
|
|
|
|
ossl_unused void *provctx)
|
2019-12-01 22:01:50 +08:00
|
|
|
{
|
2021-02-27 01:02:36 +08:00
|
|
|
PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
|
|
|
|
|
|
|
|
if (prsactx != NULL && !prsactx->flag_allow_md)
|
|
|
|
return settable_ctx_params_no_digest;
|
|
|
|
return settable_ctx_params;
|
2019-12-01 22:01:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int rsa_get_ctx_md_params(void *vprsactx, OSSL_PARAM *params)
|
|
|
|
{
|
|
|
|
PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
|
|
|
|
|
|
|
|
if (prsactx->mdctx == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return EVP_MD_CTX_get_params(prsactx->mdctx, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const OSSL_PARAM *rsa_gettable_ctx_md_params(void *vprsactx)
|
|
|
|
{
|
|
|
|
PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
|
|
|
|
|
|
|
|
if (prsactx->md == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return EVP_MD_gettable_ctx_params(prsactx->md);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rsa_set_ctx_md_params(void *vprsactx, const OSSL_PARAM params[])
|
|
|
|
{
|
|
|
|
PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
|
|
|
|
|
|
|
|
if (prsactx->mdctx == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return EVP_MD_CTX_set_params(prsactx->mdctx, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const OSSL_PARAM *rsa_settable_ctx_md_params(void *vprsactx)
|
|
|
|
{
|
|
|
|
PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
|
|
|
|
|
|
|
|
if (prsactx->md == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return EVP_MD_settable_ctx_params(prsactx->md);
|
|
|
|
}
|
|
|
|
|
2020-09-28 10:28:29 +08:00
|
|
|
const OSSL_DISPATCH ossl_rsa_signature_functions[] = {
|
2019-12-01 22:01:50 +08:00
|
|
|
{ OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))rsa_newctx },
|
2020-05-07 14:51:09 +08:00
|
|
|
{ OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))rsa_sign_init },
|
2019-12-01 22:01:50 +08:00
|
|
|
{ OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))rsa_sign },
|
2020-05-07 14:51:09 +08:00
|
|
|
{ OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))rsa_verify_init },
|
2019-12-01 22:01:50 +08:00
|
|
|
{ OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))rsa_verify },
|
2020-05-07 14:51:09 +08:00
|
|
|
{ OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT,
|
|
|
|
(void (*)(void))rsa_verify_recover_init },
|
|
|
|
{ OSSL_FUNC_SIGNATURE_VERIFY_RECOVER,
|
|
|
|
(void (*)(void))rsa_verify_recover },
|
2019-12-01 22:01:50 +08:00
|
|
|
{ OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
|
2020-05-07 14:51:09 +08:00
|
|
|
(void (*)(void))rsa_digest_sign_init },
|
2019-12-01 22:01:50 +08:00
|
|
|
{ OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
|
|
|
|
(void (*)(void))rsa_digest_signverify_update },
|
|
|
|
{ OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
|
|
|
|
(void (*)(void))rsa_digest_sign_final },
|
|
|
|
{ OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
|
2020-05-07 14:51:09 +08:00
|
|
|
(void (*)(void))rsa_digest_verify_init },
|
2019-12-01 22:01:50 +08:00
|
|
|
{ OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
|
|
|
|
(void (*)(void))rsa_digest_signverify_update },
|
|
|
|
{ OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
|
|
|
|
(void (*)(void))rsa_digest_verify_final },
|
|
|
|
{ OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))rsa_freectx },
|
|
|
|
{ OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))rsa_dupctx },
|
|
|
|
{ OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))rsa_get_ctx_params },
|
|
|
|
{ OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
|
|
|
|
(void (*)(void))rsa_gettable_ctx_params },
|
|
|
|
{ OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))rsa_set_ctx_params },
|
|
|
|
{ OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
|
|
|
|
(void (*)(void))rsa_settable_ctx_params },
|
|
|
|
{ OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
|
|
|
|
(void (*)(void))rsa_get_ctx_md_params },
|
|
|
|
{ OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
|
|
|
|
(void (*)(void))rsa_gettable_ctx_md_params },
|
|
|
|
{ OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
|
|
|
|
(void (*)(void))rsa_set_ctx_md_params },
|
|
|
|
{ OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
|
|
|
|
(void (*)(void))rsa_settable_ctx_md_params },
|
2023-04-19 22:08:22 +08:00
|
|
|
OSSL_DISPATCH_END
|
2019-12-01 22:01:50 +08:00
|
|
|
};
|