diff options
author | Michael Paquier <michael@paquier.xyz> | 2023-03-24 13:34:26 +0900 |
---|---|---|
committer | Michael Paquier <michael@paquier.xyz> | 2023-03-24 13:34:26 +0900 |
commit | 36f40ce2dc66f1a36d6a12f7a0352e1c5bf1063e (patch) | |
tree | 75fa43d529706426487f0f8c8e3addb0039a8215 /src/test/ssl/t | |
parent | e522049f23998e64fd0b88cd66de8e8f42100bf1 (diff) | |
download | postgresql-36f40ce2dc66f1a36d6a12f7a0352e1c5bf1063e.tar.gz postgresql-36f40ce2dc66f1a36d6a12f7a0352e1c5bf1063e.zip |
libpq: Add sslcertmode option to control client certificates
The sslcertmode option controls whether the server is allowed and/or
required to request a certificate from the client. There are three
modes:
- "allow" is the default and follows the current behavior, where a
configured client certificate is sent if the server requests one
(via one of its default locations or sslcert). With the current
implementation, will happen whenever TLS is negotiated.
- "disable" causes the client to refuse to send a client certificate
even if sslcert is configured or if a client certificate is available in
one of its default locations.
- "require" causes the client to fail if a client certificate is never
sent and the server opens a connection anyway. This doesn't add any
additional security, since there is no guarantee that the server is
validating the certificate correctly, but it may helpful to troubleshoot
more complicated TLS setups.
sslcertmode=require requires SSL_CTX_set_cert_cb(), available since
OpenSSL 1.0.2. Note that LibreSSL does not include it.
Using a connection parameter different than require_auth has come up as
the simplest design because certificate authentication does not rely
directly on any of the AUTH_REQ_* codes, and one may want to require a
certificate to be sent in combination of a given authentication method,
like SCRAM-SHA-256.
TAP tests are added in src/test/ssl/, some of them relying on sslinfo to
check if a certificate has been set. These are compatible across all
the versions of OpenSSL supported on HEAD (currently down to 1.0.1).
Author: Jacob Champion
Reviewed-by: Aleksander Alekseev, Peter Eisentraut, David G. Johnston,
Michael Paquier
Discussion: https://postgr.es/m/9e5a8ccddb8355ea9fa4b75a1e3a9edc88a70cd3.camel@vmware.com
Diffstat (limited to 'src/test/ssl/t')
-rw-r--r-- | src/test/ssl/t/001_ssltests.pl | 42 | ||||
-rw-r--r-- | src/test/ssl/t/003_sslinfo.pl | 24 |
2 files changed, 66 insertions, 0 deletions
diff --git a/src/test/ssl/t/001_ssltests.pl b/src/test/ssl/t/001_ssltests.pl index 3094e27af3a..dc43b8f81aa 100644 --- a/src/test/ssl/t/001_ssltests.pl +++ b/src/test/ssl/t/001_ssltests.pl @@ -42,6 +42,10 @@ my $SERVERHOSTADDR = '127.0.0.1'; # This is the pattern to use in pg_hba.conf to match incoming connections. my $SERVERHOSTCIDR = '127.0.0.1/32'; +# Determine whether build supports sslcertmode=require. +my $supports_sslcertmode_require = + check_pg_config("#define HAVE_SSL_CTX_SET_CERT_CB 1"); + # Allocation of base connection string shared among multiple tests. my $common_connstr; @@ -191,6 +195,22 @@ $node->connect_ok( "$common_connstr sslrootcert=ssl/both-cas-2.crt sslmode=verify-ca", "cert root file that contains two certificates, order 2"); +# sslcertmode=allow and disable should both work without a client certificate. +$node->connect_ok( + "$common_connstr sslrootcert=ssl/root+server_ca.crt sslmode=require sslcertmode=disable", + "connect with sslcertmode=disable"); +$node->connect_ok( + "$common_connstr sslrootcert=ssl/root+server_ca.crt sslmode=require sslcertmode=allow", + "connect with sslcertmode=allow"); + +# sslcertmode=require, however, should fail. +$node->connect_fails( + "$common_connstr sslrootcert=ssl/root+server_ca.crt sslmode=require sslcertmode=require", + "connect with sslcertmode=require fails without a client certificate", + expected_stderr => $supports_sslcertmode_require + ? qr/server accepted connection without a valid SSL certificate/ + : qr/sslcertmode value "require" is not supported/); + # CRL tests # Invalid CRL filename is the same as no CRL, succeeds @@ -538,6 +558,28 @@ $node->connect_ok( "certificate authorization succeeds with correct client cert in encrypted DER format" ); +# correct client cert with sslcertmode=allow or require +if ($supports_sslcertmode_require) +{ + $node->connect_ok( + "$common_connstr user=ssltestuser sslcertmode=require sslcert=ssl/client.crt " + . sslkey('client.key'), + "certificate authorization succeeds with correct client cert and sslcertmode=require" + ); +} +$node->connect_ok( + "$common_connstr user=ssltestuser sslcertmode=allow sslcert=ssl/client.crt " + . sslkey('client.key'), + "certificate authorization succeeds with correct client cert and sslcertmode=allow" +); + +# client cert is not sent if sslcertmode=disable. +$node->connect_fails( + "$common_connstr user=ssltestuser sslcertmode=disable sslcert=ssl/client.crt " + . sslkey('client.key'), + "certificate authorization fails with correct client cert and sslcertmode=disable", + expected_stderr => qr/connection requires a valid client certificate/); + # correct client cert in encrypted PEM with wrong password $node->connect_fails( "$common_connstr user=ssltestuser sslcert=ssl/client.crt " diff --git a/src/test/ssl/t/003_sslinfo.pl b/src/test/ssl/t/003_sslinfo.pl index 3f498fff704..c073625213e 100644 --- a/src/test/ssl/t/003_sslinfo.pl +++ b/src/test/ssl/t/003_sslinfo.pl @@ -43,6 +43,10 @@ my $SERVERHOSTADDR = '127.0.0.1'; # This is the pattern to use in pg_hba.conf to match incoming connections. my $SERVERHOSTCIDR = '127.0.0.1/32'; +# Determine whether build supports sslcertmode=require. +my $supports_sslcertmode_require = + check_pg_config("#define HAVE_SSL_CTX_SET_CERT_CB 1"); + # Allocation of base connection string shared among multiple tests. my $common_connstr; @@ -166,4 +170,24 @@ $result = $node->safe_psql( connstr => $common_connstr); is($result, 'CA:FALSE|t', 'extract extension from cert'); +# Sanity tests for sslcertmode, using ssl_client_cert_present() +my @cases = ( + { opts => "sslcertmode=allow", present => 't' }, + { opts => "sslcertmode=allow sslcert=invalid", present => 'f' }, + { opts => "sslcertmode=disable", present => 'f' },); +if ($supports_sslcertmode_require) +{ + push(@cases, { opts => "sslcertmode=require", present => 't' }); +} + +foreach my $c (@cases) +{ + $result = $node->safe_psql( + "trustdb", + "SELECT ssl_client_cert_present();", + connstr => "$common_connstr dbname=trustdb $c->{'opts'}"); + is($result, $c->{'present'}, + "ssl_client_cert_present() for $c->{'opts'}"); +} + done_testing(); |