implement tls.probe(target)

moved from tls::cmd to tls::lib
This commit is contained in:
nworbnhoj 2023-09-11 08:05:25 +10:00
parent ee5168254e
commit 8ab3aa85be
2 changed files with 65 additions and 46 deletions

View file

@ -75,60 +75,20 @@ pub fn shellchat<'a>(
// pddb if trusted.
Some("probe") => {
log::set_max_level(log::LevelFilter::Info);
log::info!("starting TLS probe");
// Attempt to open the tls connection with an empty root_store
let root_store = rustls::RootCertStore::empty();
// Stifle the default rustls certificate verification's complaint about an
// unknown/untrusted CA root certificate so that we get to see the certificate chain
let stifled_verifier =
Arc::new(danger::StifledCertificateVerification { roots: root_store });
let config = rustls::ClientConfig::builder()
.with_safe_defaults()
.with_custom_certificate_verifier(stifled_verifier)
.with_no_client_auth();
let target = match tokens.next() {
Some(target) => target,
None => "bunnyfoo.com",
None => "betrusted.io",
};
let server_name = target.try_into().unwrap_or_else(|e| {
log::warn!("failed to create sever_name from {target}: {e}");
write!(
let tls = Tls::new();
match tls.probe(target) {
Ok(count) => write!(ret, "{} {}", count, t!("tls.probe_done", locales::LANG)).ok(),
Err(_) => write!(
ret,
"{} {target}",
t!("tls.probe_fail_servername", locales::LANG)
)
.ok();
"bunnyfoo.com".try_into().unwrap()
});
let mut conn = rustls::ClientConnection::new(Arc::new(config), server_name).unwrap();
log::info!("connect TCPstream to {}", target);
let url = format!("{}:443", target);
match TcpStream::connect(url) {
Ok(mut sock) => {
match conn.complete_io(&mut sock) {
Ok(_) => log::info!("handshake complete"),
Err(e) => {
write!(ret, "{e}").ok();
log::warn!("{e}");
}
}
conn.send_close_notify();
match conn.peer_certificates() {
Some(certificates) => {
let tls = Tls::new();
let count = tls.check_trust(certificates);
write!(ret, "{} {}", count, t!("tls.probe_done", locales::LANG)).ok();
}
None => (),
};
}
Err(e) => {
write!(ret, "{e}").ok();
log::warn!("{e}")
}
.ok(),
};
log::set_max_level(log::LevelFilter::Info);
}

View file

@ -244,6 +244,65 @@ impl Tls {
}
root_store
}
/// Probes the target and returns true is at least 1 certificate is trusted
///
/// Establishes a tls connection to the target host, extracts the
/// certificates offered and immediately closes the connection.
/// The certificates are presented by modal to the user, and saved to the
/// pddb if trusted.
///
/// By default, rustls only provides access to a trusted certificate chain.
/// Probe briefly stifles the certificate validation (ie trusts everything)
/// in order to grab the untrusted cetificate chain and present it to the
/// user for examination.
///
/// # Arguments
///
/// * `target` - the target tls site (i.e. betrusted.io)
///
pub fn probe(&self, target: &str) -> Result<usize, Error> {
log::info!("starting TLS probe");
// Attempt to open the tls connection with an empty root_store
let root_store = rustls::RootCertStore::empty();
// Stifle the default rustls certificate verification's complaint about an
// unknown/untrusted CA root certificate so that we get to see the certificate chain
let stifled_verifier =
Arc::new(danger::StifledCertificateVerification { roots: root_store });
let config = rustls::ClientConfig::builder()
.with_safe_defaults()
.with_custom_certificate_verifier(stifled_verifier)
.with_no_client_auth();
match target.try_into() {
Ok(server_name) => {
let mut conn =
rustls::ClientConnection::new(Arc::new(config), server_name).unwrap();
log::info!("connect TCPstream to {}", target);
let url = format!("{}:443", target);
match TcpStream::connect(url) {
Ok(mut sock) => {
match conn.complete_io(&mut sock) {
Ok(_) => log::info!("handshake complete"),
Err(e) => log::warn!("{e}"),
}
conn.send_close_notify();
match conn.peer_certificates() {
Some(certificates) => Ok(self.check_trust(certificates)),
None => Ok(0),
}
}
Err(e) => {
log::warn!("{e}");
Err(e)
}
}
}
Err(e) => {
log::warn!("failed to create sever_name from {target}: {e}");
Err(Error::from(ErrorKind::InvalidInput))
}
}
}
}
// https://stackoverflow.com/questions/57029974/how-to-split-string-into-chunks-in-rust-to-insert-spaces