mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
tls replace std::fs with pddb::Pddb
This commit is contained in:
parent
0698d5003e
commit
2311ef29d5
4 changed files with 272 additions and 175 deletions
|
|
@ -22,6 +22,7 @@ pddb = { path = "../../services/pddb" }
|
||||||
der = { version = "0.7.6", features = ["derive"] }
|
der = { version = "0.7.6", features = ["derive"] }
|
||||||
locales = {path = "../../locales"}
|
locales = {path = "../../locales"}
|
||||||
rkyv = "0.4.3"
|
rkyv = "0.4.3"
|
||||||
|
sha2 = "0.9.8"
|
||||||
|
|
||||||
# note requirement for patch to xous-ring in workspace Cargo.toml
|
# note requirement for patch to xous-ring in workspace Cargo.toml
|
||||||
rustls = { version = "0.21.2", features = ["dangerous_configuration"] }
|
rustls = { version = "0.21.2", features = ["dangerous_configuration"] }
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
use crate::{check_trust, danger, del_all_cert, trusted::Trusted};
|
use crate::{danger, Tls};
|
||||||
use locales::t;
|
use locales::t;
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
|
use std::str::from_utf8;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub fn shellchat<'a>(
|
pub fn shellchat<'a>(
|
||||||
|
|
@ -15,8 +16,9 @@ pub fn shellchat<'a>(
|
||||||
// delete ALL trusted CA Certificates
|
// delete ALL trusted CA Certificates
|
||||||
Some("delete") => {
|
Some("delete") => {
|
||||||
log::info!("starting TLS delete certificates");
|
log::info!("starting TLS delete certificates");
|
||||||
let count = del_all_cert().unwrap();
|
let tls = Tls::new();
|
||||||
write!(ret, "delete {count} certificates").ok();
|
let count = tls.del_all_cert().unwrap();
|
||||||
|
write!(ret, "deleted {count} certificates").ok();
|
||||||
log::info!("finished TLS delete certificates");
|
log::info!("finished TLS delete certificates");
|
||||||
}
|
}
|
||||||
Some("help") => {
|
Some("help") => {
|
||||||
|
|
@ -26,20 +28,26 @@ pub fn shellchat<'a>(
|
||||||
// save/trust all Root CA's in webpki-roots en-masse
|
// save/trust all Root CA's in webpki-roots en-masse
|
||||||
#[cfg(feature = "rootCA")]
|
#[cfg(feature = "rootCA")]
|
||||||
Some("mozilla") => {
|
Some("mozilla") => {
|
||||||
let rotas: Vec<crate::RustlsOwnedTrustAnchor> = webpki_roots::TLS_SERVER_ROOTS
|
let rotas: Vec<(String, crate::RustlsOwnedTrustAnchor)> =
|
||||||
.0
|
webpki_roots::TLS_SERVER_ROOTS
|
||||||
.iter()
|
.0
|
||||||
.map(|ta| {
|
.iter()
|
||||||
crate::RustlsOwnedTrustAnchor::from_subject_spki_name_constraints(
|
.map(|ta| {
|
||||||
ta.subject,
|
let rota =
|
||||||
ta.spki,
|
crate::RustlsOwnedTrustAnchor::from_subject_spki_name_constraints(
|
||||||
ta.name_constraints,
|
ta.subject,
|
||||||
)
|
ta.spki,
|
||||||
})
|
ta.name_constraints,
|
||||||
.collect();
|
);
|
||||||
|
let key = rota.subject();
|
||||||
|
(key, rota)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
for rota in rotas {
|
let tls = Tls::new();
|
||||||
crate::save_cert(format!("webpki-root-{count}").as_str(), &rota).unwrap();
|
for (key, rota) in rotas {
|
||||||
|
tls.save_cert(&key, &rota)
|
||||||
|
.unwrap_or_else(|e| log::warn!("{e}"));
|
||||||
count += 1;
|
count += 1;
|
||||||
}
|
}
|
||||||
write!(ret, "trusted {count} certificates").ok();
|
write!(ret, "trusted {count} certificates").ok();
|
||||||
|
|
@ -86,7 +94,8 @@ pub fn shellchat<'a>(
|
||||||
|
|
||||||
match conn.peer_certificates() {
|
match conn.peer_certificates() {
|
||||||
Some(certificates) => {
|
Some(certificates) => {
|
||||||
let count = check_trust(certificates);
|
let tls = Tls::new();
|
||||||
|
let count = tls.check_trust(certificates);
|
||||||
write!(ret, "trusted {count} certificates").ok();
|
write!(ret, "trusted {count} certificates").ok();
|
||||||
}
|
}
|
||||||
None => (),
|
None => (),
|
||||||
|
|
@ -105,10 +114,10 @@ pub fn shellchat<'a>(
|
||||||
log::set_max_level(log::LevelFilter::Info);
|
log::set_max_level(log::LevelFilter::Info);
|
||||||
log::info!("starting TLS run");
|
log::info!("starting TLS run");
|
||||||
log::info!("build TLS client config");
|
log::info!("build TLS client config");
|
||||||
let trusted = Trusted::new().unwrap();
|
let tls = Tls::new();
|
||||||
let config = rustls::ClientConfig::builder()
|
let config = rustls::ClientConfig::builder()
|
||||||
.with_safe_defaults()
|
.with_safe_defaults()
|
||||||
.with_root_certificates(trusted.into())
|
.with_root_certificates(tls.root_store())
|
||||||
.with_no_client_auth();
|
.with_no_client_auth();
|
||||||
let target = match tokens.next() {
|
let target = match tokens.next() {
|
||||||
Some(target) => target,
|
Some(target) => target,
|
||||||
|
|
@ -167,8 +176,8 @@ pub fn shellchat<'a>(
|
||||||
Some("trusted") => {
|
Some("trusted") => {
|
||||||
log::set_max_level(log::LevelFilter::Info);
|
log::set_max_level(log::LevelFilter::Info);
|
||||||
log::info!("starting TLS trusted listing");
|
log::info!("starting TLS trusted listing");
|
||||||
let trusted = Trusted::new().expect("failed to initiate trusted iterator");
|
let tls = Tls::new();
|
||||||
for rota in trusted {
|
for rota in tls.trusted() {
|
||||||
write!(ret, "🏛 {}\n", rota.subject()).ok();
|
write!(ret, "🏛 {}\n", rota.subject()).ok();
|
||||||
}
|
}
|
||||||
log::info!("finished TLS trusted listing");
|
log::info!("finished TLS trusted listing");
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
pub mod cmd;
|
pub mod cmd;
|
||||||
pub mod danger;
|
pub mod danger;
|
||||||
pub mod rota;
|
pub mod rota;
|
||||||
pub mod trusted;
|
|
||||||
|
|
||||||
use crate::rota::RustlsOwnedTrustAnchor;
|
use crate::rota::RustlsOwnedTrustAnchor;
|
||||||
use locales::t;
|
use locales::t;
|
||||||
|
|
@ -9,182 +8,265 @@ use modals::Modals;
|
||||||
use rkyv::{
|
use rkyv::{
|
||||||
de::deserializers::AllocDeserializer,
|
de::deserializers::AllocDeserializer,
|
||||||
ser::{serializers::WriteSerializer, Serializer},
|
ser::{serializers::WriteSerializer, Serializer},
|
||||||
AlignedVec, Deserialize
|
Deserialize,
|
||||||
};
|
};
|
||||||
use rustls::Certificate;
|
use rustls::{Certificate, RootCertStore};
|
||||||
use std::convert::{Into, TryFrom};
|
use std::convert::{Into, TryFrom};
|
||||||
use std::fs::File;
|
|
||||||
use std::io::{Error, ErrorKind, Read, Write};
|
use std::io::{Error, ErrorKind, Read, Write};
|
||||||
use std::path::PathBuf;
|
use x509_parser::prelude::{oid_registry, FromDer, X509Certificate};
|
||||||
use x509_parser::prelude::{FromDer, X509Certificate};
|
|
||||||
use xous_names::XousNames;
|
use xous_names::XousNames;
|
||||||
|
|
||||||
/// PDDB Dict for tls trusted certificates keys
|
/// PDDB Dict for tls trusted certificates keys
|
||||||
const TLS_TRUSTED_DICT: &str = "tls.trusted";
|
const TLS_TRUSTED_DICT: &str = "tls.trusted";
|
||||||
const CURRENT_VERSION_KEY: &str = "__version";
|
const CURRENT_VERSION_KEY: &str = "__version";
|
||||||
|
|
||||||
// presents a modal to the user to select trusted tls certificates
|
pub struct Tls {
|
||||||
// and saves the selected certificates to the pddb
|
pddb: pddb::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();
|
|
||||||
|
|
||||||
let certificates: Vec<X509Certificate> = certificates
|
impl Tls {
|
||||||
.iter()
|
pub fn new() -> Tls {
|
||||||
.map(|cert| X509Certificate::from_der(cert.as_ref()))
|
Tls {
|
||||||
.filter(|result| result.is_ok())
|
pddb: pddb::Pddb::new(),
|
||||||
.map(|result| result.unwrap().1)
|
}
|
||||||
.filter(|x509| x509.is_ca())
|
}
|
||||||
.collect();
|
|
||||||
|
|
||||||
let chain: Vec<String> = certificates
|
// presents a modal to the user to select trusted tls certificates
|
||||||
.iter()
|
// and saves the selected certificates to the pddb
|
||||||
.map(|x509| {
|
// returns a count of trusted certificates
|
||||||
let subject = x509.subject();
|
pub fn check_trust(&self, certificates: &[Certificate]) -> usize {
|
||||||
format!(
|
let xns = XousNames::new().unwrap();
|
||||||
"{}{}\n{}\n{}",
|
let modals = Modals::new(&xns).unwrap();
|
||||||
if x509.is_ca() { "🏛 " } else { "" },
|
|
||||||
&subject,
|
let certificates: Vec<X509Certificate> = certificates
|
||||||
&x509.raw_serial_as_string()[0..24],
|
.iter()
|
||||||
&x509.raw_serial_as_string()[24..],
|
.map(|cert| X509Certificate::from_der(cert.as_ref()))
|
||||||
)
|
.filter(|result| result.is_ok())
|
||||||
})
|
.map(|result| result.unwrap().1)
|
||||||
.collect();
|
.filter(|x509| x509.is_ca())
|
||||||
let chain: Vec<&str> = chain.iter().map(AsRef::as_ref).collect();
|
.collect();
|
||||||
modals
|
|
||||||
.add_list(chain)
|
let chain: Vec<String> = certificates
|
||||||
.expect("couldn't build checkbox list");
|
.iter()
|
||||||
match modals.get_checkbox(t!("tls.check_trust_prompt", locales::LANG)) {
|
.map(|x509| {
|
||||||
Ok(trusted) => {
|
let subject = x509.subject();
|
||||||
trusted
|
format!(
|
||||||
.iter()
|
"{}{}\n{}",
|
||||||
.for_each(|cert| log::info!("trusts {}", cert));
|
if x509.is_ca() { "🏛 " } else { "" },
|
||||||
modals
|
&subject,
|
||||||
.get_check_index()
|
"sha256 fingerprint here",
|
||||||
.unwrap()
|
)
|
||||||
.iter()
|
})
|
||||||
.map(|i| &certificates[*i])
|
.collect();
|
||||||
.map(|x509| {
|
let chain: Vec<&str> = chain.iter().map(AsRef::as_ref).collect();
|
||||||
(
|
modals
|
||||||
x509.raw_serial_as_string(),
|
.add_list(chain)
|
||||||
RustlsOwnedTrustAnchor::from(x509),
|
.expect("couldn't build checkbox list");
|
||||||
)
|
match modals.get_checkbox(t!("tls.check_trust_prompt", locales::LANG)) {
|
||||||
})
|
Ok(trusted) => {
|
||||||
.for_each(|(key, val)| {
|
trusted
|
||||||
save_cert(&key, &val).unwrap_or_else(|e| {
|
.iter()
|
||||||
log::warn!("failed to save cert: {e}");
|
.for_each(|cert| log::info!("trusts {}", cert));
|
||||||
modals
|
modals
|
||||||
.show_notification(
|
.get_check_index()
|
||||||
format!("failed to save: {}", &val.subject()).as_str(),
|
.unwrap()
|
||||||
None,
|
.iter()
|
||||||
)
|
.map(|i| &certificates[*i])
|
||||||
.expect("modal failed");
|
.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()
|
||||||
trusted.len()
|
}
|
||||||
}
|
_ => {
|
||||||
_ => {
|
log::error!("get_checkbox failed");
|
||||||
log::error!("get_checkbox failed");
|
0
|
||||||
0
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
// deletes ALL tls trust-anchors from the pddb
|
||||||
// deletes ALL tls trust-anchors from the pddb
|
// returns the number of certs deleted
|
||||||
// returns the number of certs deleted
|
pub fn del_all_cert(&self) -> Result<usize, Error> {
|
||||||
pub fn del_all_cert() -> Result<usize, Error> {
|
let count = match self.pddb.list_keys(TLS_TRUSTED_DICT, None) {
|
||||||
let mut count = 0;
|
Ok(list) => list.len(),
|
||||||
let mut keypath = PathBuf::new();
|
Err(_) => 0,
|
||||||
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}"),
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
};
|
match self.pddb.delete_dict(TLS_TRUSTED_DICT, None) {
|
||||||
Ok(count)
|
Ok(_) => {
|
||||||
}
|
log::info!("Deleted {}\n", TLS_TRUSTED_DICT);
|
||||||
|
self.pddb
|
||||||
|
.sync()
|
||||||
// deletes a tls trust-anchor from the pddb
|
.or_else(|e| Ok::<(), Error>(log::warn!("{e}")))
|
||||||
pub fn del_cert(key: &str) -> Result<(), Error> {
|
.ok();
|
||||||
let mut keypath = PathBuf::new();
|
// match self.pddb.sync() {
|
||||||
keypath.push(TLS_TRUSTED_DICT);
|
// Err(e) => log::warn!("{e}"),
|
||||||
keypath.push(key);
|
// _ => (),
|
||||||
match std::fs::remove_file(keypath) {
|
// }
|
||||||
Ok(_) => log::info!("deleted {key}") ,
|
}
|
||||||
Err(e) => log::warn!("failed to delete {key} : {e}"),
|
Err(e) => log::warn!("failed to delete {}: {:?}", TLS_TRUSTED_DICT, 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)?;
|
|
||||||
}
|
}
|
||||||
keypath.push(key);
|
Ok(count)
|
||||||
|
}
|
||||||
|
|
||||||
// serialize the trust-anchor
|
// deletes a tls trust-anchor from the pddb
|
||||||
let mut serializer = WriteSerializer::new(AlignedVec::new());
|
pub fn del_cert(&self, key: &str) -> Result<(), Error> {
|
||||||
let pos: u16 = u16::try_from(serializer.serialize_value(ta).unwrap()).expect("data > u16");
|
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
|
// saves a tls trust-anchor to the pddb
|
||||||
let mut buf = serializer.into_inner();
|
pub fn save_cert(&self, key: &str, ta: &RustlsOwnedTrustAnchor) -> Result<(), Error> {
|
||||||
let pos_bytes = pos.to_be_bytes();
|
if key.starts_with("__") {
|
||||||
buf.push(pos_bytes[0]);
|
Err(Error::new(
|
||||||
buf.push(pos_bytes[1]);
|
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
|
// serialize the trust-anchor
|
||||||
File::create(keypath)?.write_all(&buf[..])?;
|
let mut serializer = WriteSerializer::new(buf);
|
||||||
Ok(())
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,15 @@
|
||||||
|
|
||||||
use der::{Encode, Header, Reader, Tag};
|
use der::{Encode, Header, Reader, Tag};
|
||||||
use rkyv::{Archive, Deserialize, Serialize};
|
use rkyv::{Archive, Deserialize, Serialize};
|
||||||
|
use sha2::Digest;
|
||||||
|
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
use std::fmt;
|
||||||
use std::io::{Error, ErrorKind};
|
use std::io::{Error, ErrorKind};
|
||||||
use x509_parser::prelude::{FromDer, X509Certificate};
|
use x509_parser::prelude::{FromDer, X509Certificate};
|
||||||
|
|
||||||
|
pub const MAX_ROTA_BYTES: usize = 1028;
|
||||||
|
|
||||||
/// A close mirror of rustls::OwnedTrustAnchor - but with extras
|
/// A close mirror of rustls::OwnedTrustAnchor - but with extras
|
||||||
/// Note that the subject, spki & name_constraints fields are
|
/// Note that the subject, spki & name_constraints fields are
|
||||||
/// DER encoded but WITHOUT the DER header.
|
/// DER encoded but WITHOUT the DER header.
|
||||||
|
|
@ -40,7 +45,7 @@ impl RustlsOwnedTrustAnchor {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::warn!("{:?}", e);
|
log::warn!("{:?}", e.into_inner().unwrap());
|
||||||
"der decode failed".to_string()
|
"der decode failed".to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue