TLS
Serving HTTPS from a PEM certificate chain and key, or from a rustls configuration of your own.
TLS is a property of a server crate. ulo-net loads the certificate into a rustls ServerConfig when the app listens, and the crate that accepts connections wraps it in its runtime's TLS stack: tokio-rustls on tokio, futures-rustls on smol. An embedded app has no tls method, because the host terminates TLS.
PEM files
examples/20-http-tls:
use ulo_http_hyper::Server;
use ulo_net::Tls;
pub fn server(endpoint: &str, cert: &str, key: &str) -> Server {
Server::new(endpoint).tls(Tls::from_pem_files(cert, key))
}Every endpoint of the server then speaks TLS, offering h2 and http/1.1. The certificate file holds the chain with the leaf first. Tls::from_pem(cert_bytes, key_bytes) takes PEM already in memory.
Nothing is read where a Tls is built. The files are read and parsed during listen, with every other configuration check, so an unreadable file, a malformed certificate or a key that does not match it fails startup as StartupError::Configure before any port is bound. A PEM error from a file names the file.
To try it:
openssl req -x509 -newkey rsa:2048 -nodes -days 1 -subj /CN=localhost \
-addext subjectAltName=DNS:localhost,IP:127.0.0.1 -keyout key.pem -out cert.pem
TLS_CERT=cert.pem TLS_KEY=key.pem cargo run -p ex-20-http-tls
curl --cacert cert.pem https://localhost:3000/Client certificates and other rustls settings
PEM cannot ask a client for a certificate. Build a rustls ServerConfig yourself, for instance with with_client_cert_verifier, a certificate resolver that picks by SNI, or session tickets, and hand it over with Tls::from_config(Arc<ServerConfig>).
The server still decides the ALPN protocols its transport needs. For HTTP:
- A configuration naming no protocols, or
acme-tls/1alone, is givenh2andhttp/1.1(followed byacme-tls/1when it named it). - A configuration naming both is accepted, and the server offers exactly those two in its order. Any other protocol,
h3included, is dropped. - A configuration naming one of the two without the other fails
listenasConfigure(TlsError::Alpn).
Handshake time
Server::handshake_timeout(Bound) bounds the TLS handshake: 30 seconds unset. A failed or timed-out handshake is logged at debug and drops that connection. The accept loop is unaffected.
What a handler sees
The ConnInfo of a request on a server crate carries the ALPN protocol and the SNI name of the handshake in tls. An embedding reports none, since the host terminated TLS.