Skip to content

Commit

Permalink
Only load roots & root cert if using cert validation
Browse files Browse the repository at this point in the history
  • Loading branch information
tarkah committed Apr 4, 2024
1 parent 45f3e77 commit 364db2e
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions irc/src/connection/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,24 @@ pub async fn connect<'a>(
client_cert_path: Option<&'a PathBuf>,
client_key_path: Option<&'a PathBuf>,
) -> Result<TlsStream<TcpStream>, Error> {
let mut roots = rustls::RootCertStore::empty();

for cert in rustls_native_certs::load_native_certs()? {
roots.add(cert).unwrap();
}
if let Some(cert_path) = root_cert_path {
let cert_bytes = fs::read(&cert_path).await?;
let certs =
rustls_pemfile::certs(&mut Cursor::new(&cert_bytes)).collect::<Result<Vec<_>, _>>()?;
roots.add_parsable_certificates(certs);
}

let builder = if accept_invalid_certs {
rustls::ClientConfig::builder()
.dangerous()
.with_custom_certificate_verifier(Arc::new(AcceptInvalidCerts))
} else {
let mut roots = rustls::RootCertStore::empty();

for cert in rustls_native_certs::load_native_certs()? {
roots.add(cert).unwrap();
}

if let Some(cert_path) = root_cert_path {
let cert_bytes = fs::read(&cert_path).await?;
let certs = rustls_pemfile::certs(&mut Cursor::new(&cert_bytes))
.collect::<Result<Vec<_>, _>>()?;
roots.add_parsable_certificates(certs);
}

rustls::ClientConfig::builder().with_root_certificates(roots)
};

Expand Down

0 comments on commit 364db2e

Please sign in to comment.