Skip to content

tls ​

TLS the way sing-box 1.14.4 builds it from options (common/tls): clients for outbounds and downloads, servers for inbounds, the root store. rustls with ring; our own TLS 1.3 (ours-tls13) where rustls does not fit: uTLS fingerprints, REALITY, fragmentation, kTLS.

Crateours-mod-tls, modules/system/tls
Binary featurethe module is always in the list; tls → ours-mod-tls/implementation, unstable-ktls → ours-mod-tls/unstable-ktls; hysteria, hysteria2, tuic, dns-quic, dns-h3, transport-xhttp → ours-mod-tls/quic
Crate featuresimplementation (rustls, ring, ours-tls13/implementation, …), unstable-ktls (+ ours-tls13/unstable-ktls), quic (ours-quic: QUIC TLS; turns on implementation)

requires ​

Nothing. The module is required by everyone who builds TLS or takes the root store: http, anytls, shadowtls, trojan, vless, hysteria, hysteria2, tuic, router (remote rule-sets), group (probes), dns (DoT/DoH/DoQ), adblock, subscriptions, mixed, transport, transport-xhttp, and shadowsocks with v2ray-plugin.

provides ​

  • Provide::Unit(Store) — one Certificates store per instance (OnceLock). build prepares new roots (prepare) and publishes the TLS factory over the store, ours_tls_options::Tls (Tls::of(services)): HTTP download clients and available(), which ours-http, router, group, adblock and subscriptions take, and clients and servers over streams (client, client_sealed for ShadowTLS v3, server, browser), which http, mixed, anytls, trojan, vless, shadowtls and the v2ray-plugin of shadowsocks take — none of them names this module. With the quic feature it also publishes QUIC TLS over the same store, ours_quic::tls::Factory, which the QUIC protocols, the QUIC transport and dns take. The store itself is not in the services. commit replaces the roots and turns on file watching; close stops the watching. A failed reload does not touch the roots.
  • Provide::Point("tls"), Provide::Block { block: "tls", key: "inbound" | "outbound" } — the entities' tls block, decoded by ours_tls_options::block: presence enables TLS, the server name is sni, reality, utls, ech are their own nested blocks; reality.handshake.server is host:port (split_address, without a port — an error). Fields that make sense only together are blocks (GROUPED): fragment: {packet, record, fallback_delay}, ktls: {tx, rx}, spoof: {sni, method}, on the server client_auth: {mode, certificate, certificate_path, public_key_sha256}; lowered spreads them into flat sing-box option names, and rejects the same names in the config with a hint (`kernel_tx` is `ktls.tx`).
  • Provide::Section { key: "certificate" } — the roots section, one component.

Syntax: the tls block, certificate.

Dependencies ​

ours-tls13 (our own TLS 1.3, REALITY, uTLS, kTLS — through ours-ktls-sys), ours-net-sys (watching files), ours-api, ours-codec, ours-json, rustls, tokio-rustls, ring, x25519-dalek, arc-swap, base64 (REALITY keys, base64url).

Design ​

  • src/lib.rs — the shared parts: TcpTls (Own — TLS 1.3 of our own stream, in the process or in the kernel; Other — older versions through rustls), kTLS warnings as in sing-box, versions/cipher suites/curves in Go's terms, PEM.
  • src/client.rs — Client (std_client.go): the rustls and tls13 (uTLS/REALITY) engines. with_compatibility — the protocol reports whether kTLS benefits it; http/http_pool — download clients; quic(alpn) — the rustls configuration for QUIC protocols (uTLS, REALITY, fragmentation, kTLS are rejected there); with_session_id — our own session ID in the ClientHello (uTLS only). ClientStream::into_kernel hands over a stream the relay can splice.
  • src/server.rs — Server (std_server.go), the REALITY server, rereading the certificate, the key and the CA when the files change.
  • src/roots.rs — Certificates: the roots clients verify servers by; files and directories are reread when they change.
  • src/module.rs — MODULE and the Store unit; src/certificate.rs — the section's options.
  • src/contract.rs — the contract's traits: TlsClient for Client, TlsServer for Server, ours_tls_options::TlsFactory for Certificates. Over TCP, TLS 1.3 of our own stream comes back as Stream::Spliced(KernelStream), anything older as Stream::Boxed; trojan and vless take ours_tls13::KernelStream out of the stream once per connection (Vision, kTLS).
  • src/quic.rs (quic + implementation) — ours_quic::tls::QuicTls for Certificates: the configurations of Client::quic/quic_chrome and Server::quic as ClientTls/ServerTls.
  • src/disabled.rs — a build without implementation: the types are kept, the handshake fails before sockets are opened, the streams are uninhabited types (the splice interface remains).

kTLS ​

With unstable-ktls, the TLS 1.3 keys are handed to the kernel after the handshake (ktls.tx/ktls.rx, in the options kernel_tx/kernel_rx), and the relay moves data as over an ordinary socket. Without the feature, kernel_* are rejected when the client or the server is built ("kTLS is not built in"). Protocols with Vision (VLESS) enable kTLS for a direction when Vision finishes padding.

Tests ​

modules/system/tls/tests/: handshake.rs (clients and servers from sing-box options through a pipe), ktls.rs (kTLS on loopback, KeyUpdate, record_size_limit, warnings; needs the tls kernel module), reality.rs; unit tests in roots.rs, disabled.rs. The block's tests are in crates/tls-options/src/block.rs.

GPL-3.0-or-later