diff --git a/src/internal.c b/src/internal.c index f1d5009e31f..f93f8801d38 100644 --- a/src/internal.c +++ b/src/internal.c @@ -17532,6 +17532,15 @@ void SslWaivedCAsFree(WOLFSSL* ssl) ssl->waivedCAs = waived->next; FreeSigner(waived, SSL_CM(ssl)->heap); } +#ifndef IGNORE_KEY_EXTENSIONS + while (ssl->ekuWaived != NULL) { + struct EkuWaivedCa* waived = ssl->ekuWaived; + + ssl->ekuWaived = waived->next; + XFREE(waived, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); + } + ssl->ekuIssuerRet = 0; +#endif } #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH) @@ -19227,6 +19236,228 @@ static int CheckChainCAExtKeyUsage(const WOLFSSL* ssl, const DecodedCert* cert) return 0; } + +/* Record the CA whose Extended Key Usage error the verify callback accepted. + * The later issuer walk matches the subject name, the subject key id and the + * public key. A different certificate that reuses the key, including one + * with a different subject, is not waived. The certificate fingerprint is + * recorded too, and is required when the signer still holds the certificate. + * Returns 0, or MEMORY_E. */ +static int RememberWaivedCaEku(WOLFSSL* ssl, const DecodedCert* cert) +{ + struct EkuWaivedCa* entry; + int ret; + + if (cert == NULL || cert->publicKey == NULL) + return 0; + + entry = (struct EkuWaivedCa*)XMALLOC(sizeof(struct EkuWaivedCa), ssl->heap, + DYNAMIC_TYPE_TMP_BUFFER); + if (entry == NULL) + return MEMORY_E; + XMEMSET(entry, 0, sizeof(*entry)); +#ifndef NO_SKID + XMEMCPY(entry->hash, cert->extSubjKeyId, SIGNER_DIGEST_SIZE); +#else + XMEMCPY(entry->hash, cert->subjectHash, SIGNER_DIGEST_SIZE); +#endif + XMEMCPY(entry->nameHash, cert->subjectHash, SIGNER_DIGEST_SIZE); + ret = CalcHashId(cert->publicKey, cert->pubKeySize, entry->keyHash); + if (ret != 0) { + XFREE(entry, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); + return ret; + } + /* The decoded source is the certificate DER. The fingerprint is recorded + * whether or not the signer keeps that encoding. A signer that kept it + * is matched on the fingerprint, which separates two certificates that + * share a subject and a key. */ + if (cert->source != NULL && cert->maxIdx > 0) { + ret = CalcHashId(cert->source, cert->maxIdx, entry->certHash); + if (ret != 0) { + XFREE(entry, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER); + return ret; + } + entry->haveCertHash = 1; + } + entry->next = ssl->ekuWaived; + ssl->ekuWaived = entry; + return 0; +} + +/* 1 when this handshake already accepted ca's purpose error. */ +static int EkuCaWasWaived(const WOLFSSL* ssl, const Signer* ca) +{ + const struct EkuWaivedCa* entry; + byte keyHash[KEYID_SIZE]; + + if (ca == NULL || ca->publicKey == NULL) + return 0; + if (CalcHashId(ca->publicKey, ca->pubKeySize, keyHash) != 0) + return 0; + for (entry = ssl->ekuWaived; entry != NULL; entry = entry->next) { + if (XMEMCMP(entry->keyHash, keyHash, KEYID_SIZE) != 0) + continue; + if (XMEMCMP(entry->nameHash, ca->subjectNameHash, + SIGNER_DIGEST_SIZE) != 0) + continue; +#ifndef NO_SKID + if (XMEMCMP(entry->hash, ca->subjectKeyIdHash, + SIGNER_DIGEST_SIZE) != 0) + continue; +#else + if (XMEMCMP(entry->hash, ca->subjectNameHash, + SIGNER_DIGEST_SIZE) != 0) + continue; +#endif +#ifdef WOLFSSL_SIGNER_DER_CERT + /* The signer kept the certificate, so the fingerprint decides. + * Subject, key and subject key id are not enough: a different + * certificate can share all three. */ + if (entry->haveCertHash) { + byte certHash[KEYID_SIZE]; + + if (ca->derCert == NULL || ca->derCert->buffer == NULL || + ca->derCert->length == 0) + continue; + if (CalcHashId(ca->derCert->buffer, ca->derCert->length, + certHash) != 0) + continue; + if (XMEMCMP(entry->certHash, certHash, KEYID_SIZE) != 0) + continue; + } +#endif + return 1; + } + return 0; +} + +/* A CA the application loaded is the trust anchor when it is self-signed, or + * when its authority key id names its own subject key id. CA_TYPE parsing + * skips the signature check for that second shape, so selfSigned stays clear + * and akidNamesSelf records it. That bit does not depend on name constraints + * or on a stored key id, so the exemption holds in every build. + * RFC 5280 6.1 keeps the trust anchor outside the certification path. */ +static int SignerIsTrustAnchor(const Signer* ca) +{ + if (ca == NULL || ca->type != WOLFSSL_USER_CA) + return 0; + if (ca->selfSigned || ca->akidNamesSelf) + return 1; + return 0; +} + +/* EXTKEYUSE_AUTH_E when ca's Extended Key Usage excludes the TLS purpose. + * A waived CA and a trust anchor are allowed. */ +static int SignerPurposeRejects(const WOLFSSL* ssl, const Signer* ca, + int waived) +{ + byte purpose; + + if (SignerIsTrustAnchor(ca) || waived || !ca->extExtKeyUsageSet) + return 0; + if (ssl->options.side == WOLFSSL_CLIENT_END) + purpose = EXTKEYUSE_SERVER_AUTH; + else + purpose = EXTKEYUSE_CLIENT_AUTH; + if ((ca->extKeyUsage & (EXTKEYUSE_ANY | purpose)) == 0) { + WOLFSSL_MSG("Issuer CA ExtKeyUse doesn't allow TLS peer auth"); + return EXTKEYUSE_AUTH_E; + } + return 0; +} + +/* ca is a peer-sent self-signed certificate, so it is not the trust anchor + * by itself. Another CertManager signer with the same public key may be. + * Returns 1 when that signer is a trust anchor, EXTKEYUSE_AUTH_E when it is + * a restricted CA, 0 when there is no such signer, and BAD_MUTEX_E when the + * CA lock cannot be taken. */ +static int SameKeySignerPurpose(const WOLFSSL* ssl, const Signer* ca) +{ + WOLFSSL_CERT_MANAGER* cm = SSL_CM(ssl); + int row; + int reject = 0; + int anchor = 0; + + if (ca->publicKey == NULL) + return 0; + if (wc_LockMutex(&cm->caLock) != 0) + return BAD_MUTEX_E; + for (row = 0; row < CA_TABLE_SIZE && !anchor; row++) { + Signer* s; + + for (s = cm->caTable[row]; s != NULL; s = s->next) { + if (s == ca || s->publicKey == NULL || + s->pubKeySize != ca->pubKeySize || + XMEMCMP(s->publicKey, ca->publicKey, ca->pubKeySize) != 0) + continue; + if (SignerIsTrustAnchor(s)) { + anchor = 1; + reject = 0; + break; + } + if (reject == 0) + reject = SignerPurposeRejects(ssl, s, EkuCaWasWaived(ssl, s)); + } + } + wc_UnLockMutex(&cm->caLock); + if (anchor) + return 1; + return reject; +} + +/* Check the Extended Key Usage of each CA from ca up to the trust anchor + * against the TLS purpose. A CA whose purpose error this handshake already + * accepted is skipped, and the walk continues. A peer-sent self-signed + * certificate is not treated as the trust anchor: the CertManager CA that + * holds the same key is checked instead. With IGNORE_NAME_CONSTRAINTS there + * is no issuer lookup, so only ca is checked. pending holds Certificate + * Status Request v2 signers and callback-accepted CAs that are not in the + * certificate manager yet. + * Returns 0, EXTKEYUSE_AUTH_E, BAD_MUTEX_E or MAX_CHAIN_ERROR. */ +static int CheckSignerExtKeyUsage(const WOLFSSL* ssl, Signer* ca, + Signer* pending) +{ + Signer* prev = NULL; + Signer* parent; + int depth = 0; + + while (ca != NULL) { + int rejected; + + if (SignerIsTrustAnchor(ca)) + break; + if (ca->selfSigned) { + int same = SameKeySignerPurpose(ssl, ca); + + if (same == 1) + break; + if (same != 0) + return same; + rejected = SignerPurposeRejects(ssl, ca, EkuCaWasWaived(ssl, ca)); + if (rejected != 0) + return rejected; + break; + } + rejected = SignerPurposeRejects(ssl, ca, EkuCaWasWaived(ssl, ca)); + if (rejected != 0) + return rejected; + #ifndef IGNORE_NAME_CONSTRAINTS + parent = wolfssl_local_FindSignerByAkidOrName(SSL_CM(ssl), 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) + return MAX_CHAIN_ERROR; + prev = ca; + ca = parent; + } + + return 0; +} #endif /* IGNORE_KEY_EXTENSIONS */ #ifdef WOLFSSL_ALT_CERT_CHAINS @@ -20993,6 +21224,21 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, if (ret == 0 && args->dCert->ca != NULL && ProcessPeerCertIsWaivedCA(ssl, args->dCert->ca)) caIsTemp = 1; + #ifndef IGNORE_KEY_EXTENSIONS + /* The later issuer walk repeats this CA. Remember an + * accepted purpose error so that walk does not reject + * it again. Ancestors are still checked. The decoded + * certificate is allocated before this loop. */ + if (ret == 0 && + preCbRet == WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E) && + args->dCert->isCA) { + ret = RememberWaivedCaEku(ssl, args->dCert); + if (ret != 0) { + WOLFSSL_ERROR_VERBOSE(ret); + args->fatal = 1; + } + } + #endif if (CrlErrWaived(ssl, ret)) ret = ssl->error = 0; #if defined(HAVE_OCSP) && defined(HAVE_CRL) && \ @@ -21578,6 +21824,54 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, } } } + + /* CAs the peer did not send were not covered by + * CheckChainCAExtKeyUsage(). Chain CAs whose purpose error + * this handshake already accepted are skipped. A leaf error + * that is still pending does not skip this walk: the callback + * is asked about each error on its own. */ + if (!args->fatal && !ssl->options.verifyNone && + #ifdef WOLFSSL_TRUST_PEER_CERT + !args->haveTrustPeer && + #endif + !UsingChainVerifyCb(ssl)) { + Signer* ekuPending = NULL; + Signer* ekuTail = NULL; + int ekuRet; + + #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && \ + !defined(NO_TLS) + if (TLSX_CSR2_IsMulti(ssl->extensions)) { + ekuPending = TLSX_CSR2_GetPendingSigners( + ssl->extensions); + } + #endif + ekuPending = SslWaivedCAsJoin(ssl, ekuPending, &ekuTail); + ekuRet = CheckSignerExtKeyUsage(ssl, args->dCert->ca, + ekuPending); + SslWaivedCAsSplit(ekuTail); + if (ekuRet != 0) { + WOLFSSL_ERROR_VERBOSE(ekuRet); + #if defined(OPENSSL_EXTRA) || \ + defined(OPENSSL_EXTRA_X509_SMALL) + /* Return first cert error here */ + if (ssl->peerVerifyRet == 0) { + ssl->peerVerifyRet = + (ekuRet == WC_NO_ERR_TRACE(MAX_CHAIN_ERROR)) ? + WOLFSSL_X509_V_ERR_CERT_CHAIN_TOO_LONG : + WOLFSSL_X509_V_ERR_INVALID_PURPOSE; + } + #endif + /* Leave ret for the checks that still have to run + * when it is 0: the minimum key size, and the + * hostname and IP checks that replace ret. The + * deferred callback reports this error after those, + * and after any leaf error the application accepts. + * Writing it into ret here would skip the key-size + * check and let a later mismatch discard it. */ + ssl->ekuIssuerRet = ekuRet; + } + } #endif /* IGNORE_KEY_EXTENSIONS */ if (args->fatal) { @@ -21795,6 +22089,17 @@ int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx, #endif } +#ifndef IGNORE_KEY_EXTENSIONS + /* The leaf already had an error. Accepting that error must not + * hide a restricted issuer the walk found. */ + if (ret == 0 && ssl->ekuIssuerRet != 0) { + int ekuRet = ssl->ekuIssuerRet; + + ssl->ekuIssuerRet = 0; + ret = DoVerifyCallback(SSL_CM(ssl), ssl, ekuRet, args); + } +#endif + if (CrlErrWaived(ssl, ret)) ret = ssl->error = 0; diff --git a/tests/api/test_ssl_cert.c b/tests/api/test_ssl_cert.c index c90876733b1..b2929aeca2a 100644 --- a/tests/api/test_ssl_cert.c +++ b/tests/api/test_ssl_cert.c @@ -4091,12 +4091,17 @@ typedef struct test_eku_case { * depth 2 */ int overrideCb; /* verify callback installed on the verifying * side: 0 none, 1 accepts every error, - * 2 waives only the purpose error */ + * 2 waives only the purpose error, + * 3 waives the purpose error only at depth 1, + * 4 waives only a key-usage error, + * 5 waives only a hostname mismatch */ int verifyDepth; /* verify depth pinned on the verifying side, * 0 leaves the default in place */ int expectRet; /* expected handshake result */ const char* keyUsage; /* Key Usage on the CA under test, NULL uses * keyCertSign,cRLSign */ + int leafOnly; /* peer sends only the leaf; with extraLevel + * the middle CA is also loaded to verify */ } test_eku_case; /* Stand in for an application that knowingly accepts a rejected certificate. */ @@ -4123,6 +4128,50 @@ static int test_eku_purpose_only_cb(int preverify, return preverify; } +/* Accept a purpose error only for the intermediate at depth 1. A later report + * of the same error at depth 0, with the leaf as the current certificate, + * must not be required for the handshake to succeed. */ +static int test_eku_depth_purpose_cb(int preverify, + WOLFSSL_X509_STORE_CTX* store) +{ + if (preverify) + return 1; + if (store->error_depth == 1 && + (store->error == WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E) +#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) + || store->error == WOLFSSL_X509_V_ERR_INVALID_PURPOSE +#endif + )) { + return 1; + } + return 0; +} + +/* Accept a key-usage error and nothing else. A restricted issuer reported + * after that error must still fail the handshake. */ +static int test_eku_keyuse_only_cb(int preverify, + WOLFSSL_X509_STORE_CTX* store) +{ + if (store->error == WC_NO_ERR_TRACE(KEYUSE_SIGNATURE_E)) + return 1; + return preverify; +} + +/* Accept a hostname mismatch and nothing else. A restricted issuer reported + * after that mismatch must still fail the handshake. */ +static int test_eku_host_only_cb(int preverify, + WOLFSSL_X509_STORE_CTX* store) +{ + if (store->error == WC_NO_ERR_TRACE(DOMAIN_NAME_MISMATCH) +#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) + || store->error == WOLFSSL_X509_V_ERR_HOSTNAME_MISMATCH +#endif + ) { + return 1; + } + 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. A * NULL extKeyUsage omits the Extended Key Usage extension entirely. When @@ -4184,10 +4233,92 @@ static int test_eku_gen_ca(byte* out, int outMax, RsaKey* subjKey, return ret; } +#if defined(WOLFSSL_ALLOW_AKID_SKID_MATCH) && !defined(IGNORE_NAME_CONSTRAINTS) +/* Build a CA signed by issuerKey. The issuer name comes from nameDer and the + * authority key id comes from akidDer. Those two certificates differ, so the + * issuer name does not name the key that signed this certificate. With + * IGNORE_NAME_CONSTRAINTS the issuer walk checks only the direct issuer, so + * this certificate is not used. */ +static int test_eku_gen_ca_split_issuer(byte* out, int outMax, RsaKey* subjKey, + const byte* nameDer, int nameDerSz, const byte* akidDer, int akidDerSz, + RsaKey* issuerKey, WC_RNG* rng, const char* extKeyUsage, const char* cn) +{ + Cert cert; + int ret = 0; + + 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 && wc_SetKeyUsage(&cert, "keyCertSign,cRLSign") != 0) + ret = -1; + if (ret == 0 && extKeyUsage != NULL && + wc_SetExtKeyUsage(&cert, extKeyUsage) != 0) + ret = -1; + if (ret == 0 && wc_SetAuthKeyIdFromCert(&cert, akidDer, akidDerSz) != 0) + ret = -1; + if (ret == 0 && wc_SetIssuerBuffer(&cert, nameDer, nameDerSz) != 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, + issuerKey, NULL, rng); +#ifdef WOLFSSL_CERT_GEN_CACHE + wc_SetCert_Free(&cert); +#endif + return ret; +} +#endif /* WOLFSSL_ALLOW_AKID_SKID_MATCH && !IGNORE_NAME_CONSTRAINTS */ + +#if !defined(NO_SKID) && !defined(IGNORE_NAME_CONSTRAINTS) +/* Self-signed CA whose authority key id equals its subject key id. + * CA_TYPE parsing skips the signature check for that shape, so the signer + * is not marked self-signed. Returns the DER length, or < 0 on failure. */ +static int test_eku_gen_self_akid_ca(byte* out, int outMax, RsaKey* key, + WC_RNG* rng, const char* extKeyUsage, const char* cn) +{ + Cert cert; + int ret = 0; + + 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, key, NULL) != 0) + ret = -1; + if (ret == 0 && wc_SetAuthKeyIdFromPublicKey(&cert, key, NULL) != 0) + ret = -1; + if (ret == 0 && wc_SetKeyUsage(&cert, "keyCertSign,cRLSign") != 0) + ret = -1; + if (ret == 0 && extKeyUsage != NULL && + wc_SetExtKeyUsage(&cert, extKeyUsage) != 0) + ret = -1; + if (ret == 0) + ret = wc_MakeCert(&cert, out, (word32)outMax, key, NULL, rng); + if (ret >= 0) + ret = wc_SignCert(cert.bodySz, cert.sigType, out, (word32)outMax, + key, NULL, rng); +#ifdef WOLFSSL_CERT_GEN_CACHE + wc_SetCert_Free(&cert); +#endif + return ret; +} +#endif /* !NO_SKID && !IGNORE_NAME_CONSTRAINTS */ + /* Build a TLS leaf signed by the given CA. The leaf carries both serverAuth * and clientAuth so only the issuing CA's purpose is tested. */ static int test_eku_gen_leaf(byte* out, int outMax, RsaKey* leafKey, - const byte* issuerDer, int issuerDerSz, RsaKey* issuerKey, WC_RNG* rng) + const byte* issuerDer, int issuerDerSz, RsaKey* issuerKey, WC_RNG* rng, + const char* keyUsage) { Cert cert; int ret = 0; @@ -4203,8 +4334,8 @@ static int test_eku_gen_leaf(byte* out, int outMax, RsaKey* leafKey, ret = -1; if (ret == 0 && wc_SetAuthKeyIdFromCert(&cert, issuerDer, issuerDerSz) != 0) ret = -1; - if (ret == 0 && - wc_SetKeyUsage(&cert, "digitalSignature,keyEncipherment") != 0) + if (ret == 0 && wc_SetKeyUsage(&cert, (keyUsage != NULL) ? keyUsage : + "digitalSignature,keyEncipherment") != 0) ret = -1; if (ret == 0 && wc_SetExtKeyUsage(&cert, "serverAuth,clientAuth") != 0) ret = -1; @@ -4230,7 +4361,8 @@ static int test_eku_gen_leaf(byte* out, int outMax, RsaKey* leafKey, * through hsRet and verifyRet. */ static int test_eku_chain_handshake(const byte* chainDer, int chainSz, const byte* rootDer, int rootSz, const byte* pinnedDer, int pinnedSz, - const test_eku_case* tc, int* hsRet, long* verifyRet) + const byte* pinned2Der, int pinned2Sz, const test_eku_case* tc, + int* hsRet, long* verifyRet) { EXPECT_DECLS; WOLFSSL_CTX* ctx_c = NULL; @@ -4246,6 +4378,12 @@ static int test_eku_chain_handshake(const byte* chainDer, int chainSz, verifyCb = test_eku_override_cb; else if (tc->overrideCb == 2) verifyCb = test_eku_purpose_only_cb; + else if (tc->overrideCb == 3) + verifyCb = test_eku_depth_purpose_cb; + else if (tc->overrideCb == 4) + verifyCb = test_eku_keyuse_only_cb; + else if (tc->overrideCb == 5) + verifyCb = test_eku_host_only_cb; XMEMSET(&test_ctx, 0, sizeof(test_ctx)); /* The server always presents the generated chain so a single set of @@ -4281,11 +4419,21 @@ static int test_eku_chain_handshake(const byte* chainDer, int chainSz, if (tc->verifyDepth > 0) wolfSSL_set_verify_depth(verifySsl, tc->verifyDepth); #endif + if (tc->overrideCb == 5) { + /* The leaf common name is "EKU Leaf". This name does not match, so + * the hostname check fails before the deferred purpose error. */ + ExpectIntEQ(wolfSSL_check_domain_name(verifySsl, "wrong.example"), + WOLFSSL_SUCCESS); + } if (pinnedDer != NULL) { ExpectIntEQ(wolfSSL_CTX_load_verify_buffer(verifyCtx, pinnedDer, (long)pinnedSz, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS); } + if (pinned2Der != NULL) { + ExpectIntEQ(wolfSSL_CTX_load_verify_buffer(verifyCtx, pinned2Der, + (long)pinned2Sz, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS); + } /* The harness only reports pass or fail, so read the reason from the side * that did the verifying. */ @@ -4350,24 +4498,28 @@ static int test_eku_chain_case(const test_eku_fixture* f, } ExpectIntGT((leafSz = test_eku_gen_leaf(f->chainDer, TEST_EKU_CERT_BUF_SZ, - f->leafKey, leafIssuerDer, leafIssuerSz, leafIssuerKey, f->rng)), 0); + f->leafKey, leafIssuerDer, leafIssuerSz, leafIssuerKey, f->rng, + NULL)), 0); /* The chain buffer holds the leaf first, then its issuers in order. */ ExpectIntLE(leafSz + caSz + midSz, TEST_EKU_CERT_BUF_SZ); if (EXPECT_SUCCESS()) { chainSz = leafSz; - if (tc->extraLevel) { + if (tc->extraLevel && !tc->leafOnly) { XMEMCPY(f->chainDer + chainSz, f->midDer, (size_t)midSz); chainSz += midSz; } - XMEMCPY(f->chainDer + chainSz, f->interDer, (size_t)caSz); - chainSz += caSz; + if (!tc->leafOnly) { + XMEMCPY(f->chainDer + chainSz, f->interDer, (size_t)caSz); + chainSz += caSz; + } if (tc->selfSignedCa) { rootDer = f->interDer; rootSz = caSz; } ExpectIntEQ(test_eku_chain_handshake(f->chainDer, chainSz, - rootDer, rootSz, tc->pinCa ? f->interDer : NULL, caSz, tc, + rootDer, rootSz, tc->pinCa ? f->interDer : NULL, caSz, + (tc->leafOnly && tc->extraLevel) ? f->midDer : NULL, midSz, tc, &hsRet, &verifyRet), TEST_SUCCESS); } @@ -4408,54 +4560,84 @@ int test_wolfSSL_chain_ca_ext_key_usage(void) defined(USE_CERT_BUFFERS_2048) && !defined(IGNORE_KEY_EXTENSIONS) static const test_eku_case cases[] = { /* eku, selfSigned, selfIssued, clientPresents, pinCa, extraLevel, - * overrideCb, verifyDepth, expected handshake result, keyUsage. */ + * overrideCb, verifyDepth, expected handshake result, keyUsage, + * leafOnly. */ /* Server authentication: a CA restricted to code signing, or to * client authentication, must be refused. */ { "codeSigning", 0, 0, 0, 0, 0, 0, 0, WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E), - NULL }, + NULL, 0 }, { "clientAuth", 0, 0, 0, 0, 0, 0, 0, WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E), - NULL }, + NULL, 0 }, /* A CA that carries the purpose, or leaves it unrestricted, must not * be over-rejected. */ - { "serverAuth", 0, 0, 0, 0, 0, 0, 0, 0, NULL }, - { NULL, 0, 0, 0, 0, 0, 0, 0, 0, NULL }, - { "any", 0, 0, 0, 0, 0, 0, 0, 0, NULL }, + { "serverAuth", 0, 0, 0, 0, 0, 0, 0, 0, NULL, 0 }, + { NULL, 0, 0, 0, 0, 0, 0, 0, 0, NULL, 0 }, + { "any", 0, 0, 0, 0, 0, 0, 0, 0, NULL, 0 }, /* The rule applies to a CA the certificate manager already holds, not * only to one seen for the first time. */ { "codeSigning", 0, 0, 0, 1, 0, 0, 0, WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E), - NULL }, - { "serverAuth", 0, 0, 0, 1, 0, 0, 0, 0, NULL }, + NULL, 0 }, + { "serverAuth", 0, 0, 0, 1, 0, 0, 0, 0, NULL, 0 }, + /* The peer sends only the leaf, so the CA comes from the certificate + * manager. The result must match the case where the CA is sent. */ + { "codeSigning", 0, 0, 0, 1, 0, 0, 0, + WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E), + NULL, 1 }, + { "clientAuth", 0, 0, 0, 1, 0, 0, 0, + WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E), + NULL, 1 }, + { "serverAuth", 0, 0, 0, 1, 0, 0, 0, 0, NULL, 1 }, + { NULL, 0, 0, 0, 1, 0, 0, 0, 0, NULL, 1 }, + { "any", 0, 0, 0, 1, 0, 0, 0, 0, NULL, 1 }, + { "codeSigning", 0, 0, 0, 1, 0, 1, 0, 0, NULL, 1 }, + { "codeSigning", 0, 0, 0, 1, 0, 2, 0, 0, NULL, 1 }, + { "codeSigning", 1, 0, 0, 0, 0, 0, 0, 0, NULL, 1 }, +#ifndef IGNORE_NAME_CONSTRAINTS + /* The CA under test is above the middle CA and neither is sent. */ + { "codeSigning", 0, 0, 0, 1, 1, 0, 0, + WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E), + NULL, 1 }, + { "serverAuth", 0, 0, 0, 1, 1, 0, 0, 0, NULL, 1 }, +#endif /* A self-signed trust anchor is exempt: here it is the anchor test, * not the Extended Key Usage, that decides. */ - { "codeSigning", 1, 0, 0, 0, 0, 0, 0, 0, NULL }, + { "codeSigning", 1, 0, 0, 0, 0, 0, 0, 0, NULL, 0 }, /* A self-issued CA key rollover certificate is not an anchor. It * carries the anchor's issuer and subject names, so DecodedCert's * selfIssued flag is set, but it holds a different key and is signed * by the anchor. The Extended Key Usage must still be enforced. */ { "codeSigning", 0, 1, 0, 0, 0, 0, 0, WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E), - NULL }, - { "serverAuth", 0, 1, 0, 0, 0, 0, 0, 0, NULL }, + NULL, 0 }, + { "serverAuth", 0, 1, 0, 0, 0, 0, 0, 0, NULL, 0 }, /* Key Usage, not Extended Key Usage: the same rollover certificate * cannot sign the leaf when its Key Usage omits keyCertSign. */ #ifndef ALLOW_INVALID_CERTSIGN { "serverAuth", 0, 1, 0, 0, 0, 0, 0, WC_NO_ERR_TRACE(NOT_CA_ERROR), - "digitalSignature" }, + "digitalSignature", 0 }, #endif /* Enforcement is not limited to the CA directly above the leaf: here * the CA under test issues "EKU Middle CA", which issues the leaf. */ { "codeSigning", 0, 0, 0, 0, 1, 0, 0, WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E), - NULL }, - { "serverAuth", 0, 0, 0, 0, 1, 0, 0, 0, NULL }, + NULL, 0 }, + { "serverAuth", 0, 0, 0, 0, 1, 0, 0, 0, NULL, 0 }, /* An application that installs a verify callback keeps the last word, * as it does for every other chain error. */ - { "codeSigning", 0, 0, 0, 0, 0, 1, 0, 0, NULL }, + { "codeSigning", 0, 0, 0, 0, 0, 1, 0, 0, NULL, 0 }, + /* The purpose error is accepted only at depth 1, where the chain CA + * is presented. The issuer walk must not ask again at depth 0. */ + { "codeSigning", 0, 0, 0, 0, 0, 3, 0, 0, NULL, 0 }, + /* The same callback does not cover a CA the peer never sent. That + * error is reported for the leaf, at depth 0. */ + { "codeSigning", 0, 0, 0, 1, 0, 3, 0, + WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E), + NULL, 1 }, #ifdef OPENSSL_EXTRA /* The purpose check must not stand in for the checks that run before * it. With the depth limit pinned below the chain, a callback that @@ -4463,29 +4645,43 @@ int test_wolfSSL_chain_ca_ext_key_usage(void) * rejection, whether or not the CA under test carries the purpose. */ { "serverAuth", 0, 0, 0, 0, 1, 2, 1, WC_NO_ERR_TRACE(MAX_CHAIN_ERROR), - NULL }, + NULL, 0 }, { "codeSigning", 0, 0, 0, 0, 1, 2, 1, WC_NO_ERR_TRACE(MAX_CHAIN_ERROR), - NULL }, + NULL, 0 }, #endif #ifndef WOLFSSL_NO_CLIENT_AUTH /* Client authentication: the server applies the same rule with the * clientAuth purpose. */ { "codeSigning", 0, 0, 1, 0, 0, 0, 0, WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E), - NULL }, + NULL, 0 }, { "serverAuth", 0, 0, 1, 0, 0, 0, 0, WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E), - NULL }, - { "clientAuth", 0, 0, 1, 0, 0, 0, 0, 0, NULL }, - { NULL, 0, 0, 1, 0, 0, 0, 0, 0, NULL }, - { "any", 0, 0, 1, 0, 0, 0, 0, 0, NULL }, + NULL, 0 }, + { "clientAuth", 0, 0, 1, 0, 0, 0, 0, 0, NULL, 0 }, + { NULL, 0, 0, 1, 0, 0, 0, 0, 0, NULL, 0 }, + { "any", 0, 0, 1, 0, 0, 0, 0, 0, NULL, 0 }, + { "codeSigning", 0, 0, 1, 1, 0, 0, 0, + WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E), + NULL, 1 }, + { "serverAuth", 0, 0, 1, 1, 0, 0, 0, + WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E), + NULL, 1 }, + { "clientAuth", 0, 0, 1, 1, 0, 0, 0, 0, NULL, 1 }, + { NULL, 0, 0, 1, 1, 0, 0, 0, 0, NULL, 1 }, +#ifndef IGNORE_NAME_CONSTRAINTS + { "codeSigning", 0, 0, 1, 1, 1, 0, 0, + WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E), + NULL, 1 }, + { "clientAuth", 0, 0, 1, 1, 1, 0, 0, 0, NULL, 1 }, +#endif /* The rollover shape is rejected in the client authentication * direction too. */ { "serverAuth", 0, 1, 1, 0, 0, 0, 0, WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E), - NULL }, - { "clientAuth", 0, 1, 1, 0, 0, 0, 0, 0, NULL }, + NULL, 0 }, + { "clientAuth", 0, 1, 1, 0, 0, 0, 0, 0, NULL, 0 }, #endif }; test_eku_fixture fixture; @@ -4543,6 +4739,232 @@ int test_wolfSSL_chain_ca_ext_key_usage(void) ExpectIntEQ(test_eku_chain_case(&fixture, &cases[i]), TEST_SUCCESS); } + /* A purpose error accepted on a certificate that is not the issuer must + * not waive the CertManager CA that did issue the leaf. The peer sends + * the leaf and a codeSigning CA that reuses the issuer key. The real + * issuer is pinned, has a different subject, and is also codeSigning. */ + if (EXPECT_SUCCESS()) { + test_eku_case decoy; + int caSz = 0; + int decoySz = 0; + int leafSz = 0; + int chainSz = 0; + int hsRet = -1; + long verifyRet = -1; + + XMEMSET(&decoy, 0, sizeof(decoy)); + /* Accept a purpose error only at depth 1, which is the decoy. The + * decoy reuses the issuer key, so the subject key id matches the + * pinned CA. The subject name does not. The issuer walk reports the + * pinned CA at depth 0. */ + decoy.overrideCb = 3; + ExpectIntGT((caSz = test_eku_gen_ca(fixture.interDer, + TEST_EKU_CERT_BUF_SZ, fixture.intKey, ca_cert_der_2048, + (int)sizeof_ca_cert_der_2048, fixture.caKey, fixture.rng, + "codeSigning", "EKU Intermediate", 0, NULL)), 0); + ExpectIntGT((decoySz = test_eku_gen_ca(fixture.midDer, + TEST_EKU_CERT_BUF_SZ, fixture.intKey, ca_cert_der_2048, + (int)sizeof_ca_cert_der_2048, fixture.caKey, fixture.rng, + "codeSigning", "EKU Decoy", 0, NULL)), 0); + ExpectIntGT((leafSz = test_eku_gen_leaf(fixture.chainDer, + TEST_EKU_CERT_BUF_SZ, fixture.leafKey, fixture.interDer, caSz, + fixture.intKey, fixture.rng, NULL)), 0); + if (EXPECT_SUCCESS()) { + chainSz = leafSz; + XMEMCPY(fixture.chainDer + chainSz, fixture.midDer, + (size_t)decoySz); + chainSz += decoySz; + ExpectIntEQ(test_eku_chain_handshake(fixture.chainDer, chainSz, + ca_cert_der_2048, (int)sizeof_ca_cert_der_2048, + fixture.interDer, caSz, NULL, 0, &decoy, &hsRet, &verifyRet), + TEST_SUCCESS); + } + ExpectIntEQ(hsRet, WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E)); + } + + /* A leaf key-usage error must not skip the issuer purpose check. The + * callback accepts the key-usage error and rejects the purpose error. */ + if (EXPECT_SUCCESS()) { + test_eku_case leafKu; + int caSz = 0; + int leafSz = 0; + int chainSz = 0; + int hsRet = -1; + long verifyRet = -1; + + XMEMSET(&leafKu, 0, sizeof(leafKu)); + leafKu.overrideCb = 4; + leafKu.leafOnly = 1; + ExpectIntGT((caSz = test_eku_gen_ca(fixture.interDer, + TEST_EKU_CERT_BUF_SZ, fixture.intKey, ca_cert_der_2048, + (int)sizeof_ca_cert_der_2048, fixture.caKey, fixture.rng, + "codeSigning", "EKU Intermediate", 0, NULL)), 0); + ExpectIntGT((leafSz = test_eku_gen_leaf(fixture.chainDer, + TEST_EKU_CERT_BUF_SZ, fixture.leafKey, fixture.interDer, caSz, + fixture.intKey, fixture.rng, "keyEncipherment")), 0); + if (EXPECT_SUCCESS()) { + chainSz = leafSz; + ExpectIntEQ(test_eku_chain_handshake(fixture.chainDer, chainSz, + ca_cert_der_2048, (int)sizeof_ca_cert_der_2048, + fixture.interDer, caSz, NULL, 0, &leafKu, &hsRet, &verifyRet), + TEST_SUCCESS); + } + ExpectIntEQ(hsRet, WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E)); + } + + /* A hostname mismatch must not replace an issuer purpose error. The + * callback accepts only the mismatch. The pinned issuer is codeSigning + * and the peer sends only the leaf. */ + if (EXPECT_SUCCESS()) { + test_eku_case host; + int caSz = 0; + int leafSz = 0; + int chainSz = 0; + int hsRet = -1; + long verifyRet = -1; + + XMEMSET(&host, 0, sizeof(host)); + host.overrideCb = 5; + host.leafOnly = 1; + ExpectIntGT((caSz = test_eku_gen_ca(fixture.interDer, + TEST_EKU_CERT_BUF_SZ, fixture.intKey, ca_cert_der_2048, + (int)sizeof_ca_cert_der_2048, fixture.caKey, fixture.rng, + "codeSigning", "EKU Intermediate", 0, NULL)), 0); + ExpectIntGT((leafSz = test_eku_gen_leaf(fixture.chainDer, + TEST_EKU_CERT_BUF_SZ, fixture.leafKey, fixture.interDer, caSz, + fixture.intKey, fixture.rng, NULL)), 0); + if (EXPECT_SUCCESS()) { + chainSz = leafSz; + ExpectIntEQ(test_eku_chain_handshake(fixture.chainDer, chainSz, + ca_cert_der_2048, (int)sizeof_ca_cert_der_2048, + fixture.interDer, caSz, NULL, 0, &host, &hsRet, &verifyRet), + TEST_SUCCESS); + } + ExpectIntEQ(hsRet, WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E)); + } + +#if !defined(NO_SKID) && !defined(IGNORE_NAME_CONSTRAINTS) + /* A trusted root whose authority key id equals its subject key id is not + * marked self-signed. It is still the trust anchor, so its Extended Key + * Usage does not reject the chain. The peer sends the leaf and the + * intermediate. The root stays in the certificate manager. */ + if (EXPECT_SUCCESS()) { + test_eku_case akidRoot; + int rootGenSz = 0; + int caSz = 0; + int leafSz = 0; + int chainSz = 0; + int hsRet = -1; + long verifyRet = -1; + + XMEMSET(&akidRoot, 0, sizeof(akidRoot)); + ExpectIntGT((rootGenSz = test_eku_gen_self_akid_ca(fixture.midDer, + TEST_EKU_CERT_BUF_SZ, fixture.caKey, fixture.rng, "codeSigning", + "EKU AKID Root")), 0); + ExpectIntGT((caSz = test_eku_gen_ca(fixture.interDer, + TEST_EKU_CERT_BUF_SZ, fixture.intKey, fixture.midDer, rootGenSz, + fixture.caKey, fixture.rng, "serverAuth", "EKU Intermediate", 0, + NULL)), 0); + ExpectIntGT((leafSz = test_eku_gen_leaf(fixture.chainDer, + TEST_EKU_CERT_BUF_SZ, fixture.leafKey, fixture.interDer, caSz, + fixture.intKey, fixture.rng, NULL)), 0); + if (EXPECT_SUCCESS()) { + chainSz = leafSz; + XMEMCPY(fixture.chainDer + chainSz, fixture.interDer, (size_t)caSz); + chainSz += caSz; + ExpectIntEQ(test_eku_chain_handshake(fixture.chainDer, chainSz, + fixture.midDer, rootGenSz, NULL, 0, NULL, 0, &akidRoot, + &hsRet, &verifyRet), TEST_SUCCESS); + } + ExpectIntEQ(hsRet, 0); + } +#endif + +#ifdef WOLFSSL_SIGNER_DER_CERT + /* A purpose error accepted for one certificate must not waive a different + * certificate that has the same subject, the same key and the same + * subject key id. The two differ by serial. The signer keeps the DER, so + * the fingerprint separates them. */ + if (EXPECT_SUCCESS()) { + test_eku_case same; + int caSz = 0; + int decoySz = 0; + int leafSz = 0; + int chainSz = 0; + int hsRet = -1; + long verifyRet = -1; + + XMEMSET(&same, 0, sizeof(same)); + same.overrideCb = 3; + ExpectIntGT((caSz = test_eku_gen_ca(fixture.interDer, + TEST_EKU_CERT_BUF_SZ, fixture.intKey, ca_cert_der_2048, + (int)sizeof_ca_cert_der_2048, fixture.caKey, fixture.rng, + "codeSigning", "EKU Same", 0, NULL)), 0); + ExpectIntGT((decoySz = test_eku_gen_ca(fixture.midDer, + TEST_EKU_CERT_BUF_SZ, fixture.intKey, ca_cert_der_2048, + (int)sizeof_ca_cert_der_2048, fixture.caKey, fixture.rng, + "codeSigning", "EKU Same", 0, NULL)), 0); + if (caSz == decoySz) { + ExpectIntNE(XMEMCMP(fixture.interDer, fixture.midDer, + (size_t)caSz), 0); + } + ExpectIntGT((leafSz = test_eku_gen_leaf(fixture.chainDer, + TEST_EKU_CERT_BUF_SZ, fixture.leafKey, fixture.interDer, caSz, + fixture.intKey, fixture.rng, NULL)), 0); + if (EXPECT_SUCCESS()) { + chainSz = leafSz; + XMEMCPY(fixture.chainDer + chainSz, fixture.midDer, + (size_t)decoySz); + chainSz += decoySz; + ExpectIntEQ(test_eku_chain_handshake(fixture.chainDer, chainSz, + ca_cert_der_2048, (int)sizeof_ca_cert_der_2048, + fixture.interDer, caSz, NULL, 0, &same, &hsRet, &verifyRet), + TEST_SUCCESS); + } + ExpectIntEQ(hsRet, WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E)); + } +#endif + +#if defined(WOLFSSL_ALLOW_AKID_SKID_MATCH) && !defined(IGNORE_NAME_CONSTRAINTS) + /* The middle CA names the root as its issuer and carries the parent's + * subject key id. The parent is codeSigning. The walk must follow that + * key id and reject the parent. IGNORE_NAME_CONSTRAINTS checks only the + * direct issuer, which allows both TLS purposes, so this case does not + * run in that build. */ + if (EXPECT_SUCCESS()) { + test_eku_case akid; + int parentSz = 0; + int midSz = 0; + int leafSz = 0; + int chainSz = 0; + int hsRet = -1; + long verifyRet = -1; + + XMEMSET(&akid, 0, sizeof(akid)); + akid.leafOnly = 1; + ExpectIntGT((parentSz = test_eku_gen_ca(fixture.interDer, + TEST_EKU_CERT_BUF_SZ, fixture.intKey, ca_cert_der_2048, + (int)sizeof_ca_cert_der_2048, fixture.caKey, fixture.rng, + "codeSigning", "EKU Intermediate", 0, NULL)), 0); + ExpectIntGT((midSz = test_eku_gen_ca_split_issuer(fixture.midDer, + TEST_EKU_CERT_BUF_SZ, fixture.midKey, ca_cert_der_2048, + (int)sizeof_ca_cert_der_2048, fixture.interDer, parentSz, + fixture.intKey, fixture.rng, "serverAuth,clientAuth", + "EKU Middle CA")), 0); + ExpectIntGT((leafSz = test_eku_gen_leaf(fixture.chainDer, + TEST_EKU_CERT_BUF_SZ, fixture.leafKey, fixture.midDer, midSz, + fixture.midKey, fixture.rng, NULL)), 0); + if (EXPECT_SUCCESS()) { + chainSz = leafSz; + ExpectIntEQ(test_eku_chain_handshake(fixture.chainDer, chainSz, + ca_cert_der_2048, (int)sizeof_ca_cert_der_2048, + fixture.interDer, parentSz, fixture.midDer, midSz, &akid, + &hsRet, &verifyRet), TEST_SUCCESS); + } + ExpectIntEQ(hsRet, WC_NO_ERR_TRACE(EXTKEYUSE_AUTH_E)); + } +#endif + if (rngInit) wc_FreeRng(&rng); if (caInit) wc_FreeRsaKey(&caKey); if (midInit) wc_FreeRsaKey(&midKey); diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index 6fa2b4c954d..7e2ddd717f2 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -26242,8 +26242,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; #ifndef NO_SKID @@ -26254,6 +26254,10 @@ static Signer* FindSignerByAkidOrName(WOLFSSL_CERT_MANAGER* cm, if (cert->authKeyIdSet) { signer = GetCA(cm, cert->authKeyIdHash); #ifndef WOLFSSL_ALLOW_AKID_SKID_MATCH + /* A different issuer name drops the subject-key-id hit. With + * WOLFSSL_ALLOW_AKID_SKID_MATCH, ParseCertRelative keeps this CA, + * and the Extended Key Usage walk must keep it too. Otherwise the + * walk stops before a restricted parent. */ if (signer != NULL && XMEMCMP(signer->subjectNameHash, cert->issuerNameHash, SIGNER_DIGEST_SIZE) != 0) { @@ -26315,7 +26319,7 @@ static int SignerFromPeerChain(const Signer* signer) * it. One cached from a peer's chain is not a trust anchor. */ static int SignerIsTrustAnchor(const Signer* signer) { - if (!signer->selfSigned) { + if (!signer->selfSigned && !signer->akidNamesSelf) { return 0; } #ifndef WOLFCRYPT_ONLY @@ -27310,6 +27314,17 @@ int ParseCertRelative(DecodedCert* cert, int type, int verify, } cert->selfSigned = (sigRet == 0); } + /* CA_TYPE with AKID equal to SKID and keyCertSign skips the signature + * check above. The trust anchor is still this certificate. The bit does + * not claim the signature was verified. */ + else if (cert->selfIssued && verify != VERIFY_NAME && + type == CA_TYPE && cert->extAuthKeyIdSet && + XMEMCMP(cert->extAuthKeyId, cert->extSubjKeyId, + KEYID_SIZE) == 0 && + !(cert->extKeyUsageSet && + (cert->extKeyUsage & KEYUSE_KEY_CERT_SIGN) == 0)) { + cert->akidNamesSelf = 1; + } #ifndef IGNORE_NAME_CONSTRAINTS /* Apply each ancestor CA's name constraints to this cert. @@ -27328,7 +27343,8 @@ int ParseCertRelative(DecodedCert* cert, int type, int verify, /* Stop at trust anchor. */ if (SignerIsTrustAnchor(ncSigner)) 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) { @@ -27421,6 +27437,8 @@ int FillSigner(Signer* signer, DecodedCert* cert, int type, DerBuffer *der) } signer->maxPathLen = cert->maxPathLen; signer->selfSigned = cert->selfSigned; + signer->extExtKeyUsageSet = cert->extExtKeyUsageSet; + signer->akidNamesSelf = cert->akidNamesSelf; #ifndef IGNORE_NAME_CONSTRAINTS signer->permittedNames = cert->permittedNames; signer->excludedNames = cert->excludedNames; diff --git a/wolfssl/internal.h b/wolfssl/internal.h index ce1c784b53f..6691c61fdb8 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -7070,6 +7070,25 @@ enum Tls13KdfSendStep { }; +#if !defined(NO_CERTS) && !defined(IGNORE_KEY_EXTENSIONS) +/* One CA whose Extended Key Usage error this handshake accepted. The issuer + * walk matches the subject name, the subject key id and the public key, so + * a different certificate that reuses the key is not waived. The certificate + * fingerprint is kept for every build. It is compared when the signer still + * holds the certificate. A build that does not keep that encoding cannot + * tell two such certificates apart. + * SIGNER_DIGEST_SIZE and KEYID_SIZE come from the certificate ASN code, so a + * build with no certificates does not have this struct. */ +struct EkuWaivedCa { + byte hash[SIGNER_DIGEST_SIZE]; + byte nameHash[SIGNER_DIGEST_SIZE]; + byte keyHash[KEYID_SIZE]; + byte certHash[KEYID_SIZE]; + byte haveCertHash; + struct EkuWaivedCa* next; +}; +#endif + struct WOLFSSL { WOLFSSL_CTX* ctx; #if defined(WOLFSSL_HAPROXY) @@ -7400,6 +7419,12 @@ struct WOLFSSL { #endif #ifndef NO_CERTS Signer* waivedCAs; /* callback-accepted chain CAs */ +#ifndef IGNORE_KEY_EXTENSIONS + struct EkuWaivedCa* ekuWaived; /* purpose errors this handshake + * accepted, by CA identity */ + int ekuIssuerRet; /* issuer purpose error still to + * report after a leaf error */ +#endif #endif #ifdef KEEP_PEER_CERT #ifdef WOLFSSL_DYNAMIC_PEER_CERT diff --git a/wolfssl/wolfcrypt/asn.h b/wolfssl/wolfcrypt/asn.h index 8063dda2d4e..ce106ea92dd 100644 --- a/wolfssl/wolfcrypt/asn.h +++ b/wolfssl/wolfcrypt/asn.h @@ -2261,6 +2261,7 @@ struct DecodedCert { WC_BITFIELD extNetscapeCertTypeSet:1; /* Netscape certificate type seen */ #endif WC_BITFIELD selfSigned:1; /* Own key verified the signature */ + WC_BITFIELD akidNamesSelf:1; /* CA whose AKID names its own SKID */ #if defined(WOLFSSL_SEP) || defined(WOLFSSL_CERT_EXT) WC_BITFIELD extCertPolicySet:1; #endif @@ -2379,6 +2380,9 @@ struct Signer { byte extKeyUsage; word16 maxPathLen; WC_BITFIELD selfSigned:1; /* Own key verified the signature */ + WC_BITFIELD extExtKeyUsageSet:1; /* EKU extension present */ + /* Same bitfield word as selfSigned, so sizeof(Signer) is unchanged. */ + WC_BITFIELD akidNamesSelf:1; /* AKID names this CA's own SKID */ #ifndef IGNORE_NAME_CONSTRAINTS /*! * \brief Mirrors DecodedCert::extNameConstraintCrit and @@ -2689,6 +2693,10 @@ 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 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);