From 0ca24db2f0846e7605d343c570c2abb384d03d92 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Thu, 21 Dec 2023 17:52:52 +1100 Subject: [PATCH] implement Tls.is_trusted_cert() & Tls.is_trusted_x509() --- libs/tls/src/lib.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index db191cf7b..c6ad1e0f3 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -260,6 +260,41 @@ impl Tls { } } + pub fn is_trusted_cert(&self, cert: Certificate) -> bool { + match X509Certificate::from_der(cert.as_ref()) { + Ok(result) => self.is_trusted_x509(&result.1), + Err(e) => { + log::warn!("failed to get x509 from Certificate: {e}"); + false + } + } + } + + pub fn is_trusted_x509(&self, x509: &X509Certificate) -> bool { + let ta = RustlsOwnedTrustAnchor::from(x509); + let key = ta.pddb_key(); + match self.pddb.get( + TLS_TRUSTED_DICT, + &key, + None, + false, + false, + None, + None::, + ) { + // Ok(_) => true, + // Err(_) => false, + Ok(_) => { + log::info!("trusted: {key}"); + true + } + Err(_) => { + log::info!("UNtrusted: {key}"); + false + } + } + } + /// Returns a RootCertStore containing all trusted trust-anchors /// pub fn root_store(&self) -> RootCertStore {