implement tls Trusted iterator

This commit is contained in:
nworbnhoj 2023-06-24 14:59:47 +10:00
parent 65f92453a2
commit 0d1e89e560

View file

@ -51,6 +51,48 @@ impl<'a> From<&X509Certificate<'a>> for RustlsOwnedTrustAnchor {
}
}
pub struct Trusted {
trusted: std::fs::ReadDir,
}
impl Trusted {
pub fn new() -> Result<Trusted, Error> {
let mut keypath = PathBuf::new();
keypath.push(TLS_CERT_DICT);
if !std::fs::metadata(&keypath).is_ok() {
log::info!("dict '{}' does NOT exist.. creating it", TLS_CERT_DICT);
std::fs::create_dir_all(&keypath)?;
}
Ok(Self {
trusted: std::fs::read_dir(keypath)?,
})
}
}
impl Iterator for Trusted {
type Item = RustlsOwnedTrustAnchor;
fn next(&mut self) -> Option<Self::Item> {
match self.trusted.next() {
Some(Ok(dir_entry)) => match dir_entry.file_name().into_string() {
Ok(key) => {
log::info!("path: {}", key);
get_cert(&key).unwrap_or(None)
}
Err(e) => {
log::warn!("failed to read cert: {:?}", e);
None
}
},
Some(Err(e)) => {
log::warn!("failed to list trusted certs: {}", e);
None
}
None => None,
}
}
}
// presents a modal to the user to select trusted tls certificates
// and saves the selected certificates to the pddb
// returns false if no certificates are trusted - and true otherwise