Skip to content

Commit 854fc95

Browse files
committed
Fix to handle compatibility layer RSA PSS salt special cases like RSA_PSS_SALTLEN_DIGEST, RSA_PSS_SALTLEN_MAX. Fix for "error: ‘RSA_PSS_SALT_LEN_DEFAULT’ undeclared". For builds that don't support it just use 0 and mark not used.
1 parent 2e832eb commit 854fc95

File tree

1 file changed

+56
-59
lines changed

1 file changed

+56
-59
lines changed

src/pk.c

Lines changed: 56 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3526,9 +3526,39 @@ int wolfSSL_RSA_generate_key_ex(WOLFSSL_RSA* rsa, int bits, WOLFSSL_BIGNUM* e,
35263526
* RSA padding APIs
35273527
*/
35283528

3529-
#if defined(WC_RSA_PSS) && (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || \
3530-
defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX))
3529+
#ifdef WC_RSA_PSS
3530+
3531+
static int rsa_pss_calc_salt(int saltLen, int hashLen, int emLen)
3532+
{
3533+
/* Calculate the salt length to use for special cases. */
3534+
switch (saltLen) {
3535+
/* Negative saltLen values are treated differently. */
3536+
case RSA_PSS_SALTLEN_DIGEST:
3537+
saltLen = hashLen;
3538+
break;
3539+
case RSA_PSS_SALTLEN_MAX_SIGN:
3540+
case RSA_PSS_SALTLEN_MAX:
3541+
#ifdef WOLFSSL_PSS_LONG_SALT
3542+
saltLen = emLen - hashLen - 2;
3543+
#else
3544+
saltLen = hashLen;
3545+
#endif
3546+
break;
3547+
default:
3548+
if (saltLen < 0) {
3549+
/* log invalid salt, let wolfCrypt report error */
3550+
WOLFSSL_ERROR_MSG("invalid saltLen");
3551+
saltLen = -3; /* for wolfCrypt to produce error must be < -2 */
3552+
}
3553+
break;
3554+
}
3555+
return saltLen;
3556+
}
3557+
3558+
#if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || \
3559+
defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
35313560
#if !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
3561+
35323562
/* Add PKCS#1 PSS padding to hash.
35333563
*
35343564
*
@@ -3646,28 +3676,7 @@ int wolfSSL_RSA_padding_add_PKCS1_PSS_mgf1(WOLFSSL_RSA *rsa, unsigned char *em,
36463676
}
36473677

36483678
if (ret == 1) {
3649-
/* Calculate the salt length to use for special cases. */
3650-
/* TODO: use special case wolfCrypt values? */
3651-
switch (saltLen) {
3652-
/* Negative saltLen values are treated differently. */
3653-
case RSA_PSS_SALTLEN_DIGEST:
3654-
saltLen = hashLen;
3655-
break;
3656-
case RSA_PSS_SALTLEN_MAX_SIGN:
3657-
case RSA_PSS_SALTLEN_MAX:
3658-
#ifdef WOLFSSL_PSS_LONG_SALT
3659-
saltLen = emLen - hashLen - 2;
3660-
#else
3661-
saltLen = hashLen;
3662-
#endif
3663-
break;
3664-
default:
3665-
if (saltLen < 0) {
3666-
/* No other negative values implemented. */
3667-
WOLFSSL_ERROR_MSG("invalid saltLen");
3668-
ret = 0;
3669-
}
3670-
}
3679+
saltLen = rsa_pss_calc_salt(saltLen, hashLen, emLen);
36713680
}
36723681

36733682
if (ret == 1) {
@@ -3759,31 +3768,7 @@ int wolfSSL_RSA_verify_PKCS1_PSS_mgf1(WOLFSSL_RSA *rsa,
37593768
}
37603769

37613770
if (ret == 1) {
3762-
/* Calculate the salt length to use for special cases. */
3763-
switch (saltLen) {
3764-
/* Negative saltLen values are treated differently */
3765-
case RSA_PSS_SALTLEN_DIGEST:
3766-
saltLen = hashLen;
3767-
break;
3768-
case RSA_PSS_SALTLEN_AUTO:
3769-
#ifdef WOLFSSL_PSS_SALT_LEN_DISCOVER
3770-
saltLen = RSA_PSS_SALT_LEN_DISCOVER;
3771-
break;
3772-
#endif
3773-
case RSA_PSS_SALTLEN_MAX:
3774-
#ifdef WOLFSSL_PSS_LONG_SALT
3775-
saltLen = emLen - hashLen - 2;
3776-
#else
3777-
saltLen = hashLen;
3778-
#endif
3779-
break;
3780-
default:
3781-
if (saltLen < 0) {
3782-
/* No other negative values implemented. */
3783-
WOLFSSL_ERROR_MSG("invalid saltLen");
3784-
ret = 0;
3785-
}
3786-
}
3771+
saltLen = rsa_pss_calc_salt(saltLen, hashLen, emLen);
37873772
}
37883773

37893774
if (ret == 1) {
@@ -3849,17 +3834,22 @@ int wolfSSL_RSA_verify_PKCS1_PSS(WOLFSSL_RSA *rsa, const unsigned char *mHash,
38493834
saltLen);
38503835
}
38513836
#endif /* !HAVE_FIPS || FIPS_VERSION_GT(2,0) */
3852-
#endif /* WC_RSA_PSS && (OPENSSL_ALL || WOLFSSL_ASIO || WOLFSSL_HAPROXY ||
3853-
* WOLFSSL_NGINX) */
3837+
#endif /* OPENSSL_ALL || WOLFSSL_ASIO || WOLFSSL_HAPROXY || WOLFSSL_NGINX) */
3838+
#endif /* WC_RSA_PSS */
38543839

38553840
/*
38563841
* RSA sign/verify APIs
38573842
*/
38583843

3859-
#ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
3860-
#define DEF_PSS_SALT_LEN RSA_PSS_SALT_LEN_DEFAULT
3844+
#if defined(WC_RSA_PSS) && !defined(HAVE_SELFTEST) && \
3845+
(!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,1))
3846+
#ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
3847+
#define DEF_PSS_SALT_LEN RSA_PSS_SALT_LEN_DEFAULT
3848+
#else
3849+
#define DEF_PSS_SALT_LEN RSA_PSS_SALT_LEN_DISCOVER
3850+
#endif
38613851
#else
3862-
#define DEF_PSS_SALT_LEN RSA_PSS_SALT_LEN_DISCOVER
3852+
#define DEF_PSS_SALT_LEN 0 /* not used */
38633853
#endif
38643854

38653855
#if defined(OPENSSL_EXTRA)
@@ -4098,16 +4088,19 @@ int wolfSSL_RSA_sign_mgf(int hashAlg, const unsigned char* hash,
40984088
(!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,1))
40994089
case WC_RSA_PKCS1_PSS_PADDING:
41004090
{
4091+
RsaKey* key = (RsaKey*)rsa->internal;
41014092
enum wc_HashType mgf1, hType;
41024093
hType = wc_OidGetHash((int)nid2oid(hashAlg, oidHashType));
41034094
if (mgf1Hash == WC_NID_undef)
41044095
mgf1Hash = hashAlg;
41054096
mgf1 = wc_OidGetHash((int)nid2oid(mgf1Hash, oidHashType));
4097+
/* handle compat layer salt special cases */
4098+
saltLen = rsa_pss_calc_salt(saltLen, wc_HashGetDigestSize(hType),
4099+
wolfSSL_RSA_size(rsa));
41064100

41074101
/* Create RSA PSS signature. */
41084102
if ((signSz = wc_RsaPSS_Sign_ex(encodedSig, encSz, sigRet, outLen,
4109-
hType, wc_hash2mgf(mgf1), saltLen,
4110-
(RsaKey*)rsa->internal, rng)) <= 0) {
4103+
hType, wc_hash2mgf(mgf1), saltLen, key, rng)) <= 0) {
41114104
WOLFSSL_ERROR_MSG("Bad RSA Sign");
41124105
ret = 0;
41134106
}
@@ -4244,19 +4237,23 @@ int wolfSSL_RSA_verify_mgf(int hashAlg, const unsigned char* hash,
42444237
if (ret == 1 && padding == WC_RSA_PKCS1_PSS_PADDING) {
42454238
#if defined(WC_RSA_PSS) && !defined(HAVE_SELFTEST) && \
42464239
(!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,1))
4240+
RsaKey* key = (RsaKey*)rsa->internal;
42474241
enum wc_HashType mgf1;
42484242
hType = wc_OidGetHash((int)nid2oid(hashAlg, oidHashType));
42494243
if (mgf1Hash == WC_NID_undef)
42504244
mgf1Hash = hashAlg;
42514245
mgf1 = wc_OidGetHash((int)nid2oid(mgf1Hash, oidHashType));
42524246

4247+
/* handle compat layer salt special cases */
4248+
saltLen = rsa_pss_calc_salt(saltLen, wc_HashGetDigestSize(hType),
4249+
wolfSSL_RSA_size(rsa));
4250+
42534251
verLen = wc_RsaPSS_Verify_ex((byte*)sig, sigLen, sigDec, sigLen,
4254-
hType, wc_hash2mgf(mgf1), saltLen, (RsaKey*)rsa->internal);
4252+
hType, wc_hash2mgf(mgf1), saltLen, key);
42554253
if (verLen > 0) {
42564254
/* Check PSS padding is valid. */
42574255
if (wc_RsaPSS_CheckPadding_ex(hash, hLen, sigDec, (word32)verLen,
4258-
hType, saltLen,
4259-
mp_count_bits(&((RsaKey*)rsa->internal)->n)) != 0) {
4256+
hType, saltLen, mp_count_bits(&key->n)) != 0) {
42604257
WOLFSSL_ERROR_MSG("wc_RsaPSS_CheckPadding_ex error");
42614258
ret = WOLFSSL_FAILURE;
42624259
}

0 commit comments

Comments
 (0)