Skip to content
Closed
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
6 changes: 6 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ $ cargo run --bin tls-client
$ cargo run --bin tls-server
```

For [Wireshark TLS decryption](https://gitlab.com/wireshark/wireshark/-/wikis/TLS#using-the-pre-master-secret)

```bash
$ SSLKEYLOGFILE=./keylogfile.txt cargo run --bin tls-server
```

## Health Checking

### Server
Expand Down
6 changes: 5 additions & 1 deletion examples/src/tls/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let server = EchoServer::default();

Server::builder()
.tls_config(ServerTlsConfig::new().identity(identity))?
.tls_config(
ServerTlsConfig::new()
.identity(identity)
.install_key_log_file(cfg!(debug_assertions)),
)?
.add_service(pb::echo_server::EchoServer::new(server))
.serve(addr)
.await?;
Expand Down
16 changes: 15 additions & 1 deletion tonic/src/transport/server/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::fmt;
pub struct ServerTlsConfig {
identity: Option<Identity>,
client_ca_root: Option<Certificate>,
install_key_log_file: bool,
}

#[cfg(feature = "tls")]
Expand All @@ -27,6 +28,7 @@ impl ServerTlsConfig {
ServerTlsConfig {
identity: None,
client_ca_root: None,
install_key_log_file: false,
}
}

Expand All @@ -46,7 +48,19 @@ impl ServerTlsConfig {
}
}

/// Per session TLS secrets will be written to a file given by the SSLKEYLOGFILE environment variable.
pub fn install_key_log_file(self, install_key_log_file: bool) -> Self {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something that is dangerous and we'd want to put behind a dangerous feature flag?

ServerTlsConfig {
install_key_log_file,
..self
}
}

pub(crate) fn tls_acceptor(&self) -> Result<TlsAcceptor, crate::Error> {
TlsAcceptor::new(self.identity.clone().unwrap(), self.client_ca_root.clone())
TlsAcceptor::new(
self.identity.clone().unwrap(),
self.client_ca_root.clone(),
self.install_key_log_file,
)
}
}
7 changes: 7 additions & 0 deletions tonic/src/transport/service/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ impl TlsAcceptor {
pub(crate) fn new(
identity: Identity,
client_ca_root: Option<Certificate>,
install_key_log_file: bool,
) -> Result<Self, crate::Error> {
use tokio_rustls::rustls::KeyLogFile;

let builder = ServerConfig::builder().with_safe_defaults();

let builder = match client_ca_root {
Expand All @@ -143,6 +146,10 @@ impl TlsAcceptor {
let (cert, key) = rustls_keys::load_identity(identity)?;
let mut config = builder.with_single_cert(cert, key)?;

if install_key_log_file {
config.key_log = Arc::new(KeyLogFile::new());
}

config.alpn_protocols.push(ALPN_H2.as_bytes().to_vec());
Ok(Self {
inner: Arc::new(config),
Expand Down