Skip to content

ours-tls13 ​

libs/tls13 — our own TLS where rustls does not fit: a TLS 1.3 client with a browser's ClientHello (uTLS fingerprints) and REALITY, a REALITY server, connections whose records the kernel encrypts (kTLS), ClientHello fragmentation and the certificate checks shared by them. Ordinary TLS (a server, a client without a fingerprint) is rustls in ours-mod-tls.

Modules ​

  • hello, presets — a preset's ClientHello as uTLS builds it (metacubex/utls v1.8.7, as in sing-box 1.14.4): BoringSSL GREASE, Chrome's extension shuffling, padding, GREASE ECH; after a HelloRetryRequest — the same hello with one key share and a cookie. Presets: Chrome 133, Firefox 148 (as in Xray's uTLS: the X25519 key is half of the hybrid one), Edge 85, Safari 16.0, iOS 14, QQ 11.1; randomized — Chrome 133 or 131, once per process (preset/by_name). 360 7.5 and Android 11 OkHttp — TLS 1.2 only, which this client does not speak.
  • handshake — the client side of a full TLS 1.3 handshake (RFC 8446) with HRR; without PSK and early data; a server with an older version is an error. connect, Config, RealityClient.
  • reality, server — Xray's REALITY, as in sing-box 1.14.4: the client hides the version, the time and the short ID in the session ID (AES-256-GCM under an X25519 key, the whole ClientHello is the AAD); the server (RealityServer) passes the ClientHello to the real site (dest), answers a client that has proven itself with its own handshake on top of the site's ServerHello with records of the same length, and silently proxies the others to the site. browse — a visit to the site for a sample. With an ML-DSA-65 key (Xray's mldsa65Seed) the server certificate carries a signature over both hellos; a client with the public key (mldsa65Verify) checks it. The time in the session ID comes from ours_clock::Clock.
  • schedule, record, kx, codec — the key schedule on HMAC (the secrets stay available for kTLS and REALITY), AEAD records, X25519/P-256/P-384/X25519MLKEM768 key exchange, parsing. RUSTLS_X25519MLKEM768 — the same hybrid group for rustls (rustls has it only with aws-lc-rs): ML-KEM-768 from ml-kem, X25519 from ring. A client's encapsulation key that fails the FIPS 203 check is an invalid share, here and in the REALITY server.
  • conn::TlsStream — an established connection: data, post-handshake messages (tickets, KeyUpdate), close_notify. A write of a whole record (16 KiB) or more is sealed into a batch (up to 16 records): each record in its own slot, the plaintext placed at the same offset modulo 64 as in the source (musl's memcpy is much slower between differently aligned addresses), and the batch leaves in one writev. poll_write_from lets the source (the relay's readv) read the plaintext straight into the batch's slots, and the records are sealed in place — without a copy from the relay buffer. poll_write_records takes someone else's buffer (ours_bufpool::Buf), where the writer has laid out the records' plaintext itself with room for the header and the tag (record_room) — this is how HTTP/2 hands over DATA frames — seals them right there and sends them. Smaller writes and streams without vectored writes go back to back into one buffer. The connection holds record and batch buffers only while it moves data; free ones lie in ours-bufpool's per-thread cache (take_initialized/recycle_vec) and are trimmed together with it.
  • ktls — handing the keys to the kernel (kernel_tx/kernel_rx) after a handshake — its own or rustls's; before the hand-over, all records already read are decrypted here, and a partially read one is read to the end: the kernel must start at a record boundary (Boundary). KeyUpdate requires a kernel with key updates (Linux 6.14); on an old kernel the connection fails with an error rather than continuing on the old keys. The rustls secrets — through KeyLog.
  • fragment — fragmentation of the first ClientHello; encrypted records are not touched.
  • verify — verifying the peer's certificate: by roots, by pinned keys or not at all; the verification time is ours_clock::Clock (the ntp service's correction), for rustls — verify::Time.
  • keypair — a self-signed certificate for the client's name (ECDSA P-256, not Go's RSA 2048: too slow for every name); mldsa65_public_key — the ML-DSA-65 public key from a seed, as xray mldsa65 prints it.

Features ​

  • implementation (default) — everything above; the dependencies rustls, tokio-rustls, ring, webpki, x25519-dalek, ml-kem, ml-dsa, brotli-decompressor, flate2, ruzstd (certificate compression), ours-clock (time) and others.
  • unstable-ktls — ours-ktls-sys; turns on implementation.
  • Without implementation, stub remains: uninhabited NoTlsStream/KernelStream, so that the splice interface compiles without an implementation.

Who uses it ​

ours-mod-tls (through its own features implementation/unstable-ktls), ours-quic, the modules trojan and vless, the binary.

Tests ​

Unit tests in src/, including checking the ClientHello against src/testdata/utls-hellos.txt and the REALITY certificate with an ML-DSA-65 signature against what Go and Xray do (src/testdata/reality-mldsa65.txt). Handshakes, REALITY and kTLS end-to-end — in modules/system/tls/tests/.

Fuzz targets in libs/tls13/fuzz/ (through the fuzzing feature): client_hello and reality_auth — the ClientHello parser and the REALITY server's checks; server_message — the client-side handshake parsers, including certificate decompression; x509 — the certificate verifier; records — records read in chunks of any size; handshake_pair — a client and a REALITY server over a relay that damages, drops and reorders records.

The preset versions are recorded in libs/tls13/src/presets.rs.

GPL-3.0-or-later