sqlx_core/postgres/connection/
tls.rs1use bytes::Bytes;
2
3use crate::error::Error;
4use crate::postgres::connection::stream::PgStream;
5use crate::postgres::message::SslRequest;
6use crate::postgres::{PgConnectOptions, PgSslMode};
7
8pub(super) async fn maybe_upgrade(
9 stream: &mut PgStream,
10 options: &PgConnectOptions,
11) -> Result<(), Error> {
12 match options.ssl_mode {
14 PgSslMode::Allow | PgSslMode::Disable => {}
16
17 PgSslMode::Prefer => {
18 upgrade(stream, options).await?;
20 }
21
22 PgSslMode::Require | PgSslMode::VerifyFull | PgSslMode::VerifyCa => {
23 if !upgrade(stream, options).await? {
24 return Err(Error::Tls("server does not support TLS".into()));
26 }
27 }
28 }
29
30 Ok(())
31}
32
33async fn upgrade(stream: &mut PgStream, options: &PgConnectOptions) -> Result<bool, Error> {
34 stream.send(SslRequest).await?;
40
41 match stream.read::<Bytes>(1).await?[0] {
45 b'S' => {
46 }
48
49 b'N' => {
50 return Ok(false);
52 }
53
54 other => {
55 return Err(err_protocol!(
56 "unexpected response from SSLRequest: 0x{:02x}",
57 other
58 ));
59 }
60 }
61
62 let accept_invalid_certs = !matches!(
63 options.ssl_mode,
64 PgSslMode::VerifyCa | PgSslMode::VerifyFull
65 );
66 let accept_invalid_hostnames = !matches!(options.ssl_mode, PgSslMode::VerifyFull);
67
68 stream
69 .upgrade(
70 &options.host,
71 accept_invalid_certs,
72 accept_invalid_hostnames,
73 options.ssl_root_cert.as_ref(),
74 )
75 .await?;
76
77 Ok(true)
78}