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); + } +}