Skip to content

Commit 0c576da

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 fc940ce commit 0c576da

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

tonic/src/transport/channel/service/tls.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ pub(crate) struct TlsConnector {
2626
}
2727

2828
impl TlsConnector {
29+
#[allow(clippy::too_many_arguments)]
2930
pub(crate) fn new(
3031
ca_certs: Vec<Certificate>,
3132
trust_anchors: Vec<TrustAnchor<'static>>,
3233
identity: Option<Identity>,
3334
domain: &str,
3435
assume_http2: bool,
36+
use_key_log: bool,
3537
#[cfg(feature = "tls-native-roots")] with_native_roots: bool,
3638
#[cfg(feature = "tls-webpki-roots")] with_webpki_roots: bool,
3739
) -> Result<Self, crate::BoxError> {
@@ -87,6 +89,10 @@ impl TlsConnector {
8789
None => builder.with_no_client_auth(),
8890
};
8991

92+
if use_key_log {
93+
config.key_log = Arc::new(tokio_rustls::rustls::KeyLogFile::new());
94+
}
95+
9096
config.alpn_protocols.push(ALPN_H2.into());
9197
Ok(Self {
9298
config: Arc::new(config),

tonic/src/transport/channel/tls.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub struct ClientTlsConfig {
1818
with_native_roots: bool,
1919
#[cfg(feature = "tls-webpki-roots")]
2020
with_webpki_roots: bool,
21+
use_key_log: bool,
2122
}
2223

2324
impl ClientTlsConfig {
@@ -84,6 +85,14 @@ impl ClientTlsConfig {
8485
}
8586
}
8687

88+
/// Use key log as specified by the `SSLKEYLOGFILE` environment variable.
89+
pub fn use_key_log(self) -> Self {
90+
ClientTlsConfig {
91+
use_key_log: true,
92+
..self
93+
}
94+
}
95+
8796
/// Enables the platform's trusted certs.
8897
#[cfg(feature = "tls-native-roots")]
8998
pub fn with_native_roots(self) -> Self {
@@ -123,6 +132,7 @@ impl ClientTlsConfig {
123132
self.identity,
124133
domain,
125134
self.assume_http2,
135+
self.use_key_log,
126136
#[cfg(feature = "tls-native-roots")]
127137
self.with_native_roots,
128138
#[cfg(feature = "tls-webpki-roots")]

tonic/src/transport/server/service/tls.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ impl TlsAcceptor {
2323
client_ca_root: Option<&Certificate>,
2424
client_auth_optional: bool,
2525
ignore_client_order: bool,
26+
use_key_log: bool,
2627
) -> Result<Self, crate::BoxError> {
2728
let builder = ServerConfig::builder();
2829

@@ -45,6 +46,10 @@ impl TlsAcceptor {
4546
let mut config = builder.with_single_cert(cert, key)?;
4647
config.ignore_client_order = ignore_client_order;
4748

49+
if use_key_log {
50+
config.key_log = Arc::new(tokio_rustls::rustls::KeyLogFile::new());
51+
}
52+
4853
config.alpn_protocols.push(ALPN_H2.into());
4954
Ok(Self {
5055
inner: Arc::new(config),

tonic/src/transport/server/tls.rs

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

1516
impl fmt::Debug for ServerTlsConfig {
@@ -64,12 +65,21 @@ impl ServerTlsConfig {
6465
}
6566
}
6667

68+
/// Use key log as specified by the `SSLKEYLOGFILE` environment variable.
69+
pub fn use_key_log(self) -> Self {
70+
ServerTlsConfig {
71+
use_key_log: true,
72+
..self
73+
}
74+
}
75+
6776
pub(crate) fn tls_acceptor(&self) -> Result<TlsAcceptor, crate::BoxError> {
6877
TlsAcceptor::new(
6978
self.identity.as_ref().unwrap(),
7079
self.client_ca_root.as_ref(),
7180
self.client_auth_optional,
7281
self.ignore_client_order,
82+
self.use_key_log,
7383
)
7484
}
7585
}

0 commit comments

Comments
 (0)