A TLS server and client in Rust with rustls and tokio-rustls

rustls is a TLS library written in Rust. It’s the usual choice when you want TLS in a Rust program without linking OpenSSL: reqwest, hyper-rustls, axum-server and many others build on it. This post builds a small TLS echo server and a client that talks to it, using tokio-rustls for async I/O.
The code targets rustls 0.23.46 and tokio-rustls 0.26.6. The rustls API changed a lot between 0.19 (the version the original version of this post used) and 0.23: ServerConfig::new, NoClientAuth, internal::pemfile and RootCertStore::add_pem_file are all gone.
What rustls is and isn’t
Section titled “What rustls is and isn’t”- It supports TLS 1.2 and TLS 1.3 only. SSLv3, TLS 1.0 and TLS 1.1 aren’t implemented.
- The protocol logic is Rust, but the cryptography comes from a pluggable crypto provider. The default provider is aws-lc-rs; ring is available through the
ringfeature. Both contain C and assembly, so “pure Rust” only describes the TLS layer. - Certificate verification is done by rustls-webpki.
Dependencies
Section titled “Dependencies”[dependencies]rustls = "0.23.46"tokio-rustls = "0.26.6"tokio = { version = "1", features = ["full"] }tokio-rustls re-exports rustls as tokio_rustls::rustls, so you can also skip the direct rustls dependency. If you do keep both, make sure they resolve to the same rustls 0.23 release, otherwise the config types won’t match.
PEM parsing is built in through rustls::pki_types::pem::PemObject. The separate rustls-pemfile crate that older examples use is no longer needed.
Getting a certificate for local testing
Section titled “Getting a certificate for local testing”webpki won’t accept a self-signed leaf certificate as its own trust anchor, so you need a CA plus a certificate signed by it. mkcert does this in one step:
mkcert localhost# writes localhost.pem and localhost-key.pemcp "$(mkcert -CAROOT)/rootCA.pem" ca.pemThe server
Section titled “The server”use std::sync::Arc;
use rustls::pki_types::pem::PemObject;use rustls::pki_types::{CertificateDer, PrivateKeyDer};use tokio::io::{AsyncReadExt, AsyncWriteExt};use tokio::net::TcpListener;use tokio_rustls::TlsAcceptor;
#[tokio::main]async fn main() -> Result<(), Box<dyn std::error::Error>> { let certs = CertificateDer::pem_file_iter("localhost.pem")? .collect::<Result<Vec<_>, _>>()?; let key = PrivateKeyDer::from_pem_file("localhost-key.pem")?;
let config = rustls::ServerConfig::builder() .with_no_client_auth() .with_single_cert(certs, key)?; let acceptor = TlsAcceptor::from(Arc::new(config));
let listener = TcpListener::bind("127.0.0.1:8443").await?; println!("Listening on 127.0.0.1:8443");
loop { let (stream, peer) = listener.accept().await?; let acceptor = acceptor.clone();
tokio::spawn(async move { let mut tls = match acceptor.accept(stream).await { Ok(tls) => tls, Err(err) => { eprintln!("{peer}: handshake failed: {err}"); return; } };
let mut buf = [0u8; 1024]; match tls.read(&mut buf).await { Ok(n) if n > 0 => { let _ = tls.write_all(&buf[..n]).await; let _ = tls.shutdown().await; // sends TLS close_notify } Ok(_) => {} Err(err) => eprintln!("{peer}: read failed: {err}"), } }); }}ServerConfig::builder() uses the default crypto provider and the default protocol versions (TLS 1.2 and 1.3). with_single_cert takes the certificate chain (leaf first) and the private key. PrivateKeyDer::from_pem_file accepts PKCS#8, PKCS#1 (RSA) and SEC1 (EC) keys.
A failed handshake is logged and only ends that connection’s task. The original version of this post called .unwrap() there, which would panic the task on any bad client.
The client
Section titled “The client”use std::sync::Arc;
use rustls::pki_types::pem::PemObject;use rustls::pki_types::{CertificateDer, ServerName};use tokio::io::{AsyncReadExt, AsyncWriteExt};use tokio::net::TcpStream;use tokio_rustls::TlsConnector;
#[tokio::main]async fn main() -> Result<(), Box<dyn std::error::Error>> { let mut roots = rustls::RootCertStore::empty(); for cert in CertificateDer::pem_file_iter("ca.pem")? { roots.add(cert?)?; }
let config = rustls::ClientConfig::builder() .with_root_certificates(roots) .with_no_client_auth(); let connector = TlsConnector::from(Arc::new(config));
let stream = TcpStream::connect("127.0.0.1:8443").await?; // The name is checked against the server certificate's subject alt names let domain = ServerName::try_from("localhost")?.to_owned(); let mut tls = connector.connect(domain, stream).await?;
tls.write_all(b"hello over TLS").await?;
let mut buf = Vec::new(); tls.read_to_end(&mut buf).await?; println!("Server replied: {}", String::from_utf8_lossy(&buf)); Ok(())}The client trusts only ca.pem. To talk to public servers instead, fill the root store from the webpki-roots crate (roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned())) or use the OS certificate store through rustls-platform-verifier.
Note that ServerName must match a name in the certificate. Connecting to 127.0.0.1 but passing "localhost" works here because mkcert put localhost in the certificate.
Gotchas
Section titled “Gotchas”- Restricting protocol versions: to allow TLS 1.3 only, use
ClientConfig::builder_with_protocol_versions(&[&rustls::version::TLS13])(and the matchingServerConfigmethod) instead ofbuilder(). - Crypto provider conflicts:
builder()picks the provider from the enabled crate features. If a dependency enablesringwhile rustls also hasaws-lc-rsenabled, rustls can’t choose one and panics. Fix it by callingrustls::crypto::aws_lc_rs::default_provider().install_default()(or theringequivalent) at startup, or usebuilder_with_provider. - Closing connections: call
shutdown()on the TLS stream so the peer receivesclose_notify. Otherwise the peer sees an unexpected EOF, which rustls reports as an error.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)