diff --git a/Cargo.toml b/Cargo.toml index fd7d28780..4560c8357 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -89,7 +89,8 @@ members = [ "tools/perflib", "kernel", "loader", - "libs/userprefs" + "libs/userprefs", + "libs/tls" ] resolver = "2" diff --git a/libs/tls/Cargo.toml b/libs/tls/Cargo.toml new file mode 100644 index 000000000..228d65eef --- /dev/null +++ b/libs/tls/Cargo.toml @@ -0,0 +1,34 @@ +[package] +authors = ["john brown "] +description = "TLS tools" +edition = "2018" +name = "tls" +version = "0.1.0" + +[dependencies] +log = "0.4.14" +num-derive = { version = "0.3.3", default-features = false} +num-traits = { version = "0.2.14", default-features = false} +xous = "0.9.45" +xous-ipc = "0.9.45" +log-server = { package = "xous-api-log", version = "0.1.41" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.41" } +xous-names = { package = "xous-api-names", version = "0.9.43" } + +modals = { path = "../../services/modals" } +net = { path = "../../services/net" } +pddb = { path = "../../services/pddb" } + +der = { version = "0.7.6", features = ["derive"] } +locales = {path = "../../locales"} +rkyv = "0.4.3" +sha2 = "0.9.8" + +# note requirement for patch to xous-ring in workspace Cargo.toml +rustls = { version = "0.21.2", features = ["dangerous_configuration"] } +webpki = { package = "rustls-webpki", version = "0.100.1" } +webpki-roots = {version = "0.23.1", optional = true} +x509-parser = "0.15.0" + +[features] +rootCA = ["webpki-roots"] diff --git a/libs/tls/locales/i18n.json b/libs/tls/locales/i18n.json new file mode 100644 index 000000000..d2b499427 --- /dev/null +++ b/libs/tls/locales/i18n.json @@ -0,0 +1,62 @@ +{ + "tls.check_trust_prompt": { + "en": "Do you trust any of these certificate authorities?", + "en-tts": "Do you trust any of these certificate authorities?" + }, + "tls.cmd_help": { + "en": "Establishing an encrypted tls connection requires the host to provide a signed Certificate of identity. Each Certificate is signed for authenticity by a Certificate Authority. The CA's Certificate will in-turn be signed by yet another CA. These signed Certificates link into a chain of trust back to a trusted Root CA.\n\nTypically, this all happens automagically because many OS's & browsers incorporate a long list of trusted Root CA Certiicates. And this is OK because you trust the CA's that your OS trusts, right!\n\nOn Precursor, you must explicitly trust one or more root CA Certificates. You can:\nnet tls probe \n\tto trust a specific CA Certificate, or\nnet tls mozilla\n\tto trust all of the CA Certificates incporporated in the Firefox browser.", + "en-tts": "Establishing an encrypted tls connection requires the host to provide a signed Certificate of identity. Each Certificate is signed for authenticity by a Certificate Authority. The CA's Certificate will in-turn be signed by yet another CA. These signed Certificates link into a chain of trust back to a trusted Root CA.\n\nTypically, this all happens automagically because many OS's & browsers incorporate a long list of trusted Root CA Certiicates. And this is OK because you trust the CA's that your OS trusts, right!\n\nOn Precursor, you must explicitly trust one or more root CA Certificates. You can:\nnet tls probe \n\tto trust a specific CA Certificate, or\nnet tls mozilla\n\tto trust all of the CA Certificates incporporated in the Firefox browser." + }, + "tls.cmd": { + "en": "net tls ", + "en-tts": "net tls " + }, + "tls.deleteall_cmd": { + "en": "delete ALL trusted Certificates", + "en-tts": "delete ALL trusted Certificates" + }, + "tls.deleteall_done": { + "en": "deleted Certificates", + "en-tts": "deleted Certificates" + }, + "tls.list_cmd": { + "en": "list trusted CA certificates", + "en-tts": "list trusted CA certificates" + }, + "tls.mozilla_cmd": { + "en": "trust all Root CA's in webpki-roots", + "en-tts": "trust all Root CA's in webpki-roots" + }, + "tls.mozilla_progress": { + "en": "trusting Mozilla Root CA's", + "en-tts": "trusting Mozilla Root CA's" + }, + "tls.mozilla_done": { + "en": "trusted Root CA's", + "en-tts": "trusted Root CA's" + }, + "tls.probe_cmd": { + "en": "save host CA'a if trusted", + "en-tts": "save host CA'a if trusted" + }, + "tls.probe_fail_servername": { + "en": "failed to create sever name from", + "en-tts": "failed to create sever name from" + }, + "tls.test_cmd": { + "en": "make tls connection to host", + "en-tts": "make tls connection to host" + }, + "tls.test_success_tcp": { + "en": "tcp connected\n", + "en-tts": "tcp connected" + }, + "tls.test_success_get": { + "en": "tls accepted GET\n", + "en-tts": "tls accepted GET" + }, + "tls.test_success_bytes": { + "en": "tls received bytes", + "en-tts": "tls received bytes" + } +} diff --git a/libs/tls/src/cmd.rs b/libs/tls/src/cmd.rs new file mode 100644 index 000000000..bb08a5320 --- /dev/null +++ b/libs/tls/src/cmd.rs @@ -0,0 +1,220 @@ +use crate::{danger, Tls}; +use locales::t; +use modals::Modals; +use std::convert::TryInto; +use std::io::Read; +use std::io::Write; +use std::net::TcpStream; +use std::str::from_utf8; +use std::sync::Arc; +use xous_names::XousNames; + +pub fn shellchat<'a>( + mut tokens: impl Iterator, +) -> Result, xous::Error> { + use core::fmt::Write; + let mut ret = String::new(); + match tokens.next() { + // delete ALL trusted CA Certificates + Some("deleteall") => { + log::info!("starting TLS delete certificates"); + let tls = Tls::new(); + let count = tls.del_all_cert().unwrap(); + write!(ret, "{} {}", count, t!("tls.deleteall_done", locales::LANG)).ok(); + log::info!("finished TLS delete certificates"); + } + // helpful stuff + Some("help") => { + write!(ret, "{}", t!("tls.cmd_help", locales::LANG)).ok(); + } + // list trusted Certificate Authority certificates + Some("list") => { + log::set_max_level(log::LevelFilter::Info); + log::info!("starting TLS trusted listing"); + let tls = Tls::new(); + for rota in tls.trusted() { + write!(ret, "🏛 {}\n", rota.subject()).ok(); + } + log::info!("finished TLS trusted listing"); + } + // save/trust all Root CA's in webpki-roots en-masse + #[cfg(feature = "rootCA")] + Some("mozilla") => { + let rotas: Vec = webpki_roots::TLS_SERVER_ROOTS + .0 + .iter() + .map(|ta| { + crate::RustlsOwnedTrustAnchor::from_subject_spki_name_constraints( + ta.subject, + ta.spki, + ta.name_constraints, + ) + }) + .collect(); + let mut count:u32 = rotas.len().try_into().unwrap(); + let xns = XousNames::new().unwrap(); + let modals = Modals::new(&xns).unwrap(); + modals + .start_progress(t!("tls.mozilla_progress", locales::LANG), 0, count, 0) + .expect("no progress"); + count = 0; + let tls = Tls::new(); + for rota in rotas { + tls.save_cert(&rota).unwrap_or_else(|e| log::warn!("{e}")); + modals.update_progress(count).expect("no progress"); + count += 1; + } + modals.finish_progress().expect("finish progress"); + write!(ret, "{} {}", count, t!("tls.mozilla_done", locales::LANG)).ok(); + } + // probe establishes a tls connection to the supplied 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. + 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", + }; + let server_name = target.try_into().unwrap_or_else(|e| { + log::warn!("failed to create sever_name from {target}: {e}"); + 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, "trusted {count} certificates").ok(); + } + None => (), + }; + } + Err(e) => { + write!(ret, "{e}").ok(); + log::warn!("{e}") + } + }; + + log::set_max_level(log::LevelFilter::Info); + } + + Some("test") => { + log::set_max_level(log::LevelFilter::Info); + log::info!("starting TLS run"); + log::info!("build TLS client config"); + let tls = Tls::new(); + let config = rustls::ClientConfig::builder() + .with_safe_defaults() + .with_root_certificates(tls.root_store()) + .with_no_client_auth(); + let target = match tokens.next() { + Some(target) => target, + None => "bunnyfoo.com", + }; + log::info!("point TLS to {}", target); + let mut conn = + rustls::ClientConnection::new(Arc::new(config), target.try_into().unwrap()) + .unwrap(); + + log::info!("connect TCPstream to {}", target); + let url = format!("{}:443", target); + match TcpStream::connect(url) { + Ok(mut sock) => { + log::info!("tcp connected"); + write!(ret, "{}", t!("tls.test_success_tcp", locales::LANG)).ok(); + let mut tls = rustls::Stream::new(&mut conn, &mut sock); + log::info!("create http headers and write to server"); + let msg = format!("GET / HTTP/1.1\r\nHost: {}\r\nConnection: close\r\nAccept-Encoding: identity\r\n\r\n", target); + match tls.write_all(msg.as_bytes()) { + Ok(()) => { + log::info!("tls accepted GET"); + write!(ret, "{}", t!("tls.test_success_get", locales::LANG)).ok(); + let mut plaintext = Vec::new(); + log::info!("read TLS response"); + match tls.read_to_end(&mut plaintext) { + Ok(n) => { + log::info!("tls received {} bytes", n); + write!( + ret, + "{} {}\n", + t!("tls.test_success_bytes", locales::LANG), + n + ) + .ok(); + log::info!("{}", from_utf8(&plaintext).unwrap_or("utf-error")); + } + Err(e) => { + log::warn!("failed to read tls response: {e}"); + write!(ret, "{e}\n").ok(); + } + }; + } + Err(e) => { + log::warn!("failed to GET on tls connection: {e}"); + write!(ret, "{e}\n").ok(); + } + }; + } + Err(e) => { + log::warn!("failed to connect tcp: {e}"); + write!(ret, "{e}\n").ok(); + } + }; + + log::set_max_level(log::LevelFilter::Info); + } + None | _ => { + write!(ret, "{}\n", t!("tls.cmd", locales::LANG)).ok(); + write!(ret, "\tdeleteall\t{}\n", t!("tls.deleteall_cmd", locales::LANG)).ok(); + write!(ret, "\thelp\n").ok(); + write!(ret, "\tlist\t{}\n", t!("tls.list_cmd", locales::LANG)).ok(); + #[cfg(feature = "rootCA")] + write!(ret, "\tmozilla\t{}\n", t!("tls.mozilla_cmd", locales::LANG)).ok(); + write!( + ret, + "\tprobe \t{}\n", + t!("tls.probe_cmd", locales::LANG) + ) + .ok(); + write!( + ret, + "\ttest \t{}\n", + t!("tls.test_cmd", locales::LANG) + ) + .ok(); + } + } + Ok(Some(ret)) +} diff --git a/libs/tls/src/danger.rs b/libs/tls/src/danger.rs new file mode 100644 index 000000000..b7c606788 --- /dev/null +++ b/libs/tls/src/danger.rs @@ -0,0 +1,37 @@ +use rustls::client::{ServerCertVerified, WebPkiVerifier}; +use rustls::{CertificateError, Error, RootCertStore}; + +pub struct StifledCertificateVerification { + pub roots: RootCertStore, +} + +impl rustls::client::ServerCertVerifier for StifledCertificateVerification { + /// Will verify the certificate with the default rustls WebPkiVerifier, + /// BUT specifically overrides a `CertificateError::UnknownIssuer` and + /// return ServerCertVerified::assertion() + fn verify_server_cert( + &self, + end_entity: &rustls::Certificate, + intermediates: &[rustls::Certificate], + server_name: &rustls::ServerName, + scts: &mut dyn Iterator, + ocsp: &[u8], + now: std::time::SystemTime, + ) -> Result { + let rustls_default_verifier = WebPkiVerifier::new(self.roots.clone(), None); + match rustls_default_verifier.verify_server_cert( + end_entity, + intermediates, + server_name, + scts, + ocsp, + now, + ) { + Ok(ok) => Ok(ok), + Err(Error::InvalidCertificate(CertificateError::UnknownIssuer)) => { + Ok(ServerCertVerified::assertion()) + } + Err(e) => Err(e), + } + } +} diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs new file mode 100644 index 000000000..22ba7272d --- /dev/null +++ b/libs/tls/src/lib.rs @@ -0,0 +1,265 @@ +pub mod cmd; +pub mod danger; +pub mod rota; + +use crate::rota::RustlsOwnedTrustAnchor; +use locales::t; +use modals::Modals; +use rkyv::{ + de::deserializers::AllocDeserializer, + ser::{serializers::WriteSerializer, Serializer}, + Deserialize, +}; +use rustls::{Certificate, RootCertStore}; +use sha2::Digest; +use std::convert::{Into, TryFrom}; +use std::io::{Error, Read, Write}; +use x509_parser::prelude::{FromDer, X509Certificate}; +use xous_names::XousNames; + +/// PDDB Dict for tls trusted certificates keys +const TLS_TRUSTED_DICT: &str = "tls.trusted"; +const CURRENT_VERSION_KEY: &str = "__version"; + +pub struct Tls { + pddb: pddb::Pddb, +} + +impl Tls { + 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 + // returns a count of trusted certificates + pub fn check_trust(&self, certificates: &[Certificate]) -> usize { + let xns = XousNames::new().unwrap(); + let modals = Modals::new(&xns).unwrap(); + + let certificates: Vec<(String, X509Certificate)> = certificates + .iter() + .map(|cert| { + let mut hasher = sha2::Sha256::new(); + hasher.update(&cert); + ( + format!("{:X}", hasher.finalize()), + X509Certificate::from_der(cert.as_ref()), + ) + }) + .filter(|(_fingerprint, result)| result.is_ok()) + .map(|(fingerprint, result)| (fingerprint, result.unwrap().1)) + .filter(|(_fingerprint, x509)| x509.is_ca()) + .collect(); + + let chain: Vec = certificates + .iter() + .map(|(fingerprint, x509)| format!("🏛 {}\n{}", &x509.subject(), open_hex(fingerprint),)) + .collect(); + let chain: Vec<&str> = chain.iter().map(AsRef::as_ref).collect(); + modals + .add_list(chain) + .expect("couldn't build checkbox list"); + match modals.get_checkbox(t!("tls.check_trust_prompt", locales::LANG)) { + Ok(trusted) => { + trusted + .iter() + .for_each(|cert| log::info!("trusts {}", cert)); + modals + .get_check_index() + .unwrap() + .iter() + .map(|i| &certificates[*i].1) + .map(|x509| RustlsOwnedTrustAnchor::from(x509)) + .for_each(|rota| { + self.save_cert(&rota).unwrap_or_else(|e| { + log::warn!("failed to save cert: {e}"); + modals + .show_notification( + format!("failed to save:\n{}\n{e}", &rota.subject()).as_str(), + None, + ) + .expect("modal failed"); + }); + }); + trusted.len() + } + _ => { + log::error!("get_checkbox failed"); + 0 + } + } + } + + // deletes ALL tls trust-anchors from the pddb + // returns the number of certs deleted + pub fn del_all_cert(&self) -> Result { + let count = match self.pddb.list_keys(TLS_TRUSTED_DICT, None) { + Ok(list) => list.len(), + Err(_) => 0, + }; + match self.pddb.delete_dict(TLS_TRUSTED_DICT, None) { + Ok(_) => { + log::info!("Deleted {}\n", TLS_TRUSTED_DICT); + self.pddb + .sync() + .or_else(|e| Ok::<(), Error>(log::warn!("{e}"))) + .ok(); + // match self.pddb.sync() { + // Err(e) => log::warn!("{e}"), + // _ => (), + // } + } + Err(e) => log::warn!("failed to delete {}: {:?}", TLS_TRUSTED_DICT, e), + } + Ok(count) + } + + // deletes a tls trust-anchor from the pddb + pub fn del_cert(&self, key: &str) -> Result<(), Error> { + match self.pddb.delete_key(TLS_TRUSTED_DICT, key, None) { + Ok(_) => { + log::info!("Deleted {}:{}\n", TLS_TRUSTED_DICT, key); + self.pddb + .sync() + .or_else(|e| Ok::<(), Error>(log::warn!("{e}"))) + .ok(); + } + Err(e) => log::warn!("failed to delete {}:{}: {:?}", TLS_TRUSTED_DICT, key, e), + } + return Ok(()); + } + + // saves a tls trust-anchor to the pddb + pub fn save_cert(&self, ta: &RustlsOwnedTrustAnchor) -> Result<(), Error> { + let key = ta.pddb_key(); + match self.pddb.get( + TLS_TRUSTED_DICT, + &key, + None, + true, + true, + Some(rota::MAX_ROTA_BYTES), + None::, + ) { + Ok(mut pddb_key) => { + let mut buf = Vec::::new(); + // reserve 2 bytes to hold a u16 (see below) + let reserved = 2; + buf.push(0u8); + buf.push(0u8); + + // serialize the trust-anchor + let mut serializer = WriteSerializer::with_pos(buf, reserved); + let pos = serializer.serialize_value(ta).unwrap(); + let mut bytes = serializer.into_inner(); + + // copy pop u16 into the first 2 bytes to enable the rkyv archive to be deserialised + let pos: u16 = u16::try_from(pos).expect("data > u16"); + let pos_bytes = pos.to_be_bytes(); + bytes[0] = pos_bytes[0]; + bytes[1] = pos_bytes[1]; + match pddb_key.write(&bytes) { + Ok(len) => { + self.pddb.sync().ok(); + log::info!("Wrote {} bytes to {}:{}", len, TLS_TRUSTED_DICT, key); + } + Err(e) => { + log::warn!("Error writing {}:{}: {:?}", TLS_TRUSTED_DICT, key, e); + } + } + } + Err(e) => log::warn!("failed to create {}:{}\n{}", TLS_TRUSTED_DICT, key, e), + } + Ok(()) + } + + + // retrieves a tls trust-anchor from the pddb + pub fn get_cert(&self, key: &str) -> Option { + match self.pddb.get( + TLS_TRUSTED_DICT, + key, + None, + false, + false, + None, + None::, + ) { + Ok(mut pddb_key) => { + let mut bytes = [0u8; rota::MAX_ROTA_BYTES]; + match pddb_key.read(&mut bytes) { + Ok(_) => { + // extract pos u16 from the first 2 bytes + let pos: u16 = u16::from_be_bytes([bytes[0], bytes[1]]); + let pos: usize = pos.into(); + // deserialize the trust-anchor + let archive = unsafe { + rkyv::archived_value::(&bytes, pos) + }; + let ta = archive.deserialize(&mut AllocDeserializer {}).ok(); + log::info!("get '{}' = '{:?}'", key, &ta); + ta + } + Err(e) => { + log::warn!("failed to read {}: {e}", key); + return None; + } + } + } + Err(e) => { + log::warn!("failed to get {}: {e}", key); + return None; + } + } + } + + pub fn trusted(&self) -> Vec { + match self.pddb.list_keys(TLS_TRUSTED_DICT, None) { + Ok(list) => list + .iter() + .map(|key| self.get_cert(&key)) + .filter_map(|rota| rota) + .collect::>(), + Err(e) => { + log::warn!("failed to get iter over trusted: {e}"); + Vec::::new() + } + } + } + + pub fn root_store(&self) -> RootCertStore { + let mut root_store = RootCertStore::empty(); + match self.pddb.list_keys(TLS_TRUSTED_DICT, None) { + Ok(list) => { + let rota = list + .iter() + .map(|key| self.get_cert(&key)) + .filter_map(|rota| rota) + .map(|t| Into::::into(t)); + root_store.add_server_trust_anchors(rota); + } + Err(e) => log::warn!("failed to get iter over trusted: {e}"), + } + root_store + } +} + +// https://stackoverflow.com/questions/57029974/how-to-split-string-into-chunks-in-rust-to-insert-spaces +// insert a space between each hex value +fn open_hex(text: &str) -> String { + text.chars() + .enumerate() + .flat_map(|(i, c)| { + if i != 0 && i % 2 == 0 { + Some(' ') + } else { + None + } + .into_iter() + .chain(std::iter::once(c)) + }) + .collect::() +} diff --git a/libs/tls/src/rota.rs b/libs/tls/src/rota.rs new file mode 100644 index 000000000..c78e49f39 --- /dev/null +++ b/libs/tls/src/rota.rs @@ -0,0 +1,210 @@ +// A mirror of rustls::OwnedTrustAnchor + +use der::{Encode, Header, Reader, Tag}; +use rkyv::{Archive, Deserialize, Serialize}; +use std::cmp::min; +use std::convert::TryInto; +use std::fmt; +use std::io::{Error, ErrorKind}; +use x509_parser::prelude::{FromDer, X509Certificate}; + +pub const MAX_ROTA_BYTES: usize = 1028; + +/// A close mirror of rustls::OwnedTrustAnchor - but with extras +/// Note that the subject, spki & name_constraints fields are +/// DER encoded but WITHOUT the DER header. +#[derive(Archive, Serialize, Deserialize, Debug)] +pub struct RustlsOwnedTrustAnchor { + pub subject: Vec, + pub spki: Vec, + pub name_constraints: Option>, +} + +impl RustlsOwnedTrustAnchor { + pub fn from_subject_spki_name_constraints( + subject: impl Into>, + spki: impl Into>, + name_constraints: Option>>, + ) -> Self { + Self { + subject: subject.into(), + spki: spki.into(), + name_constraints: name_constraints.map(|x| x.into()), + } + } + + // decoded subject + pub fn subject(&self) -> String { + match add_der_header(Tag::Sequence, &self.subject) { + Ok(der) => match x509_parser::x509::X509Name::from_der(&der) { + Ok((_, decoded)) => decoded.to_string(), + Err(e) => { + log::warn!("{:?}", e); + "der decode failed".to_string() + } + }, + Err(e) => { + log::warn!("{:?}", e.into_inner().unwrap()); + "der decode failed".to_string() + } + } + } + + // decoded public key + // see https://docs.rs/x509-parser/0.15.0/src/x509_parser/x509.rs.html#260-274 + #[allow(unreachable_code)] + pub fn public_key(&self) -> Result, Error> { + todo!(); // fails decode + match add_der_header(Tag::Sequence, &self.spki) { + Ok(der) => match x509_parser::x509::SubjectPublicKeyInfo::from_der(&der) { + Ok((_, spki)) => Ok(spki.subject_public_key.data.to_vec()), + Err(e) => { + log::warn!("{:?}", e); + Err(Error::new(ErrorKind::InvalidData, "failed spki der decode")) + } + }, + Err(e) => { + log::warn!("{:?}", e.into_inner().unwrap()); + Err(Error::new(ErrorKind::InvalidData, "failed spki der header")) + } + } + } + + pub fn pddb_key(&self) -> String { + let subject = &self.subject(); + let begin = match subject.find("CN=") { + Some(begin) => Some(begin), + None => subject.find("OU="), + }; + + let mut pddb_key = match begin { + Some(mut begin) => { + begin += 3; + let end = match subject[begin..].find(",") { + Some(e) => begin + e, + None => subject.len(), + }; + &subject[begin..end] + } + None => { + log::warn!("Subject missing CN= & OU= :{}", &subject); + &subject + } + } + .to_string(); + + // grab a few arbitrary bytes from spki so pddb_key is deterministic & unique + let k = &self.spki; + pddb_key.push_str(&format!(" {:X}{:X}{:X}{:X}", k[6], k[7], k[8], k[9])); + + // mirror of pddb::KEY_NAME_LEN + // u64: vaddr/len/resvd, u32: flags, age = 95 + // would this be better as a pddb pub? + const KEY_NAME_LEN: usize = 127 - 8 - 8 - 8 - 4 - 4; + pddb_key[..min(pddb_key.len(), KEY_NAME_LEN - 1)].to_string() + } +} + +impl<'a> From<&X509Certificate<'a>> for RustlsOwnedTrustAnchor { + fn from(x509: &X509Certificate) -> Self { + // Remove the DER headers in keeping with rustls::OwnersTrustAnchor + RustlsOwnedTrustAnchor { + subject: match rm_der_header(x509.subject().as_raw()) { + Ok(naked_der) => naked_der, + Err(e) => { + log::warn!("{e}"); + b"der decode failed".to_vec() + } + }, + spki: match rm_der_header(x509.public_key().raw) { + Ok(naked_der) => naked_der, + Err(e) => { + log::warn!("{e}"); + b"der decode failed".to_vec() + } + }, + name_constraints: match x509.name_constraints() { + Ok(Some(_)) => { + log::warn!("Name Constraints ignored"); + None + } + Ok(None) => None, + Err(e) => { + log::warn!("{e}"); + None + } + }, + } + } +} + +/// Add a DER header to a DER encoded [u8] +fn add_der_header(tag: Tag, naked: &Vec) -> Result, Error> { + match Header::new(tag, naked.len()) { + Ok(header) => { + let mut buff: [u8; 32] = [0u8; 32]; + match header.encode_to_slice(&mut buff) { + Ok(der) => { + let der = [der, naked].concat(); + match x509_parser::x509::X509Name::from_der(&der) { + Ok((_, decoded)) => Ok(decoded.as_raw().to_vec()), + Err(_) => Err(Error::new(ErrorKind::InvalidData, "der parse failed: from")), + } + } + Err(_) => Err(Error::new( + ErrorKind::InvalidData, + "der parse failed: encode", + )), + } + } + Err(_) => Err(Error::new( + ErrorKind::InvalidData, + "der parse failed: header", + )), + } +} + +/// Remove a DER header from a DER encoded [u8] +fn rm_der_header(der: &[u8]) -> Result, Error> { + match der::SliceReader::new(der) { + Ok(reader) => match reader.peek_header() { + Ok(header) => match header.encoded_len() { + Ok(len) => match TryInto::::try_into(len) { + Ok(len) => Ok(der[len..].to_vec()), + Err(_) => Err(Error::new( + ErrorKind::InvalidData, + "der decode failed: into", + )), + }, + Err(_) => Err(Error::new( + ErrorKind::InvalidData, + "der decode failed: length", + )), + }, + Err(_) => Err(Error::new( + ErrorKind::InvalidData, + "der decode failed: header", + )), + }, + Err(_) => Err(Error::new( + ErrorKind::InvalidData, + "der decode failed: reader", + )), + } +} + +impl<'a> From<&webpki::TrustAnchor<'a>> for RustlsOwnedTrustAnchor { + fn from(ta: &webpki::TrustAnchor) -> Self { + Self::from_subject_spki_name_constraints(ta.subject, ta.spki, ta.name_constraints) + } +} + +impl Into for RustlsOwnedTrustAnchor { + fn into(self) -> rustls::OwnedTrustAnchor { + rustls::OwnedTrustAnchor::from_subject_spki_name_constraints( + self.subject, + self.spki, + self.name_constraints, + ) + } +} diff --git a/libs/tls/src/trusted.rs b/libs/tls/src/trusted.rs new file mode 100644 index 000000000..3b6c56b40 --- /dev/null +++ b/libs/tls/src/trusted.rs @@ -0,0 +1,57 @@ +use crate::get_cert; +use crate::{rota::RustlsOwnedTrustAnchor, TLS_TRUSTED_DICT}; +use rustls::RootCertStore; +use std::io::Error; +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_TRUSTED_DICT); + if !std::fs::metadata(&keypath).is_ok() { + log::info!("dict '{}' does NOT exist.. creating it", TLS_TRUSTED_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/services/gam/src/modal.rs b/services/gam/src/modal.rs index ede892684..c39ec6f89 100644 --- a/services/gam/src/modal.rs +++ b/services/gam/src/modal.rs @@ -69,10 +69,10 @@ pub enum ModalOpcode { // if changes are made here, also update MenuOpcode /// We use a new type for item names, so that it's easy to resize this as needed. #[derive(Debug, Copy, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] -pub struct ItemName(String::<64>); +pub struct ItemName(String::<128>); impl ItemName { pub fn new(name: &str) -> Self { - ItemName(String::<64>::from_str(name)) + ItemName(String::<128>::from_str(name)) } pub fn as_str(&self) -> &str { self.0.as_str().expect("couldn't convert item into string") diff --git a/services/gam/src/modal/checkboxes.rs b/services/gam/src/modal/checkboxes.rs index 8a51416b9..61262ae8d 100644 --- a/services/gam/src/modal/checkboxes.rs +++ b/services/gam/src/modal/checkboxes.rs @@ -45,8 +45,15 @@ impl CheckBoxes { impl ActionApi for CheckBoxes { fn set_action_opcode(&mut self, op: u32) {self.action_opcode = op} fn height(&self, glyph_height: i16, margin: i16, _modal: &Modal) -> i16 { - // total items, then +1 for the "Okay" message - (self.items.len() as i16 + 1) * glyph_height + margin * 2 + 5 // some slop needed because of the prompt character + // account for hard line-breaks in items + let line_break_count = self + .items + .iter() + .fold(0, |acc, item| acc + item.as_str().chars().filter(|c| *c == '\n').count()); + // total items + line-breaks +1 blank line +1 "Okay" message + let line_count: i16 = (self.items.len() + line_break_count + 1 + 1) as i16; + + line_count * glyph_height + 2 * margin } fn redraw(&self, at_height: i16, modal: &Modal) { // prime a textview with the correct general style parameters @@ -68,9 +75,17 @@ impl ActionApi for CheckBoxes { let emoji_slop = 2; // tweaked for a non-emoji glyph let mut cur_line = 0; + let mut cur_y = at_height; + let mut cur_line_height: i16; let mut do_okay = true; for item in self.items.iter() { - let cur_y = at_height + cur_line * modal.line_height; + // account for hard line-breaks + let item_line_count: i16 = 1 + item + .as_str() + .chars() + .filter(|c| *c == '\n') + .count() as i16; + cur_line_height = modal.line_height * item_line_count + item_line_count; // extra pixel between lines if cur_line == self.select_index { #[cfg(feature="tts")] { @@ -80,7 +95,8 @@ impl ActionApi for CheckBoxes { tv.text.clear(); tv.bounds_computed = None; tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( - Point::new(cursor_x, cur_y - emoji_slop), Point::new(cursor_x + 36, cur_y - emoji_slop + 36) + Point::new(cursor_x, cur_y - emoji_slop), + Point::new(cursor_x + 36, cur_y - emoji_slop + 36), )); write!(tv, "\u{25B6}").unwrap(); // right arrow modal.gam.post_textview(&mut tv).expect("couldn't post tv"); @@ -91,7 +107,8 @@ impl ActionApi for CheckBoxes { tv.text.clear(); tv.bounds_computed = None; tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( - Point::new(select_x, cur_y - emoji_slop), Point::new(select_x + 36, cur_y + modal.line_height) + Point::new(select_x, cur_y - emoji_slop), + Point::new(select_x + 36, cur_y + cur_line_height), )); write!(tv, "\u{d7}").unwrap(); // multiplication sign modal.gam.post_textview(&mut tv).expect("couldn't post tv"); @@ -100,15 +117,17 @@ impl ActionApi for CheckBoxes { tv.text.clear(); tv.bounds_computed = None; tv.bounds_hint = TextBounds::BoundingBox(Rectangle::new( - Point::new(text_x, cur_y), Point::new(modal.canvas_width - modal.margin, cur_y + modal.line_height) + Point::new(text_x, cur_y), + Point::new(modal.canvas_width - modal.margin, cur_y + cur_line_height), )); write!(tv, "{}", item.as_str()).unwrap(); modal.gam.post_textview(&mut tv).expect("couldn't post tv"); cur_line += 1; + cur_y += cur_line_height; } - cur_line += 1; - let cur_y = at_height + cur_line * modal.line_height; + // blank line above OK + cur_y += modal.line_height; if do_okay { tv.text.clear(); tv.bounds_computed = None; diff --git a/services/modals/src/tests.rs b/services/modals/src/tests.rs index a4b53cfc2..7f4b65b51 100644 --- a/services/modals/src/tests.rs +++ b/services/modals/src/tests.rs @@ -7,7 +7,7 @@ use xous_names::XousNames; const RADIO_TEST: [&'static str; 4] = ["zebra", "cow", "horse", "cat"]; -const CHECKBOX_TEST: [&'static str; 5] = ["happy", "😃", "安", "peaceful", "...something else!"]; +const CHECKBOX_TEST: [&'static str; 5] = ["happy", "😃", "安", "peace &\n tranquility", "Once apon a time, in a land far far away, there was a"]; /// This is an integration test of the Modals crate. It creates two competing threads /// that both try to throw up dialog boxes at the same time. Normally you *don't* want diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index 6f5e36c89..53a7c0e0b 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -75,12 +75,14 @@ tracing = {version = "0.1.35", optional = true} tracing-subscriber = {version = "0.3.11", optional = true} # for rustls testing -rustls = {version = "0.20.6", optional = true} # note requirement for patch to xous-ring in workspace Cargo.toml -# rustls = {path="../../../rustls/rustls"} -webpki-roots = {version = "0.22.4", optional = true} -ring = {version = "0.16.20", optional = true} # for testing ring math functions # note requirement for patch to xous-ring in workspace Cargo.toml -tungstenite = {version = "0.17.3", optional = true, features = ["rustls-tls-webpki-roots"]} +tls = {path = "../../libs/tls", optional = true} url = {version = "2.2.2", optional = true} +# for testing ring math functions +# note requirement for patch to xous-ring in workspace Cargo.toml +ring = {version = "0.16.20", optional = true} + +# for websocket testing +tungstenite = {version = "0.17.3", optional = true, features = ["rustls-tls-webpki-roots"]} # for performance testing perflib = {path="../../tools/perflib", optional = true} @@ -124,7 +126,9 @@ dbg-ecupdate = [] # for issuing manual commands to initiate an EC update (vs UX) test-rekey = [] no-codec = [] nettest = [] # batch network tests -tls = ["rustls", "webpki-roots", "ring", "tungstenite", "url"] +tls = ["dep:tls", "ring"] +rootCA = ["tls/rootCA"] +websocket = ["tls", "tungstenite", "url"] shellperf = ["ring", "perflib", "xous/v2p", "random-pick"] # this also needs the project-wide "perfcounter" feature to be selected. shellchat is the exclusive manager of the performance counter, do not use with e.g. "vaultperf" feature extra-tests = [] mass-storage = [] diff --git a/services/shellchat/src/cmds/net_cmd.rs b/services/shellchat/src/cmds/net_cmd.rs index fd304bc77..c2e4cda2b 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -1,35 +1,36 @@ -use crate::{ShellCmdApi, CommonEnv}; +use crate::{CommonEnv, ShellCmdApi}; use com::api::NET_MTU; -use xous_ipc::String; -#[cfg(any(feature="precursor", feature="renode"))] -use net::XousServerId; use net::NetPingCallback; -use xous::MessageEnvelope; use num_traits::*; -use std::net::{IpAddr, TcpStream, TcpListener}; -use std::io::Write; use std::io::Read; -use std::sync::{Arc, Mutex}; -use std::time::{Duration, Instant}; -use std::thread; +use std::io::Write; +use std::net::{IpAddr, TcpListener, TcpStream}; use std::sync::mpsc; -#[cfg(feature="ditherpunk")] -use std::str::FromStr; -#[cfg(feature="ditherpunk")] -use gam::DecodePng; -#[cfg(feature="tls")] -use std::convert::TryInto; -#[cfg(feature="tls")] -use tungstenite::{WebSocket, stream::MaybeTlsStream}; -#[cfg(feature="shellperf")] +use std::sync::{Arc, Mutex}; +use std::thread; +use std::time::{Duration, Instant}; +use xous::MessageEnvelope; +use xous_ipc::String; + +#[cfg(feature = "ditherpunk")] +use {gam::DecodePng, std::str::FromStr}; + +#[cfg(any(feature = "precursor", feature = "renode"))] +use net::XousServerId; + +#[cfg(feature = "shellperf")] use perflib::*; + +#[cfg(feature = "websocket")] +use tungstenite::{stream::MaybeTlsStream, WebSocket}; + pub struct NetCmd { callback_id: Option, callback_conn: u32, dns: dns::Dns, #[cfg(any(feature="precursor", feature="renode"))] ping: Option, - #[cfg(feature="tls")] + #[cfg(feature = "websocket")] ws: Option>>, #[cfg(feature="shellperf")] perfbuf: xous::MemoryRange, @@ -50,7 +51,7 @@ impl NetCmd { dns: dns::Dns::new(&xns).unwrap(), #[cfg(any(feature="precursor", feature="renode"))] ping: None, - #[cfg(feature="tls")] + #[cfg(feature = "websocket")] ws: None, #[cfg(feature="shellperf")] perfbuf, @@ -411,7 +412,7 @@ impl<'a> ShellCmdApi<'a> for NetCmd { ring::xous_test::p256_elem_add_test(); log::set_max_level(log::LevelFilter::Info); } - #[cfg(feature="tls")] + #[cfg(feature = "websocket")] "ws" => { if self.ws.is_none() { let (socket, response) = @@ -451,64 +452,16 @@ impl<'a> ShellCmdApi<'a> for NetCmd { write!(ret, "\nWeb socket session closed.").ok(); } } - #[cfg(feature="tls")] + // net tls + // mozilla trust all Root CA's in webpki-roots + // probe save host CA'a if trusted + // test make tls connection to host + // trusted list trusted CA certificates + #[cfg(feature = "tls")] "tls" => { - log::set_max_level(log::LevelFilter::Info); - log::info!("starting TLS run"); - let mut root_store = rustls::RootCertStore::empty(); - log::info!("create root store"); - root_store.add_server_trust_anchors( - webpki_roots::TLS_SERVER_ROOTS - .0 - .iter() - .map(|ta| { - rustls::OwnedTrustAnchor::from_subject_spki_name_constraints( - ta.subject, - ta.spki, - ta.name_constraints, - ) - }) - ); - log::info!("build TLS client config"); - let config = rustls::ClientConfig::builder() - .with_safe_defaults() - .with_root_certificates(root_store) - .with_no_client_auth(); - - log::info!("point TLS to bunniefoo.com"); - let server_name = "bunniefoo.com".try_into().unwrap(); - let mut conn = rustls::ClientConnection::new(Arc::new(config), server_name).unwrap(); - - log::info!("connect TCPstream to bunniefoo.com"); - let mut sock = TcpStream::connect("bunniefoo.com:443").unwrap(); - let mut tls = rustls::Stream::new(&mut conn, &mut sock); - log::info!("create http headers and write to server"); - tls.write_all( - concat!( - "GET / HTTP/1.1\r\n", - "Host: bunniefoo.com\r\n", - "Connection: close\r\n", - "Accept-Encoding: identity\r\n", - "\r\n" - ) - .as_bytes(), - ) - .unwrap(); - log::info!("readout cipher suite"); - let ciphersuite = tls - .conn - .negotiated_cipher_suite() - .unwrap(); - log::info!( - "Current ciphersuite: {:?}", - ciphersuite.suite() - ); - let mut plaintext = Vec::new(); - log::info!("read TLS response"); - tls.read_to_end(&mut plaintext).unwrap(); - log::info!("len: {}", plaintext.len()); - log::info!("{}", std::str::from_utf8(&plaintext).unwrap_or("utf-error")); - log::set_max_level(log::LevelFilter::Info); + if let Ok(Some(r)) = tls::cmd::shellchat(&mut tokens) { + write!(ret, "{r}").unwrap(); + } } // note: to use the `shellperf` option, you need to load a version of the SOC that has the performance counters built in. // this can be generated using the command `python3 .\betrusted_soc.py -e .\dummy.nky --perfcounter` in the betrusted-soc repo. @@ -933,7 +886,7 @@ fn heap_usage() -> usize { } } -#[cfg(feature ="tls")] +#[cfg(feature = "websocket")] fn join_tokens<'a>(buf: &mut String<1024>, tokens: impl Iterator) { use core::fmt::Write; for (i, tok) in tokens.enumerate() {