Log details for client certificate failures
authorPeter Eisentraut <[email protected]>
Fri, 15 Jul 2022 14:18:32 +0000 (16:18 +0200)
committerPeter Eisentraut <[email protected]>
Fri, 15 Jul 2022 15:04:48 +0000 (17:04 +0200)
commit3a0e385048adf84d212e010d82db16228e5f4060
tree25881097ea1dff7495487401f17fd62e78842a03
parent507ba16b28c724046551ef8757ebccb8b4fb6da6
Log details for client certificate failures

Currently, debugging client certificate verification failures is
mostly limited to looking at the TLS alert code on the client side.
For simple deployments, sometimes it's enough to see "sslv3 alert
certificate revoked" and know exactly what needs to be fixed, but if
you add any more complexity (multiple CA layers, misconfigured CA
certificates, etc.), trying to debug what happened based on the TLS
alert alone can be an exercise in frustration.

Luckily, the server has more information about exactly what failed in
the chain, and we already have the requisite callback implemented as a
stub.  We fill that in, collect the data, and pass the constructed
error message back to the main code via a static variable.  This lets
us add our error details directly to the final "could not accept SSL
connection" log message, as opposed to issuing intermediate LOGs.

It ends up looking like

    LOG:  connection received: host=localhost port=43112
    LOG:  could not accept SSL connection: certificate verify failed
    DETAIL:  Client certificate verification failed at depth 1: unable to get local issuer certificate.
            Failed certificate data (unverified): subject "/CN=Test CA for PostgreSQL SSL regression test client certs", serial number 2315134995201656577, issuer "/CN=Test root CA for PostgreSQL SSL regression test suite".

The length of the Subject and Issuer strings is limited to prevent
malicious client certs from spamming the logs.  In case the truncation
makes things ambiguous, the certificate's serial number is also
logged.

Author: Jacob Champion <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/d13c4a5787c2a3f83705124f0391e0738c796751[email protected]
src/backend/libpq/be-secure-openssl.c
src/test/ssl/conf/client-long.config[new file with mode: 0644]
src/test/ssl/ssl/client-long.crt[new file with mode: 0644]
src/test/ssl/ssl/client-long.key[new file with mode: 0644]
src/test/ssl/sslfiles.mk
src/test/ssl/t/001_ssltests.pl
src/test/ssl/t/SSL/Backend/OpenSSL.pm