mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
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:
parent
de1dcffd66
commit
12c74e724f
3 changed files with 46 additions and 29 deletions
|
|
@ -178,16 +178,17 @@ impl<'a> SigChat<'a> {
|
||||||
self.chat.set_busy_state(true);
|
self.chat.set_busy_state(true);
|
||||||
let tls = Tls::new();
|
let tls = Tls::new();
|
||||||
match tls.probe(host) {
|
match tls.probe(host) {
|
||||||
Ok(0) => {
|
Ok(certs) => {
|
||||||
self.modals
|
if certs.len() == 0 {
|
||||||
.show_notification(t!("sigchat.account.abort", locales::LANG), None)
|
self.modals
|
||||||
.expect("abort failed");
|
.show_notification(t!("sigchat.account.abort", locales::LANG), None)
|
||||||
self.chat.set_busy_state(false);
|
.expect("abort failed");
|
||||||
false
|
self.chat.set_busy_state(false);
|
||||||
}
|
false
|
||||||
Ok(_count) => {
|
} else {
|
||||||
self.chat.set_busy_state(false);
|
self.chat.set_busy_state(false);
|
||||||
true
|
true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
self.chat.set_busy_state(false);
|
self.chat.set_busy_state(false);
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ impl Tls {
|
||||||
///
|
///
|
||||||
/// a count of trusted certificates
|
/// 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 xns = XousNames::new().unwrap();
|
||||||
let modals = Modals::new(&xns).unwrap();
|
let modals = Modals::new(&xns).unwrap();
|
||||||
|
|
||||||
|
|
@ -278,12 +278,10 @@ impl Tls {
|
||||||
root_store
|
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.
|
/// 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.
|
/// By default, rustls only provides access to a trusted certificate chain.
|
||||||
/// Probe briefly stifles the certificate validation (ie trusts everything)
|
/// Probe briefly stifles the certificate validation (ie trusts everything)
|
||||||
|
|
@ -291,10 +289,12 @@ impl Tls {
|
||||||
/// user for examination.
|
/// user for examination.
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # 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");
|
log::info!("starting TLS probe");
|
||||||
// Attempt to open the tls connection with an empty root_store
|
// Attempt to open the tls connection with an empty root_store
|
||||||
let root_store = rustls::RootCertStore::empty();
|
let root_store = rustls::RootCertStore::empty();
|
||||||
|
|
@ -306,12 +306,12 @@ impl Tls {
|
||||||
.with_safe_defaults()
|
.with_safe_defaults()
|
||||||
.with_custom_certificate_verifier(stifled_verifier)
|
.with_custom_certificate_verifier(stifled_verifier)
|
||||||
.with_no_client_auth();
|
.with_no_client_auth();
|
||||||
match target.try_into() {
|
match host.try_into() {
|
||||||
Ok(server_name) => {
|
Ok(server_name) => {
|
||||||
let mut conn =
|
let mut conn =
|
||||||
rustls::ClientConnection::new(Arc::new(config), server_name).unwrap();
|
rustls::ClientConnection::new(Arc::new(config), server_name).unwrap();
|
||||||
log::info!("connect TCPstream to {}", target);
|
log::info!("connect TCPstream to {}", host);
|
||||||
match TcpStream::connect((target, 443)) {
|
match TcpStream::connect((host, 443)) {
|
||||||
Ok(mut sock) => {
|
Ok(mut sock) => {
|
||||||
match conn.complete_io(&mut sock) {
|
match conn.complete_io(&mut sock) {
|
||||||
Ok(_) => log::info!("handshake complete"),
|
Ok(_) => log::info!("handshake complete"),
|
||||||
|
|
@ -319,8 +319,8 @@ impl Tls {
|
||||||
}
|
}
|
||||||
conn.send_close_notify();
|
conn.send_close_notify();
|
||||||
match conn.peer_certificates() {
|
match conn.peer_certificates() {
|
||||||
Some(certificates) => Ok(self.trust_modal(certificates)),
|
Some(certificates) => Ok(certificates.to_vec()),
|
||||||
None => Ok(0),
|
None => Ok(vec![]),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|
@ -330,14 +330,30 @@ impl Tls {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
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))
|
Err(Error::from(ErrorKind::InvalidInput))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn inspect(&self, target: &str) -> Result<usize, Error> {
|
/// Inspect and optionally trust Certificates offered by the host.
|
||||||
self.probe(target)
|
///
|
||||||
|
/// 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 {
|
pub fn client_config(&self) -> ClientConfig {
|
||||||
|
|
|
||||||
|
|
@ -37,8 +37,8 @@ impl ureq::TlsConnector for TlsConnector {
|
||||||
if let Some(inner) = e.get_ref() {
|
if let Some(inner) = e.get_ref() {
|
||||||
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(n) = tls.probe(dns_name) {
|
if let Ok(certs) = tls.probe(dns_name) {
|
||||||
if n > 0 {
|
if certs.len() > 0 {
|
||||||
log::info!("try again with new trusted certs");
|
log::info!("try again with new trusted certs");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -54,8 +54,8 @@ 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(n) = tls.probe(dns_name) {
|
if let Ok(certs) = tls.probe(dns_name) {
|
||||||
if n > 0 {
|
if certs.len() > 0 {
|
||||||
log::info!("try again with new trusted certs");
|
log::info!("try again with new trusted certs");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue