pgsql: Log details for client certificate failures - Mailing list pgsql-committers

From Peter Eisentraut
Subject pgsql: Log details for client certificate failures
Date
Msg-id E1oCNLk-000LCH-Af@gemulon.postgresql.org
Whole thread Raw
Responses Re: pgsql: Log details for client certificate failures
List pgsql-committers
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 <pchampion@vmware.com>
Discussion: https://www.postgresql.org/message-id/flat/d13c4a5787c2a3f83705124f0391e0738c796751.camel@vmware.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/3a0e385048adf84d212e010d82db16228e5f4060

Modified Files
--------------
src/backend/libpq/be-secure-openssl.c | 112 +++++++++++++++++++++++++++++++++-
src/test/ssl/conf/client-long.config  |  14 +++++
src/test/ssl/ssl/client-long.crt      |  20 ++++++
src/test/ssl/ssl/client-long.key      |  27 ++++++++
src/test/ssl/sslfiles.mk              |   2 +-
src/test/ssl/t/001_ssltests.pl        |  40 +++++++++++-
src/test/ssl/t/SSL/Backend/OpenSSL.pm |   2 +-
7 files changed, 210 insertions(+), 7 deletions(-)


pgsql-committers by date:

Previous
From: Peter Eisentraut
Date:
Subject: pgsql: Convert macros to static inline functions (xlog_internal.h)
Next
From: Thomas Munro
Date:
Subject: pgsql: Make dsm_impl_posix_resize more future-proof.