| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
=pod |
| 5 |
|
| 6 |
=head1 NAME |
| 7 |
|
| 8 |
SSL::Backend::OpenSSL |
| 9 |
|
| 10 |
=head1 SYNOPSIS |
| 11 |
|
| 12 |
use SSL::Backend::OpenSSL; |
| 13 |
|
| 14 |
my $backend = SSL::Backend::OpenSSL->new(); |
| 15 |
|
| 16 |
$backend->init($pgdata); |
| 17 |
|
| 18 |
=head1 DESCRIPTION |
| 19 |
|
| 20 |
SSL::Backend::OpenSSL implements the library specific parts in SSL::Server |
| 21 |
for a PostgreSQL cluster compiled against OpenSSL. |
| 22 |
|
| 23 |
=cut |
| 24 |
|
| 25 |
package SSL::Backend::OpenSSL; |
| 26 |
|
| 27 |
use strict; |
| 28 |
use warnings FATAL => 'all'; |
| 29 |
use PostgreSQL::Test::Utils; |
| 30 |
use File::Basename; |
| 31 |
use File::Copy; |
| 32 |
|
| 33 |
=pod |
| 34 |
|
| 35 |
=head1 METHODS |
| 36 |
|
| 37 |
=over |
| 38 |
|
| 39 |
=item SSL::Backend::OpenSSL->new() |
| 40 |
|
| 41 |
Create a new instance of the OpenSSL backend. |
| 42 |
|
| 43 |
=cut |
| 44 |
|
| 45 |
sub new |
| 46 |
{ |
| 47 |
my ($class) = @_; |
| 48 |
|
| 49 |
my $self = { _library => 'OpenSSL', key => {} }; |
| 50 |
|
| 51 |
bless $self, $class; |
| 52 |
|
| 53 |
return $self; |
| 54 |
} |
| 55 |
|
| 56 |
=pod |
| 57 |
|
| 58 |
=item $backend->init(pgdata) |
| 59 |
|
| 60 |
Install certificates, keys and CRL files required to run the tests against an |
| 61 |
OpenSSL backend. |
| 62 |
|
| 63 |
=cut |
| 64 |
|
| 65 |
sub init |
| 66 |
{ |
| 67 |
my ($self, $pgdata) = @_; |
| 68 |
|
| 69 |
# Install server certificates and keys into the cluster data directory. |
| 70 |
_copy_files("ssl/server-*.crt", $pgdata); |
| 71 |
_copy_files("ssl/server-*.key", $pgdata); |
| 72 |
chmod(0600, glob "$pgdata/server-*.key") |
| 73 |
or die "failed to change permissions on server keys: $!"; |
| 74 |
_copy_files("ssl/root+client_ca.crt", $pgdata); |
| 75 |
_copy_files("ssl/root_ca.crt", $pgdata); |
| 76 |
_copy_files("ssl/root+client.crl", $pgdata); |
| 77 |
mkdir("$pgdata/root+client-crldir") |
| 78 |
or die "unable to create server CRL dir $pgdata/root+client-crldir: $!"; |
| 79 |
_copy_files("ssl/root+client-crldir/*", "$pgdata/root+client-crldir/"); |
| 80 |
|
| 81 |
# The client's private key must not be world-readable, so take a copy |
| 82 |
# of the key stored in the code tree and update its permissions. |
| 83 |
# |
| 84 |
# This changes to using keys stored in a temporary path for the rest of |
| 85 |
# the tests. To get the full path for inclusion in connection strings, the |
| 86 |
# %key hash can be interrogated. |
| 87 |
my $cert_tempdir = PostgreSQL::Test::Utils::tempdir(); |
| 88 |
my @keys = ( |
| 89 |
"client.key", "client-revoked.key", |
| 90 |
"client-der.key", "client-encrypted-pem.key", |
| 91 |
"client-encrypted-der.key", "client-dn.key", |
| 92 |
"client_ext.key", "client-long.key", |
| 93 |
"client-revoked-utf8.key"); |
| 94 |
foreach my $keyfile (@keys) |
| 95 |
{ |
| 96 |
copy("ssl/$keyfile", "$cert_tempdir/$keyfile") |
| 97 |
or die |
| 98 |
"couldn't copy ssl/$keyfile to $cert_tempdir/$keyfile for permissions change: $!"; |
| 99 |
chmod 0600, "$cert_tempdir/$keyfile" |
| 100 |
or die "failed to change permissions on $cert_tempdir/$keyfile: $!"; |
| 101 |
$self->{key}->{$keyfile} = "$cert_tempdir/$keyfile"; |
| 102 |
$self->{key}->{$keyfile} =~ s!\\!/!g |
| 103 |
if $PostgreSQL::Test::Utils::windows_os; |
| 104 |
} |
| 105 |
|
| 106 |
# Also make a copy of client.key explicitly world-readable in order to be |
| 107 |
# able to test incorrect permissions. We can't necessarily rely on the |
| 108 |
# file in the source tree having those permissions. |
| 109 |
copy("ssl/client.key", "$cert_tempdir/client_wrongperms.key") |
| 110 |
or die |
| 111 |
"couldn't copy ssl/client_key to $cert_tempdir/client_wrongperms.key for permission change: $!"; |
| 112 |
chmod 0644, "$cert_tempdir/client_wrongperms.key" |
| 113 |
or die |
| 114 |
"failed to change permissions on $cert_tempdir/client_wrongperms.key: $!"; |
| 115 |
$self->{key}->{'client_wrongperms.key'} = |
| 116 |
"$cert_tempdir/client_wrongperms.key"; |
| 117 |
$self->{key}->{'client_wrongperms.key'} =~ s!\\!/!g |
| 118 |
if $PostgreSQL::Test::Utils::windows_os; |
| 119 |
} |
| 120 |
|
| 121 |
=pod |
| 122 |
|
| 123 |
=item $backend->get_sslkey(key) |
| 124 |
|
| 125 |
Get an 'sslkey' connection string parameter for the specified B<key> which has |
| 126 |
the correct path for direct inclusion in a connection string. |
| 127 |
|
| 128 |
=cut |
| 129 |
|
| 130 |
sub get_sslkey |
| 131 |
{ |
| 132 |
my ($self, $keyfile) = @_; |
| 133 |
|
| 134 |
return " sslkey=$self->{key}->{$keyfile}"; |
| 135 |
} |
| 136 |
|
| 137 |
=pod |
| 138 |
|
| 139 |
=item $backend->set_server_cert(params) |
| 140 |
|
| 141 |
Change the configuration to use given server cert, key and crl file(s). The |
| 142 |
following parameters are supported: |
| 143 |
|
| 144 |
=over |
| 145 |
|
| 146 |
=item cafile => B<value> |
| 147 |
|
| 148 |
The CA certificate file to use for the C<ssl_ca_file> GUC. If omitted it will |
| 149 |
default to 'root+client_ca.crt'. |
| 150 |
|
| 151 |
=item certfile => B<value> |
| 152 |
|
| 153 |
The server certificate file to use for the C<ssl_cert_file> GUC. |
| 154 |
|
| 155 |
=item keyfile => B<value> |
| 156 |
|
| 157 |
The private key file to use for the C<ssl_key_file GUC>. If omitted it will |
| 158 |
default to the B<certfile>.key. |
| 159 |
|
| 160 |
=item crlfile => B<value> |
| 161 |
|
| 162 |
The CRL file to use for the C<ssl_crl_file> GUC. If omitted it will default to |
| 163 |
'root+client.crl'. |
| 164 |
|
| 165 |
=item crldir => B<value> |
| 166 |
|
| 167 |
The CRL directory to use for the C<ssl_crl_dir> GUC. If omitted, |
| 168 |
C<no ssl_crl_dir> configuration parameter will be set. |
| 169 |
|
| 170 |
=back |
| 171 |
|
| 172 |
=cut |
| 173 |
|
| 174 |
sub set_server_cert |
| 175 |
{ |
| 176 |
my ($self, $params) = @_; |
| 177 |
|
| 178 |
$params->{cafile} = 'root+client_ca' unless defined $params->{cafile}; |
| 179 |
$params->{crlfile} = 'root+client.crl' unless defined $params->{crlfile}; |
| 180 |
$params->{keyfile} = $params->{certfile} |
| 181 |
unless defined $params->{keyfile}; |
| 182 |
|
| 183 |
my $sslconf = |
| 184 |
"ssl_ca_file='$params->{cafile}.crt'\n" |
| 185 |
. "ssl_cert_file='$params->{certfile}.crt'\n" |
| 186 |
. "ssl_key_file='$params->{keyfile}.key'\n" |
| 187 |
. "ssl_crl_file='$params->{crlfile}'\n"; |
| 188 |
$sslconf .= "ssl_crl_dir='$params->{crldir}'\n" |
| 189 |
if defined $params->{crldir}; |
| 190 |
|
| 191 |
return $sslconf; |
| 192 |
} |
| 193 |
|
| 194 |
=pod |
| 195 |
|
| 196 |
=item $backend->get_library() |
| 197 |
|
| 198 |
Returns the name of the SSL library, in this case "OpenSSL". |
| 199 |
|
| 200 |
=cut |
| 201 |
|
| 202 |
sub get_library |
| 203 |
{ |
| 204 |
my ($self) = @_; |
| 205 |
|
| 206 |
return $self->{_library}; |
| 207 |
} |
| 208 |
|
| 209 |
=pod |
| 210 |
|
| 211 |
=item $backend->library_is_libressl() |
| 212 |
|
| 213 |
Detect whether the SSL library is LibreSSL. |
| 214 |
|
| 215 |
=cut |
| 216 |
|
| 217 |
sub library_is_libressl |
| 218 |
{ |
| 219 |
my ($self) = @_; |
| 220 |
|
| 221 |
# The HAVE_SSL_CTX_SET_CERT_CB macro isn't defined for LibreSSL. |
| 222 |
# We may eventually need a less-bogus heuristic. |
| 223 |
return not check_pg_config("#define HAVE_SSL_CTX_SET_CERT_CB 1"); |
| 224 |
} |
| 225 |
|
| 226 |
# Internal method for copying a set of files, taking into account wildcards |
| 227 |
sub _copy_files |
| 228 |
{ |
| 229 |
my $orig = shift; |
| 230 |
my $dest = shift; |
| 231 |
|
| 232 |
my @orig_files = glob $orig; |
| 233 |
foreach my $orig_file (@orig_files) |
| 234 |
{ |
| 235 |
my $base_file = basename($orig_file); |
| 236 |
copy($orig_file, "$dest/$base_file") |
| 237 |
or die "Could not copy $orig_file to $dest"; |
| 238 |
} |
| 239 |
return; |
| 240 |
} |
| 241 |
|
| 242 |
=pod |
| 243 |
|
| 244 |
=back |
| 245 |
|
| 246 |
=cut |
| 247 |
|
| 248 |
1; |