handshake

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package handshake performs the TLS handshake and ALPN from a fingerprint plan and keeps session tickets for resumption. Config.VerifyConnection runs in every handshake, resumed ones included.

Index

Constants

This section is empty.

Variables

View Source
var ErrMalformedECH = errors.New("handshake: an ECH config list that does not parse")

ErrMalformedECH is an ECH config list that does not parse (RFC 9849, section 4), from a server's DNS HTTPS record.

View Source
var ErrRestart = errors.New("handshake: connection dropped at the host's first CertificateRequest, to be made again")

ErrRestart reports a connection Client closed at its host's first CertificateRequest, sending nothing, as the profile's browser does (tls.clientAuth.restart): the request goes again on a new connection, which answers it.

Functions

func Client

func Client(ctx context.Context, conn net.Conn, hello fingerprint.Hello, cfg Config) (net.Conn, string, error)

Client performs the TLS handshake on conn with hello and returns the TLS connection and the negotiated ALPN protocol. It honours ctx; on error conn is closed.

func ClientIdentified added in v0.5.0

func ClientIdentified(conn net.Conn) bool

ClientIdentified reports whether conn, a connection Client returned, carries a client certificate: it sent one, or resumed a session one was sent in. Such a connection serves its own host alone (no coalescing), as Chrome's and Firefox's do; false for any other connection.

func ConnState

func ConnState(conn net.Conn) *tls.ConnectionState

ConnState is the TLS state of conn, a connection Client returned, as crypto/tls describes it; nil for any other connection.

func Spec

Spec turns a connection's ClientHello layout into a uTLS spec. GREASE placeholders, empty key shares and GREASE ECH's random fields are filled in by uTLS per connection, so a spec serves one connection.

func State

State is cs as crypto/tls describes it. ExportKeyingMaterial is not carried over: it fails on the result.

func TLSConfig

func TLSConfig(cfg Config) *utls.Config

TLSConfig is the uTLS configuration for cfg: its server name, roots and ticket cache, which QUIC uses for its 0-RTT tickets too. Both TCP and QUIC handshakes use it.

Types

type Config

type Config struct {
	// ServerName is sent in server_name, a trailing dot kept, and verified
	// against the certificate.
	ServerName string
	// OmitSNI sends no server_name; the certificate is still verified
	// against ServerName (Safari, for a host ending in a dot).
	OmitSNI bool
	// RootCAs verifies the server certificate; nil uses the system roots.
	RootCAs *x509.CertPool
	// Tickets, when set, keeps the server's TLS 1.3 tickets under
	// TicketKey for later connections. Nil: no resumption.
	Tickets   *Tickets
	TicketKey string
	// Ticket is the session this connection resumes; the hello must then
	// carry pre_shared_key (fingerprint.Plan.ResumedHello).
	Ticket *Ticket
	// VerifyConnection, when set, runs after certificate verification on
	// every handshake, resumed ones included, before any received ticket is
	// kept; an error fails the handshake.
	VerifyConnection func(tls.ConnectionState) error
	// KeyLogWriter, when set, receives the connection's TLS secrets in
	// NSS key log format, TCP and QUIC alike (Config.KeyLogWriter).
	KeyLogWriter io.Writer
	// ClientCertificate, when set, answers a server's CertificateRequest
	// (Config.GetClientCertificate, bound to the host); a nil certificate,
	// or one with no chain, sends none. ClientAuth is the profile's: how
	// the certificate is signed for, whether its session is kept, and
	// whether the first request from a host drops its connection.
	ClientCertificate func(*tls.CertificateRequestInfo) (*tls.Certificate, error)
	ClientAuth        *profile.ClientAuth
	// FirstRequest reports whether this CertificateRequest is the first
	// from the connection's host and port the client has met, and marks
	// it met. With ClientCertificate set and tls.clientAuth.restart, Client
	// then closes the connection with nothing sent and fails with
	// ErrRestart, and ClientCertificate is not asked.
	FirstRequest func() bool
	// ECH, when set, is the config the hello's inner hello is encrypted to
	// (fingerprint.Plan.ECHHello). A server that rejects it, proving
	// itself the config's public name, fails the handshake with
	// *ECHRejectedError.
	ECH *ECHConfig
}

Config is what a handshake needs besides the ClientHello.

type ECHConfig added in v0.5.0

type ECHConfig struct {
	// List is the config as an ECHConfigList of its own.
	List []byte
	// PublicName is the config's public name: the outer hello's
	// server_name, and the name a rejecting server's certificate must
	// be valid for.
	PublicName string
}

ECHConfig is the ECH config a connection encrypts its inner hello to.

func SelectECH added in v0.5.0

func SelectECH(list []byte, r *profile.RealECH) (*ECHConfig, error)

SelectECH picks the ECH config of list, the ech SvcParam of a server's HTTPS record, that the browser of r encrypts its inner hello to (profile tls.realEch): the first, in the list's order, with a measured KEM, among those the handshake can use, which it picks as uTLS's pickECHConfig does: the version RFC 9849 defines, a public name that is a DNS name, no mandatory extension, a public key its KEM reads, and a suite whose KDF and AEAD it has (RFC 9849, section 6.1). It returns nil for a list with none the browser uses, which is then sent the ClientHello as to any server. A list that does not parse is ErrMalformedECH. A config the browser was never seen to use or pass over, ahead of the one picked, and a picked config whose first usable HPKE suite (the one the handshake takes) is not among r.CipherSuites, are ErrNotMeasured: what the browser would send is unknown.

type ECHRejectedError added in v0.5.0

type ECHRejectedError struct {
	RetryConfigs []byte
}

ECHRejectedError is the error of a handshake whose server rejected ECH, having proved itself the config's public name: the request goes again on a new connection, with RetryConfigs (a list SelectECH reads) or, without, none.

func (*ECHRejectedError) Error added in v0.5.0

func (e *ECHRejectedError) Error() string

type Saved added in v0.5.0

type Saved struct {
	Key        string
	Expires    time.Time
	Ticket     []byte
	State      []byte
	Identified bool
}

Saved is a stored ticket as Export gives it and Import takes it: its key, when it stops being usable (zero: unknown), the session as uTLS encodes it (ClientSessionState.ResumptionState: the ticket, and the SessionState's Bytes, the secrets included), and whether a client certificate was sent in it.

type Store added in v0.5.0

type Store struct {
	// contains filtered or unexported fields
}

Store keeps session tickets by key (host, port, partition, proxy; QUIC apart) for the Tickets views given it: one engine's, or every engine sharing a store (Config.Tickets). It belongs to one browser once Bind or Import names it. Safe for concurrent use.

func NewStore added in v0.5.0

func NewStore() *Store

NewStore returns an empty store.

func (*Store) Bind added in v0.5.0

func (s *Store) Bind(browser string) error

Bind makes s browser's, unless it already belongs to another: a ticket links the connections that use it, so two browsers sharing a store would show as one identity.

func (*Store) Browser added in v0.5.0

func (s *Store) Browser() string

Browser is the browser s belongs to, or "".

func (*Store) Clear added in v0.5.0

func (s *Store) Clear()

Clear forgets every ticket. The store still belongs to its browser.

func (*Store) Export added in v0.5.0

func (s *Store) Export(now time.Time) ([]Saved, error)

Export returns the tickets usable at now.

func (*Store) Import added in v0.5.0

func (s *Store) Import(saved []Saved, browser string, now time.Time) error

Import adds the tickets of saved, which belong to browser (empty: unknown), still usable at now, replacing any under the same key. A session that does not parse or is neither TLS 1.2 nor 1.3, or a store belonging to another browser, fails it before anything is added.

func (*Store) Tickets added in v0.5.0

func (s *Store) Tickets(tls12 string, clock clockwork.Clock) *Tickets

Tickets returns a view of s using TLS 1.2 tickets as tls12, with clock telling expired tickets.

type Ticket

type Ticket struct {
	// contains filtered or unexported fields
}

Ticket is a session a connection resumes, taken from a Tickets.

func (*Ticket) EarlyData added in v0.3.0

func (t *Ticket) EarlyData() bool

EarlyData reports whether the ticket allows 0-RTT: its max_early_data_size is above 0. A browser offers early_data only then (fingerprint.Plan.ResumedQUICHello).

func (*Ticket) Extra

func (t *Ticket) Extra() [][]byte

Extra is the session's SessionState.Extra: what the connection that received the ticket stored with it (the QUIC version and transport parameters, quic.TicketVersion).

func (*Ticket) TLS13

func (t *Ticket) TLS13() bool

TLS13 reports a TLS 1.3 ticket, which the ClientHello offers in pre_shared_key (fingerprint.Plan.ResumedHello); a TLS 1.2 one goes in session_ticket of the full ClientHello.

type Tickets

type Tickets struct {
	// contains filtered or unexported fields
}

Tickets is a view of a Store that uses its tickets as the browser does: a TLS 1.3 ticket once, TLS 1.2 tickets by the profile's tls12 policy. A ticket uTLS would decline (expired lifetime or certificate) is never handed out, so a hello never looks resumed without resuming. Safe for concurrent use. See decision 0013.

func NewTickets

func NewTickets(tls12 string, clock clockwork.Clock) *Tickets

NewTickets returns a view of a new store using TLS 1.2 tickets as tls12 (profile.TLS12Reuse, profile.TLS12SingleUse, or "" for not at all), with clock telling expired tickets.

func (*Tickets) Drop added in v0.2.0

func (t *Tickets) Drop(key string)

Drop forgets key's ticket, if any.

func (*Tickets) Has

func (t *Tickets) Has(key string) bool

Has reports whether Take would return a ticket for key, without taking it or dropping an expired one.

func (*Tickets) Take

func (t *Tickets) Take(key string) *Ticket

Take returns the ticket a new connection for key offers, or nil. TLS 1.3 tickets, and TLS 1.2 ones under single use, leave the store: concurrent connections then resume one at a time. Expired tickets are dropped. A view keeping no TLS 1.2 tickets (QUIC's) never hands one out.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL