TLS
Source: std/tls.mw.
A TLS 1.3 client with X25519, ChaCha20-Poly1305 and AES-128-GCM. The server certificate chain and host name are always verified against the anchors you pass in; HelloRetryRequest is not supported. Load the system trust store with x509_load_system_roots.
TLS 1.3 client (RFC 8446).
- cipher suites TLS_CHACHA20_POLY1305_SHA256, TLS_AES_128_GCM_SHA256
- key exchange X25519
- server auth X.509 chain verified against the trust store and the host name, CertificateVerify with ECDSA (P-256/P-384) or RSA-PSS, and the Finished MAC. There is no way to skip verification.
Usage:
var fd: i64 = tcp_connect(host, 443);var roots: *VecPtr = vec_ptr_new(16); x509_load_system_roots(roots);var t: *Tls = tls_connect(fd, host, roots); // null on failuretls_write(t, data, n); tls_read(t, buf, cap); tls_close(t);struct Tls { fd: i64; c_key: *u8; c_iv: *u8; c_seq: i64; s_key: *u8; s_iv: *u8; s_seq: i64; c_secret: *u8; s_secret: *u8; encrypted: i64; th: *u8; thlen: i64; thcap: i64; hs: *u8; hslen: i64; hscap: i64; rbuf: *u8; rpos: i64; rlen: i64; eof: i64; failed: i64; suite: i64;}Functions
Section titled “Functions”tls_last_error
Section titled “tls_last_error”fn tls_last_error() -> *u8tls_connect
Section titled “tls_connect”fn tls_connect(fd: i64, host: *u8, anchors: *VecPtr) -> *TlsRuns the TLS 1.3 handshake on a connected socket. anchors is a VecPtr of trusted *Cert (see x509_load_system_roots). Returns null on failure (see tls_last_error). On failure the socket is left open for the caller.
tls_free
Section titled “tls_free”fn tls_free(t: *Tls)tls_handshake
Section titled “tls_handshake”fn tls_handshake(t: *Tls, host: *u8, anchors: *VecPtr) -> booltls_write
Section titled “tls_write”fn tls_write(t: *Tls, buf: *u8, n: i64) -> i64Sends n bytes; returns n, or a negative value on error.
tls_read
Section titled “tls_read”fn tls_read(t: *Tls, buf: *u8, cap: i64) -> i64Reads up to cap bytes. Returns 0 at end of stream (close_notify or closed connection), a negative value on error.
tls_close
Section titled “tls_close”fn tls_close(t: *Tls)Sends close_notify. Does not close the socket.