Skip to content

Add sslmode verify-ca and verify-full #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
config: add sslmode verify-ca and verify-full
When a connection is established, the added modes are treated in the
same way as the existing `require` mode as they both require a TLS
connection.

It's the responsibility of the user to configure the TLS stream to match
the semantics of Postgres client (e.g. enable peer cert verification).
  • Loading branch information
uce committed May 18, 2021
commit cc45c4f0badb1f705ee76cfade7ec0e1c4b3080c
4 changes: 3 additions & 1 deletion postgres/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ use tokio_postgres::{Error, Socket};
/// * `options` - Command line options used to configure the server.
/// * `application_name` - Sets the `application_name` parameter on the server.
/// * `sslmode` - Controls usage of TLS. If set to `disable`, TLS will not be used. If set to `prefer`, TLS will be used
/// if available, but not used otherwise. If set to `require`, TLS will be forced to be used. Defaults to `prefer`.
/// if available, but not used otherwise. If set to `require`, `verify-ca`, or `verify-full`, TLS will be forced to
/// be used. Defaults to `prefer`. Note that for modes `verify-ca` and `verify-full`, it's up to the user to configure
/// the SSL stream to respect the desired configuration (e.g. verification of certs, hostname verification).
/// * `host` - The host to connect to. On Unix platforms, if the host starts with a `/` character it is treated as the
/// path to the directory containing Unix domain sockets. Otherwise, it is treated as a hostname. Multiple hosts
/// can be specified, separated by commas. Each host will be tried in turn when connecting. Required if connecting
Expand Down
10 changes: 9 additions & 1 deletion tokio-postgres/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ pub enum SslMode {
Prefer,
/// Require the use of TLS.
Require,
/// Require the use of TLS. Verify peer cert without hostname verification.
VerifyCa,
/// Require the use of TLS. Verify peer cert and hostname.
VerifyFull,
}

/// Channel binding configuration.
Expand Down Expand Up @@ -95,7 +99,9 @@ pub enum Host {
/// * `options` - Command line options used to configure the server.
/// * `application_name` - Sets the `application_name` parameter on the server.
/// * `sslmode` - Controls usage of TLS. If set to `disable`, TLS will not be used. If set to `prefer`, TLS will be used
/// if available, but not used otherwise. If set to `require`, TLS will be forced to be used. Defaults to `prefer`.
/// if available, but not used otherwise. If set to `require`, `verify-ca`, or `verify-full`, TLS will be forced to
/// be used. Defaults to `prefer`. Note that for modes `verify-ca` and `verify-full`, it's up to the user to configure
/// the SSL stream to respect the desired configuration (e.g. verification of certs, hostname verification).
/// * `host` - The host to connect to. On Unix platforms, if the host starts with a `/` character it is treated as the
/// path to the directory containing Unix domain sockets. Otherwise, it is treated as a hostname. Multiple hosts
/// can be specified, separated by commas. Each host will be tried in turn when connecting. Required if connecting
Expand Down Expand Up @@ -432,6 +438,8 @@ impl Config {
"disable" => SslMode::Disable,
"prefer" => SslMode::Prefer,
"require" => SslMode::Require,
"verify-ca" => SslMode::VerifyCa,
"verify-full" => SslMode::VerifyFull,
_ => return Err(Error::config_parse(Box::new(InvalidValue("sslmode")))),
};
self.ssl_mode(mode);
Expand Down
11 changes: 6 additions & 5 deletions tokio-postgres/src/connect_tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ where
SslMode::Prefer if !tls.can_connect(ForcePrivateApi) => {
return Ok(MaybeTlsStream::Raw(stream))
}
SslMode::Prefer | SslMode::Require => {}
SslMode::Prefer | SslMode::Require | SslMode::VerifyCa | SslMode::VerifyFull => {}
}

let mut buf = BytesMut::new();
Expand All @@ -32,10 +32,11 @@ where
stream.read_exact(&mut buf).await.map_err(Error::io)?;

if buf[0] != b'S' {
if SslMode::Require == mode {
return Err(Error::tls("server does not support TLS".into()));
} else {
return Ok(MaybeTlsStream::Raw(stream));
match mode {
SslMode::Disable | SslMode::Prefer => return Ok(MaybeTlsStream::Raw(stream)),
SslMode::Require | SslMode::VerifyCa | SslMode::VerifyFull => {
return Err(Error::tls("server does not support TLS".into()))
}
}
}

Expand Down