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

Switch webpki to rustls-native-certs #558

Merged
merged 1 commit into from
Jul 9, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion async-nats/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ itoa = "1"
url = "2"
tokio-rustls = "0.23"
rustls-pemfile = "0.3.0"
webpki-roots = "0.22"
nuid = "0.3.2"
serde_nanos = "0.1.1"
time = { version = "0.3.6", features = ["parsing", "formatting", "serde", "serde-well-known"] }
rustls-native-certs = "0.6.2"

[dev-dependencies]
criterion = { version = "0.3", features = ["async_tokio"]}
Expand Down
25 changes: 16 additions & 9 deletions async-nats/src/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::fs::File;
use std::io::{self, BufReader, ErrorKind};
use std::path::PathBuf;
use tokio_rustls::rustls::{self, Certificate, OwnedTrustAnchor, PrivateKey};
use tokio_rustls::webpki;
use tokio_rustls::webpki::TrustAnchor;

/// Loads client certificates from a `.pem` file.
/// If the pem file is found, but does not contain any certificates, it will return
Expand Down Expand Up @@ -71,14 +71,21 @@ pub(crate) struct TlsOptions {

pub(crate) async fn config_tls(options: &TlsOptions) -> io::Result<rustls::ClientConfig> {
let mut root_store = rustls::RootCertStore::empty();
// adds Mozilla root certs
root_store.add_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| {
OwnedTrustAnchor::from_subject_spki_name_constraints(
ta.subject,
ta.spki,
ta.name_constraints,
for cert in rustls_native_certs::load_native_certs().map_err(|err| {
io::Error::new(
ErrorKind::Other,
format!("could not load platform certs: {}", err),
)
}));
})? {
root_store
.add(&rustls::Certificate(cert.0))
.map_err(|err| {
io::Error::new(
ErrorKind::Other,
format!("failed to read root certificates: {}", err),
)
})?;
}

// use provided ClientConfig or built it from options.
let tls_config = {
Expand All @@ -90,7 +97,7 @@ pub(crate) async fn config_tls(options: &TlsOptions) -> io::Result<rustls::Clien
let mut pem = BufReader::new(File::open(cafile)?);
let certs = rustls_pemfile::certs(&mut pem)?;
let trust_anchors = certs.iter().map(|cert| {
let ta = webpki::TrustAnchor::try_from_cert_der(&cert[..])
let ta = TrustAnchor::try_from_cert_der(&cert[..])
.map_err(|err| {
io::Error::new(
ErrorKind::InvalidInput,
Expand Down