FAQ - Help and questions

How do I configure a rustls client so it verifies server certificates?

#security#tls#rustlspublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Build a ClientConfig with a crypto provider, the safe default protocol versions and a store of trusted roots such as webpki-roots. Certificate and server-name checks are then on by default; you give the name when starting each connection. Never replace the verifier with one that accepts everything.

Checked with rustc 1.93.0, rustls 0.23.46 (ring provider), rustls-pki-types 1.16.0, webpki-roots 1.0.12, Windows 11

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

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.

Cargo.tomltoml
[dependencies]
rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] }
rustls-pki-types = "1"
webpki-roots = "1"
src/main.rsrust
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());
}
text
handshake pending: true

This 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#

  1. Pick a provider (ring here; aws-lc-rs is the other one rustls supports) and enable its feature.
  2. Use with_safe_default_protocol_versions unless you have a reason to restrict versions further.
  3. Choose the root store: bundled (webpki-roots) or the system's (rustls-native-certs).
  4. Use a higher-level HTTP client built on rustls if you do not need raw connections; most are configured through the same types.
  5. 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.

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#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet