Skip to content

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

Open
sameehj wants to merge 1 commit into
wolfSSL:masterfrom
sameehj:fix-chain-ca-expired-signer
Open

sameehj wants to merge 1 commit 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 +24 B (+0.0%, 72,520 B / 262,144 B, total: 28% used)

gcc-arm-cortex-m3

  • FLASH: .text +1,916 B (+1.4%, 137,022 B / 262,144 B, total: 52% used)

gcc-arm-cortex-m4

  • FLASH: .text +1,920 B (+0.9%, 216,088 B / 262,144 B, total: 82% used)

gcc-arm-cortex-m4-crypto-only

  • FLASH: .text +64 B (+0.0%, 185,019 B / 262,144 B, total: 71% used)

gcc-arm-cortex-m4-dtls13

  • FLASH: .text +1,920 B (+1.0%, 203,492 B / 1,048,576 B, total: 19% used)

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .rodata +24 B, .text +2,560 B (+0.3%, 809,920 B / 1,048,576 B, total: 77% used)

gcc-arm-cortex-m4-pq

  • FLASH: .text +1,920 B (+0.6%, 322,080 B / 1,048,576 B, total: 31% used)

gcc-arm-cortex-m4-rsa-only

  • FLASH: .text +1,920 B (+0.5%, 351,832 B / 1,048,576 B, total: 34% used)

gcc-arm-cortex-m4-tls12

  • FLASH: .text +1,920 B (+1.4%, 137,742 B / 262,144 B, total: 53% used)

gcc-arm-cortex-m4-tls13

  • FLASH: .rodata.CSWTCH.200 -20 B, .text +1,856 B (+0.7%, 258,482 B / 262,144 B, total: 99% used)

gcc-arm-cortex-m7

  • FLASH: .text +1,920 B (+0.9%, 216,088 B / 262,144 B, total: 82% used)

gcc-arm-cortex-m7-pq

  • FLASH: .text +1,920 B (+0.6%, 322,912 B / 1,048,576 B, total: 31% used)

gcc-arm-cortex-m7-tls13

  • FLASH: .rodata.CSWTCH.200 -20 B, .text +1,920 B (+0.7%, 258,546 B / 262,144 B, total: 99% used)

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +108 B, text +13,872 B (+5.0%, 296,452 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
@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
@philljj philljj self-assigned this Nov 19, 2026

@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 #11942

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): tests/api/test_ssl_cert.h
Coverage: 6 of 7 in-scope changed file(s) opened by the reviewer; not opened: wolfssl/internal.h

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 src/internal.c Resolved
Comment thread src/internal.c Resolved
Comment thread src/internal.c Resolved
@sameehj
sameehj force-pushed the fix-chain-ca-expired-signer branch 2 times, most recently from a477ded to 6458b7f Compare November 20, 2026 08:18
@sameehj

sameehj commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

ChainCADatesAwaitStatus() is now compiled only when Certificate Status Request v2 is enabled. Builds without that feature failed with -Werror because the function was unused. The three review threads are answered in 6458b7f.

@sameehj

sameehj commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

retest this please

@philljj
philljj requested a review from Frauschi November 22, 2026 22:51
@philljj

philljj commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

@claude review

@claude claude 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.

Findings marked 🟡 are optional suggestions and need no follow-up push.

Comment thread src/ssl_certman.c Resolved
Comment thread src/ssl_certman.c Resolved
Comment thread src/ssl_certman.c Resolved
Comment thread src/internal.c Resolved

@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 #11942

Scan targets checked: none
Failed targets: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): tests/api/test_ssl_cert.h

⚠️ Review incomplete — one or more scan targets failed before findings could be produced. See the Fenrir PR review detail page for logs.

💬 2 finding(s) from an earlier review are still open and were not re-posted:

  • Renewed chain CA dates are never stored when CSR2 multi is configured but not negotiated — src/internal.c:21554
  • Cached-CA date check is skipped when the leaf has an overridable error that isn't a date error — src/internal.c:20934

Review tier: Lite

@sameehj
sameehj force-pushed the fix-chain-ca-expired-signer branch from 22cefa3 to ec02eaf Compare November 23, 2026 10:24

@philljj philljj 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.

seeing build errors:

multi-test:

unescaped error code operands (missing WC_NO_ERR_TRACE()):
src/ssl_certman.c:3092:                if (dateErr == ASN_BEFORE_DATE_E)
src/ssl_certman.c:3094:                else if (dateErr == ASN_AFTER_DATE_E)

pic32 harmony:

In file included from /tmp/wolfssl-src-harmony-ef/wolfcrypt/src/asn.c:363:
/tmp/wolfssl-src-harmony-ef/wolfssl/internal.h:3017:38: error: unknown type name 'time_t'
 3017 |                                      time_t atTime,
      |                                      ^~~~~~
/tmp/wolfssl-src-harmony-ef/wolfssl/internal.h:207:1: note: 'time_t' is defined in header '<time.h>'; did you forget to '#include <time.h>'?

etc

In file included from /home/runner/Arduino/libraries/wolfssl/src/src/ssl.c:428:0:
/home/runner/Arduino/libraries/wolfssl/src/src/ssl_certman.c:917:41: error: unknown type name 'ChainCADate'
     int dateCheck, time_t atTime, const ChainCADate* caDates, int* caDateOut)

@philljj philljj assigned sameehj and unassigned wolfSSL-Bot Nov 23, 2026
@sameehj
sameehj force-pushed the fix-chain-ca-expired-signer branch from ec02eaf to 2c5891a Compare November 24, 2026 09:40
@sameehj

sameehj commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@philljj The three build failures are fixed in 2c5891a. The branch is one commit, rebased onto 3fd873c.

  • The date comparisons in src/ssl_certman.c use WC_NO_ERR_TRACE(ASN_BEFORE_DATE_E) and WC_NO_ERR_TRACE(ASN_AFTER_DATE_E).
  • time_t and ChainCADate are arguments of CM_VerifyBuffer_ex only when WOLFSSL_CHAIN_CA_DATES is set. That macro is off for NO_ASN_TIME_CHECK, WOLFCRYPT_ONLY, and NO_CERTS, so the PIC32 harmony build and the Arduino build keep the previous prototype.
  • The rebase keeps the WOLFSSL_ALT_CERT_CHAINS path from master together with the chain-CA date walk.

A local --enable-opensslextra --enable-crl build compiled src/libwolfssl.la.

@Frauschi

Copy link
Copy Markdown
Member

Jenkins retest this please

@Frauschi Frauschi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Two build breaks and a few gaps in the X509 store path, details inline.

Comment thread src/ssl_certman.c Resolved
Comment thread src/x509_str.c Resolved
Comment thread src/x509_str.c Resolved
Comment thread src/x509_str.c Resolved
Comment thread src/internal.c Resolved
Comment thread src/ssl_certman.c Resolved
@philljj philljj removed their assignment Nov 25, 2026
@sameehj
sameehj force-pushed the fix-chain-ca-expired-signer branch from b6bbcf0 to 5860f0a Compare November 25, 2026 11:38
@sameehj

sameehj commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto 82e1f1a. The conflict was in src/x509_str.c. The chain-CA date checks are unchanged. The verify callback, including the second call for a cached CA date error, now goes through X509StoreCallVerifyCb().

test_wolfSSL_chain_ca_cached_dates passes with --enable-opensslextra --enable-crl --enable-certgen --enable-certext and -Werror. The previous commit's checks were green. This commit had no jobs because the branch conflicted with master.

@sameehj

sameehj commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

retest this please

1 similar comment
@sameehj

sameehj commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

retest this please

Comment thread src/x509_str.c Resolved
@sameehj

sameehj commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

@philljj The build errors from Nov 23 are fixed on 5860f0a, and those checks passed. 29f30d1 adds the three wolfSSL_X509_verify_cert cases. Please review again.

@sameehj sameehj assigned wolfSSL-Bot and unassigned sameehj Dec 3, 2026
A CA cached from a peer chain is checked again each time it verifies a
certificate. wolfSSL_X509_verify_cert() reports an expired cached CA on
the certificate that CA issued. A CA supplied by the chain builder
stops that walk.

The X509 link files the supplied CA under GetHashId() of its subject
key id, the same hash the signer table uses. An SM2/SM3 build pads a
shorter key id, so a raw-length compare skipped the link and reported
the expired CA on the leaf.
@sameehj
sameehj force-pushed the fix-chain-ca-expired-signer branch from 29f30d1 to 863593e Compare December 4, 2026 03:45
@sameehj

sameehj commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

The all-sm failure is fixed. X509StoreChainCaLink() required the raw subject key id length to equal SIGNER_DIGEST_SIZE. An SM2/SM3 build pads a shorter key id, so the link was skipped and the expired cached CA was reported on the leaf.

The link now uses GetHashId(), the same hash the signer table uses. The branch is one commit on current master: 863593e.

@sameehj
sameehj requested a balanced review from Copilot December 4, 2026 08:35
@sameehj sameehj self-assigned this Dec 4, 2026

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

A same-key CA renewal can persist new validity dates with the cached signer’s old name constraints, allowing later leaf-only connections outside the renewed constraints after the original certificate expires. Independently, verification stops after the first expired cached intermediate, so a callback that permits only one expiry can accept a chain containing another expired intermediate without being asked to approve it.

4 open issues
7 resolved since last review

Review effort: Balanced

Comment thread src/ssl_certman.c
Comment on lines +3107 to +3109
if (dateErr != 0) {
ret = dateErr;
break;
Comment thread src/ssl_certman.c
Comment on lines +3215 to +3216
WOLFSSL_MSG("Updating dates of cached chain CA");
XMEMCPY(signer->validity, validity, sizeof(*validity));
Comment thread src/ssl_certman.c
Comment on lines +3347 to +3358
const Signer* issuer = cert->ca;

if (cert->selfSigned || issuer == NULL || issuer->publicKey == NULL)
return 0;

if (wc_LockMutex(&cm->caLock) != 0)
return BAD_MUTEX_E;
signer = cm_find_signer_locked(cm, hash);
if (signer != NULL && signer != issuer &&
signer->type == WOLFSSL_CHAIN_CA && signer->validity != NULL &&
XMEMCMP(signer->validity, validity, sizeof(*validity)) != 0 &&
cm_signer_key_is(signer, keyHash) &&
Comment thread src/x509_str.c
Comment on lines +678 to +688
/* subjKeyId is the raw extension. The signer table files the CA under
* GetHashId() of that extension. An SM2/SM3 build keeps a larger key-id
* buffer and pads a shorter id, so the two lengths are not always equal. */
if (issuer == NULL || !issuer->subjKeyIdSet || issuer->subjKeyId == NULL ||
issuer->subjKeyIdSz == 0 ||
issuer->pubKey.buffer == NULL || issuer->pubKey.length <= 0)
return 0;
XMEMSET(link, 0, sizeof(*link));
if (GetHashId(issuer->subjKeyId, (int)issuer->subjKeyIdSz, link->hash,
HashIdAlg((word32)issuer->sigOID)) != 0)
return 0;
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.

6 participants