@@ -31062,8 +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- 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)
3106631070{
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)
31098+ {
31099+ (void)hashAlgo;
31100+
3106731101#ifdef HAVE_ED25519
3106831102 if (ssl->pkCurveOID == ECC_ED25519_OID) {
3106931103 /* Certificate has Ed25519 key, only match with Ed25519 sig alg */
@@ -31124,9 +31158,16 @@ static int MatchSigAlgo(WOLFSSL* ssl, int sigAlgo)
3112431158 if (IsAtLeastTLSv1_3(ssl->version))
3112531159 return sigAlgo == rsa_pss_sa_algo;
3112631160 #endif
31127- /* TLS 1.2 and below - RSA-PSS allowed. */
31128- if (sigAlgo == rsa_pss_sa_algo)
31161+ /* TLS 1.2 and below - RSA-PSS allowed, but only if the RSA key is big
31162+ * enough to actually produce an RSA-PSS signature with this hash. */
31163+ if (sigAlgo == rsa_pss_sa_algo) {
31164+ word32 minSz = MinRsaPssKeySz(hashAlgo);
31165+
31166+ if (minSz != 0 && ssl->buffers.keySz != 0 &&
31167+ (word32)ssl->buffers.keySz < minSz)
31168+ return 0;
3112931169 return 1;
31170+ }
3113031171 }
3113131172#endif
3113231173#ifdef HAVE_ECC_BRAINPOOL
@@ -31273,7 +31314,7 @@ int PickHashSigAlgo(WOLFSSL* ssl, const byte* hashSigAlgo, word32 hashSigAlgoSz,
3127331314 if (hashAlgo < minHash)
3127431315 continue;
3127531316 /* Keep looking if signature algorithm isn't supported by cert. */
31276- if (!MatchSigAlgo(ssl, sigAlgo))
31317+ if (!MatchSigAlgo(ssl, hashAlgo, sigAlgo))
3127731318 continue;
3127831319
3127931320 if (matchSuites) {
0 commit comments