Skip to content

Commit 66aa283

Browse files
committed
feat: optional SSLKEYLOGFILE support
Add a `use_key_log` option to server and client TLS configs that -- when set -- will enable rustls's `SSLKEYLOGFILE` handling. This is helpful when you want to intercept TLS traffic for debugging and is generally supported by many libraries and browsers. Also see: https://wiki.wireshark.org/TLS#using-the-pre-master-secret
1 parent d5c14fa commit 66aa283

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

tonic/src/transport/channel/tls.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub struct ClientTlsConfig {
1212
domain: Option<String>,
1313
cert: Option<Certificate>,
1414
identity: Option<Identity>,
15+
use_key_log: bool,
1516
}
1617

1718
impl fmt::Debug for ClientTlsConfig {
@@ -31,6 +32,7 @@ impl ClientTlsConfig {
3132
domain: None,
3233
cert: None,
3334
identity: None,
35+
use_key_log: false,
3436
}
3537
}
3638

@@ -58,11 +60,24 @@ impl ClientTlsConfig {
5860
}
5961
}
6062

63+
/// Use key log as specified by the `SSLKEYLOGFILE` environment variable.
64+
pub fn use_key_log(self) -> Self {
65+
ClientTlsConfig {
66+
use_key_log: true,
67+
..self
68+
}
69+
}
70+
6171
pub(crate) fn tls_connector(&self, uri: Uri) -> Result<TlsConnector, crate::Error> {
6272
let domain = match &self.domain {
6373
None => uri.host().ok_or_else(Error::new_invalid_uri)?.to_string(),
6474
Some(domain) => domain.clone(),
6575
};
66-
TlsConnector::new(self.cert.clone(), self.identity.clone(), domain)
76+
TlsConnector::new(
77+
self.cert.clone(),
78+
self.identity.clone(),
79+
domain,
80+
self.use_key_log,
81+
)
6782
}
6883
}

tonic/src/transport/server/tls.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub struct ServerTlsConfig {
1010
identity: Option<Identity>,
1111
client_ca_root: Option<Certificate>,
1212
client_auth_optional: bool,
13+
use_key_log: bool,
1314
}
1415

1516
impl fmt::Debug for ServerTlsConfig {
@@ -25,6 +26,7 @@ impl ServerTlsConfig {
2526
identity: None,
2627
client_ca_root: None,
2728
client_auth_optional: false,
29+
use_key_log: false,
2830
}
2931
}
3032

@@ -57,11 +59,20 @@ impl ServerTlsConfig {
5759
}
5860
}
5961

62+
/// Use key log as specified by the `SSLKEYLOGFILE` environment variable.
63+
pub fn use_key_log(self) -> Self {
64+
ServerTlsConfig {
65+
use_key_log: true,
66+
..self
67+
}
68+
}
69+
6070
pub(crate) fn tls_acceptor(&self) -> Result<TlsAcceptor, crate::Error> {
6171
TlsAcceptor::new(
6272
self.identity.clone().unwrap(),
6373
self.client_ca_root.clone(),
6474
self.client_auth_optional,
75+
self.use_key_log,
6576
)
6677
}
6778
}

tonic/src/transport/service/connector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<C> Connector<C> {
5151

5252
host.try_into()
5353
.ok()
54-
.and_then(|dns| TlsConnector::new(None, None, dns).ok())
54+
.and_then(|dns| TlsConnector::new(None, None, dns, false).ok())
5555
}
5656
}
5757

tonic/src/transport/service/tls.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ impl TlsConnector {
3131
ca_cert: Option<Certificate>,
3232
identity: Option<Identity>,
3333
domain: String,
34+
use_key_log: bool,
3435
) -> Result<Self, crate::Error> {
3536
let builder = ClientConfig::builder().with_safe_defaults();
3637
let mut roots = RootCertStore::empty();
@@ -61,6 +62,11 @@ impl TlsConnector {
6162
};
6263

6364
config.alpn_protocols.push(ALPN_H2.as_bytes().to_vec());
65+
66+
if use_key_log {
67+
config.key_log = Arc::new(rustls::KeyLogFile::new());
68+
}
69+
6470
Ok(Self {
6571
config: Arc::new(config),
6672
domain: Arc::new(domain.as_str().try_into()?),
@@ -106,6 +112,7 @@ impl TlsAcceptor {
106112
identity: Identity,
107113
client_ca_root: Option<Certificate>,
108114
client_auth_optional: bool,
115+
use_key_log: bool,
109116
) -> Result<Self, crate::Error> {
110117
let builder = ServerConfig::builder().with_safe_defaults();
111118

@@ -131,6 +138,11 @@ impl TlsAcceptor {
131138
let mut config = builder.with_single_cert(cert, key)?;
132139

133140
config.alpn_protocols.push(ALPN_H2.as_bytes().to_vec());
141+
142+
if use_key_log {
143+
config.key_log = Arc::new(rustls::KeyLogFile::new());
144+
}
145+
134146
Ok(Self {
135147
inner: Arc::new(config),
136148
})

0 commit comments

Comments
 (0)