Skip to content

Commit fc861db

Browse files
committed
allow option for enabling the SSLKEYLOGFILE environment variable
1 parent 322021f commit fc861db

6 files changed

Lines changed: 28 additions & 2 deletions

File tree

sqlx-core/src/net/tls/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ impl std::fmt::Display for CertificateInput {
6060
pub struct TlsConfig<'a> {
6161
pub accept_invalid_certs: bool,
6262
pub accept_invalid_hostnames: bool,
63+
pub enable_keylog: bool,
6364
pub hostname: &'a str,
6465
pub root_cert_path: Option<&'a CertificateInput>,
6566
pub client_cert_path: Option<&'a CertificateInput>,

sqlx-core/src/net/tls/tls_rustls.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustls::{
1313
pem::{self, PemObject},
1414
CertificateDer, PrivateKeyDer, ServerName, UnixTime,
1515
},
16-
CertificateError, ClientConfig, ClientConnection, Error as TlsError, RootCertStore,
16+
CertificateError, ClientConfig, ClientConnection, Error as TlsError, KeyLogFile, RootCertStore,
1717
};
1818

1919
use crate::error::Error;
@@ -123,7 +123,7 @@ where
123123
}
124124
};
125125

126-
let config = if tls_config.accept_invalid_certs {
126+
let mut config = if tls_config.accept_invalid_certs {
127127
if let Some(user_auth) = user_auth {
128128
config
129129
.dangerous()
@@ -179,6 +179,9 @@ where
179179
.with_no_client_auth()
180180
}
181181
};
182+
if tls_config.enable_keylog {
183+
config.key_log = Arc::new(KeyLogFile::new());
184+
}
182185

183186
let host = ServerName::try_from(tls_config.hostname.to_owned()).map_err(Error::tls)?;
184187

sqlx-mysql/src/connection/tls.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub(super) async fn maybe_upgrade<S: Socket>(
6363
root_cert_path: options.ssl_ca.as_ref(),
6464
client_cert_path: options.ssl_client_cert.as_ref(),
6565
client_key_path: options.ssl_client_key.as_ref(),
66+
enable_keylog: options.ssl_enable_keylog,
6667
};
6768

6869
// Request TLS upgrade

sqlx-mysql/src/options/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ pub struct MySqlConnectOptions {
7171
pub(crate) ssl_ca: Option<CertificateInput>,
7272
pub(crate) ssl_client_cert: Option<CertificateInput>,
7373
pub(crate) ssl_client_key: Option<CertificateInput>,
74+
pub(crate) ssl_enable_keylog: bool,
7475
pub(crate) statement_cache_capacity: usize,
7576
pub(crate) charset: String,
7677
pub(crate) collation: Option<String>,
@@ -104,6 +105,7 @@ impl MySqlConnectOptions {
104105
ssl_ca: None,
105106
ssl_client_cert: None,
106107
ssl_client_key: None,
108+
ssl_enable_keylog: false,
107109
statement_cache_capacity: 100,
108110
log_settings: Default::default(),
109111
pipes_as_concat: true,
@@ -176,6 +178,14 @@ impl MySqlConnectOptions {
176178
self
177179
}
178180

181+
/// Enables the use of the `SSLKEYLOGFILE`` environment variable to export SSL session keys.
182+
///
183+
/// Only works with the `rustls` SSL backend
184+
pub fn ssl_enable_keylog(mut self, enable: bool) -> Self {
185+
self.ssl_enable_keylog = enable;
186+
self
187+
}
188+
179189
/// Sets the name of a file containing a list of trusted SSL Certificate Authorities.
180190
///
181191
/// # Example

sqlx-postgres/src/connection/tls.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ async fn maybe_upgrade<S: Socket>(
5858
root_cert_path: options.ssl_root_cert.as_ref(),
5959
client_cert_path: options.ssl_client_cert.as_ref(),
6060
client_key_path: options.ssl_client_key.as_ref(),
61+
enable_keylog: options.ssl_enable_keylog,
6162
};
6263

6364
tls::handshake(socket, config, SocketIntoBox).await

sqlx-postgres/src/options/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub struct PgConnectOptions {
2525
pub(crate) ssl_root_cert: Option<CertificateInput>,
2626
pub(crate) ssl_client_cert: Option<CertificateInput>,
2727
pub(crate) ssl_client_key: Option<CertificateInput>,
28+
pub(crate) ssl_enable_keylog: bool,
2829
pub(crate) statement_cache_capacity: usize,
2930
pub(crate) application_name: Option<String>,
3031
pub(crate) log_settings: LogSettings,
@@ -92,6 +93,7 @@ impl PgConnectOptions {
9293
.ok()
9394
.and_then(|v| v.parse().ok())
9495
.unwrap_or_default(),
96+
ssl_enable_keylog: false,
9597
statement_cache_capacity: 100,
9698
application_name: var("PGAPPNAME").ok(),
9799
extra_float_digits: Some("2".into()),
@@ -225,6 +227,14 @@ impl PgConnectOptions {
225227
self
226228
}
227229

230+
/// Enables the use of the `SSLKEYLOGFILE`` environment variable to export SSL session keys.
231+
///
232+
/// Only works with the `rustls` SSL backend
233+
pub fn ssl_enable_keylog(mut self, enable: bool) -> Self {
234+
self.ssl_enable_keylog = enable;
235+
self
236+
}
237+
228238
/// Sets the name of a file containing SSL certificate authority (CA) certificate(s).
229239
/// If the file exists, the server's certificate will be verified to be signed by
230240
/// one of these authorities.

0 commit comments

Comments
 (0)