From 0d1e89e560a543249b4dae2dd6b90fdb29c84207 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Sat, 24 Jun 2023 14:59:47 +1000 Subject: [PATCH] implement tls Trusted iterator --- libs/tls/src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 4a34be8e2..0410b57e0 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -51,6 +51,48 @@ impl<'a> From<&X509Certificate<'a>> for RustlsOwnedTrustAnchor { } } + +pub struct Trusted { + trusted: std::fs::ReadDir, +} + +impl Trusted { + pub fn new() -> Result { + 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 { + 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