Skip to content

Keep CA restrictions when restoring the persisted cert cache - #11875

Open
sameehj wants to merge 6 commits into
wolfSSL:masterfrom
sameehj:fix-persist-cert-cache-signer
Open

sameehj wants to merge 6 commits into
wolfSSL:masterfrom
sameehj:fix-persist-cert-cache-signer

Conversation

@sameehj

@sameehj sameehj commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

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 ParseCert and FillSigner. PERSIST_CERT_CACHE enables WOLFSSL_SIGNER_DER_CERT so 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 stores issuerKeyHash. 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.

FillSigner now passes cert->heap to AllocDer for the CA DER copy, so a static-memory build uses its pool.

WOLFSSL_CACHE_CERT_VERSION is now 3. A cache in an older layout returns CACHE_MATCH_ERROR.

Testing

Built with the default configure flags plus -DPERSIST_CERT_CACHE -DWOLFSSL_ALT_NAMES, with -Werror on ssl.c and tests/api/test_certman.c.

  • test_wolfSSL_CertManagerNameConstraint_cert_cache
  • test_wolfSSL_CertManagerPathLen_cert_cache
  • The name-constraint test also passed with NO_SKID and with WOLFSSL_SMALL_STACK.
  • A full --api run 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:

./autogen.sh
./configure --enable-savecert CPPFLAGS=-DWOLFSSL_ALT_NAMES
make tests/unit.test
./tests/unit.test -test_wolfSSL_CertManagerNameConstraint_cert_cache
./tests/unit.test -test_wolfSSL_CertManagerPathLen_cert_cache

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 (origin is the wolfSSL/wolfssl remote):

base=$(git merge-base HEAD origin/master)
git checkout "$base" -- src/ssl_certman.c wolfcrypt/src/asn.c wolfssl/wolfcrypt/settings.h
make tests/unit.test
./tests/unit.test -test_wolfSSL_CertManagerNameConstraint_cert_cache
git checkout HEAD -- src/ssl_certman.c wolfcrypt/src/asn.c wolfssl/wolfcrypt/settings.h

The test reports Failed/Skipped/Passed/All: 1/0/0/1 with:

ERROR - tests/api/test_certman.c line 4771 failed with:
    expected: wolfSSL_CertManagerVerify(cm, attacker_leaf_chain, WOLFSSL_FILETYPE_PEM) == (ASN_NAME_INVALID_E)
    result:   1 != -198

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

  • added tests
  • updated/added doxygen
  • updated appropriate READMEs
  • Updated manual and documentation

Copilot AI balanced review requested due to automatic review settings October 23, 2026 15:06
@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .text +704 B (+0.1%, 806,340 B / 1,048,576 B, total: 77% used)

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +104 B, text +12,192 B (+4.4%, 294,768 B / 67,108,864 B, total: 0% used)
  • RAM: bss +49 B, datas +16 B (+0.1%, 58,423 B / 983,040 B, total: 6% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@sameehj
sameehj force-pushed the fix-persist-cert-cache-signer branch from 90590a6 to d3e3582 Compare October 23, 2026 15:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread src/ssl_certman.c Resolved
Comment thread src/ssl_certman.c Resolved
Comment thread wolfssl/wolfcrypt/settings.h Resolved
@sameehj

sameehj commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

retest this please

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread wolfcrypt/src/asn.c Resolved
Comment thread src/ssl_certman.c Resolved
Comment thread src/ssl_certman.c Resolved
@JacobBarthelmeh

Copy link
Copy Markdown
Contributor

@sameehj can you investigate into the PRB-master-job failure and recent Fenrir bot comments?

The PERSIST_CERT_CACHE save/restore of the CA table only stored the public
key, key OID, common name and name hashes of each Signer. Everything else
FillSigner takes from the certificate (key usage, extended key usage, path
length, self-signed flag, name constraints, AKID, issuer and serial hashes,
...) came back zeroed after a restore, so a restored CA table did not behave
like the one that was saved.

Store the DER encoding and type of each CA instead and rebuild the Signer
with ParseCert/FillSigner on restore, so new Signer fields are picked up
without having to change the cache layout again. PERSIST_CERT_CACHE now
enables WOLFSSL_SIGNER_DER_CERT so the CA table keeps the DER.

The restored signers are built into a separate table and swapped in under
the CA lock, so a failed restore leaves the existing CA table in place.

Bump WOLFSSL_CACHE_CERT_VERSION to 2 so caches in the old layout are
rejected with CACHE_MATCH_ERROR.

Add a test that saves and restores a cache holding a name-constrained CA
chain and checks a leaf outside the permitted subtree is still rejected.
Rebuilding each CA from its DER recovers the path length stored in that
certificate. It does not recover the tighter limit ParseCert copies from
the issuer, because restore parses each CA before the new table is
installed. After every CA is rebuilt, apply that inherited limit from
the restored table before swapping it in.

Add a test that saves and restores the chainJ CA set and checks that a
further CA under an issuer whose effective path length is 0 is still
rejected.
A new context can load CAs with verification off. A path length of 0 is
then stored and the load still succeeds, so the test accepted ICA1. Set
the default verify mode before the load, which is what CertManagerLoadCA
does, so that CA is rejected again.
Restore can raise a path length to the certificate's own limit when the
issuer is not in the restored table. Store the path length in use and do
not raise it above that value. Store the OCSP issuer key hash as well, and
copy the issuer's subject key hash when that issuer is restored.

Pass the certificate heap to the CA DER allocation so a context memory
pool is used.

Bump the cache version to 3 so an older layout is rejected.

The path length test now drops the CA that carries the path length
extension before a second save and checks the descendant is still rejected.
cm_restore_apply_pathlen() assigns the result of XMALLOC to a struct
pointer. A C++ build rejects that conversion. Name the struct and cast
the result.

Initialize the restore fields so an -Og build does not treat them as
uninitialized. The values are set before use when the row header is
valid.
- Decode the pending CA with the certificate manager heap. FillSigner()
  moves buffers of the decoded cert into the signer, which the manager
  keeps after the connection is freed.
- Bound each row count in the cache header by the buffer size, so the
  sum of the counts cannot overflow and undersize the saved path array.
- Set the 32-bit unknown extension callback when a signer is restored,
  as AddCA() does.
- Add test_wolfSSL_CertManager_cert_cache_bad_count.
@sameehj
sameehj force-pushed the fix-persist-cert-cache-signer branch from 8ec958d to 58b2aa9 Compare November 17, 2026 10:44
@sameehj

sameehj commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

@JacobBarthelmeh Thanks. I fixed the three Fenrir findings, added a regression test, and replied on each thread:

  • The pending CA is now decoded with the certificate manager heap. FillSigner() moves buffers of the decoded cert into a signer that the manager keeps after the connection is freed.
  • The cache header row counts are now bounded by the buffer size, so their sum cannot overflow. The new test_wolfSSL_CertManager_cert_cache_bad_count fails without the bound.
  • cm_restore_signer() now sets the 32-bit unknown extension callback, as AddCA() does.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants