Skip to content

Commit b83fe22

Browse files
committed
Don't error when native certificates fail to load if other roots are loaded
1 parent 7bb10d1 commit b83fe22

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

src/verification/others.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ impl Verifier {
7777
}
7878
}
7979

80+
// Safety: There's no way for the mutex to be locked multiple times, so this is
81+
// an infallible operation.
82+
let mut extra_roots = self.extra_roots.try_lock().unwrap();
83+
if !extra_roots.is_empty() {
84+
let count = extra_roots.len();
85+
root_store.add_trust_anchors(&mut extra_roots.drain(..));
86+
log::debug!(
87+
"Loaded {count} extra CA certificates in addition to possible system roots",
88+
);
89+
}
90+
8091
#[cfg(all(target_os = "linux", not(target_arch = "wasm32")))]
8192
match rustls_native_certs::load_native_certs() {
8293
Ok(certs) => {
@@ -92,25 +103,19 @@ impl Verifier {
92103
} else {
93104
log::debug!("Loaded {added} CA certificates from the system");
94105
}
95-
96-
// Safety: There's no way for the mutex to be locked multiple times, so this is
97-
// an infallible operation.
98-
let mut extra_roots = self.extra_roots.try_lock().unwrap();
99-
if !extra_roots.is_empty() {
100-
let count = extra_roots.len();
101-
root_store.add_trust_anchors(&mut extra_roots.drain(..));
102-
log::debug!(
103-
"Loaded {count} extra CA certificates in addition to roots from the system",
104-
);
105-
}
106106
}
107107
Err(err) => {
108108
// This only contains a path to a system directory:
109109
// https://github.com/rustls/rustls-native-certs/blob/bc13b9a6bfc2e1eec881597055ca49accddd972a/src/lib.rs#L91-L94
110-
return Err(rustls::Error::General(format!(
111-
"failed to load system root certificates: {}",
112-
err
113-
)));
110+
const MSG: &str = "failed to load system root certificates: ";
111+
112+
// Don't return an error if this fails when other roots have already been loaded via
113+
// `new_with_extra_roots`. It leads to extra failure cases where connections would otherwise still work.
114+
if root_store.is_empty() {
115+
return Err(rustls::Error::General(format!("{MSG}{err}")));
116+
} else {
117+
log::error!("{MSG}{err}");
118+
}
114119
}
115120
};
116121

0 commit comments

Comments
 (0)