From 12c74e724fc90c5743ff12cfbc12c516975a76b6 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Thu, 21 Dec 2023 06:51:54 +1100 Subject: [PATCH] implement Tls.inspect(host) Tls.probe() now returns a Vec and Tls.inspect(host) presents the trust_modal to the user --- apps/sigchat/src/lib.rs | 21 ++++++++++--------- libs/tls/src/lib.rs | 46 +++++++++++++++++++++++++++-------------- libs/tls/src/xtls.rs | 8 +++---- 3 files changed, 46 insertions(+), 29 deletions(-) diff --git a/apps/sigchat/src/lib.rs b/apps/sigchat/src/lib.rs index db37bb433..03f8d1160 100644 --- a/apps/sigchat/src/lib.rs +++ b/apps/sigchat/src/lib.rs @@ -178,16 +178,17 @@ impl<'a> SigChat<'a> { self.chat.set_busy_state(true); let tls = Tls::new(); match tls.probe(host) { - Ok(0) => { - self.modals - .show_notification(t!("sigchat.account.abort", locales::LANG), None) - .expect("abort failed"); - self.chat.set_busy_state(false); - false - } - Ok(_count) => { - self.chat.set_busy_state(false); - true + Ok(certs) => { + if certs.len() == 0 { + self.modals + .show_notification(t!("sigchat.account.abort", locales::LANG), None) + .expect("abort failed"); + self.chat.set_busy_state(false); + false + } else { + self.chat.set_busy_state(false); + true + } } Err(e) => { self.chat.set_busy_state(false); diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 2f7570c03..84f79370e 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -45,7 +45,7 @@ impl Tls { /// /// a count of trusted certificates /// - pub fn trust_modal(&self, certificates: &[Certificate]) -> usize { + pub fn trust_modal(&self, certificates: Vec) -> usize { let xns = XousNames::new().unwrap(); let modals = Modals::new(&xns).unwrap(); @@ -278,12 +278,10 @@ impl Tls { root_store } - /// Probes the target and returns a count of newly trusted 1 certificates + /// Probes the host and returns the TLS chain of trust /// - /// Establishes a tls connection to the target host, extracts the + /// Establishes a tls connection to the 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) @@ -291,10 +289,12 @@ impl Tls { /// user for examination. /// /// # Arguments + /// * `host` - the target tls site (i.e. betrusted.io) /// - /// * `target` - the target tls site (i.e. betrusted.io) + /// # Returns + /// The TLS chain of trust for the host /// - pub fn probe(&self, target: &str) -> Result { + pub fn probe(&self, host: &str) -> Result, Error> { log::info!("starting TLS probe"); // Attempt to open the tls connection with an empty root_store let root_store = rustls::RootCertStore::empty(); @@ -306,12 +306,12 @@ impl Tls { .with_safe_defaults() .with_custom_certificate_verifier(stifled_verifier) .with_no_client_auth(); - match target.try_into() { + match host.try_into() { Ok(server_name) => { let mut conn = rustls::ClientConnection::new(Arc::new(config), server_name).unwrap(); - log::info!("connect TCPstream to {}", target); - match TcpStream::connect((target, 443)) { + log::info!("connect TCPstream to {}", host); + match TcpStream::connect((host, 443)) { Ok(mut sock) => { match conn.complete_io(&mut sock) { Ok(_) => log::info!("handshake complete"), @@ -319,8 +319,8 @@ impl Tls { } conn.send_close_notify(); match conn.peer_certificates() { - Some(certificates) => Ok(self.trust_modal(certificates)), - None => Ok(0), + Some(certificates) => Ok(certificates.to_vec()), + None => Ok(vec![]), } } Err(e) => { @@ -330,14 +330,30 @@ impl Tls { } } Err(e) => { - log::warn!("failed to create sever_name from {target}: {e}"); + log::warn!("failed to create sever_name from {host}: {e}"); Err(Error::from(ErrorKind::InvalidInput)) } } } - pub fn inspect(&self, target: &str) -> Result { - self.probe(target) + /// Inspect and optionally trust Certificates offered by the host. + /// + /// Probes the host and presents the certificates offered in a modal. + /// The user can optionally save trusted certificates to the pddb. + /// + /// # Arguments + /// * `host` - the target tls site (i.e. betrusted.io) + /// + /// # Returns the number of trusted Certificates offered by the host + /// + pub fn inspect(&self, host: &str) -> Result { + match self.probe(host) { + Ok(certs) => Ok(self.trust_modal(certs)), + Err(e) => { + log::warn!("failed to probe {host}: {e}"); + Ok(0) + } + } } pub fn client_config(&self) -> ClientConfig { diff --git a/libs/tls/src/xtls.rs b/libs/tls/src/xtls.rs index 53e476f90..7867ad9a9 100644 --- a/libs/tls/src/xtls.rs +++ b/libs/tls/src/xtls.rs @@ -37,8 +37,8 @@ impl ureq::TlsConnector for TlsConnector { if let Some(inner) = e.get_ref() { if let Some(rustls_error) = inner.downcast_ref::() { if let rustls::Error::InvalidCertificate(_) = rustls_error { - if let Ok(n) = tls.probe(dns_name) { - if n > 0 { + if let Ok(certs) = tls.probe(dns_name) { + if certs.len() > 0 { log::info!("try again with new trusted certs"); continue; } @@ -54,8 +54,8 @@ impl ureq::TlsConnector for TlsConnector { } // errors generated early in the tls handshake Err(rustls::Error::InvalidCertificate(_)) => { - if let Ok(n) = tls.probe(dns_name) { - if n > 0 { + if let Ok(certs) = tls.probe(dns_name) { + if certs.len() > 0 { log::info!("try again with new trusted certs"); continue; }