use std::path::{Path, PathBuf};
use std::sync::Arc;
use rustls::client::WebPkiServerVerifier;
use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier};
use rustls::crypto::CryptoProvider;
use rustls::pki_types::{CertificateDer, ServerName, UnixTime};
use rustls::{
CertificateError, ClientConfig, DigitallySignedStruct, Error as RustlsError, RootCertStore,
SignatureScheme,
};
use tokio_postgres::{Client, NoTls};
use tokio_postgres_rustls::MakeRustlsConnect;
use tracing::info;
const SSLMODE: &str = "sslmode";
const SSLROOTCERT: &str = "sslrootcert";
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SslMode {
Disable,
Prefer,
Require,
VerifyCa,
VerifyFull,
}
impl SslMode {
fn parse(value: &str) -> Result<Self, String> {
match value {
"disable" => Ok(Self::Disable),
"prefer" => Ok(Self::Prefer),
"require" => Ok(Self::Require),
"verify-ca" => Ok(Self::VerifyCa),
"verify-full" => Ok(Self::VerifyFull),
"allow" => Err("sslmode=allow is not supported; use sslmode=prefer".to_string()),
other => Err(format!(
"unsupported sslmode '{other}' (expected disable, prefer, require, verify-ca, or verify-full)"
)),
}
}
fn negotiation(self) -> &'static str {
match self {
Self::Disable => "disable",
Self::Prefer => "prefer",
Self::Require | Self::VerifyCa | Self::VerifyFull => "require",
}
}
fn verifies_chain(self, has_root_cert: bool) -> bool {
match self {
Self::VerifyCa | Self::VerifyFull => true,
Self::Require => has_root_cert,
Self::Disable | Self::Prefer => false,
}
}
fn verifies_hostname(self) -> bool {
matches!(self, Self::VerifyFull)
}
}
#[derive(Debug)]
pub struct ConnectionSettings {
pub url: String,
pub mode: SslMode,
pub root_cert: Option<PathBuf>,
}
impl ConnectionSettings {
pub fn parse(connection_string: &str) -> Result<Self, String> {
let (rest, mode_value, root_cert) = if connection_string.contains("://") {
split_url_form(connection_string)?
} else {
split_keyword_form(connection_string)?
};
let mode = match mode_value.as_deref() {
None => SslMode::Prefer,
Some(value) => SslMode::parse(value)?,
};
let url = if connection_string.contains("://") {
append_url_param(&rest, SSLMODE, mode.negotiation())
} else {
format!("{rest} {SSLMODE}={}", mode.negotiation())
};
Ok(Self {
url,
mode,
root_cert: root_cert.map(PathBuf::from),
})
}
}
fn split_url_form(input: &str) -> Result<(String, Option<String>, Option<String>), String> {
let (base, query) = match input.split_once('?') {
Some((base, query)) => (base, Some(query)),
None => (input, None),
};
let mut kept: Vec<String> = Vec::new();
let mut mode = None;
let mut root_cert = None;
if let Some(query) = query {
for item in query.split('&').filter(|item| !item.is_empty()) {
let (key, value) = item.split_once('=').unwrap_or((item, ""));
match key {
SSLMODE => mode = Some(percent_decode(value)),
SSLROOTCERT => root_cert = Some(percent_decode(value)),
_ => kept.push(item.to_string()),
}
}
}
let rest = if kept.is_empty() {
base.to_string()
} else {
format!("{base}?{}", kept.join("&"))
};
Ok((rest, mode, root_cert))
}
fn append_url_param(url: &str, key: &str, value: &str) -> String {
if url.contains('?') {
format!("{url}&{key}={value}")
} else {
format!("{url}?{key}={value}")
}
}
fn percent_decode(value: &str) -> String {
let bytes = value.as_bytes();
let mut out = Vec::with_capacity(bytes.len());
let mut index = 0;
while index < bytes.len() {
if bytes[index] == b'%' && index + 2 < bytes.len() {
let hex = std::str::from_utf8(&bytes[index + 1..index + 3]).ok();
if let Some(byte) = hex.and_then(|hex| u8::from_str_radix(hex, 16).ok()) {
out.push(byte);
index += 3;
continue;
}
}
out.push(bytes[index]);
index += 1;
}
String::from_utf8_lossy(&out).into_owned()
}
fn split_keyword_form(input: &str) -> Result<(String, Option<String>, Option<String>), String> {
let mut kept: Vec<String> = Vec::new();
let mut mode = None;
let mut root_cert = None;
let mut raw = String::new();
let mut value = String::new();
let mut quote: Option<char> = None;
let mut finish = |raw: &mut String, value: &mut String| {
if raw.is_empty() {
return;
}
match value.split_once('=') {
Some((key, val)) if key == SSLMODE => mode = Some(val.to_string()),
Some((key, val)) if key == SSLROOTCERT => root_cert = Some(val.to_string()),
_ => kept.push(std::mem::take(raw)),
}
raw.clear();
value.clear();
};
for ch in input.chars() {
match quote {
Some(q) if ch == q => {
quote = None;
value.push(ch);
raw.push(ch);
}
Some(_) => {
value.push(ch);
raw.push(ch);
}
None if ch == '\'' || ch == '"' => {
quote = Some(ch);
value.push(ch);
raw.push(ch);
}
None if ch.is_whitespace() => finish(&mut raw, &mut value),
None => {
value.push(ch);
raw.push(ch);
}
}
}
if quote.is_some() {
return Err("unterminated quoted value in connection string".to_string());
}
finish(&mut raw, &mut value);
Ok((kept.join(" "), mode, root_cert))
}
#[derive(Debug)]
struct AcceptAnyServerCert {
provider: Arc<CryptoProvider>,
}
impl ServerCertVerifier for AcceptAnyServerCert {
fn verify_server_cert(
&self,
_end_entity: &CertificateDer<'_>,
_intermediates: &[CertificateDer<'_>],
_server_name: &ServerName<'_>,
_ocsp_response: &[u8],
_now: UnixTime,
) -> Result<ServerCertVerified, RustlsError> {
Ok(ServerCertVerified::assertion())
}
fn verify_tls12_signature(
&self,
message: &[u8],
cert: &CertificateDer<'_>,
dss: &DigitallySignedStruct,
) -> Result<HandshakeSignatureValid, RustlsError> {
rustls::crypto::verify_tls12_signature(
message,
cert,
dss,
&self.provider.signature_verification_algorithms,
)
}
fn verify_tls13_signature(
&self,
message: &[u8],
cert: &CertificateDer<'_>,
dss: &DigitallySignedStruct,
) -> Result<HandshakeSignatureValid, RustlsError> {
rustls::crypto::verify_tls13_signature(
message,
cert,
dss,
&self.provider.signature_verification_algorithms,
)
}
fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
self.provider
.signature_verification_algorithms
.supported_schemes()
}
}
#[derive(Debug)]
struct ChainOnlyVerifier {
inner: Arc<WebPkiServerVerifier>,
}
impl ServerCertVerifier for ChainOnlyVerifier {
fn verify_server_cert(
&self,
end_entity: &CertificateDer<'_>,
intermediates: &[CertificateDer<'_>],
server_name: &ServerName<'_>,
ocsp_response: &[u8],
now: UnixTime,
) -> Result<ServerCertVerified, RustlsError> {
match self.inner.verify_server_cert(
end_entity,
intermediates,
server_name,
ocsp_response,
now,
) {
Err(RustlsError::InvalidCertificate(
CertificateError::NotValidForName | CertificateError::NotValidForNameContext { .. },
)) => Ok(ServerCertVerified::assertion()),
other => other,
}
}
fn verify_tls12_signature(
&self,
message: &[u8],
cert: &CertificateDer<'_>,
dss: &DigitallySignedStruct,
) -> Result<HandshakeSignatureValid, RustlsError> {
self.inner.verify_tls12_signature(message, cert, dss)
}
fn verify_tls13_signature(
&self,
message: &[u8],
cert: &CertificateDer<'_>,
dss: &DigitallySignedStruct,
) -> Result<HandshakeSignatureValid, RustlsError> {
self.inner.verify_tls13_signature(message, cert, dss)
}
fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
self.inner.supported_verify_schemes()
}
}
fn load_roots(root_cert: Option<&Path>) -> Result<RootCertStore, String> {
let mut roots = RootCertStore::empty();
match root_cert {
Some(path) => {
let pem = std::fs::read(path)
.map_err(|e| format!("cannot read sslrootcert {}: {e}", path.display()))?;
let mut added = 0;
for cert in rustls_pemfile::certs(&mut pem.as_slice()) {
let cert = cert.map_err(|e| format!("invalid certificate in sslrootcert: {e}"))?;
roots
.add(cert)
.map_err(|e| format!("cannot add sslrootcert certificate: {e}"))?;
added += 1;
}
if added == 0 {
return Err(format!(
"sslrootcert {} contains no certificates",
path.display()
));
}
}
None => {
let result = rustls_native_certs::load_native_certs();
for error in &result.errors {
info!("ignoring a native certificate store error: {error}");
}
for cert in result.certs {
let _ = roots.add(cert);
}
if roots.is_empty() {
return Err("no trust anchors available; set sslrootcert".to_string());
}
}
}
Ok(roots)
}
fn build_verifier(
settings: &ConnectionSettings,
provider: &Arc<CryptoProvider>,
) -> Result<Arc<dyn ServerCertVerifier>, String> {
if !settings.mode.verifies_chain(settings.root_cert.is_some()) {
return Ok(Arc::new(AcceptAnyServerCert {
provider: provider.clone(),
}));
}
let roots = Arc::new(load_roots(settings.root_cert.as_deref())?);
let webpki = WebPkiServerVerifier::builder_with_provider(roots, provider.clone())
.build()
.map_err(|e| format!("cannot build certificate verifier: {e}"))?;
if settings.mode.verifies_hostname() {
Ok(webpki)
} else {
Ok(Arc::new(ChainOnlyVerifier { inner: webpki }))
}
}
pub async fn connect(settings: &ConnectionSettings) -> Result<Client, String> {
info!(
"connecting to PostgreSQL (sslmode={}) ...",
settings.mode_name()
);
match settings.mode {
SslMode::Disable => drive(tokio_postgres::connect(&settings.url, NoTls).await).await,
_ => {
let provider = Arc::new(rustls::crypto::ring::default_provider());
let verifier = build_verifier(settings, &provider)?;
let config = ClientConfig::builder_with_provider(provider)
.with_safe_default_protocol_versions()
.map_err(|e| format!("TLS configuration error: {e}"))?
.dangerous()
.with_custom_certificate_verifier(verifier)
.with_no_client_auth();
let tls = MakeRustlsConnect::new(config);
drive(tokio_postgres::connect(&settings.url, tls).await).await
}
}
}
impl ConnectionSettings {
fn mode_name(&self) -> &'static str {
match self.mode {
SslMode::Disable => "disable",
SslMode::Prefer => "prefer",
SslMode::Require => "require",
SslMode::VerifyCa => "verify-ca",
SslMode::VerifyFull => "verify-full",
}
}
}
async fn drive<S>(
result: Result<
(
Client,
tokio_postgres::Connection<tokio_postgres::Socket, S>,
),
tokio_postgres::Error,
>,
) -> Result<Client, String>
where
S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin + Send + 'static,
{
let (client, connection) =
result.map_err(|e| format!("failed to connect to PostgreSQL: {}", error_chain(&e)))?;
tokio::spawn(async move {
if let Err(e) = connection.await {
tracing::error!("postgres connection error: {e}");
}
});
Ok(client)
}
fn error_chain(error: &tokio_postgres::Error) -> String {
let mut rendered = error.to_string();
let mut source = std::error::Error::source(error);
while let Some(cause) = source {
rendered.push_str(": ");
rendered.push_str(&cause.to_string());
source = cause.source();
}
rendered
}
#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;
use tokio_postgres::Config;
fn parse(url: &str) -> ConnectionSettings {
ConnectionSettings::parse(url).expect("connection string should parse")
}
#[test]
fn defaults_to_prefer() {
let settings = parse("postgresql://loader@db.example:5432/bgpkit_broker");
assert_eq!(settings.mode, SslMode::Prefer);
assert!(settings.root_cert.is_none());
assert_eq!(
settings.url,
"postgresql://loader@db.example:5432/bgpkit_broker?sslmode=prefer"
);
}
#[test]
fn keeps_other_url_parameters() {
let settings = parse(
"postgresql://loader@db.example:5432/bgpkit_broker?application_name=loader&sslmode=require",
);
assert_eq!(settings.mode, SslMode::Require);
assert_eq!(
settings.url,
"postgresql://loader@db.example:5432/bgpkit_broker?application_name=loader&sslmode=require"
);
}
#[test]
fn verification_modes_negotiate_as_require() {
for (value, expected) in [
("verify-ca", SslMode::VerifyCa),
("verify-full", SslMode::VerifyFull),
] {
let settings = parse(&format!(
"postgresql://loader@db.example:5432/db?sslmode={value}&sslrootcert=%2Fetc%2Fca.crt"
));
assert_eq!(settings.mode, expected);
assert_eq!(
settings.root_cert.as_deref(),
Some(Path::new("/etc/ca.crt"))
);
assert!(settings.url.ends_with("sslmode=require"));
assert!(!settings.url.contains("sslrootcert"));
Config::from_str(&settings.url).expect("tokio-postgres must accept the rewritten URL");
}
}
#[test]
fn disable_is_passed_through() {
let settings = parse("postgresql://loader@db.example:5432/db?sslmode=disable");
assert_eq!(settings.mode, SslMode::Disable);
assert_eq!(
settings.url,
"postgresql://loader@db.example:5432/db?sslmode=disable"
);
}
#[test]
fn keyword_form_is_supported() {
let settings = parse(
"host=db.example port=5432 user=loader password='p w' dbname=broker sslmode=verify-full sslrootcert=/etc/ca.crt",
);
assert_eq!(settings.mode, SslMode::VerifyFull);
assert_eq!(
settings.root_cert.as_deref(),
Some(Path::new("/etc/ca.crt"))
);
assert!(settings.url.contains("password='p w'"));
assert!(!settings.url.contains("sslrootcert"));
assert!(settings.url.ends_with("sslmode=require"));
Config::from_str(&settings.url).expect("tokio-postgres must accept the rewritten string");
}
#[test]
fn allow_is_rejected_with_a_hint() {
let error = ConnectionSettings::parse("postgresql://loader@db/db?sslmode=allow")
.expect_err("allow must be rejected");
assert!(error.contains("prefer"), "unexpected error: {error}");
}
#[test]
fn unknown_mode_is_rejected() {
let error = ConnectionSettings::parse("postgresql://loader@db/db?sslmode=sorta")
.expect_err("unknown mode must be rejected");
assert!(
error.contains("unsupported sslmode"),
"unexpected error: {error}"
);
}
#[test]
fn certificate_policy_per_mode() {
assert!(!SslMode::Prefer.verifies_chain(true));
assert!(!SslMode::Prefer.verifies_hostname());
assert!(!SslMode::Require.verifies_chain(false));
assert!(SslMode::Require.verifies_chain(true));
assert!(!SslMode::Require.verifies_hostname());
assert!(SslMode::VerifyCa.verifies_chain(false));
assert!(!SslMode::VerifyCa.verifies_hostname());
assert!(SslMode::VerifyFull.verifies_chain(false));
assert!(SslMode::VerifyFull.verifies_hostname());
}
#[test]
fn missing_root_certificate_is_reported() {
let settings =
parse("postgresql://loader@db/db?sslmode=verify-ca&sslrootcert=/nope/ca.crt");
let error = load_roots(settings.root_cert.as_deref()).expect_err("missing file must fail");
assert!(
error.contains("cannot read sslrootcert"),
"unexpected error: {error}"
);
}
}