Answer#
rustls is a TLS library written in Rust. Its client configuration cannot be built without saying whom to trust: you pass a RootCertStore. Everything else, including checking the certificate chain, the validity dates and the server name, happens in the default verifier.
[dependencies]
rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] }
rustls-pki-types = "1"
webpki-roots = "1"use rustls::{crypto::ring, ClientConfig, ClientConnection, RootCertStore};
use rustls_pki_types::ServerName;
use std::sync::Arc;
fn main() {
// Trust the public Web PKI roots (Mozilla's list, packaged as the webpki-roots crate).
let roots = RootCertStore { roots: webpki_roots::TLS_SERVER_ROOTS.to_vec() };
// Safe default protocol versions (TLS 1.2 and 1.3) and certificate checking switched on.
let config = ClientConfig::builder_with_provider(Arc::new(ring::default_provider()))
.with_safe_default_protocol_versions()
.expect("the provider supports the default versions")
.with_root_certificates(roots)
.with_no_client_auth();
// Starting a connection also fixes the name the server's certificate must be valid for.
let name = ServerName::try_from("example.test").expect("a valid DNS name");
let conn = ClientConnection::new(Arc::new(config), name).expect("config accepted");
println!("handshake pending: {}", conn.is_handshaking());
}handshake pending: trueThis program builds the configuration and starts a connection object, but opens no network connection; the next thread shows a full handshake.
webpki-roots packages the Mozilla root list inside your program, so the trusted set is the same on every machine and updates with the crate. The alternative, rustls-native-certs, reads the operating system's store, which suits programs that must honour company-installed roots.
How to do it#
- Pick a provider (
ringhere;aws-lc-rsis the other one rustls supports) and enable its feature. - Use
with_safe_default_protocol_versionsunless you have a reason to restrict versions further. - Choose the root store: bundled (
webpki-roots) or the system's (rustls-native-certs). - Use a higher-level HTTP client built on rustls if you do not need raw connections; most are configured through the same types.
- Update the dependencies regularly: root lists and TLS libraries receive fixes (see the audit threads).
Common mistakes#
- Using the
dangerous()configuration API to disable verification "for testing". See the thread on self-signed certificates for the right way to test. - Passing an IP address or the wrong name as the server name; the certificate is checked against it.
- Forgetting that a trusted root is trusted for every site: do not add test roots to a production store.
Related questions#
rustls or native-tls?#
Both are used in production. rustls has no dependency on a system TLS library and behaves the same on every platform; native-tls uses the platform's library (Schannel, Secure Transport or OpenSSL) and its certificate handling. Choose by deployment needs.
How do I use this with a ready-made HTTP client?#
Most clients that use rustls expose a switch for it and use sane defaults. Check the client's documentation for how it chooses roots and whether you can pass your own.
Sources#
- rustls documentation: https://docs.rs/rustls/0.23.46/rustls/
ClientConfig::builder_with_provider: https://docs.rs/rustls/0.23.46/rustls/client/struct.ClientConfig.htmlwebpki-rootsdocumentation: https://docs.rs/webpki-roots/1.0.12/webpki_roots/