From 2944e34ea69f2ace6425b04dbdbfb0ba86437ad5 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 20 Jun 2023 20:38:12 +1000 Subject: [PATCH 01/46] implement save/get rustls::OwnedTrustAnchor to PDDB --- libs/tls/Cargo.toml | 24 +++++++++++++ libs/tls/src/lib.rs | 86 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 libs/tls/Cargo.toml create mode 100644 libs/tls/src/lib.rs diff --git a/libs/tls/Cargo.toml b/libs/tls/Cargo.toml new file mode 100644 index 000000000..39a939290 --- /dev/null +++ b/libs/tls/Cargo.toml @@ -0,0 +1,24 @@ +[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" } + +rkyv = "0.4.3" +rustls = "0.21.2" +x509-parser = "0.15.0" diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs new file mode 100644 index 000000000..2a4c444a2 --- /dev/null +++ b/libs/tls/src/lib.rs @@ -0,0 +1,86 @@ +use modals::Modals; +use rkyv::{ + de::deserializers::AllocDeserializer, + ser::{serializers::WriteSerializer, Serializer}, + AlignedVec, Archive, Deserialize, Serialize, +}; +use rustls::Certificate; +use std::convert::TryFrom; +use std::fs::File; +use std::io::{Error, ErrorKind, Read, Write}; +use std::path::PathBuf; +use xous_names::XousNames; + +/// PDDB Dict for tls trusted certificates keys +const TLS_CERT_DICT: &str = "tls/cert"; +const CURRENT_VERSION_KEY: &str = "__version"; + +#[derive(Archive, Serialize, Deserialize, Debug)] +pub struct RustlsOwnedTrustAnchor { + subject: Vec, + spki: Vec, + name_constraints: Option>, +} + + +// saves a tls trust-anchor to the pddb +pub fn save_cert(key: &str, ta: &RustlsOwnedTrustAnchor) -> Result<(), Error> { + if key.starts_with("__") { + Err(Error::new( + ErrorKind::PermissionDenied, + "may not set a variable beginning with __ ", + )) + } else { + log::trace!("set '{}' = '{:?}'", key, ta); + 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)?; + } + keypath.push(key); + + // serialize the trust-anchor + let mut serializer = WriteSerializer::new(AlignedVec::new()); + let pos: u16 = u16::try_from(serializer.serialize_value(ta).unwrap()).expect("data > u16"); + + // this next bit of black-magic bolts pos onto buff to enable deserialization + let mut buf = serializer.into_inner(); + let pos_bytes = pos.to_be_bytes(); + buf.push(pos_bytes[0]); + buf.push(pos_bytes[1]); + + // save key & trust-anchor to pddb + File::create(keypath)?.write_all(&buf[..])?; + Ok(()) + } +} + +// retrieves a tls trust-anchor from the pddb +pub fn get_cert(key: &str) -> Result, Error> { + 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)?; + } + + keypath.push(key); + if let Ok(mut file) = File::open(keypath) { + // read the key & serialized trust-anchor from the pddb + let mut bytes = Vec::::new(); + let len = file.read_to_end(&mut bytes)?; + + // black unmagic to unbold pos from the buffer end + let pos: u16 = u16::from_be_bytes([bytes[len - 2], bytes[len - 1]]); + + // deserialize the trust-anchor + let archive = unsafe { rkyv::archived_value::(&bytes, pos.into()) }; + let ta = archive.deserialize(&mut AllocDeserializer {}).ok(); + + log::trace!("get '{}' = '{:?}'", key, ta); + return Ok(ta); + } else { + return Ok(None); + } +} From 8d12e3d8d3294fb07be0882901ece872a92da35c Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 20 Jun 2023 20:42:26 +1000 Subject: [PATCH 02/46] implement tls check_trust presents a modal to the user to select trusted tls certificates and saves the selected certificates to the pddb --- libs/tls/src/lib.rs | 71 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 2a4c444a2..243bc71fd 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -9,6 +9,7 @@ use std::convert::TryFrom; use std::fs::File; use std::io::{Error, ErrorKind, Read, Write}; use std::path::PathBuf; +use x509_parser::prelude::{FromDer, X509Certificate}; use xous_names::XousNames; /// PDDB Dict for tls trusted certificates keys @@ -22,6 +23,76 @@ pub struct RustlsOwnedTrustAnchor { name_constraints: Option>, } +impl<'a> From<&X509Certificate<'a>> for RustlsOwnedTrustAnchor { + fn from(x509: &X509Certificate) -> Self { + RustlsOwnedTrustAnchor { + subject: x509.subject().as_raw().to_vec(), + spki: x509.public_key().raw.to_vec(), + name_constraints: None::>, + // may have to pass value thru from certificates parameter + // name_constraints: match x509.name_constraints() { + // Ok(Some(nc)) => Some(nc.value), + // Ok(None) => None, + // Err(e) => { + // log::warn!("failed to extract x509 name_constraints: {}", e); + // None + // } + // }, + } + } +} + +// presents a modal to the user to select trusted tls certificates +// and saves the selected certificates to the pddb +// returns false if no certificates are trusted - and true otherwise +pub fn check_trust(certificates: &[Certificate]) -> bool { + let xns = XousNames::new().unwrap(); + let modals = Modals::new(&xns).unwrap(); + + let certificates: Vec = certificates + .iter() + .map(|cert| X509Certificate::from_der(cert.as_ref())) + .filter(|result| result.is_ok()) + .map(|result| result.unwrap().1) + .filter(|x509| x509.is_ca()) + .collect(); + + let chain: Vec = certificates + .iter() + .map(|x509| format!("{}", &x509.tbs_certificate.issuer())) + .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("Do you trust any of these certificate authorities?") { + Ok(trusted) => { + trusted + .iter() + .for_each(|cert| log::info!("trusts {}", cert)); + modals + .get_check_index() + .unwrap() + .iter() + .map(|i| &certificates[*i]) + .map(|x509| { + ( + x509.raw_serial_as_string(), + RustlsOwnedTrustAnchor::from(x509), + ) + }) + .for_each(|(key, val)| { + save_cert(&key, &val).expect("failed to save trusted cert"); + }); + trusted.len() > 0 + } + _ => { + log::error!("get_checkbox failed"); + false + } + } +} // saves a tls trust-anchor to the pddb pub fn save_cert(key: &str, ta: &RustlsOwnedTrustAnchor) -> Result<(), Error> { From e6ed9770655e9a06896b4ddc0b484666b31d93f0 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 20 Jun 2023 20:48:58 +1000 Subject: [PATCH 03/46] demote shellchat "net tls" to "net tls test" --- services/shellchat/src/cmds/net_cmd.rs | 109 +++++++++++++++---------- 1 file changed, 64 insertions(+), 45 deletions(-) diff --git a/services/shellchat/src/cmds/net_cmd.rs b/services/shellchat/src/cmds/net_cmd.rs index fd304bc77..6a4cb1faf 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -451,10 +451,10 @@ impl<'a> ShellCmdApi<'a> for NetCmd { write!(ret, "\nWeb socket session closed.").ok(); } } - #[cfg(feature="tls")] + #[cfg(feature = "tls")] "tls" => { - log::set_max_level(log::LevelFilter::Info); - log::info!("starting TLS run"); + if let Some(tls_cmd) = tokens.next() { + match tls_cmd { let mut root_store = rustls::RootCertStore::empty(); log::info!("create root store"); root_store.add_server_trust_anchors( @@ -466,49 +466,68 @@ impl<'a> ShellCmdApi<'a> for NetCmd { ta.subject, ta.spki, ta.name_constraints, + "test" => { + 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(), ) - }) - ); - 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); + .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); + } + _ => { + write!(ret, "net commands: test").ok(); + } + } + } } // 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. From a874ba307d837ae17392f2005e06928131b46e72 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 20 Jun 2023 20:52:19 +1000 Subject: [PATCH 04/46] implement shellchat "net tls probe" --- services/shellchat/Cargo.toml | 3 +- services/shellchat/src/cmds/net_cmd.rs | 60 ++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index 6f5e36c89..81f0aa91d 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -75,7 +75,8 @@ 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 +tls = {path="../../libs/tls"} +rustls = {version = "0.21.2", 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 diff --git a/services/shellchat/src/cmds/net_cmd.rs b/services/shellchat/src/cmds/net_cmd.rs index 6a4cb1faf..f6bc0f4e3 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -8,6 +8,8 @@ use xous::MessageEnvelope; use num_traits::*; use std::net::{IpAddr, TcpStream, TcpListener}; use std::io::Write; +#[cfg(feature = "tls")] +use rustls::{Certificate, OwnedTrustAnchor}; use std::io::Read; use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; @@ -466,6 +468,64 @@ impl<'a> ShellCmdApi<'a> for NetCmd { ta.subject, ta.spki, ta.name_constraints, + "probe" => { + log::set_max_level(log::LevelFilter::Info); + log::info!("starting TLS probe"); + let mut root_store = rustls::RootCertStore::empty(); + 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, + ) + }) + ); + let config = rustls::ClientConfig::builder() + .with_safe_defaults() + .with_root_certificates(root_store) + .with_no_client_auth(); + let target = match tokens.next() { + Some(target) => target, + None => "bunnyfoo.com", + }; + let server_name = target.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); + let mut sock = match TcpStream::connect(url) { + Ok(mut sock) => { + 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: keep-alive\r\nAccept-Encoding: identity\r\n\r\n", target); + match tls.write_all(msg.as_bytes()) { + Ok(()) => (), + Err(e) => log::warn!( + "failed to write to tls connection {}", + e + ), + }; + + log::info!("close tls connection"); + tls.conn.send_close_notify(); + + match tls.conn.peer_certificates() { + Some(certificates) => tls::check_trust(certificates), + None => false, + }; + } + Err(e) => { + log::warn!("failed to establish tls connection {}", e) + } + }; + + log::set_max_level(log::LevelFilter::Info); + } "test" => { log::set_max_level(log::LevelFilter::Info); log::info!("starting TLS run"); From 6bbf5d6ef38afc48d87196d8e4db4dba391d54a8 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 20 Jun 2023 22:39:15 +1000 Subject: [PATCH 05/46] add libs/tls in Cargo.toml [members] --- Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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" From 874086c413742bbaf9c138d870fca8691109735f Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Fri, 23 Jun 2023 04:26:10 +1000 Subject: [PATCH 06/46] extend checkboxes modal to handle explicit newline in item --- services/gam/src/modal/checkboxes.rs | 35 +++++++++++++++++++++------- services/modals/src/tests.rs | 2 +- 2 files changed, 28 insertions(+), 9 deletions(-) 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 From 80ff0cb52a51f6a277042fc5d28a6381063bbb23 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Fri, 23 Jun 2023 07:26:12 +1000 Subject: [PATCH 07/46] increase modal ItemName length to 128 --- services/gam/src/modal.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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") From 65f92453a2dab97fc5fa9b58a6f03459c5f6374b Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Sat, 24 Jun 2023 14:57:16 +1000 Subject: [PATCH 08/46] implement tls RustlsOwnedTrustAnchor to rustls::OwnedTrustAnchor --- libs/tls/src/lib.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 243bc71fd..4a34be8e2 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -23,6 +23,15 @@ pub struct RustlsOwnedTrustAnchor { name_constraints: Option>, } +impl RustlsOwnedTrustAnchor { + pub fn to_owned_trust_anchor(self) -> rustls::OwnedTrustAnchor { + rustls::OwnedTrustAnchor::from_subject_spki_name_constraints( + self.subject, + self.spki, + self.name_constraints, + ) + } +} impl<'a> From<&X509Certificate<'a>> for RustlsOwnedTrustAnchor { fn from(x509: &X509Certificate) -> Self { RustlsOwnedTrustAnchor { From 0d1e89e560a543249b4dae2dd6b90fdb29c84207 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Sat, 24 Jun 2023 14:59:47 +1000 Subject: [PATCH 09/46] implement tls Trusted iterator --- libs/tls/src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 4a34be8e2..0410b57e0 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -51,6 +51,48 @@ impl<'a> From<&X509Certificate<'a>> 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, + } + } +} // presents a modal to the user to select trusted tls certificates // and saves the selected certificates to the pddb // returns false if no certificates are trusted - and true otherwise From 472e4407b8f9d96b92be4ee423ee9d4c35c7568a Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Sat, 24 Jun 2023 15:02:48 +1000 Subject: [PATCH 10/46] display tls certificate subject & serial in check_trust modal --- libs/tls/src/lib.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 0410b57e0..61c06bebd 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -110,10 +110,17 @@ pub fn check_trust(certificates: &[Certificate]) -> bool { let chain: Vec = certificates .iter() - .map(|x509| format!("{}", &x509.tbs_certificate.issuer())) + .map(|x509| { + let subject = x509.subject(); + format!( + "{}\n{}\n{}", + &subject, + &x509.raw_serial_as_string()[0..24], + &x509.raw_serial_as_string()[24..], + ) + }) .collect(); let chain: Vec<&str> = chain.iter().map(AsRef::as_ref).collect(); - modals .add_list(chain) .expect("couldn't build checkbox list"); From 5feaac2bae725bcdd0f8cfba1fcb32e24c15452d Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Sat, 24 Jun 2023 15:37:10 +1000 Subject: [PATCH 11/46] simplify shellchat net tls probe --- services/shellchat/src/cmds/net_cmd.rs | 27 ++++++++++++-------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/services/shellchat/src/cmds/net_cmd.rs b/services/shellchat/src/cmds/net_cmd.rs index f6bc0f4e3..14b2af4a2 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -500,27 +500,24 @@ impl<'a> ShellCmdApi<'a> for NetCmd { let url = format!("{}:443", target); let mut sock = match TcpStream::connect(url) { Ok(mut sock) => { - 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: keep-alive\r\nAccept-Encoding: identity\r\n\r\n", target); - match tls.write_all(msg.as_bytes()) { - Ok(()) => (), - Err(e) => log::warn!( - "failed to write to tls connection {}", - e - ), - }; + match conn.complete_io(&mut sock){ + Ok(_) => log::info!("handshake complete"), + Err(e) => { + write!(ret, "{e}").ok(); + log::warn!("{e}"); + } + } + conn.send_close_notify(); - log::info!("close tls connection"); - tls.conn.send_close_notify(); - - match tls.conn.peer_certificates() { + match conn.peer_certificates() { Some(certificates) => tls::check_trust(certificates), None => false, }; + } Err(e) => { - log::warn!("failed to establish tls connection {}", e) + write!(ret, "{e}").ok(); + log::warn!("{e}") } }; From fcc4cde41b0927cbc8ba820a6dc726de609070f4 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Sat, 24 Jun 2023 18:56:55 +1000 Subject: [PATCH 12/46] add CA icon to tls check_trust modal --- libs/tls/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 61c06bebd..3bd08d188 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -113,7 +113,8 @@ pub fn check_trust(certificates: &[Certificate]) -> bool { .map(|x509| { let subject = x509.subject(); format!( - "{}\n{}\n{}", + "{}{}\n{}\n{}", + if x509.is_ca() { "🏛 " } else { "" }, &subject, &x509.raw_serial_as_string()[0..24], &x509.raw_serial_as_string()[24..], From 4ea0c8457de8f855723af76546b6ecb7e54661f7 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Sun, 25 Jun 2023 12:11:30 +1000 Subject: [PATCH 13/46] implement tls::danger::StifledCertificateVerification StifledCertificateVerification::verify_server_cert() will verify the certificate with the default rustls WebPkiVerifier, BUT specifically override a `CertificateError::UnknownIssuer` and return ServerCertVerified::assertion() This allows us to use rustls to open a tls connection to a host with an untrusted certificate chain. And subsequently get to see the certificate chain, and possibly choose to trust the chain, or links in it. --- libs/tls/src/danger.rs | 37 ++++++++++++++++++++++++++ libs/tls/src/lib.rs | 4 +++ services/shellchat/src/cmds/net_cmd.rs | 18 ++++--------- 3 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 libs/tls/src/danger.rs 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 index 3bd08d188..51c22b970 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -1,3 +1,5 @@ +pub mod danger; + use modals::Modals; use rkyv::{ de::deserializers::AllocDeserializer, @@ -93,6 +95,8 @@ impl Iterator for Trusted { } } } + + // presents a modal to the user to select trusted tls certificates // and saves the selected certificates to the pddb // returns false if no certificates are trusted - and true otherwise diff --git a/services/shellchat/src/cmds/net_cmd.rs b/services/shellchat/src/cmds/net_cmd.rs index 14b2af4a2..b5f4346d7 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -471,22 +471,14 @@ impl<'a> ShellCmdApi<'a> for NetCmd { "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 mut root_store = rustls::RootCertStore::empty(); - 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, - ) - }) - ); + // 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_root_certificates(root_store) + .with_custom_certificate_verifier(stifled_verifier) .with_no_client_auth(); let target = match tokens.next() { Some(target) => target, From 70b41cbf78addbaaef62e5ee3d3ce3a21de22ca8 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Sun, 25 Jun 2023 16:26:06 +1000 Subject: [PATCH 14/46] implement Into for RustlsOwnedTrustAnchor --- libs/tls/src/lib.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 51c22b970..d1cc5e5de 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -26,12 +26,12 @@ pub struct RustlsOwnedTrustAnchor { } impl RustlsOwnedTrustAnchor { - pub fn to_owned_trust_anchor(self) -> rustls::OwnedTrustAnchor { - rustls::OwnedTrustAnchor::from_subject_spki_name_constraints( - self.subject, - self.spki, - self.name_constraints, - ) + + + + + + } } impl<'a> From<&X509Certificate<'a>> for RustlsOwnedTrustAnchor { @@ -53,6 +53,15 @@ impl<'a> From<&X509Certificate<'a>> for RustlsOwnedTrustAnchor { } } +impl Into for RustlsOwnedTrustAnchor { + fn into(self) -> rustls::OwnedTrustAnchor { + rustls::OwnedTrustAnchor::from_subject_spki_name_constraints( + self.subject, + self.spki, + self.name_constraints, + ) + } +} pub struct Trusted { trusted: std::fs::ReadDir, From 21961ad75679f00ee04f9529c1fdfb39f842ad23 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Sun, 25 Jun 2023 16:31:17 +1000 Subject: [PATCH 15/46] implement Into for Trusted --- libs/tls/src/lib.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index d1cc5e5de..3736c99b4 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -6,8 +6,9 @@ use rkyv::{ ser::{serializers::WriteSerializer, Serializer}, AlignedVec, Archive, Deserialize, Serialize, }; -use rustls::Certificate; +use rustls::{Certificate, DistinguishedName, RootCertStore}; use std::convert::TryFrom; +use std::convert::{Into, TryFrom}; use std::fs::File; use std::io::{Error, ErrorKind, Read, Write}; use std::path::PathBuf; @@ -105,6 +106,15 @@ impl Iterator for Trusted { } } +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 + } +} // presents a modal to the user to select trusted tls certificates // and saves the selected certificates to the pddb From a741e7988ba1e1b136a8f61fa7d2221c19405800 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Mon, 26 Jun 2023 08:12:11 +1000 Subject: [PATCH 16/46] tidy-up tls cargo & use --- libs/tls/Cargo.toml | 2 +- libs/tls/src/lib.rs | 2 +- services/shellchat/Cargo.toml | 15 +++++--- services/shellchat/src/cmds/net_cmd.rs | 48 +++++++++++++++----------- 4 files changed, 40 insertions(+), 27 deletions(-) diff --git a/libs/tls/Cargo.toml b/libs/tls/Cargo.toml index 39a939290..2cd2ddb98 100644 --- a/libs/tls/Cargo.toml +++ b/libs/tls/Cargo.toml @@ -20,5 +20,5 @@ net = { path = "../../services/net" } pddb = { path = "../../services/pddb" } rkyv = "0.4.3" -rustls = "0.21.2" +rustls = { version = "0.21.2", features = ["dangerous_configuration"]} x509-parser = "0.15.0" diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 3736c99b4..58bf90b66 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -7,7 +7,7 @@ use rkyv::{ AlignedVec, Archive, Deserialize, Serialize, }; use rustls::{Certificate, DistinguishedName, RootCertStore}; -use std::convert::TryFrom; +use std::cmp::min; use std::convert::{Into, TryFrom}; use std::fs::File; use std::io::{Error, ErrorKind, Read, Write}; diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index 81f0aa91d..49da17b27 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -76,12 +76,19 @@ tracing-subscriber = {version = "0.3.11", optional = true} # for rustls testing tls = {path="../../libs/tls"} -rustls = {version = "0.21.2", optional = true} # note requirement for patch to xous-ring in workspace Cargo.toml -# rustls = {path="../../../rustls/rustls"} + +# note requirement for patch to xous-ring in workspace Cargo.toml +rustls = {version = "0.21.2", optional = true} 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"]} url = {version = "2.2.2", optional = true} +x509-parser = "0.15.0" + +# 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} diff --git a/services/shellchat/src/cmds/net_cmd.rs b/services/shellchat/src/cmds/net_cmd.rs index b5f4346d7..abd45f128 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -1,30 +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; -#[cfg(feature = "tls")] -use rustls::{Certificate, OwnedTrustAnchor}; +use rustls::internal::msgs::base::PayloadU16; +use rustls::internal::msgs::handshake::DistinguishedName; 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 = "tls")] +use { + rustls::{Certificate, OwnedTrustAnchor}, + std::convert::TryInto, + tls::{danger, Trusted}, + tungstenite::{stream::MaybeTlsStream, WebSocket}, +}; + pub struct NetCmd { callback_id: Option, callback_conn: u32, From 4d1411432f8fa3479346453a27e6ee74a32c009b Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Mon, 26 Jun 2023 11:11:45 +1000 Subject: [PATCH 17/46] mirror RustlsOwnedTrustAnchor::from_subject_spki_name_constraints() --- libs/tls/src/lib.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 58bf90b66..fdbc45197 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -21,13 +21,24 @@ const CURRENT_VERSION_KEY: &str = "__version"; #[derive(Archive, Serialize, Deserialize, Debug)] pub struct RustlsOwnedTrustAnchor { - subject: Vec, - spki: Vec, - name_constraints: Option>, + 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()) + } +} From 42eb72b3bded29d7316922e8152a76da55a80e7e Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Mon, 26 Jun 2023 11:14:26 +1000 Subject: [PATCH 18/46] implement shellchat net tls mozilla save/trust all Root CA's in webpki-roots en-masse --- services/shellchat/src/cmds/net_cmd.rs | 38 ++++++++++++++++++-------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/services/shellchat/src/cmds/net_cmd.rs b/services/shellchat/src/cmds/net_cmd.rs index abd45f128..7b88668eb 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -463,17 +463,23 @@ impl<'a> ShellCmdApi<'a> for NetCmd { "tls" => { if let Some(tls_cmd) = tokens.next() { match tls_cmd { - 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, + // save/trust all Root CA's in webpki-roots en-masse + "mozilla" => { + let mut rotas: Vec = webpki_roots::TLS_SERVER_ROOTS + .0 + .iter() + .map(|ta| tls::RustlsOwnedTrustAnchor::from_subject_spki_name_constraints( + ta.subject, + ta.spki, + ta.name_constraints, + )) + .collect(); + let mut i = 0; + for rota in rotas { + tls::save_cert(format!("webpki-root-{i}").as_str(), &rota).unwrap(); + i += 1; + } + } "probe" => { log::set_max_level(log::LevelFilter::Info); log::info!("starting TLS probe"); @@ -579,7 +585,15 @@ impl<'a> ShellCmdApi<'a> for NetCmd { log::set_max_level(log::LevelFilter::Info); } _ => { - write!(ret, "net commands: test").ok(); + write!(ret, "net tls \n").ok(); + write!( + ret, + "\tmozilla\ttrust all Root CA's in webpki-roots\n" + ) + .ok(); + write!(ret, "\tprobe \tsave host CA'a if trusted\n").ok(); + write!(ret, "\ttest \tmake tls connection to host\n") + .ok(); } } } From be9f0fd29a1e47fdaec01caf344e897ac42e048a Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Mon, 26 Jun 2023 12:35:20 +1000 Subject: [PATCH 19/46] implement tls RustlsOwnedTrustAnchor::subject() --- libs/tls/src/lib.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index fdbc45197..6e567b3dc 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -6,13 +6,14 @@ use rkyv::{ ser::{serializers::WriteSerializer, Serializer}, AlignedVec, Archive, Deserialize, Serialize, }; -use rustls::{Certificate, DistinguishedName, RootCertStore}; +use rustls::{Certificate, RootCertStore}; use std::cmp::min; use std::convert::{Into, TryFrom}; use std::fs::File; use std::io::{Error, ErrorKind, Read, Write}; use std::path::PathBuf; use x509_parser::prelude::{FromDer, X509Certificate}; +use x509_parser::der_parser::der::Tag; use xous_names::XousNames; /// PDDB Dict for tls trusted certificates keys @@ -41,6 +42,17 @@ pub fn from_subject_spki_name_constraints( } + pub fn subject(&self) -> String { + let der = self.subject.clone(); + let len = min(der.len(), 127); + let mut der = der[..len].to_owned(); + der.insert(0, len as u8); + der.insert(0, Tag::Sequence.0 as u8); + match x509_parser::x509::X509Name::from_der(&der) { + Ok((_, decoded)) => decoded.to_string(), + Err(e) => format!("der parse failed: {e}").to_string(), + } + } From 33169f9df9066650315fa0883808e59bc70e2dd4 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Mon, 26 Jun 2023 12:38:12 +1000 Subject: [PATCH 20/46] implement shellchat net tls trusted list trusted Certificate Authority certificates --- services/shellchat/src/cmds/net_cmd.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/services/shellchat/src/cmds/net_cmd.rs b/services/shellchat/src/cmds/net_cmd.rs index 7b88668eb..0a1766b50 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -584,6 +584,17 @@ impl<'a> ShellCmdApi<'a> for NetCmd { ); log::set_max_level(log::LevelFilter::Info); } + // list trusted Certificate Authority certificates + "trusted" => { + log::set_max_level(log::LevelFilter::Info); + log::info!("starting TLS trusted listing"); + let trusted = + Trusted::new().expect("failed to initiate trusted iterator"); + for rota in trusted { + write!(ret, "🏛 {}\n", rota.subject()).ok(); + } + log::info!("finished TLS trusted listing"); + } _ => { write!(ret, "net tls \n").ok(); write!( @@ -594,6 +605,7 @@ impl<'a> ShellCmdApi<'a> for NetCmd { write!(ret, "\tprobe \tsave host CA'a if trusted\n").ok(); write!(ret, "\ttest \tmake tls connection to host\n") .ok(); + write!(ret, "\ttrusted\tlist trusted CA certificates\n").ok(); } } } From 6ef1e8d67c234669b59c071c0d8c1c43985be726 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Mon, 26 Jun 2023 15:25:46 +1000 Subject: [PATCH 21/46] show help when: shellchat net tls --- services/shellchat/src/cmds/net_cmd.rs | 175 +++++++++++++------------ 1 file changed, 90 insertions(+), 85 deletions(-) diff --git a/services/shellchat/src/cmds/net_cmd.rs b/services/shellchat/src/cmds/net_cmd.rs index 0a1766b50..db6cd39f8 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -461,73 +461,79 @@ impl<'a> ShellCmdApi<'a> for NetCmd { } #[cfg(feature = "tls")] "tls" => { - if let Some(tls_cmd) = tokens.next() { - match tls_cmd { - // save/trust all Root CA's in webpki-roots en-masse - "mozilla" => { - let mut rotas: Vec = webpki_roots::TLS_SERVER_ROOTS - .0 - .iter() - .map(|ta| tls::RustlsOwnedTrustAnchor::from_subject_spki_name_constraints( - ta.subject, - ta.spki, - ta.name_constraints, - )) - .collect(); - let mut i = 0; - for rota in rotas { - tls::save_cert(format!("webpki-root-{i}").as_str(), &rota).unwrap(); - i += 1; - } + match tokens.next() { + // save/trust all Root CA's in webpki-roots en-masse + Some("mozilla") => { + let mut rotas: Vec = webpki_roots::TLS_SERVER_ROOTS + .0 + .iter() + .map(|ta| tls::RustlsOwnedTrustAnchor::from_subject_spki_name_constraints( + ta.subject, + ta.spki, + ta.name_constraints, + )) + .collect(); + let mut i = 0; + for rota in rotas { + tls::save_cert(format!("webpki-root-{i}").as_str(), &rota).unwrap(); + i += 1; } - "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 mut 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(); - let mut conn = - rustls::ClientConnection::new(Arc::new(config), server_name) - .unwrap(); - log::info!("connect TCPstream to {}", target); - let url = format!("{}:443", target); - let mut sock = 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}"); - } + } + // 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 mut 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(); + let mut conn = + rustls::ClientConnection::new(Arc::new(config), server_name) + .unwrap(); + log::info!("connect TCPstream to {}", target); + let url = format!("{}:443", target); + let mut sock = 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) => tls::check_trust(certificates), - None => false, - }; - } - Err(e) => { - write!(ret, "{e}").ok(); - log::warn!("{e}") - } - }; + conn.send_close_notify(); - log::set_max_level(log::LevelFilter::Info); - } - "test" => { + match conn.peer_certificates() { + Some(certificates) => tls::check_trust(certificates), + None => false, + }; + } + 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"); let mut root_store = rustls::RootCertStore::empty(); @@ -583,30 +589,29 @@ impl<'a> ShellCmdApi<'a> for NetCmd { std::str::from_utf8(&plaintext).unwrap_or("utf-error") ); log::set_max_level(log::LevelFilter::Info); + } + // list trusted Certificate Authority certificates + Some("trusted") => { + log::set_max_level(log::LevelFilter::Info); + log::info!("starting TLS trusted listing"); + let trusted = + Trusted::new().expect("failed to initiate trusted iterator"); + for rota in trusted { + write!(ret, "🏛 {}\n", rota.subject()).ok(); } - // list trusted Certificate Authority certificates - "trusted" => { - log::set_max_level(log::LevelFilter::Info); - log::info!("starting TLS trusted listing"); - let trusted = - Trusted::new().expect("failed to initiate trusted iterator"); - for rota in trusted { - write!(ret, "🏛 {}\n", rota.subject()).ok(); - } - log::info!("finished TLS trusted listing"); - } - _ => { - write!(ret, "net tls \n").ok(); - write!( - ret, - "\tmozilla\ttrust all Root CA's in webpki-roots\n" - ) + log::info!("finished TLS trusted listing"); + } + _ | None => { + write!(ret, "net tls \n").ok(); + write!( + ret, + "\tmozilla\ttrust all Root CA's in webpki-roots\n" + ) + .ok(); + write!(ret, "\tprobe \tsave host CA'a if trusted\n").ok(); + write!(ret, "\ttest \tmake tls connection to host\n") .ok(); - write!(ret, "\tprobe \tsave host CA'a if trusted\n").ok(); - write!(ret, "\ttest \tmake tls connection to host\n") - .ok(); - write!(ret, "\ttrusted\tlist trusted CA certificates\n").ok(); - } + write!(ret, "\ttrusted\tlist trusted CA certificates\n").ok(); } } } From ab8d40189ae0006b5446d3e610d61e5328813fa0 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Mon, 26 Jun 2023 16:00:21 +1000 Subject: [PATCH 22/46] modify `shellchat net tls test` to use trusted CA certificates --- services/shellchat/src/cmds/net_cmd.rs | 103 ++++++++++++------------- 1 file changed, 50 insertions(+), 53 deletions(-) diff --git a/services/shellchat/src/cmds/net_cmd.rs b/services/shellchat/src/cmds/net_cmd.rs index db6cd39f8..45425b1a1 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -534,61 +534,58 @@ impl<'a> ShellCmdApi<'a> for NetCmd { } Some("test") => { - 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::set_max_level(log::LevelFilter::Info); + log::info!("starting TLS run"); + log::info!("build TLS client config"); + let trusted = Trusted::new().unwrap(); + let config = rustls::ClientConfig::builder() + .with_safe_defaults() + .with_root_certificates(trusted.into()) + .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!("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 {}", target); + let url = format!("{}:443", target); + let mut sock = TcpStream::connect(url).unwrap(); + 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!("GET on tls accepted"); + write!(ret, "GET on tls accepted\n").ok(); + } + Err(e) => { + log::warn!("failed to GET on tls connection: {e}"); + write!(ret, "{e}\n").ok(); + } + }; - 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); + let mut plaintext = Vec::new(); + log::info!("read TLS response"); + match tls.read_to_end(&mut plaintext) { + Ok(n) => { + log::info!("received {} bytes in response on tls", n); + write!(ret, "received {} bytes in response on tls\n", n).ok(); + } + Err(e) => { + log::warn!("failed to read tls response: {e}"); + write!(ret, "{e}\n").ok(); + } + }; + log::info!( + "{}", + std::str::from_utf8(&plaintext).unwrap_or("utf-error") + ); + + log::set_max_level(log::LevelFilter::Info); } // list trusted Certificate Authority certificates Some("trusted") => { From d6fe702278fa143a3421edc943e8eb8640236cd2 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Mon, 26 Jun 2023 19:32:57 +1000 Subject: [PATCH 23/46] improve tls error handling --- libs/tls/src/lib.rs | 10 +++- services/shellchat/src/cmds/net_cmd.rs | 75 +++++++++++++++----------- 2 files changed, 54 insertions(+), 31 deletions(-) diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 6e567b3dc..e0b13b2a0 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -188,7 +188,15 @@ pub fn check_trust(certificates: &[Certificate]) -> bool { ) }) .for_each(|(key, val)| { - save_cert(&key, &val).expect("failed to save trusted cert"); + save_cert(&key, &val).unwrap_or_else(|e| { + log::warn!("failed to save cert: {e}"); + modals + .show_notification( + format!("failed to save: {}", &val.subject()).as_str(), + None, + ) + .expect("modal failed"); + }); }); trusted.len() > 0 } diff --git a/services/shellchat/src/cmds/net_cmd.rs b/services/shellchat/src/cmds/net_cmd.rs index 45425b1a1..c95cf2635 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -502,7 +502,11 @@ impl<'a> ShellCmdApi<'a> for NetCmd { Some(target) => target, None => "bunnyfoo.com", }; - let server_name = target.try_into().unwrap(); + let server_name = target.try_into().unwrap_or_else(|e| { + log::warn!("failed to create sever_name from {target}: {e}"); + write!(ret, "failed to create sever_name from {target}"); + "bunnyfoo.com".try_into().unwrap() + }); let mut conn = rustls::ClientConnection::new(Arc::new(config), server_name) .unwrap(); @@ -547,44 +551,55 @@ impl<'a> ShellCmdApi<'a> for NetCmd { None => "bunnyfoo.com", }; log::info!("point TLS to {}", target); - let mut conn = - rustls::ClientConnection::new(Arc::new(config), target.try_into().unwrap()) - .unwrap(); + 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); - let mut sock = TcpStream::connect(url).unwrap(); - 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!("GET on tls accepted"); - write!(ret, "GET on tls accepted\n").ok(); + match TcpStream::connect(url) { + Ok(mut sock) => { + log::info!("tcp connected"); + write!(ret, "tcp connected\n").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, "tls accepted GET\n").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, "tls received {} bytes\n", n).ok(); + log::info!( + "{}", + std::str::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 GET on tls connection: {e}"); + log::warn!("failed to connect tcp: {e}"); write!(ret, "{e}\n").ok(); } }; - let mut plaintext = Vec::new(); - log::info!("read TLS response"); - match tls.read_to_end(&mut plaintext) { - Ok(n) => { - log::info!("received {} bytes in response on tls", n); - write!(ret, "received {} bytes in response on tls\n", n).ok(); - } - Err(e) => { - log::warn!("failed to read tls response: {e}"); - write!(ret, "{e}\n").ok(); - } - }; - log::info!( - "{}", - std::str::from_utf8(&plaintext).unwrap_or("utf-error") - ); - log::set_max_level(log::LevelFilter::Info); } // list trusted Certificate Authority certificates From aeeff17a6a98227fa8654f9c2c626e5d3975102f Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 27 Jun 2023 14:24:11 +1000 Subject: [PATCH 24/46] relocate `shellchat net tls` sub-commands to `libs/tls/src/cmd.rs` --- libs/tls/Cargo.toml | 6 +- libs/tls/src/lib.rs | 1 + services/shellchat/Cargo.toml | 8 +- services/shellchat/src/cmds/net_cmd.rs | 180 ++----------------------- 4 files changed, 15 insertions(+), 180 deletions(-) diff --git a/libs/tls/Cargo.toml b/libs/tls/Cargo.toml index 2cd2ddb98..a17c2fa31 100644 --- a/libs/tls/Cargo.toml +++ b/libs/tls/Cargo.toml @@ -20,5 +20,9 @@ net = { path = "../../services/net" } pddb = { path = "../../services/pddb" } rkyv = "0.4.3" -rustls = { version = "0.21.2", features = ["dangerous_configuration"]} + +# 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" } x509-parser = "0.15.0" diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index e0b13b2a0..3b12da43d 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -1,3 +1,4 @@ +pub mod cmd; pub mod danger; use modals::Modals; diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index 49da17b27..71148d00c 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -76,13 +76,7 @@ tracing-subscriber = {version = "0.3.11", optional = true} # for rustls testing tls = {path="../../libs/tls"} - -# note requirement for patch to xous-ring in workspace Cargo.toml -rustls = {version = "0.21.2", optional = true} -webpki-roots = {version = "0.22.4", optional = true} url = {version = "2.2.2", optional = true} -x509-parser = "0.15.0" - # for testing ring math functions # note requirement for patch to xous-ring in workspace Cargo.toml ring = {version = "0.16.20", optional = true} @@ -132,7 +126,7 @@ 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 = ["ring", "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 c95cf2635..af900950f 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -24,12 +24,7 @@ use net::XousServerId; use perflib::*; #[cfg(feature = "tls")] -use { - rustls::{Certificate, OwnedTrustAnchor}, - std::convert::TryInto, - tls::{danger, Trusted}, - tungstenite::{stream::MaybeTlsStream, WebSocket}, -}; +use tungstenite::{stream::MaybeTlsStream, WebSocket}; pub struct NetCmd { callback_id: Option, @@ -459,173 +454,14 @@ impl<'a> ShellCmdApi<'a> for NetCmd { write!(ret, "\nWeb socket session closed.").ok(); } } + // 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" => { - match tokens.next() { - // save/trust all Root CA's in webpki-roots en-masse - Some("mozilla") => { - let mut rotas: Vec = webpki_roots::TLS_SERVER_ROOTS - .0 - .iter() - .map(|ta| tls::RustlsOwnedTrustAnchor::from_subject_spki_name_constraints( - ta.subject, - ta.spki, - ta.name_constraints, - )) - .collect(); - let mut i = 0; - for rota in rotas { - tls::save_cert(format!("webpki-root-{i}").as_str(), &rota).unwrap(); - i += 1; - } - } - // 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 mut 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, "failed to create sever_name from {target}"); - "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); - let mut sock = 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) => tls::check_trust(certificates), - None => false, - }; - } - 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 trusted = Trusted::new().unwrap(); - let config = rustls::ClientConfig::builder() - .with_safe_defaults() - .with_root_certificates(trusted.into()) - .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, "tcp connected\n").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, "tls accepted GET\n").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, "tls received {} bytes\n", n).ok(); - log::info!( - "{}", - std::str::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); - } - // list trusted Certificate Authority certificates - Some("trusted") => { - log::set_max_level(log::LevelFilter::Info); - log::info!("starting TLS trusted listing"); - let trusted = - Trusted::new().expect("failed to initiate trusted iterator"); - for rota in trusted { - write!(ret, "🏛 {}\n", rota.subject()).ok(); - } - log::info!("finished TLS trusted listing"); - } - _ | None => { - write!(ret, "net tls \n").ok(); - write!( - ret, - "\tmozilla\ttrust all Root CA's in webpki-roots\n" - ) - .ok(); - write!(ret, "\tprobe \tsave host CA'a if trusted\n").ok(); - write!(ret, "\ttest \tmake tls connection to host\n") - .ok(); - write!(ret, "\ttrusted\tlist trusted CA certificates\n").ok(); - } - } + "tls" => 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. From b67c015abc4964d68b0e56a36a4b274f9322ca91 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 27 Jun 2023 14:40:30 +1000 Subject: [PATCH 25/46] 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 + } +} From 0e85ff2aac5fb2a2fdce9d43c29cb075e9058b8d Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 27 Jun 2023 18:54:16 +1000 Subject: [PATCH 26/46] refactor `tls::rota` --- libs/tls/src/lib.rs | 88 ++--------------------------------------- libs/tls/src/rota.rs | 75 +++++++++++++++++++++++++++++++++++ libs/tls/src/trusted.rs | 4 +- 3 files changed, 81 insertions(+), 86 deletions(-) create mode 100644 libs/tls/src/rota.rs diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 7de729951..6d6d92698 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -1,107 +1,27 @@ pub mod cmd; pub mod danger; +pub mod rota; pub mod trusted; +use crate::rota::RustlsOwnedTrustAnchor; use modals::Modals; use rkyv::{ de::deserializers::AllocDeserializer, ser::{serializers::WriteSerializer, Serializer}, - AlignedVec, Archive, Deserialize, Serialize, + AlignedVec, Deserialize }; -use rustls::{Certificate, RootCertStore}; -use std::cmp::min; +use rustls::Certificate; use std::convert::{Into, TryFrom}; use std::fs::File; use std::io::{Error, ErrorKind, Read, Write}; use std::path::PathBuf; use x509_parser::prelude::{FromDer, X509Certificate}; -use x509_parser::der_parser::der::Tag; use xous_names::XousNames; /// PDDB Dict for tls trusted certificates keys const TLS_CERT_DICT: &str = "tls/cert"; const CURRENT_VERSION_KEY: &str = "__version"; -#[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()) - } -} - - - pub fn subject(&self) -> String { - let der = self.subject.clone(); - let len = min(der.len(), 127); - let mut der = der[..len].to_owned(); - der.insert(0, len as u8); - der.insert(0, Tag::Sequence.0 as u8); - match x509_parser::x509::X509Name::from_der(&der) { - Ok((_, decoded)) => decoded.to_string(), - Err(e) => format!("der parse failed: {e}").to_string(), - } - } - - - - } -} -impl<'a> From<&X509Certificate<'a>> for RustlsOwnedTrustAnchor { - fn from(x509: &X509Certificate) -> Self { - RustlsOwnedTrustAnchor { - subject: x509.subject().as_raw().to_vec(), - spki: x509.public_key().raw.to_vec(), - name_constraints: None::>, - // may have to pass value thru from certificates parameter - // name_constraints: match x509.name_constraints() { - // Ok(Some(nc)) => Some(nc.value), - // Ok(None) => None, - // Err(e) => { - // log::warn!("failed to extract x509 name_constraints: {}", e); - // None - // } - // }, - } - } -} - -impl Into for RustlsOwnedTrustAnchor { - fn into(self) -> rustls::OwnedTrustAnchor { - rustls::OwnedTrustAnchor::from_subject_spki_name_constraints( - self.subject, - self.spki, - self.name_constraints, - ) - } -} - - - - - - - - - - - - } -} - // presents a modal to the user to select trusted tls certificates // and saves the selected certificates to the pddb // returns false if no certificates are trusted - and true otherwise diff --git a/libs/tls/src/rota.rs b/libs/tls/src/rota.rs new file mode 100644 index 000000000..8f954c92d --- /dev/null +++ b/libs/tls/src/rota.rs @@ -0,0 +1,75 @@ +// A mirror of rustls::OwnedTrustAnchor + +use rkyv::{Archive, Deserialize, Serialize,}; +use std::cmp::min; +use std::fmt; +use x509_parser::der_parser::der::Tag; +use x509_parser::prelude::{FromDer, X509Certificate}; + +#[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()), + } + } + + pub fn subject(&self) -> String { + let der = self.subject.clone(); + let len = min(der.len(), 127); + let mut der = der[..len].to_owned(); + der.insert(0, len as u8); + der.insert(0, Tag::Sequence.0 as u8); + match x509_parser::x509::X509Name::from_der(&der) { + Ok((_, decoded)) => decoded.to_string(), + Err(e) => format!("der parse failed: {e}").to_string(), + } + } +} + +impl<'a> From<&X509Certificate<'a>> for RustlsOwnedTrustAnchor { + fn from(x509: &X509Certificate) -> Self { + RustlsOwnedTrustAnchor { + subject: x509.subject().as_raw().to_vec(), + spki: x509.public_key().raw.to_vec(), + name_constraints: None, + // may have to pass value thru from certificates parameter + // name_constraints: match x509.name_constraints() { + // Ok(Some(nc)) => Some(nc.value), + // Ok(None) => None, + // Err(e) => { + // log::warn!("failed to extract x509 name_constraints: {}", e); + // None + // } + // }, + } + } +} + +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 index 763026792..bbef8516e 100644 --- a/libs/tls/src/trusted.rs +++ b/libs/tls/src/trusted.rs @@ -1,7 +1,7 @@ use crate::get_cert; -use crate::{RustlsOwnedTrustAnchor, TLS_CERT_DICT}; +use crate::{rota::RustlsOwnedTrustAnchor, TLS_CERT_DICT}; use rustls::RootCertStore; -use std::io::{Error, ErrorKind, Read, Write}; +use std::io::Error; use std::path::PathBuf; pub struct Trusted { From b8b591401fb84d68ec1a9b22b9d2ac805c0370cb Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Wed, 28 Jun 2023 05:56:41 +1000 Subject: [PATCH 27/46] reconfigure `[features]` `tls` `rootCA` & `websocket` --- libs/tls/Cargo.toml | 5 ++++- services/shellchat/Cargo.toml | 6 ++++-- services/shellchat/src/cmds/net_cmd.rs | 16 +++++++++------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/libs/tls/Cargo.toml b/libs/tls/Cargo.toml index a17c2fa31..6311e3f60 100644 --- a/libs/tls/Cargo.toml +++ b/libs/tls/Cargo.toml @@ -24,5 +24,8 @@ rkyv = "0.4.3" # 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" } +webpki-roots = {version = "0.23.1", optional = true} x509-parser = "0.15.0" + +[features] +rootCA = ["webpki-roots"] diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index 71148d00c..80291cfeb 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -75,7 +75,7 @@ tracing = {version = "0.1.35", optional = true} tracing-subscriber = {version = "0.3.11", optional = true} # for rustls testing -tls = {path="../../libs/tls"} +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 @@ -126,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 = ["ring", "tungstenite", "url"] +tls = ["dep:tls"] +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 af900950f..fdb82e4ae 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -23,7 +23,7 @@ use net::XousServerId; #[cfg(feature = "shellperf")] use perflib::*; -#[cfg(feature = "tls")] +#[cfg(feature = "websocket")] use tungstenite::{stream::MaybeTlsStream, WebSocket}; pub struct NetCmd { @@ -32,7 +32,7 @@ pub struct NetCmd { 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, @@ -53,7 +53,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, @@ -414,7 +414,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) = @@ -460,8 +460,10 @@ impl<'a> ShellCmdApi<'a> for NetCmd { // test make tls connection to host // trusted list trusted CA certificates #[cfg(feature = "tls")] - "tls" => if let Ok(Some(r)) = tls::cmd::shellchat(&mut tokens) { - write!(ret, "{r}").unwrap(); + "tls" => { + 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. @@ -886,7 +888,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() { From 3acc43f2b0de8e7c4b8d4aeebdcf98ea2d179e1e Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Wed, 28 Jun 2023 06:07:46 +1000 Subject: [PATCH 28/46] checkin `libs/tls/src/cmd.rs` --- libs/tls/src/cmd.rs | 170 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 libs/tls/src/cmd.rs diff --git a/libs/tls/src/cmd.rs b/libs/tls/src/cmd.rs new file mode 100644 index 000000000..51771ce02 --- /dev/null +++ b/libs/tls/src/cmd.rs @@ -0,0 +1,170 @@ +use crate::{check_trust, danger, trusted::Trusted}; +use std::convert::TryInto; +use std::io::Read; +use std::io::Write; +use std::net::TcpStream; +use std::sync::Arc; + +pub fn shellchat<'a>( + mut tokens: impl Iterator, +) -> Result, xous::Error> { + use core::fmt::Write; + let mut ret = String::new(); + match tokens.next() { + // 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 i = 0; + for rota in rotas { + crate::save_cert(format!("webpki-root-{i}").as_str(), &rota).unwrap(); + i += 1; + } + } + // 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, "failed to create sever_name from {target}").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) => check_trust(certificates), + None => false, + }; + } + 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 trusted = Trusted::new().unwrap(); + let config = rustls::ClientConfig::builder() + .with_safe_defaults() + .with_root_certificates(trusted.into()) + .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, "tcp connected\n").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, "tls accepted GET\n").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, "tls received {} bytes\n", n).ok(); + log::info!( + "{}", + std::str::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); + } + // list trusted Certificate Authority certificates + Some("trusted") => { + log::set_max_level(log::LevelFilter::Info); + log::info!("starting TLS trusted listing"); + let trusted = Trusted::new().expect("failed to initiate trusted iterator"); + for rota in trusted { + write!(ret, "🏛 {}\n", rota.subject()).ok(); + } + log::info!("finished TLS trusted listing"); + } + None | _ => { + write!(ret, "net tls \n").ok(); + #[cfg(feature = "rootCA")] + write!(ret, "\tmozilla\ttrust all Root CA's in webpki-roots\n").ok(); + write!(ret, "\tprobe \tsave host CA'a if trusted\n").ok(); + write!(ret, "\ttest \tmake tls connection to host\n").ok(); + write!(ret, "\ttrusted\tlist trusted CA certificates\n").ok(); + } + } + Ok(Some(ret)) +} From 2f042cff70cc24e3739c21dba94fae61905ed557 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Wed, 28 Jun 2023 13:35:51 +1000 Subject: [PATCH 29/46] implement tls locales/i18n.json en en-tts --- libs/tls/Cargo.toml | 1 + libs/tls/locales/i18n.json | 46 ++++++++++++++++++++++++++++++++++++++ libs/tls/src/cmd.rs | 24 ++++++++++++-------- libs/tls/src/lib.rs | 3 ++- 4 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 libs/tls/locales/i18n.json diff --git a/libs/tls/Cargo.toml b/libs/tls/Cargo.toml index 6311e3f60..2ce191452 100644 --- a/libs/tls/Cargo.toml +++ b/libs/tls/Cargo.toml @@ -19,6 +19,7 @@ modals = { path = "../../services/modals" } net = { path = "../../services/net" } pddb = { path = "../../services/pddb" } +locales = {path = "../../locales"} rkyv = "0.4.3" # note requirement for patch to xous-ring in workspace Cargo.toml diff --git a/libs/tls/locales/i18n.json b/libs/tls/locales/i18n.json new file mode 100644 index 000000000..915c95989 --- /dev/null +++ b/libs/tls/locales/i18n.json @@ -0,0 +1,46 @@ +{ + "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.mozilla_cmd": { + "en": "trust all Root CA's in webpki-roots", + "en-tts": "trust all Root CA's in webpki-roots" + }, + "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" + }, + "tls.trusted_cmd": { + "en": "list trusted CA certificates", + "en-tts": "list trusted CA certificates" + } +} diff --git a/libs/tls/src/cmd.rs b/libs/tls/src/cmd.rs index 51771ce02..143a80510 100644 --- a/libs/tls/src/cmd.rs +++ b/libs/tls/src/cmd.rs @@ -1,4 +1,5 @@ use crate::{check_trust, danger, trusted::Trusted}; +use locales::t; use std::convert::TryInto; use std::io::Read; use std::io::Write; @@ -11,6 +12,10 @@ pub fn shellchat<'a>( use core::fmt::Write; let mut ret = String::new(); match tokens.next() { + Some("help") => { + write!(ret, "{}", t!("tls.cmd_help", locales::LANG)).ok(); + () + } // save/trust all Root CA's in webpki-roots en-masse #[cfg(feature = "rootCA")] Some("mozilla") => { @@ -54,7 +59,7 @@ pub fn shellchat<'a>( }; let server_name = target.try_into().unwrap_or_else(|e| { log::warn!("failed to create sever_name from {target}: {e}"); - write!(ret, "failed to create sever_name from {target}").ok(); + 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(); @@ -108,20 +113,20 @@ pub fn shellchat<'a>( match TcpStream::connect(url) { Ok(mut sock) => { log::info!("tcp connected"); - write!(ret, "tcp connected\n").ok(); + 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, "tls accepted GET\n").ok(); + 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, "tls received {} bytes\n", n).ok(); + write!(ret, "{} {}\n", t!("tls.test_success_bytes", locales::LANG), n).ok(); log::info!( "{}", std::str::from_utf8(&plaintext).unwrap_or("utf-error") @@ -158,12 +163,13 @@ pub fn shellchat<'a>( log::info!("finished TLS trusted listing"); } None | _ => { - write!(ret, "net tls \n").ok(); + write!(ret, "{}\n", t!("tls.cmd", locales::LANG)).ok(); + write!(ret, "\thelp\n").ok(); #[cfg(feature = "rootCA")] - write!(ret, "\tmozilla\ttrust all Root CA's in webpki-roots\n").ok(); - write!(ret, "\tprobe \tsave host CA'a if trusted\n").ok(); - write!(ret, "\ttest \tmake tls connection to host\n").ok(); - write!(ret, "\ttrusted\tlist trusted CA certificates\n").ok(); + 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(); + write!(ret, "\ttrusted\t{}\n", t!("tls.trusted_cmd", locales::LANG)).ok(); } } Ok(Some(ret)) diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 6d6d92698..30b617eaf 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -4,6 +4,7 @@ pub mod rota; pub mod trusted; use crate::rota::RustlsOwnedTrustAnchor; +use locales::t; use modals::Modals; use rkyv::{ de::deserializers::AllocDeserializer, @@ -54,7 +55,7 @@ pub fn check_trust(certificates: &[Certificate]) -> bool { modals .add_list(chain) .expect("couldn't build checkbox list"); - match modals.get_checkbox("Do you trust any of these certificate authorities?") { + match modals.get_checkbox(t!("tls.check_trust_prompt", locales::LANG)) { Ok(trusted) => { trusted .iter() From 7cef80e193b6b2685608fa09fbb8b58ef5c23d73 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Thu, 29 Jun 2023 09:42:19 +1000 Subject: [PATCH 30/46] tls remove der header from captured RustlsOwnedTrustAnchor fields --- libs/tls/Cargo.toml | 1 + libs/tls/src/rota.rs | 57 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/libs/tls/Cargo.toml b/libs/tls/Cargo.toml index 2ce191452..5e38b0143 100644 --- a/libs/tls/Cargo.toml +++ b/libs/tls/Cargo.toml @@ -19,6 +19,7 @@ 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" diff --git a/libs/tls/src/rota.rs b/libs/tls/src/rota.rs index 8f954c92d..e4f5024f3 100644 --- a/libs/tls/src/rota.rs +++ b/libs/tls/src/rota.rs @@ -1,11 +1,17 @@ // A mirror of rustls::OwnedTrustAnchor -use rkyv::{Archive, Deserialize, Serialize,}; +use der::{Encode, Reader}; +use rkyv::{Archive, Deserialize, Serialize}; use std::cmp::min; +use std::convert::TryInto; use std::fmt; +use std::io::{Error, ErrorKind}; use x509_parser::der_parser::der::Tag; use x509_parser::prelude::{FromDer, X509Certificate}; +/// 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, @@ -30,6 +36,7 @@ impl RustlsOwnedTrustAnchor { let der = self.subject.clone(); let len = min(der.len(), 127); let mut der = der[..len].to_owned(); + // hack back in the DER header to enable decode :-/ der.insert(0, len as u8); der.insert(0, Tag::Sequence.0 as u8); match x509_parser::x509::X509Name::from_der(&der) { @@ -41,10 +48,24 @@ impl RustlsOwnedTrustAnchor { impl<'a> From<&X509Certificate<'a>> for RustlsOwnedTrustAnchor { fn from(x509: &X509Certificate) -> Self { + // Remove the DER headers in keeping with rustls::OwnersTrustAnchor RustlsOwnedTrustAnchor { - subject: x509.subject().as_raw().to_vec(), - spki: x509.public_key().raw.to_vec(), + 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: None, + // name_constraints: x509.name_constraints().unwrap_or(None).map(|c| c.value.into()), // may have to pass value thru from certificates parameter // name_constraints: match x509.name_constraints() { // Ok(Some(nc)) => Some(nc.value), @@ -58,6 +79,36 @@ impl<'a> From<&X509Certificate<'a>> for RustlsOwnedTrustAnchor { } } +/// Remove the DER header from the DER encoded [u8] +/// 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) From 579f2c36e8117102cdfe48458e6139957f1f2ba4 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Thu, 29 Jun 2023 11:27:45 +1000 Subject: [PATCH 31/46] tls implement rota add_der_header() --- libs/tls/src/rota.rs | 54 ++++++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/libs/tls/src/rota.rs b/libs/tls/src/rota.rs index e4f5024f3..3e3918d2c 100644 --- a/libs/tls/src/rota.rs +++ b/libs/tls/src/rota.rs @@ -1,12 +1,9 @@ // A mirror of rustls::OwnedTrustAnchor -use der::{Encode, Reader}; +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::der_parser::der::Tag; use x509_parser::prelude::{FromDer, X509Certificate}; /// A close mirror of rustls::OwnedTrustAnchor - but with extras @@ -32,16 +29,20 @@ impl RustlsOwnedTrustAnchor { } } + // decoded subject pub fn subject(&self) -> String { - let der = self.subject.clone(); - let len = min(der.len(), 127); - let mut der = der[..len].to_owned(); - // hack back in the DER header to enable decode :-/ - der.insert(0, len as u8); - der.insert(0, Tag::Sequence.0 as u8); - match x509_parser::x509::X509Name::from_der(&der) { - Ok((_, decoded)) => decoded.to_string(), - Err(e) => format!("der parse failed: {e}").to_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); + "der decode failed".to_string() + } } } } @@ -79,7 +80,32 @@ impl<'a> From<&X509Certificate<'a>> for RustlsOwnedTrustAnchor { } } -/// Remove the DER header from the DER encoded [u8] +/// Add a DER header to a DER encoded [u8] +fn add_der_header(tag: Tag, chook: &Vec) -> Result, Error> { + match Header::new(tag, chook.len()) { + Ok(header) => { + let mut buff: [u8; 32] = [0u8; 32]; + match header.encode_to_slice(&mut buff) { + Ok(der) => { + let der = [der, chook].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) { From 53b002ad9c996d41bce0ad544c1da50b4d539b5c Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Thu, 29 Jun 2023 13:10:28 +1000 Subject: [PATCH 32/46] return count of trusted certificates from shellchat net tls probe & shellchat net tls mozilla --- libs/tls/src/cmd.rs | 14 +++++++++----- libs/tls/src/lib.rs | 8 ++++---- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/libs/tls/src/cmd.rs b/libs/tls/src/cmd.rs index 143a80510..5446ff7ef 100644 --- a/libs/tls/src/cmd.rs +++ b/libs/tls/src/cmd.rs @@ -30,11 +30,12 @@ pub fn shellchat<'a>( ) }) .collect(); - let mut i = 0; + let mut count = 0; for rota in rotas { - crate::save_cert(format!("webpki-root-{i}").as_str(), &rota).unwrap(); - i += 1; + crate::save_cert(format!("webpki-root-{count}").as_str(), &rota).unwrap(); + count += 1; } + write!(ret, "trusted {count} certificates").ok(); } // probe establishes a tls connection to the supplied host, extracts the // certificates offered and immediately closes the connection. @@ -77,8 +78,11 @@ pub fn shellchat<'a>( conn.send_close_notify(); match conn.peer_certificates() { - Some(certificates) => check_trust(certificates), - None => false, + Some(certificates) => { + let count = check_trust(certificates); + write!(ret, "trusted {count} certificates").ok(); + } + None => (), }; } Err(e) => { diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 30b617eaf..c5f99976c 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -25,8 +25,8 @@ const CURRENT_VERSION_KEY: &str = "__version"; // presents a modal to the user to select trusted tls certificates // and saves the selected certificates to the pddb -// returns false if no certificates are trusted - and true otherwise -pub fn check_trust(certificates: &[Certificate]) -> bool { +// returns a count of trusted certificates +pub fn check_trust(certificates: &[Certificate]) -> usize { let xns = XousNames::new().unwrap(); let modals = Modals::new(&xns).unwrap(); @@ -82,11 +82,11 @@ pub fn check_trust(certificates: &[Certificate]) -> bool { .expect("modal failed"); }); }); - trusted.len() > 0 + trusted.len() } _ => { log::error!("get_checkbox failed"); - false + 0 } } } From 0545a08c8eeb9ef3ad558c9a772537f55de0c017 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Thu, 29 Jun 2023 13:13:01 +1000 Subject: [PATCH 33/46] remove redundant use rustls from net_cmd.rs --- services/shellchat/src/cmds/net_cmd.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/services/shellchat/src/cmds/net_cmd.rs b/services/shellchat/src/cmds/net_cmd.rs index fdb82e4ae..c2e4cda2b 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -2,8 +2,6 @@ use crate::{CommonEnv, ShellCmdApi}; use com::api::NET_MTU; use net::NetPingCallback; use num_traits::*; -use rustls::internal::msgs::base::PayloadU16; -use rustls::internal::msgs::handshake::DistinguishedName; use std::io::Read; use std::io::Write; use std::net::{IpAddr, TcpListener, TcpStream}; From 1e95167f4767d13a220d596da4f9241805558182 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Thu, 29 Jun 2023 14:41:25 +1000 Subject: [PATCH 34/46] implement shellchat net tls delete --- libs/tls/locales/i18n.json | 4 ++++ libs/tls/src/cmd.rs | 10 +++++++++- libs/tls/src/lib.rs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/libs/tls/locales/i18n.json b/libs/tls/locales/i18n.json index 915c95989..e8e85a05c 100644 --- a/libs/tls/locales/i18n.json +++ b/libs/tls/locales/i18n.json @@ -11,6 +11,10 @@ "en": "net tls ", "en-tts": "net tls " }, + "tls.delete_cmd": { + "en": "delete ALL trusted Certificates", + "en-tts": "delete ALL trusted Certificates" + }, "tls.mozilla_cmd": { "en": "trust all Root CA's in webpki-roots", "en-tts": "trust all Root CA's in webpki-roots" diff --git a/libs/tls/src/cmd.rs b/libs/tls/src/cmd.rs index 5446ff7ef..a0f83e8a0 100644 --- a/libs/tls/src/cmd.rs +++ b/libs/tls/src/cmd.rs @@ -1,4 +1,4 @@ -use crate::{check_trust, danger, trusted::Trusted}; +use crate::{check_trust, danger, del_all_cert, trusted::Trusted}; use locales::t; use std::convert::TryInto; use std::io::Read; @@ -12,6 +12,13 @@ pub fn shellchat<'a>( use core::fmt::Write; let mut ret = String::new(); match tokens.next() { + // delete ALL trusted CA Certificates + Some("delete") => { + log::info!("starting TLS delete certificates"); + let count = del_all_cert().unwrap(); + write!(ret, "delete {count} certificates").ok(); + log::info!("finished TLS delete certificates"); + } Some("help") => { write!(ret, "{}", t!("tls.cmd_help", locales::LANG)).ok(); () @@ -168,6 +175,7 @@ pub fn shellchat<'a>( } None | _ => { write!(ret, "{}\n", t!("tls.cmd", locales::LANG)).ok(); + write!(ret, "\tdelete\t{}\n", t!("tls.delete_cmd", locales::LANG)).ok(); write!(ret, "\thelp\n").ok(); #[cfg(feature = "rootCA")] write!(ret, "\tmozilla\t{}\n", t!("tls.mozilla_cmd", locales::LANG)).ok(); diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index c5f99976c..0ec4d0697 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -91,6 +91,42 @@ pub fn check_trust(certificates: &[Certificate]) -> usize { } } + +// deletes ALL tls trust-anchors from the pddb +// returns the number of certs deleted +pub fn del_all_cert() -> Result { + let mut count = 0; + let mut keypath = PathBuf::new(); + keypath.push(TLS_CERT_DICT); + if std::fs::metadata(&keypath).is_ok() { + for entry in std::fs::read_dir(keypath)? { + let entry = entry?; + let key = entry.file_name().into_string().unwrap(); + let path = entry.path(); + match std::fs::remove_file(path) { + Ok(_) => { + log::info!("deleted {key}"); + count += 1; + } + Err(e) => log::warn!("failed to delete {key} : {e}"), + }; + }; + }; + Ok(count) +} + + +// deletes a tls trust-anchor from the pddb +pub fn del_cert(key: &str) -> Result<(), Error> { + let mut keypath = PathBuf::new(); + keypath.push(TLS_CERT_DICT); + keypath.push(key); + match std::fs::remove_file(keypath) { + Ok(_) => log::info!("deleted {key}") , + Err(e) => log::warn!("failed to delete {key} : {e}"), + }; + return Ok(()); +} // saves a tls trust-anchor to the pddb pub fn save_cert(key: &str, ta: &RustlsOwnedTrustAnchor) -> Result<(), Error> { if key.starts_with("__") { From f69b247398a16efe0bb30376d4f12525b63bc7b3 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Thu, 29 Jun 2023 20:28:23 +1000 Subject: [PATCH 35/46] fix tls feature dependency on ring --- services/shellchat/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index 80291cfeb..53a7c0e0b 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -126,7 +126,7 @@ dbg-ecupdate = [] # for issuing manual commands to initiate an EC update (vs UX) test-rekey = [] no-codec = [] nettest = [] # batch network tests -tls = ["dep:tls"] +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 From 0698d5003e5323ce2dff24200ef61c4935166bd1 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Fri, 30 Jun 2023 05:01:40 +1000 Subject: [PATCH 36/46] rename pddb dict to tls.trusted --- libs/tls/src/lib.rs | 14 +++++++------- libs/tls/src/trusted.rs | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 0ec4d0697..8a28fcd6a 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -20,7 +20,7 @@ use x509_parser::prelude::{FromDer, X509Certificate}; use xous_names::XousNames; /// PDDB Dict for tls trusted certificates keys -const TLS_CERT_DICT: &str = "tls/cert"; +const TLS_TRUSTED_DICT: &str = "tls.trusted"; const CURRENT_VERSION_KEY: &str = "__version"; // presents a modal to the user to select trusted tls certificates @@ -97,7 +97,7 @@ pub fn check_trust(certificates: &[Certificate]) -> usize { pub fn del_all_cert() -> Result { let mut count = 0; let mut keypath = PathBuf::new(); - keypath.push(TLS_CERT_DICT); + keypath.push(TLS_TRUSTED_DICT); if std::fs::metadata(&keypath).is_ok() { for entry in std::fs::read_dir(keypath)? { let entry = entry?; @@ -119,7 +119,7 @@ pub fn del_all_cert() -> Result { // deletes a tls trust-anchor from the pddb pub fn del_cert(key: &str) -> Result<(), Error> { let mut keypath = PathBuf::new(); - keypath.push(TLS_CERT_DICT); + keypath.push(TLS_TRUSTED_DICT); keypath.push(key); match std::fs::remove_file(keypath) { Ok(_) => log::info!("deleted {key}") , @@ -137,9 +137,9 @@ pub fn save_cert(key: &str, ta: &RustlsOwnedTrustAnchor) -> Result<(), Error> { } else { log::trace!("set '{}' = '{:?}'", key, ta); let mut keypath = PathBuf::new(); - keypath.push(TLS_CERT_DICT); + keypath.push(TLS_TRUSTED_DICT); if !std::fs::metadata(&keypath).is_ok() { - log::info!("dict '{}' does NOT exist.. creating it", TLS_CERT_DICT); + log::info!("dict '{}' does NOT exist.. creating it", TLS_TRUSTED_DICT); std::fs::create_dir_all(&keypath)?; } keypath.push(key); @@ -163,9 +163,9 @@ pub fn save_cert(key: &str, ta: &RustlsOwnedTrustAnchor) -> Result<(), Error> { // retrieves a tls trust-anchor from the pddb pub fn get_cert(key: &str) -> Result, Error> { let mut keypath = PathBuf::new(); - keypath.push(TLS_CERT_DICT); + keypath.push(TLS_TRUSTED_DICT); if !std::fs::metadata(&keypath).is_ok() { - log::info!("dict '{}' does NOT exist.. creating it", TLS_CERT_DICT); + log::info!("dict '{}' does NOT exist.. creating it", TLS_TRUSTED_DICT); std::fs::create_dir_all(&keypath)?; } diff --git a/libs/tls/src/trusted.rs b/libs/tls/src/trusted.rs index bbef8516e..3b6c56b40 100644 --- a/libs/tls/src/trusted.rs +++ b/libs/tls/src/trusted.rs @@ -1,5 +1,5 @@ use crate::get_cert; -use crate::{rota::RustlsOwnedTrustAnchor, TLS_CERT_DICT}; +use crate::{rota::RustlsOwnedTrustAnchor, TLS_TRUSTED_DICT}; use rustls::RootCertStore; use std::io::Error; use std::path::PathBuf; @@ -11,9 +11,9 @@ pub struct Trusted { impl Trusted { pub fn new() -> Result { let mut keypath = PathBuf::new(); - keypath.push(TLS_CERT_DICT); + keypath.push(TLS_TRUSTED_DICT); if !std::fs::metadata(&keypath).is_ok() { - log::info!("dict '{}' does NOT exist.. creating it", TLS_CERT_DICT); + log::info!("dict '{}' does NOT exist.. creating it", TLS_TRUSTED_DICT); std::fs::create_dir_all(&keypath)?; } Ok(Self { From 2311ef29d5d9d381f80c09f24a378961dbea799c Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Mon, 3 Jul 2023 05:34:16 +1000 Subject: [PATCH 37/46] tls replace std::fs with pddb::Pddb --- libs/tls/Cargo.toml | 1 + libs/tls/src/cmd.rs | 51 +++--- libs/tls/src/lib.rs | 388 ++++++++++++++++++++++++++----------------- libs/tls/src/rota.rs | 7 +- 4 files changed, 272 insertions(+), 175 deletions(-) diff --git a/libs/tls/Cargo.toml b/libs/tls/Cargo.toml index 5e38b0143..228d65eef 100644 --- a/libs/tls/Cargo.toml +++ b/libs/tls/Cargo.toml @@ -22,6 +22,7 @@ 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"] } diff --git a/libs/tls/src/cmd.rs b/libs/tls/src/cmd.rs index a0f83e8a0..1763ea955 100644 --- a/libs/tls/src/cmd.rs +++ b/libs/tls/src/cmd.rs @@ -1,9 +1,10 @@ -use crate::{check_trust, danger, del_all_cert, trusted::Trusted}; +use crate::{danger, Tls}; use locales::t; 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; pub fn shellchat<'a>( @@ -15,8 +16,9 @@ pub fn shellchat<'a>( // delete ALL trusted CA Certificates Some("delete") => { log::info!("starting TLS delete certificates"); - let count = del_all_cert().unwrap(); - write!(ret, "delete {count} certificates").ok(); + let tls = Tls::new(); + let count = tls.del_all_cert().unwrap(); + write!(ret, "deleted {count} certificates").ok(); log::info!("finished TLS delete certificates"); } Some("help") => { @@ -26,20 +28,26 @@ pub fn shellchat<'a>( // 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 rotas: Vec<(String, crate::RustlsOwnedTrustAnchor)> = + webpki_roots::TLS_SERVER_ROOTS + .0 + .iter() + .map(|ta| { + let rota = + crate::RustlsOwnedTrustAnchor::from_subject_spki_name_constraints( + ta.subject, + ta.spki, + ta.name_constraints, + ); + let key = rota.subject(); + (key, rota) + }) + .collect(); let mut count = 0; - for rota in rotas { - crate::save_cert(format!("webpki-root-{count}").as_str(), &rota).unwrap(); + let tls = Tls::new(); + for (key, rota) in rotas { + tls.save_cert(&key, &rota) + .unwrap_or_else(|e| log::warn!("{e}")); count += 1; } write!(ret, "trusted {count} certificates").ok(); @@ -86,7 +94,8 @@ pub fn shellchat<'a>( match conn.peer_certificates() { Some(certificates) => { - let count = check_trust(certificates); + let tls = Tls::new(); + let count = tls.check_trust(certificates); write!(ret, "trusted {count} certificates").ok(); } None => (), @@ -105,10 +114,10 @@ pub fn shellchat<'a>( log::set_max_level(log::LevelFilter::Info); log::info!("starting TLS run"); log::info!("build TLS client config"); - let trusted = Trusted::new().unwrap(); + let tls = Tls::new(); let config = rustls::ClientConfig::builder() .with_safe_defaults() - .with_root_certificates(trusted.into()) + .with_root_certificates(tls.root_store()) .with_no_client_auth(); let target = match tokens.next() { Some(target) => target, @@ -167,8 +176,8 @@ pub fn shellchat<'a>( Some("trusted") => { log::set_max_level(log::LevelFilter::Info); log::info!("starting TLS trusted listing"); - let trusted = Trusted::new().expect("failed to initiate trusted iterator"); - for rota in trusted { + let tls = Tls::new(); + for rota in tls.trusted() { write!(ret, "🏛 {}\n", rota.subject()).ok(); } log::info!("finished TLS trusted listing"); diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 8a28fcd6a..e29be5201 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -1,7 +1,6 @@ pub mod cmd; pub mod danger; pub mod rota; -pub mod trusted; use crate::rota::RustlsOwnedTrustAnchor; use locales::t; @@ -9,182 +8,265 @@ use modals::Modals; use rkyv::{ de::deserializers::AllocDeserializer, ser::{serializers::WriteSerializer, Serializer}, - AlignedVec, Deserialize + Deserialize, }; -use rustls::Certificate; +use rustls::{Certificate, RootCertStore}; use std::convert::{Into, TryFrom}; -use std::fs::File; use std::io::{Error, ErrorKind, Read, Write}; -use std::path::PathBuf; -use x509_parser::prelude::{FromDer, X509Certificate}; +use x509_parser::prelude::{oid_registry, 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"; -// 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(certificates: &[Certificate]) -> usize { - let xns = XousNames::new().unwrap(); - let modals = Modals::new(&xns).unwrap(); +pub struct Tls { + pddb: pddb::Pddb, +} - let certificates: Vec = certificates - .iter() - .map(|cert| X509Certificate::from_der(cert.as_ref())) - .filter(|result| result.is_ok()) - .map(|result| result.unwrap().1) - .filter(|x509| x509.is_ca()) - .collect(); +impl Tls { + pub fn new() -> Tls { + Tls { + pddb: pddb::Pddb::new(), + } + } - let chain: Vec = certificates - .iter() - .map(|x509| { - let subject = x509.subject(); - format!( - "{}{}\n{}\n{}", - if x509.is_ca() { "🏛 " } else { "" }, - &subject, - &x509.raw_serial_as_string()[0..24], - &x509.raw_serial_as_string()[24..], - ) - }) - .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]) - .map(|x509| { - ( - x509.raw_serial_as_string(), - RustlsOwnedTrustAnchor::from(x509), - ) - }) - .for_each(|(key, val)| { - save_cert(&key, &val).unwrap_or_else(|e| { - log::warn!("failed to save cert: {e}"); - modals - .show_notification( - format!("failed to save: {}", &val.subject()).as_str(), - None, - ) - .expect("modal failed"); + // 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 = certificates + .iter() + .map(|cert| X509Certificate::from_der(cert.as_ref())) + .filter(|result| result.is_ok()) + .map(|result| result.unwrap().1) + .filter(|x509| x509.is_ca()) + .collect(); + + let chain: Vec = certificates + .iter() + .map(|x509| { + let subject = x509.subject(); + format!( + "{}{}\n{}", + if x509.is_ca() { "🏛 " } else { "" }, + &subject, + "sha256 fingerprint here", + ) + }) + .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]) + .map(|x509| { + let subject = &x509 + .subject() + .to_string_with_registry(oid_registry()) + .unwrap(); + (subject.to_owned(), RustlsOwnedTrustAnchor::from(x509)) + }) + .for_each(|(key, val)| { + self.save_cert(&key, &val).unwrap_or_else(|e| { + log::warn!("failed to save cert: {e}"); + modals + .show_notification( + format!("failed to save:\n{}\n{e}", &val.subject()).as_str(), + None, + ) + .expect("modal failed"); + }); }); - }); - trusted.len() - } - _ => { - log::error!("get_checkbox failed"); - 0 + 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() -> Result { - let mut count = 0; - let mut keypath = PathBuf::new(); - keypath.push(TLS_TRUSTED_DICT); - if std::fs::metadata(&keypath).is_ok() { - for entry in std::fs::read_dir(keypath)? { - let entry = entry?; - let key = entry.file_name().into_string().unwrap(); - let path = entry.path(); - match std::fs::remove_file(path) { - Ok(_) => { - log::info!("deleted {key}"); - count += 1; - } - Err(e) => log::warn!("failed to delete {key} : {e}"), - }; + // 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, }; - }; - Ok(count) -} - - -// deletes a tls trust-anchor from the pddb -pub fn del_cert(key: &str) -> Result<(), Error> { - let mut keypath = PathBuf::new(); - keypath.push(TLS_TRUSTED_DICT); - keypath.push(key); - match std::fs::remove_file(keypath) { - Ok(_) => log::info!("deleted {key}") , - Err(e) => log::warn!("failed to delete {key} : {e}"), - }; - return Ok(()); -} -// saves a tls trust-anchor to the pddb -pub fn save_cert(key: &str, ta: &RustlsOwnedTrustAnchor) -> Result<(), Error> { - if key.starts_with("__") { - Err(Error::new( - ErrorKind::PermissionDenied, - "may not set a variable beginning with __ ", - )) - } else { - log::trace!("set '{}' = '{:?}'", key, ta); - 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)?; + 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), } - keypath.push(key); + Ok(count) + } - // serialize the trust-anchor - let mut serializer = WriteSerializer::new(AlignedVec::new()); - let pos: u16 = u16::try_from(serializer.serialize_value(ta).unwrap()).expect("data > u16"); + // 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(()); + } - // this next bit of black-magic bolts pos onto buff to enable deserialization - let mut buf = serializer.into_inner(); - let pos_bytes = pos.to_be_bytes(); - buf.push(pos_bytes[0]); - buf.push(pos_bytes[1]); + // saves a tls trust-anchor to the pddb + pub fn save_cert(&self, key: &str, ta: &RustlsOwnedTrustAnchor) -> Result<(), Error> { + if key.starts_with("__") { + Err(Error::new( + ErrorKind::PermissionDenied, + "may not set a variable beginning with __ ", + )) + } else { + 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) + buf.push(0u8); + buf.push(0u8); - // save key & trust-anchor to pddb - File::create(keypath)?.write_all(&buf[..])?; - Ok(()) + // serialize the trust-anchor + let mut serializer = WriteSerializer::new(buf); + 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); + } + } + } + _ => log::warn!("failed to create {}:{}", TLS_TRUSTED_DICT, key), + } + 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[2..2+pos], 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 } } -// retrieves a tls trust-anchor from the pddb -pub fn get_cert(key: &str) -> Result, Error> { - 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)?; - } - keypath.push(key); - if let Ok(mut file) = File::open(keypath) { - // read the key & serialized trust-anchor from the pddb - let mut bytes = Vec::::new(); - let len = file.read_to_end(&mut bytes)?; - // black unmagic to unbold pos from the buffer end - let pos: u16 = u16::from_be_bytes([bytes[len - 2], bytes[len - 1]]); - // deserialize the trust-anchor - let archive = unsafe { rkyv::archived_value::(&bytes, pos.into()) }; - let ta = archive.deserialize(&mut AllocDeserializer {}).ok(); - log::trace!("get '{}' = '{:?}'", key, ta); - return Ok(ta); - } else { - return Ok(None); - } -} + + + + + + + diff --git a/libs/tls/src/rota.rs b/libs/tls/src/rota.rs index 3e3918d2c..e7a64d903 100644 --- a/libs/tls/src/rota.rs +++ b/libs/tls/src/rota.rs @@ -2,10 +2,15 @@ use der::{Encode, Header, Reader, Tag}; use rkyv::{Archive, Deserialize, Serialize}; +use sha2::Digest; + 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. @@ -40,7 +45,7 @@ impl RustlsOwnedTrustAnchor { } }, Err(e) => { - log::warn!("{:?}", e); + log::warn!("{:?}", e.into_inner().unwrap()); "der decode failed".to_string() } } From a4de987eb853824cb019e452c71c973a2f0d8166 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Mon, 3 Jul 2023 07:45:01 +1000 Subject: [PATCH 38/46] show fingerprint in Tls::check_trusted() modal --- libs/tls/src/lib.rs | 46 ++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index e29be5201..fc9237852 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -11,6 +11,7 @@ use rkyv::{ Deserialize, }; use rustls::{Certificate, RootCertStore}; +use sha2::Digest; use std::convert::{Into, TryFrom}; use std::io::{Error, ErrorKind, Read, Write}; use x509_parser::prelude::{oid_registry, FromDer, X509Certificate}; @@ -38,25 +39,24 @@ impl Tls { let xns = XousNames::new().unwrap(); let modals = Modals::new(&xns).unwrap(); - let certificates: Vec = certificates + let certificates: Vec<(String, X509Certificate)> = certificates .iter() - .map(|cert| X509Certificate::from_der(cert.as_ref())) - .filter(|result| result.is_ok()) - .map(|result| result.unwrap().1) - .filter(|x509| x509.is_ca()) + .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(|x509| { - let subject = x509.subject(); - format!( - "{}{}\n{}", - if x509.is_ca() { "🏛 " } else { "" }, - &subject, - "sha256 fingerprint here", - ) - }) + .map(|(fingerprint, x509)| format!("🏛 {}\n{}", &x509.subject(), open_hex(fingerprint),)) .collect(); let chain: Vec<&str> = chain.iter().map(AsRef::as_ref).collect(); modals @@ -71,7 +71,7 @@ impl Tls { .get_check_index() .unwrap() .iter() - .map(|i| &certificates[*i]) + .map(|i| &certificates[*i].1) .map(|x509| { let subject = &x509 .subject() @@ -259,6 +259,22 @@ impl Tls { } } +// 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::() +} From ffb573f5dcf6c71448c89d53b220af02ed6dc756 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Mon, 3 Jul 2023 16:49:59 +1000 Subject: [PATCH 39/46] implement tls::rota::pddb_key --- libs/tls/src/cmd.rs | 31 ++++++--------- libs/tls/src/lib.rs | 94 ++++++++++++++++++++------------------------ libs/tls/src/rota.rs | 37 ++++++++++++++++- 3 files changed, 90 insertions(+), 72 deletions(-) diff --git a/libs/tls/src/cmd.rs b/libs/tls/src/cmd.rs index 1763ea955..43fd81dd0 100644 --- a/libs/tls/src/cmd.rs +++ b/libs/tls/src/cmd.rs @@ -28,26 +28,21 @@ pub fn shellchat<'a>( // save/trust all Root CA's in webpki-roots en-masse #[cfg(feature = "rootCA")] Some("mozilla") => { - let rotas: Vec<(String, crate::RustlsOwnedTrustAnchor)> = - webpki_roots::TLS_SERVER_ROOTS - .0 - .iter() - .map(|ta| { - let rota = - crate::RustlsOwnedTrustAnchor::from_subject_spki_name_constraints( - ta.subject, - ta.spki, - ta.name_constraints, - ); - let key = rota.subject(); - (key, rota) - }) - .collect(); + 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 = 0; let tls = Tls::new(); - for (key, rota) in rotas { - tls.save_cert(&key, &rota) - .unwrap_or_else(|e| log::warn!("{e}")); + for rota in rotas { + tls.save_cert(&rota).unwrap_or_else(|e| log::warn!("{e}")); count += 1; } write!(ret, "trusted {count} certificates").ok(); diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index fc9237852..c5f987324 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -13,8 +13,8 @@ use rkyv::{ use rustls::{Certificate, RootCertStore}; use sha2::Digest; use std::convert::{Into, TryFrom}; -use std::io::{Error, ErrorKind, Read, Write}; -use x509_parser::prelude::{oid_registry, FromDer, X509Certificate}; +use std::io::{Error, Read, Write}; +use x509_parser::prelude::{FromDer, X509Certificate}; use xous_names::XousNames; /// PDDB Dict for tls trusted certificates keys @@ -72,19 +72,13 @@ impl Tls { .unwrap() .iter() .map(|i| &certificates[*i].1) - .map(|x509| { - let subject = &x509 - .subject() - .to_string_with_registry(oid_registry()) - .unwrap(); - (subject.to_owned(), RustlsOwnedTrustAnchor::from(x509)) - }) - .for_each(|(key, val)| { - self.save_cert(&key, &val).unwrap_or_else(|e| { + .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}", &val.subject()).as_str(), + format!("failed to save:\n{}\n{e}", &rota.subject()).as_str(), None, ) .expect("modal failed"); @@ -139,53 +133,49 @@ impl Tls { } // saves a tls trust-anchor to the pddb - pub fn save_cert(&self, key: &str, ta: &RustlsOwnedTrustAnchor) -> Result<(), Error> { - if key.starts_with("__") { - Err(Error::new( - ErrorKind::PermissionDenied, - "may not set a variable beginning with __ ", - )) - } else { - 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) - buf.push(0u8); - buf.push(0u8); + pub fn save_cert(&self, ta: &RustlsOwnedTrustAnchor) -> Result<(), Error> { + // let xns = XousNames::new().unwrap(); + // let modals = Modals::new(&xns).unwrap(); + 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) + buf.push(0u8); + buf.push(0u8); - // serialize the trust-anchor - let mut serializer = WriteSerializer::new(buf); - let pos = serializer.serialize_value(ta).unwrap(); - let mut bytes = serializer.into_inner(); + // serialize the trust-anchor + let mut serializer = WriteSerializer::new(buf); + 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]; + // 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); - } + 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); } } - _ => log::warn!("failed to create {}:{}", TLS_TRUSTED_DICT, key), } - Ok(()) + Err(e) => log::warn!("failed to create {}:{}\n{}", TLS_TRUSTED_DICT, key, e), } + Ok(()) } diff --git a/libs/tls/src/rota.rs b/libs/tls/src/rota.rs index e7a64d903..783aeb4f5 100644 --- a/libs/tls/src/rota.rs +++ b/libs/tls/src/rota.rs @@ -2,8 +2,7 @@ use der::{Encode, Header, Reader, Tag}; use rkyv::{Archive, Deserialize, Serialize}; -use sha2::Digest; - +use std::cmp::min; use std::convert::TryInto; use std::fmt; use std::io::{Error, ErrorKind}; @@ -50,6 +49,40 @@ impl RustlsOwnedTrustAnchor { } } } + + 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 { From 211e27b7592468dcd8c5ac00aa09cb2f56d76967 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Mon, 3 Jul 2023 18:49:56 +1000 Subject: [PATCH 40/46] refine handling of tls rota Certificate name_constraints (slightly) --- libs/tls/src/rota.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/libs/tls/src/rota.rs b/libs/tls/src/rota.rs index 783aeb4f5..cf6b06f25 100644 --- a/libs/tls/src/rota.rs +++ b/libs/tls/src/rota.rs @@ -103,17 +103,17 @@ impl<'a> From<&X509Certificate<'a>> for RustlsOwnedTrustAnchor { b"der decode failed".to_vec() } }, - name_constraints: None, - // name_constraints: x509.name_constraints().unwrap_or(None).map(|c| c.value.into()), - // may have to pass value thru from certificates parameter - // name_constraints: match x509.name_constraints() { - // Ok(Some(nc)) => Some(nc.value), - // Ok(None) => None, - // Err(e) => { - // log::warn!("failed to extract x509 name_constraints: {}", e); - // None - // } - // }, + name_constraints: match x509.name_constraints() { + Ok(Some(_)) => { + log::warn!("Name Constraints ignored"); + None + } + Ok(None) => None, + Err(e) => { + log::warn!("{e}"); + None + } + }, } } } From 044fed1096bb6b4a7961aca5630f376305d2c51a Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Mon, 3 Jul 2023 19:01:05 +1000 Subject: [PATCH 41/46] tls tidy-up --- libs/tls/src/cmd.rs | 40 +++++++++++++++++++++++++++++----------- libs/tls/src/lib.rs | 11 ----------- libs/tls/src/rota.rs | 6 +++--- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/libs/tls/src/cmd.rs b/libs/tls/src/cmd.rs index 43fd81dd0..d65f86d99 100644 --- a/libs/tls/src/cmd.rs +++ b/libs/tls/src/cmd.rs @@ -21,10 +21,10 @@ pub fn shellchat<'a>( write!(ret, "deleted {count} certificates").ok(); log::info!("finished TLS delete certificates"); } + // helpful stuff Some("help") => { - write!(ret, "{}", t!("tls.cmd_help", locales::LANG)).ok(); - () - } + write!(ret, "{}", t!("tls.cmd_help", locales::LANG)).ok(); + } // save/trust all Root CA's in webpki-roots en-masse #[cfg(feature = "rootCA")] Some("mozilla") => { @@ -70,7 +70,12 @@ pub fn shellchat<'a>( }; 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(); + 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(); @@ -141,11 +146,14 @@ pub fn shellchat<'a>( 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!( - "{}", - std::str::from_utf8(&plaintext).unwrap_or("utf-error") - ); + 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}"); @@ -183,8 +191,18 @@ pub fn shellchat<'a>( write!(ret, "\thelp\n").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(); + write!( + ret, + "\tprobe \t{}\n", + t!("tls.probe_cmd", locales::LANG) + ) + .ok(); + write!( + ret, + "\ttest \t{}\n", + t!("tls.test_cmd", locales::LANG) + ) + .ok(); write!(ret, "\ttrusted\t{}\n", t!("tls.trusted_cmd", locales::LANG)).ok(); } } diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index c5f987324..e248bb17a 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -265,14 +265,3 @@ fn open_hex(text: &str) -> String { }) .collect::() } - - - - - - - - - - - diff --git a/libs/tls/src/rota.rs b/libs/tls/src/rota.rs index cf6b06f25..af040bdf0 100644 --- a/libs/tls/src/rota.rs +++ b/libs/tls/src/rota.rs @@ -119,13 +119,13 @@ impl<'a> From<&X509Certificate<'a>> for RustlsOwnedTrustAnchor { } /// Add a DER header to a DER encoded [u8] -fn add_der_header(tag: Tag, chook: &Vec) -> Result, Error> { - match Header::new(tag, chook.len()) { +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, chook].concat(); + 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")), From 5c9905a0be2aa4e640ba15435f3964c4f3960c07 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 4 Jul 2023 05:51:59 +1000 Subject: [PATCH 42/46] add progress bar to snapchat `net tls mozilla` --- libs/tls/locales/i18n.json | 8 ++++++++ libs/tls/src/cmd.rs | 14 ++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/libs/tls/locales/i18n.json b/libs/tls/locales/i18n.json index e8e85a05c..f3df496ff 100644 --- a/libs/tls/locales/i18n.json +++ b/libs/tls/locales/i18n.json @@ -19,6 +19,14 @@ "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" diff --git a/libs/tls/src/cmd.rs b/libs/tls/src/cmd.rs index d65f86d99..5efe59097 100644 --- a/libs/tls/src/cmd.rs +++ b/libs/tls/src/cmd.rs @@ -1,11 +1,13 @@ 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, @@ -39,13 +41,21 @@ pub fn shellchat<'a>( ) }) .collect(); - let mut count = 0; + 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; } - write!(ret, "trusted {count} certificates").ok(); + 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. From 527ad433ba85991e2f5b2584719c5ab97fba371b Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 4 Jul 2023 07:15:35 +1000 Subject: [PATCH 43/46] rename snapchat `net tls delete` to `net tls deleteall` --- libs/tls/locales/i18n.json | 6 +++++- libs/tls/src/cmd.rs | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/libs/tls/locales/i18n.json b/libs/tls/locales/i18n.json index f3df496ff..1a918f07e 100644 --- a/libs/tls/locales/i18n.json +++ b/libs/tls/locales/i18n.json @@ -11,10 +11,14 @@ "en": "net tls ", "en-tts": "net tls " }, - "tls.delete_cmd": { + "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.mozilla_cmd": { "en": "trust all Root CA's in webpki-roots", "en-tts": "trust all Root CA's in webpki-roots" diff --git a/libs/tls/src/cmd.rs b/libs/tls/src/cmd.rs index 5efe59097..e670d244b 100644 --- a/libs/tls/src/cmd.rs +++ b/libs/tls/src/cmd.rs @@ -16,11 +16,11 @@ pub fn shellchat<'a>( let mut ret = String::new(); match tokens.next() { // delete ALL trusted CA Certificates - Some("delete") => { + Some("deleteall") => { log::info!("starting TLS delete certificates"); let tls = Tls::new(); let count = tls.del_all_cert().unwrap(); - write!(ret, "deleted {count} certificates").ok(); + write!(ret, "{} {}", count, t!("tls.deleteall_done", locales::LANG)).ok(); log::info!("finished TLS delete certificates"); } // helpful stuff @@ -197,7 +197,7 @@ pub fn shellchat<'a>( } None | _ => { write!(ret, "{}\n", t!("tls.cmd", locales::LANG)).ok(); - write!(ret, "\tdelete\t{}\n", t!("tls.delete_cmd", locales::LANG)).ok(); + write!(ret, "\tdeleteall\t{}\n", t!("tls.deleteall_cmd", locales::LANG)).ok(); write!(ret, "\thelp\n").ok(); #[cfg(feature = "rootCA")] write!(ret, "\tmozilla\t{}\n", t!("tls.mozilla_cmd", locales::LANG)).ok(); From a68ae232b381e0bc837bb1dda43515adef593189 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 4 Jul 2023 07:17:00 +1000 Subject: [PATCH 44/46] rename snapchat `net tls trusted` to `net tls list` --- libs/tls/locales/i18n.json | 8 ++++---- libs/tls/src/cmd.rs | 22 +++++++++++----------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/libs/tls/locales/i18n.json b/libs/tls/locales/i18n.json index 1a918f07e..d2b499427 100644 --- a/libs/tls/locales/i18n.json +++ b/libs/tls/locales/i18n.json @@ -19,6 +19,10 @@ "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" @@ -54,9 +58,5 @@ "tls.test_success_bytes": { "en": "tls received bytes", "en-tts": "tls received bytes" - }, - "tls.trusted_cmd": { - "en": "list trusted CA certificates", - "en-tts": "list trusted CA certificates" } } diff --git a/libs/tls/src/cmd.rs b/libs/tls/src/cmd.rs index e670d244b..bb08a5320 100644 --- a/libs/tls/src/cmd.rs +++ b/libs/tls/src/cmd.rs @@ -27,6 +27,16 @@ pub fn shellchat<'a>( 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") => { @@ -185,20 +195,11 @@ pub fn shellchat<'a>( log::set_max_level(log::LevelFilter::Info); } - // list trusted Certificate Authority certificates - Some("trusted") => { - 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"); - } 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!( @@ -213,7 +214,6 @@ pub fn shellchat<'a>( t!("tls.test_cmd", locales::LANG) ) .ok(); - write!(ret, "\ttrusted\t{}\n", t!("tls.trusted_cmd", locales::LANG)).ok(); } } Ok(Some(ret)) From a5ef4585a83977dcc8a134ee02c43160a88a7ca8 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 4 Jul 2023 07:29:37 +1000 Subject: [PATCH 45/46] fix tls::Tls::save_cert() --- libs/tls/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index e248bb17a..7cf1c95db 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -149,11 +149,12 @@ impl Tls { 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::new(buf); + let mut serializer = WriteSerializer::with_pos(buf, reserved); let pos = serializer.serialize_value(ta).unwrap(); let mut bytes = serializer.into_inner(); @@ -199,7 +200,7 @@ impl Tls { let pos: usize = pos.into(); // deserialize the trust-anchor let archive = unsafe { - rkyv::archived_value::(&bytes[2..2+pos], pos) + rkyv::archived_value::(&bytes, pos) }; let ta = archive.deserialize(&mut AllocDeserializer {}).ok(); log::info!("get '{}' = '{:?}'", key, &ta); From 4e0298c17ad2c51aa220a88dc69bf2f56e51076f Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Tue, 4 Jul 2023 07:34:55 +1000 Subject: [PATCH 46/46] include broken tls::rota::public_key() --- libs/tls/src/lib.rs | 3 --- libs/tls/src/rota.rs | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index 7cf1c95db..22ba7272d 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -134,8 +134,6 @@ impl Tls { // saves a tls trust-anchor to the pddb pub fn save_cert(&self, ta: &RustlsOwnedTrustAnchor) -> Result<(), Error> { - // let xns = XousNames::new().unwrap(); - // let modals = Modals::new(&xns).unwrap(); let key = ta.pddb_key(); match self.pddb.get( TLS_TRUSTED_DICT, @@ -163,7 +161,6 @@ impl Tls { 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(); diff --git a/libs/tls/src/rota.rs b/libs/tls/src/rota.rs index af040bdf0..c78e49f39 100644 --- a/libs/tls/src/rota.rs +++ b/libs/tls/src/rota.rs @@ -50,6 +50,26 @@ impl RustlsOwnedTrustAnchor { } } + // 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=") {