Module
std.x.tls.cert — X.509 v3 certificate parsing and path validation (RFC 5280 over X.690 DER, RFC 9525 names). Stability tier: x — the THIRD rung of the stdc-tls campaign (sprint sc22), the rung that makes sc21's opaque Certificate bytes MEAN something: sc21 carries the DER and builds the §4.4.3 signed octets; this module parses the certificate, checks each signature with its issuer's key, and validates a path to a caller-supplied root set (sc79) or one trust anchor, against a hostname and a SUPPLIED time.
The nursery banner (D31). std.x.tls.cert is not std.tls: its path, names and behaviour may change or vanish in one sprint; no other std module may depend on it. Its reason and graduation trigger live in std/x/README.md. It depends on FIVE sibling residents — std.x.crypto.curve25519 (Ed25519), std.x.crypto.p256 (ECDSA P-256, sc23), std.x.crypto.p384 (ECDSA P-384, sc79), std.x.crypto.rsa (RSASSA-PKCS1-v1_5 and RSASSA-PSS, sc79) and std.x.crypto.sha2 (the digests P-384 verifies over) — resident-on-resident edges the graduation move to std.tls/std.crypto must resolve, named in the register beside the record/handshake/jose edges.
Built from the DOCUMENTS, not ported. ITU-T X.690 (the DER encoding rules — §8.1 TLV structure and lengths, §8.3 INTEGER minimality, §8.19 OID subidentifiers, §10 the definite-form and minimal-length rules, §11.1 BOOLEAN, §11.5 DEFAULT-value absence, §11.6 SET-OF ordering) and RFC 5280 (the X.509/PKIX profile — §4.1 the certificate structure, §4.1.2.2 serial numbers, §4.1.2.5 the UTCTime/GeneralizedTime split and the 2-digit-year window, §4.2 the extension rules, §6.1 path validation) plus RFC 8410 (Ed25519 AlgorithmIdentifier: NO parameters, §3). Section numbers are cited at the rejection sites they license.
## The robustness contract (the sprint's flagship)
Every length read from the input is VALIDATED against the remaining window before a single content byte is touched: a length that claims more bytes than remain, an indefinite length (X.690 §10.1 forbids it in DER), a non-minimal length encoding, a reserved length octet — each is a clean, named der rejection, never an out-of-bounds read and never a trap. wolf's checked arithmetic (X3) and region memory make the overrun STRUCTURALLY impossible; the decoder's job on top of that is to turn "impossible" into "rejected", because a server cannot abort on a bad input. The decoder is grammar-directed (it walks exactly the X.509 productions, and carries anything it does not model as opaque bytes), so there is NO unbounded recursion for a nesting bomb to exhaust: a deeply nested structure inside an unknown extension is never walked at all.
## The scope line (honest about the crypto ladder)
Signature VALIDATION (sc79's table, verify_signed_by): Ed25519 (RFC 8410); ecdsa-with-SHA256 under a P-256 key; ecdsa-with-SHA256, -SHA384 and -SHA512 under a P-384 key (RFC 5758, the curve named in the key's parameters, RFC 5480); sha256/384/512WithRSAEncryption (RFC 8017 PKCS#1 v1.5) and RSASSA-PSS (RFC 4055 parameters: SHA-256, -384 or -512, MGF1 over the same hash, the stated salt length) under an rsaEncryption key of 2048 to 4096 bits. Every other pairing PARSES structurally (the hardening win is algorithm-independent) and validation answers the named unsupported_alg — NEVER a silent accept: SHA-1 and MD5 signatures, P-521, Ed448, a P-256 key under a digest other than SHA-256, an RSASSA-PSS (id-RSASSA-PSS) key, an RSA modulus over 4096 bits. An RSA key under 2048 bits, or with an exponent outside the rsa module's range, is weak_key, wherever it sits in a path.
Path validation (RFC 5280 §6.1, this subset): validity at the supplied time, basicConstraints cA and pathLenConstraint, keyUsage keyCertSign on issuers and digitalSignature on the leaf (RFC 8446 §4.4.2.2), extendedKeyUsage serverAuth (or anyExtendedKeyUsage) on the leaf and on every issuer that carries the extension, unknown critical extensions refused by name, dNSName SAN matching per RFC 9525 with a wildcard only as the whole left-most label (san_matches). NAMED LIMITS (deliberately not half-built): nameConstraints are REFUSED by name (name_constraints) wherever they appear, never evaluated; revocation (CRL/OCSP) is not consulted; the policy machinery (§4.2.1.4, §6.1's policy tree) is not implemented, so a CRITICAL policy extension is unknown_critical; the CN fallback for hostnames is not implemented (SAN is the only source); non-dNSName SAN forms (IP, URI, email) are carried, not matched; names chain by exact DER bytes, not by RFC 5280 §7.1's caseIgnoreMatch. ## ct: certificates are PUBLIC data
Everything this module touches — the DER, the names, the validity dates, the public keys, the signatures — is public wire material. No secret is present anywhere in this module, so no function here carries a constant-time obligation (unlike the handshake's secrets or sc18's signing scalar; the sc18 rule: VERIFY is public data). Every pub fn states ct: public data so the posture is explicit rather than inferred. The 0..255 ingestion guard is the recorded value-dependent branch shared with the whole ladder (F-0035's real byte type retires it); it runs on public bytes here.
## Time is a SUPPLIED argument, never the wall clock
Validity checking takes now as an argument in the compact form yyyymmddhhmmss (UTC, the timestamp helper builds it). Certificate times decode to the same form, so the comparison is plain integer order and every test is deterministic — nothing in this module reads a clock (it is capability-free: pure, comptime-refused only by its delegate curve25519 when a validation reaches a signature).
## The seams: validate_path (sc79) and validate_chain (ws05, frozen in shape)
validate_path(chain, hostname, roots, now) -> Cert ! {...} is the public-PKI seam std.x.tls.client consumes: chain is the peer's certificate chain as CONCATENATED DER, leaf first (the cert_data entries of sc21's Certificate message, joined — DER is self-delimiting, cert_end finds each boundary), the rest in any order; roots is the caller's trust store as concatenated DER (a distribution's bundle converted to DER is the intended input; an entry this profile cannot parse is skipped); now is the supplied validation time. The path is built depth-first with backtracking, roots before the peer's intermediates at every step, bounded at eight certificates and sixteen link attempts. validate_chain(chain, hostname, anchor, now) is the sc22 seam wolf-wws ws05 consumes, unchanged in shape: the same validation with a root set of exactly one anchor. The answer is the parsed LEAF (subject, SAN, validity, SPKI — the fields ws05 reads) or a NAMED rejection: der (malformed encoding), unsupported_alg, bad_signature, expired, not_yet_valid, name_mismatch, not_ca, issuer_mismatch, unknown_critical, and since sc79 weak_key, bad_usage and name_constraints. sc22 owns certificate meaning; sc21 owns the handshake flow that carries the bytes; ws05 owns the socket conversation. std/x/README.md records both seams.
Byte sequences are List[byte] with the 0..255 element contract (§11's interim, F-0035), the whole ladder's convention. Chain validation is iterative (no recursion, no per-chain allocation beyond the parsed certificates); a root that ends a path is validated like any chain certificate (validity, basicConstraints, keyUsage, extendedKeyUsage, key policy) — a deliberately STRICTER reading than RFC 5280 §6.1.1(d)'s anchor-as-input, stated here because every root a modern store carries meets the profile, and an expired or non-CA root is better refused than trusted.
Functions and methods
fn · Source line 1199
pub fn parse(bytes: List[byte]) -> Cert ! {der}
Parse ONE DER certificate occupying the whole of bytes (RFC 5280 §4.1 over X.690 DER; trailing bytes reject). Structural validation only — signatures, dates and names are DECODED here and JUDGED in validate_chain, so a certificate signed with an algorithm outside the ladder still parses (the split the header states). Every malformed, truncated or non-DER encoding is the named der rejection — no input reads out of bounds and none traps (the robustness contract; the conformance suite in tests/x/tls/cert/ pins each clause). Rejections enforced here include: every length class (indefinite, non-minimal, overrun, reserved), INTEGER and BOOLEAN minimality, OID subidentifier form, the version field (present must be v3; v1's DEFAULT must be absent, X.690 §11.5; v2 rejects), serial positivity and the 20-octet cap (§4.1.2.2), the §4.1.2.3 TBS/outer algorithm equality, the §4.1.2.5 time rules (2-digit window, mandatory seconds and Z, the 2049/2050 split), DN SET-OF order (X.690 §11.6), CN string types (§4.1.2.4), SPKI form (zero unused bits; RFC 8410's no-params, 32-byte Ed25519 key), the §4.1.2.8 unique-ID prohibition, and the §4.2 extension rules (non-empty, no duplicates, DEFAULT-FALSE absence, basicConstraints/keyUsage/subjectAltName inner form). ct: public data; no secret is present.
fn · Source line 1356
pub fn cert_end(chain: List[byte], offset: int) -> int ! {der}
The end offset (exclusive) of the DER certificate starting at offset in chain — the boundary walker for CONCATENATED DER (sc21's certificate_list entries, joined). Validates the outer TLV header only; parse validates the body. Truncation and every malformed length reject der. ct: public data.
fn · Source line 1377
pub fn oid_ed25519() -> str
The Ed25519 algorithm OID (RFC 8410), the key and the signature algorithm at once. Zero-argument functions stand in for consts (F-0025). ct: public data.
fn · Source line 1383
pub fn oid_ecdsa_sha256() -> str
The ecdsa-with-SHA256 signature algorithm OID (X9.62 / RFC 5480 — the ES256 certificate signature). ct: public data.
fn · Source line 1392
pub fn oid_ec_public_key() -> str
The id-ecPublicKey OID (X9.62 — an EC subjectPublicKey). The curve itself rides in the SPKI parameters (RFC 5480's namedCurve, decoded into Cert.spki_curve): P-256 and P-384 validate, and each curve's on-curve check refuses a key that is not its point; any other curve is unsupported_alg. ct: public data.
fn · Source line 1403
pub fn alg_name(dotted: str) -> str
Classify a dotted algorithm OID: "ed25519" (1.3.101.112), "rsa" (the PKCS#1 arc — signature algorithms and rsaEncryption), "ecdsa" (the X9.62 arc — ECDSA signatures and id-ecPublicKey), "ed448" (1.3.101.113), else "unknown". Classification is for REPORTING; validation reads the exact OIDs (verify_signed_by's table) and answers unsupported_alg for every pairing outside it. ct: public data.
fn · Source line 1421
pub fn ku_digital_signature() -> int
KeyUsage bit weight: digitalSignature (bit 0). ct: public data.
fn · Source line 1428
pub fn ku_key_cert_sign() -> int
KeyUsage bit weight: keyCertSign (bit 5) — the bit an issuing CA's keyUsage must assert (§4.2.1.3). ct: public data.
fn · Source line 1439
pub fn host_matches(pattern: str, host: str) -> bool
Exact, case-insensitive DNS name match (DNS names compare case-insensitively; both sides are lowered). NO wildcard support — a *. pattern here never matches a hostname, which fails CLOSED. Kept exact because callers outside certificate validation read it (a server choosing its certificate by SNI); certificate validation matches SAN entries with san_matches (RFC 9525) since sc79. ct: public data.
fn · Source line 1455
pub fn timestamp(y: int, mo: int, d: int, h: int, mi: int, s: int) -> int
Build the compact yyyymmddhhmmss timestamp validate_chain and the Cert fields speak (UTC). Caller-contract violations trap assert (X3's posture: the caller could have checked the ranges); certificate DATA with bad ranges rejects der in the parser instead — §10's trap-where-the-caller-broke-it rule. ct: public data.
fn · Source line 1479
pub fn oid_rsa_encryption() -> str
The rsaEncryption key OID (RFC 8017 A.1; RFC 3279 §2.3.1): an RSA subjectPublicKey whose BIT STRING holds the DER RSAPublicKey. ct: public data.
fn · Source line 1485
pub fn oid_secp256r1() -> str
The prime256v1 (P-256) named-curve OID (RFC 5480 §2.1.1.1). ct: public data.
fn · Source line 1491
pub fn oid_secp384r1() -> str
The secp384r1 (P-384) named-curve OID (RFC 5480 §2.1.1.1). ct: public data.
fn · Source line 1498
pub fn oid_server_auth() -> str
The id-kp-serverAuth key purpose (RFC 5280 §4.2.1.12): the extendedKeyUsage value a TLS server's certificate must allow. ct: public data.
fn · Source line 1680
pub fn verify_signed_by(child: Cert, issuer: Cert) -> int ! { der, unsupported_alg, bad_signature, weak_key, }
Verify child's signature over its TBSCertificate bytes with issuer's key. The table (sc79):
- Ed25519 (RFC 8410) under an Ed25519 key — curve25519.verify; - ecdsa-with-SHA256 under a P-256 key — p256.verify_der; - ecdsa-with-SHA256, -SHA384 or -SHA512 under a P-384 key — p384.verify_digest_der (bits2int over the digest); - sha256/384/512WithRSAEncryption under an rsaEncryption key — rsa.verify_pkcs1; - RSASSA-PSS (RFC 4055) under an rsaEncryption key, the hash, MGF1 and salt length from the signature's own parameters — rsa.verify_pss.
unsupported_alg for every other pairing — the NAMED honest rejection, never a silent accept: SHA-1 signatures, P-256 with a digest other than SHA-256, P-521, Ed448, an RSASSA-PSS key, a signature algorithm whose key type does not match the issuer's. bad_signature when the verification itself fails (each verifier also answers false for a malformed key point or signature encoding). weak_key and der from the issuer's RSA key (rsa_key), der and unsupported_alg from malformed or unsupported PSS parameters. Returns 1 on success (the accept_finished shape). ct: public data — a VERIFY handles no secret (the sc18 rule); the verifiers' own ct: lines govern their internals.
fn · Source line 1874
pub fn san_matches(pattern: str, host: str) -> bool
RFC 9525 §6.3 dNSName matching (DNS names compare case-insensitively; both sides are lowered). A pattern without * matches exactly. A wildcard matches only as the COMPLETE left-most label of a pattern with at least two labels after it (*.example.com): it stands for exactly one non-empty label of the host. Every other * (a partial label w*.example.com, a wildcard below the left-most label, a bare * or *.com) matches nothing — the rule fails CLOSED. ct: public data.
cert.san_matches("*.example.com", "www.example.com") == true
cert.san_matches("*.example.com", "a.b.example.com") == false
cert.san_matches("*.example.com", "example.com") == false
cert.san_matches("w*.example.com", "www.example.com") == false
cert.san_matches("Example.COM", "example.com") == true
fn · Source line 2083
pub fn validate_path(chain: List[byte], hostname: str, roots: List[byte], now: int) -> Cert ! { der, unsupported_alg, bad_signature, expired, not_yet_valid, name_mismatch, not_ca, issuer_mismatch, unknown_critical, weak_key, bad_usage, name_constraints, }
PATH VALIDATION against a root set (sc79, RFC 5280 §6.1 in the subset the module header names): build a path from the peer's leaf to one of the caller's roots and validate it at the SUPPLIED time, returning the parsed LEAF.
chain is the peer's certificates as concatenated DER, LEAF FIRST (the TLS Certificate message's entries joined); the rest may arrive in any order and may include certificates the path does not use. roots is the caller's trust store as concatenated DER (a distribution's bundle converted to DER is the intended input); a root this profile cannot parse is skipped. now is timestamp(...)-shaped UTC. No clock is read and no file is opened.
The leaf first: der (any certificate of chain malformed), then unknown_critical, not_yet_valid, expired, name_mismatch (RFC 9525 against its SAN), bad_usage (keyUsage without digitalSignature, extendedKeyUsage without serverAuth), name_constraints, and its key (weak_key for an RSA key under 2048 bits). Then the path, depth-first with backtracking: at each step the roots whose subject is the current certificate's issuer are tried before the peer's other certificates, each candidate judged by link (validity, not_ca, bad_usage, name_constraints, weak_key, then the signature: bad_signature, unsupported_alg). A root that links ends the path. Bounds: at most eight certificates in a path and sixteen link attempts in all. When no path exists the answer is the FIRST link refusal met, or issuer_mismatch when no candidate ever carried the right name. Revocation (CRL/OCSP) is NAMED RESIDUE: nothing here consults revocation state. ct: public data throughout (certificates carry no secret).
fn · Source line 2191
pub fn validate_chain(chain: List[byte], hostname: str, anchor: List[byte], now: int) -> Cert ! { der, unsupported_alg, bad_signature, expired, not_yet_valid, name_mismatch, not_ca, issuer_mismatch, unknown_critical, weak_key, bad_usage, name_constraints, }
THE ws05 SEAM (frozen in shape since sc22): validate the peer's chain against ONE trust anchor — validate_path with a root set of exactly anchor, so it builds, judges and refuses exactly as that function does (sc79 widened what it accepts: RSA, RSA-PSS and P-384 signatures, RFC 9525 wildcards; and what it refuses by name: weak_key, bad_usage, name_constraints). A malformed anchor is der, as it always was. ct: public data throughout (certificates carry no secret).
fn · Source line 2218
pub fn to_hex(b: List[byte]) -> str
Lowercase hex of a byte list. ct: public data (and a rendering convenience only, never a secret-handling path).