Skip to content

Commit 14d939b

Browse files
committed
Key-size check for RSA-PSS/SHA-256/384/512
1 parent a68176c commit 14d939b

1 file changed

Lines changed: 41 additions & 7 deletions

File tree

src/internal.c

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31062,10 +31062,42 @@ int SetSuitesHashSigAlgo(Suites* suites, const char* list)
3106231062
#endif /* OPENSSL_EXTRA */
3106331063

3106431064
#if !defined(NO_TLS) && (!defined(NO_WOLFSSL_SERVER) || !defined(NO_CERTS))
31065-
/* Smallest RSA key able to make an RSA-PSS/SHA-256 signature: emLen >= 2*hLen+2 = 66 bytes (RFC 8017 9.1.1, salt len = hash len). */
31066-
#define RSA_PSS_SHA256_MIN_SZ 66 /* bytes: 2*WC_SHA256_DIGEST_SIZE + 2 */
31067-
static int MatchSigAlgo(WOLFSSL* ssl, int sigAlgo)
31065+
#ifdef WC_RSA_PSS
31066+
/* Smallest RSA key able to make an RSA-PSS signature with the given hash:
31067+
* emLen >= 2*hLen + 2 (RFC 8017 9.1.1, salt length = hash length).
31068+
* Returns 0 when the hash isn't one used with RSA-PSS - no restriction. */
31069+
static word32 MinRsaPssKeySz(int hashAlgo)
31070+
{
31071+
word32 hLen;
31072+
31073+
switch (hashAlgo) {
31074+
#ifndef NO_SHA256
31075+
case sha256_mac:
31076+
hLen = WC_SHA256_DIGEST_SIZE;
31077+
break;
31078+
#endif
31079+
#ifdef WOLFSSL_SHA384
31080+
case sha384_mac:
31081+
hLen = WC_SHA384_DIGEST_SIZE;
31082+
break;
31083+
#endif
31084+
#ifdef WOLFSSL_SHA512
31085+
case sha512_mac:
31086+
hLen = WC_SHA512_DIGEST_SIZE;
31087+
break;
31088+
#endif
31089+
default:
31090+
return 0;
31091+
}
31092+
31093+
return 2 * hLen + 2;
31094+
}
31095+
#endif /* WC_RSA_PSS */
31096+
31097+
static int MatchSigAlgo(WOLFSSL* ssl, int hashAlgo, int sigAlgo)
3106831098
{
31099+
(void)hashAlgo;
31100+
3106931101
#ifdef HAVE_ED25519
3107031102
if (ssl->pkCurveOID == ECC_ED25519_OID) {
3107131103
/* Certificate has Ed25519 key, only match with Ed25519 sig alg */
@@ -31127,10 +31159,12 @@ static int MatchSigAlgo(WOLFSSL* ssl, int sigAlgo)
3112731159
return sigAlgo == rsa_pss_sa_algo;
3112831160
#endif
3112931161
/* TLS 1.2 and below - RSA-PSS allowed, but only if the RSA key is big
31130-
* enough to actually produce an RSA-PSS/SHA-256 signature. */
31162+
* enough to actually produce an RSA-PSS signature with this hash. */
3113131163
if (sigAlgo == rsa_pss_sa_algo) {
31132-
if (ssl->buffers.keySz != 0 &&
31133-
ssl->buffers.keySz < RSA_PSS_SHA256_MIN_SZ)
31164+
word32 minSz = MinRsaPssKeySz(hashAlgo);
31165+
31166+
if (minSz != 0 && ssl->buffers.keySz != 0 &&
31167+
(word32)ssl->buffers.keySz < minSz)
3113431168
return 0;
3113531169
return 1;
3113631170
}
@@ -31280,7 +31314,7 @@ int PickHashSigAlgo(WOLFSSL* ssl, const byte* hashSigAlgo, word32 hashSigAlgoSz,
3128031314
if (hashAlgo < minHash)
3128131315
continue;
3128231316
/* Keep looking if signature algorithm isn't supported by cert. */
31283-
if (!MatchSigAlgo(ssl, sigAlgo))
31317+
if (!MatchSigAlgo(ssl, hashAlgo, sigAlgo))
3128431318
continue;
3128531319

3128631320
if (matchSuites) {

0 commit comments

Comments
 (0)