Documentation
¶
Overview ¶
Package utls runs TLS as a protocol layer over a UIO transport, with crypto/tls doing the cryptography.
Server and Client return a uio.Layer: name one in a Listen's or a Dial's uio.WithLayers and it hands everything above it plaintext, so whatever runs there — a protocol handler, another layer, another TLS layer — speaks TLS without knowing it:
listener, err := events.Listen(addr, uio.WithLayers(utls.Server(config))) conn, err := events.Dial(addr, uio.WithLayers(utls.Client(config)))
Nothing costs a goroutine. The handshake runs as a coroutine that OnRead resumes with the bytes that arrive: when crypto/tls asks its transport for more than the round carried, the transport hands control back and the next round goes on from there, so the rounds the executor already serializes carry the handshake between them. The executor's goroutines passing the rounds on to each other must not be locked to OS threads. Records after the handshake are decrypted in OnRead, straight from the bytes the round read; a record that arrives across several rounds stays cut in crypto/tls's buffer until it is whole.
A record carries up to 16 KiB, so a transport whose reads are smaller carries every record through several rounds: set Events.MaxBufferSize to at least a record. 16 KiB reads — the cadence crypto/tls runs at — move a bulk transfer about 1.4× the 4 KiB default's, ahead of crypto/tls on the same wire; 64 KiB reads measured 1.4–1.8× the default and fastest on both hosts we ran.
The handshake is bounded by the layer's own timer, whose expiry closes the connection. A connection opens to the handler above once its handshake settles; a connection whose handshake fails or times out — accepted or dialed — ends without a handler callback (the Handler never opened, so it hears neither OnOpen nor OnClose), and a dial reports its cause from DialContext. Close sends the peer close_notify once what was accepted has left, and CloseWrite sends it one way, then shuts the connection below's writing half — a stack below that cannot end one direction reports ErrUnsupported after close_notify went. A peer's close_notify delivers what arrived with it and then the close. The connection below's deadlines and fd-shaped options reach through to it.
One crypto/tls behavior this relies on is not a written contract: that a temporary net.Error from the transport Read makes it keep the partial record it buffered and retry. The tests pin it, so a Go release that changes it fails here rather than in the field.
Session tickets are the Config's, as they are for crypto/tls: servers that share one Config resume one another's sessions, and servers that each build their own do not — the client falls back to a full handshake, which is slower, not an error. Share one Config across every Events a process serves through, and install the same ticket keys with [tls.Config.SetSessionTicketKeys] on every process that shares an address.
Size the transport's reads to the record: a TLS record carries up to 16 KiB, and a socket read smaller than that splits one record across several rounds, each costing a syscall. On UIO that is uio.Events.MaxBufferSize, whose default of 4 KiB turns one 16 KiB record into four reads; a transfer reads about twice as fast with 16 KiB and about half again as fast with 64 KiB.
Index ¶
Constants ¶
const DefaultHandshakeTimeout = 10 * time.Second
DefaultHandshakeTimeout bounds a handshake when the layer is installed without the HandshakeTimeout option.
const DefaultStagingBound = 64 << 10
DefaultStagingBound is the decrypted-byte bound the layer is installed with when it is installed without the StagingBound option.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
Config is the TLS configuration the layer runs with: certificates on a server, ServerName or InsecureSkipVerify on a client, ALPN either way. It is crypto/tls's own configuration, so anything crypto/tls accepts here works, and a Config built for crypto/tls needs no conversion.
type ConnectionState ¶
type ConnectionState = stdtls.ConnectionState
ConnectionState is what a settled connection's State reports.
type Option ¶
type Option func(*tlsLayer)
Option configures the layer Server or Client builds.
func HandshakeTimeout ¶
HandshakeTimeout bounds the handshake. Zero means DefaultHandshakeTimeout, a negative value means no bound.
func StagingBound ¶
StagingBound bounds the decrypted bytes the layer holds for the handler above: at its bound the layer stops taking new chunks from the connection below, so a pause at the top stills the wire. It paces the rounds rather than capping one delivery: a chunk in hand is decrypted whole — stopping mid-chunk would carry its ciphertext through a copy into the next round — and crypto/tls hands a record over whole however its bytes arrived, one it had been assembling from earlier rounds included, so the delivery may pass the bound by that record (up to 16 KiB) and the turn's chunk (one socket read). The framework pauses the layer at its own delivery bound (DefaultDeliverBound, or MaxInboundBuffered) as well, so a larger StagingBound is capped there. During the handshake it bounds the ciphertext the layer takes from below that the handshake has not read yet, though never below one TLS record. Zero means DefaultStagingBound.