From 1e95167f4767d13a220d596da4f9241805558182 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Thu, 29 Jun 2023 14:41:25 +1000 Subject: [PATCH] implement shellchat net tls delete --- libs/tls/locales/i18n.json | 4 ++++ libs/tls/src/cmd.rs | 10 +++++++++- libs/tls/src/lib.rs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/libs/tls/locales/i18n.json b/libs/tls/locales/i18n.json index 915c95989..e8e85a05c 100644 --- a/libs/tls/locales/i18n.json +++ b/libs/tls/locales/i18n.json @@ -11,6 +11,10 @@ "en": "net tls ", "en-tts": "net tls " }, + "tls.delete_cmd": { + "en": "delete ALL trusted Certificates", + "en-tts": "delete ALL trusted Certificates" + }, "tls.mozilla_cmd": { "en": "trust all Root CA's in webpki-roots", "en-tts": "trust all Root CA's in webpki-roots" diff --git a/libs/tls/src/cmd.rs b/libs/tls/src/cmd.rs index 5446ff7ef..a0f83e8a0 100644 --- a/libs/tls/src/cmd.rs +++ b/libs/tls/src/cmd.rs @@ -1,4 +1,4 @@ -use crate::{check_trust, danger, trusted::Trusted}; +use crate::{check_trust, danger, del_all_cert, trusted::Trusted}; use locales::t; use std::convert::TryInto; use std::io::Read; @@ -12,6 +12,13 @@ pub fn shellchat<'a>( use core::fmt::Write; let mut ret = String::new(); match tokens.next() { + // 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(); + log::info!("finished TLS delete certificates"); + } Some("help") => { write!(ret, "{}", t!("tls.cmd_help", locales::LANG)).ok(); () @@ -168,6 +175,7 @@ pub fn shellchat<'a>( } None | _ => { write!(ret, "{}\n", t!("tls.cmd", locales::LANG)).ok(); + write!(ret, "\tdelete\t{}\n", t!("tls.delete_cmd", locales::LANG)).ok(); write!(ret, "\thelp\n").ok(); #[cfg(feature = "rootCA")] write!(ret, "\tmozilla\t{}\n", t!("tls.mozilla_cmd", locales::LANG)).ok(); diff --git a/libs/tls/src/lib.rs b/libs/tls/src/lib.rs index c5f99976c..0ec4d0697 100644 --- a/libs/tls/src/lib.rs +++ b/libs/tls/src/lib.rs @@ -91,6 +91,42 @@ pub fn check_trust(certificates: &[Certificate]) -> usize { } } + +// deletes ALL tls trust-anchors from the pddb +// returns the number of certs deleted +pub fn del_all_cert() -> Result { + let mut count = 0; + let mut keypath = PathBuf::new(); + keypath.push(TLS_CERT_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}"), + }; + }; + }; + 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_CERT_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("__") {