Skip to content

Commit

Permalink
examples: configure KeyLogFile for all examples.
Browse files Browse the repository at this point in the history
The example programs distributed with Rustls are often used to
demonstrate TLS features, or debug issues, that require being able to
view plaintext data captured in a pcap (e.g. with Wireshark).

To make this convenient this commit updates the examples (minus the MIO
examples that already did so ) to configure Rustls with a `KeyLogFile`
implementation of the `KeyLog` trait. Users can then specify the
`SSLKEYLOGFILE` environment variable to log the required session secrets
for use with their pcap tool of choice.
  • Loading branch information
cpu authored and ctz committed Aug 29, 2023
1 parent 0f5deca commit c29c75b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
23 changes: 13 additions & 10 deletions examples/src/bin/server_acceptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,19 @@ impl TestPki {
// Build a server config using the fresh verifier. If necessary, this could be customized
// based on the ClientHello (e.g. selecting a different certificate, or customizing
// supported algorithms/protocol versions).
Arc::new(
ServerConfig::builder()
.with_safe_defaults()
.with_client_cert_verifier(verifier)
.with_single_cert(
vec![Certificate(self.server_cert_der.clone())],
PrivateKey(self.server_key_der.clone()),
)
.unwrap(),
)
let mut server_config = ServerConfig::builder()
.with_safe_defaults()
.with_client_cert_verifier(verifier)
.with_single_cert(
vec![Certificate(self.server_cert_der.clone())],
PrivateKey(self.server_key_der.clone()),
)
.unwrap();

// Allow using SSLKEYLOGFILE.
server_config.key_log = Arc::new(rustls::KeyLogFile::new());

Arc::new(server_config)
}

/// Issue a certificate revocation list (CRL) for the revoked `serials` provided (may be empty).
Expand Down
3 changes: 3 additions & 0 deletions examples/src/bin/simple_0rtt_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ fn main() {
.with_root_certificates(root_store)
.with_no_client_auth();

// Allow using SSLKEYLOGFILE.
config.key_log = Arc::new(rustls::KeyLogFile::new());

// Enable early data.
config.enable_early_data = true;
let config = Arc::new(config);
Expand Down
5 changes: 4 additions & 1 deletion examples/src/bin/simpleclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ fn main() {
)
}),
);
let config = rustls::ClientConfig::<Ring>::builder()
let mut config = rustls::ClientConfig::<Ring>::builder()
.with_safe_defaults()
.with_root_certificates(root_store)
.with_no_client_auth();

// Allow using SSLKEYLOGFILE.
config.key_log = Arc::new(rustls::KeyLogFile::new());

let server_name = "www.rust-lang.org".try_into().unwrap();
let mut conn = rustls::ClientConnection::new(Arc::new(config), server_name).unwrap();
let mut sock = TcpStream::connect("www.rust-lang.org:443").unwrap();
Expand Down

0 comments on commit c29c75b

Please sign in to comment.