rustls_native_certs/lib.rs
1//! rustls-native-certs allows rustls to use the platform's native certificate
2//! store when operating as a TLS client.
3//!
4//! It provides the following functions:
5//! * A higher level function [load_native_certs](fn.build_native_certs.html)
6//! which returns a `rustls::RootCertStore` pre-filled from the native
7//! certificate store. It is only available if the `rustls` feature is
8//! enabled.
9//! * A lower level function [build_native_certs](fn.build_native_certs.html)
10//! that lets callers pass their own certificate parsing logic. It is
11//! available to all users.
12
13#[cfg(all(unix, not(target_os = "macos")))]
14mod unix;
15#[cfg(all(unix, not(target_os = "macos")))]
16use unix as platform;
17
18#[cfg(windows)]
19mod windows;
20#[cfg(windows)]
21use windows as platform;
22
23#[cfg(target_os = "macos")]
24mod macos;
25#[cfg(target_os = "macos")]
26use macos as platform;
27
28#[cfg(feature = "rustls")]
29mod rustls;
30
31use std::io::Error;
32use std::io::BufRead;
33
34#[cfg(feature = "rustls")]
35pub use crate::rustls::{load_native_certs, PartialResult};
36
37pub trait RootStoreBuilder {
38 fn load_der(&mut self, der: Vec<u8>) -> Result<(), Error>;
39 fn load_pem_file(&mut self, rd: &mut dyn BufRead) -> Result<(), Error>;
40}
41
42/// Loads root certificates found in the platform's native certificate
43/// store, executing callbacks on the provided builder.
44///
45/// This function fails in a platform-specific way, expressed in a `std::io::Error`.
46///
47/// This function can be expensive: on some platforms it involves loading
48/// and parsing a ~300KB disk file. It's therefore prudent to call
49/// this sparingly.
50pub fn build_native_certs<B: RootStoreBuilder>(builder: &mut B) -> Result<(), Error> {
51 platform::build_native_certs(builder)
52}