Certificate Chains and Path Validation
Browsers trust about 150 root CAs. Those roots almost never sign server certificates directly: their keys sit offline in vaults, and day-to-day issuance is done by intermediate CAs they certified. A server therefore presents a chain, leaf → intermediate(s), and the client has to build a path to a root it trusts and check every link. Most real certificate errors are chain errors, and most certificate-validation vulnerabilities are shortcuts in this process.
What path validation checks
RFC 5280 section 6 defines the algorithm. For each certificate on the path from the trusted root down to the leaf, the verifier checks:
- Signature: each certificate is signed by the key of the one above it.
- Names chain: each issuer name matches the subject of the certificate above (key identifiers help pick the right one).
- Validity: the current time is inside every certificate's
notBefore–notAfter. - CA constraints: every certificate above the leaf has
basicConstraints CA:TRUEandkeyCertSignin its key usage;pathlenlimits are respected. - Policy and name constraints: an intermediate restricted to
permitted: .lab.testcan't issue forbank.example. - Purpose: the leaf's extended key usage fits the use (
serverAuthfor a TLS server). - Revocation: none of the certificates is revoked (next lesson).
Then, separately, the application checks that the leaf is for the name it wanted (verify_hostname). openssl verify does the chain part; s_client -verify_hostname adds the name check (OpenSSL s_client and s_server).
Building and verifying a chain with OpenSSL
The session creates a three-level PKI (root, issuing CA with pathlen:0, leaf) with fresh lab-only keys, then verifies it three ways. -untrusted gives OpenSSL candidate intermediates that it may use to build the path but must not trust on their own, which is exactly how a TLS client treats the extra certificates a server sends.
# 1. A three-level lab PKI made in this session: root -> issuing CA -> leaf (lab-only keys)
openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes -keyout root.key -subj "/O=Lab/CN=Lab Root R2" \
-days 3650 -addext "basicConstraints=critical,CA:TRUE" -addext "keyUsage=critical,keyCertSign,cRLSign" -out root.crt 2>/dev/null
openssl req -new -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes -keyout int.key -subj "/O=Lab/CN=Lab Issuing CA 1" -out int.csr 2>/dev/null
printf 'basicConstraints=critical,CA:TRUE,pathlen:0\nkeyUsage=critical,keyCertSign,cRLSign\n' > int.ext
openssl x509 -req -in int.csr -CA root.crt -CAkey root.key -set_serial 0x2001 -days 1825 -extfile int.ext -out int.crt 2>/dev/null
openssl req -new -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes -keyout api.key -subj "/CN=api.lab.test" -out api.csr 2>/dev/null
printf 'basicConstraints=critical,CA:FALSE\nsubjectAltName=DNS:api.lab.test\nextendedKeyUsage=serverAuth\n' > api.ext
openssl x509 -req -in api.csr -CA int.crt -CAkey int.key -set_serial 0x3001 -days 90 -extfile api.ext -out api.crt 2>/dev/null
echo "== chain with the intermediate supplied as untrusted:"
openssl verify -show_chain -CAfile root.crt -untrusted int.crt api.crt
echo "== server forgot to send the intermediate:"
openssl verify -CAfile root.crt api.crt 2>&1
echo "== intermediate trusted as if it were a root (partial chain):"
openssl verify -partial_chain -CAfile int.crt api.crt== chain with the intermediate supplied as untrusted:
api.crt: OK
Chain:
depth=0: CN = api.lab.test (untrusted)
depth=1: O = Lab, CN = Lab Issuing CA 1 (untrusted)
depth=2: O = Lab, CN = Lab Root R2
== server forgot to send the intermediate:
CN = api.lab.test
error 20 at 0 depth lookup: unable to get local issuer certificate
error api.crt: verification failed
== intermediate trusted as if it were a root (partial chain):
api.crt: OK
- With the intermediate, the path leaf → issuing CA → root is built and every check passes; only the root (depth 2) is trusted.
- Without it, verification fails with error 20. This is the most common TLS misconfiguration in practice: the server is configured with the leaf only. Browsers often hide the problem (they cache intermediates or fetch them from the AIA URL), while
curl, Python, Java and IoT clients fail, so "it works in my browser" proves nothing. -partial_chainlets a trusted intermediate act as an anchor. Useful for private PKIs, dangerous if done by accident.
Constraints are enforced
The sample lab root in $SAMPLES/pki was created with pathlen:0: it may sign leaves, but no CA below it may sign anything. The session below uses the lab CA key (lab-only, in the sandbox) to sign a "rogue" intermediate and a leaf under it, and the verifier refuses.
P=$SAMPLES/pki
openssl x509 -in $P/ca.crt -noout -subject -ext basicConstraints
openssl req -new -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes -keyout int.key -subj "/O=ExcelTAD Lab/CN=Rogue Issuing CA" -out int.csr 2>/dev/null
printf 'basicConstraints=critical,CA:TRUE\nkeyUsage=critical,keyCertSign\n' > int.ext
openssl x509 -req -in int.csr -CA $P/ca.crt -CAkey $P/ca.key -set_serial 0x2002 -days 365 -extfile int.ext -out int.crt 2>/dev/null
openssl req -new -newkey ec -pkeyopt ec_paramgen_curve:P-256 -nodes -keyout x.key -subj "/CN=x.lab.test" -out x.csr 2>/dev/null
printf 'basicConstraints=critical,CA:FALSE\nsubjectAltName=DNS:x.lab.test\n' > x.ext
openssl x509 -req -in x.csr -CA int.crt -CAkey int.key -set_serial 0x3002 -days 30 -extfile x.ext -out x.crt 2>/dev/null
openssl verify -CAfile $P/ca.crt -untrusted int.crt x.crt 2>&1subject=C = XX, O = ExcelTAD Lab, CN = ExcelTAD Lab Root CA
X509v3 Basic Constraints: critical
CA:TRUE, pathlen:0
C = XX, O = ExcelTAD Lab, CN = ExcelTAD Lab Root CA
error 25 at 2 depth lookup: path length constraint exceeded
error x.crt: verification failed
Error 25: the path has a CA below a pathlen:0 root. Constraints like this limit the damage if a subordinate key is misused.
The same checks in Python
cryptography's verification API (implemented in Rust, following RFC 5280 and the CA/Browser Forum's web-PKI profile) builds the path and checks names and time explicitly. The keys are fixed lab-only scalars so the certificates are reproducible.
import datetime
from cryptography import x509
from cryptography.x509.oid import NameOID, ExtendedKeyUsageOID
from cryptography.x509.verification import PolicyBuilder, Store, VerificationError
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives import hashes
T0 = datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc)
ca_key = ec.derive_private_key(1001, ec.SECP256R1()) # fixed lab-only keys
leaf_key = ec.derive_private_key(1002, ec.SECP256R1())
def name(cn): return x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, cn)])
def make_ca(key):
return (x509.CertificateBuilder().subject_name(name("Lab Root CA")).issuer_name(name("Lab Root CA"))
.public_key(key.public_key()).serial_number(1).not_valid_before(T0).not_valid_after(T0 + datetime.timedelta(days=3650))
.add_extension(x509.BasicConstraints(ca=True, path_length=None), critical=True)
.add_extension(x509.KeyUsage(False, False, False, False, False, True, True, False, False), critical=True)
.add_extension(x509.SubjectKeyIdentifier.from_public_key(key.public_key()), critical=False)
.sign(key, hashes.SHA256()))
ca = make_ca(ca_key)
leaf = (x509.CertificateBuilder().subject_name(name("api.lab.test")).issuer_name(ca.subject)
.public_key(leaf_key.public_key()).serial_number(2).not_valid_before(T0).not_valid_after(T0 + datetime.timedelta(days=90))
.add_extension(x509.SubjectAlternativeName([x509.DNSName("api.lab.test")]), critical=False)
.add_extension(x509.ExtendedKeyUsage([ExtendedKeyUsageOID.SERVER_AUTH]), critical=False)
.add_extension(x509.AuthorityKeyIdentifier.from_issuer_public_key(ca_key.public_key()), critical=False)
.sign(ca_key, hashes.SHA256()))
def check(host, days, roots):
at = T0 + datetime.timedelta(days=days) # a fixed validation moment, not "now"
v = PolicyBuilder().store(Store(roots)).time (at).build_server_verifier(x509.DNSName(host))
try:
chain = v.verify(leaf, [])
return "OK, chain: " + " <- ".join(c.subject.rfc4514_string() for c in chain)
except VerificationError as e:
return "FAIL: " + str(e).split(" (encountered")[0]
print("right name, day 10 :", check("api.lab.test", 10, [ca]))
print("wrong name :", check("www.lab.test", 10, [ca]))
print("day 200 (expired) :", check("api.lab.test", 200, [ca]))
print("look-alike root :", check("api.lab.test", 10, [make_ca(ec.derive_private_key(9999, ec.SECP256R1()))]))right name, day 10 : OK, chain: CN=api.lab.test <- CN=Lab Root CA
wrong name : FAIL: validation failed: leaf certificate has no matching subjectAltName
day 200 (expired) : FAIL: validation failed: cert is not valid at validation time
look-alike root : FAIL: validation failed: candidates exhausted: signature does not match
The last case is a root with the same name but a different key: names alone prove nothing; the signature check catches it.
Historic validation bugs
- No basicConstraints check (Moxie Marlinspike, 2002, Internet Explorer; again in iOS in 2011): any leaf could sign certificates for any site.
- Apple "goto fail" (2014, CVE-2014-1266): a duplicated line skipped the signature check in TLS handshakes.
- GnuTLS (2014) treated some invalid certificates as valid because of an error-code mix-up.
- Windows CryptoAPI "CurveBall" (2020, CVE-2020-0601): ECC certificates with custom curve parameters let an attacker impersonate any root with the same public key.
How chain problems are detected
openssl s_client -connect host:443 -servername name -showcertsagainst your own servers: count the certificates sent; check thatopenssl verify -untrustedbuilds the chain without browser help.- Monitoring that uses a strict client (curl or Python), not a browser.
- Code review for disabled or custom validation:
verify=False,CERT_NONE,InsecureSkipVerify: true,TrustAllCerts,checkServerTrustedthat returns without checking,rejectUnauthorized: false.
How it is fixed
- Configure servers with the full chain (leaf + intermediates, not the root):
fullchain.pemfrom ACME clients. - Use the platform's verifier with host-name checking; to trust a private CA, add that CA to the client's trust store rather than disabling verification.
- Put name constraints and
pathlenon private intermediates.
Pitfalls
- Including the root in the served chain: harmless but wasteful; including the wrong intermediate (an expired cross-sign) breaks old clients.
- Trusting an intermediate directly (
-partial_chain, pinning to an intermediate) and then failing at the next CA rotation. - Testing only with a browser.
Practice
1. Users report that the mobile app fails with "unable to get local issuer certificate" but the website works in Chrome. What is the likely cause and fix?
The server sends only the leaf; Chrome fills in the intermediate from its cache or AIA, the app doesn't. Configure the full chain on the server and recheck with openssl s_client -showcerts.
2. In the Python example, which check would catch an attacker who copies the lab root's subject name into a root of their own?
The signature check: the leaf was signed by the real root's key, and the look-alike root has a different key, so the path can't be built.
3. Why does a TLS client treat the intermediates a server sends as "untrusted"?
Anyone can send any certificate. They are only candidates for building a path; trust must come from a root in the client's own store, and every link must verify.
Test yourself
What does OpenSSL error 20, 'unable to get local issuer certificate', usually mean for a server?
- The certificate expired
- The chain can't be completed to a trusted root, often a missing intermediate
- The host name is wrong
- The key is too short
What does pathlen:0 on a CA certificate mean?
- It can't sign anything
- It can sign leaf certificates but no further CA may appear below it
- The path must have zero certificates
- It expires immediately
Why is checking only the issuer name not enough?
- Names are case-sensitive
- Anyone can create a CA with the same name; only the signature proves which key signed
- Names are encrypted
- Names change at renewal
Which step is separate from chain validation and must be done by the application?
- Signature checks
- Validity dates
- Matching the leaf against the expected host name
- basicConstraints