From 8ab3aa85bebcca8eb2e4a59162260b2409e4985d Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Mon, 11 Sep 2023 08:05:25 +1000 Subject: [PATCH] implement tls.probe(target) moved from tls::cmd to tls::lib --- libs/tls/src/cmd.rs | 52 +++++---------------------------------- libs/tls/src/lib.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 46 deletions(-) diff --git a/libs/tls/src/cmd.rs b/libs/tls/src/cmd.rs index da877270f..fb372504c 100644 --- a/libs/tls/src/cmd.rs +++ b/libs/tls/src/cmd.rs @@ -75,60 +75,20 @@ pub fn shellchat<'a>( // pddb if trusted. Some("probe") => { log::set_max_level(log::LevelFilter::Info); - log::info!("starting TLS probe"); - // Attempt to open the tls connection with an empty root_store - let root_store = rustls::RootCertStore::empty(); - // Stifle the default rustls certificate verification's complaint about an - // unknown/untrusted CA root certificate so that we get to see the certificate chain - let stifled_verifier = - Arc::new(danger::StifledCertificateVerification { roots: root_store }); - let config = rustls::ClientConfig::builder() - .with_safe_defaults() - .with_custom_certificate_verifier(stifled_verifier) - .with_no_client_auth(); let target = match tokens.next() { Some(target) => target, - None => "bunnyfoo.com", + None => "betrusted.io", }; - let server_name = target.try_into().unwrap_or_else(|e| { - log::warn!("failed to create sever_name from {target}: {e}"); - write!( + let tls = Tls::new(); + match tls.probe(target) { + Ok(count) => write!(ret, "{} {}", count, t!("tls.probe_done", locales::LANG)).ok(), + Err(_) => write!( ret, "{} {target}", t!("tls.probe_fail_servername", locales::LANG) ) - .ok(); - "bunnyfoo.com".try_into().unwrap() - }); - let mut conn = rustls::ClientConnection::new(Arc::new(config), server_name).unwrap(); - log::info!("connect TCPstream to {}", target); - let url = format!("{}:443", target); - match TcpStream::connect(url) { - Ok(mut sock) => { - match conn.complete_io(&mut sock) { - Ok(_) => log::info!("handshake complete"), - Err(e) => { - write!(ret, "{e}").ok(); - log::warn!("{e}"); - } - } - conn.send_close_notify(); - - match conn.peer_certificates() { - Some(certificates) => { - let tls = Tls::new(); - let count = tls.check_trust(certificates); - write!(ret, "{} {}", count, t!("tls.probe_done", locales::LANG)).ok(); - } - None => (), - }; - } - Err(e) => { - write!(ret, "{e}").ok(); - log::warn!("{e}") - } + .ok(), }; - log::set_max_level(log::LevelFilter::Info); } diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index fdf7451ce..8f34bd601 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -244,6 +244,65 @@ impl Tls { } root_store } + + /// Probes the target and returns true is at least 1 certificate is trusted + /// + /// Establishes a tls connection to the target host, extracts the + /// certificates offered and immediately closes the connection. + /// The certificates are presented by modal to the user, and saved to the + /// pddb if trusted. + /// + /// By default, rustls only provides access to a trusted certificate chain. + /// Probe briefly stifles the certificate validation (ie trusts everything) + /// in order to grab the untrusted cetificate chain and present it to the + /// user for examination. + /// + /// # Arguments + /// + /// * `target` - the target tls site (i.e. betrusted.io) + /// + pub fn probe(&self, target: &str) -> Result { + log::info!("starting TLS probe"); + // Attempt to open the tls connection with an empty root_store + let root_store = rustls::RootCertStore::empty(); + // Stifle the default rustls certificate verification's complaint about an + // unknown/untrusted CA root certificate so that we get to see the certificate chain + let stifled_verifier = + Arc::new(danger::StifledCertificateVerification { roots: root_store }); + let config = rustls::ClientConfig::builder() + .with_safe_defaults() + .with_custom_certificate_verifier(stifled_verifier) + .with_no_client_auth(); + match target.try_into() { + Ok(server_name) => { + let mut conn = + rustls::ClientConnection::new(Arc::new(config), server_name).unwrap(); + log::info!("connect TCPstream to {}", target); + let url = format!("{}:443", target); + match TcpStream::connect(url) { + Ok(mut sock) => { + match conn.complete_io(&mut sock) { + Ok(_) => log::info!("handshake complete"), + Err(e) => log::warn!("{e}"), + } + conn.send_close_notify(); + match conn.peer_certificates() { + Some(certificates) => Ok(self.check_trust(certificates)), + None => Ok(0), + } + } + Err(e) => { + log::warn!("{e}"); + Err(e) + } + } + } + Err(e) => { + log::warn!("failed to create sever_name from {target}: {e}"); + Err(Error::from(ErrorKind::InvalidInput)) + } + } + } } // https://stackoverflow.com/questions/57029974/how-to-split-string-into-chunks-in-rust-to-insert-spaces