From b67c015abc4964d68b0e56a36a4b274f9322ca91 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 27 Jun 2023 14:40:30 +1000 Subject: [PATCH] refactor `tls::trusted` --- libs/tls/src/lib.rs | 52 +++++-------------------------------- libs/tls/src/trusted.rs | 57 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 45 deletions(-) create mode 100644 libs/tls/src/trusted.rs diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 3b12da43d..7de729951 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -1,5 +1,6 @@ pub mod cmd; pub mod danger; +pub mod trusted; use modals::Modals; use rkyv::{ @@ -88,55 +89,16 @@ impl Into 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, - } - } -} -impl Into for Trusted { - fn into(self) -> RootCertStore { - let mut root_store = rustls::RootCertStore::empty(); - let trusted = Trusted::new().unwrap(); - root_store - .add_server_trust_anchors(trusted.map(|t| Into::::into(t))); - root_store + + + + + + } } diff --git a/libs/tls/src/trusted.rs b/libs/tls/src/trusted.rs new file mode 100644 index 000000000..763026792 --- /dev/null +++ b/libs/tls/src/trusted.rs @@ -0,0 +1,57 @@ +use crate::get_cert; +use crate::{RustlsOwnedTrustAnchor, TLS_CERT_DICT}; +use rustls::RootCertStore; +use std::io::{Error, ErrorKind, Read, Write}; +use std::path::PathBuf; + +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, + } + } +} + +impl Into for Trusted { + fn into(self) -> RootCertStore { + let mut root_store = rustls::RootCertStore::empty(); + let trusted = Trusted::new().unwrap(); + root_store + .add_server_trust_anchors(trusted.map(|t| Into::::into(t))); + root_store + } +}