Postgresql Ssl Client Certificate

Postgresql Ssl Client Certificate 4,6/5 1811 votes

18.9.1. Basic SetupWith SSL support compiled in, the PostgreSQL server can be started with SSL enabled by setting the parameter to on in postgresql.conf. The server will listen for both normal and SSL connections on the same TCP port, and will negotiate with any connecting client on whether to use SSL.


By default, this is at the client's option; see about how to set up the server to require use of SSL for some or all connections.To start in SSL mode, files containing the server certificate and private key must exist. By default, these files are expected to be named server.crt and server.key, respectively, in the server's data directory, but other names and locations can be specified using the configuration parameters and.On Unix systems, the permissions on server.key must disallow any access to world or group; achieve this by the command chmod 0600 server.key. Alternatively, the file can be owned by root and have group read access (that is, 0640 permissions). That setup is intended for installations where certificate and key files are managed by the operating system. The user under which the PostgreSQL server runs should then be made a member of the group that has access to those certificate and key files.If the data directory allows group read access then certificate files may need to be located outside of the data directory in order to conform to the security requirements outlined above.

Generally, group access is enabled to allow an unprivileged user to backup the database, and in that case the backup software will not be able to read the certificate files and will likely error.If the private key is protected with a passphrase, the server will prompt for the passphrase and will not start until it has been entered. Using a passphrase also disables the ability to change the server's SSL configuration without a server restart.

Furthermore, passphrase-protected private keys cannot be used at all on Windows.The first certificate in server.crt must be the server's certificate because it must match the server's private key. The certificates of “ intermediate” certificate authorities can also be appended to the file. Doing this avoids the necessity of storing intermediate certificates on clients, assuming the root and intermediate certificates were created with v3ca extensions. This allows easier expiration of intermediate certificates.It is not necessary to add the root certificate to server.crt. Instead, clients must have the root certificate of the server's certificate chain. 18.9.2. OpenSSL ConfigurationPostgreSQL reads the system-wide OpenSSL configuration file. By default, this file is named openssl.cnf and is located in the directory reported by openssl version -d.

This default can be overridden by setting environment variable OPENSSLCONF to the name of the desired configuration file.OpenSSL supports a wide range of ciphers and authentication algorithms, of varying strength. While a list of ciphers can be specified in the OpenSSL configuration file, you can specify ciphers specifically for use by the database server by modifying in postgresql.conf. 18.9.3. Using Client CertificatesTo require the client to supply a trusted certificate, place certificates of the root certificate authorities ( CAs) you trust in a file in the data directory, set the parameter in postgresql.conf to the new file name, and add the authentication option clientcert=1 to the appropriate hostssl line(s) in pghba.conf. A certificate will then be requested from the client during SSL connection startup. (See for a description of how to set up certificates on the client.) The server will verify that the client's certificate is signed by one of the trusted certificate authorities.Intermediate certificates that chain up to existing root certificates can also appear in the file if you wish to avoid storing them on clients (assuming the root and intermediate certificates were created with v3ca extensions).

Certificate Revocation List (CRL) entries are also checked if the parameter is set. (See for diagrams showing SSL certificate usage.)The clientcert authentication option is available for all authentication methods, but only in pghba.conf lines specified as hostssl. When clientcert is not specified or is set to 0, the server will still verify any presented client certificates against its CA file, if one is configured — but it will not insist that a client certificate be presented.If you are setting up client certificates, you may wish to use the cert authentication method, so that the certificates control user authentication as well as providing connection security. See for details. (It is not necessary to specify clientcert=1 explicitly when using the cert authentication method.).

FileContentsEffect( $PGDATA/server.crt)server certificatesent to client to indicate server's identity( $PGDATA/server.key)server private keyproves server certificate was sent by the owner; does not indicate certificate owner is trustworthytrusted certificate authoritieschecks that client certificate is signed by a trusted certificate authoritycertificates revoked by certificate authoritiesclient certificate must not be on this listThe server reads these files at server start and whenever the server configuration is reloaded. On Windows systems, they are also re-read whenever a new backend process is spawned for a new client connection.If an error in these files is detected at server start, the server will refuse to start. But if an error is detected during a configuration reload, the files are ignored and the old SSL configuration continues to be used.

On Windows systems, if an error in these files is detected at backend start, that backend will be unable to establish an SSL connection. In all these cases, the error condition is reported in the server log. 18.9.5. Creating CertificatesTo create a simple self-signed certificate for the server, valid for 365 days, use the following OpenSSL command, replacing with the server's host name:openssl req -new -x509 -days 365 -nodes -text -out server.crt -keyout server.key -subj '/CN='Then do:chmod og-rwx server.keybecause the server will reject the file if its permissions are more liberal than this.

Postgresql Ssl Client Certificate Template

I am trying to connect to my PostgreSQL server on AWS using SSL from the OpenSSL sclient on XP. I can connect to a third party using this sclient. On both the server and XP, I am using openssl version 0.9.8.k.When I try to connect to my server, I get the result:CONNECTED(000:error:140790E5:SSL routines:SSL23WRITE:ssl handshake failure:s23lib.c:188:In the Postgres log, I see the entry:2009-10-30 13:58:08 UTC LOG: invalid length of startup packetI'd appreciate any suggestions about where to look to get this working. Maybe there is a public AWS image I can look at on which PostgreSQL is known to work? You didn't specify why you wanted to use sclient.If it is to interact with the database, any decent client will do. Psql can be called with the sslmode=require option. See man psql.If it is to check the SSL certificate (which is why I came across your question), it still doesn't work with sclient as Magnus pointed out 7 years ago.However, I found this python script which can retrieve the SSL certificate:For example to check certificate dates: openssl x509 -noout -dates.