diff --git a/libs/tls/src/cmd.rs b/libs/tls/src/cmd.rs index 472231eb0..7a690cf1d 100644 --- a/libs/tls/src/cmd.rs +++ b/libs/tls/src/cmd.rs @@ -1,15 +1,17 @@ -use crate::Tls; -use locales::t; -use rustls::pki_types::ServerName; use std::convert::TryFrom; use std::io::Read; use std::io::Write; use std::net::TcpStream; use std::str::from_utf8; use std::sync::Arc; + +use locales::t; +use rustls::pki_types::ServerName; #[cfg(feature = "rootCA")] use {modals::Modals, std::convert::TryInto, xous_names::XousNames}; +use crate::Tls; + pub fn shellchat<'a>(mut tokens: impl Iterator) -> Result, xous::Error> { use core::fmt::Write; let mut ret = String::new(); diff --git a/libs/tls/src/danger.rs b/libs/tls/src/danger.rs index 0103623c0..f9a846f8b 100644 --- a/libs/tls/src/danger.rs +++ b/libs/tls/src/danger.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use locales::t; use modals::Modals; use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier}; @@ -5,13 +7,11 @@ use rustls::client::WebPkiServerVerifier; use rustls::crypto::{ring, verify_tls12_signature, verify_tls13_signature, WebPkiSupportedAlgorithms}; use rustls::pki_types::{CertificateDer, Der, ServerName, TrustAnchor, UnixTime}; use rustls::{CertificateError, DigitallySignedStruct, Error, RootCertStore, SignatureScheme}; -use std::sync::Arc; use xous_names::XousNames; /// The entire purpose of the StifledCertificateVerification is to gain access to /// the certificate-chain-of-trust offered by a host by stifling CertificateError::UnknownIssuer /// see https://github.com/rustls/rustls/issues/1819 -/// #[derive(Debug)] pub struct StifledCertificateVerification { pub roots: RootCertStore, @@ -27,7 +27,6 @@ impl StifledCertificateVerification { /// The somewhat hacky work-around is to add a single bogus TrustAnchor to RootCertStore, /// and neither the bogus TrustAnchor or the RootCertStore exist beyond the lifetime of /// this StifledCertificateVerification. - /// pub fn new() -> Self { let mut root_cert_store = rustls::RootCertStore::empty(); // rustls::ServerCertVerifierBuilder::build() returns a @@ -123,7 +122,5 @@ impl ServerCertVerifier for StifledCertificateVerification { verify_tls13_signature(message, cert, dss, &self.supported) } - fn supported_verify_schemes(&self) -> Vec { - self.supported.supported_schemes() - } + fn supported_verify_schemes(&self) -> Vec { self.supported.supported_schemes() } } diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 59ba7457f..8e530f560 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -3,6 +3,11 @@ mod danger; pub mod ota; pub mod xtls; +use std::convert::{Into, TryFrom, TryInto}; +use std::io::{Error, ErrorKind, Read, Write}; +use std::net::TcpStream; +use std::sync::Arc; + use locales::t; use modals::Modals; use ota::OwnedTrustAnchor; @@ -13,10 +18,6 @@ use rkyv::{ }; use rustls::pki_types::{CertificateDer, TrustAnchor}; use rustls::{ClientConfig, ClientConnection, RootCertStore}; -use std::convert::{Into, TryFrom, TryInto}; -use std::io::{Error, ErrorKind, Read, Write}; -use std::net::TcpStream; -use std::sync::Arc; use x509_parser::prelude::{parse_x509_certificate, FromDer, X509Certificate}; use xous_names::XousNames; @@ -28,9 +29,7 @@ pub struct Tls { } impl Tls { - pub fn new() -> Tls { - Tls { pddb: pddb::Pddb::new() } - } + pub fn new() -> Tls { Tls { pddb: pddb::Pddb::new() } } /// Presents a modal to the user to select trusted tls certificates /// and saves the selected certificates to the pddb @@ -42,7 +41,6 @@ impl Tls { /// # Returns /// /// a count of trusted certificates - /// pub fn trust_modal(&self, certificates: Vec) -> usize { let xns = XousNames::new().unwrap(); let modals = Modals::new(&xns).unwrap(); @@ -94,7 +92,6 @@ impl Tls { /// # Returns /// /// the number of trust-anchors deleted - /// pub fn del_all_rota(&self) -> Result { let count = match self.pddb.list_keys(TLS_TRUSTED_DICT, None) { Ok(list) => list.len(), @@ -115,7 +112,6 @@ impl Tls { /// # Arguments /// /// * `key` - the pddb-key containing the unwanted trust-anchor - /// pub fn del_rota(&self, key: &str) -> Result<(), Error> { match self.pddb.delete_key(TLS_TRUSTED_DICT, key, None) { Ok(_) => { @@ -132,7 +128,6 @@ impl Tls { /// # Arguments /// /// * `ta` - a trusted trust-anchor - /// pub fn save_ta(&self, ta: &OwnedTrustAnchor) -> Result<(), Error> { match ta.pddb_key() { Ok(key) => { @@ -183,7 +178,6 @@ impl Tls { /// # Arguments /// /// * `key` - pddb key holding the trust-anchor - /// pub fn get_ota(&self, key: &str) -> Option { match self.pddb.get(TLS_TRUSTED_DICT, key, None, false, false, None, None::) { Ok(mut pddb_key) => { @@ -218,7 +212,6 @@ impl Tls { /// # Returns /// /// a Vec of OwnedTrustAnchor. - /// pub fn trusted(&self) -> Vec { match self.pddb.list_keys(TLS_TRUSTED_DICT, None) { Ok(list) => list @@ -242,7 +235,6 @@ impl Tls { /// # Returns /// /// true if the certificate is saved in the TLS_TRUSTED_DICT in the pddb - /// pub fn is_trusted_cert(&self, cert: CertificateDer) -> bool { match parse_x509_certificate(cert.as_ref()) { Ok(result) => self.is_trusted_x509(&result.1), @@ -294,7 +286,6 @@ impl Tls { /// # Returns /// /// a RootCertStore suitable for rustls - /// pub fn root_store(&self) -> RootCertStore { let mut root_store = RootCertStore::empty(); let trusted = match self.pddb.list_keys(TLS_TRUSTED_DICT, None) { @@ -331,7 +322,6 @@ impl Tls { /// /// * A Vec containing the TLS chain of trust offered by the host /// * Error if the communication with the host fails - /// pub fn probe(&self, host: &str) -> Result, Error> { log::info!("starting TLS probe"); match host.to_owned().try_into() { @@ -387,7 +377,6 @@ impl Tls { /// # Returns /// /// the number of trusted Certificates offered by the host - /// pub fn inspect(&self, host: &str) -> Result { match self.probe(host) { Ok(certs) => { @@ -418,7 +407,6 @@ impl Tls { /// # Returns /// /// true if the user trusts at least one of the Certificates offered by the host. - /// pub fn accessible(&self, host: &str, inspect: bool) -> bool { match self.probe(host) { Ok(certs) => match certs.iter().find(|&cert| self.is_trusted_cert(cert.clone())) { @@ -446,7 +434,6 @@ impl Tls { /// # Returns /// /// an owned rusttls stream on the tcp-stream provided - /// pub fn stream_owned( &self, host: &str, diff --git a/libs/tls/src/ota.rs b/libs/tls/src/ota.rs index 9e3c32dc6..b84d1fd79 100644 --- a/libs/tls/src/ota.rs +++ b/libs/tls/src/ota.rs @@ -1,11 +1,12 @@ // A rkyv serialisable intermediatory for a TrustAnchor -use der::{Encode, Header, Reader, Tag}; -use rkyv::{Archive, Deserialize, Serialize}; -use rustls::pki_types::{Der, TrustAnchor}; use std::cmp::min; use std::convert::TryInto; use std::fmt; use std::io::{Error, ErrorKind}; + +use der::{Encode, Header, Reader, Tag}; +use rkyv::{Archive, Deserialize, Serialize}; +use rustls::pki_types::{Der, TrustAnchor}; use x509_parser::prelude::{FromDer, X509Certificate}; use x509_parser::x509::X509Name; @@ -44,7 +45,6 @@ impl OwnedTrustAnchor { /// # Returns /// /// An OwnedTrustAnchor based on the supplied X509Certificate - /// pub fn from_x509(x509: &X509Certificate) -> Result { match ( rm_der_header(x509.subject().as_raw()), diff --git a/libs/tls/src/xtls.rs b/libs/tls/src/xtls.rs index 81e0c02aa..1894267e4 100644 --- a/libs/tls/src/xtls.rs +++ b/libs/tls/src/xtls.rs @@ -1,6 +1,7 @@ +use std::{convert::TryFrom, fmt::Debug, io, net::TcpStream, result::Result, sync::Arc}; + use rustls::pki_types::ServerName; use rustls::{ClientConnection, StreamOwned}; -use std::{convert::TryFrom, fmt::Debug, io, net::TcpStream, result::Result, sync::Arc}; use ureq::{ReadWrite, Response}; use crate::Tls; @@ -92,35 +93,23 @@ pub struct TlsStream(StreamOwned>); impl TlsStream { /// Returns a shared reference to the inner stream. - pub fn get_ref(&self) -> &Box { - self.0.get_ref() - } + pub fn get_ref(&self) -> &Box { self.0.get_ref() } /// Returns a mutable reference to the inner stream. - pub fn get_mut(&mut self) -> &mut Box { - self.0.get_mut() - } + pub fn get_mut(&mut self) -> &mut Box { self.0.get_mut() } } impl io::Read for TlsStream { - fn read(&mut self, buf: &mut [u8]) -> Result { - Ok(self.0.read(buf)?) - } + fn read(&mut self, buf: &mut [u8]) -> Result { Ok(self.0.read(buf)?) } } impl io::Write for TlsStream { - fn write(&mut self, buf: &[u8]) -> Result { - Ok(self.0.write(buf)?) - } + fn write(&mut self, buf: &[u8]) -> Result { Ok(self.0.write(buf)?) } - fn flush(&mut self) -> Result<(), std::io::Error> { - Ok(self.0.flush()?) - } + fn flush(&mut self) -> Result<(), std::io::Error> { Ok(self.0.flush()?) } } // required for the return type in TlsConnect::connect() impl ReadWrite for TlsStream { - fn socket(&self) -> Option<&TcpStream> { - self.get_ref().socket() - } + fn socket(&self) -> Option<&TcpStream> { self.get_ref().socket() } }