Skip to content

Commit 344d493

Browse files
committed
with_native_roots: Restore robustness to unparseable certs
rustls-native-certs used to filter out invalid certs on Unix, where certs are parsed from PEM files. rustls/rustls-native-certs#26 changed it to passing them unparsed. Now that hyper-rustls does the parsing, keep being robust to invalid certs. Implementation modified from rustls::RootCertStore::add_parsable_certificates, which cannot be used directly due to a newtype in rustls-native-certs.
1 parent 6bc647f commit 344d493

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/config.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,25 @@ impl ConfigBuilderExt for ConfigBuilder<ClientConfig, WantsVerifier> {
2323
#[cfg_attr(docsrs, doc(cfg(feature = "rustls-native-certs")))]
2424
fn with_native_roots(self) -> ClientConfig {
2525
let mut roots = rustls::RootCertStore::empty();
26+
let mut valid_count = 0;
27+
let mut invalid_count = 0;
28+
2629
for cert in rustls_native_certs::load_native_certs().expect("could not load platform certs")
2730
{
28-
roots.add(&rustls::Certificate(cert.0)).unwrap();
31+
let cert = rustls::Certificate(cert.0);
32+
match roots.add(&cert) {
33+
Ok(_) => valid_count += 1,
34+
Err(err) => {
35+
log::trace!("invalid cert der {:?}", cert.0);
36+
log::debug!("certificate parsing failed: {:?}", err);
37+
invalid_count += 1
38+
}
39+
}
2940
}
30-
41+
log::debug!(
42+
"with_native_roots processed {} valid and {} invalid certs",
43+
valid_count, invalid_count
44+
);
3145
assert!(!roots.is_empty(), "no CA certificates found");
3246

3347
self.with_root_certificates(roots).with_no_client_auth()

0 commit comments

Comments
 (0)