| 1 | use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::errorrust-analyzer cargo sqlx-postgres 0.8.3 error/::Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#; |
| 2 | use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::netrust-analyzer cargo sqlx-core 0.8.3 net/::tlsrust-analyzer cargo sqlx-core 0.8.3 net/tls/::{selfrust-analyzer cargo sqlx-core 0.8.3 net/tls/, TlsConfigrust-analyzer cargo sqlx-core 0.8.3 net/tls/TlsConfig#}; |
| 3 | use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::netrust-analyzer cargo sqlx-core 0.8.3 net/::{Socketrust-analyzer cargo sqlx-core 0.8.3 net/socket/Socket#, SocketIntoBoxrust-analyzer cargo sqlx-core 0.8.3 net/socket/SocketIntoBox#, WithSocketrust-analyzer cargo sqlx-core 0.8.3 net/socket/WithSocket#}; |
| 4 | |
| 5 | use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::messagerust-analyzer cargo sqlx-postgres 0.8.3 message/::SslRequestrust-analyzer cargo sqlx-postgres 0.8.3 message/ssl_request/SslRequest#; |
| 6 | use craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::{PgConnectOptionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#, PgSslModerust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#}; |
| 7 | |
| 8 | pub struct MaybeUpgradeTlsrust-analyzer cargo sqlx-postgres 0.8.3 connection/tls/MaybeUpgradeTls#<'a>(pub &'a PgConnectOptionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#); |
| 9 | |
| 10 | impl<'a> WithSocketrust-analyzer cargo sqlx-core 0.8.3 net/socket/WithSocket# for MaybeUpgradeTlsrust-analyzer cargo sqlx-postgres 0.8.3 connection/tls/MaybeUpgradeTls#<'a> { |
| 11 | type Outputrust-analyzer cargo sqlx-postgres 0.8.3 connection/tls/impl#[`MaybeUpgradeTls<'a>`][WithSocket]Output# = craterust-analyzer cargo sqlx-postgres 0.8.3 crate/::Resultrust-analyzer cargo sqlx-core 0.8.3 error/Result#<Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn Socketrust-analyzer cargo sqlx-core 0.8.3 net/socket/Socket#>>; |
| 12 | |
| 13 | async fn with_socketrust-analyzer cargo sqlx-postgres 0.8.3 connection/tls/impl#[`MaybeUpgradeTls<'a>`][WithSocket]with_socket().<S: Socketrust-analyzer cargo sqlx-core 0.8.3 net/socket/Socket#>(self, socket: S) -> Selfrust-analyzer cargo sqlx-postgres 0.8.3 connection/tls/impl#[`MaybeUpgradeTls<'a>`][WithSocket]::Outputrust-analyzer cargo sqlx-core 0.8.3 net/socket/WithSocket#Output# { |
| 14 | maybe_upgraderust-analyzer cargo sqlx-postgres 0.8.3 connection/tls/maybe_upgrade().(socket, self.0rust-analyzer cargo sqlx-postgres 0.8.3 connection/tls/MaybeUpgradeTls#0.).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll(). |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | async fn maybe_upgraderust-analyzer cargo sqlx-postgres 0.8.3 connection/tls/maybe_upgrade().<S: Socketrust-analyzer cargo sqlx-core 0.8.3 net/socket/Socket#>( |
| 19 | mut socket: S, |
| 20 | options: &PgConnectOptionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#, |
| 21 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn Socketrust-analyzer cargo sqlx-core 0.8.3 net/socket/Socket#>, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#> { |
| 22 | // https://www.postgresql.org/docs/12/libpq-ssl.html#LIBPQ-SSL-SSLMODE-STATEMENTS |
| 23 | match options.ssl_moderust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_mode. { |
| 24 | // FIXME: Implement ALLOW |
| 25 | PgSslModerust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#::Allowrust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#Allow# | PgSslModerust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#::Disablerust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#Disable# => return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(socket)), |
| 26 | |
| 27 | PgSslModerust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#::Preferrust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#Prefer# => { |
| 28 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().tlsrust-analyzer cargo sqlx-core 0.8.3 net/tls/::availablerust-analyzer cargo sqlx-core 0.8.3 net/tls/available().() { |
| 29 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(socket)); |
| 30 | } |
| 31 | |
| 32 | // try upgrade, but its okay if we fail |
| 33 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().request_upgraderust-analyzer cargo sqlx-postgres 0.8.3 connection/tls/request_upgrade().(&mut socket, options).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 34 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(socket)); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | PgSslModerust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#::Requirerust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#Require# | PgSslModerust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#::VerifyFullrust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#VerifyFull# | PgSslModerust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#::VerifyCarust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#VerifyCa# => { |
| 39 | tlsrust-analyzer cargo sqlx-core 0.8.3 net/tls/::error_if_unavailablerust-analyzer cargo sqlx-core 0.8.3 net/tls/error_if_unavailable().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 40 | |
| 41 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().request_upgraderust-analyzer cargo sqlx-postgres 0.8.3 connection/tls/request_upgrade().(&mut socket, options).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). { |
| 42 | // upgrade failed, die |
| 43 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#::Tlsrust-analyzer cargo sqlx-core 0.8.3 error/Error#Tls#("server does not support TLS".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | let accept_invalid_certs = !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!( |
| 49 | options.ssl_moderust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_mode., |
| 50 | PgSslModerust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#::VerifyCarust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#VerifyCa# | PgSslModerust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#::VerifyFullrust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#VerifyFull# |
| 51 | ); |
| 52 | let accept_invalid_hostnames = !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(options.ssl_moderust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_mode., PgSslModerust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#::VerifyFullrust-analyzer cargo sqlx-postgres 0.8.3 options/ssl_mode/PgSslMode#VerifyFull#); |
| 53 | |
| 54 | let config = TlsConfigrust-analyzer cargo sqlx-core 0.8.3 net/tls/TlsConfig# { |
| 55 | accept_invalid_certsrust-analyzer cargo sqlx-core 0.8.3 net/tls/TlsConfig#accept_invalid_certs., |
| 56 | accept_invalid_hostnamesrust-analyzer cargo sqlx-core 0.8.3 net/tls/TlsConfig#accept_invalid_hostnames., |
| 57 | hostnamerust-analyzer cargo sqlx-core 0.8.3 net/tls/TlsConfig#hostname.: &options.hostrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#host., |
| 58 | root_cert_pathrust-analyzer cargo sqlx-core 0.8.3 net/tls/TlsConfig#root_cert_path.: options.ssl_root_certrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_root_cert..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().(), |
| 59 | client_cert_pathrust-analyzer cargo sqlx-core 0.8.3 net/tls/TlsConfig#client_cert_path.: options.ssl_client_certrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_client_cert..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().(), |
| 60 | client_key_pathrust-analyzer cargo sqlx-core 0.8.3 net/tls/TlsConfig#client_key_path.: options.ssl_client_keyrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#ssl_client_key..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().(), |
| 61 | }; |
| 62 | |
| 63 | tlsrust-analyzer cargo sqlx-core 0.8.3 net/tls/::handshakerust-analyzer cargo sqlx-core 0.8.3 net/tls/handshake().(socket, config, SocketIntoBoxrust-analyzer cargo sqlx-core 0.8.3 net/socket/SocketIntoBox#).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll(). |
| 64 | } |
| 65 | |
| 66 | async fn request_upgraderust-analyzer cargo sqlx-postgres 0.8.3 connection/tls/request_upgrade().( |
| 67 | socket: &mut impl Socketrust-analyzer cargo sqlx-core 0.8.3 net/socket/Socket#, |
| 68 | _options: &PgConnectOptionsrust-analyzer cargo sqlx-postgres 0.8.3 options/PgConnectOptions#, |
| 69 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<bool, Errorrust-analyzer cargo sqlx-core 0.8.3 error/Error#> { |
| 70 | // https://www.postgresql.org/docs/current/protocol-flow.html#id-1.10.5.7.11 |
| 71 | |
| 72 | // To initiate an SSL-encrypted connection, the frontend initially sends an |
| 73 | // SSLRequest message rather than a StartupMessage |
| 74 | |
| 75 | socket.writerust-analyzer cargo sqlx-core 0.8.3 net/socket/Socket#write().(SslRequestrust-analyzer cargo sqlx-postgres 0.8.3 message/ssl_request/SslRequest#::BYTESrust-analyzer cargo sqlx-postgres 0.8.3 message/ssl_request/impl#[SslRequest]BYTES.).rust-analyzer cargo sqlx-core 0.8.3 net/socket/impl#[`Write<'a, S>`][Future]poll().awaitrust-analyzer cargo sqlx-core 0.8.3 net/socket/impl#[`Write<'a, S>`][Future]poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 76 | |
| 77 | // The server then responds with a single byte containing S or N, indicating that |
| 78 | // it is willing or unwilling to perform SSL, respectively. |
| 79 | |
| 80 | let mut response = [0u8]; |
| 81 | |
| 82 | socket.readrust-analyzer cargo sqlx-core 0.8.3 net/socket/Socket#read().(&mut &mut response[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().).rust-analyzer cargo sqlx-core 0.8.3 net/socket/impl#[`Read<'a, S, B>`][Future]poll().awaitrust-analyzer cargo sqlx-core 0.8.3 net/socket/impl#[`Read<'a, S, B>`][Future]poll().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 83 | |
| 84 | match response[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index(). { |
| 85 | b'S' => { |
| 86 | // The server is ready and willing to accept an SSL connection |
| 87 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(true) |
| 88 | } |
| 89 | |
| 90 | b'N' => { |
| 91 | // The server is _unwilling_ to perform SSL |
| 92 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(false) |
| 93 | } |
| 94 | |
| 95 | other => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err_protocolrust-analyzer cargo sqlx-core 0.8.3 error/err_protocol!!( |
| 96 | "unexpected response from SSLRequest: 0x{:02x}", |
| 97 | other |
| 98 | )), |
| 99 | } |
| 100 | } |