refine tls:rota.subject() -> Result<String, Error>

This commit is contained in:
nworbnhoj 2024-02-28 05:24:57 +10:00
parent 8ed22b8257
commit faece9783e
2 changed files with 90 additions and 66 deletions

View file

@ -136,7 +136,9 @@ impl Tls {
///
/// * `ta` - a trusted trust-anchor
pub fn save_ta(&self, ta: &OwnedTrustAnchor) -> Result<(), Error> {
let key = ta.pddb_key();
match ta.pddb_key() {
Ok(key) => {
// log::warn!("AAA {:?}", ta);
match self.pddb.get(TLS_TRUSTED_DICT, &key, None, true, true, Some(ota::MAX_OTA_BYTES), None::<fn()>)
{
Ok(mut pddb_key) => {
@ -168,6 +170,9 @@ impl Tls {
}
Err(e) => log::warn!("failed to create {}:{}\n{}", TLS_TRUSTED_DICT, key, e),
}
}
Err(e) => log::warn!("failed to get pddb_key {e}"),
}
Ok(())
}
@ -250,7 +255,8 @@ impl Tls {
/// true if the certificate is saved in the TLS_TRUSTED_DICT in the pddb
pub fn is_trusted_x509(&self, x509: &X509Certificate) -> bool {
let ta = OwnedTrustAnchor::from(x509);
let key = ta.pddb_key();
match ta.pddb_key() {
Ok(key) => {
match self.pddb.get(TLS_TRUSTED_DICT, &key, None, false, false, None, None::<fn()>) {
Ok(_) => {
log::info!("trusted: {key}");
@ -262,6 +268,12 @@ impl Tls {
}
}
}
Err(e) => {
log::warn!("failed to get pddb_key: {e}");
false
}
}
}
/// Returns a RootCertStore containing all trusted trust-anchors
pub fn root_store(&self) -> RootCertStore {

View file

@ -3,6 +3,7 @@ use rkyv::{Archive, Deserialize, Serialize};
use rustls::pki_types::{Der, TrustAnchor};
use std::cmp::min;
use std::fmt;
use std::io::{Error, ErrorKind};
use x509_parser::prelude::{FromDer, X509Certificate};
pub const MAX_OTA_BYTES: usize = 1028;
@ -27,8 +28,9 @@ impl OwnedTrustAnchor {
}
}
pub fn pddb_key(&self) -> String {
let subject = self.subject();
pub fn pddb_key(&self) -> Result<String, Error> {
match self.subject() {
Ok(subject) => {
let begin = match subject.find("CN=") {
Some(begin) => Some(begin),
None => subject.find("OU="),
@ -58,24 +60,34 @@ impl OwnedTrustAnchor {
// 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()
Ok(pddb_key[..min(pddb_key.len(), KEY_NAME_LEN - 1)].to_string())
}
Err(e) => {
log::warn!("failed to construct pddb_key: {e}");
Err(Error::from(ErrorKind::InvalidData))
}
}
}
// decoded subject
pub fn subject(&self) -> String {
pub fn subject(&self) -> Result<String, Error> {
match x509_parser::x509::X509Name::from_der(&self.subject) {
Ok((_, decoded)) => decoded.to_string(),
Ok((_, decoded)) => Ok(decoded.to_string()),
Err(e) => {
log::warn!("{:?}", e);
"der decode failed".to_string()
log::warn!("failed to decode Subject: {:?}", e);
Err(Error::from(ErrorKind::InvalidData))
}
}
}
impl fmt::Debug for OwnedTrustAnchor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}\nDer{:?}", self.subject().unwrap_or("Subject error".to_string()), self.spki)
}
}
impl fmt::Display for OwnedTrustAnchor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.subject())
write!(f, "{}", self.subject().unwrap_or("Subject error".to_string()))
}
}