Skip to content

Commit 5d96ad7

Browse files
author
Daniel Frankcom
committed
postgres: Use 'postgresql' ALPN for SSL connections
1 parent 9b94ca8 commit 5d96ad7

File tree

6 files changed

+18
-2
lines changed

6 files changed

+18
-2
lines changed

sqlx-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async-std = { workspace = true, optional = true }
3737
tokio = { workspace = true, optional = true }
3838

3939
# TLS
40-
native-tls = { version = "0.2.10", optional = true }
40+
native-tls = { version = "0.2.10", features = ["alpn"], optional = true }
4141

4242
rustls = { version = "0.23.15", default-features = false, features = ["std", "tls12"], optional = true }
4343
webpki-roots = { version = "0.26", optional = true }

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ pub struct TlsConfig<'a> {
6464
pub root_cert_path: Option<&'a CertificateInput>,
6565
pub client_cert_path: Option<&'a CertificateInput>,
6666
pub client_key_path: Option<&'a CertificateInput>,
67+
pub alpn_protocols: Option<Vec<&'a str>>,
6768
}
6869

6970
pub async fn handshake<S, Ws>(

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ pub async fn handshake<S: Socket>(
5353
builder.add_root_certificate(native_tls::Certificate::from_pem(&data).map_err(Error::tls)?);
5454
}
5555

56+
if let Some(protocols) = config.alpn_protocols {
57+
builder.request_alpns(&protocols);
58+
}
59+
5660
// authentication using user's key-file and its associated certificate
5761
if let (Some(cert_path), Some(key_path)) = (config.client_cert_path, config.client_key_path) {
5862
let cert_path = cert_path.data().await?;

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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()
@@ -183,6 +183,15 @@ where
183183
}
184184
};
185185

186+
if let Some(alpn_protocols) = tls_config.alpn_protocols {
187+
let alpn_protocols: Vec<Vec<u8>> = alpn_protocols
188+
.into_iter()
189+
.map(|s| s.as_bytes().to_vec())
190+
.collect();
191+
192+
config.alpn_protocols = alpn_protocols;
193+
}
194+
186195
let host = ServerName::try_from(tls_config.hostname.to_owned()).map_err(Error::tls)?;
187196

188197
let mut socket = RustlsSocket {

sqlx-mysql/src/connection/tls.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ pub(super) async fn maybe_upgrade<S: Socket>(
6666
root_cert_path: options.ssl_ca.as_ref(),
6767
client_cert_path: options.ssl_client_cert.as_ref(),
6868
client_key_path: options.ssl_client_key.as_ref(),
69+
alpn_protocols: None,
6970
};
7071

7172
// Request TLS upgrade

sqlx-postgres/src/connection/tls.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ async fn maybe_upgrade<S: Socket>(
8282
root_cert_path: options.ssl_root_cert.as_ref(),
8383
client_cert_path: options.ssl_client_cert.as_ref(),
8484
client_key_path: options.ssl_client_key.as_ref(),
85+
alpn_protocols: Some(vec!["postgresql"]),
8586
};
8687

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

0 commit comments

Comments
 (0)