Skip to content

Commit 33181a7

Browse files
alexclaude
andauthored
Reject critical nameConstraints extensions containing directoryName constraints (#15014)
* Handle directoryName name constraints without erroring Previously, evaluating a directoryName name constraint against a directoryName SAN always failed with a generic "unsupported name constraint" error. We still don't implement directoryName matching; instead, such a constraint now simply never matches: an excluded directoryName subtree never excludes anything, and a permitted directoryName subtree fails with a dedicated error message since no directoryName SAN can ever satisfy it. https://claude.ai/code/session_01LJgFCNp7irHqqfQ6jXmPhE * Apply directoryName name constraints to the subject field Per RFC 5280 4.2.1.10, directoryName name constraints apply to the subject field (when non-empty) in addition to directoryName SANs. Since we don't implement directoryName matching, a directoryName constraint that applies to a subsequent certificate with a non-empty subject is now rejected as unsupported, rather than silently ignored. A directoryName constraint paired with a directoryName SAN continues to be rejected as an unsupported name constraint. https://claude.ai/code/session_01LJgFCNp7irHqqfQ6jXmPhE * Reject critical nameConstraints containing directoryName constraints Instead of tracking subject DNs through the name chain, simply reject any chain where a critical nameConstraints extension contains a directoryName constraint. RFC 5280 4.2.1.10 requires directoryName constraints to be applied to the subject field (in addition to directoryName SANs), and requires applications that cannot process a constraint in a critical nameConstraints extension to reject the certificate. We don't implement directoryName matching, so the only conforming option for a critical extension is rejection. Non-critical extensions may be partially ignored, so their directoryName constraints fall through to the existing per-SAN evaluation, which continues to reject directoryName-vs-directoryName pairs as unsupported. https://claude.ai/code/session_01LJgFCNp7irHqqfQ6jXmPhE * Address review feedback - Drop the changelog entry - Reframe the comment around RFC 5280's subject-field requirement, which is what the per-SAN evaluation can't cover - Restructure the directoryName scan as two ||'d helper calls https://claude.ai/code/session_01LJgFCNp7irHqqfQ6jXmPhE --------- Co-authored-by: Claude <[email protected]>
1 parent 6080dc7 commit 33181a7

2 files changed

Lines changed: 182 additions & 2 deletions

File tree

‎src/rust/cryptography-x509-verification/src/lib.rs‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use asn1::ObjectIdentifier;
1919
use cryptography_x509::common::Asn1Read;
2020
use cryptography_x509::extensions::{
2121
AuthorityKeyIdentifier, DuplicateExtensionsError, Extensions, NameConstraints,
22-
SubjectAlternativeName,
22+
SequenceOfSubtrees, SubjectAlternativeName,
2323
};
2424
use cryptography_x509::name::GeneralName;
2525
use cryptography_x509::oid::{
@@ -424,7 +424,38 @@ impl<'a, 'chain, B: CryptoOps> ChainBuilder<'a, 'chain, B> {
424424
budget: &mut Budget,
425425
) -> ValidationResult<'chain, Chain<'chain, B>, B> {
426426
if let Some(nc) = working_cert_extensions.get_extension(&NAME_CONSTRAINTS_OID) {
427-
name_chain.evaluate_constraints(&nc.value()?, budget)?;
427+
let constraints: NameConstraints<'chain, Asn1Read> = nc.value()?;
428+
429+
// RFC 5280 4.2.1.10 requires directoryName constraints to be
430+
// applied to the subject field of subsequent certificates, not
431+
// just to their directoryName SANs (which the evaluation below
432+
// would handle), and requires applications that cannot process
433+
// a constraint in a critical name constraints extension to
434+
// reject the certificate. We don't implement directoryName
435+
// matching, so a critical extension containing any directoryName
436+
// constraint is a hard error. A non-critical extension may be
437+
// (partially) ignored, so its directoryName constraints fall
438+
// through to the per-SAN evaluation below.
439+
fn contains_directory_name(
440+
subtrees: &Option<SequenceOfSubtrees<'_, Asn1Read>>,
441+
) -> bool {
442+
subtrees.as_ref().is_some_and(|subtrees| {
443+
subtrees
444+
.clone()
445+
.any(|subtree| matches!(subtree.base, GeneralName::DirectoryName(_)))
446+
})
447+
}
448+
if nc.critical
449+
&& (contains_directory_name(&constraints.permitted_subtrees)
450+
|| contains_directory_name(&constraints.excluded_subtrees))
451+
{
452+
return Err(ValidationError::new(ValidationErrorKind::FatalError(
453+
"critical nameConstraints extension contains an unsupported \
454+
directoryName constraint",
455+
)));
456+
}
457+
458+
name_chain.evaluate_constraints(&constraints, budget)?;
428459
}
429460

430461
// Look in the store's root set to see if the working cert is listed.

‎tests/x509/verification/test_verification.py‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,3 +860,152 @@ def test_wrong_subject_alt_name(self):
860860
builder.build_server_verifier(DNSName("wrong.io")).verify(
861861
self.leaf, []
862862
)
863+
864+
865+
class TestNameConstraints:
866+
validation_time = datetime.datetime(2025, 1, 1)
867+
dirname = x509.DirectoryName(
868+
x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "PyCA")])
869+
)
870+
871+
@classmethod
872+
def _build_chain(cls, name_constraints, leaf_sans, *, nc_critical):
873+
# Builds a (ca, leaf) chain where the CA carries the given
874+
# nameConstraints extension and the leaf the given SANs.
875+
ca_key = ec.generate_private_key(ec.SECP256R1())
876+
leaf_key = ec.generate_private_key(ec.SECP256R1())
877+
878+
not_before = datetime.datetime(2024, 1, 1)
879+
not_after = datetime.datetime(2034, 1, 1)
880+
881+
ca_name = x509.Name(
882+
[x509.NameAttribute(NameOID.COMMON_NAME, "Test CA")]
883+
)
884+
ca = (
885+
x509.CertificateBuilder()
886+
.subject_name(ca_name)
887+
.issuer_name(ca_name)
888+
.public_key(ca_key.public_key())
889+
.serial_number(x509.random_serial_number())
890+
.not_valid_before(not_before)
891+
.not_valid_after(not_after)
892+
.add_extension(
893+
x509.BasicConstraints(ca=True, path_length=None),
894+
critical=True,
895+
)
896+
.add_extension(
897+
x509.KeyUsage(
898+
digital_signature=False,
899+
content_commitment=False,
900+
key_encipherment=False,
901+
data_encipherment=False,
902+
key_agreement=False,
903+
key_cert_sign=True,
904+
crl_sign=True,
905+
encipher_only=False,
906+
decipher_only=False,
907+
),
908+
critical=True,
909+
)
910+
.add_extension(name_constraints, critical=nc_critical)
911+
.sign(ca_key, hashes.SHA256())
912+
)
913+
914+
leaf = (
915+
x509.CertificateBuilder()
916+
.subject_name(
917+
x509.Name(
918+
[x509.NameAttribute(NameOID.COMMON_NAME, "example.com")]
919+
)
920+
)
921+
.issuer_name(ca_name)
922+
.public_key(leaf_key.public_key())
923+
.serial_number(x509.random_serial_number())
924+
.not_valid_before(not_before)
925+
.not_valid_after(not_after)
926+
.add_extension(
927+
x509.SubjectAlternativeName(leaf_sans), critical=False
928+
)
929+
.add_extension(
930+
x509.AuthorityKeyIdentifier.from_issuer_public_key(
931+
ca_key.public_key()
932+
),
933+
critical=False,
934+
)
935+
.sign(ca_key, hashes.SHA256())
936+
)
937+
938+
return ca, leaf
939+
940+
def _verifier(self, ca):
941+
builder = PolicyBuilder().store(Store([ca]))
942+
builder = builder.time(self.validation_time)
943+
return builder.build_client_verifier()
944+
945+
@pytest.mark.parametrize("subtree", ["permitted", "excluded"])
946+
def test_critical_dirname_constraint_rejected(self, subtree):
947+
# Per RFC 5280 4.2.1.10, directoryName constraints apply to the
948+
# subject field as well as directoryName SANs, and an application
949+
# that cannot process a constraint in a critical nameConstraints
950+
# extension must reject the certificate. We don't implement
951+
# directoryName matching, so a critical extension containing a
952+
# directoryName constraint is rejected outright.
953+
ca, leaf = self._build_chain(
954+
x509.NameConstraints(
955+
permitted_subtrees=(
956+
[self.dirname] if subtree == "permitted" else None
957+
),
958+
excluded_subtrees=(
959+
[self.dirname] if subtree == "excluded" else None
960+
),
961+
),
962+
[x509.DNSName("example.com")],
963+
nc_critical=True,
964+
)
965+
with pytest.raises(
966+
VerificationError,
967+
match="critical nameConstraints extension contains an "
968+
"unsupported directoryName constraint",
969+
):
970+
self._verifier(ca).verify(leaf, [])
971+
972+
def test_critical_supported_constraint_ok(self):
973+
# A critical nameConstraints extension without directoryName
974+
# constraints is processed normally.
975+
ca, leaf = self._build_chain(
976+
x509.NameConstraints(
977+
permitted_subtrees=[x509.DNSName("example.com")],
978+
excluded_subtrees=None,
979+
),
980+
[x509.DNSName("example.com")],
981+
nc_critical=True,
982+
)
983+
self._verifier(ca).verify(leaf, [])
984+
985+
def test_noncritical_dirname_constraint_with_dirname_san(self):
986+
# A non-critical directoryName constraint isn't rejected outright,
987+
# but evaluating it against a directoryName SAN is still
988+
# unsupported.
989+
ca, leaf = self._build_chain(
990+
x509.NameConstraints(
991+
permitted_subtrees=[self.dirname], excluded_subtrees=None
992+
),
993+
[x509.DNSName("example.com"), self.dirname],
994+
nc_critical=False,
995+
)
996+
with pytest.raises(
997+
VerificationError, match="unsupported name constraint"
998+
):
999+
self._verifier(ca).verify(leaf, [])
1000+
1001+
def test_noncritical_dirname_constraint_ignored(self):
1002+
# A non-critical directoryName constraint is ignored when no SAN
1003+
# is a directoryName.
1004+
ca, leaf = self._build_chain(
1005+
x509.NameConstraints(
1006+
permitted_subtrees=[self.dirname], excluded_subtrees=None
1007+
),
1008+
[x509.DNSName("example.com")],
1009+
nc_critical=False,
1010+
)
1011+
self._verifier(ca).verify(leaf, [])

0 commit comments

Comments
 (0)