Skip to content
CryoCryo home
Stdlibnet::tls

context

import std::net::tls::context; · source

Each of the two context types owns an OpenSSL SSL_CTX and mints a TlsStream per connection from it. connect and accept are the async entry points: hand over a connected TcpStream and get back a TlsStream once the handshake completes. start_connect and start_accept hand back the TlsHandshake future instead, for callers composing it by hand — under a Futures::timeout, say.

Types

TlsConnector

type struct TlsConnector {
    ctx:    void*;
    verify: boolean;

    static new() -> Result<TlsConnector, IoError>;
    danger_accept_invalid_certs(mut this) -> TlsConnector;
    with_alpn_h2(mut this) -> TlsConnector;
    async connect(&this, stream: TcpStream, hostname: Str) -> Result<TlsStream, IoError>;
    start_connect(&this, stream: TcpStream, hostname: Str) -> Result<TlsHandshake, IoError>;
}

The client side: the TLS client method, with certificate verification against the system store on by default. danger_accept_invalid_certs() is named to be conspicuous at the call site, because it disables verification. with_alpn_h2() offers h2 then http/1.1 during the handshake, and negotiated_alpn() on the resulting stream reports which one the server picked — that is how a client decides between net::http and net::http2 on a single connection. Both are fluent and consume the connector.

Properties

PropertyType
ctxvoid*
verifyboolean

Constructors

static new() -> Result<TlsConnector, IoError>;

New client context with the system trust store loaded and peer verification on by default.

Methods

danger_accept_invalid_certs(mut this) -> TlsConnector;

Disable certificate + hostname verification. Named to be ugly on purpose: only for self-signed test servers or pinned-out-of-band peers. Fluent: returns the connector.

with_alpn_h2(mut this) -> TlsConnector;

Advertise HTTP/2 (and HTTP/1.1) via ALPN. Read the negotiated result back with TlsStream::negotiated_alpn. Fluent.

async connect(&this, stream: TcpStream, hostname: Str) -> Result<TlsStream, IoError>;

Wrap an established TcpStream in TLS, performing the client handshake with SNI set to hostname (also the name verified against the peer certificate when verification is on). Consumes the TcpStream; on failure it is closed before returning.

start_connect(&this, stream: TcpStream, hostname: Str) -> Result<TlsHandshake, IoError>;

Set the socket non-blocking and mint the SSL* with the SNI and verification wiring, handing back the future that drives the handshake.

TlsAcceptor

type struct TlsAcceptor {
    ctx: void*;

    static new(cert_path: Str, key_path: Str) -> Result<TlsAcceptor, IoError>;
    async accept(&this, stream: TcpStream) -> Result<TlsStream, IoError>;
    start_accept(&this, stream: TcpStream) -> Result<TlsHandshake, IoError>;
}

The server side: the TLS server method loaded with the PEM certificate chain and private key at cert_path and key_path. accept runs the server half of the handshake on an accepted TcpStream.

Because SSL_new takes a reference on the context, a stream outlives its connector or acceptor regardless of which drops first. Both close their context through an inherent drop.

Properties

PropertyType
ctxvoid*

Constructors

static new(cert_path: Str, key_path: Str) -> Result<TlsAcceptor, IoError>;

New server context from a PEM certificate chain and private key (paths on disk). Fails InvalidInput if either file is missing or the key doesn't match the certificate.

Methods

async accept(&this, stream: TcpStream) -> Result<TlsStream, IoError>;

Complete the server handshake over an accepted TcpStream. Consumes it; closes it on failure.

start_accept(&this, stream: TcpStream) -> Result<TlsHandshake, IoError>;

Mint the SSL* and hand back the handshake future, the server-side mirror of start_connect. A socket handed over by TcpAccept is already non-blocking, but it is set here rather than assumed.

Constants

const ALPN_H2_HTTP11_LEN: u32 = 11;