netio/tls/mod.rs#
Source: lib/netio/tls/mod.rs
tls -- TLS 1.3 servers and clients, and TLS 1.2 towards public servers (RFC 8446, 5246)
Both sides: TLS 1.3 · ChaCha20-Poly1305, AES-128-GCM, AES-256-GCM · X25519MLKEM768
(post-quantum hybrid), X25519, P-256, P-384 · certificates and keys of Ed25519, ECDSA
(P-256 / P-384) and RSA (the web PKI or a CA of one's own) · mutual authentication ·
HelloRetryRequest · KeyUpdate · resumption with session tickets (psk_dhe_ke) · 0-RTT with
replay protection (opt-in) · OCSP stapling (status_request, RFC 6066 8 / RFC 8446 4.4.2.1).
Public servers (client side, Trust::System / Custom): also TLS 1.2 with ECDHE and AEAD suites.
Not here: external PSKs, psk_ke, TLS 1.2 resumption, renegotiation, OCSP or CRLs fetched by
the client itself.
connect(stream, ClientConfig) -> TlsStream accept(stream, ServerConfig) -> TlsStream
TlsStream implements Stream (HTTP runs over it unchanged); peer_certs() after the handshake
Resumption: ServerConfig::tickets (Tickets: keys, lifetime), ClientConfig::sessions
(Sessions: a client's tickets per server); TlsStream::resumed()
0-RTT: ServerConfig::early_data (bytes; 0 = none), connect_early(stream, cfg, data) ->
(TlsStream, bytes taken), TlsStream::read_early (a server: data that may be a replay)
OCSP: ServerConfig::ocsp (Staples: the responses to staple, netio::ocsp::refresh fetches
them); ClientConfig::revocation (Revocation::Stapled by default: a stapled response is
checked, a revoked certificate refused); TlsStream::ocsp_response
Identity::new(chain, key) / Identity::from_pem(certs, key): a chain and its key (PrivateKey:
Ed25519, ECDSA, RSA; PKCS #8, SEC 1, PKCS #1)
Trust::Anchors(CAs) / Pinned(SHA-256 of the leaf) / Any (trust on first use: the caller
shows the fingerprint and pins it) / System(extra CAs) (the operating system's check, the
extra CAs under its policy) / Dane(TLSA records) (RFC 6698, 7671: crate::dane)
conn.rs is the I/O-free core (feed bytes, take bytes); this file drives it over a Stream.
struct Identitya certificate chain (DER, leaf first) and the leaf's private key (Ed25519, ECDSA, RSA)chain: Vec<Vec<u8>>key: PrivateKey
impl Identity
fn new(chain: Vec<Vec<u8>>, key: impl Into<PrivateKey>) -> Identitya chain (DER, leaf first) and its key (an ed25519::Keypair or an x509::PrivateKey)fn from_pem(certs: &str, key: &str) -> Result<Identity, String>from PEM: the certificates (leaf first, as a CA hands them out) and the key ("PRIVATE KEY", "EC PRIVATE KEY" or "RSA PRIVATE KEY"; not an encrypted one); the key must be the leaf'senum TrustAnchors(Vec<Cert>)-- certificates issued by one of these CAsPinned(Vec<[u8; 32]>)-- exactly these leaf certificates (SHA-256 of the DER)Any-- anything (only to read the fingerprint for a trust decision)System(Vec<Cert>)-- the operating system's certificate authorities, and these as well (a company CA): the system checks the chain (Windows CryptoAPI, Apple's Security framework with the stapled OCSP response; on Linux the distribution's bundle: crate::roots), these under its own policy (Windows with them as its roots where its own give no path, Apple as anchors beside its own; Linux where the bundle gives no path); the alert follows its reason (expired, revoked, another name or usage, unknown CA). Allows TLS 1.2 and the public suitesCustom(Verifier)-- a check the program brings (a PKI of its own, keys pinned by their SPKI, Certificate Transparency, the platform's verifier, a test peer); a client offers with it what it offers with System (TLS 1.2, the public suites)Dane(Vec<crate::dane::Tlsa>)-- DANE: the server's TLSA records, from a lookup DNSSEC found secure (crate::dane::lookup, dane::trust): DANE-EE the server's key itself, DANE-TA a CA of its chain, PKIX-TA / PKIX-EE with the system's check (crate::dane::check, RFC 7671); without a usable record the system's check alone. Revocation and the stapled response count where the system's check decides, as with System, not for DANE-EE and DANE-TA. Allows TLS 1.2 and the public suites, as System
type Verifier = Arc<dyn Fn(&[Cert], &str, i64) -> Result<(), String> + Send + Sync>the check of Trust::Custom: the certificates the peer sent that could be read (leaf first), the name the client asked for ("" for a client's certificate) and the time (Unix seconds) -> Ok, or why they are refused (the handshake ends with bad_certificate)struct ClientConfiga client's side:ClientConfig { identity: Some(id), ..ClientConfig::new(trust, "host") }trust: Trustidentity: Option<Identity>server_name: String-- the name the certificate must carry (DNS name or IP address); may be empty only with Trust::Pinned / Trust::Any, which do not check italpn: Vec<String>-- the application protocols to offer (ALPN, RFC 7301: "h2", "http/1.1", "acme-tls/1"), in order of preference; the server's choice is TlsStream::alpn. An empty name or one over 255 bytes is left out, and the names after the first 8 KiB of them; none left: no ALPN offeredsessions: Option<Arc<Sessions>>-- session tickets to resume with (TLS 1.3): the connection offers one of this server and keeps the ones it gets. Several configs may share a cache: it keeps the tickets by server name, trust and client identity. None: no resumptionrevocation: Revocation-- what a stapled OCSP response decides (with Trust::Anchors, System and Custom; a pinned certificate or Trust::Any asks for none)
impl ClientConfig
fn new(trust: Trust, server_name: &str) -> ClientConfigthis trust and server name; no client certificate, no ALPN, no resumption; stapled OCSP responses checked (Revocation::Stapled)enum Revocationthe OCSP response a client asks the server to staple (status_request): a usable one is signed by the issuer of the server's certificate (in the chain or an anchor) or a responder it named, about that certificate, and current (RFC 6960; x509::ocsp::check)Off-- none asked forStapled-- asked for and checked: a certificate a usable response calls revoked is refused (certificate_revoked); without a usable response the handshake goes on, unless the certificate must be stapled (RFC 7633, Cert::must_staple)Required-- a usable response that says good is required (bad_certificate_status_response without one)
struct Hellowhat a ClientHello asks for: the program's ServerConfig::select picks the identity by itserver_name: String-- the name of server_name (SNI), lower case; empty: nonealpn: Vec<Vec<u8>>-- the application protocols offered (ALPN), as the client sent them: byte strings (RFC 7301 3.1), b"h2", b"http/1.1"sig_algs: Vec<u16>-- the signature schemes the client takes (TLS code points)
type Select = Arc<dyn Fn(&Hello) -> Option<Identity> + Send + Sync>ServerConfig::select: the identity for a ClientHello, or None for the configured onesstruct ServerConfiga server's side:ServerConfig { client_auth: Some(trust), ..ServerConfig::new(identity) }identity: Identityidentities: Vec<Identity>-- more certificates (SNI: other names; or other key types for the same name, as an ECDSA and an RSA certificate): the server shows the first of these and thenidentitythat names the server_name the client asked for and whose key makes a signature the client takes; without such oneidentity, or the first whose key fits when its key does notselect: Option<Select>-- a choice of the program's own, asked before the lists (certificates renewed while the server runs, ACME's tls-alpn-01 challenge); None from it: the lists decideclient_auth: Option<Trust>-- ask for a client certificate trusted like thisclient_auth_optional: bool-- a client without a certificate is accepted too (one that sends one must pass the check); for a port that consoles and agents sharealpn: Vec<String>-- the application protocols the server speaks (ALPN), in its order of preference; a client that offers protocols, none of them these, is refused (no_application_protocol, RFC 7301 3.2). Empty: ALPN is not answered. "acme-tls/1" (RFC 8737, acme::TlsAlpn01) counts only for a client that offers it alonetickets: Option<Arc<Tickets>>-- session tickets (TLS 1.3 resumption): sent after each handshake and taken back for a handshake without certificates. A resumed connection keeps the client certificates of its first handshake (peer_certs) for up to 7 days, while the same client_auth trust asks for them and takes them again (each resumption checks them). None: no ticketsearly_data: u32-- 0-RTT (needs tickets): the most bytes of early data a client may send with a ticket; 0 (the default): none. A ticket that allows more than the server takes now is resumed without 0-RTT. Early data can be replayed by an attacker (RFC 8446 8, E.5): the server takes it only from a ClientHello within 10 seconds of the ticket's age and the first time it comes (Tickets::replay_check), keeps it apart (TlsStream::read_early) and hands it to read() only once the handshake is done. With 0-RTT taken, accept returns after the server's flight, before the client's Finishedocsp: Option<Arc<Staples>>-- the OCSP responses to staple for the certificates shown, to clients that ask for one (netio::ocsp::refresh keeps them current). None: no stapling
impl ServerConfig
fn new(identity: Identity) -> ServerConfigthis identity; no client certificates, no ALPN, no ticketsenum TlsErrorAlert(u8, &'static str)-- we ended the connection with this alertPeerAlert(u8)-- the peer ended it with this alertNet(NetError)Closed
fn alert_name(a: u8) -> Stringstruct TlsStreamTLS over a stream
impl TlsStream
fn test_send_empty_record(&self) -> NetResult<()>(tests) send an application data record without datafn peer_certs(&self) -> Vec<Cert>the peer's certificate chain (leaf first)fn last_error(&self) -> Option<TlsError>fn version(&self) -> (&'static str, u16)"TLS 1.3" / "TLS 1.2" and the cipher suite numberfn group(&self) -> u16the key exchange group (kx: X25519_MLKEM768, X25519, SECP256R1 ...)fn alpn(&self) -> Option<String>the application protocol both sides agreed on (ALPN), if anyfn server_name(&self) -> Stringa server: the name the client asked for (SNI, lower case; empty: none)fn resumed(&self) -> boolthe handshake resumed a session (a ticket instead of certificates)fn ocsp_response(&self) -> Option<Vec<u8>>a client: the OCSP response the server stapled for its certificate (DER), when it sent one; whether it was usable decided ClientConfig::revocationfn read_early(&self, buf: &mut [u8]) -> NetResult<usize>a server that took 0-RTT: the client's early data as it comes, before the handshake is done; Ok(0) at its end (or when none was taken). An attacker may have replayed it (RFC 8446 8): act on it only where twice does no harm (RFC 8470). What is not read here comes first from read() once the handshake is donefn connect(inner: Box<dyn Stream>, cfg: ClientConfig) -> Result<TlsStream, TlsError>client side: handshake overinnerfn connect_early(inner: Box<dyn Stream>, cfg: ClientConfig, early: &[u8]) -> Result<(TlsStream, usize), TlsError>client side with 0-RTT:earlygoes right after the ClientHello when a ticket of cfg.sessions allows early data (as much as the ticket takes). -> the stream and the bytes ofearlythe server took: 0 when none went (no such ticket) or the server refused them; the caller sends the rest with write_all, if it still applies (TlsStream::alpn may differ: RFC 8446 4.2.10). Early data can be replayed by an attacker: only for requests that do no harm twicefn accept(inner: Box<dyn Stream>, cfg: ServerConfig) -> Result<TlsStream, TlsError>server side: handshake overinner