Skip to content
Open
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
24 changes: 15 additions & 9 deletions tonic/src/transport/channel/service/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl TlsConnector {
trust_anchors: Vec<TrustAnchor<'static>>,
identity: Option<Identity>,
server_cert_verifier: Option<Arc<dyn ServerCertVerifier>>,
provider: Option<Arc<crypto::CryptoProvider>>,
domain: &str,
assume_http2: bool,
use_key_log: bool,
Expand All @@ -50,15 +51,20 @@ impl TlsConnector {
.unwrap()
}

#[allow(unreachable_patterns)]
let builder = match crypto::CryptoProvider::get_default() {
Some(provider) => with_provider(provider.clone()),
#[cfg(feature = "tls-ring")]
None => with_provider(Arc::new(crypto::ring::default_provider())),
#[cfg(feature = "tls-aws-lc")]
None => with_provider(Arc::new(crypto::aws_lc_rs::default_provider())),
// somehow tls is enabled, but neither of the crypto features are enabled.
_ => ClientConfig::builder(),
let builder = match provider {
Some(provider) => with_provider(provider),
None => {
#[allow(unreachable_patterns)]
match crypto::CryptoProvider::get_default() {
Some(provider) => with_provider(provider.clone()),
#[cfg(feature = "tls-ring")]
None => with_provider(Arc::new(crypto::ring::default_provider())),
#[cfg(feature = "tls-aws-lc")]
None => with_provider(Arc::new(crypto::aws_lc_rs::default_provider())),
// somehow tls is enabled, but neither of the crypto features are enabled.
_ => ClientConfig::builder(),
}
}
};

let builder = match server_cert_verifier {
Expand Down
32 changes: 32 additions & 0 deletions tonic/src/transport/channel/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use http::Uri;
use std::sync::Arc;
use std::time::Duration;
use tokio_rustls::rustls::client::danger::ServerCertVerifier;
use tokio_rustls::rustls::crypto::CryptoProvider;
use tokio_rustls::rustls::pki_types::TrustAnchor;

/// Configures TLS settings for endpoints.
Expand All @@ -23,6 +24,7 @@ pub struct ClientTlsConfig {
with_webpki_roots: bool,
use_key_log: bool,
timeout: Option<Duration>,
provider: Option<Arc<CryptoProvider>>,
}

impl ClientTlsConfig {
Expand Down Expand Up @@ -135,6 +137,19 @@ impl ClientTlsConfig {
}
}

/// Sets a custom rustls [`CryptoProvider`] used to build this client's TLS
/// configuration.
///
/// When unset, the process-wide default installed via
/// `CryptoProvider::install_default` is used, falling back to the provider
/// selected by the `tls-ring` or `tls-aws-lc` feature.
pub fn with_provider(self, provider: Arc<CryptoProvider>) -> Self {
ClientTlsConfig {
provider: Some(provider),
..self
}
}

pub(crate) fn into_tls_connector(self, uri: &Uri) -> Result<TlsConnector, crate::BoxError> {
self.build_tls_connector(uri, None)
}
Expand All @@ -161,6 +176,7 @@ impl ClientTlsConfig {
self.trust_anchors,
self.identity,
server_cert_verifier,
self.provider,
domain,
self.assume_http2,
self.use_key_log,
Expand All @@ -172,3 +188,19 @@ impl ClientTlsConfig {
)
}
}

#[cfg(all(test, feature = "tls-ring"))]
mod tests {
use super::*;
use tokio_rustls::rustls::crypto::ring;

#[test]
fn with_provider_is_used_to_build_the_connector() {
// A connector configured with an explicit provider builds successfully
// and does not depend on a process-wide default provider being installed.
let connector = ClientTlsConfig::new()
.with_provider(Arc::new(ring::default_provider()))
.into_tls_connector(&Uri::from_static("https://example.com"));
assert!(connector.is_ok());
}
}
9 changes: 7 additions & 2 deletions tonic/src/transport/server/service/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tokio::io::{AsyncRead, AsyncWrite};
use tokio::time;
use tokio_rustls::{
TlsAcceptor as RustlsAcceptor,
rustls::{RootCertStore, ServerConfig, server::WebPkiClientVerifier},
rustls::{RootCertStore, ServerConfig, crypto::CryptoProvider, server::WebPkiClientVerifier},
server::TlsStream,
};

Expand All @@ -29,8 +29,13 @@ impl TlsAcceptor {
ignore_client_order: bool,
use_key_log: bool,
timeout: Option<Duration>,
provider: Option<Arc<CryptoProvider>>,
) -> Result<Self, crate::BoxError> {
let builder = ServerConfig::builder();
let builder = match provider {
Some(provider) => ServerConfig::builder_with_provider(provider)
.with_safe_default_protocol_versions()?,
None => ServerConfig::builder(),
};

let builder = match client_ca_root {
None => builder.with_no_client_auth(),
Expand Down
19 changes: 18 additions & 1 deletion tonic/src/transport/server/tls.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{fmt, time::Duration};
use std::{fmt, sync::Arc, time::Duration};

use tokio_rustls::rustls::crypto::CryptoProvider;

use super::service::TlsAcceptor;
use crate::transport::tls::{Certificate, Identity};
Expand All @@ -12,6 +14,7 @@ pub struct ServerTlsConfig {
ignore_client_order: bool,
use_key_log: bool,
timeout: Option<Duration>,
provider: Option<Arc<CryptoProvider>>,
}

impl fmt::Debug for ServerTlsConfig {
Expand Down Expand Up @@ -82,6 +85,19 @@ impl ServerTlsConfig {
}
}

/// Sets a custom rustls [`CryptoProvider`] used to build this server's TLS
/// configuration.
///
/// When unset, the process-wide default installed via
/// `CryptoProvider::install_default` is used, falling back to the provider
/// selected by the `tls-ring` or `tls-aws-lc` feature.
pub fn with_provider(self, provider: Arc<CryptoProvider>) -> Self {
ServerTlsConfig {
provider: Some(provider),
..self
}
}

pub(crate) fn tls_acceptor(&self) -> Result<TlsAcceptor, crate::BoxError> {
TlsAcceptor::new(
self.identity.as_ref().unwrap(),
Expand All @@ -90,6 +106,7 @@ impl ServerTlsConfig {
self.ignore_client_order,
self.use_key_log,
self.timeout,
self.provider.clone(),
)
}
}
Loading