implement Tls.inspect(host)

Tls.probe() now returns a Vec<Certificate> and Tls.inspect(host) presents the trust_modal to the user
This commit is contained in:
nworbnhoj 2023-12-21 06:51:54 +11:00
parent de1dcffd66
commit 12c74e724f
3 changed files with 46 additions and 29 deletions

View file

@ -178,16 +178,17 @@ impl<'a> SigChat<'a> {
self.chat.set_busy_state(true);
let tls = Tls::new();
match tls.probe(host) {
Ok(0) => {
self.modals
.show_notification(t!("sigchat.account.abort", locales::LANG), None)
.expect("abort failed");
self.chat.set_busy_state(false);
false
}
Ok(_count) => {
self.chat.set_busy_state(false);
true
Ok(certs) => {
if certs.len() == 0 {
self.modals
.show_notification(t!("sigchat.account.abort", locales::LANG), None)
.expect("abort failed");
self.chat.set_busy_state(false);
false
} else {
self.chat.set_busy_state(false);
true
}
}
Err(e) => {
self.chat.set_busy_state(false);

View file

@ -45,7 +45,7 @@ impl Tls {
///
/// a count of trusted certificates
///
pub fn trust_modal(&self, certificates: &[Certificate]) -> usize {
pub fn trust_modal(&self, certificates: Vec<Certificate>) -> usize {
let xns = XousNames::new().unwrap();
let modals = Modals::new(&xns).unwrap();
@ -278,12 +278,10 @@ impl Tls {
root_store
}
/// Probes the target and returns a count of newly trusted 1 certificates
/// Probes the host and returns the TLS chain of trust
///
/// Establishes a tls connection to the target host, extracts the
/// Establishes a tls connection to the 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)
@ -291,10 +289,12 @@ impl Tls {
/// user for examination.
///
/// # Arguments
/// * `host` - the target tls site (i.e. betrusted.io)
///
/// * `target` - the target tls site (i.e. betrusted.io)
/// # Returns
/// The TLS chain of trust for the host
///
pub fn probe(&self, target: &str) -> Result<usize, Error> {
pub fn probe(&self, host: &str) -> Result<Vec<Certificate>, Error> {
log::info!("starting TLS probe");
// Attempt to open the tls connection with an empty root_store
let root_store = rustls::RootCertStore::empty();
@ -306,12 +306,12 @@ impl Tls {
.with_safe_defaults()
.with_custom_certificate_verifier(stifled_verifier)
.with_no_client_auth();
match target.try_into() {
match host.try_into() {
Ok(server_name) => {
let mut conn =
rustls::ClientConnection::new(Arc::new(config), server_name).unwrap();
log::info!("connect TCPstream to {}", target);
match TcpStream::connect((target, 443)) {
log::info!("connect TCPstream to {}", host);
match TcpStream::connect((host, 443)) {
Ok(mut sock) => {
match conn.complete_io(&mut sock) {
Ok(_) => log::info!("handshake complete"),
@ -319,8 +319,8 @@ impl Tls {
}
conn.send_close_notify();
match conn.peer_certificates() {
Some(certificates) => Ok(self.trust_modal(certificates)),
None => Ok(0),
Some(certificates) => Ok(certificates.to_vec()),
None => Ok(vec![]),
}
}
Err(e) => {
@ -330,14 +330,30 @@ impl Tls {
}
}
Err(e) => {
log::warn!("failed to create sever_name from {target}: {e}");
log::warn!("failed to create sever_name from {host}: {e}");
Err(Error::from(ErrorKind::InvalidInput))
}
}
}
pub fn inspect(&self, target: &str) -> Result<usize, Error> {
self.probe(target)
/// Inspect and optionally trust Certificates offered by the host.
///
/// Probes the host and presents the certificates offered in a modal.
/// The user can optionally save trusted certificates to the pddb.
///
/// # Arguments
/// * `host` - the target tls site (i.e. betrusted.io)
///
/// # Returns the number of trusted Certificates offered by the host
///
pub fn inspect(&self, host: &str) -> Result<usize, Error> {
match self.probe(host) {
Ok(certs) => Ok(self.trust_modal(certs)),
Err(e) => {
log::warn!("failed to probe {host}: {e}");
Ok(0)
}
}
}
pub fn client_config(&self) -> ClientConfig {

View file

@ -37,8 +37,8 @@ impl ureq::TlsConnector for TlsConnector {
if let Some(inner) = e.get_ref() {
if let Some(rustls_error) = inner.downcast_ref::<rustls::Error>() {
if let rustls::Error::InvalidCertificate(_) = rustls_error {
if let Ok(n) = tls.probe(dns_name) {
if n > 0 {
if let Ok(certs) = tls.probe(dns_name) {
if certs.len() > 0 {
log::info!("try again with new trusted certs");
continue;
}
@ -54,8 +54,8 @@ impl ureq::TlsConnector for TlsConnector {
}
// errors generated early in the tls handshake
Err(rustls::Error::InvalidCertificate(_)) => {
if let Ok(n) = tls.probe(dns_name) {
if n > 0 {
if let Ok(certs) = tls.probe(dns_name) {
if certs.len() > 0 {
log::info!("try again with new trusted certs");
continue;
}