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
use rustls::RootCertStore;
use std::io::{Error, ErrorKind};
use std::io::BufRead;
use crate::RootStoreBuilder;
pub type PartialResult<T, E> = Result<T, (Option<T>, E)>;
pub fn load_native_certs() -> PartialResult<RootCertStore, Error> {
struct RootCertStoreLoader {
store: RootCertStore,
};
impl RootStoreBuilder for RootCertStoreLoader {
fn load_der(&mut self, der: Vec<u8>) -> Result<(), Error> {
self.store.add(&rustls::Certificate(der))
.map_err(|err| Error::new(ErrorKind::InvalidData, err))
}
fn load_pem_file(&mut self, rd: &mut dyn BufRead) -> Result<(), Error> {
self.store.add_pem_file(rd)
.map(|_| ())
.map_err(|()| Error::new(ErrorKind::InvalidData, format!("could not load PEM file")))
}
}
let mut loader = RootCertStoreLoader {
store: RootCertStore::empty(),
};
match crate::build_native_certs(&mut loader) {
Err(err) if loader.store.is_empty() => Err((None, err)),
Err(err) => Err((Some(loader.store), err)),
Ok(()) => Ok(loader.store),
}
}