diff --git a/src/internal.c b/src/internal.c index 5a8af6ee8e1..ba4b525e565 100644 --- a/src/internal.c +++ b/src/internal.c @@ -10652,6 +10652,10 @@ void wolfSSL_ResourceFree(WOLFSSL* ssl) wolfSSL_CRYPTO_cleanup_ex_data(&ssl->ex_data); #endif +#ifdef WOLFSSL_CHAIN_CA_DATES + wolfssl_local_FreeChainCADates(ssl); +#endif + FreeCiphers(ssl); FreeArrays(ssl, 0); #ifdef WOLFSSL_CALLBACKS @@ -10956,11 +10960,51 @@ void wolfSSL_ResourceFree(WOLFSSL* ssl) #endif /* WOLFSSL_STATIC_MEMORY */ } +#ifdef WOLFSSL_CHAIN_CA_DATES +/* Free the chain CA dates of this handshake without storing them. */ +void wolfssl_local_FreeChainCADates(WOLFSSL* ssl) +{ + ChainCADate* entry; + + if (ssl == NULL) + return; + + while ((entry = ssl->chainCaDates) != NULL) { + ssl->chainCaDates = entry->next; + XFREE(entry, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); + } +} + +/* Store the renewed dates of this handshake on the cached chain CAs, then free + * the list. Call this only when the handshake verified the peer, including + * its certificate status. A failure leaves the cached dates as they were. */ +void wolfssl_local_CommitChainCADates(WOLFSSL* ssl) +{ + ChainCADate* entry; + + if (ssl == NULL) + return; + + for (entry = ssl->chainCaDates; entry != NULL; entry = entry->next) { + if (!entry->waived && + wolfssl_local_SetChainCAValidity(SSL_CM(ssl), entry->hash, + entry->keyHash, &entry->validity) != 0) { + WOLFSSL_MSG("Could not store dates of renewed chain CA"); + } + } + wolfssl_local_FreeChainCADates(ssl); +} +#endif /* WOLFSSL_CHAIN_CA_DATES */ + /* Free any handshake resources no longer needed */ void FreeHandshakeResources(WOLFSSL* ssl) { WOLFSSL_ENTER("FreeHandshakeResources"); +#ifdef WOLFSSL_CHAIN_CA_DATES + wolfssl_local_FreeChainCADates(ssl); +#endif + #ifdef WOLFSSL_DTLS if (ssl->options.dtls) { /* DTLS_POOL (DTLSv1.3 flushes the queue autonomously) */ @@ -18780,6 +18824,76 @@ static int CheckChainCAExtKeyUsage(const WOLFSSL* ssl, const DecodedCert* cert) } #endif /* IGNORE_KEY_EXTENSIONS */ +#ifdef WOLFSSL_CHAIN_CA_DATES +/* Record the dates of the chain CA in cert for this handshake. A waived + * entry is a CA whose date error the verify callback accepted, and the walk + * over cached CAs does not report that error again. Otherwise cert renews a + * cached CA, and the walk uses its dates. The renewed dates are stored on the + * cached CA by wolfssl_local_CommitChainCADates() when the handshake verifies + * the peer. + * Returns 0, MEMORY_E, BAD_MUTEX_E or a CalcHashId() or date error. */ +static int AddChainCADate(WOLFSSL* ssl, const DecodedCert* cert, int waived) +{ + int ret; + ChainCADate* entry; + + if (cert->publicKey == NULL) + return 0; + + entry = (ChainCADate*)XMALLOC(sizeof(ChainCADate), ssl->heap, + DYNAMIC_TYPE_TMP_BUFFER); + if (entry == NULL) + return MEMORY_E; + XMEMSET(entry, 0, sizeof(ChainCADate)); + entry->waived = (byte)(waived != 0); + +#ifndef NO_SKID + XMEMCPY(entry->hash, cert->extSubjKeyId, SIGNER_DIGEST_SIZE); +#else + XMEMCPY(entry->hash, cert->subjectHash, SIGNER_DIGEST_SIZE); +#endif + ret = CalcHashId(cert->publicKey, cert->pubKeySize, entry->keyHash); + if (ret == 0) + ret = wolfssl_local_SetSignerValidity(&entry->validity, cert); + if (ret == 0 && !waived) { + ret = wolfssl_local_IsChainCARenewal(SSL_CM(ssl), entry->hash, + entry->keyHash, cert, &entry->validity); + if (ret == 1) { + WOLFSSL_MSG("Peer sent a renewed cached chain CA"); + ret = 0; + } + else if (ret == 0) { + /* Not a renewal, so there is nothing to record. */ + ret = 1; + } + } + if (ret != 0) { + XFREE(entry, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); + return (ret == 1) ? 0 : ret; + } + + entry->next = ssl->chainCaDates; + ssl->chainCaDates = entry; + + return 0; +} + +/* Check the cached chain CAs on the path above the leaf, with the dates this + * handshake decided. */ +static int CheckPeerChainCADates(WOLFSSL* ssl, Signer* ca) +{ + Signer* pending = NULL; + +#if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && !defined(NO_TLS) + if (TLSX_CSR2_IsMulti(ssl->extensions)) + pending = TLSX_CSR2_GetPendingSigners(ssl->extensions); +#endif + + return wolfssl_local_CheckChainCADates(SSL_CM(ssl), ca, pending, + ssl->chainCaDates); +} +#endif /* WOLFSSL_CHAIN_CA_DATES */ + #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) /* Parse a chain certificate as a CA and add it to the pending signers list * for Certificate Status Request v2. */ @@ -19872,6 +19986,11 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, { word32 listSz; + #ifdef WOLFSSL_CHAIN_CA_DATES + /* Drop the dates of an earlier chain that was not verified. */ + wolfssl_local_FreeChainCADates(ssl); + #endif + #ifdef WOLFSSL_CALLBACKS if (ssl->hsInfoOn) AddPacketName(ssl, "Certificate"); @@ -20209,6 +20328,9 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, unsigned long peerVerifyRet = ssl->peerVerifyRet; #endif int skipAddCA = 0; + #ifdef WOLFSSL_CHAIN_CA_DATES + int caVerifyRet = 0; + #endif #if defined(HAVE_OCSP) && defined(HAVE_CRL) int ocspNoUrl = 0; int ocspStapleDeferred = 0; @@ -20569,11 +20691,33 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, } #endif /* defined(__APPLE__) && defined(WOLFSSL_SYS_CA_CERTS) */ + #ifdef WOLFSSL_CHAIN_CA_DATES + caVerifyRet = ret; + #endif SetRevocationVerifyRet(ssl, ret); /* Do verify callback */ ret = DoVerifyCallbackHeldCrl(ssl, args, ret, &heldCrlRet); if (CrlErrWaived(ssl, ret)) ret = ssl->error = 0; + #ifdef WOLFSSL_CHAIN_CA_DATES + /* The walk at the leaf checks cached CAs again. It must + * not report a date error on this CA that the callback + * accepted, and it uses the dates of a renewed CA. Those + * dates reach the cached CA only when the handshake + * verifies the peer. */ + if (ret == 0 && args->dCert->isCA && + !ssl->options.verifyNone) { + if (caVerifyRet == 0) { + ret = AddChainCADate(ssl, args->dCert, 0); + } + else if (caVerifyRet == + WC_NO_ERR_TRACE(ASN_BEFORE_DATE_E) || + caVerifyRet == + WC_NO_ERR_TRACE(ASN_AFTER_DATE_E)) { + ret = AddChainCADate(ssl, args->dCert, 1); + } + } + #endif #if defined(HAVE_OCSP) && defined(HAVE_CRL) && \ (defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \ defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)) @@ -20710,6 +20854,10 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, case TLS_ASYNC_DO: { + #ifdef WOLFSSL_CHAIN_CA_DATES + int caDateRet = 0; + #endif + /* peer's, may not have one if blank client cert sent by TLSv1.2 */ if (args->count > 0) { WOLFSSL_MSG("Verifying Peer's cert"); @@ -20779,6 +20927,24 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, } } #endif + #ifdef WOLFSSL_CHAIN_CA_DATES + /* A CA cached from an earlier peer chain may have expired + * since. A date error on the leaf does not hide that one: the + * callback is asked about it after it accepts the leaf's. */ + if ((ret == 0 || ret == WC_NO_ERR_TRACE(ASN_BEFORE_DATE_E) || + ret == WC_NO_ERR_TRACE(ASN_AFTER_DATE_E)) && + !ssl->options.verifyNone && !UsingChainVerifyCb(ssl) + #ifdef WOLFSSL_TRUST_PEER_CERT + && !args->haveTrustPeer + #endif + ) { + caDateRet = CheckPeerChainCADates(ssl, args->dCert->ca); + if (ret == 0) { + ret = caDateRet; + caDateRet = 0; + } + } + #endif if (ret == 0) { WOLFSSL_MSG("Verified Peer's cert"); @@ -20960,6 +21126,12 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, /* Do verify callback. */ args->leafVerifyErr = ret = DoVerifyCallback(SSL_CM(ssl), ssl, ret, args); + #ifdef WOLFSSL_CHAIN_CA_DATES + if (ret == 0 && caDateRet != 0) { + args->leafVerifyErr = ret = DoVerifyCallback( + SSL_CM(ssl), ssl, caDateRet, args); + } + #endif if (ret != 0) { WOLFSSL_MSG("\tfatal cert error"); @@ -21374,6 +21546,22 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, ssl->options.serverState = SERVER_CERT_COMPLETE; } + #ifdef WOLFSSL_CHAIN_CA_DATES + if (ret != 0) { + wolfssl_local_FreeChainCADates(ssl); + } + #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && !defined(NO_TLS) + else if (TLSX_CSR2_IsMulti(ssl->extensions)) { + /* The stapled status of the chain CAs is checked later. + * TLSX_CSR2_MergePendingCA() stores the renewed dates and + * TLSX_CSR2_ClearPendingCA() drops them. */ + } + #endif + else { + wolfssl_local_CommitChainCADates(ssl); + } + #endif + /* Advance state and proceed */ ssl->options.asyncState = TLS_ASYNC_END; } /* case TLS_ASYNC_FINALIZE */ diff --git a/src/ssl_certman.c b/src/ssl_certman.c index 0946251320b..aaa959e582e 100644 --- a/src/ssl_certman.c +++ b/src/ssl_certman.c @@ -664,6 +664,15 @@ int wolfSSL_CertManagerLoadCABufferType(WOLFSSL_CERT_MANAGER* cm, } else { ret = SetCAType(cm, dCert->extSubjKeyId, type); } + #ifdef WOLFSSL_CHAIN_CA_DATES + /* A chain CA is not a trust anchor and its dates are checked + * each time it is used. */ + if (ret == WOLFSSL_SUCCESS && type == WOLFSSL_CHAIN_CA && + wolfssl_local_SetChainCADates(cm, dCert->extSubjKeyId, + dCert) != 0) { + ret = WOLFSSL_FATAL_ERROR; + } + #endif } if (dCert) { @@ -900,6 +909,7 @@ int CM_VerifyBuffer_ex(WOLFSSL_CERT_MANAGER* cm, const unsigned char* buff, { int ret = 0; int crlRet = 0; + int caDateRet = 0; int fatal = 0; DerBuffer* der = NULL; WC_DECLARE_VAR(cert, DecodedCert, 1, 0); @@ -956,6 +966,19 @@ int CM_VerifyBuffer_ex(WOLFSSL_CERT_MANAGER* cm, const unsigned char* buff, * against a known CA. */ ret = ParseCertRelative(cert, CERT_TYPE, VERIFY, cm, NULL); } +#ifdef WOLFSSL_CHAIN_CA_DATES + if ((ret == 0) || (ret == WC_NO_ERR_TRACE(ASN_BEFORE_DATE_E)) || + (ret == WC_NO_ERR_TRACE(ASN_AFTER_DATE_E))) { + /* A CA cached from a peer's chain may have expired since. A date + * error on cert does not hide that one: the callback is asked about + * it after it accepts the error on cert. */ + caDateRet = wolfssl_local_CheckChainCADates(cm, cert->ca, NULL, NULL); + if (ret == 0) { + ret = caDateRet; + caDateRet = 0; + } + } +#endif #ifdef HAVE_CRL if (cm->crlEnabled && ((ret == 0) || @@ -969,6 +992,7 @@ int CM_VerifyBuffer_ex(WOLFSSL_CERT_MANAGER* cm, const unsigned char* buff, #endif (void)fatal; + (void)caDateRet; #if !defined(NO_WOLFSSL_CM_VERIFY) && \ (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)) @@ -1026,6 +1050,11 @@ int CM_VerifyBuffer_ex(WOLFSSL_CERT_MANAGER* cm, const unsigned char* buff, } /* Use callback to verify certificate. */ ret = DoVerifyCallback(cm, NULL, ret, args); + /* Waiving the date error on cert must not waive the one on a + * cached chain CA. */ + if ((ret == 0) && (caDateRet != 0)) { + ret = DoVerifyCallback(cm, NULL, caDateRet, args); + } /* Waiving the date error must not waive the CRL result. */ if ((ret == 0) && (crlRet != 0)) { ret = DoVerifyCallback(cm, NULL, crlRet, args); @@ -2884,6 +2913,288 @@ int AlreadySigner(WOLFSSL_CERT_MANAGER* cm, byte* hash) return ret; } +#ifdef WOLFSSL_CHAIN_CA_DATES +/* Find the signer that AddCA() filed under hash. Call with caLock held. */ +static Signer* cm_find_signer_locked(WOLFSSL_CERT_MANAGER* cm, + const byte* hash) +{ + Signer* signer = cm->caTable[HashSigner(hash)]; + + while (signer != NULL) { + #ifndef NO_SKID + if (XMEMCMP(hash, signer->subjectKeyIdHash, SIGNER_DIGEST_SIZE) == 0) + #else + if (XMEMCMP(hash, signer->subjectNameHash, SIGNER_DIGEST_SIZE) == 0) + #endif + break; + signer = signer->next; + } + + return signer; +} + +/* Returns 1 when the public key of signer hashes to keyHash. */ +static int cm_signer_key_is(const Signer* signer, const byte* keyHash) +{ + byte hash[KEYID_SIZE]; + + if (signer->publicKey == NULL || signer->pubKeySize == 0) + return 0; + if (CalcHashId(signer->publicKey, signer->pubKeySize, hash) != 0) + return 0; + + return XMEMCMP(hash, keyHash, KEYID_SIZE) == 0; +} + +/* Find the entry in dates for signer. AddCA() files a signer under the same + * hash as the entry, and the entry also names the public key. */ +static const ChainCADate* cm_find_chain_ca_date(const ChainCADate* dates, + const Signer* signer) +{ + for (; dates != NULL; dates = dates->next) { + #ifndef NO_SKID + if (XMEMCMP(dates->hash, signer->subjectKeyIdHash, + SIGNER_DIGEST_SIZE) == 0 && + #else + if (XMEMCMP(dates->hash, signer->subjectNameHash, + SIGNER_DIGEST_SIZE) == 0 && + #endif + cm_signer_key_is(signer, dates->keyHash)) { + break; + } + } + + return dates; +} + +/* Check the dates of each CA cached from a peer's chain on the path from ca up + * to the trust anchor. Such a CA is not a trust anchor, so it is checked each + * time it is used and not only when the peer that sent it was verified. Other + * CAs keep no dates and are skipped. The walk stops at a self-signed CA and at + * a CA the application loaded (WOLFSSL_USER_CA or WOLFSSL_USER_INTER): the + * path ends at that trust anchor. With IGNORE_NAME_CONSTRAINTS there is no + * issuer lookup and only ca is checked. + * + * @param [in] cm Certificate manager. + * @param [in] ca CA that verified the certificate. May be NULL. + * @param [in] pending Certificate Status Request v2 signers that are not in + * the certificate manager yet. May be NULL. + * @param [in] dates Dates from this handshake. A CA with a waived entry is + * not checked. A CA with renewed dates is checked against + * them. May be NULL. + * @return 0 when no cached chain CA on the path is out of date. + * @return ASN_BEFORE_DATE_E when a cached chain CA is not valid yet. + * @return ASN_AFTER_DATE_E when a cached chain CA has expired. + * @return BAD_MUTEX_E when the CA lock cannot be taken. + * @return MAX_CHAIN_ERROR when the path is longer than + * WOLFSSL_MAX_CHAIN_DEPTH. + */ +int wolfssl_local_CheckChainCADates(WOLFSSL_CERT_MANAGER* cm, Signer* ca, + Signer* pending, const ChainCADate* dates) +{ + int ret = 0; + int depth = 0; + int type; + int haveDates; + Signer* prev = NULL; + Signer* parent; + const ChainCADate* entry; + SignerValidity validity; + + if (cm == NULL || wc_AsnGetSkipDateCheck()) + return 0; + + while (ret == 0 && ca != NULL) { + /* Another handshake can set the dates, and the application can + * change the type. Read both under the lock. */ + if (wc_LockMutex(&cm->caLock) != 0) { + ret = BAD_MUTEX_E; + break; + } + type = ca->type; + haveDates = (type == WOLFSSL_CHAIN_CA && ca->validity != NULL); + if (haveDates) + XMEMCPY(&validity, ca->validity, sizeof(validity)); + wc_UnLockMutex(&cm->caLock); + + if (type == WOLFSSL_USER_CA || type == WOLFSSL_USER_INTER) + break; + if (haveDates) { + entry = cm_find_chain_ca_date(dates, ca); + if (entry != NULL && !entry->waived) + XMEMCPY(&validity, &entry->validity, sizeof(validity)); + if (entry != NULL && entry->waived) { + WOLFSSL_MSG("Date error of cached chain CA already accepted"); + } + else if (!XVALIDATE_DATE(&validity.notBefore[2], + validity.notBefore[0], ASN_BEFORE, + validity.notBefore[1])) { + WOLFSSL_MSG("Cached chain CA is not yet valid"); + ret = ASN_BEFORE_DATE_E; + break; + } + else if (!XVALIDATE_DATE(&validity.notAfter[2], + validity.notAfter[0], ASN_AFTER, + validity.notAfter[1])) { + WOLFSSL_MSG("Cached chain CA has expired"); + ret = ASN_AFTER_DATE_E; + break; + } + } + if (ca->selfSigned) + break; + #ifndef IGNORE_NAME_CONSTRAINTS + parent = wolfssl_local_FindSignerByAkidOrName(cm, pending, ca); + #else + (void)pending; + parent = NULL; + #endif + /* Stop on missing parent, self-loop, or A->B->A cycle. */ + if (parent == NULL || parent == ca || parent == prev) + break; + if (++depth >= WOLFSSL_MAX_CHAIN_DEPTH) { + ret = MAX_CHAIN_ERROR; + break; + } + prev = ca; + ca = parent; + } + + if (ret != 0) + WOLFSSL_ERROR_VERBOSE(ret); + + return ret; +} + +/* Store validity on the CA cached from a peer's chain that AddCA() filed + * under hash. A CA of any other type, or with a different public key, is left + * unchanged. + * + * @param [in] cm Certificate manager. + * @param [in] hash Hash identifying the CA, as AddCA() files it. + * @param [in] keyHash CalcHashId() of the public key of the CA. + * @param [in] validity Dates to store. + * @return 0 on success, including when there is nothing to change. + * @return BAD_FUNC_ARG when an argument is NULL. + * @return BAD_MUTEX_E when the CA lock cannot be taken. + * @return MEMORY_E when dynamic memory allocation fails. + */ +int wolfssl_local_SetChainCAValidity(WOLFSSL_CERT_MANAGER* cm, + const byte* hash, const byte* keyHash, const SignerValidity* validity) +{ + int ret = 0; + Signer* signer; + + if (cm == NULL || hash == NULL || keyHash == NULL || validity == NULL) + return BAD_FUNC_ARG; + + if (wc_LockMutex(&cm->caLock) != 0) + return BAD_MUTEX_E; + signer = cm_find_signer_locked(cm, hash); + if (signer != NULL && signer->type == WOLFSSL_CHAIN_CA && + cm_signer_key_is(signer, keyHash)) { + if (signer->validity == NULL) { + signer->validity = (SignerValidity*)XMALLOC( + sizeof(SignerValidity), cm->heap, DYNAMIC_TYPE_SIGNER); + if (signer->validity == NULL) + ret = MEMORY_E; + else + XMEMCPY(signer->validity, validity, sizeof(*validity)); + } + else if (XMEMCMP(signer->validity, validity, + sizeof(*validity)) != 0) { + WOLFSSL_MSG("Updating dates of cached chain CA"); + XMEMCPY(signer->validity, validity, sizeof(*validity)); + } + } + wc_UnLockMutex(&cm->caLock); + + return ret; +} + +/* Store the dates of cert on the CA cached from a peer's chain that it is the + * certificate for. Used when an application loads a CA as WOLFSSL_CHAIN_CA. + * + * @param [in] cm Certificate manager. + * @param [in] hash Hash identifying the CA, as AddCA() files it. + * @param [in] cert Certificate for the CA. + * @return 0 on success, including when there is nothing to change. + * @return BAD_FUNC_ARG when cm, hash or cert is NULL. + * @return ASN_DATE_SZ_E when a date in cert is too long. + * @return BAD_MUTEX_E when the CA lock cannot be taken. + * @return MEMORY_E when dynamic memory allocation fails. + */ +int wolfssl_local_SetChainCADates(WOLFSSL_CERT_MANAGER* cm, const byte* hash, + const DecodedCert* cert) +{ + int ret; + byte keyHash[KEYID_SIZE]; + SignerValidity validity; + + if (cm == NULL || hash == NULL || cert == NULL || + cert->publicKey == NULL) + return BAD_FUNC_ARG; + + ret = wolfssl_local_SetSignerValidity(&validity, cert); + if (ret == 0) + ret = CalcHashId(cert->publicKey, cert->pubKeySize, keyHash); + if (ret == 0) + ret = wolfssl_local_SetChainCAValidity(cm, hash, keyHash, &validity); + + return ret; +} + +/* Decide whether cert renews the CA cached from a peer's chain under hash. + * The cached CA must be a WOLFSSL_CHAIN_CA with the same public key and other + * dates. The issuer of the cached CA must have signed cert: cert names the + * same issuer, and the key that verified it is not the key of the cached CA. + * Without that, the holder of the cached CA's key could sign a new + * certificate for it with later dates. + * + * @param [in] cm Certificate manager. + * @param [in] hash Hash identifying the CA, as AddCA() files it. + * @param [in] keyHash CalcHashId() of the public key of cert. + * @param [in] cert Verified certificate from the peer's chain. + * @param [in] validity Dates of cert. + * @return 1 when cert renews the cached CA. + * @return 0 when it does not. + * @return BAD_MUTEX_E when the CA lock cannot be taken. + */ +int wolfssl_local_IsChainCARenewal(WOLFSSL_CERT_MANAGER* cm, + const byte* hash, const byte* keyHash, const DecodedCert* cert, + const SignerValidity* validity) +{ + int ret = 0; + Signer* signer; + 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) && + !(issuer->pubKeySize == signer->pubKeySize && + XMEMCMP(issuer->publicKey, signer->publicKey, + signer->pubKeySize) == 0) + #if defined(HAVE_OCSP) || defined(HAVE_CRL) || \ + defined(WOLFSSL_AKID_NAME) || !defined(IGNORE_NAME_CONSTRAINTS) + && XMEMCMP(signer->issuerNameHash, cert->issuerHash, + SIGNER_DIGEST_SIZE) == 0 + #endif + ) { + ret = 1; + } + wc_UnLockMutex(&cm->caLock); + + return ret; +} +#endif /* WOLFSSL_CHAIN_CA_DATES */ + #ifdef WOLFSSL_TRUST_PEER_CERT /* use the first 32 bits of the digest as the table row */ static WC_INLINE word32 TrustedPeerHashSigner(const byte* hash) diff --git a/src/tls.c b/src/tls.c index d33c98fcc47..7af9e4cd57f 100644 --- a/src/tls.c +++ b/src/tls.c @@ -4553,6 +4553,10 @@ int TLSX_CSR2_ClearPendingCA(WOLFSSL *ssl) TLSX_CSR2_FreePendingSigners(csr2->pendingSigners, SSL_CM(ssl)->heap); csr2->pendingSigners = NULL; +#ifdef WOLFSSL_CHAIN_CA_DATES + /* The status checks failed, so a renewed chain CA keeps its old dates. */ + wolfssl_local_FreeChainCADates(ssl); +#endif return 0; } @@ -4575,6 +4579,10 @@ int TLSX_CSR2_MergePendingCA(WOLFSSL* ssl) s = next; } csr2->pendingSigners = NULL; +#ifdef WOLFSSL_CHAIN_CA_DATES + /* The status checks passed, so store the dates of renewed chain CAs. */ + wolfssl_local_CommitChainCADates(ssl); +#endif return r; } diff --git a/tests/api/test_ssl_cert.c b/tests/api/test_ssl_cert.c index 5871f320be4..83894a831ab 100644 --- a/tests/api/test_ssl_cert.c +++ b/tests/api/test_ssl_cert.c @@ -4471,7 +4471,450 @@ int test_wolfSSL_alt_chain_forged_ca(void) return EXPECT_RESULT(); } -#if defined(WOLFSSL_SMALL_CERT_VERIFY) && \ +#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_RSA) && \ + !defined(NO_TLS) && !defined(NO_SHA256) && !defined(NO_ASN_TIME) && \ + defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && \ + defined(USE_CERT_BUFFERS_2048) && !defined(IGNORE_KEY_EXTENSIONS) && \ + !defined(NO_ASN_TIME_CHECK) + +#define TEST_CA_DATE_DAY ((time_t)86400) + +static time_t test_ca_date_now = 0; + +/* Report test_ca_date_now as the current time. */ +static time_t test_ca_date_time_cb(time_t* t) +{ + if (t != NULL) + *t = test_ca_date_now; + + return test_ca_date_now; +} + +/* Stand in for an application that accepts an expired certificate. */ +static int test_ca_date_accept_cb(int preverify, + WOLFSSL_X509_STORE_CTX* store) +{ +#ifdef OPENSSL_COMPATIBLE_DEFAULTS + if (store->error == WOLFSSL_X509_V_ERR_CERT_HAS_EXPIRED) +#else + if (store->error == WC_NO_ERR_TRACE(ASN_AFTER_DATE_E)) +#endif + return 1; + + return preverify; +} + +/* Stand in for an application that accepts a certificate that is not yet + * valid. */ +static int test_ca_date_accept_early_cb(int preverify, + WOLFSSL_X509_STORE_CTX* store) +{ +#ifdef OPENSSL_COMPATIBLE_DEFAULTS + if (store->error == WOLFSSL_X509_V_ERR_CERT_NOT_YET_VALID) +#else + if (store->error == WC_NO_ERR_TRACE(ASN_BEFORE_DATE_E)) +#endif + return 1; + + return preverify; +} + +/* Stand in for an application that accepts an expired CA only where the peer + * sends it, at depth 1. */ +static int test_ca_date_accept_depth1_cb(int preverify, + WOLFSSL_X509_STORE_CTX* store) +{ + if (store->error_depth == 1) + return test_ca_date_accept_cb(preverify, store); + + return preverify; +} + +/* Build a CA:TRUE certificate for subjKey. A NULL issuerDer makes it + * self-signed, otherwise it is signed by issuerKey in the name of issuerDer. + * When akid is set the Authority Key Identifier names the signing key, + * otherwise the extension is left out. Returns the DER length, or < 0 on + * failure. */ +static int test_ca_date_gen_ca(byte* out, int outMax, RsaKey* subjKey, + const byte* issuerDer, int issuerDerSz, RsaKey* issuerKey, WC_RNG* rng, + const char* cn, int akid) +{ + Cert cert; + int ret = 0; + RsaKey* signKey = (issuerDer == NULL) ? subjKey : issuerKey; + + if (wc_InitCert(&cert) != 0) + return -1; + cert.isCA = 1; + cert.sigType = CTC_SHA256wRSA; + XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE - 1); + XSTRNCPY(cert.subject.org, "wolfSSL_test", CTC_NAME_SIZE - 1); + XSTRNCPY(cert.subject.commonName, cn, CTC_NAME_SIZE - 1); + if (wc_SetSubjectKeyIdFromPublicKey(&cert, subjKey, NULL) != 0) + ret = -1; + if (ret == 0 && akid && + wc_SetAuthKeyIdFromPublicKey(&cert, signKey, NULL) != 0) + ret = -1; + if (ret == 0 && wc_SetKeyUsage(&cert, "keyCertSign,cRLSign") != 0) + ret = -1; + /* wc_InitCert() leaves selfSigned set, so naming no issuer is what makes + * the generated certificate self-signed. */ + if (ret == 0 && issuerDer != NULL && + wc_SetIssuerBuffer(&cert, issuerDer, issuerDerSz) != 0) + ret = -1; + if (ret == 0) + ret = wc_MakeCert(&cert, out, (word32)outMax, subjKey, NULL, rng); + if (ret >= 0) + ret = wc_SignCert(cert.bodySz, cert.sigType, out, (word32)outMax, + signKey, NULL, rng); +#ifdef WOLFSSL_CERT_GEN_CACHE + wc_SetCert_Free(&cert); +#endif + return ret; +} + +/* Connect a new client from *ctx_c to a new server that presents chainDer, + * with the clock at now. *ctx_c is created on the first call and trusts only + * rootDer, so later calls share its certificate manager. The client's error, + * or 0 when the handshake completes, is returned through hsRet. */ +static int test_ca_date_connect(WOLFSSL_CTX** ctx_c, const byte* rootDer, + int rootSz, const byte* chainDer, int chainSz, time_t now, + VerifyCallback verifyCb, int* hsRet) +{ + EXPECT_DECLS; + WOLFSSL_CTX* ctx_s = NULL; + WOLFSSL* ssl_c = NULL; + WOLFSSL* ssl_s = NULL; + struct test_memio_ctx test_ctx; + + *hsRet = -1; + XMEMSET(&test_ctx, 0, sizeof(test_ctx)); + ExpectIntEQ(test_memio_setup_ex(&test_ctx, ctx_c, &ctx_s, &ssl_c, &ssl_s, + wolfSSLv23_client_method, wolfSSLv23_server_method, + (byte*)rootDer, rootSz, (byte*)chainDer, chainSz, + (byte*)client_key_der_2048, (int)sizeof_client_key_der_2048), 0); + + if (EXPECT_SUCCESS()) { + wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_PEER, verifyCb); + test_ca_date_now = now; + ExpectIntEQ(wc_SetTimeCb(test_ca_date_time_cb), 0); + if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) == 0) + *hsRet = 0; + else + *hsRet = wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR); + (void)wc_SetTimeCb(NULL); + } + + wolfSSL_free(ssl_s); + wolfSSL_free(ssl_c); + wolfSSL_CTX_free(ctx_s); + + return EXPECT_RESULT(); +} + +/* Verify leafDer with a certificate manager that trusts rootDer and holds + * caDer as a CA of type caType. A non-NULL subDer is a CA issued by caDer and + * is held as a CA of type subType. The clock is at now and a non-NULL + * verifyCb is the verify callback. Returns the result of + * wolfSSL_CertManagerVerifyBuffer(), or -1 when the setup fails. */ +static int test_ca_date_cm_verify(const byte* rootDer, int rootSz, + const byte* caDer, int caSz, int caType, const byte* subDer, int subSz, + int subType, const byte* leafDer, int leafSz, time_t now, + VerifyCallback verifyCb) +{ + WOLFSSL_CERT_MANAGER* cm; + int ret = -1; + + cm = wolfSSL_CertManagerNew(); + if (cm == NULL) + return ret; + +#ifndef NO_WOLFSSL_CM_VERIFY + if (verifyCb != NULL) + wolfSSL_CertManagerSetVerify(cm, verifyCb); +#else + (void)verifyCb; +#endif + if (wolfSSL_CertManagerLoadCABuffer(cm, rootDer, rootSz, + WOLFSSL_FILETYPE_ASN1) == WOLFSSL_SUCCESS && + wolfSSL_CertManagerLoadCABufferType(cm, caDer, caSz, + WOLFSSL_FILETYPE_ASN1, 0, WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS, + caType) == WOLFSSL_SUCCESS && + (subDer == NULL || + wolfSSL_CertManagerLoadCABufferType(cm, subDer, subSz, + WOLFSSL_FILETYPE_ASN1, 0, WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS, + subType) == WOLFSSL_SUCCESS)) { + test_ca_date_now = now; + if (wc_SetTimeCb(test_ca_date_time_cb) == 0) { + ret = wolfSSL_CertManagerVerifyBuffer(cm, leafDer, leafSz, + WOLFSSL_FILETYPE_ASN1); + (void)wc_SetTimeCb(NULL); + } + } + wolfSSL_CertManagerFree(cm); + + return ret; +} +#endif /* cached chain CA date test dependencies */ + +/* Test that a CA cached from a peer's chain is not used once it has expired. + * + * After a handshake the client keeps the intermediate CA that the server sent. + * A later server that sends only its leaf is verified with that cached CA, so + * the CA's dates must be checked then too: the client must fail the same way + * it does when the expired CA is sent. A CA renewed with the same key and sent + * again replaces the cached dates. A CA that the application loaded is a trust + * anchor and its dates are not checked again. + * + * @return TEST_SUCCESS on success. + */ +int test_wolfSSL_chain_ca_cached_dates(void) +{ + EXPECT_DECLS; +#if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_RSA) && \ + !defined(NO_TLS) && !defined(NO_SHA256) && !defined(NO_ASN_TIME) && \ + defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && \ + defined(USE_CERT_BUFFERS_2048) && !defined(IGNORE_KEY_EXTENSIONS) && \ + !defined(NO_ASN_TIME_CHECK) + const byte* rootDer = ca_cert_der_2048; + int rootSz = (int)sizeof_ca_cert_der_2048; + WOLFSSL_CTX* ctx_c = NULL; + WC_RNG rng; + RsaKey caKey; + RsaKey intKey; + RsaKey leafKey; + int rngInit = 0; + int caInit = 0; + int intInit = 0; + int leafInit = 0; + byte* oldCaDer = NULL; + byte* newCaDer = NULL; + byte* chainDer = NULL; + byte* earlyDer = NULL; + byte* selfDer = NULL; + byte* noAkidDer = NULL; + byte* subDer = NULL; + byte* subLeafDer = NULL; + int oldCaSz = 0; + int newCaSz = 0; + int leafSz = 0; + int earlySz = 0; + int selfSz = 0; + int noAkidSz = 0; + int subSz = 0; + int subLeafSz = 0; + int hsRet = -1; + time_t now = wc_Time(NULL); + /* The old CA expires in 100 days. The leaf and the renewed CA are valid + * for 500 days. */ + time_t later = now + 200 * TEST_CA_DATE_DAY; + word32 idx; + + ExpectNotNull(oldCaDer = (byte*)XMALLOC(TEST_EKU_CERT_BUF_SZ, NULL, + DYNAMIC_TYPE_TMP_BUFFER)); + ExpectNotNull(newCaDer = (byte*)XMALLOC(TEST_EKU_CERT_BUF_SZ, NULL, + DYNAMIC_TYPE_TMP_BUFFER)); + ExpectNotNull(chainDer = (byte*)XMALLOC(3 * TEST_EKU_CERT_BUF_SZ, NULL, + DYNAMIC_TYPE_TMP_BUFFER)); + ExpectNotNull(earlyDer = (byte*)XMALLOC(TEST_EKU_CERT_BUF_SZ, NULL, + DYNAMIC_TYPE_TMP_BUFFER)); + ExpectNotNull(selfDer = (byte*)XMALLOC(TEST_EKU_CERT_BUF_SZ, NULL, + DYNAMIC_TYPE_TMP_BUFFER)); + ExpectNotNull(noAkidDer = (byte*)XMALLOC(TEST_EKU_CERT_BUF_SZ, NULL, + DYNAMIC_TYPE_TMP_BUFFER)); + ExpectNotNull(subDer = (byte*)XMALLOC(TEST_EKU_CERT_BUF_SZ, NULL, + DYNAMIC_TYPE_TMP_BUFFER)); + ExpectNotNull(subLeafDer = (byte*)XMALLOC(TEST_EKU_CERT_BUF_SZ, NULL, + DYNAMIC_TYPE_TMP_BUFFER)); + + ExpectIntEQ(wc_InitRng(&rng), 0); + if (EXPECT_SUCCESS()) rngInit = 1; + ExpectIntEQ(wc_InitRsaKey(&caKey, NULL), 0); + if (EXPECT_SUCCESS()) caInit = 1; + idx = 0; + ExpectIntEQ(wc_RsaPrivateKeyDecode(ca_key_der_2048, &idx, &caKey, + (word32)sizeof_ca_key_der_2048), 0); + ExpectIntEQ(wc_InitRsaKey(&intKey, NULL), 0); + if (EXPECT_SUCCESS()) intInit = 1; + idx = 0; + ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &intKey, + (word32)sizeof_server_key_der_2048), 0); + ExpectIntEQ(wc_InitRsaKey(&leafKey, NULL), 0); + if (EXPECT_SUCCESS()) leafInit = 1; + idx = 0; + ExpectIntEQ(wc_RsaPrivateKeyDecode(client_key_der_2048, &idx, &leafKey, + (word32)sizeof_client_key_der_2048), 0); + + /* The old CA was issued 400 days ago and is valid for 500 days. The + * renewed CA has the same name and key and is issued now. */ + if (EXPECT_SUCCESS()) { + test_ca_date_now = now - 400 * TEST_CA_DATE_DAY; + ExpectIntEQ(wc_SetTimeCb(test_ca_date_time_cb), 0); + ExpectIntGT((oldCaSz = test_eku_gen_ca(oldCaDer, TEST_EKU_CERT_BUF_SZ, + &intKey, rootDer, rootSz, &caKey, &rng, NULL, "CA Date Inter", 0, + NULL)), 0); + (void)wc_SetTimeCb(NULL); + } + ExpectIntGT((newCaSz = test_eku_gen_ca(newCaDer, TEST_EKU_CERT_BUF_SZ, + &intKey, rootDer, rootSz, &caKey, &rng, NULL, "CA Date Inter", 0, + NULL)), 0); + /* chainDer holds the leaf followed by the old CA. */ + ExpectIntGT((leafSz = test_eku_gen_leaf(chainDer, TEST_EKU_CERT_BUF_SZ, + &leafKey, oldCaDer, oldCaSz, &intKey, &rng)), 0); + if (EXPECT_SUCCESS()) + XMEMCPY(chainDer + leafSz, oldCaDer, (size_t)oldCaSz); + /* A leaf from the old CA that is not valid until after later. */ + if (EXPECT_SUCCESS()) { + test_ca_date_now = later + 10 * TEST_CA_DATE_DAY; + ExpectIntEQ(wc_SetTimeCb(test_ca_date_time_cb), 0); + ExpectIntGT((earlySz = test_eku_gen_leaf(earlyDer, + TEST_EKU_CERT_BUF_SZ, &leafKey, oldCaDer, oldCaSz, &intKey, &rng)), + 0); + (void)wc_SetTimeCb(NULL); + } + /* A self-signed certificate with the name and key of the old CA. */ + ExpectIntGT((selfSz = test_ca_date_gen_ca(selfDer, TEST_EKU_CERT_BUF_SZ, + &intKey, NULL, 0, NULL, &rng, "CA Date Inter", 1)), 0); + /* The old CA renewed with no Authority Key Identifier. */ + ExpectIntGT((noAkidSz = test_ca_date_gen_ca(noAkidDer, + TEST_EKU_CERT_BUF_SZ, &intKey, rootDer, rootSz, &caKey, &rng, + "CA Date Inter", 0)), 0); + /* A CA below the old CA that the application loads, and its leaf. */ + ExpectIntGT((subSz = test_eku_gen_ca(subDer, TEST_EKU_CERT_BUF_SZ, + &leafKey, oldCaDer, oldCaSz, &intKey, &rng, NULL, "CA Date Sub", 0, + NULL)), 0); + ExpectIntGT((subLeafSz = test_eku_gen_leaf(subLeafDer, + TEST_EKU_CERT_BUF_SZ, &caKey, subDer, subSz, &leafKey, &rng)), 0); + + /* The first handshake sends the old CA and the client caches it. */ + ExpectIntEQ(test_ca_date_connect(&ctx_c, rootDer, rootSz, chainDer, + leafSz + oldCaSz, now, NULL, &hsRet), TEST_SUCCESS); + ExpectIntEQ(hsRet, 0); + /* While it is valid, the cached CA verifies a leaf sent alone. */ + ExpectIntEQ(test_ca_date_connect(&ctx_c, rootDer, rootSz, chainDer, + leafSz, now, NULL, &hsRet), TEST_SUCCESS); + ExpectIntEQ(hsRet, 0); + /* Once it has expired, sending it fails and so must using the cached + * copy. */ + ExpectIntEQ(test_ca_date_connect(&ctx_c, rootDer, rootSz, chainDer, + leafSz + oldCaSz, later, NULL, &hsRet), TEST_SUCCESS); + ExpectIntEQ(hsRet, WC_NO_ERR_TRACE(ASN_AFTER_DATE_E)); + ExpectIntEQ(test_ca_date_connect(&ctx_c, rootDer, rootSz, chainDer, + leafSz, later, NULL, &hsRet), TEST_SUCCESS); + ExpectIntEQ(hsRet, WC_NO_ERR_TRACE(ASN_AFTER_DATE_E)); + /* A verify callback can accept the date error, as for any other. */ + ExpectIntEQ(test_ca_date_connect(&ctx_c, rootDer, rootSz, chainDer, + leafSz, later, test_ca_date_accept_cb, &hsRet), TEST_SUCCESS); + ExpectIntEQ(hsRet, 0); + /* A leaf date error that the callback accepts does not hide the date + * error of the cached CA. */ + ExpectIntEQ(test_ca_date_connect(&ctx_c, rootDer, rootSz, earlyDer, + earlySz, later, test_ca_date_accept_early_cb, &hsRet), TEST_SUCCESS); + ExpectIntEQ(hsRet, WC_NO_ERR_TRACE(ASN_AFTER_DATE_E)); + /* The renewed CA is accepted and its dates replace the cached ones, so a + * leaf sent alone verifies again. */ + if (EXPECT_SUCCESS()) + XMEMCPY(chainDer + leafSz, newCaDer, (size_t)newCaSz); + ExpectIntEQ(test_ca_date_connect(&ctx_c, rootDer, rootSz, chainDer, + leafSz + newCaSz, later, NULL, &hsRet), TEST_SUCCESS); + ExpectIntEQ(hsRet, 0); + ExpectIntEQ(test_ca_date_connect(&ctx_c, rootDer, rootSz, chainDer, + leafSz, later, NULL, &hsRet), TEST_SUCCESS); + ExpectIntEQ(hsRet, 0); + wolfSSL_CTX_free(ctx_c); + ctx_c = NULL; + + /* An expired CA that the callback accepts at depth 1 is not reported + * again at depth 0. The cached copy keeps its old dates, so a leaf sent + * alone later fails at depth 0. */ + if (EXPECT_SUCCESS()) + XMEMCPY(chainDer + leafSz, oldCaDer, (size_t)oldCaSz); + ExpectIntEQ(test_ca_date_connect(&ctx_c, rootDer, rootSz, chainDer, + leafSz + oldCaSz, later, test_ca_date_accept_depth1_cb, &hsRet), + TEST_SUCCESS); + ExpectIntEQ(hsRet, 0); + ExpectIntEQ(test_ca_date_connect(&ctx_c, rootDer, rootSz, chainDer, + leafSz, later, test_ca_date_accept_depth1_cb, &hsRet), TEST_SUCCESS); + ExpectIntEQ(hsRet, WC_NO_ERR_TRACE(ASN_AFTER_DATE_E)); + wolfSSL_CTX_free(ctx_c); + ctx_c = NULL; + + /* A self-signed certificate with the name and key of the cached CA is not + * a renewal and does not change the cached dates. */ + ExpectIntEQ(test_ca_date_connect(&ctx_c, rootDer, rootSz, chainDer, + leafSz + oldCaSz, now, NULL, &hsRet), TEST_SUCCESS); + ExpectIntEQ(hsRet, 0); + if (EXPECT_SUCCESS()) + XMEMCPY(chainDer + leafSz, selfDer, (size_t)selfSz); + ExpectIntEQ(test_ca_date_connect(&ctx_c, rootDer, rootSz, chainDer, + leafSz + selfSz, now, NULL, &hsRet), TEST_SUCCESS); + ExpectIntEQ(hsRet, 0); + ExpectIntEQ(test_ca_date_connect(&ctx_c, rootDer, rootSz, chainDer, + leafSz, later, NULL, &hsRet), TEST_SUCCESS); + ExpectIntEQ(hsRet, WC_NO_ERR_TRACE(ASN_AFTER_DATE_E)); + wolfSSL_CTX_free(ctx_c); + ctx_c = NULL; + + /* A renewed CA with no Authority Key Identifier also replaces the cached + * dates. */ + if (EXPECT_SUCCESS()) + XMEMCPY(chainDer + leafSz, oldCaDer, (size_t)oldCaSz); + ExpectIntEQ(test_ca_date_connect(&ctx_c, rootDer, rootSz, chainDer, + leafSz + oldCaSz, now, NULL, &hsRet), TEST_SUCCESS); + ExpectIntEQ(hsRet, 0); + if (EXPECT_SUCCESS()) + XMEMCPY(chainDer + leafSz, noAkidDer, (size_t)noAkidSz); + ExpectIntEQ(test_ca_date_connect(&ctx_c, rootDer, rootSz, chainDer, + leafSz + noAkidSz, later, NULL, &hsRet), TEST_SUCCESS); + ExpectIntEQ(hsRet, 0); + ExpectIntEQ(test_ca_date_connect(&ctx_c, rootDer, rootSz, chainDer, + leafSz, later, NULL, &hsRet), TEST_SUCCESS); + ExpectIntEQ(hsRet, 0); + wolfSSL_CTX_free(ctx_c); + ctx_c = NULL; + + /* Certificate manager verification checks a cached chain CA the same way. + * A CA that the application loaded is a trust anchor: its dates and the + * dates of the CAs above it are not checked again. + * wolfSSL_CertManagerLoadCABufferType() finds the CA to set the type of by + * its subject key identifier. */ +#ifndef NO_SKID + ExpectIntEQ(test_ca_date_cm_verify(rootDer, rootSz, oldCaDer, oldCaSz, + WOLFSSL_CHAIN_CA, NULL, 0, 0, chainDer, leafSz, now, NULL), + WOLFSSL_SUCCESS); + ExpectIntEQ(test_ca_date_cm_verify(rootDer, rootSz, oldCaDer, oldCaSz, + WOLFSSL_CHAIN_CA, NULL, 0, 0, chainDer, leafSz, later, NULL), + WC_NO_ERR_TRACE(ASN_AFTER_DATE_E)); +#ifndef NO_WOLFSSL_CM_VERIFY + ExpectIntEQ(test_ca_date_cm_verify(rootDer, rootSz, oldCaDer, oldCaSz, + WOLFSSL_CHAIN_CA, NULL, 0, 0, earlyDer, earlySz, later, + test_ca_date_accept_early_cb), WC_NO_ERR_TRACE(ASN_AFTER_DATE_E)); +#endif + ExpectIntEQ(test_ca_date_cm_verify(rootDer, rootSz, oldCaDer, oldCaSz, + WOLFSSL_CHAIN_CA, subDer, subSz, WOLFSSL_USER_CA, subLeafDer, + subLeafSz, later, NULL), WOLFSSL_SUCCESS); +#endif + ExpectIntEQ(test_ca_date_cm_verify(rootDer, rootSz, oldCaDer, oldCaSz, + WOLFSSL_USER_CA, NULL, 0, 0, chainDer, leafSz, later, NULL), + WOLFSSL_SUCCESS); + + if (rngInit) wc_FreeRng(&rng); + if (caInit) wc_FreeRsaKey(&caKey); + if (intInit) wc_FreeRsaKey(&intKey); + if (leafInit) wc_FreeRsaKey(&leafKey); + XFREE(oldCaDer, NULL, DYNAMIC_TYPE_TMP_BUFFER); + XFREE(newCaDer, NULL, DYNAMIC_TYPE_TMP_BUFFER); + XFREE(chainDer, NULL, DYNAMIC_TYPE_TMP_BUFFER); + XFREE(earlyDer, NULL, DYNAMIC_TYPE_TMP_BUFFER); + XFREE(selfDer, NULL, DYNAMIC_TYPE_TMP_BUFFER); + XFREE(noAkidDer, NULL, DYNAMIC_TYPE_TMP_BUFFER); + XFREE(subDer, NULL, DYNAMIC_TYPE_TMP_BUFFER); + XFREE(subLeafDer, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif + return EXPECT_RESULT(); +} + + #if defined(WOLFSSL_SMALL_CERT_VERIFY) && \ defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_RSA) && \ !defined(NO_TLS) && !defined(NO_SHA256) && !defined(NO_ASN_TIME) && \ defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && \ diff --git a/tests/api/test_ssl_cert.h b/tests/api/test_ssl_cert.h index 14d4a2e59e8..b37d53f649e 100644 --- a/tests/api/test_ssl_cert.h +++ b/tests/api/test_ssl_cert.h @@ -77,6 +77,7 @@ int test_wolfSSL_verify_empty_server_cert(void); int test_wolfSSL_verify_post_handshake_defers(void); int test_wolfSSL_chain_ca_ext_key_usage(void); int test_wolfSSL_alt_chain_forged_ca(void); +int test_wolfSSL_chain_ca_cached_dates(void); int test_wolfSSL_small_cert_verify_sig_error(void); int test_wolfSSL_session_chain_oversized_cert(void); int test_wolfSSL_session_ticket_oversized_peer_cert(void); @@ -137,6 +138,7 @@ int test_wolfSSL_SESSION_get0_peer_full_chain(void); test_wolfSSL_verify_post_handshake_defers), \ TEST_DECL_GROUP("ssl_cert", test_wolfSSL_chain_ca_ext_key_usage), \ TEST_DECL_GROUP("ssl_cert", test_wolfSSL_alt_chain_forged_ca), \ + TEST_DECL_GROUP("ssl_cert", test_wolfSSL_chain_ca_cached_dates), \ TEST_DECL_GROUP("ssl_cert", \ test_wolfSSL_small_cert_verify_sig_error), \ TEST_DECL_GROUP("ssl_cert", \ diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index a66b07d1497..e1fca0430a2 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -25948,8 +25948,8 @@ Signer* findSignerByName(Signer *list, byte *hash) /* Find a signer for cert in cm and extraCAList. Prefers AKID->SKID * with name-hash validation. Fall back to name-only when AKID is * absent. */ -static Signer* FindSignerByAkidOrName(WOLFSSL_CERT_MANAGER* cm, - Signer* extraCAList, Signer* cert) +Signer* wolfssl_local_FindSignerByAkidOrName(WOLFSSL_CERT_MANAGER* cm, + Signer* extraCAList, Signer* cert) { Signer* signer = NULL; #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2 @@ -26976,7 +26976,8 @@ int ParseCertRelative(DecodedCert* cert, int type, int verify, /* Stop at trust anchor (self-signed). */ if (ncSigner->selfSigned) break; - ncParent = FindSignerByAkidOrName(cm, extraCAList, ncSigner); + ncParent = wolfssl_local_FindSignerByAkidOrName(cm, extraCAList, + ncSigner); /* Stop on missing parent, self-loop, or A->B->A cycle. */ if (ncParent == NULL || ncParent == ncSigner || ncParent == ncPrev) @@ -27008,6 +27009,31 @@ int ParseCertRelative(DecodedCert* cert, int type, int verify, return ret; } +#if !defined(NO_ASN_TIME_CHECK) && !defined(WOLFCRYPT_ONLY) +/* Copy the DER encoded validity dates of cert into validity. + * Returns 0, BAD_FUNC_ARG or ASN_DATE_SZ_E. */ +int wolfssl_local_SetSignerValidity(SignerValidity* validity, + const DecodedCert* cert) +{ + if (validity == NULL || cert == NULL || cert->beforeDate == NULL || + cert->afterDate == NULL) + return BAD_FUNC_ARG; + + if (cert->beforeDateLen <= 2 || + cert->beforeDateLen > (int)sizeof(validity->notBefore) || + cert->afterDateLen <= 2 || + cert->afterDateLen > (int)sizeof(validity->notAfter)) + return ASN_DATE_SZ_E; + + XMEMSET(validity, 0, sizeof(SignerValidity)); + XMEMCPY(validity->notBefore, cert->beforeDate, + (size_t)cert->beforeDateLen); + XMEMCPY(validity->notAfter, cert->afterDate, (size_t)cert->afterDateLen); + + return 0; +} +#endif /* !NO_ASN_TIME_CHECK && !WOLFCRYPT_ONLY */ + int FillSigner(Signer* signer, DecodedCert* cert, int type, DerBuffer *der) { int ret = 0; @@ -27015,6 +27041,21 @@ int FillSigner(Signer* signer, DecodedCert* cert, int type, DerBuffer *der) if (signer == NULL || cert == NULL) return BAD_FUNC_ARG; +#if !defined(NO_ASN_TIME_CHECK) && !defined(WOLFCRYPT_ONLY) + /* A CA from a peer's chain is not a trust anchor. Keep its dates so they + * can be checked again each time it is used. */ + if (type == WOLFSSL_CHAIN_CA) { + signer->validity = (SignerValidity*)XMALLOC(sizeof(SignerValidity), + cert->heap, DYNAMIC_TYPE_SIGNER); + if (signer->validity == NULL) { + ret = MEMORY_E; + } + else { + ret = wolfssl_local_SetSignerValidity(signer->validity, cert); + } + } +#endif + #ifdef WOLFSSL_DUAL_ALG_CERTS if (ret == 0 && signer != NULL) { if (cert->extSapkiSet && cert->sapkiLen > 0) { @@ -27156,6 +27197,9 @@ void FreeSigner(Signer* signer, void* heap) #endif #ifdef WOLFSSL_SIGNER_DER_CERT FreeDer(&signer->derCert); +#endif +#if !defined(NO_ASN_TIME_CHECK) && !defined(WOLFCRYPT_ONLY) + XFREE(signer->validity, heap, DYNAMIC_TYPE_SIGNER); #endif XFREE(signer, heap, DYNAMIC_TYPE_SIGNER); } diff --git a/wolfssl/internal.h b/wolfssl/internal.h index 22ba3e9ce3f..bdc4c5ddff2 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -3008,6 +3008,24 @@ WOLFSSL_LOCAL int CM_VerifyBuffer_ex(WOLFSSL_CERT_MANAGER* cm, const byte* buff, long sz, int format, int prev_err, int* crlErr); +#if !defined(NO_ASN_TIME_CHECK) && !defined(WOLFCRYPT_ONLY) && \ + !defined(NO_CERTS) + #define WOLFSSL_CHAIN_CA_DATES +#endif + +#ifdef WOLFSSL_CHAIN_CA_DATES +/* Dates of a CA cached from a peer's chain, decided in this handshake. A + * waived entry is a CA whose date error the verify callback accepted. Other + * entries hold the dates of a renewed CA, stored on the cached CA only when + * the handshake verifies the peer. */ +typedef struct ChainCADate { + struct ChainCADate* next; + byte hash[SIGNER_DIGEST_SIZE]; /* hash AddCA() files the CA under */ + byte keyHash[KEYID_SIZE]; /* CalcHashId() of its public key */ + SignerValidity validity; + byte waived; +} ChainCADate; +#endif #ifndef NO_CERTS #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH) @@ -5075,6 +5093,20 @@ int ProcessOldClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx, WOLFSSL_LOCAL int SetCAType(WOLFSSL_CERT_MANAGER* cm, byte* hash, int type); WOLFSSL_LOCAL int AlreadySigner(WOLFSSL_CERT_MANAGER* cm, byte* hash); +#ifdef WOLFSSL_CHAIN_CA_DATES + WOLFSSL_LOCAL int wolfssl_local_CheckChainCADates(WOLFSSL_CERT_MANAGER* cm, + Signer* ca, Signer* pending, const ChainCADate* dates); + WOLFSSL_LOCAL int wolfssl_local_SetChainCAValidity( + WOLFSSL_CERT_MANAGER* cm, const byte* hash, const byte* keyHash, + const SignerValidity* validity); + WOLFSSL_LOCAL int wolfssl_local_SetChainCADates(WOLFSSL_CERT_MANAGER* cm, + const byte* hash, const DecodedCert* cert); + WOLFSSL_LOCAL int wolfssl_local_IsChainCARenewal(WOLFSSL_CERT_MANAGER* cm, + const byte* hash, const byte* keyHash, const DecodedCert* cert, + const SignerValidity* validity); + WOLFSSL_LOCAL void wolfssl_local_CommitChainCADates(WOLFSSL* ssl); + WOLFSSL_LOCAL void wolfssl_local_FreeChainCADates(WOLFSSL* ssl); +#endif #ifdef WOLFSSL_TRUST_PEER_CERT WOLFSSL_LOCAL int AddTrustedPeer(WOLFSSL_CERT_MANAGER* cm, DerBuffer** pDer, int verify); @@ -7384,6 +7416,9 @@ struct WOLFSSL { #ifdef HAVE_ONE_TIME_AUTH OneTimeAuth auth; #endif +#ifdef WOLFSSL_CHAIN_CA_DATES + ChainCADate* chainCaDates; /* chain CA dates of this handshake */ +#endif #ifdef HAVE_TLS_EXTENSIONS TLSX* extensions; /* RFC 6066 TLS Extensions data */ #ifdef OPENSSL_EXTRA diff --git a/wolfssl/wolfcrypt/asn.h b/wolfssl/wolfcrypt/asn.h index d5f89a90f0e..915509e06ed 100644 --- a/wolfssl/wolfcrypt/asn.h +++ b/wolfssl/wolfcrypt/asn.h @@ -2366,6 +2366,16 @@ struct DecodedCert { /* Signer and CRL hashes come from CalcHashId*(). */ #define SIGNER_DIGEST_SIZE KEYID_SIZE +#if !defined(NO_ASN_TIME_CHECK) && !defined(WOLFCRYPT_ONLY) +/* Validity of a CA cached from a peer's chain, DER encoded: tag, length and + * date. Such a CA is not a trust anchor, so it is checked whenever it is used + * to verify a certificate. */ +typedef struct SignerValidity { + byte notBefore[MAX_DATE_SIZE + 2]; + byte notAfter[MAX_DATE_SIZE + 2]; +} SignerValidity; +#endif + /* CA Signers */ /* if change layout change PERSIST_CERT_CACHE functions too */ struct Signer { @@ -2437,6 +2447,9 @@ struct Signer { #ifdef WOLFSSL_SIGNER_DER_CERT DerBuffer* derCert; #endif +#if !defined(NO_ASN_TIME_CHECK) && !defined(WOLFCRYPT_ONLY) + SignerValidity* validity; /* dates of a WOLFSSL_CHAIN_CA */ +#endif #if defined(WOLFSSL_RENESAS_TSIP_TLS) || defined(WOLFSSL_RENESAS_FSPSM_TLS) word32 cm_idx; #endif @@ -2683,6 +2696,14 @@ WOLFSSL_LOCAL int wc_GetPubX509(DecodedCert* cert, int verify, int* badDate); WOLFSSL_LOCAL const byte* OidFromId(word32 id, word32 type, word32* oidSz); WOLFSSL_LOCAL Signer* findSignerByKeyHash(Signer *list, byte *hash); WOLFSSL_LOCAL Signer* findSignerByName(Signer *list, byte *hash); +#ifndef IGNORE_NAME_CONSTRAINTS +WOLFSSL_LOCAL Signer* wolfssl_local_FindSignerByAkidOrName( + WOLFSSL_CERT_MANAGER* cm, Signer* extraCAList, Signer* cert); +#endif +#if !defined(NO_ASN_TIME_CHECK) && !defined(WOLFCRYPT_ONLY) +WOLFSSL_LOCAL int wolfssl_local_SetSignerValidity(SignerValidity* validity, + const DecodedCert* cert); +#endif WOLFSSL_TEST_VIS int FillSigner(Signer* signer, DecodedCert* cert, int type, DerBuffer *der); WOLFSSL_TEST_VIS Signer* MakeSigner(void* heap); WOLFSSL_TEST_VIS void FreeSigner(Signer* signer, void* heap);