rustls_native_certs/
unix.rs

1use crate::RootStoreBuilder;
2use openssl_probe;
3use std::io::{Error, ErrorKind};
4use std::io::BufReader;
5use std::fs::File;
6use std::path::Path;
7
8fn load_file(builder: &mut impl RootStoreBuilder, path: &Path) -> Result<(), Error> {
9    let f = File::open(&path)?;
10    let mut f = BufReader::new(f);
11    if builder.load_pem_file(&mut f).is_err() {
12        Err(Error::new(ErrorKind::InvalidData,
13                       format!("Could not load PEM file {:?}", path)))
14    } else {
15        Ok(())
16    }
17}
18
19pub fn build_native_certs<B: RootStoreBuilder>(builder: &mut B) -> Result<(), Error> {
20    let likely_locations = openssl_probe::probe();
21    let mut first_error = None;
22
23    if let Some(file) = likely_locations.cert_file {
24        match load_file(builder, &file) {
25            Err(err) => {
26                first_error = first_error.or_else(|| Some(err));
27            }
28            _ => {}
29        }
30    }
31
32    if let Some(err) = first_error {
33        Err(err)
34    } else {
35        Ok(())
36    }
37}