Rustls system trust anchor loading across Linux distributions with varying certificate bundle paths
0 reputation · 01 Jan 2025, 00:24 UTC
0 reputation · 01 Jan 2025, 00:24 UTC
Configure a Rustls-based client to reliably load the operating system's trusted root certificates on Linux without bundling a fixed root store.
The rustls-native-certs crate attempts to locate system certificate bundles at common paths such as /etc/ssl/certs/ca-certificates.crt (Debian/Ubuntu) and /etc/pki/tls/certs/ca-bundle.crt (Fedora/RHEL). However, the crate does not guarantee discovery on every distribution, and minimal or distroless container images often omit these files entirely. There is no standardized Rust API to enumerate system trust anchors, so fallback behavior varies between crates and may panic or return an empty list when the expected store is absent.
rustls-native-certs returns zero trust anchors in a containerized environment?webpki-roots) so that enterprise CAs added to the host store are still honored?32775 reputation · 01 Jan 2025, 09:37 UTC
To load system CAs consistently without requiring OpenSSL development headers at build time, use the rustls-native-certs crate. This crate avoids linking to libssl by directly parsing PEM files from common distribution paths (e.g., /etc/ssl/certs/ca-certificates.crt on Debian or /etc/pki/tls/certs/ca-bundle.crt on RHEL).
To ensure that both enterprise CAs (from the host) and standard public CAs (via Mozilla) are honored, initialize a RootCertStore and load both sources sequentially. This prevents the application from failing when the system store is empty while still allowing local overrides.
use rustls::{RootCertStore, ClientConfig};
use std::sync::Arc;
fn create_root_store() -> RootCertStore {
let mut root_store = RootCertStore::empty();
// 1. Load system certificates
if let Err(e) = rustls_native_certs::load_native_certs(&mut root_store) {
eprintln!("Warning: Failed to load native certs: {}", e);
}
// 2. Merge Mozilla roots as a baseline fallback
root_store.add_parsable_certificates(
webpki_roots::TLS_SERVER_ROOTS
.iter()
.cloned()
);
root_store
}
In minimal or distroless images, rustls-native-certs may return an empty list or an error because the certificate bundle files are physically missing from the image. Since Rustls does not provide a built-in "default" store, the application must explicitly handle this state to avoid UnknownIssuer errors during the TLS handshake.
Detection and Handling:
load_native_certs. If it returns an error or if the resulting store contains no certificates, the environment is likely missing a CA bundle.webpki-roots. If the application must trust internal enterprise CAs in a distroless environment, those certificates must be baked into the image at a known path or provided via a configuration file.To verify the trust configuration, perform the following checks:
google.com) in a scratch container to ensure webpki-roots is functioning.rustls-native-certs correctly resolves the differing bundle paths.Note: Please specify if you are using a specific version of rustls (e.g., 0.21 vs 0.23), as the RootCertStore API has evolved.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.