Skip to content

Commit

Permalink
Add a tls_built_in_root_certs option for Client (#1150)
Browse files Browse the repository at this point in the history
  • Loading branch information
amousset authored Jan 27, 2021
1 parent bd9ff9f commit 31b11c3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/async_impl/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ struct Config {
#[cfg(feature = "__tls")]
root_certs: Vec<Certificate>,
#[cfg(feature = "__tls")]
tls_built_in_root_certs: bool,
#[cfg(feature = "__tls")]
tls: TlsBackend,
http2_only: bool,
http1_title_case_headers: bool,
Expand Down Expand Up @@ -146,6 +148,8 @@ impl ClientBuilder {
#[cfg(feature = "__tls")]
root_certs: Vec::new(),
#[cfg(feature = "__tls")]
tls_built_in_root_certs: true,
#[cfg(feature = "__tls")]
identity: None,
#[cfg(feature = "__tls")]
tls: TlsBackend::default(),
Expand Down Expand Up @@ -209,6 +213,8 @@ impl ClientBuilder {

tls.danger_accept_invalid_certs(!config.certs_verification);

tls.disable_built_in_roots(!config.tls_built_in_root_certs);

for cert in config.root_certs {
cert.add_to_native_tls(&mut tls);
}
Expand Down Expand Up @@ -261,10 +267,12 @@ impl ClientBuilder {
tls.set_protocols(&["h2".into(), "http/1.1".into()]);
}
#[cfg(feature = "rustls-tls-webpki-roots")]
if config.tls_built_in_root_certs {
tls.root_store
.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
}
#[cfg(feature = "rustls-tls-native-roots")]
{
if config.tls_built_in_root_certs {
let roots_slice = NATIVE_ROOTS.as_ref().unwrap().roots.as_slice();
tls.root_store.roots.extend_from_slice(roots_slice);
}
Expand Down Expand Up @@ -719,6 +727,23 @@ impl ClientBuilder {
self
}

/// Controls the use of built-in/preloaded certificates during certificate validation.
///
/// Defaults to `true` -- built-in system certs will be used.
///
/// # Optional
///
/// This requires the optional `default-tls`, `native-tls`, or `rustls-tls(-...)`
/// feature to be enabled.
#[cfg(feature = "__tls")]
pub fn tls_built_in_root_certs(
mut self,
tls_built_in_root_certs: bool,
) -> ClientBuilder {
self.config.tls_built_in_root_certs = tls_built_in_root_certs;
self
}

/// Sets the identity to be used for client certificate authentication.
///
/// # Optional
Expand Down
16 changes: 16 additions & 0 deletions src/blocking/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,22 @@ impl ClientBuilder {
self.with_inner(move |inner| inner.add_root_certificate(cert))
}

/// Controls the use of built-in system certificates during certificate validation.
///
/// Defaults to `true` -- built-in system certs will be used.
///
/// # Optional
///
/// This requires the optional `default-tls`, `native-tls`, or `rustls-tls(-...)`
/// feature to be enabled.
#[cfg(feature = "__tls")]
pub fn tls_built_in_root_certs(
self,
tls_built_in_root_certs: bool,
) -> ClientBuilder {
self.with_inner(move |inner| inner.tls_built_in_root_certs(tls_built_in_root_certs))
}

/// Sets the identity to be used for client certificate authentication.
#[cfg(feature = "__tls")]
pub fn identity(self, identity: Identity) -> ClientBuilder {
Expand Down
15 changes: 15 additions & 0 deletions tests/badssl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ async fn test_badssl_self_signed() {
assert!(text.contains("<title>self-signed.badssl.com</title>"));
}

#[cfg(feature = "__tls")]
#[tokio::test]
async fn test_badssl_no_built_in_roots() {
let result = reqwest::Client::builder()
.tls_built_in_root_certs(false)
.no_proxy()
.build()
.unwrap()
.get("https://mozilla-modern.badssl.com/")
.send()
.await;

assert!(result.is_err());
}

#[cfg(feature = "native-tls")]
#[tokio::test]
async fn test_badssl_wrong_host() {
Expand Down

0 comments on commit 31b11c3

Please sign in to comment.