tls replace std::fs with pddb::Pddb

This commit is contained in:
nworbnhoj 2023-07-03 05:34:16 +10:00
parent 0698d5003e
commit 2311ef29d5
4 changed files with 272 additions and 175 deletions

View file

@ -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"] }

View file

@ -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<crate::RustlsOwnedTrustAnchor> = 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");

View file

@ -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<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())
.collect();
impl Tls {
pub fn new() -> Tls {
Tls {
pddb: pddb::Pddb::new(),
}
}
let chain: Vec<String> = 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<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())
.collect();
let chain: Vec<String> = 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<usize, Error> {
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<usize, Error> {
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::<fn()>,
) {
Ok(mut pddb_key) => {
let mut buf = Vec::<u8>::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<RustlsOwnedTrustAnchor> {
match self.pddb.get(
TLS_TRUSTED_DICT,
key,
None,
false,
false,
None,
None::<fn()>,
) {
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::<RustlsOwnedTrustAnchor>(&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<RustlsOwnedTrustAnchor> {
match self.pddb.list_keys(TLS_TRUSTED_DICT, None) {
Ok(list) => list
.iter()
.map(|key| self.get_cert(&key))
.filter_map(|rota| rota)
.collect::<Vec<RustlsOwnedTrustAnchor>>(),
Err(e) => {
log::warn!("failed to get iter over trusted: {e}");
Vec::<RustlsOwnedTrustAnchor>::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::<rustls::OwnedTrustAnchor>::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<Option<RustlsOwnedTrustAnchor>, 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::<u8>::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::<RustlsOwnedTrustAnchor>(&bytes, pos.into()) };
let ta = archive.deserialize(&mut AllocDeserializer {}).ok();
log::trace!("get '{}' = '{:?}'", key, ta);
return Ok(ta);
} else {
return Ok(None);
}
}

View file

@ -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()
}
}