Repository navigation
Reject PKCS#7 digests too weak for the ML-DSA parameter set. Skip CertificateStatus for PSK and anonymous cipher suites. Reject nonzero unused bits in signature BIT STRINGs. - #12418
Conversation
Thanks to qzqdz for the report.
Fixes F-19347. Thanks to Alexander Liggesmeyer (CISPA Helmholtz Center for Information Security) and Anthropic for the reports.
|
@claude review |
|
There was a problem hiding this comment.
Separately from the inline findings, I checked the new unused-bits checks in wolfcrypt/src/asn.c and found no problems:
- In CheckCertSignature_ex and VerifyX509Acert, the
else ifchains still compile and behave correctly when WC_RSA_PSS is undefined. - Each new check runs only when
ret == 0, so it cannot overwrite an earlier error. - The new early return in ParseX509Acert frees dataASN, the same way the existing returns next to it do.
Extended reasoning...
The asn.c changes are small and follow existing patterns. I read each changed site to confirm the preprocessor branches are correct, no earlier error code is overwritten, and dataASN is freed. The PR also changes crypto and TLS paths in pkcs7.c and internal.c, and it has confirmed inline findings, so it should not be approved.
Findings marked 🟡 are optional suggestions and need no follow-up push.
| if (pkcs7->sidType != DEGENERATE_SID && | ||
| (pkcs7->publicKeyOID == ML_DSA_44k || | ||
| pkcs7->publicKeyOID == ML_DSA_65k || | ||
| pkcs7->publicKeyOID == ML_DSA_87k) && | ||
| wc_PKCS7_GetDefaultSignedAttribCount(pkcs7) + | ||
| pkcs7->signedAttribsSz > 0) { | ||
| ret = wc_PKCS7_MlDsaCheckDigest(pkcs7->publicKeyOID, pkcs7->hashOID); | ||
| if (ret != 0) { | ||
| return ret; | ||
| } |
There was a problem hiding this comment.
🔴 OpenSSL-compat users signing with ML-DSA-65 or ML-DSA-87 keys can no longer produce any SignedData; on the base branch PKCS7_sign plus i2d_PKCS7 worked. The new signing check rejects SHA-256 for these keys. wolfSSL_PKCS7_sign always sets hashOID = SHA256h (src/ssl_p7p12.c:572), and the compat API has no way to change it. Fix: when the signer is ML-DSA, the compat sign path should default to a digest that passes wc_PKCS7_MlDsaCheckDigest (SHA-512, as RFC 9882 requires and OpenSSL uses), as SLH-DSA already does with wc_PKCS7_SlhDsaDefaultHashOID.
Why this was flagged
The condition is an OPENSSL_EXTRA build with ML-DSA enabled. ML-DSA private keys load as WC_EVP_PKEY_DILITHIUM EVP_PKEYs (src/pk.c:6650-6658). wolfSSL_PKCS7_sign (src/ssl_p7p12.c:517) calls wc_PKCS7_InitWithCert, which sets publicKeyOID to ML_DSA_65k/ML_DSA_87k, then hard-codes p7->pkcs7.hashOID = SHA256h at src/ssl_p7p12.c:572. defaultSignedAttribs is left at 0, so wc_PKCS7_GetDefaultSignedAttribCount returns 3 (src/pkcs7.c:2777-2784). When the caller serializes via i2d_PKCS7/SMIME_write_PKCS7, wc_PKCS7_EncodeSignedData (src/ssl_p7p12.c:426) reaches the new block at wolfcrypt/src/pkcs7.c:4163-4172. There wc_PKCS7_MlDsaCheckDigest finds 32 < 48 or 32 < 64 and returns BAD_FUNC_ARG, so serialization fails. On the base branch the same calls produced a SignedData. The compat API cannot pick another digest: PKCS7_PARTIAL is rejected at src/ssl_p7p12.c:531, and nothing in the change sets an ML-DSA default hashOID the way wc_PKCS7_SlhDsaDefaultHashOID does at pkcs7.c:4186. tests/api/test_ossl_p7p12.c has no ML-DSA case, so the tests miss this.
Verification: wolfSSL_PKCS7_sign hard-codes p7->pkcs7.hashOID = SHA256h; at src/ssl_p7p12.c:572. The new check in PKCS7_EncodeSigned (pkcs7.c:4163-4173) calls wc_PKCS7_MlDsaCheckDigest. That function needs 48 bytes for ML_DSA_65k and 64 bytes for ML_DSA_87k, but SHA-256 gives 32, so it returns BAD_FUNC_ARG and i2d fails. On the base nothing checked the digest size.
| TEST_DECL(test_wolfSSL_inject_partial_record), | ||
| TEST_DECL(test_ocsp_status_callback), | ||
| TEST_DECL(test_ocsp_status_request_scr), | ||
| TEST_DECL(test_ocsp_status_request_psk_suite), |
There was a problem hiding this comment.
🟡 nit (optional): CI will not count the new PSK CertificateStatus test toward the module groups, so the new internal.c branch looks untested there. AGENTS.md requires tests to be registered with TEST_DECL_GROUP, and CI runs only the module groups. The test is added with plain TEST_DECL, so it is not in the "ocsp" group that the neighbouring entries use. Fix: register it as TEST_DECL_GROUP("ocsp", test_ocsp_status_request_psk_suite).
Why this was flagged
The new early return in SendCertificateStatus (src/internal.c:31597, usingPSK_cipher || usingAnon_cipher) is exercised only by test_ocsp_status_request_psk_suite (tests/api/test_ocsp.c). tests/api.c:50499 registers that test with TEST_DECL(test_ocsp_status_request_psk_suite), with no group. The entries right after it use TEST_DECL_GROUP("ocsp", ...). AGENTS.md says tests must be registered with TEST_DECL_GROUP because "CI runs only the unit.test groups that test its module", so a test outside the group "does not count". The result is that a per-group CI or MC/DC run does not cover the new internal.c decision, even though the test exists. The neighbouring test_ocsp_status_request_scr also uses plain TEST_DECL, but that only shows the rule has been missed before; it does not make it safe here.
Verification: The diff adds the test at tests/api.c:50499 as TEST_DECL(test_ocsp_status_request_psk_suite),, so the test belongs to no group. AGENTS.md:25-29 says tests for changes in src/ must be "registered with TEST_DECL_GROUP". So group-based coverage runs never execute this test, and they miss the new usingPSK_cipher || usingAnon_cipher early return in SendCertificateStatus.
…efore the SSL is created.
There was a problem hiding this comment.
🟡 Changes recommended
Missing feature guards cause build failures in supported configurations, and CSR signature validation still accepts nonzero unused bits.
7 open issues
Guard the call when CertManagerVerifyBuffer is unavailable · New Guard the PEM-dependent attribute-certificate block · New Use the CA fixture matching the configured encoding · New Exclude the fixture test when SHA-1 is disabled · New Guard test on the CBC cipher-suite build macro · New Register the server PSK callback before memory-I/O setup · New Reject nonzero unused bits in the CSR branch · New
What changed in this PR
Hardens wolfSSL’s PKCS#7 signature handling, certificate parsing, and OCSP stapling.
Changes:
- Enforce digest-strength requirements for ML-DSA signed attributes.
- Skip CertificateStatus for PSK and anonymous cipher suites.
- Reject nonzero unused signature bits and add regression tests.
| File | Description |
|---|---|
| wolfcrypt/test/test.c | Adds ML-DSA digest-strength tests. |
| wolfcrypt/src/pkcs7.c | Validates ML-DSA digest strength during encoding and verification. |
| wolfcrypt/src/asn.c | Rejects nonzero unused signature bits. |
| tests/api/test_ocsp.h | Declares the PSK stapling regression test. |
| tests/api/test_ocsp.c | Tests PSK stapling with and without certificates. |
| tests/api/test_asn.h | Declares and registers signature-bit tests. |
| tests/api/test_asn.c | Adds malformed signature-bit regression tests. |
| tests/api.c | Registers the PSK OCSP test. |
| src/internal.c | Skips inapplicable CertificateStatus messages and handles missing certificates. |
| certs/renewcerts.sh | Generates ML-DSA CMS digest fixtures. |
| certs/mldsa/include.am | Includes CMS fixtures in distribution archives. |
Review effort: Max
| ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, | ||
| (long)derSz, WOLFSSL_FILETYPE_ASN1), | ||
| pass ? WC_NO_ERR_TRACE(ASN_EXPECT_0_E) : WOLFSSL_SUCCESS); |
| } | ||
| #endif | ||
|
|
||
| #ifdef WOLFSSL_ACERT |
| int type; | ||
|
|
||
| ExpectNotNull(cm = wolfSSL_CertManagerNew()); | ||
| ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, "./certs/ca-cert.pem", NULL), |
| der = NULL; | ||
| #endif | ||
|
|
||
| #if defined(HAVE_OCSP) && defined(OPENSSL_EXTRA) |
| defined(HAVE_CERTIFICATE_STATUS_REQUEST) && !defined(WOLFSSL_NO_TLS12) && \ | ||
| !defined(NO_PSK) && defined(HAVE_ECC) && defined(HAVE_AES_CBC) && \ | ||
| !defined(NO_AES) && defined(WOLFSSL_AES_128) && !defined(NO_SHA256) && \ | ||
| !defined(NO_HMAC) |
| if (ctx_s != NULL) { | ||
| wolfSSL_SetIORecv(ctx_s, test_memio_read_cb); | ||
| wolfSSL_SetIOSend(ctx_s, test_memio_write_cb); | ||
| } |
| if (GetASNItem_UnusedBits(dataASN[X509CERTASN_IDX_SIGNATURE]) | ||
| != 0) { | ||
| ret = ASN_EXPECT_0_E; |
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #12418
Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): certs/mldsa/cms-mldsa87-sha384.der, certs/mldsa/cms-mldsa87-sha512.der, certs/mldsa/include.am, certs/renewcerts.sh, tests/api/test_asn.h, tests/api/test_ocsp.h
Coverage: 6 of 7 in-scope changed file(s) opened by the reviewer; not opened: tests/api.c
Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)
This review was generated automatically by Fenrir. Reported findings require changes before merge.
Review tier: Max
| break; | ||
| } | ||
| if (signedAttribSz > 0) { | ||
| ret = wc_PKCS7_MlDsaCheckDigest(pkcs7->publicKeyOID, |
There was a problem hiding this comment.
ML-DSA digest-strength check bypassed in stream-output verify via outer digestAlgorithms hash · PKCS7/CMS verification semantics
The new check tests SignerInfo hashOID, but in streamOutCb verify messageDigest is compared against a hash made with the outer digestAlgorithms entry (stream->hashType, unbound to hashOID), so setting SignerInfo digestAlgorithm to SHA-512 lets a SHA-1 ML-DSA-87 signature verify.
Suggested fix: Before using streamHash, require pkcs7->stream->hashType == wc_OidGetHash(pkcs7->hashOID) and fail verification on mismatch.
Basis: RFC 5652 §5.3 and §11.2: the messageDigest attribute is computed over eContent using the signer's SignerInfo digestAlgorithm.
Related known findings (similar but distinct; listed for context, not part of this finding)
- F-18631: that finding is the stream-output path skipping the content-hash comparison when certificates are present; here the comparison runs, but with an algorithm the new ML-DSA strength check never examines.
…k CSR signature unused bits. Fix test guards.
|
Thanks for the quick fix, Kareem! I reviewed the ML-DSA digest checks on both the signing and verification paths, and they look good to me. I also appreciate the regression tests covering both SHA-384 rejection and SHA-512 acceptance for ML-DSA-87. |


Description
Fixes zd#22948, zd#22949, #12402, F-19347
Testing
Built in tests, provided reproducers, added tests
Checklist