Skip to content

Check the dates of cached chain CAs when they are used - #11942

Open
sameehj wants to merge 2 commits into
wolfSSL:masterfrom
sameehj:fix-chain-ca-expired-signer
Open

sameehj wants to merge 2 commits into
wolfSSL:masterfrom
sameehj:fix-chain-ca-expired-signer

Conversation

@sameehj

@sameehj sameehj commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Description

After a handshake, ProcessPeerCerts() adds each verified CA from the peer's chain to the certificate manager as a WOLFSSL_CHAIN_CA. The cached Signer kept no validity dates. A later peer that sent only its leaf was then verified with the cached CA, and only the leaf's own dates were checked. The same chain was rejected with ASN_AFTER_DATE_E when the expired CA was sent, or when a new context was used, and accepted when the CA was omitted.

This change makes the result independent of whether the CA is sent:

  • Signer gains a validity pointer that holds the DER encoded notBefore and notAfter. It is allocated only for a WOLFSSL_CHAIN_CA, so CAs the application loads cost one pointer each.
  • wolfssl_local_CheckChainCADates() walks from the issuer of the leaf up to the trust anchor and checks the dates of every chain CA on the way. It runs once, for the leaf. A date error on a cached CA is reported as ASN_BEFORE_DATE_E or ASN_AFTER_DATE_E at depth 0 and goes through the verify callback like any other date error. When the leaf has a date error too, the callback gets the leaf's error first, then the cached CA's error, so accepting one does not accept the other. wc_AsnSetSkipDateCheck() turns the walk off as it does the other date checks.
  • The walk stops at a self-signed CA and at a CA the application loaded (WOLFSSL_USER_CA, WOLFSSL_USER_INTER), because that CA is the trust anchor of the path. Those CAs are not checked again, as before. A chain CA retyped with wolfSSL_CertManagerLoadCABufferType() follows its new type.
  • The walk reuses the name constraints issuer lookup, now exported as wolfssl_local_FindSignerByAkidOrName(), with the same cycle guard and WOLFSSL_MAX_CHAIN_DEPTH limit. With IGNORE_NAME_CONSTRAINTS only the direct issuer is checked. Certificate Status Request v2 pending signers are searched too.
  • The type and dates of each CA are read under caLock, because another handshake can update them.
  • When the verify callback accepts a date error on a CA that the peer sent, the handshake records that CA as waived. The walk does not report the error again at depth 0. The waiver is not stored, so a later leaf alone fails.
  • A CA renewed with the same key is not added again, because AddCA() already has a signer under its subject key identifier. When the peer sends such a CA and it verifies, its dates are recorded for the handshake and the walk uses them. They are stored on the cached CA only when the peer is verified. With Certificate Status Request v2 multi-stapling, TLSX_CSR2_MergePendingCA() stores them after the stapled status is accepted, and TLSX_CSR2_ClearPendingCA() drops them. A failed chain drops them.
  • wolfssl_local_IsChainCARenewal() accepts a renewal only when the cached CA is a WOLFSSL_CHAIN_CA with the same public key and other dates, the new certificate has the same issuer, it is not self-signed, and it was not verified with the cached CA's own key. The cached CA is found by the hash AddCA() uses, so a renewal without an Authority Key Identifier is found too.
  • wolfSSL_CertManagerVerifyBuffer() checks cached chain CAs the same way. wolfSSL_CertManagerLoadCABufferType() stores the dates of a CA loaded as WOLFSSL_CHAIN_CA, for example one saved through the CA cache callback.

Notes for review:

  • sizeof(Signer) changes, so a PERSIST_CERT_CACHE file saved before this change is rejected on restore, as with any Signer layout change. A restored signer has no type or dates and is not checked, as today. Keep CA restrictions when restoring the persisted cert cache #11875 rebuilds each CA from its DER.
  • When a cached ancestor is out of date, the error is reported at depth 0, not at the depth of the cached CA.
  • WOLFSSL gains one pointer, chainCaDates. The list is freed at the end of the certificate message, at the end of the handshake and in wolfSSL_free().
  • Check CA ExtKeyUsage for issuers from the CertManager #11873 exports the same wolfssl_local_FindSignerByAkidOrName() with an identical hunk, so the two merge cleanly in either order.

Testing

test_wolfSSL_chain_ca_cached_dates generates a CA that expires before its leaf and moves the clock with wc_SetTimeCb(). T1 is now and T2 is after the CA expires.

One client context is reused across handshakes:

  • full chain at T1: accepted, and the CA is cached
  • leaf alone at T1: accepted
  • full chain at T2: ASN_AFTER_DATE_E, as before
  • leaf alone at T2: ASN_AFTER_DATE_E
  • leaf alone at T2 with a verify callback that accepts the date error: accepted
  • a leaf that is not valid yet at T2, with a callback that accepts only that error: ASN_AFTER_DATE_E
  • full chain at T2 with the CA renewed under the same key: accepted, and then the leaf alone at T2: accepted

New client contexts:

  • full chain at T2 with a callback that accepts the expiry only at depth 1: accepted. Then the leaf alone at T2: ASN_AFTER_DATE_E
  • after caching the CA, a self-signed certificate with its name, key and Authority Key Identifier: the cached dates do not change, so the leaf alone at T2 gives ASN_AFTER_DATE_E
  • after caching the CA, the CA renewed without an Authority Key Identifier at T2: accepted, and then the leaf alone at T2: accepted

wolfSSL_CertManagerVerifyBuffer():

  • the CA loaded as WOLFSSL_CHAIN_CA at T1 and T2, and as WOLFSSL_USER_CA at T2
  • a leaf that is not valid yet at T2, with a callback that accepts only that error: ASN_AFTER_DATE_E
  • the expired CA loaded as WOLFSSL_CHAIN_CA, and a CA below it loaded as WOLFSSL_USER_CA: a leaf from the lower CA verifies

Without the library change, the test fails at the leaf-alone T2 case (0 != -151). With the first commit only, seven checks fail: the not-valid-yet leaf (TLS and certificate manager), the depth 1 waiver, the self-signed certificate, the renewal without an Authority Key Identifier (two checks) and the loaded CA below a chain CA.

This revision is built with CMake and -Werror -Wpedantic -Wshadow -Wdeclaration-after-statement, with WOLFSSL_ALL, with the default options, with WOLFSSL_ALL plus NO_ASN_TIME_CHECK, and with WOLFSSL_ALL plus IGNORE_KEY_EXTENSIONS. With NO_SKID, the only errors are the GetCAByName errors that master also has. The full unit test passes with WOLFSSL_ALL. The ssl_cert, certman, ocsp and ssl_crl_ocsp groups pass with AddressSanitizer and UndefinedBehaviorSanitizer. leaks reports no leaks for the new test.

The test does not cover the multi-stapling path with a failed status for a renewed CA, because that needs an OCSP responder for the chain CA.

Tested on macOS arm64 (Apple clang).

Checklist

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

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

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m0plus

  • FLASH: .text +4 B (+0.0%, 71,359 B / 262,144 B, total: 27% used)

gcc-arm-cortex-m3

  • FLASH: .text +1,412 B (+1.1%, 133,761 B / 262,144 B, total: 51% used)

gcc-arm-cortex-m4

  • FLASH: .text +1,408 B (+0.7%, 211,112 B / 262,144 B, total: 81% used)

gcc-arm-cortex-m4-dtls13

  • FLASH: .text +1,408 B (+0.7%, 198,816 B / 1,048,576 B, total: 19% used)

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .rodata +32 B, .text +1,536 B (+0.2%, 807,204 B / 1,048,576 B, total: 77% used)

gcc-arm-cortex-m4-pq

  • FLASH: .text +1,344 B (+0.4%, 316,720 B / 1,048,576 B, total: 30% used)

gcc-arm-cortex-m4-rsa-only

  • FLASH: .text +1,408 B (+0.4%, 346,984 B / 1,048,576 B, total: 33% used)

gcc-arm-cortex-m4-tls12

  • FLASH: .text +1,408 B (+1.1%, 134,537 B / 262,144 B, total: 51% used)

gcc-arm-cortex-m4-tls13

  • FLASH: .text +1,408 B (+0.6%, 253,442 B / 262,144 B, total: 97% used)

gcc-arm-cortex-m7

  • FLASH: .text +1,408 B (+0.7%, 211,112 B / 262,144 B, total: 81% used)

gcc-arm-cortex-m7-pq

  • FLASH: .text +1,408 B (+0.4%, 317,616 B / 1,048,576 B, total: 30% used)

gcc-arm-cortex-m7-tls13

  • FLASH: .text +1,408 B (+0.6%, 253,442 B / 262,144 B, total: 97% used)

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +104 B, text +12,184 B (+4.4%, 294,760 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.

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

Unresolved critical flaws allow unauthorized or failed-handshake updates to extend cached CA validity, bypass issuer expiry checks, and introduce a data race in shared CA state.

7 open issues
What changed in this PR

This PR adds validity checks for cached chain CAs in TLS and certificate-manager verification, aiming to make verification consistent whether peers send their intermediates or omit them.

Changes:

  • Store validity dates on cached chain signers and check them along the issuer path.
  • Refresh cached dates when a verified CA renewal uses the same key.
  • Add regression tests for expiration, callback overrides, renewal, and application-loaded trust anchors.
File Description
wolfssl/​wolfcrypt/​asn.h Defines signer validity storage and shared helper declarations.
wolfssl/​internal.h Declares cached-CA date-check and update helpers.
wolfcrypt/​src/​asn.c Stores and frees signer dates; exposes issuer lookup.
tests/​api/​test_ssl_cert.h Registers the cached-CA validity test.
tests/​api/​test_ssl_cert.c Tests cached expiration, renewal, and verification behavior.
src/​ssl_certman.c Checks cached issuer dates and updates stored validity.
src/​internal.c Integrates date checks and renewal updates into handshakes.

Review effort: Balanced

Comment thread src/internal.c Resolved
Comment thread src/internal.c Resolved
Comment thread src/ssl_certman.c Resolved
Comment thread src/ssl_certman.c Resolved
Comment thread src/internal.c Resolved
Comment thread src/internal.c Resolved
Comment thread src/ssl_certman.c Resolved
@sameehj
sameehj force-pushed the fix-chain-ca-expired-signer branch from ac540f9 to 6301a67 Compare November 2, 2026 10:27
After a handshake, ProcessPeerCerts() adds each verified CA from the
peer's chain to the certificate manager as a WOLFSSL_CHAIN_CA. The Signer
kept no validity dates, so a later peer that sent only its leaf was
verified with the cached CA even after that CA had expired. Sending the
expired CA, or using a new context, failed with ASN_AFTER_DATE_E.

Keep the DER encoded notBefore and notAfter of a chain CA in its Signer,
and after parsing each peer certificate walk from its issuer up to the
self-signed trust anchor, checking the dates of every chain CA on the
way. The error is reported as ASN_BEFORE_DATE_E or ASN_AFTER_DATE_E on
that certificate, so a verify callback can accept it as before. CAs the
application loaded are trust anchors and are not checked again. The walk
uses the name constraints issuer lookup, exported as
wolfssl_local_FindSignerByAkidOrName(). With IGNORE_NAME_CONSTRAINTS only
the direct issuer is checked.

A CA renewed with the same key is not added again, so the peer sending
it now updates the dates of the cached CA. Otherwise the cached copy
would fail a chain that includes a valid renewed CA.

wolfSSL_CertManagerVerifyBuffer() checks cached chain CAs the same way,
and wolfSSL_CertManagerLoadCABufferType() stores the dates of a CA loaded
as WOLFSSL_CHAIN_CA.

Add test_wolfSSL_chain_ca_cached_dates, which reuses one client context
for a full chain, a leaf alone before and after the CA expires, a verify
callback accepting the error and a renewed CA, and checks certificate
manager verification.
Address the review of the cached chain CA date check.

The walk over cached chain CAs now runs once, for the leaf, and not for
each certificate as it is parsed. A date error on a cached CA is reported
at depth 0, after the verify callback has seen the error of the leaf
itself, so accepting a leaf date error does not hide an expired cached CA.
wolfSSL_CertManagerVerifyBuffer() does the same.

The walk reads the type and dates of each CA under caLock and stops at a
CA that the application loaded (WOLFSSL_USER_CA or WOLFSSL_USER_INTER), as
that CA is the trust anchor of the path.

A CA in the peer's chain whose date error the verify callback accepted is
recorded for the handshake, so the walk does not report that error again
at depth 0.

A renewed CA no longer changes the cached dates while the chain is
processed. Its dates are recorded for the handshake, used by the walk, and
stored on the cached CA only after the peer is verified. With Certificate
Status Request v2 multi-stapling that is when the stapled status of the
chain CAs is accepted, in TLSX_CSR2_MergePendingCA(), and a failed status
drops them. A renewal is found by the subject key identifier, the same
hash AddCA() uses, so a renewed CA without an Authority Key Identifier is
found too. It must have the same key as the cached CA, be issued by the
same issuer, not be self-signed and not be signed by the cached CA's own
key.

Extend test_wolfSSL_chain_ca_cached_dates for a leaf that is not yet
valid, a CA date error accepted at depth 1, a self-signed certificate with
the cached CA's name and key, a renewal without an Authority Key
Identifier and a loaded CA below a cached chain CA.
@sameehj
sameehj force-pushed the fix-chain-ca-expired-signer branch from 6301a67 to 6d36ea3 Compare November 17, 2026 06:51
@sameehj sameehj assigned wolfSSL-Bot and unassigned sameehj Nov 17, 2026
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.

3 participants