Skip to content

Commit 5fd6105

Browse files
committed
Fix handling of SCRAM-SHA-256's channel binding with RSA-PSS certificates
OpenSSL 1.1.1 and newer versions have added support for RSA-PSS certificates, which requires the use of a specific routine in OpenSSL to determine which hash function to use when compiling it when using channel binding in SCRAM-SHA-256. X509_get_signature_nid(), that is the original routine the channel binding code has relied on, is not able to determine which hash algorithm to use for such certificates. However, X509_get_signature_info(), new to OpenSSL 1.1.1, is able to do it. This commit switches the channel binding logic to rely on X509_get_signature_info() over X509_get_signature_nid(), which would be the choice when building with 1.1.1 or newer. The error could have been triggered on the client or the server, hence libpq and the backend need to have their related code paths patched. Note that attempting to load an RSA-PSS certificate with OpenSSL 1.1.0 or older leads to a failure due to an unsupported algorithm. The discovery of relying on X509_get_signature_info() comes from Jacob, the tests have been written by Heikki (with few tweaks from me), while I have bundled the whole together while adding the bits needed for MSVC and meson. This issue exists since channel binding exists, so backpatch all the way down. Some tests are added in 15~, triggered if compiling with OpenSSL 1.1.1 or newer, where the certificate and key files can easily be generated for RSA-PSS. Reported-by: Gunnar "Nick" Bluth Author: Jacob Champion, Heikki Linnakangas Discussion: https://postgr.es/m/17760-b6c61e752ec07060@postgresql.org Backpatch-through: 11
1 parent a9fa6d7 commit 5fd6105

File tree

14 files changed

+145
-11
lines changed

14 files changed

+145
-11
lines changed

configure

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13260,6 +13260,18 @@ if test "x$ac_cv_func_CRYPTO_lock" = xyes; then :
1326013260
#define HAVE_CRYPTO_LOCK 1
1326113261
_ACEOF
1326213262

13263+
fi
13264+
done
13265+
13266+
# Function introduced in OpenSSL 1.1.1.
13267+
for ac_func in X509_get_signature_info
13268+
do :
13269+
ac_fn_c_check_func "$LINENO" "X509_get_signature_info" "ac_cv_func_X509_get_signature_info"
13270+
if test "x$ac_cv_func_X509_get_signature_info" = xyes; then :
13271+
cat >>confdefs.h <<_ACEOF
13272+
#define HAVE_X509_GET_SIGNATURE_INFO 1
13273+
_ACEOF
13274+
1326313275
fi
1326413276
done
1326513277

configure.ac

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,8 @@ if test "$with_ssl" = openssl ; then
13521352
# thread-safety. In 1.1.0, it's no longer required, and CRYPTO_lock()
13531353
# function was removed.
13541354
AC_CHECK_FUNCS([CRYPTO_lock])
1355+
# Function introduced in OpenSSL 1.1.1.
1356+
AC_CHECK_FUNCS([X509_get_signature_info])
13551357
AC_DEFINE([USE_OPENSSL], 1, [Define to 1 to build with OpenSSL support. (--with-ssl=openssl)])
13561358
elif test "$with_ssl" != no ; then
13571359
AC_MSG_ERROR([--with-ssl must specify openssl])

src/backend/libpq/be-secure-openssl.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,7 +1320,7 @@ be_tls_get_peer_serial(Port *port, char *ptr, size_t len)
13201320
ptr[0] = '\0';
13211321
}
13221322

1323-
#ifdef HAVE_X509_GET_SIGNATURE_NID
1323+
#if defined(HAVE_X509_GET_SIGNATURE_NID) || defined(HAVE_X509_GET_SIGNATURE_INFO)
13241324
char *
13251325
be_tls_get_certificate_hash(Port *port, size_t *len)
13261326
{
@@ -1338,10 +1338,15 @@ be_tls_get_certificate_hash(Port *port, size_t *len)
13381338

13391339
/*
13401340
* Get the signature algorithm of the certificate to determine the hash
1341-
* algorithm to use for the result.
1341+
* algorithm to use for the result. Prefer X509_get_signature_info(),
1342+
* introduced in OpenSSL 1.1.1, which can handle RSA-PSS signatures.
13421343
*/
1344+
#if HAVE_X509_GET_SIGNATURE_INFO
1345+
if (!X509_get_signature_info(server_cert, &algo_nid, NULL, NULL, NULL))
1346+
#else
13431347
if (!OBJ_find_sigid_algs(X509_get_signature_nid(server_cert),
13441348
&algo_nid, NULL))
1349+
#endif
13451350
elog(ERROR, "could not determine server certificate signature algorithm");
13461351

13471352
/*

src/include/libpq/libpq-be.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ extern void be_tls_get_peer_serial(Port *port, char *ptr, size_t len);
300300
* This is not supported with old versions of OpenSSL that don't have
301301
* the X509_get_signature_nid() function.
302302
*/
303-
#if defined(USE_OPENSSL) && defined(HAVE_X509_GET_SIGNATURE_NID)
303+
#if defined(USE_OPENSSL) && (defined(HAVE_X509_GET_SIGNATURE_NID) || defined(HAVE_X509_GET_SIGNATURE_INFO))
304304
#define HAVE_BE_TLS_GET_CERTIFICATE_HASH
305305
extern char *be_tls_get_certificate_hash(Port *port, size_t *len);
306306
#endif

src/include/pg_config.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,9 @@
718718
/* Define to 1 if you have the `writev' function. */
719719
#undef HAVE_WRITEV
720720

721+
/* Define to 1 if you have the `X509_get_signature_info' function. */
722+
#undef HAVE_X509_GET_SIGNATURE_INFO
723+
721724
/* Define to 1 if you have the `X509_get_signature_nid' function. */
722725
#undef HAVE_X509_GET_SIGNATURE_NID
723726

src/interfaces/libpq/fe-secure-openssl.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ pgtls_write(PGconn *conn, const void *ptr, size_t len)
380380
return n;
381381
}
382382

383-
#ifdef HAVE_X509_GET_SIGNATURE_NID
383+
#if defined(HAVE_X509_GET_SIGNATURE_NID) || defined(HAVE_X509_GET_SIGNATURE_INFO)
384384
char *
385385
pgtls_get_peer_certificate_hash(PGconn *conn, size_t *len)
386386
{
@@ -400,10 +400,15 @@ pgtls_get_peer_certificate_hash(PGconn *conn, size_t *len)
400400

401401
/*
402402
* Get the signature algorithm of the certificate to determine the hash
403-
* algorithm to use for the result.
403+
* algorithm to use for the result. Prefer X509_get_signature_info(),
404+
* introduced in OpenSSL 1.1.1, which can handle RSA-PSS signatures.
404405
*/
406+
#if HAVE_X509_GET_SIGNATURE_INFO
407+
if (!X509_get_signature_info(peer_cert, &algo_nid, NULL, NULL, NULL))
408+
#else
405409
if (!OBJ_find_sigid_algs(X509_get_signature_nid(peer_cert),
406410
&algo_nid, NULL))
411+
#endif
407412
{
408413
appendPQExpBufferStr(&conn->errorMessage,
409414
libpq_gettext("could not determine server certificate signature algorithm\n"));

src/interfaces/libpq/libpq-int.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ extern ssize_t pgtls_write(PGconn *conn, const void *ptr, size_t len);
804804
* This is not supported with old versions of OpenSSL that don't have
805805
* the X509_get_signature_nid() function.
806806
*/
807-
#if defined(USE_OPENSSL) && defined(HAVE_X509_GET_SIGNATURE_NID)
807+
#if defined(USE_OPENSSL) && (defined(HAVE_X509_GET_SIGNATURE_NID) || defined(HAVE_X509_GET_SIGNATURE_INFO))
808808
#define HAVE_PGTLS_GET_PEER_CERTIFICATE_HASH
809809
extern char *pgtls_get_peer_certificate_hash(PGconn *conn, size_t *len);
810810
#endif

src/test/ssl/README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ ssl/ subdirectory. The Makefile also contains a rule, "make sslfiles", to
9292
recreate them if you need to make changes. "make sslfiles-clean" is required
9393
in order to recreate the full set of keypairs and certificates. To rebuild
9494
separate files, touch (or remove) the files in question and run "make sslfiles".
95+
This step requires at least OpenSSL 1.1.1.
9596

9697
TODO
9798
====
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# An OpenSSL format CSR config file for creating a server certificate.
2+
#
3+
# This is identical to server-cn-only certificate, but we specify
4+
# RSA-PSS as the algorithm on the command line.
5+
6+
[ req ]
7+
distinguished_name = req_distinguished_name
8+
prompt = no
9+
10+
[ req_distinguished_name ]
11+
CN = common-name.pg-ssltest.test
12+
OU = PostgreSQL test suite
13+
14+
# No Subject Alternative Names

src/test/ssl/ssl/server-rsapss.crt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-----BEGIN CER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