TLS

TLS wraps TCP connections and listeners with native TLS 1.2/1.3 state while using OpenSSL only for low-level cryptographic primitives. Deadlines and socket lifecycle stay aligned with the underlying transport. Concrete-address dialing and listening use the same connect and listen entrypoints as the string-address surface. String-address client dialing uses the resolver/policy layer as Reseau.TCP.connect; see Name Resolution for that part of the stack.

Configuration, Types, and Errors

The main TLS building blocks are:

Reseau.TLS.ClientAuthMode — Module
ClientAuthMode

Server-side client certificate policy for native TLS handshakes.

The values intentionally mirror Go's client-auth policy surface: callers pick a policy up front in Config, then the TLS 1.2/1.3 server handshakes interpret that policy consistently when deciding whether to request, require, and verify client certificates.

source
Reseau.TLS.Config — Type
Config(; ...)
Config(cfg::Config; ...)

Reusable TLS configuration for client and server sessions.

Keyword arguments:

  • server_name: hostname or IP literal used for certificate verification. For clients, this also drives SNI when it is a hostname. If omitted, connect will try to derive it from the dial target.
  • verify_peer: whether to validate the remote certificate chain against trusted roots.
  • verify_hostname: whether to validate the remote peer name against the presented leaf certificate. This defaults to verify_peer, but can be enabled separately.
  • client_auth: server-side client certificate policy.
  • cert_file / key_file: PEM-encoded certificate chain and private key. Servers must provide both; clients may provide both for mutual TLS.
  • key_passphrase: passphrase for an encrypted key_file. It cannot contain null characters, and an unencrypted key ignores it. The key is loaded at the first handshake that needs it, not by Config or listen, so a server with a wrong or missing passphrase still listens and each handshake throws TLSError. A client loads its key only when the server asks for a certificate.
  • ca_file: CA bundle or CA directory used to verify remote servers.
  • ca_dir: additional CA directory used together with ca_file. Each explicit source must contain usable PEM certificates. If both are omitted, client verification uses NetworkOptions.ca_roots_path(). An explicit source replaces that default.
  • client_ca_file / client_ca_dir: CA bundle or directory, and optional additional directory, used by servers to verify client certificates. At least one is required for server configs that verify presented client certs. Directory entries are read as PEM files; hashed filenames are accepted but are not required.
  • alpn_protocols: ordered ALPN protocol preference list.
  • curve_preferences: ordered native ECDHE group preferences for TLS 1.2/1.3. When empty, native handshakes default to [TLS.X25519, TLS.P256, TLS.P384, TLS.P521].
  • handshake_timeout_ns: optional cap, in monotonic nanoseconds, applied only while the handshake is running. Existing transport deadlines still win if they are earlier.
  • min_version / max_version: TLS protocol version bounds. Only TLS 1.2 and TLS 1.3 are supported; nothing leaves the bound unset.

Copy an existing config with Config(cfg::Config; kwargs...), passing only the settings above that should change. The copy shares cfg's session caches, session-ticket keys, and loaded local identity, so a per-connection copy that only sets server_name or handshake_timeout_ns keeps resuming sessions; changing cert_file, key_file, or key_passphrase gives the copy its own identity state. Use this instead of rebuilding a Config field by field: the private fields and their order are not part of the API. The copy keywords take exactly the field types (String rather than any AbstractString), which keeps a copy built from another config's fields resolvable under --trim.

Returns a reusable immutable Config.

Throws ConfigError if the keyword combination is internally inconsistent.

source
Reseau.TLS.ConnectionState — Type
ConnectionState

Snapshot of negotiated TLS connection state.

Fields:

  • handshake_complete: whether the TLS handshake has finished successfully.
  • version: negotiated TLS protocol version string.
  • alpn_protocol: negotiated ALPN protocol, or nothing if ALPN was not used.
  • cipher_suite: negotiated cipher suite name, or nothing if it is not available.
  • using_native_tls13: whether the connection is using the native TLS 1.3 path.
  • did_resume: whether the connection resumed a previously cached TLS session.
  • did_hello_retry_request: whether the handshake observed a TLS 1.3 HelloRetryRequest.
  • has_resumable_session: whether this connection currently has a cached resumable native TLS session.
  • curve: negotiated native TLS key share group name, or nothing if it is not available.
source
Reseau.TLS.Conn — Type
Conn

TLS stream wrapper over one TCP.Conn.

Conn is safe for one concurrent reader and one concurrent writer. Handshake, read, and write all have separate locks so lazy handshakes and shutdown can coordinate cleanly across the native TLS state machine. Because Conn <: IO, standard Base stream helpers like read, read!, readbytes!, eof, and write apply directly to decrypted application data.

source
Reseau.TLS.Listener — Type
Listener

TLS listener wrapper over TCP.Listener.

Accepted connections are wrapped in server-side TLS state but are not handshaken until handshake! or the first read/write call.

source
Reseau.TLS.ConfigError — Type
ConfigError

Raised when TLS configuration is invalid.

This is thrown before any network I/O starts, typically while normalizing a Config or preparing a TLS connection/listener.

source
Reseau.TLS.TLSError — Type
TLSError

Raised when TLS handshake/read/write/close operations fail.

code is 0 for pure Julia-side failures and may be nonzero when the OpenSSL-backed primitive layer reports a backend error. cause preserves the underlying Julia-side exception when the failure originated in the transport/poller layer or a higher-level TLS check.

source
Reseau.TLS.TLSHandshakeTimeoutError — Type
TLSHandshakeTimeoutError

Raised when handshake exceeds the configured timeout.

This is kept separate from TLSError because handshake timeouts are often configured and handled distinctly from generic TLS failures.

source
Reseau.TLS.DeadlineExceededError — Type
DeadlineExceededError

Alias for the transport deadline timeout type used by TLS.

Catch TLS.DeadlineExceededError for direct listener accept timeouts and when inspecting TLSError.cause. Higher-level TLS operations may wrap deadline expiry in TLSError or TLSHandshakeTimeoutError so callers can keep operation-specific handling.

source

ClientAuthMode is an enum-like policy surface with the following cases:

  • TLS.ClientAuthMode.NoClientCert
  • TLS.ClientAuthMode.RequestClientCert
  • TLS.ClientAuthMode.RequireAnyClientCert
  • TLS.ClientAuthMode.VerifyClientCertIfGiven
  • TLS.ClientAuthMode.RequireAndVerifyClientCert

For server-side verified client-certificate auth, provide client_ca_file, client_ca_dir, or both in Config.

To trust a bundle and a directory together, set both paths:

config = TLS.Config(ca_file="company-roots.pem", ca_dir="additional-roots")

ca_file still accepts either a bundle file or a directory. ca_dir must be a directory. Reseau reads PEM certificates from its files; hashed filenames are not required. Both sources contribute trusted roots. An invalid source is not ignored because the other source is valid. Missing paths and non-directory ca_dir values are rejected before dialing. Certificate contents are loaded when verification runs. The same rules apply to client_ca_file and client_ca_dir for client certificates.

Providing either explicit source replaces the default roots. To add a directory to the system bundle, pass the bundle path as ca_file too. Config copies retain both paths. Resumed sessions are checked against the current configured roots.

Certificate verification also rejects the eight eMudhra CA public keys compromised in October 2026, including their cross-signed certificates. This built-in distrust applies to leaves, intermediates, and trust anchors, including explicit CA sources and resumed sessions. It is a local key denylist, not general CRL or OCSP checking; keep Reseau and the configured CA bundle updated.

Client and Server Construction

You can either dial and handshake a client in one step from a concrete TCP.SocketAddr or string address, or wrap an existing TCP.Conn manually:

Reseau.TLS.client — Function
client(tcp, config) -> Conn

Wrap an established TCP.Conn in client-side TLS state.

The handshake is deferred until handshake! or the first read/write operation.

Throws ConfigError or TLSError if the TLS state cannot be initialized.

source
Reseau.TLS.server — Function
server(tcp, config) -> Conn

Wrap an established TCP.Conn in server-side TLS state.

The handshake is deferred until handshake! or the first read/write operation.

Throws ConfigError or TLSError if the TLS state cannot be initialized.

source
Reseau.TLS.connect — Function
connect(remote_addr; kwargs...) -> Conn
connect(remote_addr, local_addr; kwargs...) -> Conn

Connect to a concrete TCP.SocketAddr, negotiate TLS, and return a fully handshaken client connection.

This is the direct-address counterpart to the string-address connect(network, address; ...) overloads. The underlying socket setup is delegated to TCP.connect, after which the returned transport is wrapped in TLS and handshaken immediately.

TLS keyword arguments are forwarded to Config. If server_name is omitted, it is inferred from the concrete remote address when possible so peer verification and SNI can follow the same rules as the string-address client path.

source
connect(network, address; kwargs...) -> Conn

Connect to address, negotiate TLS, and return a fully handshaken client connection.

Network-resolution keyword arguments are:

  • timeout_ns
  • deadline_ns
  • local_addr
  • fallback_delay_ns
  • resolver
  • policy

TLS keyword arguments are forwarded to Config:

  • server_name
  • verify_peer
  • client_auth
  • cert_file
  • key_file
  • ca_file / ca_dir
  • client_ca_file / client_ca_dir
  • alpn_protocols
  • handshake_timeout_ns
  • min_version
  • max_version

If server_name is omitted, it is derived from address when possible so SNI and peer verification use the dial target's host name automatically.

source
connect(address; kwargs...) -> Conn

Convenience shorthand for connect("tcp", address; kwargs...).

source
Reseau.TLS.listen — Function
listen(network, address, config; backlog=128, reuseaddr=true) -> Listener

Create a TLS listener by first creating a TCP listener and then associating a server Config with accepted connections.

Accepted Conns are returned in lazy-handshake form.

Throws ConfigError if the server config is invalid and propagates any listener creation errors from TCP.listen.

source
listen(local_addr, config; backlog=128, reuseaddr=true) -> Listener

Create a TLS listener from a concrete local TCP.SocketAddr.

This is the direct-address counterpart to listen(network, address, config; ...). The underlying TCP listener is created with TCP.listen, then accepted connections are wrapped in lazy-handshake TLS state.

source
Reseau.TLS.accept — Function
accept(listener) -> Conn

Accept one inbound TCP connection and wrap it in server-side TLS state.

The returned Conn has not handshaken yet. If the listener deadline expires, accept(listener) throws DeadlineExceededError.

source
Reseau.TLS.handshake! — Function
handshake!(conn)

Run the TLS handshake to completion if it has not already finished.

This method is idempotent. Concurrent callers serialize through handshake_lock, so only one task drives the native TLS state machine while the others observe the completed state afterward.

Returns nothing.

Throws:

  • TLSHandshakeTimeoutError when config.handshake_timeout_ns expires
  • TLSError for TLS or transport failures
  • EOFError if the peer closes cleanly during the handshake
  • InterruptException if the waiting task is interrupted; later calls throw a TLSError
source

Important behaviors to keep in mind:

  • connect returns a fully handshaken client connection for both concrete-address and string-address dialing.
  • listen returns a TLS listener whose accepted connections are lazy-handshake wrappers.
  • client and server are the direct wrapping APIs when you already control the underlying TCP.Conn.
  • handshake! is idempotent, so eager and lazy handshake styles can coexist safely.

I/O, Lifecycle, Deadlines, and Inspection

TLS connections still behave like Julia streams, but read and write operate on plaintext application data while Reseau handles TLS record framing underneath:

Base.read! — Method
read!(conn, buf) -> buf

Read exactly length(buf) decrypted bytes into buf or throw EOFError.

Because Conn <: IO, Base's generic read! implementation already supports mutable byte views like @view bytes[2:5] in addition to plain vectors.

Use readbytes! or readavailable when you want a count-returning read that may stop early.

source
Base.readbytes! — Method
readbytes!(conn, buf, nb=length(buf); all::Bool=true) -> Int

Read up to nb decrypted bytes into buf, returning the byte count.

If all is true (the default), the call keeps reading until nb bytes have been transferred, EOF is reached, or an error occurs. If all is false, at most one underlying TLS read is performed.

Resizable Vector{UInt8} buffers grow when needed, matching Julia's standard readbytes! behavior. Fixed-size contiguous byte views must satisfy nb <= length(buf).

source
Base.readavailable — Method
readavailable(conn) -> Vector{UInt8}

Read and return decrypted plaintext that is ready without requiring a full-buffer exact read.

source
Base.eof — Method
eof(conn) -> Bool

Wait until decrypted data is available or the stream has ended, and return true if it has ended: the peer sent close_notify, the transport reached EOF at a record boundary (as read treats it), or conn was closed locally, including while waiting. A record cut off by EOF throws TLSError.

source
Base.isopen — Method
isopen(conn) -> Bool

Return true while both the TLS state and underlying TCP transport remain open.

source
Base.isopen — Method
isopen(listener) -> Bool

Return true while the wrapped TCP listener remains open.

source
Base.write — Method
write(conn, buf) -> Int
write(conn, buf, nbytes) -> Int

Write plaintext application bytes through the TLS connection.

write(conn, buf) attempts to write the entire buffer. The fixed-size byte-buffer overload allows callers to cap the write at nbytes.

source
Base.close — Method
close(conn)

Close the TLS connection and the underlying TCP transport.

If the handshake completed and no write is in progress, this sends a TLS close_notify alert first, waiting at most 5 seconds for the transport to accept it. A task blocked reading conn does not prevent the alert; it wakes with a closed-connection error. The method is idempotent and returns nothing.

source
Base.close — Method
close(listener)

Close the underlying TCP listener. Repeated closes are treated as no-ops.

source
Base.closewrite — Method
closewrite(conn)

Send TLS shutdown on the write side and mark future writes as permanently failed.

This is the TLS analogue of half-closing a TCP socket. It requires the handshake to have completed because the TLS close-notify alert is itself a TLS record.

Returns nothing.

Throws TLSError if the TLS shutdown path fails.

source
Reseau.TLS.set_deadline! — Function
set_deadline!(conn, deadline_ns)

Set both read and write deadlines on the underlying transport.

deadline_ns is interpreted using the same monotonic-clock convention as TCP and IOPoll: 0 clears the deadline, negative values mean the deadline has already expired, and positive values are absolute time_ns() values.

source
set_deadline!(listener, deadline_ns)

Set the accept deadline on the underlying TCP listener.

This affects accept(listener) only. The returned Conn still uses its own connection and handshake deadlines afterward. Expired accept waits throw DeadlineExceededError.

source
Reseau.TLS.set_read_deadline! — Function
set_read_deadline!(conn, deadline_ns)

Set the read deadline on the underlying transport. See set_deadline! for the timestamp convention.

source
Reseau.TLS.set_write_deadline! — Function
set_write_deadline!(conn, deadline_ns)

Set the write deadline on the underlying transport. See set_deadline! for the timestamp convention.

source
Reseau.TLS.local_addr — Function
local_addr(conn)

Return the local TCP.SocketAddr for the wrapped transport.

source
local_addr(listener)

Return the local listening address for the wrapped TCP listener.

This is an alias for addr(listener).

source
Reseau.TLS.net_conn — Function
net_conn(conn) -> TCP.Conn

Expose the underlying TCP connection.

Callers that need transport-level inspection can reach through the TLS wrapper without reconstructing the socket state.

source
Reseau.TLS.connection_state — Function
connection_state(conn) -> ConnectionState

Return a snapshot of the currently negotiated TLS state.

This does not force a handshake; if the handshake has not yet run, the returned ConnectionState reports handshake_complete=false.

source
Reseau.TLS.addr — Function
addr(listener)

Return the local listening address for the wrapped TCP listener.

source

Details that matter in practice:

  • read!(conn, buf) fills buf or throws EOFError; use readbytes! or readavailable when you want short-read behavior.
  • TLS deadlines apply to the wrapped TCP transport and follow the TCP model, except that plaintext already decrypted is still returned after the read deadline passes.
  • A timed-out TLS write is treated as a permanent write failure because partial record emission leaves future writes unsafe.
  • close(conn) and close(listener) are idempotent, and isopen is available on both TLS connections and listeners.

TLS.DeadlineExceededError is the public alias for the underlying transport timeout type. accept(listener) throws it directly when the listener deadline expires. Higher-level TLS operations may instead raise TLSError with the timeout in cause, or a handshake-specific TLSHandshakeTimeoutError.

Practical Usage Notes

  • If server_name is omitted, connect derives it from the dial target when possible so SNI and certificate verification use that host name automatically.
  • If both ca_file and ca_dir are omitted for outbound verification, Reseau falls back to NetworkOptions.ca_roots_path() when that path is available.
  • connection_state does not force the handshake to run; it reports the current negotiated state as-is.
  • set_deadline! also applies to TLS.Listener, where it sets the accept deadline only. local_addr(listener) is available as an alias for addr.

Where To Go Next

  • Read TCP for the underlying transport model and socket lifecycle.
  • Read Name Resolution for the advanced resolver and address-family policy controls shared by TCP.connect and TLS.connect.
  • Read API Reference for the canonical TLS docstrings.