| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test SCRAM authentication and TLS channel binding types |
| 5 |
|
| 6 |
use strict; |
| 7 |
use warnings FATAL => 'all'; |
| 8 |
use PostgreSQL::Test::Cluster; |
| 9 |
use PostgreSQL::Test::Utils; |
| 10 |
use Test::More; |
| 11 |
|
| 12 |
use File::Copy; |
| 13 |
|
| 14 |
use FindBin; |
| 15 |
use lib $FindBin::RealBin; |
| 16 |
|
| 17 |
use SSL::Server; |
| 18 |
|
| 19 |
if ($ENV{with_ssl} ne 'openssl') |
| 20 |
{ |
| 21 |
plan skip_all => 'OpenSSL not supported by this build'; |
| 22 |
} |
| 23 |
if (!$ENV{PG_TEST_EXTRA} || $ENV{PG_TEST_EXTRA} !~ /\bssl\b/) |
| 24 |
{ |
| 25 |
plan skip_all => |
| 26 |
'Potentially unsafe test SSL not enabled in PG_TEST_EXTRA'; |
| 27 |
} |
| 28 |
|
| 29 |
my $ssl_server = SSL::Server->new(); |
| 30 |
|
| 31 |
sub sslkey |
| 32 |
{ |
| 33 |
return $ssl_server->sslkey(@_); |
| 34 |
} |
| 35 |
|
| 36 |
sub switch_server_cert |
| 37 |
{ |
| 38 |
$ssl_server->switch_server_cert(@_); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
# This is the hostname used to connect to the server. |
| 43 |
my $SERVERHOSTADDR = '127.0.0.1'; |
| 44 |
# This is the pattern to use in pg_hba.conf to match incoming connections. |
| 45 |
my $SERVERHOSTCIDR = '127.0.0.1/32'; |
| 46 |
|
| 47 |
# Determine whether this build uses OpenSSL or LibreSSL. |
| 48 |
my $libressl = $ssl_server->is_libressl; |
| 49 |
|
| 50 |
# Determine whether build supports detection of hash algorithms for |
| 51 |
# RSA-PSS certificates. |
| 52 |
my $supports_rsapss_certs = |
| 53 |
check_pg_config("#define HAVE_X509_GET_SIGNATURE_INFO 1"); |
| 54 |
|
| 55 |
# As of 5/2025, LibreSSL doesn't actually work for RSA-PSS certificates. |
| 56 |
$supports_rsapss_certs = 0 if $libressl; |
| 57 |
|
| 58 |
# Allocation of base connection string shared among multiple tests. |
| 59 |
my $common_connstr; |
| 60 |
|
| 61 |
# Set up the server. |
| 62 |
|
| 63 |
note "setting up data directory"; |
| 64 |
my $node = PostgreSQL::Test::Cluster->new('primary'); |
| 65 |
$node->init; |
| 66 |
|
| 67 |
# PGHOST is enforced here to set up the node, subsequent connections |
| 68 |
# will use a dedicated connection string. |
| 69 |
$ENV{PGHOST} = $node->host; |
| 70 |
$ENV{PGPORT} = $node->port; |
| 71 |
$node->start; |
| 72 |
|
| 73 |
# could fail in FIPS mode |
| 74 |
my $md5_works = ($node->psql('postgres', "select md5('')") == 0); |
| 75 |
|
| 76 |
# Configure server for SSL connections, with password handling. |
| 77 |
$ssl_server->configure_test_server_for_ssl( |
| 78 |
$node, $SERVERHOSTADDR, $SERVERHOSTCIDR, |
| 79 |
"scram-sha-256", |
| 80 |
password => "pass", |
| 81 |
password_enc => "scram-sha-256"); |
| 82 |
switch_server_cert($node, certfile => 'server-cn-only'); |
| 83 |
$ENV{PGPASSWORD} = "pass"; |
| 84 |
$common_connstr = |
| 85 |
"dbname=trustdb sslmode=require sslcert=invalid sslrootcert=invalid hostaddr=$SERVERHOSTADDR host=localhost"; |
| 86 |
|
| 87 |
# Default settings |
| 88 |
$node->connect_ok( |
| 89 |
"$common_connstr user=ssltestuser", |
| 90 |
"Basic SCRAM authentication with SSL"); |
| 91 |
|
| 92 |
# Test channel_binding |
| 93 |
$node->connect_fails( |
| 94 |
"$common_connstr user=ssltestuser channel_binding=invalid_value", |
| 95 |
"SCRAM with SSL and channel_binding=invalid_value", |
| 96 |
expected_stderr => qr/invalid channel_binding value: "invalid_value"/); |
| 97 |
$node->connect_ok("$common_connstr user=ssltestuser channel_binding=disable", |
| 98 |
"SCRAM with SSL and channel_binding=disable"); |
| 99 |
$node->connect_ok("$common_connstr user=ssltestuser channel_binding=require", |
| 100 |
"SCRAM with SSL and channel_binding=require"); |
| 101 |
|
| 102 |
# Now test when the user has an MD5-encrypted password; should fail |
| 103 |
SKIP: |
| 104 |
{ |
| 105 |
skip "MD5 not supported" unless $md5_works; |
| 106 |
$node->connect_fails( |
| 107 |
"$common_connstr user=md5testuser channel_binding=require", |
| 108 |
"MD5 with SSL and channel_binding=require", |
| 109 |
expected_stderr => |
| 110 |
qr/channel binding required but not supported by server's authentication request/ |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
# Now test with auth method 'cert' by connecting to 'certdb'. Should fail, |
| 115 |
# because channel binding is not performed. Note that ssl/client.key may |
| 116 |
# be used in a different test, so the name of this temporary client key |
| 117 |
# is chosen here to be unique. |
| 118 |
my $cert_tempdir = PostgreSQL::Test::Utils::tempdir(); |
| 119 |
my $client_tmp_key = "$cert_tempdir/client_scram.key"; |
| 120 |
copy("ssl/client.key", "$cert_tempdir/client_scram.key") |
| 121 |
or die |
| 122 |
"couldn't copy ssl/client_key to $cert_tempdir/client_scram.key for permission change: $!"; |
| 123 |
chmod 0600, "$cert_tempdir/client_scram.key" |
| 124 |
or die "failed to change permissions on $cert_tempdir/client_scram.key: $!"; |
| 125 |
$client_tmp_key =~ s!\\!/!g if $PostgreSQL::Test::Utils::windows_os; |
| 126 |
$node->connect_fails( |
| 127 |
"sslcert=ssl/client.crt sslkey=$client_tmp_key sslrootcert=invalid hostaddr=$SERVERHOSTADDR host=localhost dbname=certdb user=ssltestuser channel_binding=require", |
| 128 |
"Cert authentication and channel_binding=require", |
| 129 |
expected_stderr => |
| 130 |
qr/channel binding required, but server authenticated client without channel binding/ |
| 131 |
); |
| 132 |
|
| 133 |
# Certificate verification at the connection level should still work fine. |
| 134 |
$node->connect_ok( |
| 135 |
"sslcert=ssl/client.crt sslkey=$client_tmp_key sslrootcert=invalid hostaddr=$SERVERHOSTADDR host=localhost dbname=verifydb user=ssltestuser", |
| 136 |
"SCRAM with clientcert=verify-full", |
| 137 |
log_like => [ |
| 138 |
qr/connection authenticated: identity="ssltestuser" method=scram-sha-256/ |
| 139 |
]); |
| 140 |
|
| 141 |
# channel_binding should continue to work independently of require_auth. |
| 142 |
$node->connect_ok( |
| 143 |
"$common_connstr user=ssltestuser channel_binding=disable require_auth=scram-sha-256", |
| 144 |
"SCRAM with SSL, channel_binding=disable, and require_auth=scram-sha-256" |
| 145 |
); |
| 146 |
SKIP: |
| 147 |
{ |
| 148 |
skip "MD5 not supported" unless $md5_works; |
| 149 |
$node->connect_fails( |
| 150 |
"$common_connstr user=md5testuser require_auth=md5 channel_binding=require", |
| 151 |
"channel_binding can fail even when require_auth succeeds", |
| 152 |
expected_stderr => |
| 153 |
qr/channel binding required but not supported by server's authentication request/ |
| 154 |
); |
| 155 |
} |
| 156 |
$node->connect_ok( |
| 157 |
"$common_connstr user=ssltestuser channel_binding=require require_auth=scram-sha-256", |
| 158 |
"SCRAM with SSL, channel_binding=require, and require_auth=scram-sha-256" |
| 159 |
); |
| 160 |
|
| 161 |
# Now test with a server certificate that uses the RSA-PSS algorithm. |
| 162 |
# This checks that the certificate can be loaded and that channel binding |
| 163 |
# works. (see bug #17760) |
| 164 |
if ($supports_rsapss_certs) |
| 165 |
{ |
| 166 |
switch_server_cert($node, certfile => 'server-rsapss'); |
| 167 |
$node->connect_ok( |
| 168 |
"$common_connstr user=ssltestuser channel_binding=require", |
| 169 |
"SCRAM with SSL and channel_binding=require, server certificate uses 'rsassaPss'", |
| 170 |
log_like => [ |
| 171 |
qr/connection authenticated: identity="ssltestuser" method=scram-sha-256/ |
| 172 |
]); |
| 173 |
} |
| 174 |
done_testing(); |