Skip to content

Commit

Permalink
feat(transport): Add a tls-webpki-roots feature to add trust roots fr…
Browse files Browse the repository at this point in the history
…om webpki-roots (#660)
  • Loading branch information
joshtriplett committed May 24, 2021
1 parent 4d2667d commit 32173dc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
5 changes: 4 additions & 1 deletion tonic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ transport = [
"tokio/time",
]
tls = ["transport", "tokio-rustls"]
tls-roots = ["tls", "rustls-native-certs"]
tls-roots-common = ["tls"]
tls-roots = ["tls-roots-common", "rustls-native-certs"]
tls-webpki-roots = ["tls-roots-common", "webpki-roots"]
prost = ["prost1", "prost-derive"]

# [[bench]]
Expand Down Expand Up @@ -76,6 +78,7 @@ tracing-futures = { version = "0.2", optional = true }
# rustls
tokio-rustls = { version = "0.22", optional = true }
rustls-native-certs = { version = "0.5", optional = true }
webpki-roots = { version = "0.21.1", optional = true }

[dev-dependencies]
tokio = { version = "1.0", features = ["rt", "macros"] }
Expand Down
2 changes: 2 additions & 0 deletions tonic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
//! - `tls-roots`: Adds system trust roots to `rustls`-based gRPC clients using the
//! `rustls-native-certs` crate. Not enabled by default. `tls` must be enabled to use
//! `tls-roots`.
//! - `tls-webpki-roots`: Add the standard trust roots from the `webpki-roots` crate to
//! `rustls`-based gRPC clients. Not enabled by default.
//! - `prost`: Enables the [`prost`] based gRPC [`Codec`] implementation.
//!
//! # Structure
Expand Down
6 changes: 3 additions & 3 deletions tonic/src/transport/service/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<C> Connector<C> {
Self { inner, tls }
}

#[cfg(feature = "tls-roots")]
#[cfg(feature = "tls-roots-common")]
fn tls_or_default(&self, scheme: Option<&str>, host: Option<&str>) -> Option<TlsConnector> {
use tokio_rustls::webpki::DNSNameRef;

Expand Down Expand Up @@ -74,10 +74,10 @@ where
}

fn call(&mut self, uri: Uri) -> Self::Future {
#[cfg(all(feature = "tls", not(feature = "tls-roots")))]
#[cfg(all(feature = "tls", not(feature = "tls-roots-common")))]
let tls = self.tls.clone();

#[cfg(feature = "tls-roots")]
#[cfg(feature = "tls-roots-common")]
let tls = self.tls_or_default(uri.scheme_str(), uri.host());

let connect = self.inner.make_connection(uri);
Expand Down
7 changes: 7 additions & 0 deletions tonic/src/transport/service/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ impl TlsConnector {
};
}

#[cfg(feature = "tls-webpki-roots")]
{
config
.root_store
.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
}

if let Some(cert) = ca_cert {
let mut buf = std::io::Cursor::new(&cert.pem[..]);
config.root_store.add_pem_file(&mut buf).unwrap();
Expand Down

0 comments on commit 32173dc

Please sign in to comment.