JFLib

netpkt/tls.rs#

Source: lib/netpkt/tls.rs

tls -- TLS 1.3 without I/O (RFC 8446): a core for both sides of a connection: feed it the bytes
that came, take the bytes to send, the plain text and the events; no sockets, no clock.  Random
bytes come from a seed the caller passes in, so a connection is the same byte for byte for the same
seed.  The records and the handshake are the real thing, a peer of another make understands them
(apps/coretest group tlscore talks to netio::tls, tools/tls_interop.py --core to OpenSSL)

  PORT                     443
  CT_*, HS_*, AL_*, EXT_*  content types, handshake types, alerts, extensions; ct_name(t),
                           hs_name(t), alert_name(a), ext_name(e), suite_name(s), group_name(g),
                           sig_name(s)
  SUITE, VERSION           the one suite, the version's name
  Identity { chain, key }  a certificate chain (DER, leaf first) and the leaf's Ed25519 key
  ClientCfg { name, anchors, now, insecure, accept }   the name asked for (also the SNI, unless an
                           address), what the client trusts, the time for the check; insecure takes
                           any certificate, accept these fingerprints (SHA-256 of the leaf) whatever
                           their faults
  Core::client(cfg, seed) / Core::server(identity, seed)
    push(bytes)            what the connection brought (any split)
    take_out()             the bytes to send
    take_events()          what happened (Event)
    read() -> bytes        the plain text that came and was not read yet
    send(bytes)            plain text, in records of at most MAX_PLAIN bytes (after Established)
    close()                close_notify; dead(): nothing more will come or go
    established(), peer_closed(), name() (the SNI a client sent), peer_certs(), faults()
    (x509 F_* bits of the server's chain), excused() (it has faults and went on all the same),
    failure(), footprint()
  Event                    Hello { name }, Certificate { faults }, Established, PeerClosed, Failed(Failure)
  Failure { alert, ours, why }   the alert that ended it, whether we sent it, a word for the log
  schedule(ecdhe) -> (handshake, master), derive(secret, label, hash), expand_label, finished,
  traffic_keys             the key schedule (7.1, 7.3, 4.4.4)
  Part, dissect(segment)   what a dissector sees in a TCP segment: records with their type,
                           version and length, or bytes that do not start a record

What it speaks: TLS_CHACHA20_POLY1305_SHA256, X25519 and Ed25519 signatures and certificates
(x509.rs); ClientHello, ServerHello, EncryptedExtensions, Certificate, CertificateVerify, Finished,
KeyUpdate received, alerts.  Not here: HelloRetryRequest (a client without an X25519 share gets
handshake_failure), client certificates, resumption and 0-RTT, the other suites (netio::tls has a
wider client), the middlebox-compatibility ChangeCipherSpec (one received is ignored).  A client
checks the chain itself; a fault ends the handshake with the alert for it unless the configuration
excuses it.  The CertificateVerify signature and both Finished are checked always.
/// the well-known port of HTTP over TLS
PORT: u16 = 443
/// the one cipher suite
SUITE: u16 = 0x1303
/// the version as a dialog names it
VERSION: &str = "TLS 1.3"
CT_CCS: u8 = 20
CT_ALERT: u8 = 21
CT_HANDSHAKE: u8 = 22
CT_DATA: u8 = 23
HS_CLIENT_HELLO: u8 = 1
HS_SERVER_HELLO: u8 = 2
HS_NEW_SESSION_TICKET: u8 = 4
HS_ENCRYPTED_EXTENSIONS: u8 = 8
HS_CERTIFICATE: u8 = 11
HS_CERTIFICATE_VERIFY: u8 = 15
HS_FINISHED: u8 = 20
HS_KEY_UPDATE: u8 = 24
AL_CLOSE_NOTIFY: u8 = 0
AL_UNEXPECTED_MESSAGE: u8 = 10
AL_BAD_RECORD_MAC: u8 = 20
AL_RECORD_OVERFLOW: u8 = 22
AL_HANDSHAKE_FAILURE: u8 = 40
AL_BAD_CERTIFICATE: u8 = 42
AL_CERTIFICATE_EXPIRED: u8 = 45
AL_ILLEGAL_PARAMETER: u8 = 47
AL_UNKNOWN_CA: u8 = 48
AL_DECODE_ERROR: u8 = 50
AL_DECRYPT_ERROR: u8 = 51
AL_PROTOCOL_VERSION: u8 = 70
AL_INTERNAL_ERROR: u8 = 80
AL_MISSING_EXTENSION: u8 = 109
AL_UNSUPPORTED_EXTENSION: u8 = 110
EXT_SERVER_NAME: u16 = 0
EXT_GROUPS: u16 = 10
EXT_SIG_ALGS: u16 = 13
EXT_VERSIONS: u16 = 43
EXT_KEY_SHARE: u16 = 51
/// the largest plain fragment
MAX_PLAIN: usize = 1 << 14
/// the largest record body on the wire
MAX_RECORD: usize = MAX_PLAIN + 256
  • fn ct_name(t: u8) -> &'static str the name of a content type

  • fn hs_name(t: u8) -> &'static str the name of a handshake type

  • fn alert_name(a: u8) -> &'static str the name of an alert (RFC 8446 6)

  • fn ext_name(e: u16) -> &'static str the name of an extension

  • fn suite_name(s: u16) -> &'static str the name of a cipher suite

  • fn group_name(g: u16) -> &'static str the name of a key exchange group

  • fn sig_name(s: u16) -> &'static str the name of a signature scheme

  • fn expand_label(secret: &[u8], label: &str, context: &[u8], out: &mut [u8]) HKDF-Expand-Label(secret, label, context, len) (7.1)

  • fn derive(secret: &[u8; 32], label: &str, hash: &[u8; 32]) -> [u8; 32] Derive-Secret(secret, label, transcript hash)

  • fn schedule(ecdhe: &[u8; 32]) -> ([u8; 32], [u8; 32]) the handshake secret and the master secret from the (EC)DHE secret (no PSK)

  • fn finished(base: &[u8; 32], transcript: &[u8; 32]) -> [u8; 32] verify_data of a Finished (4.4.4)

  • fn traffic_keys(secret: &[u8; 32]) -> ([u8; 32], [u8; 12]) the AEAD key and IV of a traffic secret (7.3)

  • struct Identity a certificate chain (DER, leaf first) and the leaf's key

    • chain: Vec<Vec<u8>>
    • key: Keypair
  • struct ClientCfg what a client trusts (see the module header)

    • name: String -- the name asked for: what the certificate must name, the SNI unless it is an address
    • anchors: Vec<Cert>
    • now: i64 -- seconds since 1970, for the certificates' times
    • insecure: bool -- any certificate goes
    • accept: Vec<[u8; 32]> -- fingerprints (SHA-256 of the leaf) that go whatever their faults
  • struct Failure what ended a connection

    • alert: u8
    • ours: bool -- we sent the alert (false: the peer did)
    • why: &'static str
  • enum Event what happened

    • Hello { name: String } -- a server: a ClientHello came, with this SNI ("" for none)
    • Certificate { faults: u32 } -- a client: the server's chain came and was checked (x509 F_* bits; 0: nothing wrong)
    • Established
    • PeerClosed -- close_notify came
    • Failed(Failure)
  • struct Core a connection's TLS, one side (see the module header)

impl Core

  • fn client(cfg: ClientCfg, seed: &[u8; 32]) -> Core a client: its ClientHello waits in take_out at once

  • fn server(id: Identity, seed: &[u8; 32]) -> Core a server

  • fn established(&self) -> bool

  • fn peer_closed(&self) -> bool close_notify came

  • fn dead(&self) -> bool nothing more will come or go: a failure, or both sides said close_notify

  • fn name(&self) -> &str the name: the SNI a client sent (a server), the one asked for (a client)

  • fn peer_certs(&self) -> &[Cert] the server's chain, leaf first (a client, once it came)

  • fn faults(&self) -> u32 what is wrong with the server's chain (x509 F_* bits)

  • fn excused(&self) -> bool the server's chain has faults and the handshake went on all the same (ClientCfg)

  • fn failure(&self) -> Option<Failure> what ended the handshake or the connection

  • fn footprint(&self) -> usize the bytes it holds (an estimate for a budget)

  • fn take_out(&mut self) -> Vec<u8>

  • fn take_events(&mut self) -> Vec<Event>

  • fn read(&mut self) -> Vec<u8> the plain text that came and was not read yet

  • fn send(&mut self, data: &[u8]) -> bool plain text to send (nothing before the handshake's end: false)

  • fn close(&mut self) close_notify; nothing is sent after it

  • fn push(&mut self, data: &[u8]) bytes from the peer (any split)

  • enum Part a part of a TCP segment

    • Record { at: usize, len: usize, ty: u8, version: u16, length: usize } -- a record: its place, its bytes here (less than 5 + length when the rest comes later), its type, its version on the wire, its length
    • Rest { at: usize, len: usize } -- bytes that do not start a record (the rest of one)
  • fn dissect(b: &[u8]) -> Vec<Part> the parts of a segment's payload

Source: docs/API.md in the JFLib repository.