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

Load system trusted root certs for rustls #369

Merged
merged 8 commits into from
Mar 12, 2022
Merged
Changes from 1 commit
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
Next Next commit
Load system trusted root certs for rustls
Add ability to use trusted root certificates from the OS with rustls automatically.

This is a work-in-progress and very rough.
  • Loading branch information
sagebind committed Jan 8, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit c6ac27ba83fdc3fbea413599c8316318c93d8df1
27 changes: 23 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -19,25 +19,24 @@ features = ["cookies", "json", "nightly"]
status = "actively-developed"

[features]
default = ["http2", "static-curl", "text-decoding"]
default = ["http2", "native-tls", "static-curl", "text-decoding"]
cookies = ["httpdate"]
http2 = ["curl/http2"]
json = ["serde", "serde_json"]
native-tls = ["curl/ssl", "curl-sys/ssl"]
nightly = []
psl = ["httpdate", "parking_lot", "publicsuffix"]
spnego = ["curl-sys/spnego"]
static-curl = ["curl/static-curl"]
static-ssl = ["curl/static-ssl"]
text-decoding = ["encoding_rs", "mime"]
unstable-rustls-tls = ["curl/rustls"]
unstable-interceptors = []
unstable-rustls-tls = ["pem", "rustls-ffi", "rustls-native-certs", "curl/rustls"]

[dependencies]
async-channel = "1.6"
castaway = "0.1"
crossbeam-utils = "0.8"
curl = "0.4.42"
curl-sys = "0.4.52"
event-listener = "2.5"
futures-lite = "1.11"
http = "0.2.1"
@@ -49,6 +48,14 @@ sluice = "0.5.4"
url = "2.2"
waker-fn = "1"

[dependencies.curl]
version = "0.4.42"
default-features = false

[dependencies.curl-sys]
version = "0.4.52"
default-features = false

[dependencies.encoding_rs]
version = "0.8"
optional = true
@@ -65,11 +72,23 @@ optional = true
version = "0.11"
optional = true

[dependencies.pem]
version = "1"
optional = true

[dependencies.publicsuffix]
version = "2.0.6"
features = ["std"]
optional = true

[dependencies.rustls-ffi]
version = "0.8"
optional = true

[dependencies.rustls-native-certs]
version = "0.6"
optional = true

[dependencies.serde]
version = "1.0"
optional = true
2 changes: 2 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1156,6 +1156,8 @@ impl HttpClient {

easy.http_headers(headers)?;

crate::tls::init_default_tls_config(&mut easy);

Ok((easy, future))
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -261,6 +261,7 @@ mod request;
mod response;
mod task;
mod text;
mod tls;
mod trailer;

pub mod auth;
29 changes: 29 additions & 0 deletions src/tls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! TLS client configuration.

use std::{fs::File, io::Write};

use curl::easy::Easy2;

#[allow(unsafe_code)]
pub(crate) fn init_default_tls_config<H>(easy: &mut Easy2<H>) {
// Load native root certificates and add them to the handle.
#[cfg(feature = "rustls-native-certs")]
{
if let Ok(certs) = rustls_native_certs::load_native_certs() {
// TODO: The rustls backend in curl doesn't support
// `CURLOPT_CAINFO_BLOB` yet. For now use an awful hack of
// generating a PEM file on-demand for curl to use.
let mut tmp = File::create(".tmp-roots.pem").unwrap();
for cert in certs {
let pem = pem::Pem {
tag: String::from("CERTIFICATE"),
contents: cert.0,
};
tmp.write_all(pem::encode(&pem).as_bytes()).unwrap();
}
tmp.flush().unwrap();
drop(tmp);
easy.cainfo(".tmp-roots.pem").unwrap();
}
}
}