~/bend-docscommunity

client.bend checks

raw source on the hub · import element-tls13@0.1.0.0/client.bend as Client

8 imports
import Base
import element-crypto@0.1.0.0/suite.bend as Suite
import ./tls13.bend as T
import ./tls12.bend as T12
import element-crypto@0.1.0.0/x25519.bend as X
import element-crypto@0.1.0.0/ecdh.bend as Ecdh
import element-crypto@0.1.0.0/poly1305.bend as Poly
import element-x509@0.1.0.0/cert.bend as X509

Types

type S12 source · line 26 · raw

Data

TLS 1.2 (tls/tls12.bend), between ServerHello and application data: the suite, the server random, the server's ECDHE key (empty until ServerKeyExchange), whether the server echoed extended_master_secret, the master secret (empty until ServerHelloDone), whether ChangeCipherSpec came.

type Phase source · line 32 · raw

Data

PHs12: the server's plaintext flight (Certificate, ServerKeyExchange, ServerHelloDone). PFin12: ours is sent, its ChangeCipherSpec and Finished are awaited. PApp12: application data under TLS 1.2 records.

type Keys source · line 43 · raw

Data

Traffic keys in use: read (server) and write (client), with sequence numbers.

type Sec source · line 47 · raw

Data

The handshake secret and the two handshake traffic secrets (from CH..SH).

type Trust source · line 54 · raw

Data

How to authenticate the server: check on, the chain must reach one of roots for host at now (seconds since 1970) and CertificateVerify must verify with the leaf key; check off (an explicit opt-out), the connection is encrypted but unauthenticated.

type Cs source · line 57 · raw

Data

type MK source · line 345 · raw

Data

type IK source · line 433 · raw

Data

type MK12 source · line 608 · raw

Data

type R12 source · line 699 · raw

Data

What a record means to TLS 1.2, by phase and type; a match then runs only the branch it names (decryption is never computed for nothing).

Definitions

def no_keys source · line 64 · raw

Keys

def keys_for source · line 68 · raw

@+read_secret:List<&2, U32> -> @+write_secret:List<&2, U32> -> @+wseq:U32 -> @+n:U32 -> Keys

n: the suite's key length (T.key_len), which also selects its AEAD.

def key_len_of source · line 71 · raw

@k:Keys -> U32

def start source · line 77 · raw

@+trust:Trust -> @+host:String -> @+rnd:List<&2, U32> -> @+req:List<&2, U32> -> Cs

rnd: 96 random bytes (32 random, 32 session id, 32 x25519 private key).

def fail source · line 82 · raw

@+msg:String -> Phase

def with_send source · line 87 · raw

@st:Cs -> @+more:List<&2, U32> -> Cs

def clear_send source · line 92 · raw

@st:Cs -> Cs

def with_phase source · line 97 · raw

@st:Cs -> @+p:Phase -> Cs

def with_buf source · line 102 · raw

@st:Cs -> @+b:List<&2, U32> -> Cs

def add_buf source · line 107 · raw

@st:Cs -> @+more:List<&2, U32> -> Cs

def bump_rseq source · line 112 · raw

@k:Keys -> Keys

def phase_of source · line 119 · raw

@st:Cs -> Phase

def send_of source · line 124 · raw

@st:Cs -> List<&2, U32>

def out_of source · line 130 · raw

@st:Cs -> List<&2, U32>

Application data received so far (chunks are kept newest first).

def seal_app source · line 139 · raw

@p:Phase -> @+k:List<&2, U32> -> @+iv:List<&2, U32> -> @+seq:U32 -> @+req:List<&2, U32> -> List<&2, U32>

Another request on a session whose last response was whole (keep-alive, tls/io.bend's pool): sealed as finish sealed the first, with the write key at its next sequence number (TLS 1.3 or 1.2 records); the plaintext read so far is dropped, the next response starts empty.

def again source · line 146 · raw

@st:Cs -> @+req:List<&2, U32> -> Cs

def out_chunks source · line 155 · raw

@st:Cs -> List<&2, List<&2, U32>>

the plaintext as received, newest record first (add_out prepends)

def out_len source · line 160 · raw

@st:Cs -> U32

def buf_of source · line 165 · raw

@st:Cs -> List<&2, U32>

def on_sh_keys source · line 172 · raw

@st:Cs -> @+sh:List<&2, U32> -> @+secret:List<&2, U32> -> Cs

def on_sh_shared source · line 182 · raw

@m:Maybe<&2, List<&2, U32>> -> @st:Cs -> @+sh:List<&2, U32> -> Cs

RFC 8446 §7.4.2: an all-zero X25519 secret aborts the handshake.

def on_sh_ok source · line 189 · raw

@st:Cs -> @+sh:List<&2, U32> -> Cs

def on_sh_check source · line 194 · raw

@ok:Bool -> @st:Cs -> @+sh:List<&2, U32> -> @+why:String -> Cs

def sh_problem source · line 201 · raw

@+sh:List<&2, U32> -> String

def first_msg source · line 207 · raw

@m:0xb50db566077215d442e9798060f35e33/tls13.Msgs -> List<&2, U32>

def has_msg source · line 216 · raw

@m:0xb50db566077215d442e9798060f35e33/tls13.Msgs -> Bool

def verify_ok source · line 227 · raw

@+got:List<&2, U32> -> @+want:List<&2, U32> -> Bool

def finish source · line 232 · raw

@st:Cs -> @+m:List<&2, U32> -> Cs

Server Finished verified: derive application secrets, send the client Finished (after a compatibility ChangeCipherSpec) and the request.

def on_finished source · line 252 · raw

@ok:Bool -> @st:Cs -> @+m:List<&2, U32> -> Cs

def append_tr source · line 259 · raw

@st:Cs -> @+m:List<&2, U32> -> Cs

def keep_cert source · line 264 · raw

@st:Cs -> @+m:List<&2, U32> -> Cs

def trust_of source · line 269 · raw

@st:Cs -> Trust

def tr_of source · line 274 · raw

@st:Cs -> List<&2, U32>

def certs_of source · line 279 · raw

@st:Cs -> List<&2, U32>

def entries source · line 286 · raw

@fuel:Nat -> @+bs:List<&2, U32> -> List<&2, List<&2, U32>>

Certificate (4.4.2): context<0..255>, then entries of cert_data<1..2^24-1> and extensions<0..2^16-1>. The DER certificates, leaf first.

def cert_ders source · line 296 · raw

@+m:List<&2, U32> -> List<&2, List<&2, U32>>

def first_der source · line 301 · raw

@ds:List<&2, List<&2, U32>> -> List<&2, U32>

def verify_problem source · line 308 · raw

@+t:Trust -> @+st:Cs -> @+m:List<&2, U32> -> String

def on_problem source · line 318 · raw

@+why:String -> @+st:Cs -> @+m:List<&2, U32> -> Cs

def on_verify source · line 322 · raw

@t:Trust -> @+st:Cs -> @+m:List<&2, U32> -> Cs

CertificateVerify (4.4.3): authenticate the server, unless the caller opted out.

def expected_fin source · line 331 · raw

@st:Cs -> List<&2, U32>

def still_hs source · line 338 · raw

@p:Phase -> Bool

def kind_of source · line 353 · raw

@+typ:U32 -> @+hs:Bool -> MK

def on_msg source · line 361 · raw

@k:MK -> @+st:Cs -> @+m:List<&2, U32> -> Cs

One handshake message in the Hs phase (a match: only its branch computes).

def on_msgs source · line 376 · raw

@msgs:List<&2, List<&2, U32>> -> @+st:Cs -> Cs

def with_hsbuf source · line 383 · raw

@st:Cs -> @+b:List<&2, U32> -> Cs

def hsbuf_of source · line 388 · raw

@st:Cs -> List<&2, U32>

def msgs_of source · line 393 · raw

@m:0xb50db566077215d442e9798060f35e33/tls13.Msgs -> List<&2, List<&2, U32>>

def rest_of source · line 398 · raw

@m:0xb50db566077215d442e9798060f35e33/tls13.Msgs -> List<&2, U32>

def on_hs_bytes source · line 404 · raw

@+st:Cs -> @+content:List<&2, U32> -> Cs

Decrypted handshake bytes: append, split into whole messages, handle them.

def keys_of source · line 410 · raw

@st:Cs -> Keys

def with_keys source · line 415 · raw

@st:Cs -> @+k:Keys -> Cs

def add_out source · line 420 · raw

@st:Cs -> @+more:List<&2, U32> -> Cs

def open_record source · line 425 · raw

@+k:Keys -> @+hdr:List<&2, U32> -> @+body:List<&2, U32> -> Maybe<&2, 0xb50db566077215d442e9798060f35e33/tls13.Inner>

def alert_text source · line 430 · raw

@+content:List<&2, U32> -> String

def inner_kind source · line 439 · raw

@+typ:U32 -> @+app:Bool -> IK

def on_inner_k source · line 444 · raw

@k:IK -> @+st:Cs -> @+content:List<&2, U32> -> Cs

def on_inner source · line 457 · raw

@+app:Bool -> @st:Cs -> @i:0xb50db566077215d442e9798060f35e33/tls13.Inner -> Cs

A decrypted record: an alert, application data, or handshake bytes. A match on the kind, so only the right branch runs.

def on_opened source · line 462 · raw

@+app:Bool -> @m:Maybe<&2, 0xb50db566077215d442e9798060f35e33/tls13.Inner> -> @+st:Cs -> Cs

def rec_encrypted source · line 469 · raw

@enc:Bool -> @+app:Bool -> @+st:Cs -> @+hdr:List<&2, U32> -> @+body:List<&2, U32> -> Cs

def s12_of source · line 478 · raw

@p:Phase -> S12

def s12_spub source · line 487 · raw

@s:S12 -> @+pub:List<&2, U32> -> S12

def s12_ccs_on source · line 492 · raw

@s:S12 -> S12

def sr_of source · line 497 · raw

@s:S12 -> List<&2, U32>

def ms_of source · line 502 · raw

@s:S12 -> List<&2, U32>

def ccs_of source · line 507 · raw

@s:S12 -> Bool

def has_key_of source · line 512 · raw

@s:S12 -> Bool

def with_s12_spub source · line 517 · raw

@st:Cs -> @+pub:List<&2, U32> -> Cs

def ske_problem source · line 525 · raw

@+t:Trust -> @+st:Cs -> @+m:List<&2, U32> -> String

ServerKeyExchange: the curve must be x25519 (a 32-byte key) or secp256r1 (a 65-byte uncompressed point); with check on, the chain must verify and the leaf must have signed client_random | server_random | params.

def ske_then source · line 537 · raw

@ok:Bool -> @+why:String -> @+st:Cs -> @+m:List<&2, U32> -> Cs

def on_ske_problem source · line 544 · raw

@+why:String -> @+st:Cs -> @+m:List<&2, U32> -> Cs

def pms_of source · line 550 · raw

@p256:Bool -> @+priv:List<&2, U32> -> @+spub:List<&2, U32> -> Maybe<&2, List<&2, U32>>

The key exchange follows the server's key, as the AEAD follows the key length: a 65-byte point is P-256 (crypto/ecdh.bend), 32 bytes x25519. The P-256 scalar is derived from the same 32 secret bytes under a label.

def ecdhe_pub source · line 557 · raw

@p256:Bool -> @+priv:List<&2, U32> -> List<&2, U32>

def flight_go source · line 564 · raw

@m:Maybe<&2, List<&2, U32>> -> @+pub:List<&2, U32> -> @+suite:U32 -> @+sr:List<&2, U32> -> @+spub:List<&2, U32> -> @+ems:Bool -> @+priv:List<&2, U32> -> @+tr:List<&2, U32> -> @buf:List<&2, U32> -> @hsbuf:List<&2, U32> -> @hs:Sec -> @req:List<&2, U32> -> @out:List<&2, List<&2, U32>> -> @+send:List<&2, U32> -> @certs:List<&2, U32> -> @trust:Trust -> @outn:U32 -> @+shd:List<&2, U32> -> Cs

def flight_with source · line 583 · raw

@s:S12 -> @+priv:List<&2, U32> -> @+tr:List<&2, U32> -> @buf:List<&2, U32> -> @hsbuf:List<&2, U32> -> @hs:Sec -> @req:List<&2, U32> -> @out:List<&2, List<&2, U32>> -> @+send:List<&2, U32> -> @certs:List<&2, U32> -> @trust:Trust -> @outn:U32 -> @+shd:List<&2, U32> -> Cs

def client_flight source · line 593 · raw

@st:Cs -> @+shd:List<&2, U32> -> Cs

ServerHelloDone: answer with ClientKeyExchange, ChangeCipherSpec and an encrypted Finished (seq 0); the request waits for the server's Finished. ServerHelloDone enters the transcript here, once (the session hash of the extended master secret covers it, then ClientKeyExchange).

def on_done12 source · line 598 · raw

@has_key:Bool -> @+st:Cs -> @+m:List<&2, U32> -> Cs

def s12_has_key source · line 605 · raw

@p:Phase -> Bool

def in_hs12 source · line 616 · raw

@p:Phase -> Bool

def kind12_of source · line 623 · raw

@+typ:U32 -> @+hs:Bool -> MK12

def on_msg12 source · line 630 · raw

@k:MK12 -> @+st:Cs -> @+m:List<&2, U32> -> Cs

def on_msgs12 source · line 645 · raw

@msgs:List<&2, List<&2, U32>> -> @+st:Cs -> Cs

def on_hs12_bytes source · line 652 · raw

@+st:Cs -> @+content:List<&2, U32> -> Cs

def open12_record source · line 656 · raw

@+k:Keys -> @+typ:U32 -> @+body:List<&2, U32> -> Maybe<&2, 0xb50db566077215d442e9798060f35e33/tls13.Inner>

def app12 source · line 662 · raw

@st:Cs -> Cs

The server's Finished verified: send the request (write seq 1).

def expected12 source · line 670 · raw

@st:Cs -> List<&2, U32>

def fin12_check source · line 675 · raw

@ok:Bool -> @+st:Cs -> Cs

def on_fin12 source · line 682 · raw

@m:Maybe<&2, 0xb50db566077215d442e9798060f35e33/tls13.Inner> -> @+st:Cs -> Cs

def ccs_seen source · line 692 · raw

@st:Cs -> Cs

def r12_hs source · line 707 · raw

@+typ:U32 -> R12

def r12_fin source · line 710 · raw

@+typ:U32 -> @+ccs:Bool -> R12

def r12_app source · line 716 · raw

@+typ:U32 -> R12

def on_r12 source · line 719 · raw

@k:R12 -> @+st:Cs -> @+typ:U32 -> @+body:List<&2, U32> -> Cs

def is_tls12 source · line 737 · raw

@+sh:List<&2, U32> -> Bool

A TLS 1.2 answer: no supported_versions, legacy_version 0x0303.

def sh12_problem source · line 740 · raw

@+sh:List<&2, U32> -> String

def on_sh12_ok source · line 745 · raw

@st:Cs -> @+sh:List<&2, U32> -> @+rest:List<&2, U32> -> Cs

After a TLS 1.2 ServerHello the same bytes often carry the next messages.

def on_sh12_check source · line 752 · raw

@ok:Bool -> @st:Cs -> @+sh:List<&2, U32> -> @+rest:List<&2, U32> -> @+why:String -> Cs

def on_sh source · line 759 · raw

@v12:Bool -> @st:Cs -> @+sh:List<&2, U32> -> @+all:List<&2, U32> -> Cs

def sh_ready source · line 767 · raw

@ready:Bool -> @st:Cs -> @+m:0xb50db566077215d442e9798060f35e33/tls13.Msgs -> @+all:List<&2, U32> -> Cs

A plaintext handshake record while waiting for ServerHello.

def on_plain_hs source · line 774 · raw

@st:Cs -> @+body:List<&2, U32> -> Cs

def on_record source · line 782 · raw

@p:Phase -> @+st:Cs -> @+typ:U32 -> @+hdr:List<&2, U32> -> @+body:List<&2, U32> -> Cs

A whole record, dispatched on the phase (a match: one branch computes).

def rec_len source · line 802 · raw

@+buf:List<&2, U32> -> U32

def whole_record source · line 805 · raw

@+buf:List<&2, U32> -> Bool

def live source · line 808 · raw

@p:Phase -> Bool

def step_record source · line 817 · raw

@go:Bool -> @+st:Cs -> Cs

def pump source · line 832 · raw

@fuel:Nat -> @go:Bool -> @+st:Cs -> Cs

Process every whole record in the buffer, stopping as soon as none is left (go is computed by the caller: a match cannot scrutinize a computed value).

def feed source · line 844 · raw

@+st:Cs -> @+bytes:List<&2, U32> -> Cs

def failed_msg source · line 848 · raw

@p:Phase -> String