Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't panic when rustls-native-certs errors #564

Merged
merged 3 commits into from
Dec 1, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions src/rtls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,39 @@ impl Write for RustlsStream {

#[cfg(feature = "native-certs")]
fn root_certs() -> rustls::RootCertStore {
let mut root_store = rustls::RootCertStore::empty();
use log::error;

let certs = rustls_native_certs::load_native_certs().expect("Could not load platform certs");
let mut root_cert_store = rustls::RootCertStore::empty();

let mut valid_count = 0;
let mut invalid_count = 0;
let certs = rustls_native_certs::load_native_certs().unwrap_or_else(|e| {
error!("loading native certificates: {}", e);
vec![]
});
for cert in certs {
// Repackage the certificate DER bytes.
let rustls_cert = rustls::Certificate(cert.0);

root_store
.add(&rustls_cert)
.expect("Failed to add native certificate too root store");
let cert = rustls::Certificate(cert.0);
// Continue on parsing errors, as native stores often include ancient or syntactically
// invalid certificates, like root certificates without any X509 extensions.
// Inspiration: https://github.com/rustls/rustls/blob/633bf4ba9d9521a95f68766d04c22e2b01e68318/rustls/src/anchors.rs#L105-L112
match root_cert_store.add(&cert) {
Ok(_) => valid_count += 1,
Err(err) => {
invalid_count += 1;
log::warn!(
"rustls failed to parse DER certificate {:?} {:?}",
&err,
&cert
);
}
}
}

root_store
if valid_count == 0 && invalid_count > 0 {
error!(
"no valid certificates loaded by rustls-native-certs. all HTTPS requests will fail."
);
}
root_cert_store
}

#[cfg(not(feature = "native-certs"))]
Expand Down