| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
use strict; |
| 5 |
use warnings FATAL => 'all'; |
| 6 |
use PostgreSQL::Test::Cluster; |
| 7 |
use PostgreSQL::Test::Utils; |
| 8 |
use Test::More; |
| 9 |
|
| 10 |
use File::Copy; |
| 11 |
|
| 12 |
use FindBin; |
| 13 |
use lib $FindBin::RealBin; |
| 14 |
|
| 15 |
use SSL::Server; |
| 16 |
|
| 17 |
if ($ENV{with_ssl} ne 'openssl') |
| 18 |
{ |
| 19 |
plan skip_all => 'OpenSSL not supported by this build'; |
| 20 |
} |
| 21 |
if (!$ENV{PG_TEST_EXTRA} || $ENV{PG_TEST_EXTRA} !~ /\bssl\b/) |
| 22 |
{ |
| 23 |
plan skip_all => |
| 24 |
'Potentially unsafe test SSL not enabled in PG_TEST_EXTRA'; |
| 25 |
} |
| 26 |
|
| 27 |
#### Some configuration |
| 28 |
my $ssl_server = SSL::Server->new(); |
| 29 |
|
| 30 |
sub sslkey |
| 31 |
{ |
| 32 |
return $ssl_server->sslkey(@_); |
| 33 |
} |
| 34 |
|
| 35 |
sub switch_server_cert |
| 36 |
{ |
| 37 |
$ssl_server->switch_server_cert(@_); |
| 38 |
} |
| 39 |
|
| 40 |
# This is the hostname used to connect to the server. This cannot be a |
| 41 |
# hostname, because the server certificate is always for the domain |
| 42 |
# postgresql-ssl-regression.test. |
| 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 build supports sslcertmode=require. |
| 48 |
my $supports_sslcertmode_require = |
| 49 |
check_pg_config("#define HAVE_SSL_CTX_SET_CERT_CB 1"); |
| 50 |
|
| 51 |
# Allocation of base connection string shared among multiple tests. |
| 52 |
my $common_connstr; |
| 53 |
|
| 54 |
#### Set up the server. |
| 55 |
|
| 56 |
note "setting up data directory"; |
| 57 |
my $node = PostgreSQL::Test::Cluster->new('primary'); |
| 58 |
$node->init; |
| 59 |
|
| 60 |
# PGHOST is enforced here to set up the node, subsequent connections |
| 61 |
# will use a dedicated connection string. |
| 62 |
$ENV{PGHOST} = $node->host; |
| 63 |
$ENV{PGPORT} = $node->port; |
| 64 |
$node->start; |
| 65 |
|
| 66 |
$ssl_server->configure_test_server_for_ssl($node, $SERVERHOSTADDR, |
| 67 |
$SERVERHOSTCIDR, 'trust', extensions => [qw(sslinfo)]); |
| 68 |
|
| 69 |
# We aren't using any CRL's in this suite so we can keep using server-revoked |
| 70 |
# as server certificate for simple client.crt connection much like how the |
| 71 |
# 001 test does. |
| 72 |
switch_server_cert($node, certfile => 'server-revoked'); |
| 73 |
|
| 74 |
# Set of default settings for SSL parameters in connection string. This |
| 75 |
# makes the tests protected against any defaults the environment may have |
| 76 |
# in ~/.postgresql/. |
| 77 |
my $default_ssl_connstr = |
| 78 |
"sslkey=invalid sslcert=invalid sslrootcert=invalid sslcrl=invalid sslcrldir=invalid"; |
| 79 |
|
| 80 |
$common_connstr = |
| 81 |
"$default_ssl_connstr sslrootcert=ssl/root+server_ca.crt sslmode=require dbname=certdb hostaddr=$SERVERHOSTADDR host=localhost " |
| 82 |
. "user=ssltestuser sslcert=ssl/client_ext.crt " |
| 83 |
. sslkey('client_ext.key'); |
| 84 |
|
| 85 |
# Make sure we can connect even though previous test suites have established this |
| 86 |
$node->connect_ok( |
| 87 |
$common_connstr, |
| 88 |
"certificate authorization succeeds with correct client cert in PEM format", |
| 89 |
); |
| 90 |
|
| 91 |
my $result; |
| 92 |
|
| 93 |
$result = $node->safe_psql( |
| 94 |
"certdb", |
| 95 |
"SELECT ssl_is_used();", |
| 96 |
connstr => $common_connstr); |
| 97 |
is($result, 't', "ssl_is_used() for TLS connection"); |
| 98 |
|
| 99 |
$result = $node->safe_psql( |
| 100 |
"certdb", |
| 101 |
"SELECT ssl_version();", |
| 102 |
connstr => $common_connstr |
| 103 |
. " ssl_min_protocol_version=TLSv1.2 " |
| 104 |
. "ssl_max_protocol_version=TLSv1.2"); |
| 105 |
is($result, 'TLSv1.2', "ssl_version() correctly returning TLS protocol"); |
| 106 |
|
| 107 |
$result = $node->safe_psql( |
| 108 |
"certdb", |
| 109 |
"SELECT ssl_cipher() = cipher FROM pg_stat_ssl WHERE pid = pg_backend_pid();", |
| 110 |
connstr => $common_connstr); |
| 111 |
is($result, 't', "ssl_cipher() compared with pg_stat_ssl"); |
| 112 |
|
| 113 |
$result = $node->safe_psql( |
| 114 |
"certdb", |
| 115 |
"SELECT ssl_client_cert_present();", |
| 116 |
connstr => $common_connstr); |
| 117 |
is($result, 't', "ssl_client_cert_present() for connection with cert"); |
| 118 |
|
| 119 |
$result = $node->safe_psql( |
| 120 |
"trustdb", |
| 121 |
"SELECT ssl_client_cert_present();", |
| 122 |
connstr => |
| 123 |
"$default_ssl_connstr sslrootcert=ssl/root+server_ca.crt sslmode=require " |
| 124 |
. "dbname=trustdb hostaddr=$SERVERHOSTADDR user=ssltestuser host=localhost" |
| 125 |
); |
| 126 |
is($result, 'f', "ssl_client_cert_present() for connection without cert"); |
| 127 |
|
| 128 |
$result = $node->safe_psql( |
| 129 |
"certdb", |
| 130 |
"SELECT ssl_client_serial() = client_serial FROM pg_stat_ssl WHERE pid = pg_backend_pid();", |
| 131 |
connstr => $common_connstr); |
| 132 |
is($result, 't', "ssl_client_serial() compared with pg_stat_ssl"); |
| 133 |
|
| 134 |
# Must not use safe_psql since we expect an error here |
| 135 |
$result = $node->psql( |
| 136 |
"certdb", |
| 137 |
"SELECT ssl_client_dn_field('invalid');", |
| 138 |
connstr => $common_connstr); |
| 139 |
is($result, '3', "ssl_client_dn_field() for an invalid field"); |
| 140 |
|
| 141 |
$result = $node->safe_psql( |
| 142 |
"trustdb", |
| 143 |
"SELECT ssl_client_dn_field('commonName');", |
| 144 |
connstr => |
| 145 |
"$default_ssl_connstr sslrootcert=ssl/root+server_ca.crt sslmode=require " |
| 146 |
. "dbname=trustdb hostaddr=$SERVERHOSTADDR user=ssltestuser host=localhost" |
| 147 |
); |
| 148 |
is($result, '', "ssl_client_dn_field() for connection without cert"); |
| 149 |
|
| 150 |
$result = $node->safe_psql( |
| 151 |
"certdb", |
| 152 |
"SELECT '/CN=' || ssl_client_dn_field('commonName') = client_dn FROM pg_stat_ssl WHERE pid = pg_backend_pid();", |
| 153 |
connstr => $common_connstr); |
| 154 |
is($result, 't', "ssl_client_dn_field() for commonName"); |
| 155 |
|
| 156 |
$result = $node->safe_psql( |
| 157 |
"certdb", |
| 158 |
"SELECT ssl_issuer_dn() = issuer_dn FROM pg_stat_ssl WHERE pid = pg_backend_pid();", |
| 159 |
connstr => $common_connstr); |
| 160 |
is($result, 't', "ssl_issuer_dn() for connection with cert"); |
| 161 |
|
| 162 |
$result = $node->safe_psql( |
| 163 |
"certdb", |
| 164 |
"SELECT '/CN=' || ssl_issuer_field('commonName') = issuer_dn FROM pg_stat_ssl WHERE pid = pg_backend_pid();", |
| 165 |
connstr => $common_connstr); |
| 166 |
is($result, 't', "ssl_issuer_field() for commonName"); |
| 167 |
|
| 168 |
$result = $node->safe_psql( |
| 169 |
"certdb", |
| 170 |
"SELECT value, critical FROM ssl_extension_info() WHERE name = 'basicConstraints';", |
| 171 |
connstr => $common_connstr); |
| 172 |
is($result, 'CA:FALSE|t', 'extract extension from cert'); |
| 173 |
|
| 174 |
# Sanity tests for sslcertmode, using ssl_client_cert_present() |
| 175 |
my @cases = ( |
| 176 |
{ opts => "sslcertmode=allow", present => 't' }, |
| 177 |
{ opts => "sslcertmode=allow sslcert=invalid", present => 'f' }, |
| 178 |
{ opts => "sslcertmode=disable", present => 'f' },); |
| 179 |
if ($supports_sslcertmode_require) |
| 180 |
{ |
| 181 |
push(@cases, { opts => "sslcertmode=require", present => 't' }); |
| 182 |
} |
| 183 |
|
| 184 |
foreach my $c (@cases) |
| 185 |
{ |
| 186 |
$result = $node->safe_psql( |
| 187 |
"trustdb", |
| 188 |
"SELECT ssl_client_cert_present();", |
| 189 |
connstr => "$common_connstr dbname=trustdb $c->{opts}"); |
| 190 |
is($result, $c->{present}, "ssl_client_cert_present() for $c->{opts}"); |
| 191 |
} |
| 192 |
|
| 193 |
done_testing(); |