refine tls probe() remove unnecessary Option

This commit is contained in:
nworbnhoj 2024-02-29 06:18:43 +10:00
parent 17c1fabc1e
commit 6d24f1af41
2 changed files with 23 additions and 24 deletions

View file

@ -311,8 +311,10 @@ impl Tls {
/// ///
/// # Returns /// # Returns
/// ///
/// The TLS chain of trust for the host /// * A Vec<CertificateDer> containing the TLS chain of trust offered by the host
pub fn probe(&self, host: &str) -> Result<Option<Vec<CertificateDer>>, Error> { /// * Error if the communication with the host fails
///
pub fn probe(&self, host: &str) -> Result<Vec<CertificateDer>, Error> {
log::info!("starting TLS probe"); log::info!("starting TLS probe");
match host.to_owned().try_into() { match host.to_owned().try_into() {
Ok(server_name) => { Ok(server_name) => {
@ -337,9 +339,9 @@ impl Tls {
Some(certificates) => { Some(certificates) => {
let cert_owned: Vec<CertificateDer<'static>> = let cert_owned: Vec<CertificateDer<'static>> =
certificates.iter().map(|cert| cert.clone().into_owned()).collect(); certificates.iter().map(|cert| cert.clone().into_owned()).collect();
Ok(Some(cert_owned)) Ok(cert_owned)
} }
None => Ok(None), None => Ok(Vec::<CertificateDer<'static>>::new()),
} }
} }
Err(e) => { Err(e) => {
@ -369,10 +371,13 @@ impl Tls {
/// the number of trusted Certificates offered by the host /// the number of trusted Certificates offered by the host
pub fn inspect(&self, host: &str) -> Result<usize, Error> { pub fn inspect(&self, host: &str) -> Result<usize, Error> {
match self.probe(host) { match self.probe(host) {
Ok(certs) => match certs { Ok(certs) => {
Some(certs) => Ok(self.trust_modal(certs.to_vec())), if certs.len() > 0 {
None => Ok(0), Ok(self.trust_modal(certs.to_vec()))
}, } else {
Ok(0)
}
}
Err(e) => { Err(e) => {
log::warn!("failed to probe {host}: {e}"); log::warn!("failed to probe {host}: {e}");
Ok(0) Ok(0)
@ -394,15 +399,13 @@ impl Tls {
/// # Returns /// # Returns
/// ///
/// true if the user trusts at least one of the Certificates offered by the host. /// true if the user trusts at least one of the Certificates offered by the host.
///
pub fn accessible(&self, host: &str, inspect: bool) -> bool { pub fn accessible(&self, host: &str, inspect: bool) -> bool {
match self.probe(host) { match self.probe(host) {
Ok(certs) => match certs { Ok(certs) => match certs.iter().find(|&cert| self.is_trusted_cert(cert.clone())) {
Some(certs) => match certs.iter().find(|&cert| self.is_trusted_cert(cert.clone())) {
Some(_) => true, Some(_) => true,
None => inspect && (self.trust_modal(certs.to_vec()) > 0), None => inspect && (self.trust_modal(certs.to_vec()) > 0),
}, },
None => false,
},
Err(e) => { Err(e) => {
log::warn!("failed to probe {host}: {e}"); log::warn!("failed to probe {host}: {e}");
false false

View file

@ -37,7 +37,6 @@ impl ureq::TlsConnector for TlsConnector {
if let Some(rustls_error) = inner.downcast_ref::<rustls::Error>() { if let Some(rustls_error) = inner.downcast_ref::<rustls::Error>() {
if let rustls::Error::InvalidCertificate(_) = rustls_error { if let rustls::Error::InvalidCertificate(_) = rustls_error {
if let Ok(certs) = tls.probe(dns_name) { if let Ok(certs) = tls.probe(dns_name) {
if let Some(certs) = certs {
if certs.len() > 0 { if certs.len() > 0 {
log::info!("try again with new trusted certs"); log::info!("try again with new trusted certs");
continue; continue;
@ -46,7 +45,6 @@ impl ureq::TlsConnector for TlsConnector {
} }
} }
} }
}
// non certificate chain errors // non certificate chain errors
log::warn!("{e}"); log::warn!("{e}");
break; break;
@ -56,14 +54,12 @@ impl ureq::TlsConnector for TlsConnector {
// errors generated early in the tls handshake // errors generated early in the tls handshake
Err(rustls::Error::InvalidCertificate(_)) => { Err(rustls::Error::InvalidCertificate(_)) => {
if let Ok(certs) = tls.probe(dns_name) { if let Ok(certs) = tls.probe(dns_name) {
if let Some(certs) = certs {
if certs.len() > 0 { if certs.len() > 0 {
log::info!("try again with new trusted certs"); log::info!("try again with new trusted certs");
continue; continue;
} }
} }
} }
}
// non certificate chain errors // non certificate chain errors
Err(e) => { Err(e) => {
log::warn!("{e}"); log::warn!("{e}");