1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use crate::RootStoreBuilder;
use openssl_probe;
use std::io::{Error, ErrorKind};
use std::io::BufReader;
use std::fs::File;
use std::path::Path;

fn load_file(builder: &mut impl RootStoreBuilder, path: &Path) -> Result<(), Error> {
    let f = File::open(&path)?;
    let mut f = BufReader::new(f);
    if builder.load_pem_file(&mut f).is_err() {
        Err(Error::new(ErrorKind::InvalidData,
                       format!("Could not load PEM file {:?}", path)))
    } else {
        Ok(())
    }
}

pub fn build_native_certs<B: RootStoreBuilder>(builder: &mut B) -> Result<(), Error> {
    let likely_locations = openssl_probe::probe();
    let mut first_error = None;

    if let Some(file) = likely_locations.cert_file {
        match load_file(builder, &file) {
            Err(err) => {
                first_error = first_error.or_else(|| Some(err));
            }
            _ => {}
        }
    }

    if let Some(err) = first_error {
        Err(err)
    } else {
        Ok(())
    }
}