From 30ceba03c553188e18cb46789dd25ec293bdace3 Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 10 Jul 2026 07:20:10 +0200 Subject: [PATCH] dsa: gate CheckDsaLN on err==MP_OKAY in _DsaImportParamsRaw The (L,N) size check ran unconditionally, so after an earlier failure it overwrote the specific error (e.g. DH_CHECK_PUB_E from the p primality check) with BAD_FUNC_ARG, and computed qSz from a q that was never read (the q read is itself gated on err==MP_OKAY). Gate the size check the same way as the surrounding steps so the first, most specific error is returned. --- wolfcrypt/src/dsa.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/wolfcrypt/src/dsa.c b/wolfcrypt/src/dsa.c index 3ee03876fe..c9caf21306 100644 --- a/wolfcrypt/src/dsa.c +++ b/wolfcrypt/src/dsa.c @@ -546,13 +546,17 @@ static int _DsaImportParamsRaw(DsaKey* dsa, const char* p, const char* q, if (err == MP_OKAY) err = mp_read_radix(&dsa->g, g, MP_RADIX_HEX); - /* verify (L,N) pair bit lengths */ - pSz = mp_unsigned_bin_size(&dsa->p); - qSz = mp_unsigned_bin_size(&dsa->q); + /* verify (L,N) pair bit lengths - only when the reads above succeeded, so + * a more specific earlier error (e.g. DH_CHECK_PUB_E from the primality + * check) is not overwritten and qSz is not read from an unset q. */ + if (err == MP_OKAY) { + pSz = mp_unsigned_bin_size(&dsa->p); + qSz = mp_unsigned_bin_size(&dsa->q); - if (CheckDsaLN(pSz * WOLFSSL_BIT_SIZE, qSz * WOLFSSL_BIT_SIZE) != 0) { - WOLFSSL_MSG("Invalid DSA p or q parameter size"); - err = BAD_FUNC_ARG; + if (CheckDsaLN(pSz * WOLFSSL_BIT_SIZE, qSz * WOLFSSL_BIT_SIZE) != 0) { + WOLFSSL_MSG("Invalid DSA p or q parameter size"); + err = BAD_FUNC_ARG; + } } if (err != MP_OKAY) {