tlspuffin/tls/rustls/key_log.rs
1/// This trait represents the ability to do something useful
2/// with key material, such as logging it to a file for debugging.
3///
4/// Naturally, secrets passed over the interface are *extremely*
5/// sensitive and can break the security of past, present and
6/// future sessions.
7///
8/// You'll likely want some interior mutability in your
9/// implementation to make this useful.
10pub trait KeyLog: Send + Sync {
11 /// Log the given `secret`. `client_random` is provided for
12 /// session identification. `label` describes precisely what
13 /// `secret` means:
14 ///
15 /// - `CLIENT_RANDOM`: `secret` is the master secret for a TLSv1.2 session.
16 /// - `CLIENT_EARLY_TRAFFIC_SECRET`: `secret` encrypts early data transmitted by a client
17 /// - `SERVER_HANDSHAKE_TRAFFIC_SECRET`: `secret` encrypts handshake messages from the server
18 /// during a TLSv1.3 handshake.
19 /// - `CLIENT_HANDSHAKE_TRAFFIC_SECRET`: `secret` encrypts handshake messages from the client
20 /// during a TLSv1.3 handshake.
21 /// - `SERVER_TRAFFIC_SECRET_0`: `secret` encrypts post-handshake data from the server in a
22 /// TLSv1.3 session.
23 /// - `CLIENT_TRAFFIC_SECRET_0`: `secret` encrypts post-handshake data from the client in a
24 /// TLSv1.3 session.
25 /// - `EXPORTER_SECRET`: `secret` is the post-handshake exporter secret in a TLSv1.3 session.
26 ///
27 /// These strings are selected to match the NSS key log format:
28 /// <https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format>
29 fn log(&self, label: &str, client_random: &[u8], secret: &[u8]);
30
31 /// Indicates whether the secret with label `label` will be logged.
32 ///
33 /// If `will_log` returns true then `log` will be called with the secret.
34 /// Otherwise, `log` will not be called for the secret. This is a
35 /// performance optimization.
36 fn will_log(&self, _label: &str) -> bool {
37 true
38 }
39}
40
41/// KeyLog that does exactly nothing.
42pub struct NoKeyLog;
43
44impl KeyLog for NoKeyLog {
45 fn log(&self, _: &str, _: &[u8], _: &[u8]) {}
46
47 #[inline]
48 fn will_log(&self, _label: &str) -> bool {
49 false
50 }
51}