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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use openssl_probe;
use rustls::RootCertStore;
use std::io::{Error, ErrorKind};
use std::io::BufReader;
use std::fs::File;
use std::path::Path;
use crate::PartialResult;
fn load_file(store: &mut RootCertStore, path: &Path) -> Result<(), Error> {
let f = File::open(&path)?;
let mut f = BufReader::new(f);
if store.add_pem_file(&mut f).is_err() {
Err(Error::new(ErrorKind::InvalidData,
format!("Could not load PEM file {:?}", path)))
} else {
Ok(())
}
}
pub fn load_native_certs() -> PartialResult<RootCertStore, Error> {
let likely_locations = openssl_probe::probe();
let mut store = RootCertStore::empty();
let mut first_error = None;
if let Some(file) = likely_locations.cert_file {
match load_file(&mut store, &file) {
Err(err) => {
first_error = first_error.or_else(|| Some(err));
}
_ => {}
}
}
if let Some(err) = first_error {
if store.is_empty() {
Err((None, err))
} else {
Err((Some(store), err))
}
} else {
Ok(store)
}
}