From ce3e53b56cbfc1ad197757b4b87f47d07c8beffb Mon Sep 17 00:00:00 2001 From: John Safranek Date: Mon, 16 Nov 2026 09:31:12 -0800 Subject: [PATCH] tests: check ancestor name constraints on chains A leaf below a peer-supplied subordinate CA must still meet the name constraints of the CAs above that CA. The test verifies a three-cert chain under a constrained root as both server and client. It needs wolfSSL 5.9.4 or later, but not cert generation. - keys/nc-*.der: constrained root, CA excluding a subtree, CA, 3 leaves; gen-nameconstraints.sh regenerates them, configure links them - leaf in the permitted subtree verifies; leaves outside it and in the excluded subtree are rejected, alone and within the chain - wolfSSL must reject each bad leaf with ASN_NAME_INVALID_E, so a broken fixture cannot pass as a rejection - add WOLFSSL_V5_9_4 to internal.h Issue: F-13950 --- configure.ac | 8 +- keys/gen-nameconstraints.sh | 73 ++++++++++ keys/include.am | 3 + keys/nc-ca-a.der | Bin 0 -> 470 bytes keys/nc-ca-b.der | Bin 0 -> 444 bytes keys/nc-leaf-excl.der | Bin 0 -> 470 bytes keys/nc-leaf-in.der | Bin 0 -> 463 bytes keys/nc-leaf-out.der | Bin 0 -> 462 bytes keys/nc-root.der | Bin 0 -> 422 bytes tests/unit.c | 256 ++++++++++++++++++++++++++++++------ wolfssh/internal.h | 1 + 11 files changed, 300 insertions(+), 41 deletions(-) create mode 100755 keys/gen-nameconstraints.sh create mode 100644 keys/nc-ca-a.der create mode 100644 keys/nc-ca-b.der create mode 100644 keys/nc-leaf-excl.der create mode 100644 keys/nc-leaf-in.der create mode 100644 keys/nc-leaf-out.der create mode 100644 keys/nc-root.der diff --git a/configure.ac b/configure.ac index 42cf4b943..207e874fe 100644 --- a/configure.ac +++ b/configure.ac @@ -550,7 +550,13 @@ AC_CONFIG_LINKS([keys/gretel-key-rsa.pub:keys/gretel-key-rsa.pub keys/ca-key-ecc.der:keys/ca-key-ecc.der keys/server-cert.der:keys/server-cert.der keys/fred-cert.der:keys/fred-cert.der - keys/fred-key.der:keys/fred-key.der]) + keys/fred-key.der:keys/fred-key.der + keys/nc-root.der:keys/nc-root.der + keys/nc-ca-a.der:keys/nc-ca-a.der + keys/nc-ca-b.der:keys/nc-ca-b.der + keys/nc-leaf-in.der:keys/nc-leaf-in.der + keys/nc-leaf-out.der:keys/nc-leaf-out.der + keys/nc-leaf-excl.der:keys/nc-leaf-excl.der]) # Set the automake conditionals. AM_CONDITIONAL([BUILD_SERVER],[test "x$ENABLED_SERVER" = "xyes"]) diff --git a/keys/gen-nameconstraints.sh b/keys/gen-nameconstraints.sh new file mode 100755 index 000000000..aad6421df --- /dev/null +++ b/keys/gen-nameconstraints.sh @@ -0,0 +1,73 @@ +#!/bin/sh +# Generate the name-constraint test chain: nc-root (permitted DNS +# example.com) -> nc-ca-a (excluded DNS bad.example.com) -> nc-ca-b -> +# leaves. All ECDSA P-256, valid 100 years. +set -e +OUT=${1:-.} +cd "$OUT" +D=36500 +for n in root ca-a ca-b leaf-in leaf-out leaf-excl; do + openssl ecparam -name prime256v1 -genkey -noout -out nc-$n-key.pem +done +cat > nc-root.cnf < nc-$1.cnf < nc-$1.cnf <ItPqr+Ut*vj&TC|5XlQ6=Xl`t7Y8EBIZ)6A* zFfcNP3K(dh8X;*Q4lzO?IZ-!JFSQ~uw;(4~FF8NgAksjXjU8+?6C>0O%#7^JP7Ex+ zXY9F^#TO_&)B8=B+^^kNB&^KhH_QKgedtFWUv3DC$@kI6k?QM;I)xd1=du=P$%JW7VcT-VKprHm%pzeR)*y2CdHuJobDK{b*Iuq$yCJ)= z=(3lofg(tO9LPLz1JQ-T27*oeNr@>ao%&F; z8m>1|cjsD9k(*iNZF{3eok=0-bm@tciYJe>?d=!7FLqku{sm6$c?;J1ecU)9J!5nK Fd;rp}l}P{q literal 0 HcmV?d00001 diff --git a/keys/nc-ca-b.der b/keys/nc-ca-b.der new file mode 100644 index 0000000000000000000000000000000000000000..8df8dc3d2b6f624d3fbe471cab20f0ae89abc8b0 GIT binary patch literal 444 zcmXqLV%%ZS#2CAPnTe5!NhD1$syE(e3+KhmD%HZDTi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I z+>om2WFQSvAjl$OAk-v~kzZVb5LmXSvrHxhaVz;5OWqw_ba;o*5>16W5q&Fv{ZITZzS;U)`V)r(eEI-^ CER` z4MT1NPB!LH7B*p~&|pJJ191?CnMWWwQ8!61wIVUMASYEXIX~AxL7dmf%+S!#%+TD} z+|(>eg5SsxC}3b@3>7d?Mm0;sKnP+Mx1Y0uPikVCf`4g=L8O5&8#~x~CPt_`m>Job zofufo8d-8JbO^~RS*pqIq1*gIK0)MIxuWG^wzI1(<k?}tZlL3PPABe{f;sO22-e4dL;`6bH zv54F`wpVk)YWKT7E2Bgdb#oUtKWja0APX^HW!6E|LSe^7bc zkgDlqAPG_+$RcDQ(8QOKUtFS>T9%okSCU#>f*h#K9t;MqOo|Lk#6Ajd$~|AVAt6m- zcQT9Z4`cVf|16K`6UJl;=D#?hK7b_hUUiRre;wR{6>aA z0Rtmrr~qLjA`OJu*uge3F+#0oW@Kk}Vqme`W3@$K*Bkld?!GOnK8P)yo*X}Ih0oiL zrKi>k-qt)C?^wOkVfRXwKR$P#P2YCbPI>da&WA@ni%ZVfYZc-;Yu(eui3afo{6H7W z3NtePXJIv922x-z$nt{(fIehoGmr%-;A0VE5xM)k{@d2M%_oj)FW0Tzklk2x*~=6n zEeBFAY#_LR-+;G?JGCM)w;(4~FF8LKIkcGF84O&Q6hgLJsQ$iTwoFdVzW(kM*>3N$ zlX7d8|H%2s`0{t;e%A|33QBhmo@Cy@-K}b{qdo1hhxz^Vy_Mxc-|jCgKa`jB;lw)t DSn-L7 literal 0 HcmV?d00001 diff --git a/tests/unit.c b/tests/unit.c index 1344b12df..5cc0881e9 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -91,6 +91,17 @@ #define WOLFSSH_TEST_SET_CERTMAN #endif +/* The name constraints test reads only the P-256 fixture certs in ./keys, so + * it needs no cert generation. wolfSSL applies every ancestor's name + * constraints to a cert only from 5.9.4 (bc26ce05c); earlier releases check + * just the immediate signer's. */ +#if defined(WOLFSSH_TEST_INTERNAL) && defined(WOLFSSH_CERTS) && \ + !defined(NO_FILESYSTEM) && !defined(WOLFSSH_NO_ECDSA_SHA2_NISTP256) && \ + defined(WOLFSSL_V5_9_4) && (LIBWOLFSSL_VERSION_HEX >= WOLFSSL_V5_9_4) && \ + !defined(IGNORE_NAME_CONSTRAINTS) + #define WOLFSSH_TEST_CERTMAN_NAMECONSTRAINTS +#endif + #ifdef WOLFSSH_CERTS #include #include @@ -20011,11 +20022,13 @@ static int test_IdentifyAsn1Key(void) * WOLFSSH_TEST_INTERNAL section. Each carries its own feature guard; * WOLFSSH_TEST_CERTMAN_PROMOTE still implies WOLFSSH_TEST_INTERNAL. */ -/* Guard by the actual users, to avoid -Wunused-function: the promote tests, - * the root CA half of test_SetCertManager(), and test_ParseECCPubKeyCert(). +/* Guard by the actual users, to avoid -Wunused-function: the promote and + * name constraints tests, the root CA half of test_SetCertManager(), and + * test_ParseECCPubKeyCert(). * That last one needs WOLFSSH_TEST_INTERNAL, which ide/winvs does not * define, so it does not build there. */ #if defined(WOLFSSH_TEST_CERTMAN_PROMOTE) || \ + defined(WOLFSSH_TEST_CERTMAN_NAMECONSTRAINTS) || \ (defined(WOLFSSH_TEST_SET_CERTMAN) && \ defined(WOLFSSH_TEST_CERTMAN_ROOTCA)) || \ (defined(WOLFSSH_TEST_INTERNAL) && defined(WOLFSSH_CERTS) && \ @@ -20069,10 +20082,55 @@ static int certmanLoadFile(const char* fn, byte** buf, word32* bufSz) } #endif /* WOLFSSH_TEST_CERTMAN_PROMOTE || + * WOLFSSH_TEST_CERTMAN_NAMECONSTRAINTS || * (WOLFSSH_TEST_SET_CERTMAN && WOLFSSH_TEST_CERTMAN_ROOTCA) || * (WOLFSSH_TEST_INTERNAL && WOLFSSH_CERTS && !NO_FILESYSTEM && * !WOLFSSH_NO_ECDSA_SHA2_NISTP256) */ +#if defined(WOLFSSH_TEST_CERTMAN_PROMOTE) || \ + defined(WOLFSSH_TEST_CERTMAN_NAMECONSTRAINTS) + +static void certmanPutU32(byte* p, word32 v) +{ + p[0] = (byte)(v >> 24); + p[1] = (byte)(v >> 16); + p[2] = (byte)(v >> 8); + p[3] = (byte)(v); +} + +/* Append a length-prefixed cert (the framing VerifyCerts_buffer expects). + * chainCap is the capacity of chain; on insufficient space the chain is left + * unchanged so a cert-size regression surfaces as a failed assertion rather + * than memory corruption. The subtractions are ordered to avoid word32 + * underflow. */ +static word32 certmanAppendCert(byte* chain, word32 chainCap, word32 chainSz, + const byte* cert, word32 certSz) +{ + if (chainSz > chainCap || + certSz > chainCap - chainSz || + UINT32_SZ > chainCap - chainSz - certSz) { + return chainSz; + } + certmanPutU32(chain + chainSz, certSz); + chainSz += UINT32_SZ; + WMEMCPY(chain + chainSz, cert, certSz); + chainSz += certSz; + return chainSz; +} + +/* The FPKI profile check runs after the RFC 6187 usage check and never + * matches a test leaf, so in an FPKI build a leaf that passes the usage + * check surfaces as WS_CERT_PROFILE_E rather than WS_SUCCESS. Either way it + * is distinct from WS_CERT_KEY_USAGE_E. */ +#ifdef WOLFSSH_NO_FPKI + #define CERTMAN_LEAF_USAGE_OK WS_SUCCESS +#else + #define CERTMAN_LEAF_USAGE_OK WS_CERT_PROFILE_E +#endif + +#endif /* WOLFSSH_TEST_CERTMAN_PROMOTE || + * WOLFSSH_TEST_CERTMAN_NAMECONSTRAINTS */ + #ifdef WOLFSSH_TEST_CERTMAN_PROMOTE /* Forge an end-entity cert whose issuer is the supplied cert and which is @@ -20263,34 +20321,6 @@ static int certmanForgeCert(const char* cn, int isCA, const char* keyUsage, der, derSz); } -static void certmanPutU32(byte* p, word32 v) -{ - p[0] = (byte)(v >> 24); - p[1] = (byte)(v >> 16); - p[2] = (byte)(v >> 8); - p[3] = (byte)(v); -} - -/* Append a length-prefixed cert (the framing VerifyCerts_buffer expects). - * chainCap is the capacity of chain; on insufficient space the chain is left - * unchanged so a cert-size regression surfaces as a failed assertion rather - * than memory corruption. The subtractions are ordered to avoid word32 - * underflow. */ -static word32 certmanAppendCert(byte* chain, word32 chainCap, word32 chainSz, - const byte* cert, word32 certSz) -{ - if (chainSz > chainCap || - certSz > chainCap - chainSz || - UINT32_SZ > chainCap - chainSz - certSz) { - return chainSz; - } - certmanPutU32(chain + chainSz, certSz); - chainSz += UINT32_SZ; - WMEMCPY(chain + chainSz, cert, certSz); - chainSz += certSz; - return chainSz; -} - /* Regression test: a peer-supplied end-entity (non-CA) * certificate presented at an intermediate position in a chain must not be * promoted into the cert manager's trust store. If it were, the holder of any @@ -21308,16 +21338,6 @@ static int test_CertMan_AllocFailKeepsRoot(void) #ifdef WOLFSSL_CERT_EXT -/* The FPKI profile check runs after the RFC 6187 usage check and never - * matches a forged leaf, so in an FPKI build a leaf that passes the usage - * check surfaces as WS_CERT_PROFILE_E rather than WS_SUCCESS. Either way it - * is distinct from WS_CERT_KEY_USAGE_E. */ -#ifdef WOLFSSH_NO_FPKI - #define CERTMAN_LEAF_USAGE_OK WS_SUCCESS -#else - #define CERTMAN_LEAF_USAGE_OK WS_CERT_PROFILE_E -#endif - /* Forges a leaf signed by the test root carrying the given KeyUsage and * ExtendedKeyUsage (NULL omits the extension), verifies it through a manager * owned by a CTX of the given endpoint, or a standalone manager when side is @@ -21649,6 +21669,155 @@ static int test_CertMan_ProfileLeafNoSigner(void) #endif /* WOLFSSH_TEST_CERTMAN_PROMOTE */ +#ifdef WOLFSSH_TEST_CERTMAN_NAMECONSTRAINTS + +#define CERTMAN_NC_LEAF_IN 0 +#define CERTMAN_NC_LEAF_OUT 1 +#define CERTMAN_NC_LEAF_EXCL 2 + +/* Verifies leaf (one of CERTMAN_NC_LEAF_*) through a CTX of the given + * endpoint. The chain is [leaf, nc-ca-b, nc-ca-a] with only nc-root trusted. + * nc-root permits DNS example.com and nc-ca-a excludes bad.example.com; + * neither CA below them carries a constraint, so a leaf that breaks one is + * caught only by applying a constraint from an ancestor above the leaf's + * issuer. The leaf is then verified by itself against the intermediates the + * first pass left in the manager. + * + * wolfSSH reports a name constraint violation as the catch-all + * WS_CERT_OTHER_E, so the leaf is first verified with a wolfSSL cert manager + * holding the whole chain, which must return ASN_NAME_INVALID_E for a leaf + * outside the namespace. A broken fixture then fails rather than passing as + * a rejection. */ +static int certmanNameConstraintsCase(int side, int leaf) +{ + static const char* const leafFile[] = { + "./keys/nc-leaf-in.der", "./keys/nc-leaf-out.der", + "./keys/nc-leaf-excl.der" + }; + int result = 0; + int ret; + int expected; + byte* root = NULL; + byte* caA = NULL; + byte* caB = NULL; + byte* lf = NULL; + word32 rootSz = 0, caASz = 0, caBSz = 0, lfSz = 0; + byte chain[4096]; + word32 chainSz; + WOLFSSL_CERT_MANAGER* wcm = NULL; + WOLFSSH_CTX* ctx = NULL; + + if (certmanLoadFile("./keys/nc-root.der", &root, &rootSz) != 0 || + certmanLoadFile("./keys/nc-ca-a.der", &caA, &caASz) != 0 || + certmanLoadFile("./keys/nc-ca-b.der", &caB, &caBSz) != 0 || + certmanLoadFile(leafFile[leaf], &lf, &lfSz) != 0) { + printf("CertMan: can't load name constraint certs\n"); + result = -1000; goto done; + } + + wcm = wolfSSL_CertManagerNew(); + if (wcm == NULL || + wolfSSL_CertManagerLoadCABuffer(wcm, root, (long)rootSz, + WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS || + wolfSSL_CertManagerLoadCABuffer(wcm, caA, (long)caASz, + WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS || + wolfSSL_CertManagerLoadCABuffer(wcm, caB, (long)caBSz, + WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) { + printf("CertMan: can't load the name constraint CAs\n"); + result = -1006; goto done; + } + if (leaf == CERTMAN_NC_LEAF_IN) + expected = WOLFSSL_SUCCESS; + else + expected = ASN_NAME_INVALID_E; + ret = wolfSSL_CertManagerVerifyBuffer(wcm, lf, (long)lfSz, + WOLFSSL_FILETYPE_ASN1); + if (ret != expected) { + printf("CertMan: wolfSSL verify of name constraint leaf %d: got %d\n", + leaf, ret); + result = -1007; goto done; + } + + ctx = wolfSSH_CTX_new(side, NULL); + if (ctx == NULL || + wolfSSH_CTX_AddRootCert_buffer(ctx, root, rootSz, + WOLFSSH_FORMAT_ASN1) != WS_SUCCESS) { + result = -1001; goto done; + } + + chainSz = certmanAppendCert(chain, (word32)sizeof(chain), 0, lf, lfSz); + chainSz = certmanAppendCert(chain, (word32)sizeof(chain), chainSz, + caB, caBSz); + chainSz = certmanAppendCert(chain, (word32)sizeof(chain), chainSz, + caA, caASz); + ret = wolfSSH_CERTMAN_VerifyCerts_buffer(ctx->certMan, chain, chainSz, 3); + if (leaf == CERTMAN_NC_LEAF_IN) { + if (ret != CERTMAN_LEAF_USAGE_OK) { + printf("CertMan: in-namespace leaf, side %d: got %d\n", + side, ret); + result = -1002; goto done; + } + } + else if (ret != WS_CERT_OTHER_E) { + printf("CertMan: out-of-namespace leaf %d, side %d: got %d, " + "expected %d\n", leaf, side, ret, WS_CERT_OTHER_E); + result = -1003; goto done; + } + + chainSz = certmanAppendCert(chain, (word32)sizeof(chain), 0, lf, lfSz); + ret = wolfSSH_CERTMAN_VerifyCerts_buffer(ctx->certMan, chain, chainSz, 1); + if (leaf == CERTMAN_NC_LEAF_IN) { + if (ret != CERTMAN_LEAF_USAGE_OK) { + printf("CertMan: lone in-namespace leaf, side %d: got %d\n", + side, ret); + result = -1004; goto done; + } + } + else if (ret != WS_CERT_OTHER_E) { + printf("CertMan: lone out-of-namespace leaf %d, side %d: got %d, " + "expected %d\n", leaf, side, ret, WS_CERT_OTHER_E); + result = -1005; goto done; + } + +done: + if (ctx != NULL) + wolfSSH_CTX_free(ctx); + if (wcm != NULL) + wolfSSL_CertManagerFree(wcm); + free(root); + free(caA); + free(caB); + free(lf); + return result; +} + +/* Regression test for Fenrir 13950: the chain verifier validates each cert + * on its own, so a leaf below a peer-supplied subordinate CA must still meet + * the name constraints of the configured root and of the CA between them. + * Runs the permitted-subtree and excluded-subtree violations, and a leaf + * inside the namespace so the failures cannot be a broken chain, as both the + * server (user certs) and the client (host certs). */ +static int test_CertMan_NameConstraints(void) +{ + static const int sides[] = { + WOLFSSH_ENDPOINT_SERVER, WOLFSSH_ENDPOINT_CLIENT + }; + int result = 0; + word32 i; + int leaf; + + for (i = 0; result == 0 && i < sizeof(sides) / sizeof(sides[0]); i++) { + for (leaf = CERTMAN_NC_LEAF_IN; + result == 0 && leaf <= CERTMAN_NC_LEAF_EXCL; leaf++) { + result = certmanNameConstraintsCase(sides[i], leaf); + } + } + + return result; +} + +#endif /* WOLFSSH_TEST_CERTMAN_NAMECONSTRAINTS */ + #ifdef WOLFSSH_TEST_SET_CERTMAN /* wolfSSH_SetCertManager imports a WOLFSSL_CERT_MANAGER by reference into * the wolfSSH context. Test argument checking, importing the same manager @@ -30274,6 +30443,13 @@ int wolfSSH_UnitTest(int argc, char** argv) #endif #endif +#ifdef WOLFSSH_TEST_CERTMAN_NAMECONSTRAINTS + unitResult = test_CertMan_NameConstraints(); + printf("CertMan_NameConstraints: %s\n", + (unitResult == 0 ? "SUCCESS" : "FAILED")); + testResult = testResult || unitResult; +#endif + #ifdef WOLFSSH_KEYGEN #ifndef WOLFSSH_NO_RSA unitResult = test_RsaKeyGen(); diff --git a/wolfssh/internal.h b/wolfssh/internal.h index ee4b1d46d..6f48b1280 100644 --- a/wolfssh/internal.h +++ b/wolfssh/internal.h @@ -126,6 +126,7 @@ extern "C" { #define WOLFSSL_V5_8_0 0x05008000 #define WOLFSSL_V5_8_4 0x05008004 #define WOLFSSL_V5_9_2 0x05009002 +#define WOLFSSL_V5_9_4 0x05009004 /* wolfSSL 5.8.0 added the trusted-certificate PEM header, both the * TRUSTED_CERT_TYPE enum and PemToDer()'s fallback to it. */