Skip to content

Commit 4548997

Browse files
authored
feat(tls): upgrade to tokio-rustls 0.23 (rustls 0.20) (#859)
1 parent daf406d commit 4548997

File tree

6 files changed

+87
-128
lines changed

6 files changed

+87
-128
lines changed

tonic/Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ codegen = ["async-trait"]
2727
compression = ["flate2"]
2828
default = ["transport", "codegen", "prost"]
2929
prost = ["prost1", "prost-derive"]
30-
tls = ["transport", "tokio-rustls"]
30+
tls = ["rustls-pemfile", "transport", "tokio-rustls"]
3131
tls-roots = ["tls-roots-common", "rustls-native-certs"]
3232
tls-roots-common = ["tls"]
3333
tls-webpki-roots = ["tls-roots-common", "webpki-roots"]
@@ -79,9 +79,10 @@ tower = {version = "0.4.7", features = ["balance", "buffer", "discover", "limit"
7979
tracing-futures = {version = "0.2", optional = true}
8080

8181
# rustls
82-
rustls-native-certs = {version = "0.5", optional = true}
83-
tokio-rustls = {version = "0.22", optional = true}
84-
webpki-roots = {version = "0.21.1", optional = true}
82+
rustls-pemfile = { version = "0.2.1", optional = true }
83+
rustls-native-certs = { version = "0.6.1", optional = true }
84+
tokio-rustls = { version = "0.23.1", optional = true }
85+
webpki-roots = { version = "0.22.1", optional = true }
8586

8687
# compression
8788
flate2 = {version = "1.0", optional = true}

tonic/src/transport/channel/tls.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub struct ClientTlsConfig {
1414
domain: Option<String>,
1515
cert: Option<Certificate>,
1616
identity: Option<Identity>,
17-
rustls_raw: Option<tokio_rustls::rustls::ClientConfig>,
1817
}
1918

2019
#[cfg(feature = "tls")]
@@ -36,7 +35,6 @@ impl ClientTlsConfig {
3635
domain: None,
3736
cert: None,
3837
identity: None,
39-
rustls_raw: None,
4038
}
4139
}
4240

@@ -49,8 +47,6 @@ impl ClientTlsConfig {
4947
}
5048

5149
/// Sets the CA Certificate against which to verify the server's TLS certificate.
52-
///
53-
/// This has no effect if `rustls_client_config` is used to configure Rustls.
5450
pub fn ca_certificate(self, ca_certificate: Certificate) -> Self {
5551
ClientTlsConfig {
5652
cert: Some(ca_certificate),
@@ -59,35 +55,18 @@ impl ClientTlsConfig {
5955
}
6056

6157
/// Sets the client identity to present to the server.
62-
///
63-
/// This has no effect if `rustls_client_config` is used to configure Rustls.
6458
pub fn identity(self, identity: Identity) -> Self {
6559
ClientTlsConfig {
6660
identity: Some(identity),
6761
..self
6862
}
6963
}
7064

71-
/// Use options specified by the given `ClientConfig` to configure TLS.
72-
///
73-
/// This overrides all other TLS options set via other means.
74-
pub fn rustls_client_config(self, config: tokio_rustls::rustls::ClientConfig) -> Self {
75-
ClientTlsConfig {
76-
rustls_raw: Some(config),
77-
..self
78-
}
79-
}
80-
8165
pub(crate) fn tls_connector(&self, uri: Uri) -> Result<TlsConnector, crate::Error> {
8266
let domain = match &self.domain {
8367
None => uri.host().ok_or_else(Error::new_invalid_uri)?.to_string(),
8468
Some(domain) => domain.clone(),
8569
};
86-
match &self.rustls_raw {
87-
None => {
88-
TlsConnector::new_with_rustls_cert(self.cert.clone(), self.identity.clone(), domain)
89-
}
90-
Some(c) => TlsConnector::new_with_rustls_raw(c.clone(), domain),
91-
}
70+
TlsConnector::new(self.cert.clone(), self.identity.clone(), domain)
9271
}
9372
}

tonic/src/transport/server/conn.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::transport::Certificate;
77
#[cfg(feature = "tls")]
88
use std::sync::Arc;
99
#[cfg(feature = "tls")]
10-
use tokio_rustls::{rustls::Session, server::TlsStream};
10+
use tokio_rustls::server::TlsStream;
1111

1212
/// Trait that connected IO resources implement and use to produce info about the connection.
1313
///
@@ -115,10 +115,10 @@ where
115115
let (inner, session) = self.get_ref();
116116
let inner = inner.connect_info();
117117

118-
let certs = if let Some(certs) = session.get_peer_certificates() {
118+
let certs = if let Some(certs) = session.peer_certificates() {
119119
let certs = certs
120120
.into_iter()
121-
.map(|c| Certificate::from_pem(c.0))
121+
.map(|c| Certificate::from_pem(c))
122122
.collect();
123123
Some(Arc::new(certs))
124124
} else {

tonic/src/transport/server/tls.rs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use std::fmt;
1111
pub struct ServerTlsConfig {
1212
identity: Option<Identity>,
1313
client_ca_root: Option<Certificate>,
14-
rustls_raw: Option<tokio_rustls::rustls::ServerConfig>,
1514
}
1615

1716
#[cfg(feature = "tls")]
@@ -28,7 +27,6 @@ impl ServerTlsConfig {
2827
ServerTlsConfig {
2928
identity: None,
3029
client_ca_root: None,
31-
rustls_raw: None,
3230
}
3331
}
3432

@@ -48,24 +46,7 @@ impl ServerTlsConfig {
4846
}
4947
}
5048

51-
/// Use options specified by the given `ServerConfig` to configure TLS.
52-
///
53-
/// This overrides all other TLS options set via other means.
54-
pub fn rustls_server_config(
55-
&mut self,
56-
config: tokio_rustls::rustls::ServerConfig,
57-
) -> &mut Self {
58-
self.rustls_raw = Some(config);
59-
self
60-
}
61-
6249
pub(crate) fn tls_acceptor(&self) -> Result<TlsAcceptor, crate::Error> {
63-
match &self.rustls_raw {
64-
None => TlsAcceptor::new_with_rustls_identity(
65-
self.identity.clone().unwrap(),
66-
self.client_ca_root.clone(),
67-
),
68-
Some(config) => TlsAcceptor::new_with_rustls_raw(config.clone()),
69-
}
50+
TlsAcceptor::new(self.identity.clone().unwrap(), self.client_ca_root.clone())
7051
}
7152
}

tonic/src/transport/service/connector.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use super::io::BoxedIo;
33
#[cfg(feature = "tls")]
44
use super::tls::TlsConnector;
55
use http::Uri;
6+
#[cfg(feature = "tls-roots-common")]
7+
use std::convert::TryInto;
68
use std::task::{Context, Poll};
79
use tower::make::MakeConnection;
810
use tower_service::Service;
@@ -39,22 +41,18 @@ impl<C> Connector<C> {
3941

4042
#[cfg(feature = "tls-roots-common")]
4143
fn tls_or_default(&self, scheme: Option<&str>, host: Option<&str>) -> Option<TlsConnector> {
42-
use tokio_rustls::webpki::DNSNameRef;
43-
4444
if self.tls.is_some() {
4545
return self.tls.clone();
4646
}
4747

48-
match (scheme, host) {
49-
(Some("https"), Some(host)) => {
50-
if DNSNameRef::try_from_ascii(host.as_bytes()).is_ok() {
51-
TlsConnector::new_with_rustls_cert(None, None, host.to_owned()).ok()
52-
} else {
53-
None
54-
}
55-
}
56-
_ => None,
57-
}
48+
let host = match (scheme, host) {
49+
(Some("https"), Some(host)) => host,
50+
_ => return None,
51+
};
52+
53+
host.try_into()
54+
.ok()
55+
.and_then(|dns| TlsConnector::new(None, None, dns).ok())
5856
}
5957
}
6058

0 commit comments

Comments
 (0)