Skip to content

Commit fac2fa9

Browse files
postgres: Use 'postgresql' ALPN for SSL connections
1 parent 2d23681 commit fac2fa9

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
@@ -46,7 +46,7 @@ async-std = { workspace = true, optional = true }
4646
tokio = { workspace = true, optional = true }
4747

4848
# TLS
49-
native-tls = { version = "0.2.10", optional = true }
49+
native-tls = { version = "0.2.10", features = ["alpn"], optional = true }
5050

5151
rustls = { version = "0.23.24", default-features = false, features = ["std", "tls12"], optional = true }
5252
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()
@@ -180,6 +180,15 @@ where
180180
}
181181
};
182182

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

185194
let mut socket = RustlsSocket {

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+
alpn_protocols: None,
6667
};
6768

6869
// 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)