P02 Lab 10 — TLS / X.509 Certificate-Chain Validator

WARMUP: Chapter 4 (TLS and PKI).

Threat model

Validating a server certificate is a specific algorithm, and the failure modes are classic, high- impact bugs: accepting an expired cert, a name-mismatched cert (the attacker's valid cert for their domain presented for yours), a cert that doesn't chain to a trusted root, or trusting a non-CA intermediate. Any one turns TLS from "authenticated channel" into "encrypted channel to an attacker." This lab builds the path-validation algorithm that prevents all four.

What you build (lab.py)

  • name_matches(sans, hostname) — SAN match with single-label wildcard semantics (*.example.com matches one left label, not multiple, not the bare domain).
  • build_chain(leaf, intermediates, trust_store) — follow issuer→subject links to a trusted anchor; guard against missing links and loops.
  • verify_chain(...) — name → anchored → validity period → CA basic-constraints/keyCertSign on intermediates → valid.

Cases the tests cover

  • Valid leaf→intermediate→root and leaf-directly-from-root; name mismatch; expired and not-yet-valid; untrusted/rogue root; missing intermediate (untrusted); intermediate that isn't a CA or lacks keyCertSign.
  • Wildcard subtleties (one label only; not the bare domain).

Run

pip install -r requirements.txt
LAB_MODULE=solution pytest -q
pytest -q

Hardening / extensions

  • Add signature verification (the real trust step — here trust is by anchor subject) and path-length basic constraints.
  • Add revocation (CRL/OCSP) and OCSP stapling; and EKU (serverAuth) checks.
  • Add name-constraints, SAN types (IP), and CT (Certificate Transparency) SCT verification.

Interview / resume

"Built an X.509 path validator — chain construction to a trusted anchor, SAN/wildcard hostname matching, validity windows, and CA basic-constraints/keyCertSign checks — the algorithm whose bugs turn TLS into an encrypted channel to the attacker."

Limitations: chain linkage by name and trust by anchor subject (no cryptographic signature verification); no revocation, EKU, name-constraints, or CT (all listed as extensions).