Skip to content

X.509

Source: std/x509.mw.

Parses DER certificates, loads PEM bundles and verifies a chain against a set of trust anchors, including validity dates, CA constraints, key usage and host name matching.

X.509 certificates (RFC 5280): parsing, hostname matching, chain building checks and a PEM trust store. Supported: RSA (PKCS#1 v1.5) and ECDSA (P-256 / P-384) signatures with SHA-1/256/384/512, and the extensions basicConstraints, keyUsage, extKeyUsage and subjectAltName. Unknown *critical* extensions make a certificate unacceptable.

Name Type Value Notes
X509_KEY_RSA i64 1
X509_KEY_EC i64 2
X509_OK i64 0
X509_E_EMPTY i64 1
X509_E_VALIDITY i64 2
X509_E_SIGNATURE i64 3
X509_E_ISSUER i64 4
X509_E_UNTRUSTED i64 5
X509_E_HOSTNAME i64 6
X509_E_NOT_CA i64 7
X509_E_PATHLEN i64 8
X509_E_USAGE i64 9
struct Cert {
der: *u8;
derlen: i64;
tbs: *u8;
tbslen: i64;
issuer: *u8;
issuerlen: i64;
subject: *u8;
subjectlen: i64;
not_before: i64;
not_after: i64;
key_type: i64;
curve: i64;
key1: *u8;
key1len: i64;
key2: *u8;
key2len: i64;
sig_scheme: i64;
sig_hash: i64;
sig: *u8;
siglen: i64;
has_bc: i64;
is_ca: i64;
pathlen: i64;
has_ku: i64;
ku: i64;
has_eku: i64;
eku_ok: i64;
san: *u8;
sanlen: i64;
}
fn x509_strerror(code: i64) -> *u8
fn x509_free(c: *Cert)
fn x509_parse(der: *u8, derlen: i64) -> *Cert

Parses a DER certificate (the bytes are copied). Returns null if the certificate is malformed or uses unsupported algorithms/critical extensions.

fn x509_check_signature(c: *Cert, issuer: *Cert) -> bool
fn x509_host_match(c: *Cert, host: *u8) -> bool
fn x509_load_pem(buf: *u8, len: i64, out: *VecPtr) -> i64

Appends every certificate found in a PEM buffer to out (a VecPtr of *Cert). Returns how many were added.

fn x509_load_pem_file(path: *u8, out: *VecPtr) -> i64
fn x509_load_system_roots(out: *VecPtr) -> i64

Loads the system trust store from the usual bundle locations.

fn x509_valid_at(c: *Cert, now: i64) -> bool
fn x509_verify_chain(chain: *VecPtr, anchors: *VecPtr, host: *u8, now: i64) -> i64

Checks chain (leaf first; a VecPtr of *Cert as sent by the server) against anchors, for host (null skips the name check) at Unix time now. Returns X509_OK or an X509_E_* code.