From a10f61aeb76d80a64836412898ce2ea4bbd23564 Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Fri, 7 Aug 2026 22:11:44 +0200 Subject: [PATCH 01/20] Confine callback-accepted chain CAs to their own connection When an application's verify callback accepted a chain wolfSSL had rejected, any CA in that chain was added to the WOLFSSL_CTX certificate manager. This meant later connections trusted that same CA as well. This change makes it so that these CAs are added to a list owned by the Certificate message and reach ParseCertRelative() via its extraCa argument. --- src/internal.c | 116 +++++++++++++++++++++++++++++++++++++++++++- tests/api.c | 76 +++++++++++++++++++++++++++++ wolfcrypt/src/asn.c | 10 +--- wolfssl/internal.h | 1 + 4 files changed, 192 insertions(+), 11 deletions(-) diff --git a/src/internal.c b/src/internal.c index 5a8af6ee8e1..1dc5d27d11d 100644 --- a/src/internal.c +++ b/src/internal.c @@ -17878,12 +17878,100 @@ void DoCrlCallback(WOLFSSL_CERT_MANAGER* cm, WOLFSSL* ssl, } #endif +#ifndef NO_ASN +/* Build a signer for a chain CA the verify callback waived */ +static int ProcessPeerCertAddWaivedCA(WOLFSSL* ssl, ProcPeerCertArgs* args, + buffer* cert) +{ + int ret = 0; + WC_DECLARE_VAR(dCertAdd, DecodedCert, 1, 0); + int dCertAdd_inited = 0; + DerBuffer* derBuffer = NULL; + Signer* s = NULL; + + WC_ALLOC_VAR_EX(dCertAdd, DecodedCert, 1, ssl->heap, + DYNAMIC_TYPE_TMP_BUFFER, + { + ret = MEMORY_E; + goto exit_waived; + }); + InitDecodedCert(dCertAdd, cert->buffer, cert->length, ssl->heap); + dCertAdd_inited = 1; + ret = ParseCert(dCertAdd, CA_TYPE, NO_VERIFY, SSL_CM(ssl)); + if (ret != 0) + goto exit_waived; + + if (!dCertAdd->isCA) { + WOLFSSL_MSG("Waived chain cert is not a CA, not usable as a signer"); + goto exit_waived; + } +#ifndef ALLOW_INVALID_CERTSIGN + if (!dCertAdd->selfSigned && dCertAdd->extKeyUsageSet && + (dCertAdd->extKeyUsage & KEYUSE_KEY_CERT_SIGN) == 0) { + WOLFSSL_MSG("Waived chain cert doesn't have key usage cert signing"); + ret = NOT_CA_ERROR; + goto exit_waived; + } +#endif + + ret = AllocDer(&derBuffer, cert->length, CA_TYPE, ssl->heap); + if (ret != 0 || derBuffer == NULL) + goto exit_waived; + XMEMCPY(derBuffer->buffer, cert->buffer, cert->length); + + s = MakeSigner(SSL_CM(ssl)->heap); + if (s == NULL) { + ret = MEMORY_E; + goto exit_waived; + } + ret = FillSigner(s, dCertAdd, WOLFSSL_TEMP_CA, derBuffer); + if (ret != 0) + goto exit_waived; + + s->next = args->waivedCAs; + args->waivedCAs = s; + s = NULL; + +exit_waived: + if (s != NULL) + FreeSigner(s, SSL_CM(ssl)->heap); + if (derBuffer != NULL) + FreeDer(&derBuffer); + if (dCertAdd_inited) + FreeDecodedCert(dCertAdd); + WC_FREE_VAR_EX(dCertAdd, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); + + return ret; +} + +static int ProcessPeerCertIsWaivedCA(ProcPeerCertArgs* args, const Signer* ca) +{ + const Signer* s; + + for (s = args->waivedCAs; s != NULL; s = s->next) { + if (s == ca) + return 1; + } + + return 0; +} +#endif /* !NO_ASN */ + static void FreeProcPeerCertArgs(WOLFSSL* ssl, void* pArgs) { ProcPeerCertArgs* args = (ProcPeerCertArgs*)pArgs; (void)ssl; +#ifndef NO_ASN + while (args->waivedCAs != NULL) { + Signer* s = args->waivedCAs; + + args->waivedCAs = s->next; + FreeSigner(s, SSL_CM(ssl)->heap); + } +#endif + XFREE(args->certs, ssl->heap, DYNAMIC_TYPE_DER); args->certs = NULL; #ifdef WOLFSSL_TLS13 @@ -18181,6 +18269,7 @@ static int ProcessPeerCertParse(WOLFSSL* ssl, ProcPeerCertArgs* args, byte* subjectHash = NULL; int alreadySigner = 0; Signer *extraSigners = NULL; + Signer *waivedTail = NULL; #if defined(HAVE_RPK) int cType; #endif @@ -18313,6 +18402,14 @@ PRAGMA_GCC_DIAG_POP } #endif /* Parse Certificate */ + if (args->waivedCAs != NULL) { + waivedTail = args->waivedCAs; + while (waivedTail->next != NULL) + waivedTail = waivedTail->next; + waivedTail->next = extraSigners; + extraSigners = args->waivedCAs; + } + ret = ParseCertRelative(args->dCert, certType, verify, SSL_CM(ssl), extraSigners); #ifdef KEEP_PEER_CERT /* An error here can still be overridden and the leaf accepted, while @@ -18331,6 +18428,9 @@ PRAGMA_GCC_DIAG_POP } #endif + if (waivedTail != NULL) + waivedTail->next = NULL; + #if defined(HAVE_RPK) /* Confirm the received certificate's form (X.509 vs raw public key) matches * the type negotiated with the peer. An un-negotiated bare key is reported @@ -20219,6 +20319,8 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)) int holdCrl = 0; #endif + int preCbRet; + int caIsTemp = 0; /* select last certificate */ args->certIdx = args->count - 1; @@ -20571,7 +20673,13 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, SetRevocationVerifyRet(ssl, ret); /* Do verify callback */ + preCbRet = ret; ret = DoVerifyCallbackHeldCrl(ssl, args, ret, &heldCrlRet); + if (ret == 0 && preCbRet != 0) + caIsTemp = 1; + if (ret == 0 && args->dCert->ca != NULL && + ProcessPeerCertIsWaivedCA(args, args->dCert->ca)) + caIsTemp = 1; if (CrlErrWaived(ssl, ret)) ret = ssl->error = 0; #if defined(HAVE_OCSP) && defined(HAVE_CRL) && \ @@ -20596,7 +20704,8 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, #endif #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) if (ret == 0 && addToPendingCAs && !alreadySigner && - !ssl->options.verifyNone && !skipAddCA) { + !ssl->options.verifyNone && !skipAddCA && + !caIsTemp) { /* The verifyNone and skipAddCA conditions mirror the * guards on the AddCA() call below. A certificate the * surrounding code has already declined to admit as a @@ -20631,7 +20740,10 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, cert->buffer, cert->length); } #endif /* SESSION_CERTS && WOLFSSL_ALT_CERT_CHAINS */ - if (!alreadySigner) { + if (caIsTemp) { + ret = ProcessPeerCertAddWaivedCA(ssl, args, cert); + } + else if (!alreadySigner) { DerBuffer* add = NULL; ret = AllocDer(&add, cert->length, CA_TYPE, ssl->heap); if (ret < 0) diff --git a/tests/api.c b/tests/api.c index 709c9fbd3d3..52a31ad7c2a 100644 --- a/tests/api.c +++ b/tests/api.c @@ -42980,6 +42980,81 @@ static int test_override_alt_cert_chain(void) } #endif +#if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_RSA) && \ + !defined(WOLFSSL_ALT_CERT_CHAINS) +static int test_chain_ca_no_persist_verify_cb(int preverify, + WOLFSSL_X509_STORE_CTX* store) +{ + (void)preverify; + (void)store; + return 1; +} + +static int test_chain_ca_no_persist_client_ctx(WOLFSSL_CTX* ctx) +{ + EXPECT_DECLS; + + ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(wolfSSL_CTX_GetCertManager(ctx)), + WOLFSSL_SUCCESS); + wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, + test_chain_ca_no_persist_verify_cb); + + return EXPECT_RESULT(); +} + +static int test_chain_ca_no_persist_server_ctx(WOLFSSL_CTX* ctx) +{ + EXPECT_DECLS; + + ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx, + "./certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS); + + return EXPECT_RESULT(); +} + +static int test_chain_ca_no_persist_result(WOLFSSL* ssl) +{ + EXPECT_DECLS; + WOLFSSL_CERT_MANAGER* cm = NULL; + + ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(wolfSSL_get_SSL_CTX(ssl))); + /* The CA trusted via callback must not be usable as a signer afterwards */ + ExpectIntNE(wolfSSL_CertManagerVerify(cm, + "./certs/intermediate/ca-int2-cert.pem", WOLFSSL_FILETYPE_PEM), + WOLFSSL_SUCCESS); + /* Nor the CA that only verified because the aforementioned one vouched for + * it */ + ExpectIntNE(wolfSSL_CertManagerVerify(cm, + "./certs/intermediate/server-int-cert.pem", WOLFSSL_FILETYPE_PEM), + WOLFSSL_SUCCESS); + + return EXPECT_RESULT(); +} + +static int test_chain_ca_no_persist(void) +{ + EXPECT_DECLS; + test_ssl_cbf client_cbs, server_cbs; + + XMEMSET(&client_cbs, 0, sizeof(client_cbs)); + XMEMSET(&server_cbs, 0, sizeof(server_cbs)); + + client_cbs.ctx_ready = test_chain_ca_no_persist_client_ctx; + client_cbs.on_result = test_chain_ca_no_persist_result; + server_cbs.ctx_ready = test_chain_ca_no_persist_server_ctx; + + ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs, + &server_cbs, NULL), TEST_SUCCESS); + + return EXPECT_RESULT(); +} +#else +static int test_chain_ca_no_persist(void) +{ + return TEST_SKIPPED; +} +#endif + static int test_rpk_set_xxx_cert_type(void) { EXPECT_DECLS; @@ -48992,6 +49067,7 @@ TEST_CASE testCases[] = { /* Can't memory test as client/server Asserts. */ TEST_DECL(test_harden_no_secure_renegotiation), TEST_DECL(test_override_alt_cert_chain), + TEST_DECL(test_chain_ca_no_persist), TEST_DECL(test_rpk_set_xxx_cert_type), TEST_DECL(test_short_session_id), /* Can't memory test as client/server hangs. */ diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index 097f604c2d4..3dfed4c11d5 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -25921,12 +25921,8 @@ static Signer* FindSignerByAkidOrName(WOLFSSL_CERT_MANAGER* cm, Signer* extraCAList, Signer* cert) { Signer* signer = NULL; -#ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2 - #ifndef NO_SKID +#ifndef NO_SKID Signer* exCaSigner; - #endif -#else - (void)extraCAList; #endif #ifndef NO_SKID @@ -25947,7 +25943,6 @@ static Signer* FindSignerByAkidOrName(WOLFSSL_CERT_MANAGER* cm, signer = GetCA(cm, cert->issuerNameHash); #endif -#ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2 if (signer == NULL && extraCAList != NULL) { #ifndef NO_SKID if (cert->authKeyIdSet) { @@ -25972,7 +25967,6 @@ static Signer* FindSignerByAkidOrName(WOLFSSL_CERT_MANAGER* cm, signer = findSignerByName(extraCAList, cert->issuerNameHash); #endif } -#endif return signer; } @@ -26556,11 +26550,9 @@ int ParseCertRelative(DecodedCert* cert, int type, int verify, if (!cert->selfIssued || (verify != NO_VERIFY && type != CA_TYPE && type != TRUSTED_PEER_TYPE)) { cert->ca = NULL; -#ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2 if (extraCAList != NULL) { cert->ca = findSignerByName(extraCAList, cert->issuerHash); } -#endif #ifndef NO_SKID if (cert->ca == NULL && cert->extAuthKeyIdSet) { cert->ca = GetCA(cm, cert->extAuthKeyId); diff --git a/wolfssl/internal.h b/wolfssl/internal.h index 22ba3e9ce3f..772172ee1d4 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -3018,6 +3018,7 @@ typedef struct ProcPeerCertArgs { #endif #ifndef NO_ASN DecodedCert* dCert; + Signer* waivedCAs; /* chain CAs the verify callback waived */ #endif word32 idx; word32 begin; From f0177788ee21b95ab32789b9f8bbbbe83d986a00 Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Wed, 16 Sep 2026 19:47:56 +0200 Subject: [PATCH 02/20] Remove code repetition --- src/internal.c | 147 ++++++++++++++++++------------------------------- wolfssl/ssl.h | 5 +- 2 files changed, 57 insertions(+), 95 deletions(-) diff --git a/src/internal.c b/src/internal.c index 1dc5d27d11d..5a61e0c38aa 100644 --- a/src/internal.c +++ b/src/internal.c @@ -17879,9 +17879,9 @@ void DoCrlCallback(WOLFSSL_CERT_MANAGER* cm, WOLFSSL* ssl, #endif #ifndef NO_ASN -/* Build a signer for a chain CA the verify callback waived */ -static int ProcessPeerCertAddWaivedCA(WOLFSSL* ssl, ProcPeerCertArgs* args, - buffer* cert) +/* Parse a chain certificate as a CA and return it as a filled signer. */ +static int ProcessPeerCertMakeChainSigner(WOLFSSL* ssl, buffer* cert, int type, + Signer** out) { int ret = 0; WC_DECLARE_VAR(dCertAdd, DecodedCert, 1, 0); @@ -17889,50 +17889,62 @@ static int ProcessPeerCertAddWaivedCA(WOLFSSL* ssl, ProcPeerCertArgs* args, DerBuffer* derBuffer = NULL; Signer* s = NULL; + *out = NULL; + WC_ALLOC_VAR_EX(dCertAdd, DecodedCert, 1, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER, { ret = MEMORY_E; - goto exit_waived; + goto exit_mcs; }); InitDecodedCert(dCertAdd, cert->buffer, cert->length, ssl->heap); dCertAdd_inited = 1; +#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) + /* A signer doesn't keep the X509_NAMEs, don't build them. */ + dCertAdd->noX509Name = 1; +#endif ret = ParseCert(dCertAdd, CA_TYPE, NO_VERIFY, SSL_CM(ssl)); if (ret != 0) - goto exit_waived; + goto exit_mcs; if (!dCertAdd->isCA) { - WOLFSSL_MSG("Waived chain cert is not a CA, not usable as a signer"); - goto exit_waived; + WOLFSSL_MSG("Chain cert is not a CA, not usable as a signer"); + goto exit_mcs; } #ifndef ALLOW_INVALID_CERTSIGN + /* A CA needs keyCertSign once a Key Usage extension is present. Report + * the same error AddCA() would. */ if (!dCertAdd->selfSigned && dCertAdd->extKeyUsageSet && (dCertAdd->extKeyUsage & KEYUSE_KEY_CERT_SIGN) == 0) { - WOLFSSL_MSG("Waived chain cert doesn't have key usage cert signing"); + WOLFSSL_MSG("Chain cert doesn't have key usage certificate signing"); ret = NOT_CA_ERROR; - goto exit_waived; + goto exit_mcs; } #endif + /* The Extended Key Usage purpose check is deliberately not repeated here. + * ProcessPeerCerts() applies it to this same certificate before offering it + * to a signer, and AddCA() does not apply it either, so repeating it would + * only take effect after a verify callback had already overridden the + * rejection, silently undoing that decision. */ ret = AllocDer(&derBuffer, cert->length, CA_TYPE, ssl->heap); if (ret != 0 || derBuffer == NULL) - goto exit_waived; + goto exit_mcs; XMEMCPY(derBuffer->buffer, cert->buffer, cert->length); s = MakeSigner(SSL_CM(ssl)->heap); if (s == NULL) { ret = MEMORY_E; - goto exit_waived; + goto exit_mcs; } - ret = FillSigner(s, dCertAdd, WOLFSSL_TEMP_CA, derBuffer); + ret = FillSigner(s, dCertAdd, type, derBuffer); if (ret != 0) - goto exit_waived; + goto exit_mcs; - s->next = args->waivedCAs; - args->waivedCAs = s; + *out = s; s = NULL; -exit_waived: +exit_mcs: if (s != NULL) FreeSigner(s, SSL_CM(ssl)->heap); if (derBuffer != NULL) @@ -17944,6 +17956,23 @@ static int ProcessPeerCertAddWaivedCA(WOLFSSL* ssl, ProcPeerCertArgs* args, return ret; } +/* Add a chain CA the verify callback accepted to a list owned by this + * Certificate message. */ +static int ProcessPeerCertAddWaivedCA(WOLFSSL* ssl, ProcPeerCertArgs* args, + buffer* cert) +{ + Signer* s = NULL; + int ret; + + ret = ProcessPeerCertMakeChainSigner(ssl, cert, WOLFSSL_TEMP_CA, &s); + if (ret == 0 && s != NULL) { + s->next = args->waivedCAs; + args->waivedCAs = s; + } + + return ret; +} + static int ProcessPeerCertIsWaivedCA(ProcPeerCertArgs* args, const Signer* ca) { const Signer* s; @@ -18885,88 +18914,20 @@ static int CheckChainCAExtKeyUsage(const WOLFSSL* ssl, const DecodedCert* cert) * for Certificate Status Request v2. */ static int ProcessPeerCertAddPendingCA(WOLFSSL* ssl, buffer* cert) { - int ret = 0; - WC_DECLARE_VAR(dCertAdd, DecodedCert, 1, 0); - int dCertAdd_inited = 0; - DerBuffer *derBuffer = NULL; - Signer *s = NULL; + Signer* s = NULL; + int ret; - WC_ALLOC_VAR_EX(dCertAdd, DecodedCert, 1, ssl->heap, - DYNAMIC_TYPE_TMP_BUFFER, - { - ret=MEMORY_E; - goto exit_req_v2; - }); - InitDecodedCert(dCertAdd, cert->buffer, cert->length, - ssl->heap); -#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) - /* A signer doesn't keep the X509_NAMEs, don't build them. */ - dCertAdd->noX509Name = 1; -#endif - dCertAdd_inited = 1; - ret = ParseCert(dCertAdd, CA_TYPE, NO_VERIFY, - SSL_CM(ssl)); - if (ret != 0) { - goto exit_req_v2; - } - /* Only a certificate that is actually usable as a CA may enter the pending - * signer pool. ParseCertRelative() consults that pool ahead of the - * certificate manager and uses the signer as a verification key without - * further checks, so admission has to enforce the same capabilities AddCA() - * requires of a chain CA. - * - * A non-CA is skipped rather than reported as an error, because - * ProcessPeerCerts() only offers a chain cert to AddCA() when isCA is set: - * such a certificate is already left out of the certificate manager without - * failing the handshake. */ - if (!dCertAdd->isCA) { - WOLFSSL_MSG("Chain cert is not a CA, not adding as pending CA"); - goto exit_req_v2; - } -#ifndef ALLOW_INVALID_CERTSIGN - /* A CA needs keyCertSign once a Key Usage extension is present. Fail the - * handshake rather than quietly leaving it out of the pool. */ - if (!dCertAdd->selfSigned && dCertAdd->extKeyUsageSet && - (dCertAdd->extKeyUsage & KEYUSE_KEY_CERT_SIGN) == 0) { - WOLFSSL_MSG("Chain cert doesn't have key usage certificate signing"); - ret = NOT_CA_ERROR; - goto exit_req_v2; - } -#endif - /* The Extended Key Usage purpose check is deliberately not repeated here. - * ProcessPeerCerts() applies it to this same certificate before offering it - * to the pool, and AddCA() does not apply it either, so repeating it would - * only take effect after a verify callback had already overridden the - * rejection, silently undoing that decision in CSR v2 builds alone. */ - ret = AllocDer(&derBuffer, cert->length, CA_TYPE, ssl->heap); - if (ret != 0 || derBuffer == NULL) { - goto exit_req_v2; - } - XMEMCPY(derBuffer->buffer, cert->buffer, cert->length); - s = MakeSigner(SSL_CM(ssl)->heap); - if (s == NULL) { - ret = MEMORY_E; - goto exit_req_v2; - } - /* WOLFSSL_CHAIN_CA, not CA_TYPE: TLSX_CSR2_MergePendingCA() promotes this - * signer into the certificate manager, and the unload path + /* WOLFSSL_CHAIN_CA, not WOLFSSL_TEMP_CA: TLSX_CSR2_MergePendingCA() + * promotes this signer into the certificate manager, and the unload path * (wolfSSL_CertManagerUnloadIntermediateCerts()) selects entries by that * type. AddCA() records chain CAs the same way. */ - ret = FillSigner(s, dCertAdd, WOLFSSL_CHAIN_CA, derBuffer); - if (ret != 0) { - goto exit_req_v2; + ret = ProcessPeerCertMakeChainSigner(ssl, cert, WOLFSSL_CHAIN_CA, &s); + if (ret == 0 && s != NULL) { + ret = TLSX_CSR2_AddPendingSigner(ssl->extensions, s); + if (ret != 0) + FreeSigner(s, SSL_CM(ssl)->heap); } - ret = TLSX_CSR2_AddPendingSigner(ssl->extensions, s); -exit_req_v2: - if (s && (ret != 0)) - FreeSigner(s, SSL_CM(ssl)->heap); - if (derBuffer) - FreeDer(&derBuffer); - if (dCertAdd_inited) - FreeDecodedCert(dCertAdd); - WC_FREE_VAR_EX(dCertAdd, ssl->heap, - DYNAMIC_TYPE_TMP_BUFFER); return ret; } #endif /* HAVE_CERTIFICATE_STATUS_REQUEST_V2 */ diff --git a/wolfssl/ssl.h b/wolfssl/ssl.h index 817ad51ac26..c02b92e374f 100644 --- a/wolfssl/ssl.h +++ b/wolfssl/ssl.h @@ -4198,8 +4198,9 @@ enum { WOLFSSL_USER_CA = 1, /* user added as trusted */ WOLFSSL_CHAIN_CA = 2, /* added to cache from trusted chain */ - WOLFSSL_TEMP_CA = 3, /* Temp intermediate CA, only for use by - * X509_STORE */ + WOLFSSL_TEMP_CA = 3, /* Temp intermediate CA, scoped to a single + * X509_STORE verification or Certificate + * message */ WOLFSSL_USER_INTER = 4 /* user added intermediate cert */ }; From 0cbac245438d551072d2372dbecc99fd6d000410 Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Wed, 16 Sep 2026 21:03:08 +0200 Subject: [PATCH 03/20] Fix test #define gating --- tests/api.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/api.c b/tests/api.c index 52a31ad7c2a..6b416400007 100644 --- a/tests/api.c +++ b/tests/api.c @@ -42980,8 +42980,8 @@ static int test_override_alt_cert_chain(void) } #endif -#if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_RSA) && \ - !defined(WOLFSSL_ALT_CERT_CHAINS) +#if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \ + defined(WOLFSSL_PEM_TO_DER) && !defined(WOLFSSL_ALT_CERT_CHAINS) static int test_chain_ca_no_persist_verify_cb(int preverify, WOLFSSL_X509_STORE_CTX* store) { From 85a85924a94e8a2de0f5274a2f0eefdd07a34603 Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Wed, 16 Sep 2026 22:55:28 +0200 Subject: [PATCH 04/20] Address Fenrir findings --- src/internal.c | 2 +- wolfcrypt/src/asn.c | 27 ++++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/internal.c b/src/internal.c index 5a61e0c38aa..7a5e24f6471 100644 --- a/src/internal.c +++ b/src/internal.c @@ -17897,7 +17897,7 @@ static int ProcessPeerCertMakeChainSigner(WOLFSSL* ssl, buffer* cert, int type, ret = MEMORY_E; goto exit_mcs; }); - InitDecodedCert(dCertAdd, cert->buffer, cert->length, ssl->heap); + InitDecodedCert(dCertAdd, cert->buffer, cert->length, SSL_CM(ssl)->heap); dCertAdd_inited = 1; #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) /* A signer doesn't keep the X509_NAMEs, don't build them. */ diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index 3dfed4c11d5..06589fb7a0d 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -26550,9 +26550,30 @@ int ParseCertRelative(DecodedCert* cert, int type, int verify, if (!cert->selfIssued || (verify != NO_VERIFY && type != CA_TYPE && type != TRUSTED_PEER_TYPE)) { cert->ca = NULL; - if (extraCAList != NULL) { - cert->ca = findSignerByName(extraCAList, cert->issuerHash); - } + if (extraCAList != NULL) { + #ifndef NO_SKID + if (cert->extAuthKeyIdSet) { + Signer* exCa; + + for (exCa = extraCAList; exCa != NULL; exCa = exCa->next) { + if (XMEMCMP(exCa->subjectKeyIdHash, + cert->extAuthKeyId, + SIGNER_DIGEST_SIZE) == 0 && + XMEMCMP(exCa->subjectNameHash, cert->issuerHash, + SIGNER_DIGEST_SIZE) == 0) { + cert->ca = exCa; + break; + } + } + /* AKID is authoritative; do not fall back to name. */ + } + else + #endif + { + cert->ca = findSignerByName(extraCAList, + cert->issuerHash); + } + } #ifndef NO_SKID if (cert->ca == NULL && cert->extAuthKeyIdSet) { cert->ca = GetCA(cm, cert->extAuthKeyId); From 25fa25f646ef3a32a50d38638fa9f8ceff37619a Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Wed, 14 Oct 2026 04:22:16 +0200 Subject: [PATCH 05/20] Fix handling of CAs waived by the verify callback - A chain could become longer than the waived CA's path length; the limit was being lost and silently replaced with the maximum - A hash used to authorize delegated OCSP responders was left blank on waived CAs - OCSP responses signed by a waived CA no longer fail to verify - The callback is no longer asked again for every cert below a waived CA in WOLFSSL_SMALL_CERT_VERIFY builds - Unknown-extension callback is now called on waived CAs with an unknown critical extension - Changed waived CA list lifetime to last for the whole connection rather than the Certificate message, so it's still there when a stapled OCSP status arrives --- src/internal.c | 101 +++++++++++++++++++++++++++------------- src/ocsp.c | 13 +++++- wolfcrypt/src/asn.c | 10 +++- wolfssl/internal.h | 9 +++- wolfssl/wolfcrypt/asn.h | 2 + 5 files changed, 97 insertions(+), 38 deletions(-) diff --git a/src/internal.c b/src/internal.c index 7a5e24f6471..38907235d93 100644 --- a/src/internal.c +++ b/src/internal.c @@ -10641,6 +10641,15 @@ void wolfSSL_ResourceFree(WOLFSSL* ssl) * example with the RNG, it isn't used beyond the handshake except when * using stream ciphers where it is retained. */ +#ifndef NO_CERTS + while (ssl->waivedCAs != NULL) { + Signer* waived = ssl->waivedCAs; + + ssl->waivedCAs = waived->next; + FreeSigner(waived, SSL_CM(ssl)->heap); + } +#endif + if (ssl->options.side == WOLFSSL_SERVER_END) { WOLFSSL_MSG("Free'ing server ssl"); } @@ -17878,10 +17887,35 @@ void DoCrlCallback(WOLFSSL_CERT_MANAGER* cm, WOLFSSL* ssl, } #endif +#ifndef NO_CERTS +/* Join the connection's accepted CAs onto base for one lookup. */ +Signer* SslWaivedCAsJoin(WOLFSSL* ssl, Signer* base, Signer** pTail) +{ + Signer* tail = NULL; + + if (ssl != NULL && ssl->waivedCAs != NULL) { + tail = ssl->waivedCAs; + while (tail->next != NULL) + tail = tail->next; + tail->next = base; + base = ssl->waivedCAs; + } + *pTail = tail; + + return base; +} + +void SslWaivedCAsSplit(Signer* tail) +{ + if (tail != NULL) + tail->next = NULL; +} +#endif /* !NO_CERTS */ + #ifndef NO_ASN /* Parse a chain certificate as a CA and return it as a filled signer. */ static int ProcessPeerCertMakeChainSigner(WOLFSSL* ssl, buffer* cert, int type, - Signer** out) + Signer* extraCa, Signer** out) { int ret = 0; WC_DECLARE_VAR(dCertAdd, DecodedCert, 1, 0); @@ -17903,7 +17937,11 @@ static int ProcessPeerCertMakeChainSigner(WOLFSSL* ssl, buffer* cert, int type, /* A signer doesn't keep the X509_NAMEs, don't build them. */ dCertAdd->noX509Name = 1; #endif - ret = ParseCert(dCertAdd, CA_TYPE, NO_VERIFY, SSL_CM(ssl)); +#ifdef WC_ASN_UNKNOWN_EXT_CB + if (SSL_CM(ssl)->unknownExtCallback != NULL) + wc_SetUnknownExtCallback(dCertAdd, SSL_CM(ssl)->unknownExtCallback); +#endif + ret = ParseCertEx(dCertAdd, CA_TYPE, NO_VERIFY, SSL_CM(ssl), extraCa); if (ret != 0) goto exit_mcs; @@ -17956,28 +17994,27 @@ static int ProcessPeerCertMakeChainSigner(WOLFSSL* ssl, buffer* cert, int type, return ret; } -/* Add a chain CA the verify callback accepted to a list owned by this - * Certificate message. */ -static int ProcessPeerCertAddWaivedCA(WOLFSSL* ssl, ProcPeerCertArgs* args, - buffer* cert) +/* Add a chain CA the verify callback accepted to the connection's list. */ +static int ProcessPeerCertAddWaivedCA(WOLFSSL* ssl, buffer* cert) { Signer* s = NULL; int ret; - ret = ProcessPeerCertMakeChainSigner(ssl, cert, WOLFSSL_TEMP_CA, &s); + ret = ProcessPeerCertMakeChainSigner(ssl, cert, WOLFSSL_TEMP_CA, + ssl->waivedCAs, &s); if (ret == 0 && s != NULL) { - s->next = args->waivedCAs; - args->waivedCAs = s; + s->next = ssl->waivedCAs; + ssl->waivedCAs = s; } return ret; } -static int ProcessPeerCertIsWaivedCA(ProcPeerCertArgs* args, const Signer* ca) +static int ProcessPeerCertIsWaivedCA(WOLFSSL* ssl, const Signer* ca) { const Signer* s; - for (s = args->waivedCAs; s != NULL; s = s->next) { + for (s = ssl->waivedCAs; s != NULL; s = s->next) { if (s == ca) return 1; } @@ -17992,15 +18029,6 @@ static void FreeProcPeerCertArgs(WOLFSSL* ssl, void* pArgs) (void)ssl; -#ifndef NO_ASN - while (args->waivedCAs != NULL) { - Signer* s = args->waivedCAs; - - args->waivedCAs = s->next; - FreeSigner(s, SSL_CM(ssl)->heap); - } -#endif - XFREE(args->certs, ssl->heap, DYNAMIC_TYPE_DER); args->certs = NULL; #ifdef WOLFSSL_TLS13 @@ -18431,13 +18459,7 @@ PRAGMA_GCC_DIAG_POP } #endif /* Parse Certificate */ - if (args->waivedCAs != NULL) { - waivedTail = args->waivedCAs; - while (waivedTail->next != NULL) - waivedTail = waivedTail->next; - waivedTail->next = extraSigners; - extraSigners = args->waivedCAs; - } + extraSigners = SslWaivedCAsJoin(ssl, extraSigners, &waivedTail); ret = ParseCertRelative(args->dCert, certType, verify, SSL_CM(ssl), extraSigners); #ifdef KEEP_PEER_CERT @@ -18457,8 +18479,7 @@ PRAGMA_GCC_DIAG_POP } #endif - if (waivedTail != NULL) - waivedTail->next = NULL; + SslWaivedCAsSplit(waivedTail); #if defined(HAVE_RPK) /* Confirm the received certificate's form (X.509 vs raw public key) matches @@ -18489,6 +18510,15 @@ PRAGMA_GCC_DIAG_POP } #ifdef WOLFSSL_SMALL_CERT_VERIFY + /* The check above searched the cert manager only; redo it on the key. */ + if (sigRet != 0 && args->dCert->ca != NULL && + args->dCert->ca->publicKey != NULL && + ProcessPeerCertIsWaivedCA(ssl, args->dCert->ca)) { + sigRet = CheckCertSignaturePubKey(cert->buffer, cert->length, + ssl->heap, args->dCert->ca->publicKey, + args->dCert->ca->pubKeySize, (int)args->dCert->ca->keyOID); + } + /* get signature check failures from above (a negotiated RPK leaf is parsed * with NO_VERIFY, so no signature check was run for it) */ if (ret == 0 || (sigRet != 0 && IsPostSigParseErr(ret))) { @@ -18921,7 +18951,8 @@ static int ProcessPeerCertAddPendingCA(WOLFSSL* ssl, buffer* cert) * promotes this signer into the certificate manager, and the unload path * (wolfSSL_CertManagerUnloadIntermediateCerts()) selects entries by that * type. AddCA() records chain CAs the same way. */ - ret = ProcessPeerCertMakeChainSigner(ssl, cert, WOLFSSL_CHAIN_CA, &s); + ret = ProcessPeerCertMakeChainSigner(ssl, cert, WOLFSSL_CHAIN_CA, + NULL, &s); if (ret == 0 && s != NULL) { ret = TLSX_CSR2_AddPendingSigner(ssl->extensions, s); if (ret != 0) @@ -20639,7 +20670,7 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, if (ret == 0 && preCbRet != 0) caIsTemp = 1; if (ret == 0 && args->dCert->ca != NULL && - ProcessPeerCertIsWaivedCA(args, args->dCert->ca)) + ProcessPeerCertIsWaivedCA(ssl, args->dCert->ca)) caIsTemp = 1; if (CrlErrWaived(ssl, ret)) ret = ssl->error = 0; @@ -20702,7 +20733,7 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, } #endif /* SESSION_CERTS && WOLFSSL_ALT_CERT_CHAINS */ if (caIsTemp) { - ret = ProcessPeerCertAddWaivedCA(ssl, args, cert); + ret = ProcessPeerCertAddWaivedCA(ssl, cert); } else if (!alreadySigner) { DerBuffer* add = NULL; @@ -21619,6 +21650,7 @@ static int DoCertificateStatus(WOLFSSL* ssl, byte* input, word32* inOutIdx, word32 list_length = status_length; byte idx = 0; Signer *pendingCAs = NULL; + Signer *waivedTail = NULL; #ifdef WOLFSSL_SMALL_STACK CertStatus* status; @@ -21677,7 +21709,8 @@ static int DoCertificateStatus(WOLFSSL* ssl, byte* input, word32* inOutIdx, ) { InitOcspResponse(response, single, status, input +*inOutIdx, status_length, ssl->heap); - response->pendingCAs = pendingCAs; + response->pendingCAs = SslWaivedCAsJoin(ssl, pendingCAs, + &waivedTail); if ((OcspResponseDecode(response, SSL_CM(ssl), ssl->heap, 0, 0) != 0) || (response->responseStatus != OCSP_SUCCESSFUL) @@ -21703,6 +21736,8 @@ static int DoCertificateStatus(WOLFSSL* ssl, byte* input, word32* inOutIdx, /* only frees 'single' if single->isDynamic is set */ FreeOcspResponse(response); + SslWaivedCAsSplit(waivedTail); + waivedTail = NULL; ret = CsrDoStatusVerifyCb(ssl, input + *inOutIdx, status_length, idx, ret); diff --git a/src/ocsp.c b/src/ocsp.c index d2f72f937b6..5d176177261 100644 --- a/src/ocsp.c +++ b/src/ocsp.c @@ -351,6 +351,9 @@ int CheckOcspResponse(WOLFSSL_OCSP *ocsp, byte *response, int responseSz, #endif int ret; int validated = 0; /* ocsp validation flag */ +#ifndef NO_CERTS + Signer* waivedTail = NULL; +#endif (void)heap; @@ -377,9 +380,12 @@ int CheckOcspResponse(WOLFSSL_OCSP *ocsp, byte *response, int responseSz, if (ssl != NULL && TLSX_CSR2_IsMulti(ssl->extensions)) { ocspResponse->pendingCAs = TLSX_CSR2_GetPendingSigners(ssl->extensions); } -#else - (void)ssl; #endif +#ifndef NO_CERTS + ocspResponse->pendingCAs = SslWaivedCAsJoin(ssl, ocspResponse->pendingCAs, + &waivedTail); +#endif + (void)ssl; ret = OcspResponseDecode(ocspResponse, ocsp->cm, ocsp->cm->heap, 0, 0); if (ret != 0) { ocsp->error = ret; @@ -455,6 +461,9 @@ int CheckOcspResponse(WOLFSSL_OCSP *ocsp, byte *response, int responseSz, wc_UnLockMutex(&ocsp->ocspLock); end: +#ifndef NO_CERTS + SslWaivedCAsSplit(waivedTail); +#endif if (ret == 0 && validated == 1) { WOLFSSL_MSG("New OcspResponse validated"); } diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index 06589fb7a0d..6e794855c27 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -25147,7 +25147,8 @@ static int DecodeCertReq(DecodedCert* cert, int* criticalExt) #endif /* WOLFSSL_ASN_TEMPLATE */ -int ParseCert(DecodedCert* cert, int type, int verify, WOLFSSL_CERT_MANAGER* cm) +int ParseCertEx(DecodedCert* cert, int type, int verify, + WOLFSSL_CERT_MANAGER* cm, Signer* extraCa) { int ret; #if (!defined(WC_ASN_NO_HEAP) && !defined(NO_WOLFSSL_CM_VERIFY)) || \ @@ -25155,7 +25156,7 @@ int ParseCert(DecodedCert* cert, int type, int verify, WOLFSSL_CERT_MANAGER* cm) char* ptr; #endif - ret = ParseCertRelative(cert, type, verify, cm, NULL); + ret = ParseCertRelative(cert, type, verify, cm, extraCa); if (ret < 0) return ret; @@ -25198,6 +25199,11 @@ int ParseCert(DecodedCert* cert, int type, int verify, WOLFSSL_CERT_MANAGER* cm) return ret; } +int ParseCert(DecodedCert* cert, int type, int verify, WOLFSSL_CERT_MANAGER* cm) +{ + return ParseCertEx(cert, type, verify, cm, NULL); +} + int wc_ParseCert(DecodedCert* cert, int type, int verify, WOLFSSL_CERT_MANAGER* cm) { diff --git a/wolfssl/internal.h b/wolfssl/internal.h index 772172ee1d4..c2b6ff5c4ca 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -3018,7 +3018,6 @@ typedef struct ProcPeerCertArgs { #endif #ifndef NO_ASN DecodedCert* dCert; - Signer* waivedCAs; /* chain CAs the verify callback waived */ #endif word32 idx; word32 begin; @@ -3063,6 +3062,11 @@ typedef struct ProcPeerCertArgs { } ProcPeerCertArgs; WOLFSSL_LOCAL int DoVerifyCallback(WOLFSSL_CERT_MANAGER* cm, WOLFSSL* ssl, int cert_err, ProcPeerCertArgs* args); +#ifndef NO_CERTS +WOLFSSL_LOCAL Signer* SslWaivedCAsJoin(WOLFSSL* ssl, Signer* base, + Signer** pTail); +WOLFSSL_LOCAL void SslWaivedCAsSplit(Signer* tail); +#endif WOLFSSL_LOCAL void DoCrlCallback(WOLFSSL_CERT_MANAGER* cm, WOLFSSL* ssl, ProcPeerCertArgs* args, int* outRet); @@ -7358,6 +7362,9 @@ struct WOLFSSL { #ifdef WOLFSSL_LOCAL_X509_STORE WOLFSSL_X509_STORE* x509_store_pt; /* take ownership of external store */ #endif +#ifndef NO_CERTS + Signer* waivedCAs; /* callback-accepted chain CAs */ +#endif #ifdef KEEP_PEER_CERT #ifdef WOLFSSL_DYNAMIC_PEER_CERT WOLFSSL_X509* peerCert; /* X509 peer cert, NULL when none diff --git a/wolfssl/wolfcrypt/asn.h b/wolfssl/wolfcrypt/asn.h index d5f89a90f0e..97a0bd37469 100644 --- a/wolfssl/wolfcrypt/asn.h +++ b/wolfssl/wolfcrypt/asn.h @@ -2653,6 +2653,8 @@ WOLFSSL_ASN_API int AddSignature(byte* buf, int bodySz, const byte* sig, int sig WOLFSSL_LOCAL int ParseCertRelative(DecodedCert* cert, int type, int verify, WOLFSSL_CERT_MANAGER* cm, Signer *extraCa); +WOLFSSL_LOCAL int ParseCertEx(DecodedCert* cert, int type, int verify, + WOLFSSL_CERT_MANAGER* cm, Signer* extraCa); WOLFSSL_LOCAL int DecodeToKey(DecodedCert* cert, int verify); #ifdef WOLFSSL_ASN_TEMPLATE WOLFSSL_LOCAL int DecodeCert(DecodedCert* cert, int verify, int* criticalExt); From 93ac3a2a65bfead8bca2106f06cd9771c826803f Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Wed, 14 Oct 2026 05:41:23 +0200 Subject: [PATCH 06/20] Fix server only build --- src/internal.c | 48 ++++++++++++++++++++++------------------------ wolfssl/internal.h | 9 ++++----- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/src/internal.c b/src/internal.c index 38907235d93..b674fde8e89 100644 --- a/src/internal.c +++ b/src/internal.c @@ -17265,6 +17265,29 @@ int InitSigPkCb(WOLFSSL* ssl, SignatureCtx* sigCtx) #endif /* HAVE_PK_CALLBACKS */ +/* Join the connection's accepted CAs onto base for one lookup. */ +Signer* SslWaivedCAsJoin(WOLFSSL* ssl, Signer* base, Signer** pTail) +{ + Signer* tail = NULL; + + if (ssl != NULL && ssl->waivedCAs != NULL) { + tail = ssl->waivedCAs; + while (tail->next != NULL) + tail = tail->next; + tail->next = base; + base = ssl->waivedCAs; + } + *pTail = tail; + + return base; +} + +void SslWaivedCAsSplit(Signer* tail) +{ + if (tail != NULL) + tail->next = NULL; +} + #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH) #ifdef WOLFSSL_CHAIN_VERIFY_CB @@ -17887,31 +17910,6 @@ void DoCrlCallback(WOLFSSL_CERT_MANAGER* cm, WOLFSSL* ssl, } #endif -#ifndef NO_CERTS -/* Join the connection's accepted CAs onto base for one lookup. */ -Signer* SslWaivedCAsJoin(WOLFSSL* ssl, Signer* base, Signer** pTail) -{ - Signer* tail = NULL; - - if (ssl != NULL && ssl->waivedCAs != NULL) { - tail = ssl->waivedCAs; - while (tail->next != NULL) - tail = tail->next; - tail->next = base; - base = ssl->waivedCAs; - } - *pTail = tail; - - return base; -} - -void SslWaivedCAsSplit(Signer* tail) -{ - if (tail != NULL) - tail->next = NULL; -} -#endif /* !NO_CERTS */ - #ifndef NO_ASN /* Parse a chain certificate as a CA and return it as a filled signer. */ static int ProcessPeerCertMakeChainSigner(WOLFSSL* ssl, buffer* cert, int type, diff --git a/wolfssl/internal.h b/wolfssl/internal.h index c2b6ff5c4ca..11ed803a36e 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -3010,6 +3010,10 @@ WOLFSSL_LOCAL int CM_VerifyBuffer_ex(WOLFSSL_CERT_MANAGER* cm, const byte* buff, #ifndef NO_CERTS +WOLFSSL_LOCAL Signer* SslWaivedCAsJoin(WOLFSSL* ssl, Signer* base, + Signer** pTail); +WOLFSSL_LOCAL void SslWaivedCAsSplit(Signer* tail); + #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH) typedef struct ProcPeerCertArgs { buffer* certs; @@ -3062,11 +3066,6 @@ typedef struct ProcPeerCertArgs { } ProcPeerCertArgs; WOLFSSL_LOCAL int DoVerifyCallback(WOLFSSL_CERT_MANAGER* cm, WOLFSSL* ssl, int cert_err, ProcPeerCertArgs* args); -#ifndef NO_CERTS -WOLFSSL_LOCAL Signer* SslWaivedCAsJoin(WOLFSSL* ssl, Signer* base, - Signer** pTail); -WOLFSSL_LOCAL void SslWaivedCAsSplit(Signer* tail); -#endif WOLFSSL_LOCAL void DoCrlCallback(WOLFSSL_CERT_MANAGER* cm, WOLFSSL* ssl, ProcPeerCertArgs* args, int* outRet); From 9888cf9c4e95008a6a4ae8ccb5d51540fbd0d5f9 Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Fri, 6 Nov 2026 19:46:08 +0100 Subject: [PATCH 07/20] ParseCertRelative NO_SKID: don't rerun GetCA if CA already found --- wolfcrypt/src/asn.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index 6e794855c27..4232247a9a5 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -26635,7 +26635,8 @@ int ParseCertRelative(DecodedCert* cert, int type, int verify, } #endif /* WOLFSSL_NO_TRUSTED_CERTS_VERIFY */ #else - cert->ca = GetCA(cm, cert->issuerHash); + if (cert->ca == NULL) + cert->ca = GetCA(cm, cert->issuerHash); #endif /* !NO_SKID */ if (cert->ca) { From 54a28ec5f6352e34cfa2d7585078732d41e3ed09 Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Fri, 6 Nov 2026 22:24:03 +0100 Subject: [PATCH 08/20] Let OCSP find CAs the verify callback accepted The lookup that searches an OCSP responder's extra signer list was compiled only when OCSP stapling v2 was enabled, while it's now needed for the certs accepted via the verify callback. --- wolfcrypt/src/asn.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index 4232247a9a5..baa26453131 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -38186,7 +38186,7 @@ static Signer *OcspFindSigner(OcspResponse *resp, WOLFSSL_CERT_MANAGER *cm) if (s) return s; } -#if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) +#ifndef NO_CERTS if (resp->pendingCAs == NULL) return NULL; From 89307a16fc701f4bfd351f529bea9bd0806a294a Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Fri, 6 Nov 2026 23:02:01 +0100 Subject: [PATCH 09/20] Ensure wolfSSL_clear() also clears the waived CA list --- src/internal.c | 18 ++++++++++++------ src/ssl.c | 5 +++++ wolfssl/internal.h | 1 + 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/internal.c b/src/internal.c index b674fde8e89..059e88ff9e3 100644 --- a/src/internal.c +++ b/src/internal.c @@ -10642,12 +10642,7 @@ void wolfSSL_ResourceFree(WOLFSSL* ssl) * using stream ciphers where it is retained. */ #ifndef NO_CERTS - while (ssl->waivedCAs != NULL) { - Signer* waived = ssl->waivedCAs; - - ssl->waivedCAs = waived->next; - FreeSigner(waived, SSL_CM(ssl)->heap); - } + SslWaivedCAsFree(ssl); #endif if (ssl->options.side == WOLFSSL_SERVER_END) { @@ -17288,6 +17283,17 @@ void SslWaivedCAsSplit(Signer* tail) tail->next = NULL; } +/* Drop the CAs that were accepted by a verify callback */ +void SslWaivedCAsFree(WOLFSSL* ssl) +{ + while (ssl->waivedCAs != NULL) { + Signer* waived = ssl->waivedCAs; + + ssl->waivedCAs = waived->next; + FreeSigner(waived, SSL_CM(ssl)->heap); + } +} + #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH) #ifdef WOLFSSL_CHAIN_VERIFY_CB diff --git a/src/ssl.c b/src/ssl.c index 6dd0b4c1685..8e8f61f3566 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -5810,6 +5810,11 @@ size_t wolfSSL_get_client_random(const WOLFSSL* ssl, unsigned char* out, #endif } +#ifndef NO_CERTS + /* Trust for the previous peer must not carry into the next. */ + SslWaivedCAsFree(ssl); +#endif + /* reset error */ ssl->error = 0; diff --git a/wolfssl/internal.h b/wolfssl/internal.h index 11ed803a36e..06c538dc2f0 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -3013,6 +3013,7 @@ WOLFSSL_LOCAL int CM_VerifyBuffer_ex(WOLFSSL_CERT_MANAGER* cm, const byte* buff, WOLFSSL_LOCAL Signer* SslWaivedCAsJoin(WOLFSSL* ssl, Signer* base, Signer** pTail); WOLFSSL_LOCAL void SslWaivedCAsSplit(Signer* tail); +WOLFSSL_LOCAL void SslWaivedCAsFree(WOLFSSL* ssl); #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH) typedef struct ProcPeerCertArgs { From 6bf68e7867b127ec4d6c4cbff0e629b2dc241ab0 Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Sat, 7 Nov 2026 06:30:22 +0100 Subject: [PATCH 10/20] Let stapled OCSP responses find CAs accepted by verify callback --- src/internal.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/internal.c b/src/internal.c index 059e88ff9e3..482f4a87e8f 100644 --- a/src/internal.c +++ b/src/internal.c @@ -17057,6 +17057,7 @@ static int ProcessCSR_ex(WOLFSSL* ssl, byte* input, word32* inOutIdx, word32 status_length, int idx) { int ret = 0; + Signer* waivedTail = NULL; OcspRequest* request; #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) TLSX* ext = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST); @@ -17131,6 +17132,8 @@ static int ProcessCSR_ex(WOLFSSL* ssl, byte* input, word32* inOutIdx, /* InitOcspResponse sets single and status to response struct. */ InitOcspResponse(response, single, status, input +*inOutIdx, status_length, ssl->heap); + response->pendingCAs = SslWaivedCAsJoin(ssl, response->pendingCAs, + &waivedTail); /* Extract producedDate */ if (OcspResponseDecode(response, SSL_CM(ssl), ssl->heap, 1, 1) != 0) { @@ -17146,6 +17149,8 @@ static int ProcessCSR_ex(WOLFSSL* ssl, byte* input, word32* inOutIdx, FreeOcspResponse(response); InitOcspResponse(response, single, status, input + *inOutIdx, status_length, ssl->heap); + response->pendingCAs = SslWaivedCAsJoin(ssl, response->pendingCAs, + &waivedTail); if (OcspResponseDecode(response, SSL_CM(ssl), ssl->heap, 0, 0) != 0) ret = BAD_CERTIFICATE_STATUS_ERROR; @@ -17166,6 +17171,7 @@ static int ProcessCSR_ex(WOLFSSL* ssl, byte* input, word32* inOutIdx, /* FreeOcspResponse frees status and single only if * single->isDynamic is set. */ FreeOcspResponse(response); + SslWaivedCAsSplit(waivedTail); WC_FREE_VAR_EX(status, ssl->heap, DYNAMIC_TYPE_OCSP_STATUS); WC_FREE_VAR_EX(single, ssl->heap, DYNAMIC_TYPE_OCSP_ENTRY); From 9afe5a1640ed7690adbc50444b5061dcbc1304f1 Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Mon, 16 Nov 2026 22:06:15 +0100 Subject: [PATCH 11/20] Don't cache chain CA whose CRL result was held for OCSP staple --- src/internal.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/internal.c b/src/internal.c index 482f4a87e8f..c0ce4d5f088 100644 --- a/src/internal.c +++ b/src/internal.c @@ -20676,6 +20676,8 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, SetRevocationVerifyRet(ssl, ret); /* Do verify callback */ preCbRet = ret; + if (heldCrlRet != 0) + caIsTemp = 1; ret = DoVerifyCallbackHeldCrl(ssl, args, ret, &heldCrlRet); if (ret == 0 && preCbRet != 0) caIsTemp = 1; From 11dc31768ca7e68912b94f0d103c16ecd6f2f3c1 Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Tue, 17 Nov 2026 18:55:07 +0100 Subject: [PATCH 12/20] Let CRL checks and X509_verify_cert find CAs waived by verify callback - Pass the connection's waived CA list down the handshake CRL checks to VerifyCRLE; - Don't put a CRL entry's verdict into the cert manager when a CA waived by the callback verified it; - Carry the waived CA list on WOLFSSL_X509_STORE_CTX so an X509_verify_cert() from the callback resolves the waived CAs too; - Clear it in X509_STORE_CTX_init so a context built by the application only verifies against the store; - Check in test_wolfSSL_X509_STORE_CTX_set0_crls_handshake that the chain CAs go into the waived CA list rather than to the certificate store. --- src/crl.c | 44 +++++++++++++++++++++------------- src/internal.c | 14 ++++++----- src/ssl_certman.c | 11 +++++---- src/ssl_load.c | 3 ++- src/x509_str.c | 6 +++-- tests/api/test_ossl_x509_str.c | 13 ++++++---- wolfssl/crl.h | 7 +++--- wolfssl/internal.h | 4 ++-- wolfssl/ssl.h | 3 +++ 9 files changed, 66 insertions(+), 39 deletions(-) diff --git a/src/crl.c b/src/crl.c index 1a62f7f521e..31af03e812a 100644 --- a/src/crl.c +++ b/src/crl.c @@ -485,7 +485,7 @@ static int FindRevokedSerial(RevokedCert* rc, byte* serial, int serialSz, /* cacheResult should only be set when cm is the owning cm of crl. A cached * result is not valid for other cms because they can trust different CAs. */ static int VerifyCRLE(const WOLFSSL_CRL* crl, CRL_Entry* crle, - WOLFSSL_CERT_MANAGER* cm, int cacheResult) + WOLFSSL_CERT_MANAGER* cm, Signer* extraCa, int cacheResult) { Signer* ca = NULL; SignatureCtx sigCtx; @@ -510,6 +510,18 @@ static int VerifyCRLE(const WOLFSSL_CRL* crl, CRL_Entry* crle, #else /* NO_SKID */ ca = GetCA(cm, crle->issuerHash); #endif /* NO_SKID */ + if (ca == NULL && extraCa != NULL) { + ca = findSignerByName(extraCa, crle->issuerHash); + #ifndef NO_SKID + if ((ca != NULL) && crle->extAuthKeyIdSet && + (XMEMCMP(ca->subjectKeyIdHash, crle->extAuthKeyId, + SIGNER_DIGEST_SIZE) != 0)) { + ca = NULL; + } + #endif + if (ca != NULL) + cacheResult = 0; + } if (ca == NULL) { WOLFSSL_MSG("Did NOT find CRL issuer CA"); return ASN_CRL_NO_SIGNER_E; @@ -538,7 +550,7 @@ static int VerifyCRLE(const WOLFSSL_CRL* crl, CRL_Entry* crle, static int CheckCertCRLList(WOLFSSL_CRL* crl, byte* issuerHash, byte* serial, int serialSz, byte* serialHash, int *pFoundEntry, - WOLFSSL_CERT_MANAGER* cm) + WOLFSSL_CERT_MANAGER* cm, Signer* extraCa) { CRL_Entry* crle; int foundEntry = 0; @@ -558,7 +570,7 @@ static int CheckCertCRLList(WOLFSSL_CRL* crl, byte* issuerHash, byte* serial, if (cm != crl->cm) { /* cm is not the owning cm of crl. The cached result is only * valid for the owning cm. Verify again without caching. */ - ret = VerifyCRLE(crl, crle, cm, 0); + ret = VerifyCRLE(crl, crle, cm, extraCa, 0); if (ret != 0) { WOLFSSL_MSG("Cannot use CRL as it didn't verify"); break; @@ -574,7 +586,7 @@ static int CheckCertCRLList(WOLFSSL_CRL* crl, byte* issuerHash, byte* serial, /* A different thread may have verified the entry while we * were waiting for the mutex. */ if (crle->verified == 0) - ret = VerifyCRLE(crl, crle, cm, 1); + ret = VerifyCRLE(crl, crle, cm, extraCa, 1); wc_UnLockMutex(&crle->verifyMutex); @@ -629,7 +641,7 @@ static int CheckCertCRLList(WOLFSSL_CRL* crl, byte* issuerHash, byte* serial, static int CheckCertCRLCm(WOLFSSL_CRL* crl, byte* issuerHash, byte* serial, int serialSz, byte* serialHash, const byte* extCrlInfo, int extCrlInfoSz, void* issuerName, WOLFSSL_CERT_MANAGER* cm, - int canRetry) + int canRetry, Signer* extraCa) { int foundEntry = 0; int ret = 0; @@ -651,7 +663,7 @@ static int CheckCertCRLCm(WOLFSSL_CRL* crl, byte* issuerHash, byte* serial, #endif ret = CheckCertCRLList(crl, issuerHash, serial, serialSz, serialHash, - &foundEntry, cm); + &foundEntry, cm, extraCa); #ifdef HAVE_CRL_IO if (foundEntry == 0) { @@ -665,7 +677,7 @@ static int CheckCertCRLCm(WOLFSSL_CRL* crl, byte* issuerHash, byte* serial, else if (cbRet >= 0) { /* try again */ ret = CheckCertCRLList(crl, issuerHash, serial, serialSz, - serialHash, &foundEntry, cm); + serialHash, &foundEntry, cm, extraCa); } } } @@ -684,7 +696,7 @@ static int CheckCertCRLCm(WOLFSSL_CRL* crl, byte* issuerHash, byte* serial, if (loadRet == WOLFSSL_SUCCESS) { /* try again */ ret = CheckCertCRLList(crl, issuerHash, serial, serialSz, - serialHash, &foundEntry, cm); + serialHash, &foundEntry, cm, extraCa); } } } @@ -731,10 +743,10 @@ static int CheckCertCRLCm(WOLFSSL_CRL* crl, byte* issuerHash, byte* serial, int CheckCertCRL_ex(WOLFSSL_CRL* crl, byte* issuerHash, byte* serial, int serialSz, byte* serialHash, const byte* extCrlInfo, - int extCrlInfoSz, void* issuerName) + int extCrlInfoSz, void* issuerName, Signer* extraCa) { return CheckCertCRLCm(crl, issuerHash, serial, serialSz, serialHash, - extCrlInfo, extCrlInfoSz, issuerName, crl->cm, 1); + extCrlInfo, extCrlInfoSz, issuerName, crl->cm, 1, extraCa); } /* Get the issuer X509_NAME of cert to pass to CheckCertCRLCm(). */ @@ -754,28 +766,28 @@ static void* CertCrlIssuerName(DecodedCert* cert) } static int CheckDecodedCertCRL(WOLFSSL_CRL* crl, DecodedCert* cert, - WOLFSSL_CERT_MANAGER* cm, int canRetry) + WOLFSSL_CERT_MANAGER* cm, int canRetry, Signer* extraCa) { void* issuerName = CertCrlIssuerName(cert); return CheckCertCRLCm(crl, cert->issuerHash, cert->serial, cert->serialSz, NULL, cert->extCrlInfo, cert->extCrlInfoSz, issuerName, cm, - canRetry); + canRetry, extraCa); } /* Is the cert ok with CRL, return 0 on success */ -int CheckCertCRL(WOLFSSL_CRL* crl, DecodedCert* cert) +int CheckCertCRL(WOLFSSL_CRL* crl, DecodedCert* cert, Signer* extraCa) { - return CheckDecodedCertCRL(crl, cert, crl->cm, 1); + return CheckDecodedCertCRL(crl, cert, crl->cm, 1, extraCa); } /* Check cert against crl using cm for CRL signature verification. Does not * modify crl, so crl can be a caller-owned object shared between threads. * Return 0 on success. */ int CheckCertCRLFromCm(WOLFSSL_CERT_MANAGER* cm, WOLFSSL_CRL* crl, - DecodedCert* cert) + DecodedCert* cert, Signer* extraCa) { - return CheckDecodedCertCRL(crl, cert, cm, 0); + return CheckDecodedCertCRL(crl, cert, cm, 0, extraCa); } #ifdef HAVE_CRL_UPDATE_CB diff --git a/src/internal.c b/src/internal.c index c0ce4d5f088..04e4727ac35 100644 --- a/src/internal.c +++ b/src/internal.c @@ -17422,6 +17422,8 @@ int SetupStoreCtxCallback(WOLFSSL_X509_STORE_CTX** store_pt, store->error_depth = args->certIdx; #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) store->peerCertDepth = args->certIdx; + /* An X509_verify_cert() from the callback resolves the chain as we do. */ + store->extraCa = (ssl != NULL) ? ssl->waivedCAs : NULL; #endif store->discardSessionCerts = 0; store->domain = domain; @@ -17904,7 +17906,7 @@ void DoCrlCallback(WOLFSSL_CERT_MANAGER* cm, WOLFSSL* ssl, /* Point to current cm to be able to verify CRL */ userCrl->cm = SSL_CM(ssl); *outRet = CheckCertCRLFromCm(SSL_CM(ssl), userCrl, - args->dCert); + args->dCert, ssl->waivedCAs); } else *outRet = CRL_MISSING; @@ -18790,7 +18792,7 @@ static int ProcessPeerCertsChainCRLCheck(WOLFSSL* ssl, ProcPeerCertArgs* args) for (; ret == 0 && ca != NULL && ca != prev; prev = ca, ca = GetCAByName(cm, ca->issuerNameHash)) { ret = CheckCertCRL_ex(cm->crl, ca->issuerNameHash, NULL, 0, - ca->serialHash, NULL, 0, NULL); + ca->serialHash, NULL, 0, NULL, ssl->waivedCAs); #ifdef WOLFSSL_NONBLOCK_OCSP if (ret == WC_NO_ERR_TRACE(OCSP_WANT_READ)) break; @@ -19092,7 +19094,7 @@ static int ProcessPeerCertLeafRevocation(WOLFSSL* ssl, ProcPeerCertArgs* args, int crlRet; WOLFSSL_MSG("Taking deferred Leaf CRL check"); - crlRet = CheckCertCRL(SSL_CM(ssl)->crl, args->dCert); + crlRet = CheckCertCRL(SSL_CM(ssl)->crl, args->dCert, ssl->waivedCAs); #ifdef WOLFSSL_NONBLOCK_OCSP if (crlRet == WC_NO_ERR_TRACE(OCSP_WANT_READ)) { *pRet = crlRet; @@ -19148,7 +19150,7 @@ static int ProcessPeerCertLeafRevocation(WOLFSSL* ssl, ProcPeerCertArgs* args, if ((ret == 0 || ret == WC_NO_ERR_TRACE(OCSP_CERT_UNKNOWN)) && doLookup && SSL_CM(ssl)->crlEnabled) { WOLFSSL_MSG("Doing Leaf CRL check"); - ret = CheckCertCRL(SSL_CM(ssl)->crl, args->dCert); + ret = CheckCertCRL(SSL_CM(ssl)->crl, args->dCert, ssl->waivedCAs); #ifdef WOLFSSL_NONBLOCK_OCSP /* The CRL lookup I/O callback is using the * same WOULD_BLOCK error code as OCSP's I/O @@ -20507,7 +20509,7 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, if (doCrlLookup) { WOLFSSL_MSG("Doing Non Leaf CRL check"); ret = CheckCertCRL(SSL_CM(ssl)->crl, - args->dCert); + args->dCert, ssl->waivedCAs); #ifdef WOLFSSL_NONBLOCK_OCSP /* The CRL lookup I/O callback is using the * same WOULD_BLOCK error code as OCSP's I/O @@ -46675,7 +46677,7 @@ static int AddPSKtoPreMasterSecret(WOLFSSL* ssl) #ifdef HAVE_CRL if (ret == 0 && SSL_CM(ssl)->crlEnabled) { ret = CheckCertCRLFromCm(SSL_CM(ssl), SSL_CM(ssl)->crl, - dCert); + dCert, ssl->waivedCAs); } #endif if (ret == 0) { diff --git a/src/ssl_certman.c b/src/ssl_certman.c index 0946251320b..96b533094a3 100644 --- a/src/ssl_certman.c +++ b/src/ssl_certman.c @@ -888,6 +888,7 @@ static int cm_verify_err_overridable(int err) * @param [in] format Format of encoding. Valid values: * WOLFSSL_FILETYPE_ASN1, WOLFSSL_FILETYPE_PEM. * @param [in] prev_err Previous error. Passed to callback. + * @param [in] extraCa Signers to search besides the manager's. May be NULL. * @param [out] crlErr CRL result, not merged into the return value. May be * NULL. * @return WOLFSSL_SUCCESS on success. @@ -896,7 +897,7 @@ static int cm_verify_err_overridable(int err) * the wolfSSL build. */ int CM_VerifyBuffer_ex(WOLFSSL_CERT_MANAGER* cm, const unsigned char* buff, - long sz, int format, int prev_err, int* crlErr) + Signer* extraCa, long sz, int format, int prev_err, int* crlErr) { int ret = 0; int crlRet = 0; @@ -954,14 +955,14 @@ int CM_VerifyBuffer_ex(WOLFSSL_CERT_MANAGER* cm, const unsigned char* buff, /* Parse DER into decoded certificate fields and verify signature * against a known CA. */ - ret = ParseCertRelative(cert, CERT_TYPE, VERIFY, cm, NULL); + ret = ParseCertRelative(cert, CERT_TYPE, VERIFY, cm, extraCa); } #ifdef HAVE_CRL if (cm->crlEnabled && ((ret == 0) || (ret == WC_NO_ERR_TRACE(ASN_BEFORE_DATE_E)) || (ret == WC_NO_ERR_TRACE(ASN_AFTER_DATE_E)))) { - crlRet = CheckCertCRLFromCm(cm, cm->crl, cert); + crlRet = CheckCertCRLFromCm(cm, cm->crl, cert, extraCa); if (crlRet != 0) { WOLFSSL_MSG("CheckCertCRL failed"); } @@ -1097,7 +1098,7 @@ int wolfSSL_CertManagerVerifyBuffer(WOLFSSL_CERT_MANAGER* cm, } else { /* No previous error. */ - ret = CM_VerifyBuffer_ex(cm, buff, sz, format, 0, NULL); + ret = CM_VerifyBuffer_ex(cm, buff, NULL, sz, format, 0, NULL); } return ret; @@ -2145,7 +2146,7 @@ int wolfSSL_CertManagerCheckCRL(WOLFSSL_CERT_MANAGER* cm, /* A date error is returned only after the parse completed, * so still answer the revocation question that was asked. */ ret = cm_crl_merge(ret, - CheckCertCRLFromCm(cm, cm->crl, cert)); + CheckCertCRLFromCm(cm, cm->crl, cert, NULL)); } /* Dispose of dynamically allocated memory. */ diff --git a/src/ssl_load.c b/src/ssl_load.c index 75d624b4fc3..46c06aa699d 100644 --- a/src/ssl_load.c +++ b/src/ssl_load.c @@ -2586,7 +2586,8 @@ static int ProcessBufferCertTypes(WOLFSSL_CTX* ctx, WOLFSSL* ssl, if ((ctx != NULL) && (ctx->cm != NULL) && (ctx->cm->verifyCallback != NULL)) { /* Verify and use callback. */ - ret = CM_VerifyBuffer_ex(ctx->cm, buff, sz, format, ret, NULL); + ret = CM_VerifyBuffer_ex(ctx->cm, buff, NULL, sz, format, ret, + NULL); /* Convert error. */ if (ret == 0) { ret = WOLFSSL_FATAL_ERROR; diff --git a/src/x509_str.c b/src/x509_str.c index 5b9c910a4a6..d15a3cb627a 100644 --- a/src/x509_str.c +++ b/src/x509_str.c @@ -256,6 +256,7 @@ int wolfSSL_X509_STORE_CTX_init(WOLFSSL_X509_STORE_CTX* ctx, ctx->crls = NULL; #endif ctx->peerCertDepth = 0; + ctx->extraCa = NULL; if (ctx->chain != NULL) { wolfSSL_sk_X509_pop_free(ctx->chain, NULL); ctx->chain = NULL; @@ -563,7 +564,8 @@ static int X509StoreCheckCtxCrls(WOLFSSL_X509_STORE_CTX* ctx) continue; /* Use the store's cm to verify the CRL. The caller-owned crl is * not modified. */ - ret = CheckCertCRLFromCm(ctx->store->cm, crl, cert); + ret = CheckCertCRLFromCm(ctx->store->cm, crl, cert, + ctx->extraCa); if (ret == 0) found = 1; else if (ret == WC_NO_ERR_TRACE(CRL_CERT_DATE_ERR)) @@ -636,7 +638,7 @@ static int X509StoreVerifyCert(WOLFSSL_X509_STORE_CTX* ctx, int* cbRejected, if (ctx->current_cert != NULL && ctx->current_cert->derCert != NULL) { ret = CM_VerifyBuffer_ex(ctx->store->cm, - ctx->current_cert->derCert->buffer, + ctx->current_cert->derCert->buffer, ctx->extraCa, ctx->current_cert->derCert->length, WOLFSSL_FILETYPE_ASN1, 0, &crlRet); datesChecked = (ret == WOLFSSL_SUCCESS) || diff --git a/tests/api/test_ossl_x509_str.c b/tests/api/test_ossl_x509_str.c index 380569617ed..4b06419c5b3 100644 --- a/tests/api/test_ossl_x509_str.c +++ b/tests/api/test_ossl_x509_str.c @@ -4786,6 +4786,7 @@ static STACK_OF(X509_CRL)* test_set0_crls_stack; static int test_set0_crls_preverify; /* preverify_ok seen at depth 0 */ static int test_set0_crls_error; /* error seen when preverify_ok == 0 */ static int test_set0_crls_reject_peer; /* verify cb rejects the peer cert */ +static int test_set0_crls_chain_local; /* chain CAs on the connection's list */ /* Mimics OpenVPN's cert_verify_callback. */ static int test_set0_crls_cert_verify_cb(X509_STORE_CTX* ctx, void* arg) @@ -4800,6 +4801,7 @@ static int test_set0_crls_verify_cb(int preverify_ok, X509_STORE_CTX* ctx) { if (X509_STORE_CTX_get_error_depth(ctx) == 0) { test_set0_crls_preverify = preverify_ok; + test_set0_crls_chain_local = (ctx->extraCa != NULL); if (test_set0_crls_reject_peer) return 0; } @@ -4823,13 +4825,13 @@ static int test_set0_crls_ctx_ready(WOLFSSL_CTX* ctx) return EXPECT_RESULT(); } -static int test_set0_crls_chain_cached; /* chain CAs kept in the store */ +static int test_set0_crls_chain_cached; /* chain CAs left in the store */ static int test_set0_crls_ctx_cleanup(WOLFSSL_CTX* ctx) { WOLFSSL_CERT_MANAGER* cm = wolfSSL_CTX_GetCertManager(ctx); - /* The peer cert only verifies when the chain CAs were kept. */ + /* A chain CA left in the store would let the peer cert verify on its own. */ (void)wolfSSL_CertManagerDisableCRL(cm); test_set0_crls_chain_cached = (wolfSSL_CertManagerVerify(cm, "./certs/intermediate/server-int-cert.pem", WOLFSSL_FILETYPE_PEM) == @@ -4904,7 +4906,8 @@ int test_wolfSSL_X509_STORE_CTX_set0_crls_handshake(void) test_set0_crls_stack = NULL; /* The server presents intermediates. Their CRLs are only in the stack. The - * handshake succeeds and the chain CAs are kept. */ + * handshake succeeds, and the chain CAs are kept for this connection only: + * the callback cleared the error wolfSSL reported for them. */ XMEMSET(&client_cbs, 0, sizeof(client_cbs)); XMEMSET(&server_cbs, 0, sizeof(server_cbs)); client_cbs.ctx_ready = test_set0_crls_ctx_ready; @@ -4920,10 +4923,12 @@ int test_wolfSSL_X509_STORE_CTX_set0_crls_handshake(void) test_set0_crls_preverify = -1; test_set0_crls_error = 0; test_set0_crls_chain_cached = 0; + test_set0_crls_chain_local = 0; ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs, &server_cbs, NULL), TEST_SUCCESS); ExpectIntEQ(test_set0_crls_preverify, 1); - ExpectIntEQ(test_set0_crls_chain_cached, 1); + ExpectIntEQ(test_set0_crls_chain_local, 1); + ExpectIntEQ(test_set0_crls_chain_cached, 0); sk_X509_CRL_pop_free(test_set0_crls_stack, X509_CRL_free); test_set0_crls_stack = NULL; diff --git a/wolfssl/crl.h b/wolfssl/crl.h index 0a1c72d7d5e..5df2df1c4f9 100644 --- a/wolfssl/crl.h +++ b/wolfssl/crl.h @@ -44,12 +44,13 @@ WOLFSSL_LOCAL int BufferLoadCRL(WOLFSSL_CRL* crl, const byte* buff, long sz, int type, int verify); WOLFSSL_LOCAL int BufferStoreCRL(WOLFSSL_CRL* crl, byte* buff, long* inOutSz, int type); -WOLFSSL_LOCAL int CheckCertCRL(WOLFSSL_CRL* crl, DecodedCert* cert); +WOLFSSL_LOCAL int CheckCertCRL(WOLFSSL_CRL* crl, DecodedCert* cert, + Signer* extraCa); WOLFSSL_LOCAL int CheckCertCRL_ex(WOLFSSL_CRL* crl, byte* issuerHash, byte* serial, int serialSz, byte* serialHash, const byte* extCrlInfo, - int extCrlInfoSz, void* issuerName); + int extCrlInfoSz, void* issuerName, Signer* extraCa); WOLFSSL_LOCAL int CheckCertCRLFromCm(WOLFSSL_CERT_MANAGER* cm, - WOLFSSL_CRL* crl, DecodedCert* cert); + WOLFSSL_CRL* crl, DecodedCert* cert, Signer* extraCa); #ifdef HAVE_CRL_UPDATE_CB WOLFSSL_LOCAL int GetCRLInfo(WOLFSSL_CRL* crl, CrlInfo* info, const byte* buff, long sz, int type); diff --git a/wolfssl/internal.h b/wolfssl/internal.h index 06c538dc2f0..b06b84f8498 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -3005,8 +3005,8 @@ WOLFSSL_LOCAL int CM_MemRestoreCertCache(WOLFSSL_CERT_MANAGER* cm, const void* mem, int sz); WOLFSSL_LOCAL int CM_GetCertCacheMemSize(WOLFSSL_CERT_MANAGER* cm); WOLFSSL_LOCAL int CM_VerifyBuffer_ex(WOLFSSL_CERT_MANAGER* cm, const byte* buff, - long sz, int format, int prev_err, - int* crlErr); + Signer* extraCa, long sz, int format, + int prev_err, int* crlErr); #ifndef NO_CERTS diff --git a/wolfssl/ssl.h b/wolfssl/ssl.h index c02b92e374f..ce9b19996f9 100644 --- a/wolfssl/ssl.h +++ b/wolfssl/ssl.h @@ -741,6 +741,9 @@ struct WOLFSSL_X509_STORE_CTX { #endif int peerCertDepth; /* depth of current_cert in the peer's chain, * set by the handshake */ + struct Signer* extraCa; /* chain CAs the connection's verify callback + * accepted, set by the handshake. Not owned + * by this ctx. */ #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */ }; From d58d991494a02dc162f833b95a469ec053326ae2 Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Tue, 17 Nov 2026 20:28:47 +0100 Subject: [PATCH 13/20] Don't cache OCSP responses signed by a CA waived by cert callback If an OCSP response is signed by a CA waived by the certificate callback, and then it is added to the ctx's certificate manager, it will be trusted by other connections too, but these connections don't trust the signer. So, don't add that OCSP response to the cert manager in this case. --- src/ocsp.c | 11 +++++++++++ wolfcrypt/src/asn.c | 27 +++++++++++++++++++++++++-- wolfssl/wolfcrypt/asn.h | 1 + 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/ocsp.c b/src/ocsp.c index 5d176177261..053a1923e99 100644 --- a/src/ocsp.c +++ b/src/ocsp.c @@ -421,6 +421,17 @@ int CheckOcspResponse(WOLFSSL_OCSP *ocsp, byte *response, int responseSz, validated = 1; } +#ifndef NO_CERTS + /* A status reached through a signer only this connection trusts must not + * get into the manager-wide cache, otherwise later connections reuse it + * without trusting the signer. */ + if (ocspResponse->pendingSigner) { + WOLFSSL_MSG("OCSP status not cached, signer is connection local"); + status = NULL; + entry = NULL; + } +#endif + if (wc_LockMutex(&ocsp->ocspLock) != 0) { ret = BAD_MUTEX_E; goto end; diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index baa26453131..f2c2c180cdd 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -38192,21 +38192,39 @@ static Signer *OcspFindSigner(OcspResponse *resp, WOLFSSL_CERT_MANAGER *cm) if (resp->responderIdType == OCSP_RESPONDER_ID_NAME) { s = findSignerByName(resp->pendingCAs, resp->responderId.nameHash); - if (s) + if (s) { + resp->pendingSigner = 1; return s; + } } else if (KEYID_SIZE >= OCSP_RESPONDER_ID_KEY_SZ) { byte keyHash[KEYID_SIZE]; XMEMSET(keyHash, 0, KEYID_SIZE); XMEMCPY(keyHash, resp->responderId.keyHash, OCSP_RESPONDER_ID_KEY_SZ); s = findSignerByKeyHash(resp->pendingCAs, keyHash); - if (s) + if (s) { + resp->pendingSigner = 1; return s; + } } #endif return NULL; } +#ifndef NO_CERTS +static int OcspSignerInList(const Signer* list, const Signer* signer) +{ + const Signer* s; + + for (s = list; s != NULL; s = s->next) { + if (s == signer) + return 1; + } + + return 0; +} +#endif + static int OcspCheckCert(OcspResponse *resp, int noVerify, int noVerifySignature, WOLFSSL_CERT_MANAGER *cm, void *heap) { @@ -38231,6 +38249,11 @@ static int OcspCheckCert(OcspResponse *resp, int noVerify, if (ret < 0) { WOLFSSL_MSG("\tOCSP Responder certificate parsing failed"); } +#ifndef NO_CERTS + if (ret == 0 && OcspSignerInList(resp->pendingCAs, cert->ca)) { + resp->pendingSigner = 1; + } +#endif if (ret == 0 && OcspRespIdMatch(resp, diff --git a/wolfssl/wolfcrypt/asn.h b/wolfssl/wolfcrypt/asn.h index 97a0bd37469..dc36a4f587d 100644 --- a/wolfssl/wolfcrypt/asn.h +++ b/wolfssl/wolfcrypt/asn.h @@ -3185,6 +3185,7 @@ struct OcspResponse { byte* source; /* pointer to source buffer, not owned */ word32 maxIdx; /* max offset based on init size */ Signer* pendingCAs; + byte pendingSigner; /* verified through pendingCAs, not through cm */ void* heap; }; From e930d7ce5a71b3ad278048b02a38fa0dee0fecf5 Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Tue, 17 Nov 2026 20:52:19 +0100 Subject: [PATCH 14/20] Reset the waived CA list for each Certificate message This is needed so that renegotiation and post-handshake auth don't inherit signers from the previous Certificate message. Also prevents duplicate waived CAs by checking before adding. --- src/internal.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/internal.c b/src/internal.c index 04e4727ac35..0d6570e59fb 100644 --- a/src/internal.c +++ b/src/internal.c @@ -18006,6 +18006,26 @@ static int ProcessPeerCertMakeChainSigner(WOLFSSL* ssl, buffer* cert, int type, return ret; } +/* Is a CA with the same name and key already on the connection's list? */ +static int ProcessPeerCertIsDupWaivedCA(const WOLFSSL* ssl, const Signer* s) +{ + const Signer* o; + + if (s->publicKey == NULL) + return 0; + + for (o = ssl->waivedCAs; o != NULL; o = o->next) { + if ((o->publicKey != NULL) && (o->pubKeySize == s->pubKeySize) && + (XMEMCMP(o->subjectNameHash, s->subjectNameHash, + SIGNER_DIGEST_SIZE) == 0) && + (XMEMCMP(o->publicKey, s->publicKey, s->pubKeySize) == 0)) { + return 1; + } + } + + return 0; +} + /* Add a chain CA the verify callback accepted to the connection's list. */ static int ProcessPeerCertAddWaivedCA(WOLFSSL* ssl, buffer* cert) { @@ -18015,8 +18035,13 @@ static int ProcessPeerCertAddWaivedCA(WOLFSSL* ssl, buffer* cert) ret = ProcessPeerCertMakeChainSigner(ssl, cert, WOLFSSL_TEMP_CA, ssl->waivedCAs, &s); if (ret == 0 && s != NULL) { - s->next = ssl->waivedCAs; - ssl->waivedCAs = s; + if (ProcessPeerCertIsDupWaivedCA(ssl, s)) { + FreeSigner(s, SSL_CM(ssl)->heap); + } + else { + s->next = ssl->waivedCAs; + ssl->waivedCAs = s; + } } return ret; @@ -19940,6 +19965,9 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, XMEMSET(args, 0, sizeof(ProcPeerCertArgs)); args->idx = *inOutIdx; args->begin = *inOutIdx; + /* A new certificate list; the previous one's CAs are not signers for + * it. */ + SslWaivedCAsFree(ssl); #if defined(HAVE_CRL) && (defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \ defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)) /* A new certificate list; never apply the previous one's verdicts. */ From 4e49be4b86fbb896a74ca34ee5f831f382372777 Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Tue, 17 Nov 2026 21:57:52 +0100 Subject: [PATCH 15/20] Search the cert manager before the waived CA list Move the extra signer (waived CA) lookup in ParseCertRelative after every cert manager lookup, so a trusted CA has priority over an untrusted one that was accepted by the verify callback. --- wolfcrypt/src/asn.c | 49 +++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index f2c2c180cdd..52fa8127d80 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -26556,30 +26556,6 @@ int ParseCertRelative(DecodedCert* cert, int type, int verify, if (!cert->selfIssued || (verify != NO_VERIFY && type != CA_TYPE && type != TRUSTED_PEER_TYPE)) { cert->ca = NULL; - if (extraCAList != NULL) { - #ifndef NO_SKID - if (cert->extAuthKeyIdSet) { - Signer* exCa; - - for (exCa = extraCAList; exCa != NULL; exCa = exCa->next) { - if (XMEMCMP(exCa->subjectKeyIdHash, - cert->extAuthKeyId, - SIGNER_DIGEST_SIZE) == 0 && - XMEMCMP(exCa->subjectNameHash, cert->issuerHash, - SIGNER_DIGEST_SIZE) == 0) { - cert->ca = exCa; - break; - } - } - /* AKID is authoritative; do not fall back to name. */ - } - else - #endif - { - cert->ca = findSignerByName(extraCAList, - cert->issuerHash); - } - } #ifndef NO_SKID if (cert->ca == NULL && cert->extAuthKeyIdSet) { cert->ca = GetCA(cm, cert->extAuthKeyId); @@ -26639,6 +26615,31 @@ int ParseCertRelative(DecodedCert* cert, int type, int verify, cert->ca = GetCA(cm, cert->issuerHash); #endif /* !NO_SKID */ + if (cert->ca == NULL && extraCAList != NULL) { + #ifndef NO_SKID + if (cert->extAuthKeyIdSet) { + Signer* exCa; + + for (exCa = extraCAList; exCa != NULL; exCa = exCa->next) { + if (XMEMCMP(exCa->subjectKeyIdHash, + cert->extAuthKeyId, + SIGNER_DIGEST_SIZE) == 0 && + XMEMCMP(exCa->subjectNameHash, cert->issuerHash, + SIGNER_DIGEST_SIZE) == 0) { + cert->ca = exCa; + break; + } + } + /* AKID is authoritative; do not fall back to name. */ + } + else + #endif + { + cert->ca = findSignerByName(extraCAList, + cert->issuerHash); + } + } + if (cert->ca) { WOLFSSL_MSG("CA found"); } From 6e052c2f0619afc7e02f94b15291791d0f1aef98 Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Tue, 17 Nov 2026 22:06:45 +0100 Subject: [PATCH 16/20] Set the word32 unknown extension callback on the chain signer parse Install the certificate manager's word32 callback alongside the word16 one, as AddCA() does, so an intermediate whose critical extension only the word32 callback accepts no longer fails the handshake. --- src/internal.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/internal.c b/src/internal.c index 0d6570e59fb..369953dbde7 100644 --- a/src/internal.c +++ b/src/internal.c @@ -17950,6 +17950,11 @@ static int ProcessPeerCertMakeChainSigner(WOLFSSL* ssl, buffer* cert, int type, dCertAdd->noX509Name = 1; #endif #ifdef WC_ASN_UNKNOWN_EXT_CB + /* Parse it like AddCA() did. */ + if (SSL_CM(ssl)->unknownExtCallback32 != NULL) { + wc_SetUnknownExtCallback32(dCertAdd, + SSL_CM(ssl)->unknownExtCallback32); + } if (SSL_CM(ssl)->unknownExtCallback != NULL) wc_SetUnknownExtCallback(dCertAdd, SSL_CM(ssl)->unknownExtCallback); #endif From bfd5e479cc009885823085e35e145e5a76fe5630 Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Tue, 17 Nov 2026 22:56:24 +0100 Subject: [PATCH 17/20] Make the chain CA tests prove the connection's CA list is used - Waive only the missing trust anchor, so the handshake succeeds only when the chain CAs act as signers; - Check that the CA the callback waived is not usable as a signer afterwards either; - Add a test case for wolfSSL_clear() that verifies that a reused WOLFSSL object doesn't trust the previous peer's CAs; - Add a test case for a stapled OCSP response whose responder is one of those CAs. --- tests/api.c | 114 ++++++++++++++++++++++++++++++++++++++++-- tests/api/test_ocsp.c | 110 ++++++++++++++++++++++++++++++++++++++++ tests/api/test_ocsp.h | 1 + 3 files changed, 222 insertions(+), 3 deletions(-) diff --git a/tests/api.c b/tests/api.c index 6b416400007..5a7a4d6169a 100644 --- a/tests/api.c +++ b/tests/api.c @@ -42982,11 +42982,32 @@ static int test_override_alt_cert_chain(void) #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \ defined(WOLFSSL_PEM_TO_DER) && !defined(WOLFSSL_ALT_CERT_CHAINS) +/* Check if err is "no signer", also accounting for OPENSSL_EXTRA */ +static int test_chain_ca_no_persist_is_no_signer(int err) +{ + if (err == WC_NO_ERR_TRACE(ASN_NO_SIGNER_E)) + return 1; +#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) + if (err == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY) + return 1; +#endif + return 0; +} + +static int test_chain_ca_no_persist_waived; /* CAs the callback let through */ + static int test_chain_ca_no_persist_verify_cb(int preverify, WOLFSSL_X509_STORE_CTX* store) { - (void)preverify; - (void)store; + if (preverify) + return 1; + /* The peer cert has to verify through the CAs above it, not via callback, + * so the handshake only succeeds when those CAs are usable as signers. */ + if (store->error_depth == 0) + return 0; + if (!test_chain_ca_no_persist_is_no_signer(store->error)) + return 0; + test_chain_ca_no_persist_waived++; return 1; } @@ -43018,7 +43039,13 @@ static int test_chain_ca_no_persist_result(WOLFSSL* ssl) WOLFSSL_CERT_MANAGER* cm = NULL; ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(wolfSSL_get_SSL_CTX(ssl))); - /* The CA trusted via callback must not be usable as a signer afterwards */ + /* Exactly one CA needed waiving: the one whose issuer is not loaded */ + ExpectIntEQ(test_chain_ca_no_persist_waived, 1); + /* The CA the callback let through must not be usable as a signer */ + ExpectIntNE(wolfSSL_CertManagerVerify(cm, + "./certs/intermediate/ca-int-cert.pem", WOLFSSL_FILETYPE_PEM), + WOLFSSL_SUCCESS); + /* Nor the one below it, which only verified because of that */ ExpectIntNE(wolfSSL_CertManagerVerify(cm, "./certs/intermediate/ca-int2-cert.pem", WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS); @@ -43039,6 +43066,7 @@ static int test_chain_ca_no_persist(void) XMEMSET(&client_cbs, 0, sizeof(client_cbs)); XMEMSET(&server_cbs, 0, sizeof(server_cbs)); + test_chain_ca_no_persist_waived = 0; client_cbs.ctx_ready = test_chain_ca_no_persist_client_ctx; client_cbs.on_result = test_chain_ca_no_persist_result; server_cbs.ctx_ready = test_chain_ca_no_persist_server_ctx; @@ -43048,11 +43076,89 @@ static int test_chain_ca_no_persist(void) return EXPECT_RESULT(); } + +/* The CAs a verify callback let through belong to one connection. After + * wolfSSL_clear() a reused object must not still treat them as signers, so a + * peer that sends only its leaf no longer verifies. */ +static int test_chain_ca_no_persist_clear(void) +{ + EXPECT_DECLS; + struct test_memio_ctx test_ctx; + WOLFSSL_CTX* ctx_c = NULL; + WOLFSSL_CTX* ctx_s = NULL; + WOLFSSL_CTX* ctx_s2 = NULL; + WOLFSSL* ssl_c = NULL; + WOLFSSL* ssl_s = NULL; + WOLFSSL* ssl_s2 = NULL; + + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + test_chain_ca_no_persist_waived = 0; + + /* The client trusts nothing, so the top of the chain needs waiving. */ + ExpectNotNull(ctx_c = wolfSSL_CTX_new(wolfTLSv1_2_client_method())); + ExpectIntEQ(wolfSSL_CertManagerUnloadCAs( + wolfSSL_CTX_GetCertManager(ctx_c)), WOLFSSL_SUCCESS); + wolfSSL_CTX_set_verify(ctx_c, WOLFSSL_VERIFY_PEER, + test_chain_ca_no_persist_verify_cb); + wolfSSL_SetIORecv(ctx_c, test_memio_read_cb); + wolfSSL_SetIOSend(ctx_c, test_memio_write_cb); + + /* First peer sends the whole chain. */ + ExpectNotNull(ctx_s = wolfSSL_CTX_new(wolfTLSv1_2_server_method())); + ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile, + WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx_s, + "./certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS); + wolfSSL_SetIORecv(ctx_s, test_memio_read_cb); + wolfSSL_SetIOSend(ctx_s, test_memio_write_cb); + + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, + wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0); + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + ExpectIntEQ(test_chain_ca_no_persist_waived, 1); + + /* Second peer sends its leaf only, so it verifies only if the CAs from + * the first handshake are still signers. */ + ExpectNotNull(ctx_s2 = wolfSSL_CTX_new(wolfTLSv1_2_server_method())); + ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s2, svrKeyFile, + WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s2, + "./certs/intermediate/server-int-cert.pem", WOLFSSL_FILETYPE_PEM), + WOLFSSL_SUCCESS); + wolfSSL_SetIORecv(ctx_s2, test_memio_read_cb); + wolfSSL_SetIOSend(ctx_s2, test_memio_write_cb); + ExpectNotNull(ssl_s2 = wolfSSL_new(ctx_s2)); + wolfSSL_SetIOReadCtx(ssl_s2, &test_ctx); + wolfSSL_SetIOWriteCtx(ssl_s2, &test_ctx); + + ExpectIntEQ(wolfSSL_clear(ssl_c), WOLFSSL_SUCCESS); + wolfSSL_SetIOReadCtx(ssl_c, &test_ctx); + wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx); + test_memio_clear_buffer(&test_ctx, 0); + test_memio_clear_buffer(&test_ctx, 1); + + ExpectIntNE(test_memio_do_handshake(ssl_c, ssl_s2, 10, NULL), 0); + /* The leaf was refused on its own, not forgiven at depth 0. */ + ExpectIntEQ(test_chain_ca_no_persist_waived, 1); + + wolfSSL_free(ssl_s2); + wolfSSL_free(ssl_s); + wolfSSL_free(ssl_c); + wolfSSL_CTX_free(ctx_s2); + wolfSSL_CTX_free(ctx_s); + wolfSSL_CTX_free(ctx_c); + + return EXPECT_RESULT(); +} #else static int test_chain_ca_no_persist(void) { return TEST_SKIPPED; } +static int test_chain_ca_no_persist_clear(void) +{ + return TEST_SKIPPED; +} #endif static int test_rpk_set_xxx_cert_type(void) @@ -49068,6 +49174,7 @@ TEST_CASE testCases[] = { TEST_DECL(test_harden_no_secure_renegotiation), TEST_DECL(test_override_alt_cert_chain), TEST_DECL(test_chain_ca_no_persist), + TEST_DECL(test_chain_ca_no_persist_clear), TEST_DECL(test_rpk_set_xxx_cert_type), TEST_DECL(test_short_session_id), /* Can't memory test as client/server hangs. */ @@ -49156,6 +49263,7 @@ TEST_CASE testCases[] = { TEST_DECL_GROUP("ocsp", test_dtls13_nonblock_ocsp_unfragmented_cert), TEST_DECL_GROUP("ocsp", test_ocsp_status_verify_cb_would_block), TEST_DECL_GROUP("ocsp", test_dtls12_nonblock_ocsp), + TEST_DECL_GROUP("ocsp", test_ocsp_staple_signed_by_waived_ca), TEST_DECL_GROUP("ocsp", test_ocsp_ctx_request_cache), TEST_DECL_GROUP("ocsp", test_ocsp_ctx_request_cache_cert_change), TEST_DECL_GROUP("ocsp", test_ocsp_ctx_request_cache_drop_in_use), diff --git a/tests/api/test_ocsp.c b/tests/api/test_ocsp.c index e1602813774..171c848bc96 100644 --- a/tests/api/test_ocsp.c +++ b/tests/api/test_ocsp.c @@ -6473,6 +6473,112 @@ static int test_ocsp_ctx_request_rotate_cert(WOLFSSL_CTX* ctx, return EXPECT_RESULT(); } +/* A stapled OCSP response signed by a CA that only this connection holds, + * because the verify callback let it through. The responder is not in the + * certificate manager, so the staple verifies only when the connection's own + * CA list is searched too. */ +static int test_ocsp_staple_waived_ca_waived; + +static int test_ocsp_staple_waived_ca_verify_cb(int preverify, + WOLFSSL_X509_STORE_CTX* store) +{ + if (preverify) + return 1; + /* The peer cert must verify through the CA above it, not by waiver. */ + if (store->error_depth == 0) + return 0; + if (store->error != WC_NO_ERR_TRACE(ASN_NO_SIGNER_E) +#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) + && store->error != + WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY +#endif + ) { + return 0; + } + test_ocsp_staple_waived_ca_waived++; + return 1; +} + +static int test_ocsp_staple_waived_ca_io_cb(void* ioCtx, const char* url, + int urlSz, unsigned char* req, int reqSz, unsigned char** respBuf) +{ + (void)ioCtx; + (void)url; + (void)urlSz; + (void)req; + (void)reqSz; + + /* Signed by the intermediate, which is the CA the callback let through. */ + *respBuf = (unsigned char*)resp_server1_cert; + return (int)sizeof(resp_server1_cert); +} + +int test_ocsp_staple_signed_by_waived_ca(void) +{ + EXPECT_DECLS; + WOLFSSL_CTX* ctx_c = NULL; + WOLFSSL_CTX* ctx_s = NULL; + WOLFSSL* ssl_c = NULL; + WOLFSSL* ssl_s = NULL; + struct test_memio_ctx test_ctx; + + test_ocsp_staple_waived_ca_waived = 0; + + /* The certificate has to be on the CTX before any WOLFSSL is made from + * it, so build the client and server CTXs first. */ + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, NULL, NULL, + wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0); + + ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx_s, + "./certs/ocsp/server1-chain-noroot.pem"), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, + "./certs/ocsp/server1-key.pem", WOLFSSL_FILETYPE_PEM), + WOLFSSL_SUCCESS); + /* The server verifies the response before stapling it. */ + ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_s, + "./certs/ocsp/root-ca-cert.pem", NULL), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_s, + "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS); + + /* The client trusts nothing, so the intermediate that signed both the + * peer certificate and the stapled response is reachable only through + * the connection's waived CA list. */ + ExpectIntEQ(wolfSSL_CertManagerUnloadCAs( + wolfSSL_CTX_GetCertManager(ctx_c)), WOLFSSL_SUCCESS); + wolfSSL_CTX_set_verify(ctx_c, WOLFSSL_VERIFY_PEER, + test_ocsp_staple_waived_ca_verify_cb); + + ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx_c), WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx_s), WOLFSSL_SUCCESS); + /* The test certificate carries no AuthInfo, so point the lookup at a + * dummy responder to get past the no-URL check. */ + ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx_s, "http://dummy.test"), + WOLFSSL_SUCCESS); + ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx_s, + WOLFSSL_OCSP_NO_NONCE | WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS); + /* NULL free callback: the response points at a static array. */ + ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx_s, + test_ocsp_staple_waived_ca_io_cb, NULL, NULL), WOLFSSL_SUCCESS); + + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s, + wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0); + ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl_c, WOLFSSL_CSR_OCSP, 0), + WOLFSSL_SUCCESS); + + ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0); + /* Only the intermediate needed waiving. */ + ExpectIntEQ(test_ocsp_staple_waived_ca_waived, 1); + + wolfSSL_free(ssl_c); + wolfSSL_free(ssl_s); + wolfSSL_CTX_free(ctx_c); + wolfSSL_CTX_free(ctx_s); + + return EXPECT_RESULT(); +} + /* The cached leaf request is built from the CTX certificate, so it has to go * when that certificate is replaced. Otherwise every later connection asks the * responder about, and staples the status of, the certificate the CTX used to @@ -6696,6 +6802,10 @@ int test_ocsp_ctx_request_cache_drop_in_use(void) return EXPECT_RESULT(); } #else +int test_ocsp_staple_signed_by_waived_ca(void) +{ + return TEST_SKIPPED; +} int test_ocsp_ctx_request_cache(void) { return TEST_SKIPPED; diff --git a/tests/api/test_ocsp.h b/tests/api/test_ocsp.h index 7226a216d58..b9a0df126ab 100644 --- a/tests/api/test_ocsp.h +++ b/tests/api/test_ocsp.h @@ -68,6 +68,7 @@ int test_tls13_nonblock_ocsp_low_mfl(void); int test_dtls13_nonblock_ocsp_unfragmented_cert(void); int test_ocsp_status_verify_cb_would_block(void); int test_dtls12_nonblock_ocsp(void); +int test_ocsp_staple_signed_by_waived_ca(void); int test_ocsp_ctx_request_cache(void); int test_ocsp_ctx_request_cache_cert_change(void); int test_ocsp_ctx_request_cache_drop_in_use(void); From b70240c9ac4d35811749703e6393b7945b7c9699 Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Tue, 17 Nov 2026 23:25:08 +0100 Subject: [PATCH 18/20] Resolve a chain signer's issuer in both pools Look up the issuer of a signer being built in the CSRv2 pending pool too (not just in the connection's waived CA list), so the path length imposed by the issuer isn't lost when it's only in the pending pool. --- src/internal.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/internal.c b/src/internal.c index 369953dbde7..059138c2505 100644 --- a/src/internal.c +++ b/src/internal.c @@ -17927,13 +17927,15 @@ void DoCrlCallback(WOLFSSL_CERT_MANAGER* cm, WOLFSSL* ssl, #ifndef NO_ASN /* Parse a chain certificate as a CA and return it as a filled signer. */ static int ProcessPeerCertMakeChainSigner(WOLFSSL* ssl, buffer* cert, int type, - Signer* extraCa, Signer** out) + Signer** out) { int ret = 0; WC_DECLARE_VAR(dCertAdd, DecodedCert, 1, 0); int dCertAdd_inited = 0; DerBuffer* derBuffer = NULL; Signer* s = NULL; + Signer* extraCa = NULL; + Signer* waivedTail = NULL; *out = NULL; @@ -17958,7 +17960,15 @@ static int ProcessPeerCertMakeChainSigner(WOLFSSL* ssl, buffer* cert, int type, if (SSL_CM(ssl)->unknownExtCallback != NULL) wc_SetUnknownExtCallback(dCertAdd, SSL_CM(ssl)->unknownExtCallback); #endif + /* The issuer can be in either pool, and only it carries the path length + * this signer has to respect. */ +#if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && !defined(NO_TLS) + if (TLSX_CSR2_IsMulti(ssl->extensions)) + extraCa = TLSX_CSR2_GetPendingSigners(ssl->extensions); +#endif + extraCa = SslWaivedCAsJoin(ssl, extraCa, &waivedTail); ret = ParseCertEx(dCertAdd, CA_TYPE, NO_VERIFY, SSL_CM(ssl), extraCa); + SslWaivedCAsSplit(waivedTail); if (ret != 0) goto exit_mcs; @@ -18037,8 +18047,7 @@ static int ProcessPeerCertAddWaivedCA(WOLFSSL* ssl, buffer* cert) Signer* s = NULL; int ret; - ret = ProcessPeerCertMakeChainSigner(ssl, cert, WOLFSSL_TEMP_CA, - ssl->waivedCAs, &s); + ret = ProcessPeerCertMakeChainSigner(ssl, cert, WOLFSSL_TEMP_CA, &s); if (ret == 0 && s != NULL) { if (ProcessPeerCertIsDupWaivedCA(ssl, s)) { FreeSigner(s, SSL_CM(ssl)->heap); @@ -18993,8 +19002,7 @@ static int ProcessPeerCertAddPendingCA(WOLFSSL* ssl, buffer* cert) * promotes this signer into the certificate manager, and the unload path * (wolfSSL_CertManagerUnloadIntermediateCerts()) selects entries by that * type. AddCA() records chain CAs the same way. */ - ret = ProcessPeerCertMakeChainSigner(ssl, cert, WOLFSSL_CHAIN_CA, - NULL, &s); + ret = ProcessPeerCertMakeChainSigner(ssl, cert, WOLFSSL_CHAIN_CA, &s); if (ret == 0 && s != NULL) { ret = TLSX_CSR2_AddPendingSigner(ssl->extensions, s); if (ret != 0) From e3e0145cd341f923509accfc2d9d895ccb69d21f Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Tue, 17 Nov 2026 23:32:59 +0100 Subject: [PATCH 19/20] Don't copy a CA into waived CA list if already in cert manager --- src/internal.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/internal.c b/src/internal.c index 059138c2505..d5b23e80e6e 100644 --- a/src/internal.c +++ b/src/internal.c @@ -20787,10 +20787,10 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, cert->buffer, cert->length); } #endif /* SESSION_CERTS && WOLFSSL_ALT_CERT_CHAINS */ - if (caIsTemp) { + if (caIsTemp && !alreadySigner) { ret = ProcessPeerCertAddWaivedCA(ssl, cert); } - else if (!alreadySigner) { + else if (!caIsTemp && !alreadySigner) { DerBuffer* add = NULL; ret = AllocDer(&add, cert->length, CA_TYPE, ssl->heap); if (ret < 0) From 63bb22e98178c9e444a9aab3a6f51e0c5dd10dc0 Mon Sep 17 00:00:00 2001 From: Mattia Moffa Date: Wed, 18 Nov 2026 02:57:09 +0100 Subject: [PATCH 20/20] Fix CI failures on new chain CA test - Guard the wolfSSL_clear() test with !WOLFSSL_NO_TLS12 - Skip the stapled OCSP test when WOLFSSL_ALT_CERT_CHAINS is defined, because it clears the CA error before the callback runs --- tests/api.c | 7 ++++++- tests/api/test_ocsp.c | 7 +++++++ tests/unit-mcdc/test_crl_whitebox.c | 18 +++++++++--------- .../test_internal_revocation_whitebox.c | 14 ++++++++------ 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/tests/api.c b/tests/api.c index 5a7a4d6169a..df4e17b5d5b 100644 --- a/tests/api.c +++ b/tests/api.c @@ -42980,8 +42980,13 @@ static int test_override_alt_cert_chain(void) } #endif +/* WOLFSSL_ALT_CERT_CHAINS and Apple native validation both clear a chain CA's + * missing-issuer error before the verify callback runs, so there is nothing + * for it to let through. */ #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \ - defined(WOLFSSL_PEM_TO_DER) && !defined(WOLFSSL_ALT_CERT_CHAINS) + defined(WOLFSSL_PEM_TO_DER) && !defined(WOLFSSL_ALT_CERT_CHAINS) && \ + !defined(WOLFSSL_NO_TLS12) && \ + !defined(WOLFSSL_APPLE_NATIVE_CERT_VALIDATION) /* Check if err is "no signer", also accounting for OPENSSL_EXTRA */ static int test_chain_ca_no_persist_is_no_signer(int err) { diff --git a/tests/api/test_ocsp.c b/tests/api/test_ocsp.c index 171c848bc96..7a45e0e8658 100644 --- a/tests/api/test_ocsp.c +++ b/tests/api/test_ocsp.c @@ -6473,6 +6473,7 @@ static int test_ocsp_ctx_request_rotate_cert(WOLFSSL_CTX* ctx, return EXPECT_RESULT(); } +#ifndef WOLFSSL_ALT_CERT_CHAINS /* A stapled OCSP response signed by a CA that only this connection holds, * because the verify callback let it through. The responder is not in the * certificate manager, so the staple verifies only when the connection's own @@ -6578,6 +6579,12 @@ int test_ocsp_staple_signed_by_waived_ca(void) return EXPECT_RESULT(); } +#else +int test_ocsp_staple_signed_by_waived_ca(void) +{ + return TEST_SKIPPED; +} +#endif /* !WOLFSSL_ALT_CERT_CHAINS */ /* The cached leaf request is built from the CTX certificate, so it has to go * when that certificate is replaced. Otherwise every later connection asks the diff --git a/tests/unit-mcdc/test_crl_whitebox.c b/tests/unit-mcdc/test_crl_whitebox.c index 3ee20d4f938..c7f930a92a3 100644 --- a/tests/unit-mcdc/test_crl_whitebox.c +++ b/tests/unit-mcdc/test_crl_whitebox.c @@ -90,13 +90,13 @@ static void wb_check_cert_crl_ex(WOLFSSL_CERT_MANAGER* cm) XMEMSET(issuerHash, 0x22, sizeof(issuerHash)); WB_NOTE(CheckCertCRLCm(cm->crl, issuerHash, NULL, (int)sizeof(serial), - hash, NULL, 0, NULL, cm, 1)); + hash, NULL, 0, NULL, cm, 1, NULL)); WB_NOTE(CheckCertCRLCm(cm->crl, issuerHash, NULL, (int)sizeof(serial), - NULL, NULL, 0, NULL, cm, 1)); + NULL, NULL, 0, NULL, cm, 1, NULL)); WB_NOTE(CheckCertCRLCm(cm->crl, issuerHash, serial, 0, - NULL, NULL, 0, NULL, cm, 1)); + NULL, NULL, 0, NULL, cm, 1, NULL)); WB_NOTE(CheckCertCRLCm(cm->crl, issuerHash, serial, (int)sizeof(serial), - NULL, NULL, 0, NULL, cm, 1)); + NULL, NULL, 0, NULL, cm, 1, NULL)); } @@ -154,14 +154,14 @@ static void wb_missing_crl_callbacks(WOLFSSL_CERT_MANAGER* cm) /* 1. cm NULL: operand 0 false for both decisions. */ WB_NOTE(CheckCertCRLCm(cm->crl, issuerHash, serial, (int)sizeof(serial), NULL, (const byte*)url, (int)XSTRLEN(url), NULL, - NULL, 1)); + NULL, 1, NULL)); /* 2. real cm, no callbacks: operand 0 true, operand 1 false. */ cm->cbMissingCRL = NULL; cm->crlCb = NULL; WB_NOTE(CheckCertCRLCm(cm->crl, issuerHash, serial, (int)sizeof(serial), NULL, (const byte*)url, (int)XSTRLEN(url), NULL, - cm, 1)); + cm, 1, NULL)); /* 3. both callbacks installed, error cb returns 0: takes :668 TRUE (the * partner that gives operands 0 and 1 their pairs) and :686 to its third @@ -171,14 +171,14 @@ static void wb_missing_crl_callbacks(WOLFSSL_CERT_MANAGER* cm) g_crlCbResult = 0; WB_NOTE(CheckCertCRLCm(cm->crl, issuerHash, serial, (int)sizeof(serial), NULL, (const byte*)url, (int)XSTRLEN(url), NULL, - cm, 1)); + cm, 1, NULL)); /* 4. error cb returns non-zero: :686 operand 2 true, decision TRUE, which * is the override-the-CRL-error path. */ g_crlCbResult = 1; WB_NOTE(CheckCertCRLCm(cm->crl, issuerHash, serial, (int)sizeof(serial), NULL, (const byte*)url, (int)XSTRLEN(url), NULL, - cm, 1)); + cm, 1, NULL)); /* 5. a url longer than the 256-byte stack buffer takes the "CRL url too * long" arm of the copy guard inside the :668 body, which the short url @@ -189,7 +189,7 @@ static void wb_missing_crl_callbacks(WOLFSSL_CERT_MANAGER* cm) longUrl[sizeof(longUrl) - 1] = '\0'; WB_NOTE(CheckCertCRLCm(cm->crl, issuerHash, serial, (int)sizeof(serial), NULL, (const byte*)longUrl, - (int)sizeof(longUrl) - 1, NULL, cm, 1)); + (int)sizeof(longUrl) - 1, NULL, cm, 1, NULL)); } cm->cbMissingCRL = NULL; diff --git a/tests/unit-mcdc/test_internal_revocation_whitebox.c b/tests/unit-mcdc/test_internal_revocation_whitebox.c index d2d41a7a2c6..fa8424e3b1a 100644 --- a/tests/unit-mcdc/test_internal_revocation_whitebox.c +++ b/tests/unit-mcdc/test_internal_revocation_whitebox.c @@ -91,9 +91,10 @@ static int mcdc_OcspNoUrlPolicy(WOLFSSL_CERT_MANAGER* cm) #endif #ifdef HAVE_CRL -static int mcdc_CheckCertCRL(WOLFSSL_CRL* crl, DecodedCert* cert) +static int mcdc_CheckCertCRL(WOLFSSL_CRL* crl, DecodedCert* cert, + Signer* extraCa) { - (void)crl; (void)cert; + (void)crl; (void)cert; (void)extraCa; g_crlCalls++; return g_crlRet; } @@ -103,10 +104,11 @@ static int mcdc_CheckCertCRL(WOLFSSL_CRL* crl, DecodedCert* cert) * answer. */ static int mcdc_CheckCertCRL_ex(WOLFSSL_CRL* crl, byte* issuerHash, byte* serial, int serialSz, byte* serialHash, const byte* extCrlInfo, - int extCrlInfoSz, void* issuerName) + int extCrlInfoSz, void* issuerName, Signer* extraCa) { (void)crl; (void)issuerHash; (void)serial; (void)serialSz; (void)serialHash; (void)extCrlInfo; (void)extCrlInfoSz; (void)issuerName; + (void)extraCa; g_crlCalls++; return g_crlRet; } @@ -120,9 +122,9 @@ static int mcdc_CheckCertCRL_ex(WOLFSSL_CRL* crl, byte* issuerHash, #define OcspNoUrlPolicy(a) mcdc_OcspNoUrlPolicy((a)) #endif #ifdef HAVE_CRL - #define CheckCertCRL(a, b) mcdc_CheckCertCRL((a), (b)) - #define CheckCertCRL_ex(a, b, c, d, e, f, g, h) \ - mcdc_CheckCertCRL_ex((a), (b), (c), (d), (e), (f), (g), (h)) + #define CheckCertCRL(a, b, c) mcdc_CheckCertCRL((a), (b), (c)) + #define CheckCertCRL_ex(a, b, c, d, e, f, g, h, i) \ + mcdc_CheckCertCRL_ex((a), (b), (c), (d), (e), (f), (g), (h), (i)) #endif #include