Back
+30 XP
Concept

Certificate Expiration & SAN Inspector

TLS certificates expire after fixed terms, causing sudden browser warnings and broken API webhooks. Inspecting certificate metadata requires evaluating the not_after expiration timestamp and matching request domains against Subject Alternative Names (SANs) supporting wildcards (*.domain.com).

python
import datetime

# Check expiration timestamp against UTC:
is_expired = datetime.datetime.now(datetime.timezone.utc) > not_after

# Certificate Expiration & Subject Alternative Name (SAN) Inspector Every year, major tech giants have outages because someone forgot that an internal SSL/TLS certificate was set to expire at midnight. Browsers throw full-screen red warning pages, and mobile apps refuse to connect. 🛑🔐 A proactive **Certificate Inspector** performs two essential security checks: 1. **Expiration Guard**: Inspects `not_after` against current UTC time, calculating `days_remaining` to alert on-call engineers long before expiration. 2. **Subject Alternative Name (SAN) Matching**: The legacy `Common Name (CN)` is deprecated (RFC 6125). Modern clients inspect SAN extensions, matching against exact domains (`xicae.org`) and single-level wildcards (`*.xicae.org` matches `python.xicae.org`, but does *not* match `deep.sub.xicae.org`). ```python import datetime # Check expiration against UTC time: now = datetime.datetime.now(datetime.timezone.utc) days_left = (cert["not_after"] - now).days ``` ## Pop Quiz Why did modern web standards deprecate Common Name (`CN`) matching in favor of Subject Alternative Name (`SAN`) validation? - [ ] SAN supports specifying multiple distinct hostnames, wildcards, and IP addresses within a single unified X.509 extension. - [ ] Common Name fields cannot be parsed by x86-64 CPU registers. - [ ] The Common Name field converts domain strings into non-standard binary encodings. - [ ] Because web browsers reject certificates that have fewer than 10 hostnames listed. ## Real-World Challenge Implement `inspect_cert_validity(cert_info: dict, target_host: str, current_time: datetime.datetime = None) -> dict`. - `cert_info` contains `{"not_after": datetime.datetime, "san": list[str]}`. - Evaluate whether the certificate is expired. - Match `target_host` against SANs (supporting exact matches and `*.domain.com` single-level wildcards). - Return `{"valid": bool, "is_expired": bool, "days_remaining": int, "host_match": bool}`. <details> <summary>Solution</summary> ```python import datetime def matches_san(hostname: str, san_list: list[str]) -> bool: h = hostname.lower() for pattern in san_list: p = pattern.lower() if p.startswith("*."): # Single-level wildcard match (RFC 6125) domain_suffix = p[2:] if "." in h: sub, rest = h.split(".", 1) if rest == domain_suffix: return True elif h == p: return True return False def inspect_cert_validity(cert_info: dict, target_host: str, current_time: datetime.datetime = None) -> dict: now = current_time or datetime.datetime.now(datetime.timezone.utc) not_after = cert_info["not_after"] is_expired = now > not_after days_remaining = (not_after - now).days host_match = matches_san(target_host, cert_info.get("san", [])) is_valid = (not is_expired) and host_match return { "valid": is_valid, "is_expired": is_expired, "days_remaining": days_remaining, "host_match": host_match } # Verification now_utc = datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc) valid_cert = { "not_after": datetime.datetime(2026, 7, 1, tzinfo=datetime.timezone.utc), "san": ["*.xicae.org", "xicae.org"] } # 1. Valid subdomain match res1 = inspect_cert_validity(valid_cert, "python.xicae.org", current_time=now_utc) assert res1["valid"] is True assert res1["host_match"] is True assert res1["days_remaining"] == 181 # 2. Apex domain match res2 = inspect_cert_validity(valid_cert, "xicae.org", current_time=now_utc) assert res2["valid"] is True assert res2["host_match"] is True # 3. Unmatched foreign host res3 = inspect_cert_validity(valid_cert, "evil.com", current_time=now_utc) assert res3["valid"] is False assert res3["host_match"] is False # 4. Multi-level wildcard attempt rejected (*.xicae.org should not match api.v1.xicae.org) res4 = inspect_cert_validity(valid_cert, "api.v1.xicae.org", current_time=now_utc) assert res4["host_match"] is False ``` </details>