From 107cc82a06c19946da1520c1e5f4d6f0bcb6efb3 Mon Sep 17 00:00:00 2001 From: jordan Date: Thu, 27 Jun 2024 10:45:02 -0500 Subject: [PATCH] Fixes ZD 18204: check hashsigalgo matches ssl suites. --- src/internal.c | 50 ++++++++++++++++++++++++++++++++++++++++++++-- wolfssl/internal.h | 2 ++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/internal.c b/src/internal.c index c798b8c02..ba491bce5 100644 --- a/src/internal.c +++ b/src/internal.c @@ -27368,7 +27368,45 @@ static byte MinHashAlgo(WOLFSSL* ssl) return sha_mac; } +/* Check if a given peer hashSigAlgo is supported in our ssl->suites or + * ssl->ctx->suites. + * + * Returns 1 on match. + * Returns 0 otherwise. + * */ +static int SupportedHashSigAlgo(WOLFSSL* ssl, const byte * hashSigAlgo) +{ + const Suites * suites = NULL; + word32 i = 0; + + if (ssl == NULL || hashSigAlgo == NULL) { + return 0; + } + + suites = WOLFSSL_SUITES(ssl); + + if (suites == NULL || suites->hashSigAlgoSz == 0) { + return 0; + } + + for (i = 0; (i+1) < suites->hashSigAlgoSz; i += HELLO_EXT_SIGALGO_SZ) { + if (XMEMCMP(&suites->hashSigAlgo[i], hashSigAlgo, + HELLO_EXT_SIGALGO_SZ) == 0) { + /* Match found. */ + return 1; + } + } + + return 0; +} + int PickHashSigAlgo(WOLFSSL* ssl, const byte* hashSigAlgo, word32 hashSigAlgoSz) +{ + return PickHashSigAlgo_ex(ssl, hashSigAlgo, hashSigAlgoSz, 0); +} + +int PickHashSigAlgo_ex(WOLFSSL* ssl, const byte* hashSigAlgo, word32 hashSigAlgoSz, + int matchSuites) { word32 i; int ret = WC_NO_ERR_TRACE(MATCH_SUITE_ERROR); @@ -27409,6 +27447,14 @@ int PickHashSigAlgo(WOLFSSL* ssl, const byte* hashSigAlgo, word32 hashSigAlgoSz) if (!MatchSigAlgo(ssl, sigAlgo)) continue; + if (matchSuites) { + /* Keep looking if peer algorithm isn't supported in our ssl->suites + * or ssl->ctx->suites. */ + if (!SupportedHashSigAlgo(ssl, &hashSigAlgo[i])) { + continue; + } + } + #ifdef HAVE_ED25519 if (ssl->pkCurveOID == ECC_ED25519_OID) { /* Matched Ed25519 - set chosen and finished. */ @@ -35913,8 +35959,8 @@ static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx, ret = SetCipherSpecs(ssl); if (ret != 0) return ret; - ret = PickHashSigAlgo(ssl, peerSuites->hashSigAlgo, - peerSuites->hashSigAlgoSz); + ret = PickHashSigAlgo_ex(ssl, peerSuites->hashSigAlgo, + peerSuites->hashSigAlgoSz, 1); if (ret != 0) return ret; diff --git a/wolfssl/internal.h b/wolfssl/internal.h index 7bac1f6cf..fa7e32352 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -2181,6 +2181,8 @@ WOLFSSL_LOCAL int CompleteServerHello(WOLFSSL *ssl); WOLFSSL_LOCAL int CheckVersion(WOLFSSL *ssl, ProtocolVersion pv); WOLFSSL_LOCAL int PickHashSigAlgo(WOLFSSL* ssl, const byte* hashSigAlgo, word32 hashSigAlgoSz); +WOLFSSL_LOCAL int PickHashSigAlgo_ex(WOLFSSL* ssl, const byte* hashSigAlgo, + word32 hashSigAlgoSz, int matchSuites); #if defined(WOLF_PRIVATE_KEY_ID) && !defined(NO_CHECK_PRIVATE_KEY) WOLFSSL_LOCAL int CreateDevPrivateKey(void** pkey, byte* data, word32 length, int hsType, int label, int id,