From fca6e23764705590e87f6f24763ff4a33fb35b4a Mon Sep 17 00:00:00 2001 From: Anthony Hu Date: Mon, 23 Mar 2026 15:49:24 -0400 Subject: [PATCH 1/3] Add upper limit to PBKDF iteration count Add WC_PBKDF_MAX_ITERATIONS (default 100000) to cap the iteration count in wc_PBKDF1_ex(), wc_PBKDF2_ex(), and wc_PKCS12_PBKDF_ex(). --- wolfcrypt/src/pwdbased.c | 15 +++++ wolfcrypt/test/test.c | 124 +++++++++++++++++++++++++++++++++++ wolfssl/wolfcrypt/pwdbased.h | 9 +++ 3 files changed, 148 insertions(+) diff --git a/wolfcrypt/src/pwdbased.c b/wolfcrypt/src/pwdbased.c index 68e3ab16355..75251af6a7e 100644 --- a/wolfcrypt/src/pwdbased.c +++ b/wolfcrypt/src/pwdbased.c @@ -79,6 +79,11 @@ int wc_PBKDF1_ex(byte* key, int keyLen, byte* iv, int ivLen, if (iterations <= 0) iterations = 1; + if (iterations > WC_PBKDF_MAX_ITERATIONS) { + WOLFSSL_MSG("PBKDF1 iteration count exceeds WC_PBKDF_MAX_ITERATIONS"); + return BAD_FUNC_ARG; + } + hashT = wc_HashTypeConvert(hashType); err = wc_HashGetDigestSize(hashT); if (err < 0) @@ -215,6 +220,11 @@ int wc_PBKDF2_ex(byte* output, const byte* passwd, int pLen, const byte* salt, if (iterations <= 0) iterations = 1; + if (iterations > WC_PBKDF_MAX_ITERATIONS) { + WOLFSSL_MSG("PBKDF2 iteration count exceeds WC_PBKDF_MAX_ITERATIONS"); + return BAD_FUNC_ARG; + } + hashT = wc_HashTypeConvert(hashType); hLen = wc_HashGetDigestSize(hashT); if (hLen < 0) @@ -410,6 +420,11 @@ int wc_PKCS12_PBKDF_ex(byte* output, const byte* passwd, int passLen, if (iterations <= 0) iterations = 1; + if (iterations > WC_PBKDF_MAX_ITERATIONS) { + WOLFSSL_MSG("PKCS12 PBKDF iteration count exceeds WC_PBKDF_MAX_ITERATIONS"); + return BAD_FUNC_ARG; + } + hashT = wc_HashTypeConvert(hashType); ret = wc_HashGetDigestSize(hashT); if (ret < 0) diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 80866209c5f..48086b4b2f4 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -30407,6 +30407,20 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_pbkdf_test(void) if (XMEMCMP(derived, verify2, 24) != 0) return WC_TEST_RET_ENC_NC; + /* Test iteration cap: iterations above WC_PBKDF_MAX_ITERATIONS must be + * rejected to prevent CPU exhaustion DoS via crafted PKCS#12 files. */ + ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8, + WC_PBKDF_MAX_ITERATIONS + 1, kLen, WC_SHA256, + id, HEAP_HINT); + if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) + return WC_TEST_RET_ENC_NC; + + ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8, + WC_PBKDF_MAX_ITERATIONS, kLen, WC_SHA256, + id, HEAP_HINT); + if (ret < 0) + return WC_TEST_RET_ENC_EC(ret); + return 0; } #endif /* HAVE_PKCS12 && !NO_SHA256 */ @@ -30438,6 +30452,13 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf2_test(void) if (XMEMCMP(derived, verify, sizeof(verify)) != 0) return WC_TEST_RET_ENC_NC; + /* Test iteration cap */ + ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd), + salt, (int)sizeof(salt), WC_PBKDF_MAX_ITERATIONS + 1, + kLen, WC_SHA256, HEAP_HINT, devId); + if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) + return WC_TEST_RET_ENC_NC; + return 0; } @@ -30469,6 +30490,13 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf1_test(void) if (XMEMCMP(derived, verify, sizeof(verify)) != 0) return WC_TEST_RET_ENC_NC; + /* Test iteration cap */ + ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd, + (int)XSTRLEN(passwd), salt, (int)sizeof(salt), + WC_PBKDF_MAX_ITERATIONS + 1, WC_SHA, HEAP_HINT); + if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) + return WC_TEST_RET_ENC_NC; + return 0; } #endif /* HAVE_PBKDF2 && !NO_SHA */ @@ -30493,6 +30521,52 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void) if (ret != 0) return ret; #endif +#ifdef HAVE_PKCS12 + /* Test that a crafted PKCS#12 with INT_MAX MAC iterations is rejected + * immediately rather than hanging in DoPKCS12Hash(). */ + { + static const byte evil_p12[] = { + 0x30, 0x58, 0x02, 0x01, 0x03, 0x30, 0x1e, 0x06, + 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, + 0x07, 0x01, 0xa0, 0x11, 0x04, 0x0f, 0x30, 0x0d, + 0x30, 0x0b, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, + 0xf7, 0x0d, 0x01, 0x07, 0x01, 0x30, 0x33, 0x30, + 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, + 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x04, 0x08, 0x41, 0x41, 0x41, 0x41, + 0x41, 0x41, 0x41, 0x41, 0x02, 0x04, 0x7f, 0xff, + 0xff, 0xff + }; + WC_PKCS12* evilPkcs12 = wc_PKCS12_new_ex(HEAP_HINT); + if (evilPkcs12 == NULL) + return WC_TEST_RET_ENC_EC(MEMORY_E); + + ret = wc_d2i_PKCS12(evil_p12, (word32)sizeof(evil_p12), evilPkcs12); + if (ret == 0) { + byte* evilKey = NULL; + byte* evilCert = NULL; + word32 evilKeySz = 0, evilCertSz = 0; + WC_DerCertList* evilCa = NULL; + + ret = wc_PKCS12_parse(evilPkcs12, "test", &evilKey, &evilKeySz, + &evilCert, &evilCertSz, &evilCa); + XFREE(evilKey, HEAP_HINT, DYNAMIC_TYPE_PKCS); + XFREE(evilCert, HEAP_HINT, DYNAMIC_TYPE_PKCS); + if (evilCa) + wc_FreeCertList(evilCa, HEAP_HINT); + wc_PKCS12_free(evilPkcs12); + /* Parse must fail (iteration cap), not succeed or hang */ + if (ret == 0) + return WC_TEST_RET_ENC_NC; + } + else { + wc_PKCS12_free(evilPkcs12); + } + ret = 0; + } +#endif /* HAVE_PKCS12 */ #ifdef HAVE_SCRYPT ret = scrypt_test(); if (ret != 0) @@ -30592,6 +30666,56 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void) goto out; } + /* Test that a crafted PKCS#12 with INT_MAX MAC iterations is rejected + * immediately rather than hanging in DoPKCS12Hash(). This is a 90-byte + * minimal PKCS#12 with mac->itt = 0x7FFFFFFF (2,147,483,647). */ + { + static const byte evil_p12[] = { + 0x30, 0x58, 0x02, 0x01, 0x03, 0x30, 0x1e, 0x06, + 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, + 0x07, 0x01, 0xa0, 0x11, 0x04, 0x0f, 0x30, 0x0d, + 0x30, 0x0b, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, + 0xf7, 0x0d, 0x01, 0x07, 0x01, 0x30, 0x33, 0x30, + 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, + 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x04, 0x08, 0x41, 0x41, 0x41, 0x41, + 0x41, 0x41, 0x41, 0x41, 0x02, 0x04, 0x7f, 0xff, + 0xff, 0xff + }; + WC_PKCS12* evilPkcs12 = wc_PKCS12_new_ex(HEAP_HINT); + if (evilPkcs12 == NULL) { + ret = WC_TEST_RET_ENC_EC(MEMORY_E); + goto out; + } + ret = wc_d2i_PKCS12(evil_p12, (word32)sizeof(evil_p12), evilPkcs12); + if (ret != 0) { + wc_PKCS12_free(evilPkcs12); + ret = WC_TEST_RET_ENC_EC(ret); + goto out; + } + { + byte* evilKey = NULL; + byte* evilCert = NULL; + word32 evilKeySz = 0, evilCertSz = 0; + WC_DerCertList* evilCa = NULL; + ret = wc_PKCS12_parse(evilPkcs12, "test", &evilKey, &evilKeySz, + &evilCert, &evilCertSz, &evilCa); + XFREE(evilKey, HEAP_HINT, DYNAMIC_TYPE_PKCS); + XFREE(evilCert, HEAP_HINT, DYNAMIC_TYPE_PKCS); + if (evilCa) + wc_FreeCertList(evilCa, HEAP_HINT); + } + wc_PKCS12_free(evilPkcs12); + /* Must have been rejected (not hung) */ + if (ret == 0) { + ret = WC_TEST_RET_ENC_NC; + goto out; + } + ret = 0; /* rejection is the expected outcome */ + } + out: if (derCaListOut) diff --git a/wolfssl/wolfcrypt/pwdbased.h b/wolfssl/wolfcrypt/pwdbased.h index fc2eddfd8dd..fb56df627f4 100644 --- a/wolfssl/wolfcrypt/pwdbased.h +++ b/wolfssl/wolfcrypt/pwdbased.h @@ -35,6 +35,15 @@ extern "C" { #endif +/* Maximum allowed PBKDF iteration count to prevent CPU exhaustion DoS. + * Attacker-controlled PKCS#12 files can specify iterations up to INT_MAX + * (2,147,483,647) in the MAC data, causing hours of CPU time. + * Override by defining WC_PBKDF_MAX_ITERATIONS before including this header. + * Normal PKCS#12 files use 1,000–10,000 iterations. */ +#ifndef WC_PBKDF_MAX_ITERATIONS + #define WC_PBKDF_MAX_ITERATIONS 100000 +#endif + #if FIPS_VERSION3_GE(6,0,0) extern const unsigned int wolfCrypt_FIPS_pbkdf_ro_sanity[2]; WOLFSSL_LOCAL int wolfCrypt_FIPS_PBKDF_sanity(void); From 8fa5217c3406e63bf777c711fa31273ed4cf3d69 Mon Sep 17 00:00:00 2001 From: Anthony Hu Date: Thu, 26 Mar 2026 23:34:14 -0400 Subject: [PATCH 2/3] simplify the tests. --- wolfcrypt/test/test.c | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 48086b4b2f4..673dac4cc5b 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -30407,20 +30407,6 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_pbkdf_test(void) if (XMEMCMP(derived, verify2, 24) != 0) return WC_TEST_RET_ENC_NC; - /* Test iteration cap: iterations above WC_PBKDF_MAX_ITERATIONS must be - * rejected to prevent CPU exhaustion DoS via crafted PKCS#12 files. */ - ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8, - WC_PBKDF_MAX_ITERATIONS + 1, kLen, WC_SHA256, - id, HEAP_HINT); - if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) - return WC_TEST_RET_ENC_NC; - - ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8, - WC_PBKDF_MAX_ITERATIONS, kLen, WC_SHA256, - id, HEAP_HINT); - if (ret < 0) - return WC_TEST_RET_ENC_EC(ret); - return 0; } #endif /* HAVE_PKCS12 && !NO_SHA256 */ @@ -30452,13 +30438,6 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf2_test(void) if (XMEMCMP(derived, verify, sizeof(verify)) != 0) return WC_TEST_RET_ENC_NC; - /* Test iteration cap */ - ret = wc_PBKDF2_ex(derived, (byte*)passwd, (int)XSTRLEN(passwd), - salt, (int)sizeof(salt), WC_PBKDF_MAX_ITERATIONS + 1, - kLen, WC_SHA256, HEAP_HINT, devId); - if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) - return WC_TEST_RET_ENC_NC; - return 0; } @@ -30490,13 +30469,6 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf1_test(void) if (XMEMCMP(derived, verify, sizeof(verify)) != 0) return WC_TEST_RET_ENC_NC; - /* Test iteration cap */ - ret = wc_PBKDF1_ex(derived, kLen, NULL, 0, (byte*)passwd, - (int)XSTRLEN(passwd), salt, (int)sizeof(salt), - WC_PBKDF_MAX_ITERATIONS + 1, WC_SHA, HEAP_HINT); - if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) - return WC_TEST_RET_ENC_NC; - return 0; } #endif /* HAVE_PBKDF2 && !NO_SHA */ From 728d695ad17f8ed8fd70226a2c4adc7042aa9929 Mon Sep 17 00:00:00 2001 From: Anthony Hu Date: Fri, 27 Mar 2026 00:32:40 -0400 Subject: [PATCH 3/3] better macro gating in tests --- wolfcrypt/test/test.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 673dac4cc5b..b7be152ca5b 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -30493,7 +30493,8 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void) if (ret != 0) return ret; #endif -#ifdef HAVE_PKCS12 +#if defined(HAVE_PKCS12) && !defined(NO_ASN) && !defined(NO_PWDBASED) && \ + !defined(NO_HMAC) && !defined(NO_CERTS) /* Test that a crafted PKCS#12 with INT_MAX MAC iterations is rejected * immediately rather than hanging in DoPKCS12Hash(). */ { @@ -30538,7 +30539,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void) } ret = 0; } -#endif /* HAVE_PKCS12 */ +#endif /* HAVE_PKCS12 && !NO_ASN && !NO_PWDBASED && !NO_HMAC && !NO_CERTS */ #ifdef HAVE_SCRYPT ret = scrypt_test(); if (ret != 0)