Repository navigation
Conversation
|
90590a6 to
d3e3582
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Restoration can still lose inherited restrictions and OCSP issuer metadata, while mandatory DER retention exposes a static-memory allocation failure.
3 open issues
What changed in this PR
This PR changes wolfSSL’s persistent CA cache to rebuild signers from certificate DER, aiming to preserve certificate restrictions across save and restore.
Changes:
- Introduces cache format version 2, storing DER and signer type and rejecting the old format.
- Rebuilds signers in a private table, reapplies inherited path limits, and replaces the live table only after successful restoration.
- Adds name-constraint and inherited path-length regression tests.
| File | Description |
|---|---|
wolfssl/wolfcrypt/settings.h |
Enables signer DER retention for persistent certificate caches. |
tests/api/test_certman.h |
Registers the two cache regression tests. |
tests/api/test_certman.c |
Tests name constraints and inherited path limits after restoration. |
src/ssl_certman.c |
Implements the new cache format and transactional signer reconstruction. |
Review effort: Balanced
60c7daf to
e46d64f
Compare
|
retest this please |
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11875
Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): tests/api/test_certman.h
Coverage: 3 of 4 in-scope changed file(s) opened by the reviewer; not opened: tests/api/test_certman.c
Findings: 3
3 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
|
@sameehj can you investigate into the PRB-master-job failure and recent Fenrir bot comments? |
8ec958d to
58b2aa9
Compare
|
@JacobBarthelmeh Thanks. I fixed the three Fenrir findings, added a regression test, and replied on each thread:
The last PRB-master-job run was skipped because the PR had merge conflicts. I rebased the PR on master, and the CI is running again. I do not have access to the Jenkins console, so I could not read the earlier failure. If the new run fails, I will look at it. |
58b2aa9 to
fbda238
Compare
|
Rebased onto current master. The review threads are answered. The failed check 1.31.1.1 stopped on a timeout while downloading from openresty.org. I started a re-run of that job. |
|
@JacobBarthelmeh Ready for a review |
MarkAtwood
left a comment
There was a problem hiding this comment.
approve, with one ask: this needs a changelog entry before it merges.
all six earlier findings check out. the branch was force pushed so the reply commits arent in the tree any more, and i verified against the code as it now stands rather than against the replies.
copilot:
- inherited restrictions lost when the post pass cant find the issuer: fixed. an effective path length is serialized per entry (1619-1628) and applied as a hard ceiling in
cm_restore_clamp_pathlen(2092-2107). issuerKeyHashleft zero for non self issued signers underHAVE_OCSP: fixed. serialized in the entry prefix (1301-1310), restored unconditionally (1557-1567), re-derived from the restored issuer when present (2110-2131). the unconditional overwrite is the right call,ParseCertwould otherwise leave a hash derived from the old live table and that gets correctly discarded.FillSignerpassing NULL heap and breaking static memory pools: fixed atasn.c:27075, nowcert->heap, soAllocDerandFreeDeragree, and all three call sites supply a heap outliving the cert manager.
fenrir:
- use after free on the static memory heap hint: fixed, also by
asn.c:27075. note thesrc/internal.cdiff in this pr is comment only,SSL_CM(ssl)->heapat 17939 was already in the base, so asn.c is what actually fixes it. - int overflow in summed row counts: fixed, 2196-2209 bounds each column by
maxSignersminus the running count, derived from the real remaining buffer. - missing
unknownExtCallback32: fixed, 1445-1450 sets both callbacks in AddCAs order.
the property i wanted established
final maxPathLen is min(recomputed, saved) per signer, and the recompute only ever lowers since next is capped at refs[i].base (2070). so restore can never come back looser than save. that holds in both configs copilot named, including IGNORE_NAME_CONSTRAINTS with ocsp/crl/akid off, where cm_table_find_issuer always returns NULL, the recompute is skipped, and the clamp alone holds the saved value.
the load bearing detail is the clamps indexing, since saved[i] is indexed by table order while savedPath is filled in buffer order. they agree because AddCA buckets at HashSigner(subjectKeyIdHash) (3865) and restore recomputes the same hash from the same der and appends to the tail (1570), so source row i lands in bucket i in read order, and the savedPath write and the table append are 1:1 inside one ret == 0 chain (1554-1583). if that ordering were off the clamp would put the wrong ceiling on the wrong signer. it isnt off.
full signer round trip: 22 fields reproduced cleanly from the serialized der via ParseCert + FillSigner. maxPathLen and issuerKeyHash arent der derivable and are now explicitly serialized. type being saved as one byte is an improvement, the baseline lost it and restored 0 which isnt a valid enum value. selfSigned isnt a plain der field but is reproduced, since ConfirmSelfSignature does run under NO_VERIFY (asn.c:26980-26991, gated only on verify != VERIFY_NAME).
also checked and clean: an old cache is rejected rather than misread, the header layout is byte identical so the version field sits at the same offset both directions (2172-2179) and signerSz additionally discriminates HAVE_OCSP and NO_SKID. bounds on the untrusted restore buffer are clean and better than the baseline, both length reads checked against the remaining buffer before use (1525, 1545) with no pointer arithmetic past the object, unlike the baselines start + minSz + pubKeySize > end. the issuer missing fallback fails closed. and partial restore is the best part of the change, a private table swapped in only on full success under caLock (2258), memset before cleanup so nothing double frees, which is also why GetCA taking caLock itself doesnt deadlock.
the ask
src/ssl_certman.c:1260 bumps the cache format from 1 to 3 and the ChangeLog.md diff is empty. two things users will hit:
- an upgraded deployment calling
RestoreCertCacheon an existing v1 file getsCACHE_MATCH_ERRORand has to regenerate every cache PERSIST_CERT_CACHEnow force definesWOLFSSL_SIGNER_DER_CERT, so per ca ram grows by the full der
one line covering the format bump and the memory change is enough, and thats the only thing im holding this for.
happy as separate issues, not blocking
src/ssl_certman.c:1569-1583vs3880: AddCA setssigner->cm_idx = rowbut restore never does, so every restored signer hascm_idx0. on renesas tsip/fspsmRenesas_cmn_checkCAisreturn (cmIdx == g_CAscm_Idx), so ifg_CAscm_Idxis 0 every restored ca claims to be the tsip verified root (asn.c:26797), otherwise tsip is just unavailable. pre existing, the baseline restore didnt set it either. one line at the append site.src/ssl_certman.c:1561:savedPath[*savedCount]has no explicit bound and is safe only by an invariant computed two functions away. not reachable today, but any future change tosignerCountturns it into a heap write. passsignerCountin and gate on it.src/ssl_certman.c:1362-1375:cm_get_cert_cache_mem_sizeaccumulates inintwith no overflow check, and per signer bytes went from about 100 to the full der, so the existing open fenrir finding on this is roughly 20x more reachable. still about 1M cas to trigger. accumulate insize_tand fail pastINT_MAX, or call that one explicitly out of scope here.
tests
all three added tests genuinely fail if the fix is reverted, which is the question i actually cared about:
NameConstraint_cert_cache: the baseline format never serializedpermittedNames/excludedNames, so the attacker leaf would verify instead of returningASN_NAME_INVALID_E.PathLen_cert_cache: its first restore, all 4 cas, is satisfied by the recompute alone and doesnt test the clamp. the second restore with ica4 removed does: ica3 has no pathLength extension so its seeded atWOLFSSL_MAX_PATH_LEN,apply_pathlenskips it because its issuer is absent, ica2 then computes MAX-1, and onlycm_restore_clamp_pathlenpulls ica3 to its saved 1 and ica2 to its saved 0 to produce the assertedASN_PATHLEN_INV_E.cert_cache_bad_count: the unguarded sum wraps to exactly 0,savedPathis never allocated and 1557 returnsMEMORY_E, not the assertedBUFFER_E. so the missing bound doesnt actually cause a heap overflow, thesavedPath == NULLguard catches it, but the test still discriminates.
fc0fa58b61 was the right fix and is load bearing: the test now calls wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_DEFAULT, NULL) on every context before loading, and without it the ica1 load succeeds and both negative assertions are vacuous.
oracles are independent, fixed error codes and rfc 5280 semantics, and the ocsp hash assertions compare against values captured from the live table before the save, so the reference path rather than self validation.
one coverage gap, not a defect: nothing tests IGNORE_NAME_CONSTRAINTS with ocsp, crl and akid all off, because the tests own guard excludes that build. the clamp makes it safe by construction there anyway.
to be straight about the evidence: the "fails without the fix" conclusions above are traced from source, nobody re-ran the suite with the clamp removed. your green ci is the evidence for the passing direction.
fbda238 to
42e5745
Compare
|
@mark-atwood The changelog line is in 42e5745. The branch is one commit, rebased onto 3fd873c.
|
MarkAtwood
left a comment
There was a problem hiding this comment.
re-approve. code and tests are byte identical to fbda238, which i already approved, and the only new file is the changelog i asked for.
it covers the version 3 bump, CACHE_MATCH_ERROR on a version 1 restore, the per-CA DER now kept in memory, and regenerating saved caches.
make check (2) is cancelled, not failed, 45m16s with no failure logs, same cap as #11972. the other 445 passed on the rebased base.
42e5745 to
174aeb1
Compare
|
Rebased onto current master (833186a). The content conflict was in tests/api/test_certman.c and tests/api/test_certman.h. The branch keeps the new master name-constraint tests and the persisted-cache tests. src/internal.c, src/ssl_certman.c, and wolfcrypt/src/asn.c already contained both changes. The name-constraint cache test, the path-length cache test, the bad row-count test, and the new master chain-CA name-constraint tests pass locally with |
|
retest this please |
|
@claude review |
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
Additional findings (outside the current diff — GitHub can't attach inline comments there):
-
🟡
src/ssl_certman.c— If one cached CA fails to re-parse, the whole cache restore fails and the CA table stays as it was. This can happen when the unknown-extension callback is not registered before restore. cm_restore_signer() runs ParseCert with whatever callbacks are registered at restore time. A CA that was accepted at save time only because a callback handled its critical custom extension now fails with ASN_CRIT_EXT_E. Fix: document that the callbacks must be set before restoring the cache, or record the acceptance at save time. Another option is to skip the one entry that fails instead of aborting the restore.Why this was flagged
The condition is an application that loads CAs with a critical private extension, accepted through wolfSSL_CertManagerSetUnknownExtCallback (ssl_certman.c:751), and later calls wolfSSL_CTX_restore_cert_cache before registering that callback on the new context. cm_restore_signer (ssl_certman.c:151-161) copies cm->unknownExtCallback only if it is set, then calls ParseCert(cert, CA_TYPE, NO_VERIFY, cm). That call returns the critical-extension error for that CA. cm_restore_cert_row returns the error and CM_MemRestoreCertCache aborts, so none of the cached CAs are restored. On the base branch, restore copied the stored key and name fields directly. It never re-parsed the certificate, so the same cache restored without the callback. Nothing in the API or the ChangeLog entry says the callbacks must be registered before restore.
Verification: cm_restore_signer (src/ssl_certman.c ~1443-1452) attaches the unknown-ext callbacks only if non-NULL, then calls ParseCert. ParseCertRelative ends with
if (cert->criticalExt != 0) return cert->criticalExt;(asn.c ~27302). CM_MemRestoreCertCache (ssl_certman.c ~2223-2229) breaks onadded < 0, so the whole restore fails. On the base branch, restore never parsed. -
🟣
src/ssl_certman.c— After a cache restore, mutually cross-certified CAs can end up with path length 0, so valid chains through them fail with ASN_PATHLEN_INV_E. The fixed-point loop in cm_restore_apply_pathlen() repeatscounttimes. When two non-self-issued CAs name each other as issuer, every pass lowers both values, which can drive them to 0. Fix: compute each CA's inherited limit once in issuer-first order, as ParseCert did at load time, and stop at a cycle. Alternatively, never lower a signer below the value it had when the cache was saved.Why this was flagged
The trigger is a CA table holding two non-self-issued CAs, X issued by Y and Y issued by X, matched by AKID/SKID or by name in cm_table_find_issuer(). This can happen with bridge-style cross-certificates when the self-signed roots with the same SKIDs are not in the table. At load time ParseCert saw only the issuer already present, so the saved values were X=127 and Y=126. On restore, each pass of the
for (guard = 0; guard < count; guard++)loop (ssl_certman.c:606-644) sets X = Y-1, then Y = X-1. Both drop by about 2 per pass and are clamped at 0 once the cache holds about 64 or more CAs. cm_restore_clamp_pathlen() only lowers values, so it cannot undo this. An intermediate issued by X or Y then hitscert->ca->maxPathLen == 0in ParseCert (asn.c:26965) and is rejected with ASN_PATHLEN_INV_E. Before this change, the restore did not recompute inherited path lengths and loaded each saved CA without this loop.Verification: pre-existing. Trigger: the restored CA table holds a cycle of non-self-issued CAs that resolve to each other as issuers through cm_table_find_issuer(), for example mutual cross-certificates loaded without the matching self-signed roots.
How it happens: cm_restore_apply_pathlen() (src/ssl_certman.c:2002-2087; the candidate's line 644 is wrong) first sets refs[i].base to each signer's own pathLength. It then runs
for (guard = 0; guard < count; guard++). Each pass sets s->maxPathLen to min(issuer->maxPathLen - 1, base), or to 0 when the issuer's value is 0 (lines 2065-2072).
Frauschi
left a comment
There was a problem hiding this comment.
Main point is that the saved path length makes the restore-time inheritance pass unnecessary; the rest are small.
|
@Frauschi @julek-wolfssl The notes from Dec 2 are in c697171. Please review again. |
PERSIST_CERT_CACHE is now version 3. Restore rebuilds each signer from its saved DER and defines WOLFSSL_SIGNER_DER_CERT. A build that also defines WOLFSSL_NO_SIGNER_DER_CERT fails at compile time. Restore keeps the path length that was in use. A CA that is not self-signed takes its issuer key hash from the restored issuer when the authority key id can be checked.
c6302cb to
6019c0b
Compare
|
Rebased onto current master and squashed to one commit, 6019c0b.
|
There was a problem hiding this comment.
🟡 Changes recommended
Cache restoration can lose issuer links, rejecting valid chains or skipping ancestor name constraints, and the new test can write out of bounds for small CA tables.
3 open issues
3 resolved since last review
Review effort: Balanced
| else { | ||
| cert->maxPathLen = (word16)WOLFSSL_MAX_PATH_LEN; | ||
| } | ||
| ret = FillSigner(signer, cert, type, der); |
| while (s != NULL) { | ||
| Signer* issuer; | ||
|
|
||
| if (!s->selfSigned) { |
| int test_wolfSSL_CertManager_cert_cache_bad_count(void) | ||
| { | ||
| EXPECT_DECLS; | ||
| #if defined(PERSIST_CERT_CACHE) && !defined(NO_FILESYSTEM) && \ |
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #11875
Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): ChangeLog.md, doc/dox_comments/header_files/ssl.h, tests/api/test_certman.h
Coverage: 2 of 4 in-scope changed file(s) opened by the reviewer; not opened: src/internal.c, tests/api/test_certman.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: Lite
| if (!s->selfSigned) { | ||
| issuer = cm_table_find_issuer(table, s); | ||
| if ((issuer != NULL) && (issuer != s)) { | ||
| XMEMCPY(s->issuerKeyHash, issuer->subjectKeyHash, |
There was a problem hiding this comment.
Restore overwrites a correct saved issuerKeyHash with the key hash of the first CA matching only by subject name · Certificate and trust chain validation bypass
For a CA with no AKID, cm_table_find_issuer falls back to cm_table_find_name, which returns the first CA with the same subject name. Its subjectKeyHash then replaces the correct saved issuerKeyHash. With two same-named CAs (key rollover), CheckOcspResponder trusts a delegated responder for the wrong issuer key.
Suggested fix: Overwrite the saved issuerKeyHash only when the issuer is matched by AKID/SKID; otherwise keep the saved value.
Related known findings (similar but distinct; listed for context, not part of this finding)
- F-11701 (open): (1) File/function: same file, but the candidate is cm_restore_apply_issuer_key_hash in the cert-cache restore path, while F-11701 is GetCAByKeyHash, a lookup helper. (2) Operation: the candidate XMEMCPYs the subjectKeyHash of the first same-named CA over the saved issuerKeyHash. F-11701 returns a stale non-matching signer because ret is not reset before the fallback scan. (3) Root cause: the candidate's cause is a subject-name-only fallback in cm_table_find_issuer that replaces a correct saved val


Description
A CA table restored from the persisted cert cache did not match the table that was saved. The old cache stored only the public key, key OID, common name, and name hashes of each CA. Key usage, extended key usage, path length, the self-signed flag, name constraints, and the issuer link came back empty. The path length a CA inherits from its issuer was also lost, because that value is computed at load time and is not stored in the certificate.
The cache now stores each CA's DER and signer type and rebuilds the signer with
ParseCertandFillSigner.PERSIST_CERT_CACHEenablesWOLFSSL_SIGNER_DER_CERTso the CA table keeps the DER. The new signers are swapped in under the CA lock, so a failed restore leaves the current table in place.Each cache entry also stores the path length in use. After every CA is rebuilt, restore computes the inherited limit from the issuers in the new table and keeps the lower of that limit and the saved one. The limit cannot increase when the issuer is not in the cache. With
HAVE_OCSP, each entry also storesissuerKeyHash. A CA that is not self-issued takes the subject key hash of its issuer when that issuer is restored, and keeps the saved hash when it is not.FillSignernow passescert->heaptoAllocDerfor the CA DER copy, so a static-memory build uses its pool.WOLFSSL_CACHE_CERT_VERSIONis now 3. A cache in an older layout returnsCACHE_MATCH_ERROR.Testing
Built with the default configure flags plus
-DPERSIST_CERT_CACHE -DWOLFSSL_ALT_NAMES, with-Werroronssl.candtests/api/test_certman.c.test_wolfSSL_CertManagerNameConstraint_cert_cachetest_wolfSSL_CertManagerPathLen_cert_cacheNO_SKIDand withWOLFSSL_SMALL_STACK.--apirun of that build reported 0 failed and 636 passed. The remaining tests were skipped by that build.The path-length test fails if the inherited-limit pass is left out: after restore, the further CA is accepted.
How to reproduce
Build and run the new tests:
Both report
Failed/Skipped/Passed/All: 0/0/1/1.To see the bug, keep the new tests and put back the library files from master (
originis the wolfSSL/wolfssl remote):The test reports
Failed/Skipped/Passed/All: 1/0/0/1with:The restored CA has lost its name constraints, so a leaf outside the permitted names verifies. The path-length test passes on master too: master restores each CA as a zeroed signer, so that test checks the new layout and not the master bug.
Tested on macOS arm64 (Apple clang) at 60c7daf.
Checklist