LCOV - differential code coverage report
Current view: top level - src/interfaces/libpq - fe-secure-openssl.c (source / functions) Coverage Total Hit UBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 55.1 % 691 381 310 381
Current Date: 2025-09-06 07:49:51 +0900 Functions: 83.9 % 31 26 5 26
Baseline: lcov-20250906-005545-baseline Branches: 51.4 % 428 220 208 220
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 91.8 % 49 45 4 45
(360..) days: 52.3 % 642 336 306 336
Function coverage date bins:
(30,360] days: 100.0 % 6 6 6
(360..) days: 80.0 % 25 20 5 20
Branch coverage date bins:
(30,360] days: 66.7 % 21 14 7 14
(360..) days: 50.6 % 407 206 201 206

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * fe-secure-openssl.c
                                  4                 :                :  *    OpenSSL support
                                  5                 :                :  *
                                  6                 :                :  *
                                  7                 :                :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
                                  8                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  9                 :                :  *
                                 10                 :                :  *
                                 11                 :                :  * IDENTIFICATION
                                 12                 :                :  *    src/interfaces/libpq/fe-secure-openssl.c
                                 13                 :                :  *
                                 14                 :                :  * NOTES
                                 15                 :                :  *
                                 16                 :                :  *    We don't provide informational callbacks here (like
                                 17                 :                :  *    info_cb() in be-secure-openssl.c), since there's no good mechanism to
                                 18                 :                :  *    display such information to the user.
                                 19                 :                :  *
                                 20                 :                :  *-------------------------------------------------------------------------
                                 21                 :                :  */
                                 22                 :                : 
                                 23                 :                : #include "postgres_fe.h"
                                 24                 :                : 
                                 25                 :                : #include <signal.h>
                                 26                 :                : #include <fcntl.h>
                                 27                 :                : #include <ctype.h>
                                 28                 :                : 
                                 29                 :                : #include "libpq-fe.h"
                                 30                 :                : #include "fe-auth.h"
                                 31                 :                : #include "fe-secure-common.h"
                                 32                 :                : #include "libpq-int.h"
                                 33                 :                : 
                                 34                 :                : #ifdef WIN32
                                 35                 :                : #include "win32.h"
                                 36                 :                : #else
                                 37                 :                : #include <sys/socket.h>
                                 38                 :                : #include <unistd.h>
                                 39                 :                : #include <netdb.h>
                                 40                 :                : #include <netinet/in.h>
                                 41                 :                : #include <netinet/tcp.h>
                                 42                 :                : #include <arpa/inet.h>
                                 43                 :                : #endif
                                 44                 :                : 
                                 45                 :                : #include <sys/stat.h>
                                 46                 :                : 
                                 47                 :                : #ifdef WIN32
                                 48                 :                : #include "pthread-win32.h"
                                 49                 :                : #else
                                 50                 :                : #include <pthread.h>
                                 51                 :                : #endif
                                 52                 :                : 
                                 53                 :                : /*
                                 54                 :                :  * These SSL-related #includes must come after all system-provided headers.
                                 55                 :                :  * This ensures that OpenSSL can take care of conflicts with Windows'
                                 56                 :                :  * <wincrypt.h> by #undef'ing the conflicting macros.  (We don't directly
                                 57                 :                :  * include <wincrypt.h>, but some other Windows headers do.)
                                 58                 :                :  */
                                 59                 :                : #include "common/openssl.h"
                                 60                 :                : #include <openssl/ssl.h>
                                 61                 :                : #include <openssl/conf.h>
                                 62                 :                : #ifdef USE_SSL_ENGINE
                                 63                 :                : #include <openssl/engine.h>
                                 64                 :                : #endif
                                 65                 :                : #include <openssl/x509v3.h>
                                 66                 :                : 
                                 67                 :                : 
                                 68                 :                : static int  verify_cb(int ok, X509_STORE_CTX *ctx);
                                 69                 :                : static int  openssl_verify_peer_name_matches_certificate_name(PGconn *conn,
                                 70                 :                :                                                               ASN1_STRING *name_entry,
                                 71                 :                :                                                               char **store_name);
                                 72                 :                : static int  openssl_verify_peer_name_matches_certificate_ip(PGconn *conn,
                                 73                 :                :                                                             ASN1_OCTET_STRING *addr_entry,
                                 74                 :                :                                                             char **store_name);
                                 75                 :                : static int  initialize_SSL(PGconn *conn);
                                 76                 :                : static PostgresPollingStatusType open_client_SSL(PGconn *conn);
                                 77                 :                : static char *SSLerrmessage(unsigned long ecode);
                                 78                 :                : static void SSLerrfree(char *buf);
                                 79                 :                : static int  PQssl_passwd_cb(char *buf, int size, int rwflag, void *userdata);
                                 80                 :                : 
                                 81                 :                : static int  pgconn_bio_read(BIO *h, char *buf, int size);
                                 82                 :                : static int  pgconn_bio_write(BIO *h, const char *buf, int size);
                                 83                 :                : static BIO_METHOD *pgconn_bio_method(void);
                                 84                 :                : static int  ssl_set_pgconn_bio(PGconn *conn);
                                 85                 :                : 
                                 86                 :                : static pthread_mutex_t ssl_config_mutex = PTHREAD_MUTEX_INITIALIZER;
                                 87                 :                : 
                                 88                 :                : static PQsslKeyPassHook_OpenSSL_type PQsslKeyPassHook = NULL;
                                 89                 :                : static int  ssl_protocol_version_to_openssl(const char *protocol);
                                 90                 :                : 
                                 91                 :                : /* ------------------------------------------------------------ */
                                 92                 :                : /*           Procedures common to all secure sessions           */
                                 93                 :                : /* ------------------------------------------------------------ */
                                 94                 :                : 
                                 95                 :                : PostgresPollingStatusType
 4044 heikki.linnakangas@i       96                 :CBC         298 : pgtls_open_client(PGconn *conn)
                                 97                 :                : {
                                 98                 :                :     /* First time through? */
                                 99         [ +  + ]:            298 :     if (conn->ssl == NULL)
                                100                 :                :     {
                                101                 :                :         /*
                                102                 :                :          * Create a connection-specific SSL object, and load client
                                103                 :                :          * certificate, private key, and trusted CA certs.
                                104                 :                :          */
                                105         [ +  + ]:            158 :         if (initialize_SSL(conn) != 0)
                                106                 :                :         {
                                107                 :                :             /* initialize_SSL already put a message in conn->errorMessage */
                                108                 :              4 :             pgtls_close(conn);
                                109                 :              4 :             return PGRES_POLLING_FAILED;
                                110                 :                :         }
                                111                 :                :     }
                                112                 :                : 
                                113                 :                :     /* Begin or continue the actual handshake */
                                114                 :            294 :     return open_client_SSL(conn);
                                115                 :                : }
                                116                 :                : 
                                117                 :                : ssize_t
                                118                 :            345 : pgtls_read(PGconn *conn, void *ptr, size_t len)
                                119                 :                : {
                                120                 :                :     ssize_t     n;
                                121                 :            345 :     int         result_errno = 0;
                                122                 :                :     char        sebuf[PG_STRERROR_R_BUFLEN];
                                123                 :                :     int         err;
                                124                 :                :     unsigned long ecode;
                                125                 :                : 
                                126                 :            345 : rloop:
                                127                 :                : 
                                128                 :                :     /*
                                129                 :                :      * Prepare to call SSL_get_error() by clearing thread's OpenSSL error
                                130                 :                :      * queue.  In general, the current thread's error queue must be empty
                                131                 :                :      * before the TLS/SSL I/O operation is attempted, or SSL_get_error() will
                                132                 :                :      * not work reliably.  Since the possibility exists that other OpenSSL
                                133                 :                :      * clients running in the same thread but not under our control will fail
                                134                 :                :      * to call ERR_get_error() themselves (after their own I/O operations),
                                135                 :                :      * pro-actively clear the per-thread error queue now.
                                136                 :                :      */
                                137                 :            345 :     SOCK_ERRNO_SET(0);
 3438 peter_e@gmx.net           138                 :            345 :     ERR_clear_error();
 4044 heikki.linnakangas@i      139                 :            345 :     n = SSL_read(conn->ssl, ptr, len);
                                140                 :            345 :     err = SSL_get_error(conn->ssl, n);
                                141                 :                : 
                                142                 :                :     /*
                                143                 :                :      * Other clients of OpenSSL may fail to call ERR_get_error(), but we
                                144                 :                :      * always do, so as to not cause problems for OpenSSL clients that don't
                                145                 :                :      * call ERR_clear_error() defensively.  Be sure that this happens by
                                146                 :                :      * calling now.  SSL_get_error() relies on the OpenSSL per-thread error
                                147                 :                :      * queue being intact, so this is the earliest possible point
                                148                 :                :      * ERR_get_error() may be called.
                                149                 :                :      */
 3438 peter_e@gmx.net           150   [ +  +  -  + ]:            345 :     ecode = (err != SSL_ERROR_NONE || n < 0) ? ERR_get_error() : 0;
 4044 heikki.linnakangas@i      151   [ +  +  -  -  :            345 :     switch (err)
                                           +  -  - ]
                                152                 :                :     {
                                153                 :            237 :         case SSL_ERROR_NONE:
                                154         [ -  + ]:            237 :             if (n < 0)
                                155                 :                :             {
                                156                 :                :                 /* Not supposed to happen, so we don't translate the msg */
 1699 tgl@sss.pgh.pa.us         157                 :UBC           0 :                 appendPQExpBufferStr(&conn->errorMessage,
                                158                 :                :                                      "SSL_read failed but did not provide error information\n");
                                159                 :                :                 /* assume the connection is broken */
 4044 heikki.linnakangas@i      160                 :              0 :                 result_errno = ECONNRESET;
                                161                 :                :             }
 4044 heikki.linnakangas@i      162                 :CBC         237 :             break;
                                163                 :            102 :         case SSL_ERROR_WANT_READ:
                                164                 :            102 :             n = 0;
                                165                 :            102 :             break;
 4044 heikki.linnakangas@i      166                 :UBC           0 :         case SSL_ERROR_WANT_WRITE:
                                167                 :                : 
                                168                 :                :             /*
                                169                 :                :              * Returning 0 here would cause caller to wait for read-ready,
                                170                 :                :              * which is not correct since what SSL wants is wait for
                                171                 :                :              * write-ready.  The former could get us stuck in an infinite
                                172                 :                :              * wait, so don't risk it; busy-loop instead.
                                173                 :                :              */
                                174                 :              0 :             goto rloop;
                                175                 :              0 :         case SSL_ERROR_SYSCALL:
  635 tgl@sss.pgh.pa.us         176   [ #  #  #  # ]:              0 :             if (n < 0 && SOCK_ERRNO != 0)
                                177                 :                :             {
 4044 heikki.linnakangas@i      178                 :              0 :                 result_errno = SOCK_ERRNO;
                                179   [ #  #  #  # ]:              0 :                 if (result_errno == EPIPE ||
                                180                 :                :                     result_errno == ECONNRESET)
 1026 peter@eisentraut.org      181                 :              0 :                     libpq_append_conn_error(conn, "server closed the connection unexpectedly\n"
                                182                 :                :                                             "\tThis probably means the server terminated abnormally\n"
                                183                 :                :                                             "\tbefore or while processing the request.");
                                184                 :                :                 else
                                185                 :              0 :                     libpq_append_conn_error(conn, "SSL SYSCALL error: %s",
                                186                 :                :                                             SOCK_STRERROR(result_errno,
                                187                 :                :                                                           sebuf, sizeof(sebuf)));
                                188                 :                :             }
                                189                 :                :             else
                                190                 :                :             {
                                191                 :              0 :                 libpq_append_conn_error(conn, "SSL SYSCALL error: EOF detected");
                                192                 :                :                 /* assume the connection is broken */
 4044 heikki.linnakangas@i      193                 :              0 :                 result_errno = ECONNRESET;
                                194                 :              0 :                 n = -1;
                                195                 :                :             }
                                196                 :              0 :             break;
 4044 heikki.linnakangas@i      197                 :CBC           6 :         case SSL_ERROR_SSL:
                                198                 :                :             {
 3438 peter_e@gmx.net           199                 :              6 :                 char       *errm = SSLerrmessage(ecode);
                                200                 :                : 
 1026 peter@eisentraut.org      201                 :              6 :                 libpq_append_conn_error(conn, "SSL error: %s", errm);
 4044 heikki.linnakangas@i      202                 :              6 :                 SSLerrfree(errm);
                                203                 :                :                 /* assume the connection is broken */
                                204                 :              6 :                 result_errno = ECONNRESET;
                                205                 :              6 :                 n = -1;
                                206                 :              6 :                 break;
                                207                 :                :             }
 4044 heikki.linnakangas@i      208                 :UBC           0 :         case SSL_ERROR_ZERO_RETURN:
                                209                 :                : 
                                210                 :                :             /*
                                211                 :                :              * Per OpenSSL documentation, this error code is only returned for
                                212                 :                :              * a clean connection closure, so we should not report it as a
                                213                 :                :              * server crash.
                                214                 :                :              */
 1026 peter@eisentraut.org      215                 :              0 :             libpq_append_conn_error(conn, "SSL connection has been closed unexpectedly");
 4044 heikki.linnakangas@i      216                 :              0 :             result_errno = ECONNRESET;
                                217                 :              0 :             n = -1;
                                218                 :              0 :             break;
                                219                 :              0 :         default:
 1026 peter@eisentraut.org      220                 :              0 :             libpq_append_conn_error(conn, "unrecognized SSL error code: %d", err);
                                221                 :                :             /* assume the connection is broken */
 4044 heikki.linnakangas@i      222                 :              0 :             result_errno = ECONNRESET;
                                223                 :              0 :             n = -1;
                                224                 :              0 :             break;
                                225                 :                :     }
                                226                 :                : 
                                227                 :                :     /* ensure we return the intended errno to caller */
 4044 heikki.linnakangas@i      228                 :CBC         345 :     SOCK_ERRNO_SET(result_errno);
                                229                 :                : 
                                230                 :            345 :     return n;
                                231                 :                : }
                                232                 :                : 
                                233                 :                : bool
 2788 peter_e@gmx.net           234                 :            383 : pgtls_read_pending(PGconn *conn)
                                235                 :                : {
 2308 tgl@sss.pgh.pa.us         236                 :            383 :     return SSL_pending(conn->ssl) > 0;
                                237                 :                : }
                                238                 :                : 
                                239                 :                : ssize_t
 4044 heikki.linnakangas@i      240                 :            344 : pgtls_write(PGconn *conn, const void *ptr, size_t len)
                                241                 :                : {
                                242                 :                :     ssize_t     n;
                                243                 :            344 :     int         result_errno = 0;
                                244                 :                :     char        sebuf[PG_STRERROR_R_BUFLEN];
                                245                 :                :     int         err;
                                246                 :                :     unsigned long ecode;
                                247                 :                : 
                                248                 :            344 :     SOCK_ERRNO_SET(0);
 3438 peter_e@gmx.net           249                 :            344 :     ERR_clear_error();
 4044 heikki.linnakangas@i      250                 :            344 :     n = SSL_write(conn->ssl, ptr, len);
                                251                 :            344 :     err = SSL_get_error(conn->ssl, n);
 3438 peter_e@gmx.net           252   [ +  -  -  + ]:            344 :     ecode = (err != SSL_ERROR_NONE || n < 0) ? ERR_get_error() : 0;
 4044 heikki.linnakangas@i      253   [ +  -  -  -  :            344 :     switch (err)
                                           -  -  - ]
                                254                 :                :     {
                                255                 :            344 :         case SSL_ERROR_NONE:
                                256         [ -  + ]:            344 :             if (n < 0)
                                257                 :                :             {
                                258                 :                :                 /* Not supposed to happen, so we don't translate the msg */
 1699 tgl@sss.pgh.pa.us         259                 :UBC           0 :                 appendPQExpBufferStr(&conn->errorMessage,
                                260                 :                :                                      "SSL_write failed but did not provide error information\n");
                                261                 :                :                 /* assume the connection is broken */
 4044 heikki.linnakangas@i      262                 :              0 :                 result_errno = ECONNRESET;
                                263                 :                :             }
 4044 heikki.linnakangas@i      264                 :CBC         344 :             break;
 4044 heikki.linnakangas@i      265                 :UBC           0 :         case SSL_ERROR_WANT_READ:
                                266                 :                : 
                                267                 :                :             /*
                                268                 :                :              * Returning 0 here causes caller to wait for write-ready, which
                                269                 :                :              * is not really the right thing, but it's the best we can do.
                                270                 :                :              */
                                271                 :              0 :             n = 0;
                                272                 :              0 :             break;
                                273                 :              0 :         case SSL_ERROR_WANT_WRITE:
                                274                 :              0 :             n = 0;
                                275                 :              0 :             break;
                                276                 :              0 :         case SSL_ERROR_SYSCALL:
                                277                 :                : 
                                278                 :                :             /*
                                279                 :                :              * If errno is still zero then assume it's a read EOF situation,
                                280                 :                :              * and report EOF.  (This seems possible because SSL_write can
                                281                 :                :              * also do reads.)
                                282                 :                :              */
  635 tgl@sss.pgh.pa.us         283   [ #  #  #  # ]:              0 :             if (n < 0 && SOCK_ERRNO != 0)
                                284                 :                :             {
 4044 heikki.linnakangas@i      285                 :              0 :                 result_errno = SOCK_ERRNO;
                                286   [ #  #  #  # ]:              0 :                 if (result_errno == EPIPE || result_errno == ECONNRESET)
 1026 peter@eisentraut.org      287                 :              0 :                     libpq_append_conn_error(conn, "server closed the connection unexpectedly\n"
                                288                 :                :                                             "\tThis probably means the server terminated abnormally\n"
                                289                 :                :                                             "\tbefore or while processing the request.");
                                290                 :                :                 else
                                291                 :              0 :                     libpq_append_conn_error(conn, "SSL SYSCALL error: %s",
                                292                 :                :                                             SOCK_STRERROR(result_errno,
                                293                 :                :                                                           sebuf, sizeof(sebuf)));
                                294                 :                :             }
                                295                 :                :             else
                                296                 :                :             {
                                297                 :              0 :                 libpq_append_conn_error(conn, "SSL SYSCALL error: EOF detected");
                                298                 :                :                 /* assume the connection is broken */
 4044 heikki.linnakangas@i      299                 :              0 :                 result_errno = ECONNRESET;
                                300                 :              0 :                 n = -1;
                                301                 :                :             }
                                302                 :              0 :             break;
                                303                 :              0 :         case SSL_ERROR_SSL:
                                304                 :                :             {
 3438 peter_e@gmx.net           305                 :              0 :                 char       *errm = SSLerrmessage(ecode);
                                306                 :                : 
 1026 peter@eisentraut.org      307                 :              0 :                 libpq_append_conn_error(conn, "SSL error: %s", errm);
 4044 heikki.linnakangas@i      308                 :              0 :                 SSLerrfree(errm);
                                309                 :                :                 /* assume the connection is broken */
                                310                 :              0 :                 result_errno = ECONNRESET;
                                311                 :              0 :                 n = -1;
                                312                 :              0 :                 break;
                                313                 :                :             }
                                314                 :              0 :         case SSL_ERROR_ZERO_RETURN:
                                315                 :                : 
                                316                 :                :             /*
                                317                 :                :              * Per OpenSSL documentation, this error code is only returned for
                                318                 :                :              * a clean connection closure, so we should not report it as a
                                319                 :                :              * server crash.
                                320                 :                :              */
 1026 peter@eisentraut.org      321                 :              0 :             libpq_append_conn_error(conn, "SSL connection has been closed unexpectedly");
 4044 heikki.linnakangas@i      322                 :              0 :             result_errno = ECONNRESET;
                                323                 :              0 :             n = -1;
                                324                 :              0 :             break;
                                325                 :              0 :         default:
 1026 peter@eisentraut.org      326                 :              0 :             libpq_append_conn_error(conn, "unrecognized SSL error code: %d", err);
                                327                 :                :             /* assume the connection is broken */
 4044 heikki.linnakangas@i      328                 :              0 :             result_errno = ECONNRESET;
                                329                 :              0 :             n = -1;
                                330                 :              0 :             break;
                                331                 :                :     }
                                332                 :                : 
                                333                 :                :     /* ensure we return the intended errno to caller */
 4044 heikki.linnakangas@i      334                 :CBC         344 :     SOCK_ERRNO_SET(result_errno);
                                335                 :                : 
                                336                 :            344 :     return n;
                                337                 :                : }
                                338                 :                : 
                                339                 :                : char *
 2802 peter_e@gmx.net           340                 :              5 : pgtls_get_peer_certificate_hash(PGconn *conn, size_t *len)
                                341                 :                : {
                                342                 :                :     X509       *peer_cert;
                                343                 :                :     const EVP_MD *algo_type;
                                344                 :                :     unsigned char hash[EVP_MAX_MD_SIZE];    /* size for SHA-512 */
                                345                 :                :     unsigned int hash_size;
                                346                 :                :     int         algo_nid;
                                347                 :                :     char       *cert_hash;
                                348                 :                : 
                                349                 :              5 :     *len = 0;
                                350                 :                : 
                                351         [ -  + ]:              5 :     if (!conn->peer)
 2802 peter_e@gmx.net           352                 :UBC           0 :         return NULL;
                                353                 :                : 
 2802 peter_e@gmx.net           354                 :CBC           5 :     peer_cert = conn->peer;
                                355                 :                : 
                                356                 :                :     /*
                                357                 :                :      * Get the signature algorithm of the certificate to determine the hash
                                358                 :                :      * algorithm to use for the result.  Prefer X509_get_signature_info(),
                                359                 :                :      * introduced in OpenSSL 1.1.1, which can handle RSA-PSS signatures.
                                360                 :                :      */
                                361                 :                : #if HAVE_X509_GET_SIGNATURE_INFO
  934 michael@paquier.xyz       362         [ -  + ]:              5 :     if (!X509_get_signature_info(peer_cert, &algo_nid, NULL, NULL, NULL))
                                363                 :                : #else
                                364                 :                :     if (!OBJ_find_sigid_algs(X509_get_signature_nid(peer_cert),
                                365                 :                :                              &algo_nid, NULL))
                                366                 :                : #endif
                                367                 :                :     {
 1026 peter@eisentraut.org      368                 :UBC           0 :         libpq_append_conn_error(conn, "could not determine server certificate signature algorithm");
 2802 peter_e@gmx.net           369                 :              0 :         return NULL;
                                370                 :                :     }
                                371                 :                : 
                                372                 :                :     /*
                                373                 :                :      * The TLS server's certificate bytes need to be hashed with SHA-256 if
                                374                 :                :      * its signature algorithm is MD5 or SHA-1 as per RFC 5929
                                375                 :                :      * (https://tools.ietf.org/html/rfc5929#section-4.1).  If something else
                                376                 :                :      * is used, the same hash as the signature algorithm is used.
                                377                 :                :      */
 2802 peter_e@gmx.net           378         [ -  + ]:CBC           5 :     switch (algo_nid)
                                379                 :                :     {
 2802 peter_e@gmx.net           380                 :UBC           0 :         case NID_md5:
                                381                 :                :         case NID_sha1:
                                382                 :              0 :             algo_type = EVP_sha256();
                                383                 :              0 :             break;
 2802 peter_e@gmx.net           384                 :CBC           5 :         default:
                                385                 :              5 :             algo_type = EVP_get_digestbynid(algo_nid);
                                386         [ -  + ]:              5 :             if (algo_type == NULL)
                                387                 :                :             {
 1026 peter@eisentraut.org      388                 :UBC           0 :                 libpq_append_conn_error(conn, "could not find digest for NID %s",
                                389                 :                :                                         OBJ_nid2sn(algo_nid));
 2802 peter_e@gmx.net           390                 :              0 :                 return NULL;
                                391                 :                :             }
 2802 peter_e@gmx.net           392                 :CBC           5 :             break;
                                393                 :                :     }
                                394                 :                : 
                                395         [ -  + ]:              5 :     if (!X509_digest(peer_cert, algo_type, hash, &hash_size))
                                396                 :                :     {
 1026 peter@eisentraut.org      397                 :UBC           0 :         libpq_append_conn_error(conn, "could not generate peer certificate hash");
 2802 peter_e@gmx.net           398                 :              0 :         return NULL;
                                399                 :                :     }
                                400                 :                : 
                                401                 :                :     /* save result */
 2802 peter_e@gmx.net           402                 :CBC           5 :     cert_hash = malloc(hash_size);
                                403         [ -  + ]:              5 :     if (cert_hash == NULL)
                                404                 :                :     {
 1026 peter@eisentraut.org      405                 :UBC           0 :         libpq_append_conn_error(conn, "out of memory");
 2802 peter_e@gmx.net           406                 :              0 :         return NULL;
                                407                 :                :     }
 2802 peter_e@gmx.net           408                 :CBC           5 :     memcpy(cert_hash, hash, hash_size);
                                409                 :              5 :     *len = hash_size;
                                410                 :                : 
                                411                 :              5 :     return cert_hash;
                                412                 :                : }
                                413                 :                : 
                                414                 :                : /* ------------------------------------------------------------ */
                                415                 :                : /*                      OpenSSL specific code                   */
                                416                 :                : /* ------------------------------------------------------------ */
                                417                 :                : 
                                418                 :                : /*
                                419                 :                :  *  Certificate verification callback
                                420                 :                :  *
                                421                 :                :  *  This callback allows us to log intermediate problems during
                                422                 :                :  *  verification, but there doesn't seem to be a clean way to get
                                423                 :                :  *  our PGconn * structure.  So we can't log anything!
                                424                 :                :  *
                                425                 :                :  *  This callback also allows us to override the default acceptance
                                426                 :                :  *  criteria (e.g., accepting self-signed or expired certs), but
                                427                 :                :  *  for now we accept the default checks.
                                428                 :                :  */
                                429                 :                : static int
 4044 heikki.linnakangas@i      430                 :            288 : verify_cb(int ok, X509_STORE_CTX *ctx)
                                431                 :                : {
                                432                 :            288 :     return ok;
                                433                 :                : }
                                434                 :                : 
                                435                 :                : #ifdef HAVE_SSL_CTX_SET_CERT_CB
                                436                 :                : /*
                                437                 :                :  * Certificate selection callback
                                438                 :                :  *
                                439                 :                :  * This callback lets us choose the client certificate we send to the server
                                440                 :                :  * after seeing its CertificateRequest.  We only support sending a single
                                441                 :                :  * hard-coded certificate via sslcert, so we don't actually set any certificates
                                442                 :                :  * here; we just use it to record whether or not the server has actually asked
                                443                 :                :  * for one and whether we have one to send.
                                444                 :                :  */
                                445                 :                : static int
  897 michael@paquier.xyz       446                 :            105 : cert_cb(SSL *ssl, void *arg)
                                447                 :                : {
                                448                 :            105 :     PGconn     *conn = arg;
                                449                 :                : 
                                450                 :            105 :     conn->ssl_cert_requested = true;
                                451                 :                : 
                                452                 :                :     /* Do we have a certificate loaded to send back? */
                                453         [ +  + ]:            105 :     if (SSL_get_certificate(ssl))
                                454                 :             36 :         conn->ssl_cert_sent = true;
                                455                 :                : 
                                456                 :                :     /*
                                457                 :                :      * Tell OpenSSL that the callback succeeded; we're not required to
                                458                 :                :      * actually make any changes to the SSL handle.
                                459                 :                :      */
                                460                 :            105 :     return 1;
                                461                 :                : }
                                462                 :                : #endif
                                463                 :                : 
                                464                 :                : /*
                                465                 :                :  * OpenSSL-specific wrapper around
                                466                 :                :  * pq_verify_peer_name_matches_certificate_name(), converting the ASN1_STRING
                                467                 :                :  * into a plain C string.
                                468                 :                :  */
                                469                 :                : static int
 2779 peter_e@gmx.net           470                 :             34 : openssl_verify_peer_name_matches_certificate_name(PGconn *conn, ASN1_STRING *name_entry,
                                471                 :                :                                                   char **store_name)
                                472                 :                : {
                                473                 :                :     int         len;
                                474                 :                :     const unsigned char *namedata;
                                475                 :                : 
                                476                 :                :     /* Should not happen... */
 4012 heikki.linnakangas@i      477         [ -  + ]:             34 :     if (name_entry == NULL)
                                478                 :                :     {
 1026 peter@eisentraut.org      479                 :UBC           0 :         libpq_append_conn_error(conn, "SSL certificate's name entry is missing");
 4012 heikki.linnakangas@i      480                 :              0 :         return -1;
                                481                 :                :     }
                                482                 :                : 
                                483                 :                :     /*
                                484                 :                :      * GEN_DNS can be only IA5String, equivalent to US ASCII.
                                485                 :                :      */
 3278 heikki.linnakangas@i      486                 :CBC          34 :     namedata = ASN1_STRING_get0_data(name_entry);
 4012                           487                 :             34 :     len = ASN1_STRING_length(name_entry);
                                488                 :                : 
                                489                 :                :     /* OK to cast from unsigned to plain char, since it's all ASCII. */
 2779 peter_e@gmx.net           490                 :             34 :     return pq_verify_peer_name_matches_certificate_name(conn, (const char *) namedata, len, store_name);
                                491                 :                : }
                                492                 :                : 
                                493                 :                : /*
                                494                 :                :  * OpenSSL-specific wrapper around
                                495                 :                :  * pq_verify_peer_name_matches_certificate_ip(), converting the
                                496                 :                :  * ASN1_OCTET_STRING into a plain C string.
                                497                 :                :  */
                                498                 :                : static int
 1254 peter@eisentraut.org      499                 :             24 : openssl_verify_peer_name_matches_certificate_ip(PGconn *conn,
                                500                 :                :                                                 ASN1_OCTET_STRING *addr_entry,
                                501                 :                :                                                 char **store_name)
                                502                 :                : {
                                503                 :                :     int         len;
                                504                 :                :     const unsigned char *addrdata;
                                505                 :                : 
                                506                 :                :     /* Should not happen... */
                                507         [ -  + ]:             24 :     if (addr_entry == NULL)
                                508                 :                :     {
 1026 peter@eisentraut.org      509                 :UBC           0 :         libpq_append_conn_error(conn, "SSL certificate's address entry is missing");
 1254                           510                 :              0 :         return -1;
                                511                 :                :     }
                                512                 :                : 
                                513                 :                :     /*
                                514                 :                :      * GEN_IPADD is an OCTET STRING containing an IP address in network byte
                                515                 :                :      * order.
                                516                 :                :      */
 1254 peter@eisentraut.org      517                 :CBC          24 :     addrdata = ASN1_STRING_get0_data(addr_entry);
                                518                 :             24 :     len = ASN1_STRING_length(addr_entry);
                                519                 :                : 
                                520                 :             24 :     return pq_verify_peer_name_matches_certificate_ip(conn, addrdata, len, store_name);
                                521                 :                : }
                                522                 :                : 
                                523                 :                : static bool
                                524                 :             36 : is_ip_address(const char *host)
                                525                 :                : {
                                526                 :                :     struct in_addr dummy4;
                                527                 :                : #ifdef HAVE_INET_PTON
                                528                 :                :     struct in6_addr dummy6;
                                529                 :                : #endif
                                530                 :                : 
                                531                 :             36 :     return inet_aton(host, &dummy4)
                                532                 :                : #ifdef HAVE_INET_PTON
                                533   [ +  +  +  + ]:             36 :         || (inet_pton(AF_INET6, host, &dummy6) == 1)
                                534                 :                : #endif
                                535                 :                :         ;
                                536                 :                : }
                                537                 :                : 
                                538                 :                : /*
                                539                 :                :  *  Verify that the server certificate matches the hostname we connected to.
                                540                 :                :  *
                                541                 :                :  * The certificate's Common Name and Subject Alternative Names are considered.
                                542                 :                :  */
                                543                 :                : int
 2779 peter_e@gmx.net           544                 :             36 : pgtls_verify_peer_name_matches_certificate_guts(PGconn *conn,
                                545                 :                :                                                 int *names_examined,
                                546                 :                :                                                 char **first_name)
                                547                 :                : {
                                548                 :                :     STACK_OF(GENERAL_NAME) * peer_san;
                                549                 :                :     int         i;
                                550                 :             36 :     int         rc = 0;
 1254 peter@eisentraut.org      551                 :             36 :     char       *host = conn->connhost[conn->whichhost].host;
                                552                 :                :     int         host_type;
                                553                 :             36 :     bool        check_cn = true;
                                554                 :                : 
                                555   [ +  -  +  - ]:             36 :     Assert(host && host[0]);    /* should be guaranteed by caller */
                                556                 :                : 
                                557                 :                :     /*
                                558                 :                :      * We try to match the NSS behavior here, which is a slight departure from
                                559                 :                :      * the spec but seems to make more intuitive sense:
                                560                 :                :      *
                                561                 :                :      * If connhost contains a DNS name, and the certificate's SANs contain any
                                562                 :                :      * dNSName entries, then we'll ignore the Subject Common Name entirely;
                                563                 :                :      * otherwise, we fall back to checking the CN. (This behavior matches the
                                564                 :                :      * RFC.)
                                565                 :                :      *
                                566                 :                :      * If connhost contains an IP address, and the SANs contain iPAddress
                                567                 :                :      * entries, we again ignore the CN. Otherwise, we allow the CN to match,
                                568                 :                :      * EVEN IF there is a dNSName in the SANs. (RFC 6125 prohibits this: "A
                                569                 :                :      * client MUST NOT seek a match for a reference identifier of CN-ID if the
                                570                 :                :      * presented identifiers include a DNS-ID, SRV-ID, URI-ID, or any
                                571                 :                :      * application-specific identifier types supported by the client.")
                                572                 :                :      *
                                573                 :                :      * NOTE: Prior versions of libpq did not consider iPAddress entries at
                                574                 :                :      * all, so this new behavior might break a certificate that has different
                                575                 :                :      * IP addresses in the Subject CN and the SANs.
                                576                 :                :      */
                                577         [ +  + ]:             36 :     if (is_ip_address(host))
                                578                 :             16 :         host_type = GEN_IPADD;
                                579                 :                :     else
                                580                 :             20 :         host_type = GEN_DNS;
                                581                 :                : 
                                582                 :                :     /*
                                583                 :                :      * First, get the Subject Alternative Names (SANs) from the certificate,
                                584                 :                :      * and compare them against the originally given hostname.
                                585                 :                :      */
                                586                 :                :     peer_san = (STACK_OF(GENERAL_NAME) *)
 4012 heikki.linnakangas@i      587                 :             36 :         X509_get_ext_d2i(conn->peer, NID_subject_alt_name, NULL, NULL);
                                588                 :                : 
                                589         [ +  + ]:             36 :     if (peer_san)
                                590                 :                :     {
                                591                 :             29 :         int         san_len = sk_GENERAL_NAME_num(peer_san);
                                592                 :                : 
                                593         [ +  + ]:             61 :         for (i = 0; i < san_len; i++)
                                594                 :                :         {
                                595                 :             50 :             const GENERAL_NAME *name = sk_GENERAL_NAME_value(peer_san, i);
 1254 peter@eisentraut.org      596                 :             50 :             char       *alt_name = NULL;
                                597                 :                : 
                                598         [ +  + ]:             50 :             if (name->type == host_type)
                                599                 :                :             {
                                600                 :                :                 /*
                                601                 :                :                  * This SAN is of the same type (IP or DNS) as our host name,
                                602                 :                :                  * so don't allow a fallback check of the CN.
                                603                 :                :                  */
                                604                 :             43 :                 check_cn = false;
                                605                 :                :             }
                                606                 :                : 
                                607         [ +  + ]:             50 :             if (name->type == GEN_DNS)
                                608                 :                :             {
 2779 peter_e@gmx.net           609                 :             26 :                 (*names_examined)++;
                                610                 :             26 :                 rc = openssl_verify_peer_name_matches_certificate_name(conn,
 2690 tgl@sss.pgh.pa.us         611                 :             26 :                                                                        name->d.dNSName,
                                612                 :                :                                                                        &alt_name);
                                613                 :                :             }
 1254 peter@eisentraut.org      614         [ +  - ]:             24 :             else if (name->type == GEN_IPADD)
                                615                 :                :             {
                                616                 :             24 :                 (*names_examined)++;
                                617                 :             24 :                 rc = openssl_verify_peer_name_matches_certificate_ip(conn,
                                618                 :             24 :                                                                      name->d.iPAddress,
                                619                 :                :                                                                      &alt_name);
                                620                 :                :             }
                                621                 :                : 
                                622         [ +  - ]:             50 :             if (alt_name)
                                623                 :                :             {
                                624         [ +  + ]:             50 :                 if (!*first_name)
                                625                 :             29 :                     *first_name = alt_name;
                                626                 :                :                 else
                                627                 :             21 :                     free(alt_name);
                                628                 :                :             }
                                629                 :                : 
 2779 peter_e@gmx.net           630         [ +  + ]:             50 :             if (rc != 0)
                                631                 :                :             {
                                632                 :                :                 /*
                                633                 :                :                  * Either we hit an error or a match, and either way we should
                                634                 :                :                  * not fall back to the CN.
                                635                 :                :                  */
 1254 peter@eisentraut.org      636                 :             18 :                 check_cn = false;
 4012 heikki.linnakangas@i      637                 :             18 :                 break;
                                638                 :                :             }
                                639                 :                :         }
 1963 michael@paquier.xyz       640                 :             29 :         sk_GENERAL_NAME_pop_free(peer_san, GENERAL_NAME_free);
                                641                 :                :     }
                                642                 :                : 
                                643                 :                :     /*
                                644                 :                :      * If there is no subjectAltName extension of the matching type, check the
                                645                 :                :      * Common Name.
                                646                 :                :      *
                                647                 :                :      * (Per RFC 2818 and RFC 6125, if the subjectAltName extension of type
                                648                 :                :      * dNSName is present, the CN must be ignored. We break this rule if host
                                649                 :                :      * is an IP address; see the comment above.)
                                650                 :                :      */
 1254 peter@eisentraut.org      651         [ +  + ]:             36 :     if (check_cn)
                                652                 :                :     {
                                653                 :                :         X509_NAME  *subject_name;
                                654                 :                : 
 4012 heikki.linnakangas@i      655                 :             10 :         subject_name = X509_get_subject_name(conn->peer);
                                656         [ +  - ]:             10 :         if (subject_name != NULL)
                                657                 :                :         {
                                658                 :                :             int         cn_index;
                                659                 :                : 
                                660                 :             10 :             cn_index = X509_NAME_get_index_by_NID(subject_name,
                                661                 :                :                                                   NID_commonName, -1);
                                662         [ +  + ]:             10 :             if (cn_index >= 0)
                                663                 :                :             {
 1254 peter@eisentraut.org      664                 :              8 :                 char       *common_name = NULL;
                                665                 :                : 
 2779 peter_e@gmx.net           666                 :              8 :                 (*names_examined)++;
 2046 alvherre@alvh.no-ip.      667                 :              8 :                 rc = openssl_verify_peer_name_matches_certificate_name(conn,
                                668                 :              8 :                                                                        X509_NAME_ENTRY_get_data(X509_NAME_get_entry(subject_name, cn_index)),
                                669                 :                :                                                                        &common_name);
                                670                 :                : 
 1254 peter@eisentraut.org      671         [ +  - ]:              8 :                 if (common_name)
                                672                 :                :                 {
                                673         [ +  + ]:              8 :                     if (!*first_name)
                                674                 :              6 :                         *first_name = common_name;
                                675                 :                :                     else
                                676                 :              2 :                         free(common_name);
                                677                 :                :                 }
                                678                 :                :             }
                                679                 :                :         }
                                680                 :                :     }
                                681                 :                : 
 2779 peter_e@gmx.net           682                 :             36 :     return rc;
                                683                 :                : }
                                684                 :                : 
                                685                 :                : /* See pqcomm.h comments on OpenSSL implementation of ALPN (RFC 7301) */
                                686                 :                : static unsigned char alpn_protos[] = PG_ALPN_PROTOCOL_VECTOR;
                                687                 :                : 
                                688                 :                : #ifdef HAVE_SSL_CTX_SET_KEYLOG_CALLBACK
                                689                 :                : /*
                                690                 :                :  * SSL Key Logging callback
                                691                 :                :  *
                                692                 :                :  * This callback lets the user store all key material to a file for debugging
                                693                 :                :  * purposes.  The file will be written using the NSS keylog format.  LibreSSL
                                694                 :                :  * 3.5 introduced stub function to set the callback for OpenSSL compatibility
                                695                 :                :  * but the callback is never invoked.
                                696                 :                :  *
                                697                 :                :  * Error messages added to the connection object wont be printed anywhere if
                                698                 :                :  * the connection is successful.  Errors in processing keylogging are printed
                                699                 :                :  * to stderr to overcome this.
                                700                 :                :  */
                                701                 :                : static void
  156 dgustafsson@postgres      702                 :             10 : SSL_CTX_keylog_cb(const SSL *ssl, const char *line)
                                703                 :                : {
                                704                 :                :     int         fd;
                                705                 :                :     ssize_t     rc;
                                706                 :             10 :     PGconn     *conn = SSL_get_app_data(ssl);
                                707                 :                : 
                                708         [ -  + ]:             10 :     if (conn == NULL)
  156 dgustafsson@postgres      709                 :UBC           0 :         return;
                                710                 :                : 
  156 dgustafsson@postgres      711                 :CBC          10 :     fd = open(conn->sslkeylogfile, O_WRONLY | O_APPEND | O_CREAT, 0600);
                                712                 :                : 
                                713         [ +  + ]:             10 :     if (fd == -1)
                                714                 :                :     {
   58                           715                 :              5 :         fprintf(stderr, libpq_gettext("WARNING: could not open SSL key logging file \"%s\": %m\n"),
                                716                 :                :                 conn->sslkeylogfile);
  156                           717                 :              5 :         return;
                                718                 :                :     }
                                719                 :                : 
                                720                 :                :     /* line is guaranteed by OpenSSL to be NUL terminated */
                                721                 :              5 :     rc = write(fd, line, strlen(line));
                                722         [ -  + ]:              5 :     if (rc < 0)
   58 dgustafsson@postgres      723                 :UBC           0 :         fprintf(stderr, libpq_gettext("WARNING: could not write to SSL key logging file \"%s\": %m\n"),
                                724                 :                :                 conn->sslkeylogfile);
                                725                 :                :     else
  156 dgustafsson@postgres      726                 :CBC           5 :         rc = write(fd, "\n", 1);
                                727                 :                :     (void) rc;                  /* silence compiler warnings */
                                728                 :              5 :     close(fd);
                                729                 :                : }
                                730                 :                : #endif
                                731                 :                : 
                                732                 :                : /*
                                733                 :                :  *  Create per-connection SSL object, and load the client certificate,
                                734                 :                :  *  private key, and trusted CA certs.
                                735                 :                :  *
                                736                 :                :  *  Returns 0 if OK, -1 on failure (with a message in conn->errorMessage).
                                737                 :                :  */
                                738                 :                : static int
 4044 heikki.linnakangas@i      739                 :            158 : initialize_SSL(PGconn *conn)
                                740                 :                : {
                                741                 :                :     SSL_CTX    *SSL_context;
                                742                 :                :     struct stat buf;
                                743                 :                :     char        homedir[MAXPGPATH];
                                744                 :                :     char        fnbuf[MAXPGPATH];
                                745                 :                :     char        sebuf[PG_STRERROR_R_BUFLEN];
                                746                 :                :     bool        have_homedir;
                                747                 :                :     bool        have_cert;
                                748                 :                :     bool        have_rootcert;
                                749                 :                : 
                                750                 :                :     /*
                                751                 :                :      * We'll need the home directory if any of the relevant parameters are
                                752                 :                :      * defaulted.  If pqGetHomeDirectory fails, act as though none of the
                                753                 :                :      * files could be found.
                                754                 :                :      */
                                755   [ +  +  +  - ]:            158 :     if (!(conn->sslcert && strlen(conn->sslcert) > 0) ||
                                756   [ +  +  +  - ]:            118 :         !(conn->sslkey && strlen(conn->sslkey) > 0) ||
                                757   [ +  -  +  - ]:            110 :         !(conn->sslrootcert && strlen(conn->sslrootcert) > 0) ||
 1661 peter@eisentraut.org      758   [ +  +  +  + ]:            110 :         !((conn->sslcrl && strlen(conn->sslcrl) > 0) ||
                                759   [ +  +  -  + ]:              4 :           (conn->sslcrldir && strlen(conn->sslcrldir) > 0)))
 4044 heikki.linnakangas@i      760                 :             50 :         have_homedir = pqGetHomeDirectory(homedir, sizeof(homedir));
                                761                 :                :     else                        /* won't need it */
                                762                 :            108 :         have_homedir = false;
                                763                 :                : 
                                764                 :                :     /*
                                765                 :                :      * Create a new SSL_CTX object.
                                766                 :                :      *
                                767                 :                :      * We used to share a single SSL_CTX between all connections, but it was
                                768                 :                :      * complicated if connections used different certificates. So now we
                                769                 :                :      * create a separate context for each connection, and accept the overhead.
                                770                 :                :      */
 3256                           771                 :            158 :     SSL_context = SSL_CTX_new(SSLv23_method());
                                772         [ -  + ]:            158 :     if (!SSL_context)
                                773                 :                :     {
 3256 heikki.linnakangas@i      774                 :UBC           0 :         char       *err = SSLerrmessage(ERR_get_error());
                                775                 :                : 
 1026 peter@eisentraut.org      776                 :              0 :         libpq_append_conn_error(conn, "could not create SSL context: %s", err);
 3256 heikki.linnakangas@i      777                 :              0 :         SSLerrfree(err);
                                778                 :              0 :         return -1;
                                779                 :                :     }
                                780                 :                : 
                                781                 :                :     /*
                                782                 :                :      * Delegate the client cert password prompt to the libpq wrapper callback
                                783                 :                :      * if any is defined.
                                784                 :                :      *
                                785                 :                :      * If the application hasn't installed its own and the sslpassword
                                786                 :                :      * parameter is non-null, we install ours now to make sure we supply
                                787                 :                :      * PGconn->sslpassword to OpenSSL instead of letting it prompt on stdin.
                                788                 :                :      *
                                789                 :                :      * This will replace OpenSSL's default PEM_def_callback (which prompts on
                                790                 :                :      * stdin), but we're only setting it for this SSL context so it's
                                791                 :                :      * harmless.
                                792                 :                :      */
 2107 andrew@dunslane.net       793         [ +  - ]:CBC         158 :     if (PQsslKeyPassHook
                                794   [ +  +  +  - ]:            158 :         || (conn->sslpassword && strlen(conn->sslpassword) > 0))
                                795                 :                :     {
                                796                 :              3 :         SSL_CTX_set_default_passwd_cb(SSL_context, PQssl_passwd_cb);
                                797                 :              3 :         SSL_CTX_set_default_passwd_cb_userdata(SSL_context, conn);
                                798                 :                :     }
                                799                 :                : 
                                800                 :                : #ifdef HAVE_SSL_CTX_SET_CERT_CB
                                801                 :                :     /* Set up a certificate selection callback. */
  897 michael@paquier.xyz       802                 :            158 :     SSL_CTX_set_cert_cb(SSL_context, cert_cb, conn);
                                803                 :                : #endif
                                804                 :                : 
                                805                 :                :     /* Disable old protocol versions */
 3256 heikki.linnakangas@i      806                 :            158 :     SSL_CTX_set_options(SSL_context, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
                                807                 :                : 
                                808                 :                :     /* Set the minimum and maximum protocol versions if necessary */
 1955 michael@paquier.xyz       809         [ +  - ]:            158 :     if (conn->ssl_min_protocol_version &&
                                810         [ +  - ]:            158 :         strlen(conn->ssl_min_protocol_version) != 0)
                                811                 :                :     {
                                812                 :                :         int         ssl_min_ver;
                                813                 :                : 
                                814                 :            158 :         ssl_min_ver = ssl_protocol_version_to_openssl(conn->ssl_min_protocol_version);
                                815                 :                : 
 2048                           816         [ -  + ]:            158 :         if (ssl_min_ver == -1)
                                817                 :                :         {
 1026 peter@eisentraut.org      818                 :UBC           0 :             libpq_append_conn_error(conn, "invalid value \"%s\" for minimum SSL protocol version",
                                819                 :                :                                     conn->ssl_min_protocol_version);
 2043 tgl@sss.pgh.pa.us         820                 :              0 :             SSL_CTX_free(SSL_context);
 2048 michael@paquier.xyz       821                 :              0 :             return -1;
                                822                 :                :         }
                                823                 :                : 
 2048 michael@paquier.xyz       824         [ -  + ]:CBC         158 :         if (!SSL_CTX_set_min_proto_version(SSL_context, ssl_min_ver))
                                825                 :                :         {
 2048 michael@paquier.xyz       826                 :UBC           0 :             char       *err = SSLerrmessage(ERR_get_error());
                                827                 :                : 
 1026 peter@eisentraut.org      828                 :              0 :             libpq_append_conn_error(conn, "could not set minimum SSL protocol version: %s", err);
 2043 tgl@sss.pgh.pa.us         829                 :              0 :             SSLerrfree(err);
                                830                 :              0 :             SSL_CTX_free(SSL_context);
 2048 michael@paquier.xyz       831                 :              0 :             return -1;
                                832                 :                :         }
                                833                 :                :     }
                                834                 :                : 
 1955 michael@paquier.xyz       835         [ +  + ]:CBC         158 :     if (conn->ssl_max_protocol_version &&
                                836         [ +  - ]:              2 :         strlen(conn->ssl_max_protocol_version) != 0)
                                837                 :                :     {
                                838                 :                :         int         ssl_max_ver;
                                839                 :                : 
                                840                 :              2 :         ssl_max_ver = ssl_protocol_version_to_openssl(conn->ssl_max_protocol_version);
                                841                 :                : 
 2048                           842         [ -  + ]:              2 :         if (ssl_max_ver == -1)
                                843                 :                :         {
 1026 peter@eisentraut.org      844                 :UBC           0 :             libpq_append_conn_error(conn, "invalid value \"%s\" for maximum SSL protocol version",
                                845                 :                :                                     conn->ssl_max_protocol_version);
 2043 tgl@sss.pgh.pa.us         846                 :              0 :             SSL_CTX_free(SSL_context);
 2048 michael@paquier.xyz       847                 :              0 :             return -1;
                                848                 :                :         }
                                849                 :                : 
 2048 michael@paquier.xyz       850         [ -  + ]:CBC           2 :         if (!SSL_CTX_set_max_proto_version(SSL_context, ssl_max_ver))
                                851                 :                :         {
 2048 michael@paquier.xyz       852                 :UBC           0 :             char       *err = SSLerrmessage(ERR_get_error());
                                853                 :                : 
 1026 peter@eisentraut.org      854                 :              0 :             libpq_append_conn_error(conn, "could not set maximum SSL protocol version: %s", err);
 2043 tgl@sss.pgh.pa.us         855                 :              0 :             SSLerrfree(err);
                                856                 :              0 :             SSL_CTX_free(SSL_context);
 2048 michael@paquier.xyz       857                 :              0 :             return -1;
                                858                 :                :         }
                                859                 :                :     }
                                860                 :                : 
                                861                 :                :     /*
                                862                 :                :      * Disable OpenSSL's moving-write-buffer sanity check, because it causes
                                863                 :                :      * unnecessary failures in nonblocking send cases.
                                864                 :                :      */
 3256 heikki.linnakangas@i      865                 :CBC         158 :     SSL_CTX_set_mode(SSL_context, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
                                866                 :                : 
                                867                 :                :     /*
                                868                 :                :      * If the root cert file exists, load it so we can perform certificate
                                869                 :                :      * verification. If sslmode is "verify-full" we will also do further
                                870                 :                :      * verification after the connection has been completed.
                                871                 :                :      */
                                872   [ +  +  +  - ]:            158 :     if (conn->sslrootcert && strlen(conn->sslrootcert) > 0)
                                873                 :            118 :         strlcpy(fnbuf, conn->sslrootcert, sizeof(fnbuf));
                                874         [ +  - ]:             40 :     else if (have_homedir)
                                875                 :             40 :         snprintf(fnbuf, sizeof(fnbuf), "%s/%s", homedir, ROOT_CERT_FILE);
                                876                 :                :     else
 3256 heikki.linnakangas@i      877                 :UBC           0 :         fnbuf[0] = '\0';
                                878                 :                : 
  885 dgustafsson@postgres      879         [ +  + ]:CBC         158 :     if (strcmp(fnbuf, "system") == 0)
                                880                 :                :     {
                                881                 :                :         /*
                                882                 :                :          * The "system" sentinel value indicates that we should load whatever
                                883                 :                :          * root certificates are installed for use by OpenSSL; these locations
                                884                 :                :          * differ by platform. Note that the default system locations may be
                                885                 :                :          * further overridden by the SSL_CERT_DIR and SSL_CERT_FILE
                                886                 :                :          * environment variables.
                                887                 :                :          */
                                888         [ -  + ]:              3 :         if (SSL_CTX_set_default_verify_paths(SSL_context) != 1)
                                889                 :                :         {
  885 dgustafsson@postgres      890                 :UBC           0 :             char       *err = SSLerrmessage(ERR_get_error());
                                891                 :                : 
                                892                 :              0 :             libpq_append_conn_error(conn, "could not load system root certificate paths: %s",
                                893                 :                :                                     err);
                                894                 :              0 :             SSLerrfree(err);
                                895                 :              0 :             SSL_CTX_free(SSL_context);
                                896                 :              0 :             return -1;
                                897                 :                :         }
  885 dgustafsson@postgres      898                 :CBC           3 :         have_rootcert = true;
                                899                 :                :     }
                                900   [ +  -  +  + ]:            310 :     else if (fnbuf[0] != '\0' &&
                                901                 :            155 :              stat(fnbuf, &buf) == 0)
 3256 heikki.linnakangas@i      902                 :            101 :     {
                                903                 :                :         X509_STORE *cvstore;
                                904                 :                : 
                                905         [ -  + ]:            101 :         if (SSL_CTX_load_verify_locations(SSL_context, fnbuf, NULL) != 1)
                                906                 :                :         {
 3256 heikki.linnakangas@i      907                 :UBC           0 :             char       *err = SSLerrmessage(ERR_get_error());
                                908                 :                : 
 1026 peter@eisentraut.org      909                 :              0 :             libpq_append_conn_error(conn, "could not read root certificate file \"%s\": %s",
                                910                 :                :                                     fnbuf, err);
 3256 heikki.linnakangas@i      911                 :              0 :             SSLerrfree(err);
                                912                 :              0 :             SSL_CTX_free(SSL_context);
                                913                 :              0 :             return -1;
                                914                 :                :         }
                                915                 :                : 
 3256 heikki.linnakangas@i      916         [ +  - ]:CBC         101 :         if ((cvstore = SSL_CTX_get_cert_store(SSL_context)) != NULL)
                                917                 :                :         {
 1578 tgl@sss.pgh.pa.us         918                 :            101 :             char       *fname = NULL;
                                919                 :            101 :             char       *dname = NULL;
                                920                 :                : 
 3256 heikki.linnakangas@i      921   [ +  -  +  + ]:            101 :             if (conn->sslcrl && strlen(conn->sslcrl) > 0)
 1661 peter@eisentraut.org      922                 :             99 :                 fname = conn->sslcrl;
                                923   [ +  -  +  - ]:            101 :             if (conn->sslcrldir && strlen(conn->sslcrldir) > 0)
                                924                 :            101 :                 dname = conn->sslcrldir;
                                925                 :                : 
                                926                 :                :             /* defaults to use the default CRL file */
                                927   [ +  +  -  +  :            101 :             if (!fname && !dname && have_homedir)
                                              -  - ]
                                928                 :                :             {
 3256 heikki.linnakangas@i      929                 :UBC           0 :                 snprintf(fnbuf, sizeof(fnbuf), "%s/%s", homedir, ROOT_CRL_FILE);
 1661 peter@eisentraut.org      930                 :              0 :                 fname = fnbuf;
                                931                 :                :             }
                                932                 :                : 
                                933                 :                :             /* Set the flags to check against the complete CRL chain */
 1661 peter@eisentraut.org      934   [ +  +  +  -  :CBC         202 :             if ((fname || dname) &&
                                              +  + ]
                                935                 :            101 :                 X509_STORE_load_locations(cvstore, fname, dname) == 1)
                                936                 :                :             {
 3256 heikki.linnakangas@i      937                 :              5 :                 X509_STORE_set_flags(cvstore,
                                938                 :                :                                      X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
                                939                 :                :             }
                                940                 :                : 
                                941                 :                :             /* if not found, silently ignore;  we do not require CRL */
                                942                 :            101 :             ERR_clear_error();
                                943                 :                :         }
                                944                 :            101 :         have_rootcert = true;
                                945                 :                :     }
                                946                 :                :     else
                                947                 :                :     {
                                948                 :                :         /*
                                949                 :                :          * stat() failed; assume root file doesn't exist.  If sslmode is
                                950                 :                :          * verify-ca or verify-full, this is an error.  Otherwise, continue
                                951                 :                :          * without performing any server cert verification.
                                952                 :                :          */
                                953         [ +  + ]:             54 :         if (conn->sslmode[0] == 'v') /* "verify-ca" or "verify-full" */
                                954                 :                :         {
                                955                 :                :             /*
                                956                 :                :              * The only way to reach here with an empty filename is if
                                957                 :                :              * pqGetHomeDirectory failed.  That's a sufficiently unusual case
                                958                 :                :              * that it seems worth having a specialized error message for it.
                                959                 :                :              */
                                960         [ -  + ]:              2 :             if (fnbuf[0] == '\0')
 1026 peter@eisentraut.org      961                 :UBC           0 :                 libpq_append_conn_error(conn, "could not get home directory to locate root certificate file\n"
                                962                 :                :                                         "Either provide the file, use the system's trusted roots with sslrootcert=system, or change sslmode to disable server certificate verification.");
                                963                 :                :             else
 1026 peter@eisentraut.org      964                 :CBC           2 :                 libpq_append_conn_error(conn, "root certificate file \"%s\" does not exist\n"
                                965                 :                :                                         "Either provide the file, use the system's trusted roots with sslrootcert=system, or change sslmode to disable server certificate verification.", fnbuf);
 3256 heikki.linnakangas@i      966                 :              2 :             SSL_CTX_free(SSL_context);
                                967                 :              2 :             return -1;
                                968                 :                :         }
                                969                 :             52 :         have_rootcert = false;
                                970                 :                :     }
                                971                 :                : 
                                972                 :                :     /* Read the client certificate file */
 4044                           973   [ +  +  +  - ]:            156 :     if (conn->sslcert && strlen(conn->sslcert) > 0)
 3878 tgl@sss.pgh.pa.us         974                 :            116 :         strlcpy(fnbuf, conn->sslcert, sizeof(fnbuf));
 4044 heikki.linnakangas@i      975         [ +  - ]:             40 :     else if (have_homedir)
                                976                 :             40 :         snprintf(fnbuf, sizeof(fnbuf), "%s/%s", homedir, USER_CERT_FILE);
                                977                 :                :     else
 4044 heikki.linnakangas@i      978                 :UBC           0 :         fnbuf[0] = '\0';
                                979                 :                : 
  841 tgl@sss.pgh.pa.us         980         [ +  + ]:CBC         156 :     if (conn->sslcertmode[0] == 'd') /* disable */
                                981                 :                :     {
                                982                 :                :         /* don't send a client cert even if we have one */
  897 michael@paquier.xyz       983                 :              3 :         have_cert = false;
                                984                 :                :     }
                                985         [ -  + ]:            153 :     else if (fnbuf[0] == '\0')
                                986                 :                :     {
                                987                 :                :         /* no home directory, proceed without a client cert */
 4044 heikki.linnakangas@i      988                 :UBC           0 :         have_cert = false;
                                989                 :                :     }
 4044 heikki.linnakangas@i      990         [ +  + ]:CBC         153 :     else if (stat(fnbuf, &buf) != 0)
                                991                 :                :     {
                                992                 :                :         /*
                                993                 :                :          * If file is not present, just go on without a client cert; server
                                994                 :                :          * might or might not accept the connection.  Any other error,
                                995                 :                :          * however, is grounds for complaint.
                                996                 :                :          */
                                997   [ -  +  -  - ]:            115 :         if (errno != ENOENT && errno != ENOTDIR)
                                998                 :                :         {
 1026 peter@eisentraut.org      999                 :UBC           0 :             libpq_append_conn_error(conn, "could not open certificate file \"%s\": %s",
  841 tgl@sss.pgh.pa.us        1000                 :              0 :                                     fnbuf, strerror_r(errno, sebuf, sizeof(sebuf)));
 3256 heikki.linnakangas@i     1001                 :              0 :             SSL_CTX_free(SSL_context);
 4044                          1002                 :              0 :             return -1;
                               1003                 :                :         }
 4044 heikki.linnakangas@i     1004                 :CBC         115 :         have_cert = false;
                               1005                 :                :     }
                               1006                 :                :     else
                               1007                 :                :     {
                               1008                 :                :         /*
                               1009                 :                :          * Cert file exists, so load it. Since OpenSSL doesn't provide the
                               1010                 :                :          * equivalent of "SSL_use_certificate_chain_file", we have to load it
                               1011                 :                :          * into the SSL context, rather than the SSL object.
                               1012                 :                :          */
                               1013         [ -  + ]:             38 :         if (SSL_CTX_use_certificate_chain_file(SSL_context, fnbuf) != 1)
                               1014                 :                :         {
 3438 peter_e@gmx.net          1015                 :UBC           0 :             char       *err = SSLerrmessage(ERR_get_error());
                               1016                 :                : 
 1026 peter@eisentraut.org     1017                 :              0 :             libpq_append_conn_error(conn, "could not read certificate file \"%s\": %s",
                               1018                 :                :                                     fnbuf, err);
 4044 heikki.linnakangas@i     1019                 :              0 :             SSLerrfree(err);
 3256                          1020                 :              0 :             SSL_CTX_free(SSL_context);
 4044                          1021                 :              0 :             return -1;
                               1022                 :                :         }
                               1023                 :                : 
                               1024                 :                :         /* need to load the associated private key, too */
 4044 heikki.linnakangas@i     1025                 :CBC          38 :         have_cert = true;
                               1026                 :                :     }
                               1027                 :                : 
                               1028                 :                :     /*
                               1029                 :                :      * The SSL context is now loaded with the correct root and client
                               1030                 :                :      * certificates. Create a connection-specific SSL object. The private key
                               1031                 :                :      * is loaded directly into the SSL object. (We could load the private key
                               1032                 :                :      * into the context, too, but we have done it this way historically, and
                               1033                 :                :      * it doesn't really matter.)
                               1034                 :                :      */
 3256                          1035   [ +  -  +  - ]:            312 :     if (!(conn->ssl = SSL_new(SSL_context)) ||
                               1036         [ -  + ]:            312 :         !SSL_set_app_data(conn->ssl, conn) ||
  330 dgustafsson@postgres     1037                 :            156 :         !ssl_set_pgconn_bio(conn))
                               1038                 :                :     {
 3256 heikki.linnakangas@i     1039                 :UBC           0 :         char       *err = SSLerrmessage(ERR_get_error());
                               1040                 :                : 
 1026 peter@eisentraut.org     1041                 :              0 :         libpq_append_conn_error(conn, "could not establish SSL connection: %s", err);
 3256 heikki.linnakangas@i     1042                 :              0 :         SSLerrfree(err);
                               1043                 :              0 :         SSL_CTX_free(SSL_context);
                               1044                 :              0 :         return -1;
                               1045                 :                :     }
 3256 heikki.linnakangas@i     1046                 :CBC         156 :     conn->ssl_in_use = true;
                               1047                 :                : 
                               1048                 :                :     /*
                               1049                 :                :      * If SSL key logging is requested, set up the callback if a compatible
                               1050                 :                :      * version of OpenSSL is used and libpq was compiled to support it.
                               1051                 :                :      */
  156 dgustafsson@postgres     1052   [ +  +  +  - ]:            156 :     if (conn->sslkeylogfile && strlen(conn->sslkeylogfile) > 0)
                               1053                 :                :     {
                               1054                 :                : #ifdef HAVE_SSL_CTX_SET_KEYLOG_CALLBACK
                               1055                 :              2 :         SSL_CTX_set_keylog_callback(SSL_context, SSL_CTX_keylog_cb);
                               1056                 :                : #else
                               1057                 :                : #ifdef LIBRESSL_VERSION_NUMBER
                               1058                 :                :         fprintf(stderr, libpq_gettext("WARNING: sslkeylogfile support requires OpenSSL\n"));
                               1059                 :                : #else
                               1060                 :                :         fprintf(stderr, libpq_gettext("WARNING: libpq was not built with sslkeylogfile support\n"));
                               1061                 :                : #endif
                               1062                 :                : #endif
                               1063                 :                :     }
                               1064                 :                : 
                               1065                 :                :     /*
                               1066                 :                :      * SSL contexts are reference counted by OpenSSL. We can free it as soon
                               1067                 :                :      * as we have created the SSL object, and it will stick around for as long
                               1068                 :                :      * as it's actually needed.
                               1069                 :                :      */
 3256 heikki.linnakangas@i     1070                 :            156 :     SSL_CTX_free(SSL_context);
                               1071                 :            156 :     SSL_context = NULL;
                               1072                 :                : 
                               1073                 :                :     /*
                               1074                 :                :      * Set Server Name Indication (SNI), if enabled by connection parameters.
                               1075                 :                :      * Per RFC 6066, do not set it if the host is a literal IP address (IPv4
                               1076                 :                :      * or IPv6).
                               1077                 :                :      */
 1485 dgustafsson@postgres     1078   [ +  -  +  - ]:            156 :     if (conn->sslsni && conn->sslsni[0] == '1')
                               1079                 :                :     {
 1551 peter@eisentraut.org     1080                 :            156 :         const char *host = conn->connhost[conn->whichhost].host;
                               1081                 :                : 
                               1082   [ +  -  +  - ]:            156 :         if (host && host[0] &&
                               1083         [ +  + ]:            156 :             !(strspn(host, "0123456789.") == strlen(host) ||
                               1084         [ +  + ]:            146 :               strchr(host, ':')))
                               1085                 :                :         {
                               1086         [ -  + ]:            139 :             if (SSL_set_tlsext_host_name(conn->ssl, host) != 1)
                               1087                 :                :             {
 1551 peter@eisentraut.org     1088                 :UBC           0 :                 char       *err = SSLerrmessage(ERR_get_error());
                               1089                 :                : 
 1026                          1090                 :              0 :                 libpq_append_conn_error(conn, "could not set SSL Server Name Indication (SNI): %s", err);
 1551                          1091                 :              0 :                 SSLerrfree(err);
                               1092                 :              0 :                 return -1;
                               1093                 :                :             }
                               1094                 :                :         }
                               1095                 :                :     }
                               1096                 :                : 
                               1097                 :                :     /* Set ALPN */
                               1098                 :                :     {
                               1099                 :                :         int         retval;
                               1100                 :                : 
  516 heikki.linnakangas@i     1101                 :CBC         156 :         retval = SSL_set_alpn_protos(conn->ssl, alpn_protos, sizeof(alpn_protos));
                               1102                 :                : 
                               1103         [ -  + ]:            156 :         if (retval != 0)
                               1104                 :                :         {
  516 heikki.linnakangas@i     1105                 :UBC           0 :             char       *err = SSLerrmessage(ERR_get_error());
                               1106                 :                : 
  515                          1107                 :              0 :             libpq_append_conn_error(conn, "could not set SSL ALPN extension: %s", err);
  516                          1108                 :              0 :             SSLerrfree(err);
                               1109                 :              0 :             return -1;
                               1110                 :                :         }
                               1111                 :                :     }
                               1112                 :                : 
                               1113                 :                :     /*
                               1114                 :                :      * Read the SSL key. If a key is specified, treat it as an engine:key
                               1115                 :                :      * combination if there is colon present - we don't support files with
                               1116                 :                :      * colon in the name. The exception is if the second character is a colon,
                               1117                 :                :      * in which case it can be a Windows filename with drive specification.
                               1118                 :                :      */
 4044 heikki.linnakangas@i     1119   [ +  +  +  -  :CBC         156 :     if (have_cert && conn->sslkey && strlen(conn->sslkey) > 0)
                                              +  - ]
                               1120                 :                :     {
                               1121                 :                : #ifdef USE_SSL_ENGINE
                               1122         [ -  + ]:             76 :         if (strchr(conn->sslkey, ':')
                               1123                 :                : #ifdef WIN32
                               1124                 :                :             && conn->sslkey[1] != ':'
                               1125                 :                : #endif
                               1126                 :                :             )
                               1127                 :                :         {
                               1128                 :                :             /* Colon, but not in second character, treat as engine:key */
 4044 heikki.linnakangas@i     1129                 :UBC           0 :             char       *engine_str = strdup(conn->sslkey);
                               1130                 :                :             char       *engine_colon;
                               1131                 :                :             EVP_PKEY   *pkey;
                               1132                 :                : 
                               1133         [ #  # ]:              0 :             if (engine_str == NULL)
                               1134                 :                :             {
 1026 peter@eisentraut.org     1135                 :              0 :                 libpq_append_conn_error(conn, "out of memory");
 4044 heikki.linnakangas@i     1136                 :              0 :                 return -1;
                               1137                 :                :             }
                               1138                 :                : 
                               1139                 :                :             /* cannot return NULL because we already checked before strdup */
                               1140                 :              0 :             engine_colon = strchr(engine_str, ':');
                               1141                 :                : 
 2999 tgl@sss.pgh.pa.us        1142                 :              0 :             *engine_colon = '\0';   /* engine_str now has engine name */
 4044 heikki.linnakangas@i     1143                 :              0 :             engine_colon++;     /* engine_colon now has key name */
                               1144                 :                : 
                               1145                 :              0 :             conn->engine = ENGINE_by_id(engine_str);
                               1146         [ #  # ]:              0 :             if (conn->engine == NULL)
                               1147                 :                :             {
 3438 peter_e@gmx.net          1148                 :              0 :                 char       *err = SSLerrmessage(ERR_get_error());
                               1149                 :                : 
 1026 peter@eisentraut.org     1150                 :              0 :                 libpq_append_conn_error(conn, "could not load SSL engine \"%s\": %s",
                               1151                 :                :                                         engine_str, err);
 4044 heikki.linnakangas@i     1152                 :              0 :                 SSLerrfree(err);
                               1153                 :              0 :                 free(engine_str);
                               1154                 :              0 :                 return -1;
                               1155                 :                :             }
                               1156                 :                : 
                               1157         [ #  # ]:              0 :             if (ENGINE_init(conn->engine) == 0)
                               1158                 :                :             {
 3438 peter_e@gmx.net          1159                 :              0 :                 char       *err = SSLerrmessage(ERR_get_error());
                               1160                 :                : 
 1026 peter@eisentraut.org     1161                 :              0 :                 libpq_append_conn_error(conn, "could not initialize SSL engine \"%s\": %s",
                               1162                 :                :                                         engine_str, err);
 4044 heikki.linnakangas@i     1163                 :              0 :                 SSLerrfree(err);
                               1164                 :              0 :                 ENGINE_free(conn->engine);
                               1165                 :              0 :                 conn->engine = NULL;
                               1166                 :              0 :                 free(engine_str);
                               1167                 :              0 :                 return -1;
                               1168                 :                :             }
                               1169                 :                : 
                               1170                 :              0 :             pkey = ENGINE_load_private_key(conn->engine, engine_colon,
                               1171                 :                :                                            NULL, NULL);
                               1172         [ #  # ]:              0 :             if (pkey == NULL)
                               1173                 :                :             {
 3438 peter_e@gmx.net          1174                 :              0 :                 char       *err = SSLerrmessage(ERR_get_error());
                               1175                 :                : 
 1026 peter@eisentraut.org     1176                 :              0 :                 libpq_append_conn_error(conn, "could not read private SSL key \"%s\" from engine \"%s\": %s",
                               1177                 :                :                                         engine_colon, engine_str, err);
 4044 heikki.linnakangas@i     1178                 :              0 :                 SSLerrfree(err);
                               1179                 :              0 :                 ENGINE_finish(conn->engine);
                               1180                 :              0 :                 ENGINE_free(conn->engine);
                               1181                 :              0 :                 conn->engine = NULL;
                               1182                 :              0 :                 free(engine_str);
                               1183                 :              0 :                 return -1;
                               1184                 :                :             }
                               1185         [ #  # ]:              0 :             if (SSL_use_PrivateKey(conn->ssl, pkey) != 1)
                               1186                 :                :             {
 3438 peter_e@gmx.net          1187                 :              0 :                 char       *err = SSLerrmessage(ERR_get_error());
                               1188                 :                : 
 1026 peter@eisentraut.org     1189                 :              0 :                 libpq_append_conn_error(conn, "could not load private SSL key \"%s\" from engine \"%s\": %s",
                               1190                 :                :                                         engine_colon, engine_str, err);
 4044 heikki.linnakangas@i     1191                 :              0 :                 SSLerrfree(err);
                               1192                 :              0 :                 ENGINE_finish(conn->engine);
                               1193                 :              0 :                 ENGINE_free(conn->engine);
                               1194                 :              0 :                 conn->engine = NULL;
                               1195                 :              0 :                 free(engine_str);
                               1196                 :              0 :                 return -1;
                               1197                 :                :             }
                               1198                 :                : 
                               1199                 :              0 :             free(engine_str);
                               1200                 :                : 
                               1201                 :              0 :             fnbuf[0] = '\0';    /* indicate we're not going to load from a
                               1202                 :                :                                  * file */
                               1203                 :                :         }
                               1204                 :                :         else
                               1205                 :                : #endif                          /* USE_SSL_ENGINE */
                               1206                 :                :         {
                               1207                 :                :             /* PGSSLKEY is not an engine, treat it as a filename */
 3878 tgl@sss.pgh.pa.us        1208                 :CBC          38 :             strlcpy(fnbuf, conn->sslkey, sizeof(fnbuf));
                               1209                 :                :         }
                               1210                 :                :     }
 4044 heikki.linnakangas@i     1211         [ +  + ]:            118 :     else if (have_homedir)
                               1212                 :                :     {
                               1213                 :                :         /* No PGSSLKEY specified, load default file */
                               1214                 :             48 :         snprintf(fnbuf, sizeof(fnbuf), "%s/%s", homedir, USER_KEY_FILE);
                               1215                 :                :     }
                               1216                 :                :     else
                               1217                 :             70 :         fnbuf[0] = '\0';
                               1218                 :                : 
                               1219   [ +  +  +  - ]:            156 :     if (have_cert && fnbuf[0] != '\0')
                               1220                 :                :     {
                               1221                 :                :         /* read the client key from file */
                               1222                 :                : 
                               1223         [ -  + ]:             38 :         if (stat(fnbuf, &buf) != 0)
                               1224                 :                :         {
 1376 dgustafsson@postgres     1225         [ #  # ]:UBC           0 :             if (errno == ENOENT)
 1026 peter@eisentraut.org     1226                 :              0 :                 libpq_append_conn_error(conn, "certificate present, but not private key file \"%s\"",
                               1227                 :                :                                         fnbuf);
                               1228                 :                :             else
                               1229                 :              0 :                 libpq_append_conn_error(conn, "could not stat private key file \"%s\": %m",
                               1230                 :                :                                         fnbuf);
 4044 heikki.linnakangas@i     1231                 :              0 :             return -1;
                               1232                 :                :         }
                               1233                 :                : 
                               1234                 :                :         /* Key file must be a regular file */
 1286 tgl@sss.pgh.pa.us        1235         [ -  + ]:CBC          38 :         if (!S_ISREG(buf.st_mode))
                               1236                 :                :         {
 1026 peter@eisentraut.org     1237                 :UBC           0 :             libpq_append_conn_error(conn, "private key file \"%s\" is not a regular file",
                               1238                 :                :                                     fnbuf);
 1286 tgl@sss.pgh.pa.us        1239                 :              0 :             return -1;
                               1240                 :                :         }
                               1241                 :                : 
                               1242                 :                :         /*
                               1243                 :                :          * Refuse to load world-readable key files.  We accept root-owned
                               1244                 :                :          * files with mode 0640 or less, so that we can access system-wide
                               1245                 :                :          * certificates if we have a supplementary group membership that
                               1246                 :                :          * allows us to read 'em.  For files with non-root ownership, require
                               1247                 :                :          * mode 0600 or less.  We need not check the file's ownership exactly;
                               1248                 :                :          * if we're able to read it despite it having such restrictive
                               1249                 :                :          * permissions, it must have the right ownership.
                               1250                 :                :          *
                               1251                 :                :          * Note: be very careful about tightening these rules.  Some people
                               1252                 :                :          * expect, for example, that a client process running as root should
                               1253                 :                :          * be able to use a non-root-owned key file.
                               1254                 :                :          *
                               1255                 :                :          * Note that roughly similar checks are performed in
                               1256                 :                :          * src/backend/libpq/be-secure-common.c so any changes here may need
                               1257                 :                :          * to be made there as well.  However, this code caters for the case
                               1258                 :                :          * of current user == root, while that code does not.
                               1259                 :                :          *
                               1260                 :                :          * Ideally we would do similar permissions checks on Windows, but it
                               1261                 :                :          * is not clear how that would work since Unix-style permissions may
                               1262                 :                :          * not be available.
                               1263                 :                :          */
                               1264                 :                : #if !defined(WIN32) && !defined(__CYGWIN__)
 1199 tgl@sss.pgh.pa.us        1265   [ -  +  +  + ]:CBC          76 :         if (buf.st_uid == 0 ?
 1199 tgl@sss.pgh.pa.us        1266                 :UBC           0 :             buf.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO) :
 1199 tgl@sss.pgh.pa.us        1267                 :CBC          38 :             buf.st_mode & (S_IRWXG | S_IRWXO))
                               1268                 :                :         {
 1026 peter@eisentraut.org     1269                 :              1 :             libpq_append_conn_error(conn,
                               1270                 :                :                                     "private key file \"%s\" has group or world access; file must have permissions u=rw (0600) or less if owned by the current user, or permissions u=rw,g=r (0640) or less if owned by root",
                               1271                 :                :                                     fnbuf);
 4044 heikki.linnakangas@i     1272                 :              1 :             return -1;
                               1273                 :                :         }
                               1274                 :                : #endif
                               1275                 :                : 
                               1276         [ +  + ]:             37 :         if (SSL_use_PrivateKey_file(conn->ssl, fnbuf, SSL_FILETYPE_PEM) != 1)
                               1277                 :                :         {
 3438 peter_e@gmx.net          1278                 :              3 :             char       *err = SSLerrmessage(ERR_get_error());
                               1279                 :                : 
                               1280                 :                :             /*
                               1281                 :                :              * We'll try to load the file in DER (binary ASN.1) format, and if
                               1282                 :                :              * that fails too, report the original error. This could mask
                               1283                 :                :              * issues where there's something wrong with a DER-format cert,
                               1284                 :                :              * but we'd have to duplicate openssl's format detection to be
                               1285                 :                :              * smarter than this. We can't just probe for a leading -----BEGIN
                               1286                 :                :              * because PEM can have leading non-matching lines and blanks.
                               1287                 :                :              * OpenSSL doesn't expose its get_name(...) and its PEM routines
                               1288                 :                :              * don't differentiate between failure modes in enough detail to
                               1289                 :                :              * let us tell the difference between "not PEM, try DER" and
                               1290                 :                :              * "wrong password".
                               1291                 :                :              */
 2107 andrew@dunslane.net      1292         [ +  + ]:              3 :             if (SSL_use_PrivateKey_file(conn->ssl, fnbuf, SSL_FILETYPE_ASN1) != 1)
                               1293                 :                :             {
 1026 peter@eisentraut.org     1294                 :              1 :                 libpq_append_conn_error(conn, "could not load private key file \"%s\": %s",
                               1295                 :                :                                         fnbuf, err);
 2107 andrew@dunslane.net      1296                 :              1 :                 SSLerrfree(err);
                               1297                 :              1 :                 return -1;
                               1298                 :                :             }
                               1299                 :                : 
 4044 heikki.linnakangas@i     1300                 :              2 :             SSLerrfree(err);
                               1301                 :                :         }
                               1302                 :                :     }
                               1303                 :                : 
                               1304                 :                :     /* verify that the cert and key go together */
                               1305   [ +  +  -  + ]:            190 :     if (have_cert &&
                               1306                 :             36 :         SSL_check_private_key(conn->ssl) != 1)
                               1307                 :                :     {
 3438 peter_e@gmx.net          1308                 :UBC           0 :         char       *err = SSLerrmessage(ERR_get_error());
                               1309                 :                : 
 1026 peter@eisentraut.org     1310                 :              0 :         libpq_append_conn_error(conn, "certificate does not match private key file \"%s\": %s",
                               1311                 :                :                                 fnbuf, err);
 4044 heikki.linnakangas@i     1312                 :              0 :         SSLerrfree(err);
                               1313                 :              0 :         return -1;
                               1314                 :                :     }
                               1315                 :                : 
                               1316                 :                :     /*
                               1317                 :                :      * If a root cert was loaded, also set our certificate verification
                               1318                 :                :      * callback.
                               1319                 :                :      */
 3256 heikki.linnakangas@i     1320         [ +  + ]:CBC         154 :     if (have_rootcert)
 4044                          1321                 :            102 :         SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, verify_cb);
                               1322                 :                : 
                               1323                 :                :     /*
                               1324                 :                :      * Set compression option if necessary.
                               1325                 :                :      */
 1641 michael@paquier.xyz      1326   [ +  -  +  - ]:            154 :     if (conn->sslcompression && conn->sslcompression[0] == '0')
                               1327                 :            154 :         SSL_set_options(conn->ssl, SSL_OP_NO_COMPRESSION);
                               1328                 :                :     else
 1641 michael@paquier.xyz      1329                 :UBC           0 :         SSL_clear_options(conn->ssl, SSL_OP_NO_COMPRESSION);
                               1330                 :                : 
 4044 heikki.linnakangas@i     1331                 :CBC         154 :     return 0;
                               1332                 :                : }
                               1333                 :                : 
                               1334                 :                : /*
                               1335                 :                :  *  Attempt to negotiate SSL connection.
                               1336                 :                :  */
                               1337                 :                : static PostgresPollingStatusType
                               1338                 :            294 : open_client_SSL(PGconn *conn)
                               1339                 :                : {
                               1340                 :                :     int         r;
                               1341                 :                : 
  871 dgustafsson@postgres     1342                 :            294 :     SOCK_ERRNO_SET(0);
 3438 peter_e@gmx.net          1343                 :            294 :     ERR_clear_error();
 4044 heikki.linnakangas@i     1344                 :            294 :     r = SSL_connect(conn->ssl);
                               1345         [ +  + ]:            294 :     if (r <= 0)
                               1346                 :                :     {
  871 dgustafsson@postgres     1347                 :            155 :         int         save_errno = SOCK_ERRNO;
 4044 heikki.linnakangas@i     1348                 :            155 :         int         err = SSL_get_error(conn->ssl, r);
                               1349                 :                :         unsigned long ecode;
                               1350                 :                : 
 3438 peter_e@gmx.net          1351                 :            155 :         ecode = ERR_get_error();
 4044 heikki.linnakangas@i     1352   [ +  -  -  +  :            155 :         switch (err)
                                                 - ]
                               1353                 :                :         {
                               1354                 :            140 :             case SSL_ERROR_WANT_READ:
                               1355                 :            140 :                 return PGRES_POLLING_READING;
                               1356                 :                : 
 4044 heikki.linnakangas@i     1357                 :UBC           0 :             case SSL_ERROR_WANT_WRITE:
                               1358                 :              0 :                 return PGRES_POLLING_WRITING;
                               1359                 :                : 
                               1360                 :              0 :             case SSL_ERROR_SYSCALL:
                               1361                 :                :                 {
                               1362                 :                :                     char        sebuf[PG_STRERROR_R_BUFLEN];
                               1363                 :                :                     unsigned long vcode;
                               1364                 :                : 
  871 dgustafsson@postgres     1365                 :              0 :                     vcode = SSL_get_verify_result(conn->ssl);
                               1366                 :                : 
                               1367                 :                :                     /*
                               1368                 :                :                      * If we get an X509 error here for failing to load the
                               1369                 :                :                      * local issuer cert, without an error in the socket layer
                               1370                 :                :                      * it means that verification failed due to a missing
                               1371                 :                :                      * system CA pool without it being a protocol error. We
                               1372                 :                :                      * inspect the sslrootcert setting to ensure that the user
                               1373                 :                :                      * was using the system CA pool. For other errors, log
                               1374                 :                :                      * them using the normal SYSCALL logging.
                               1375                 :                :                      */
  635 tgl@sss.pgh.pa.us        1376   [ #  #  #  # ]:              0 :                     if (save_errno == 0 &&
                               1377                 :              0 :                         vcode == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY &&
  871 dgustafsson@postgres     1378         [ #  # ]:              0 :                         strcmp(conn->sslrootcert, "system") == 0)
                               1379                 :              0 :                         libpq_append_conn_error(conn, "SSL error: certificate verify failed: %s",
                               1380                 :                :                                                 X509_verify_cert_error_string(vcode));
  635 tgl@sss.pgh.pa.us        1381   [ #  #  #  # ]:              0 :                     else if (r == -1 && save_errno != 0)
 1026 peter@eisentraut.org     1382                 :              0 :                         libpq_append_conn_error(conn, "SSL SYSCALL error: %s",
                               1383                 :                :                                                 SOCK_STRERROR(save_errno, sebuf, sizeof(sebuf)));
                               1384                 :                :                     else
                               1385                 :              0 :                         libpq_append_conn_error(conn, "SSL SYSCALL error: EOF detected");
 4044 heikki.linnakangas@i     1386                 :              0 :                     pgtls_close(conn);
                               1387                 :              0 :                     return PGRES_POLLING_FAILED;
                               1388                 :                :                 }
 4044 heikki.linnakangas@i     1389                 :CBC          15 :             case SSL_ERROR_SSL:
                               1390                 :                :                 {
 3438 peter_e@gmx.net          1391                 :             15 :                     char       *err = SSLerrmessage(ecode);
                               1392                 :                : 
 1026 peter@eisentraut.org     1393                 :             15 :                     libpq_append_conn_error(conn, "SSL error: %s", err);
 4044 heikki.linnakangas@i     1394                 :             15 :                     SSLerrfree(err);
 1897 tgl@sss.pgh.pa.us        1395         [ -  + ]:             15 :                     switch (ERR_GET_REASON(ecode))
                               1396                 :                :                     {
                               1397                 :                :                             /*
                               1398                 :                :                              * UNSUPPORTED_PROTOCOL, WRONG_VERSION_NUMBER, and
                               1399                 :                :                              * TLSV1_ALERT_PROTOCOL_VERSION have been observed
                               1400                 :                :                              * when trying to communicate with an old OpenSSL
                               1401                 :                :                              * library, or when the client and server specify
                               1402                 :                :                              * disjoint protocol ranges.
                               1403                 :                :                              * NO_PROTOCOLS_AVAILABLE occurs if there's a
                               1404                 :                :                              * local misconfiguration (which can happen
                               1405                 :                :                              * despite our checks, if openssl.cnf injects a
                               1406                 :                :                              * limit we didn't account for).  It's not very
                               1407                 :                :                              * clear what would make OpenSSL return the other
                               1408                 :                :                              * codes listed here, but a hint about protocol
                               1409                 :                :                              * versions seems like it's appropriate for all.
                               1410                 :                :                              */
 1897 tgl@sss.pgh.pa.us        1411                 :UBC           0 :                         case SSL_R_NO_PROTOCOLS_AVAILABLE:
                               1412                 :                :                         case SSL_R_UNSUPPORTED_PROTOCOL:
                               1413                 :                :                         case SSL_R_BAD_PROTOCOL_VERSION_NUMBER:
                               1414                 :                :                         case SSL_R_UNKNOWN_PROTOCOL:
                               1415                 :                :                         case SSL_R_UNKNOWN_SSL_VERSION:
                               1416                 :                :                         case SSL_R_UNSUPPORTED_SSL_VERSION:
                               1417                 :                :                         case SSL_R_WRONG_SSL_VERSION:
                               1418                 :                :                         case SSL_R_WRONG_VERSION_NUMBER:
                               1419                 :                :                         case SSL_R_TLSV1_ALERT_PROTOCOL_VERSION:
                               1420                 :                : #ifdef SSL_R_VERSION_TOO_HIGH
                               1421                 :                :                         case SSL_R_VERSION_TOO_HIGH:
                               1422                 :                :                         case SSL_R_VERSION_TOO_LOW:
                               1423                 :                : #endif
 1026 peter@eisentraut.org     1424                 :              0 :                             libpq_append_conn_error(conn, "This may indicate that the server does not support any SSL protocol version between %s and %s.",
  841 tgl@sss.pgh.pa.us        1425         [ #  # ]:              0 :                                                     conn->ssl_min_protocol_version ?
                               1426                 :                :                                                     conn->ssl_min_protocol_version :
                               1427                 :                :                                                     MIN_OPENSSL_TLS_VERSION,
                               1428         [ #  # ]:              0 :                                                     conn->ssl_max_protocol_version ?
                               1429                 :                :                                                     conn->ssl_max_protocol_version :
                               1430                 :                :                                                     MAX_OPENSSL_TLS_VERSION);
 1897                          1431                 :              0 :                             break;
 1897 tgl@sss.pgh.pa.us        1432                 :CBC          15 :                         default:
                               1433                 :             15 :                             break;
                               1434                 :                :                     }
 4044 heikki.linnakangas@i     1435                 :             15 :                     pgtls_close(conn);
                               1436                 :             15 :                     return PGRES_POLLING_FAILED;
                               1437                 :                :                 }
                               1438                 :                : 
 4044 heikki.linnakangas@i     1439                 :UBC           0 :             default:
 1026 peter@eisentraut.org     1440                 :              0 :                 libpq_append_conn_error(conn, "unrecognized SSL error code: %d", err);
 4044 heikki.linnakangas@i     1441                 :              0 :                 pgtls_close(conn);
                               1442                 :              0 :                 return PGRES_POLLING_FAILED;
                               1443                 :                :         }
                               1444                 :                :     }
                               1445                 :                : 
                               1446                 :                :     /* ALPN is mandatory with direct SSL connections */
  478 heikki.linnakangas@i     1447   [ +  -  +  + ]:CBC         139 :     if (conn->current_enc_method == ENC_SSL && conn->sslnegotiation[0] == 'd')
                               1448                 :                :     {
                               1449                 :                :         const unsigned char *selected;
                               1450                 :                :         unsigned int len;
                               1451                 :                : 
  495                          1452                 :             10 :         SSL_get0_alpn_selected(conn->ssl, &selected, &len);
                               1453                 :                : 
                               1454         [ -  + ]:             10 :         if (selected == NULL)
                               1455                 :                :         {
  495 heikki.linnakangas@i     1456                 :UBC           0 :             libpq_append_conn_error(conn, "direct SSL connection was established without ALPN protocol negotiation extension");
                               1457                 :              0 :             pgtls_close(conn);
                               1458                 :              0 :             return PGRES_POLLING_FAILED;
                               1459                 :                :         }
                               1460                 :                : 
                               1461                 :                :         /*
                               1462                 :                :          * We only support one protocol so that's what the negotiation should
                               1463                 :                :          * always choose, but doesn't hurt to check.
                               1464                 :                :          */
  495 heikki.linnakangas@i     1465         [ +  - ]:CBC          10 :         if (len != strlen(PG_ALPN_PROTOCOL) ||
                               1466         [ -  + ]:             10 :             memcmp(selected, PG_ALPN_PROTOCOL, strlen(PG_ALPN_PROTOCOL)) != 0)
                               1467                 :                :         {
  495 heikki.linnakangas@i     1468                 :UBC           0 :             libpq_append_conn_error(conn, "SSL connection was established with unexpected ALPN protocol");
                               1469                 :              0 :             pgtls_close(conn);
                               1470                 :              0 :             return PGRES_POLLING_FAILED;
                               1471                 :                :         }
                               1472                 :                :     }
                               1473                 :                : 
                               1474                 :                :     /*
                               1475                 :                :      * We already checked the server certificate in initialize_SSL() using
                               1476                 :                :      * SSL_CTX_set_verify(), if root.crt exists.
                               1477                 :                :      */
                               1478                 :                : 
                               1479                 :                :     /* get server certificate */
 4044 heikki.linnakangas@i     1480                 :CBC         139 :     conn->peer = SSL_get_peer_certificate(conn->ssl);
                               1481         [ -  + ]:            139 :     if (conn->peer == NULL)
                               1482                 :                :     {
 2043 tgl@sss.pgh.pa.us        1483                 :UBC           0 :         char       *err = SSLerrmessage(ERR_get_error());
                               1484                 :                : 
 1026 peter@eisentraut.org     1485                 :              0 :         libpq_append_conn_error(conn, "certificate could not be obtained: %s", err);
 4044 heikki.linnakangas@i     1486                 :              0 :         SSLerrfree(err);
                               1487                 :              0 :         pgtls_close(conn);
                               1488                 :              0 :         return PGRES_POLLING_FAILED;
                               1489                 :                :     }
                               1490                 :                : 
 2779 peter_e@gmx.net          1491         [ +  + ]:CBC         139 :     if (!pq_verify_peer_name_matches_certificate(conn))
                               1492                 :                :     {
 4044 heikki.linnakangas@i     1493                 :             13 :         pgtls_close(conn);
                               1494                 :             13 :         return PGRES_POLLING_FAILED;
                               1495                 :                :     }
                               1496                 :                : 
                               1497                 :                :     /* SSL handshake is complete */
                               1498                 :            126 :     return PGRES_POLLING_OK;
                               1499                 :                : }
                               1500                 :                : 
                               1501                 :                : void
                               1502                 :          26455 : pgtls_close(PGconn *conn)
                               1503                 :                : {
 1640 michael@paquier.xyz      1504         [ +  + ]:          26455 :     if (conn->ssl_in_use)
                               1505                 :                :     {
                               1506         [ +  - ]:            156 :         if (conn->ssl)
                               1507                 :                :         {
                               1508                 :                :             /*
                               1509                 :                :              * We can't destroy everything SSL-related here due to the
                               1510                 :                :              * possible later calls to OpenSSL routines which may need our
                               1511                 :                :              * thread callbacks, so set a flag here and check at the end.
                               1512                 :                :              */
                               1513                 :                : 
                               1514                 :            156 :             SSL_shutdown(conn->ssl);
                               1515                 :            156 :             SSL_free(conn->ssl);
                               1516                 :            156 :             conn->ssl = NULL;
                               1517                 :            156 :             conn->ssl_in_use = false;
  516 heikki.linnakangas@i     1518                 :            156 :             conn->ssl_handshake_started = false;
                               1519                 :                :         }
                               1520                 :                : 
 1640 michael@paquier.xyz      1521         [ +  + ]:            156 :         if (conn->peer)
                               1522                 :                :         {
                               1523                 :            139 :             X509_free(conn->peer);
                               1524                 :            139 :             conn->peer = NULL;
                               1525                 :                :         }
                               1526                 :                : 
                               1527                 :                : #ifdef USE_SSL_ENGINE
                               1528         [ -  + ]:            156 :         if (conn->engine)
                               1529                 :                :         {
 1640 michael@paquier.xyz      1530                 :UBC           0 :             ENGINE_finish(conn->engine);
                               1531                 :              0 :             ENGINE_free(conn->engine);
                               1532                 :              0 :             conn->engine = NULL;
                               1533                 :                :         }
                               1534                 :                : #endif
                               1535                 :                :     }
 4044 heikki.linnakangas@i     1536                 :CBC       26455 : }
                               1537                 :                : 
                               1538                 :                : 
                               1539                 :                : /*
                               1540                 :                :  * Obtain reason string for passed SSL errcode
                               1541                 :                :  *
                               1542                 :                :  * ERR_get_error() is used by caller to get errcode to pass here.
                               1543                 :                :  * The result must be freed after use, using SSLerrfree.
                               1544                 :                :  *
                               1545                 :                :  * Some caution is needed here since ERR_reason_error_string will return NULL
                               1546                 :                :  * if it doesn't recognize the error code, or (in OpenSSL >= 3) if the code
                               1547                 :                :  * represents a system errno value.  We don't want to return NULL ever.
                               1548                 :                :  */
                               1549                 :                : static char ssl_nomem[] = "out of memory allocating error description";
                               1550                 :                : 
                               1551                 :                : #define SSL_ERR_LEN 128
                               1552                 :                : 
                               1553                 :                : static char *
 3438 peter_e@gmx.net          1554                 :             24 : SSLerrmessage(unsigned long ecode)
                               1555                 :                : {
                               1556                 :                :     const char *errreason;
                               1557                 :                :     char       *errbuf;
                               1558                 :                : 
 4044 heikki.linnakangas@i     1559                 :             24 :     errbuf = malloc(SSL_ERR_LEN);
                               1560         [ -  + ]:             24 :     if (!errbuf)
 4044 heikki.linnakangas@i     1561                 :UBC           0 :         return ssl_nomem;
 3438 peter_e@gmx.net          1562         [ -  + ]:CBC          24 :     if (ecode == 0)
                               1563                 :                :     {
 4044 heikki.linnakangas@i     1564                 :UBC           0 :         snprintf(errbuf, SSL_ERR_LEN, libpq_gettext("no SSL error reported"));
                               1565                 :              0 :         return errbuf;
                               1566                 :                :     }
 3438 peter_e@gmx.net          1567                 :CBC          24 :     errreason = ERR_reason_error_string(ecode);
 4044 heikki.linnakangas@i     1568         [ +  - ]:             24 :     if (errreason != NULL)
                               1569                 :                :     {
                               1570                 :             24 :         strlcpy(errbuf, errreason, SSL_ERR_LEN);
                               1571                 :             24 :         return errbuf;
                               1572                 :                :     }
                               1573                 :                : 
                               1574                 :                :     /*
                               1575                 :                :      * Server aborted the connection with TLS "no_application_protocol" alert.
                               1576                 :                :      * The ERR_reason_error_string() function doesn't give any error string
                               1577                 :                :      * for that for some reason, so do it ourselves.  See
                               1578                 :                :      * https://github.com/openssl/openssl/issues/24300.  This is available in
                               1579                 :                :      * OpenSSL 1.1.0 and later, as well as in LibreSSL 3.4.3 (OpenBSD 7.0) and
                               1580                 :                :      * later.
                               1581                 :                :      */
                               1582                 :                : #ifdef SSL_AD_NO_APPLICATION_PROTOCOL
  495 heikki.linnakangas@i     1583   [ #  #  #  # ]:UBC           0 :     if (ERR_GET_LIB(ecode) == ERR_LIB_SSL &&
                               1584                 :              0 :         ERR_GET_REASON(ecode) == SSL_AD_REASON_OFFSET + SSL_AD_NO_APPLICATION_PROTOCOL)
                               1585                 :                :     {
  451 peter@eisentraut.org     1586                 :              0 :         snprintf(errbuf, SSL_ERR_LEN, "no application protocol");
  495 heikki.linnakangas@i     1587                 :              0 :         return errbuf;
                               1588                 :                :     }
                               1589                 :                : #endif
                               1590                 :                : 
                               1591                 :                :     /*
                               1592                 :                :      * In OpenSSL 3.0.0 and later, ERR_reason_error_string does not map system
                               1593                 :                :      * errno values anymore.  (See OpenSSL source code for the explanation.)
                               1594                 :                :      * We can cover that shortcoming with this bit of code.  Older OpenSSL
                               1595                 :                :      * versions don't have the ERR_SYSTEM_ERROR macro, but that's okay because
                               1596                 :                :      * they don't have the shortcoming either.
                               1597                 :                :      */
                               1598                 :                : #ifdef ERR_SYSTEM_ERROR
  548 tgl@sss.pgh.pa.us        1599         [ #  # ]:              0 :     if (ERR_SYSTEM_ERROR(ecode))
                               1600                 :                :     {
  405 peter@eisentraut.org     1601                 :              0 :         strerror_r(ERR_GET_REASON(ecode), errbuf, SSL_ERR_LEN);
  548 tgl@sss.pgh.pa.us        1602                 :              0 :         return errbuf;
                               1603                 :                :     }
                               1604                 :                : #endif
                               1605                 :                : 
                               1606                 :                :     /* No choice but to report the numeric ecode */
 3438 peter_e@gmx.net          1607                 :              0 :     snprintf(errbuf, SSL_ERR_LEN, libpq_gettext("SSL error code %lu"), ecode);
 4044 heikki.linnakangas@i     1608                 :              0 :     return errbuf;
                               1609                 :                : }
                               1610                 :                : 
                               1611                 :                : static void
 4044 heikki.linnakangas@i     1612                 :CBC          24 : SSLerrfree(char *buf)
                               1613                 :                : {
                               1614         [ +  - ]:             24 :     if (buf != ssl_nomem)
                               1615                 :             24 :         free(buf);
                               1616                 :             24 : }
                               1617                 :                : 
                               1618                 :                : /* ------------------------------------------------------------ */
                               1619                 :                : /*                  SSL information functions                   */
                               1620                 :                : /* ------------------------------------------------------------ */
                               1621                 :                : 
                               1622                 :                : /*
                               1623                 :                :  *  Return pointer to OpenSSL object.
                               1624                 :                :  */
                               1625                 :                : void *
 4044 heikki.linnakangas@i     1626                 :UBC           0 : PQgetssl(PGconn *conn)
                               1627                 :                : {
                               1628         [ #  # ]:              0 :     if (!conn)
                               1629                 :              0 :         return NULL;
                               1630                 :              0 :     return conn->ssl;
                               1631                 :                : }
                               1632                 :                : 
                               1633                 :                : void *
 3868                          1634                 :              0 : PQsslStruct(PGconn *conn, const char *struct_name)
                               1635                 :                : {
                               1636         [ #  # ]:              0 :     if (!conn)
                               1637                 :              0 :         return NULL;
                               1638         [ #  # ]:              0 :     if (strcmp(struct_name, "OpenSSL") == 0)
                               1639                 :              0 :         return conn->ssl;
                               1640                 :              0 :     return NULL;
                               1641                 :                : }
                               1642                 :                : 
                               1643                 :                : const char *const *
 3591 tgl@sss.pgh.pa.us        1644                 :              0 : PQsslAttributeNames(PGconn *conn)
                               1645                 :                : {
                               1646                 :                :     static const char *const openssl_attrs[] = {
                               1647                 :                :         "library",
                               1648                 :                :         "key_bits",
                               1649                 :                :         "cipher",
                               1650                 :                :         "compression",
                               1651                 :                :         "protocol",
                               1652                 :                :         "alpn",
                               1653                 :                :         NULL
                               1654                 :                :     };
                               1655                 :                :     static const char *const empty_attrs[] = {NULL};
                               1656                 :                : 
 1072                          1657         [ #  # ]:              0 :     if (!conn)
                               1658                 :                :     {
                               1659                 :                :         /* Return attributes of default SSL library */
                               1660                 :              0 :         return openssl_attrs;
                               1661                 :                :     }
                               1662                 :                : 
                               1663                 :                :     /* No attrs for unencrypted connection */
                               1664         [ #  # ]:              0 :     if (conn->ssl == NULL)
                               1665                 :              0 :         return empty_attrs;
                               1666                 :                : 
                               1667                 :              0 :     return openssl_attrs;
                               1668                 :                : }
                               1669                 :                : 
                               1670                 :                : const char *
 3868 heikki.linnakangas@i     1671                 :CBC           1 : PQsslAttribute(PGconn *conn, const char *attribute_name)
                               1672                 :                : {
                               1673         [ +  - ]:              1 :     if (!conn)
                               1674                 :                :     {
                               1675                 :                :         /* PQsslAttribute(NULL, "library") reports the default SSL library */
 1073 tgl@sss.pgh.pa.us        1676         [ +  - ]:              1 :         if (strcmp(attribute_name, "library") == 0)
                               1677                 :              1 :             return "OpenSSL";
 3868 heikki.linnakangas@i     1678                 :UBC           0 :         return NULL;
                               1679                 :                :     }
                               1680                 :                : 
                               1681                 :                :     /* All attributes read as NULL for a non-encrypted connection */
                               1682         [ #  # ]:              0 :     if (conn->ssl == NULL)
                               1683                 :              0 :         return NULL;
                               1684                 :                : 
 1073 tgl@sss.pgh.pa.us        1685         [ #  # ]:              0 :     if (strcmp(attribute_name, "library") == 0)
                               1686                 :              0 :         return "OpenSSL";
                               1687                 :                : 
 3868 heikki.linnakangas@i     1688         [ #  # ]:              0 :     if (strcmp(attribute_name, "key_bits") == 0)
                               1689                 :                :     {
                               1690                 :                :         static char sslbits_str[12];
                               1691                 :                :         int         sslbits;
                               1692                 :                : 
                               1693                 :              0 :         SSL_get_cipher_bits(conn->ssl, &sslbits);
                               1694                 :              0 :         snprintf(sslbits_str, sizeof(sslbits_str), "%d", sslbits);
                               1695                 :              0 :         return sslbits_str;
                               1696                 :                :     }
                               1697                 :                : 
                               1698         [ #  # ]:              0 :     if (strcmp(attribute_name, "cipher") == 0)
                               1699                 :              0 :         return SSL_get_cipher(conn->ssl);
                               1700                 :                : 
                               1701         [ #  # ]:              0 :     if (strcmp(attribute_name, "compression") == 0)
 1641 michael@paquier.xyz      1702         [ #  # ]:              0 :         return SSL_get_current_compression(conn->ssl) ? "on" : "off";
                               1703                 :                : 
 3868 heikki.linnakangas@i     1704         [ #  # ]:              0 :     if (strcmp(attribute_name, "protocol") == 0)
                               1705                 :              0 :         return SSL_get_version(conn->ssl);
                               1706                 :                : 
  516                          1707         [ #  # ]:              0 :     if (strcmp(attribute_name, "alpn") == 0)
                               1708                 :                :     {
                               1709                 :                :         const unsigned char *data;
                               1710                 :                :         unsigned int len;
                               1711                 :                :         static char alpn_str[256];  /* alpn doesn't support longer than 255
                               1712                 :                :                                      * bytes */
                               1713                 :                : 
                               1714                 :              0 :         SSL_get0_alpn_selected(conn->ssl, &data, &len);
                               1715   [ #  #  #  #  :              0 :         if (data == NULL || len == 0 || len > sizeof(alpn_str) - 1)
                                              #  # ]
  495                          1716                 :              0 :             return "";
  516                          1717                 :              0 :         memcpy(alpn_str, data, len);
                               1718                 :              0 :         alpn_str[len] = 0;
                               1719                 :              0 :         return alpn_str;
                               1720                 :                :     }
                               1721                 :                : 
 3759 bruce@momjian.us         1722                 :              0 :     return NULL;                /* unknown attribute */
                               1723                 :                : }
                               1724                 :                : 
                               1725                 :                : /*
                               1726                 :                :  * Private substitute BIO: this does the sending and receiving using
                               1727                 :                :  * pqsecure_raw_write() and pqsecure_raw_read() instead, to allow those
                               1728                 :                :  * functions to disable SIGPIPE and give better error messages on I/O errors.
                               1729                 :                :  *
                               1730                 :                :  * These functions are closely modelled on the standard socket BIO in OpenSSL;
                               1731                 :                :  * see sock_read() and sock_write() in OpenSSL's crypto/bio/bss_sock.c.
                               1732                 :                :  */
                               1733                 :                : 
                               1734                 :                : /* protected by ssl_config_mutex */
                               1735                 :                : static BIO_METHOD *pgconn_bio_method_ptr;
                               1736                 :                : 
                               1737                 :                : static int
  330 dgustafsson@postgres     1738                 :CBC        2702 : pgconn_bio_read(BIO *h, char *buf, int size)
                               1739                 :                : {
                               1740                 :           2702 :     PGconn     *conn = (PGconn *) BIO_get_data(h);
                               1741                 :                :     int         res;
                               1742                 :                : 
  516 heikki.linnakangas@i     1743                 :           2702 :     res = pqsecure_raw_read(conn, buf, size);
 4044                          1744                 :           2702 :     BIO_clear_retry_flags(h);
  330 dgustafsson@postgres     1745                 :           2702 :     conn->last_read_was_eof = res == 0;
 4044 heikki.linnakangas@i     1746         [ +  + ]:           2702 :     if (res < 0)
                               1747                 :                :     {
                               1748                 :                :         /* If we were interrupted, tell caller to retry */
 3631 tgl@sss.pgh.pa.us        1749         [ +  - ]:            242 :         switch (SOCK_ERRNO)
                               1750                 :                :         {
                               1751                 :                : #ifdef EAGAIN
 4044 heikki.linnakangas@i     1752                 :            242 :             case EAGAIN:
                               1753                 :                : #endif
                               1754                 :                : #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
                               1755                 :                :             case EWOULDBLOCK:
                               1756                 :                : #endif
                               1757                 :                :             case EINTR:
                               1758                 :            242 :                 BIO_set_retry_read(h);
                               1759                 :            242 :                 break;
                               1760                 :                : 
 4044 heikki.linnakangas@i     1761                 :UBC           0 :             default:
                               1762                 :              0 :                 break;
                               1763                 :                :         }
                               1764                 :                :     }
                               1765                 :                : 
  516 heikki.linnakangas@i     1766         [ +  + ]:CBC        2702 :     if (res > 0)
                               1767                 :           2454 :         conn->ssl_handshake_started = true;
                               1768                 :                : 
 4044                          1769                 :           2702 :     return res;
                               1770                 :                : }
                               1771                 :                : 
                               1772                 :                : static int
  330 dgustafsson@postgres     1773                 :            785 : pgconn_bio_write(BIO *h, const char *buf, int size)
                               1774                 :                : {
                               1775                 :                :     int         res;
                               1776                 :                : 
                               1777                 :            785 :     res = pqsecure_raw_write((PGconn *) BIO_get_data(h), buf, size);
 4044 heikki.linnakangas@i     1778                 :            785 :     BIO_clear_retry_flags(h);
 1302 tgl@sss.pgh.pa.us        1779         [ -  + ]:            785 :     if (res < 0)
                               1780                 :                :     {
                               1781                 :                :         /* If we were interrupted, tell caller to retry */
 3631 tgl@sss.pgh.pa.us        1782         [ #  # ]:UBC           0 :         switch (SOCK_ERRNO)
                               1783                 :                :         {
                               1784                 :                : #ifdef EAGAIN
                               1785                 :              0 :             case EAGAIN:
                               1786                 :                : #endif
                               1787                 :                : #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
                               1788                 :                :             case EWOULDBLOCK:
                               1789                 :                : #endif
                               1790                 :                :             case EINTR:
                               1791                 :              0 :                 BIO_set_retry_write(h);
                               1792                 :              0 :                 break;
                               1793                 :                : 
                               1794                 :              0 :             default:
                               1795                 :              0 :                 break;
                               1796                 :                :         }
                               1797                 :                :     }
                               1798                 :                : 
 4044 heikki.linnakangas@i     1799                 :CBC         785 :     return res;
                               1800                 :                : }
                               1801                 :                : 
                               1802                 :                : static long
  330 dgustafsson@postgres     1803                 :          14530 : pgconn_bio_ctrl(BIO *h, int cmd, long num, void *ptr)
                               1804                 :                : {
                               1805                 :                :     long        res;
                               1806                 :          14530 :     PGconn     *conn = (PGconn *) BIO_get_data(h);
                               1807                 :                : 
                               1808      [ +  +  + ]:          14530 :     switch (cmd)
                               1809                 :                :     {
                               1810                 :              6 :         case BIO_CTRL_EOF:
                               1811                 :                : 
                               1812                 :                :             /*
                               1813                 :                :              * This should not be needed. pgconn_bio_read already has a way to
                               1814                 :                :              * signal EOF to OpenSSL. However, OpenSSL made an undocumented,
                               1815                 :                :              * backwards-incompatible change and now expects EOF via BIO_ctrl.
                               1816                 :                :              * See https://github.com/openssl/openssl/issues/8208
                               1817                 :                :              */
                               1818                 :              6 :             res = conn->last_read_was_eof;
                               1819                 :              6 :             break;
                               1820                 :            441 :         case BIO_CTRL_FLUSH:
                               1821                 :                :             /* libssl expects all BIOs to support BIO_flush. */
                               1822                 :            441 :             res = 1;
                               1823                 :            441 :             break;
                               1824                 :          14083 :         default:
                               1825                 :          14083 :             res = 0;
                               1826                 :          14083 :             break;
                               1827                 :                :     }
                               1828                 :                : 
                               1829                 :          14530 :     return res;
                               1830                 :                : }
                               1831                 :                : 
                               1832                 :                : static BIO_METHOD *
                               1833                 :            156 : pgconn_bio_method(void)
                               1834                 :                : {
                               1835                 :                :     BIO_METHOD *res;
                               1836                 :                : 
  649 michael@paquier.xyz      1837         [ -  + ]:            156 :     if (pthread_mutex_lock(&ssl_config_mutex))
  649 michael@paquier.xyz      1838                 :UBC           0 :         return NULL;
                               1839                 :                : 
  330 dgustafsson@postgres     1840                 :CBC         156 :     res = pgconn_bio_method_ptr;
                               1841                 :                : 
                               1842         [ +  - ]:            156 :     if (!pgconn_bio_method_ptr)
                               1843                 :                :     {
                               1844                 :                :         int         my_bio_index;
                               1845                 :                : 
 3278 heikki.linnakangas@i     1846                 :            156 :         my_bio_index = BIO_get_new_index();
                               1847         [ -  + ]:            156 :         if (my_bio_index == -1)
  649 michael@paquier.xyz      1848                 :UBC           0 :             goto err;
  330 dgustafsson@postgres     1849                 :CBC         156 :         my_bio_index |= BIO_TYPE_SOURCE_SINK;
  649 michael@paquier.xyz      1850                 :            156 :         res = BIO_meth_new(my_bio_index, "libpq socket");
                               1851         [ -  + ]:            156 :         if (!res)
  649 michael@paquier.xyz      1852                 :UBC           0 :             goto err;
                               1853                 :                : 
                               1854                 :                :         /*
                               1855                 :                :          * As of this writing, these functions never fail. But check anyway,
                               1856                 :                :          * like OpenSSL's own examples do.
                               1857                 :                :          */
  330 dgustafsson@postgres     1858   [ +  -  +  - ]:CBC         312 :         if (!BIO_meth_set_write(res, pgconn_bio_write) ||
                               1859         [ -  + ]:            312 :             !BIO_meth_set_read(res, pgconn_bio_read) ||
                               1860                 :            156 :             !BIO_meth_set_ctrl(res, pgconn_bio_ctrl))
                               1861                 :                :         {
  649 michael@paquier.xyz      1862                 :UBC           0 :             goto err;
                               1863                 :                :         }
                               1864                 :                :     }
                               1865                 :                : 
  330 dgustafsson@postgres     1866                 :CBC         156 :     pgconn_bio_method_ptr = res;
  649 michael@paquier.xyz      1867                 :            156 :     pthread_mutex_unlock(&ssl_config_mutex);
                               1868                 :            156 :     return res;
                               1869                 :                : 
  649 michael@paquier.xyz      1870                 :UBC           0 : err:
                               1871         [ #  # ]:              0 :     if (res)
                               1872                 :              0 :         BIO_meth_free(res);
                               1873                 :              0 :     pthread_mutex_unlock(&ssl_config_mutex);
                               1874                 :              0 :     return NULL;
                               1875                 :                : }
                               1876                 :                : 
                               1877                 :                : static int
  330 dgustafsson@postgres     1878                 :CBC         156 : ssl_set_pgconn_bio(PGconn *conn)
                               1879                 :                : {
                               1880                 :                :     BIO        *bio;
                               1881                 :                :     BIO_METHOD *bio_method;
                               1882                 :                : 
                               1883                 :            156 :     bio_method = pgconn_bio_method();
 3278 heikki.linnakangas@i     1884         [ -  + ]:            156 :     if (bio_method == NULL)
  330 dgustafsson@postgres     1885                 :UBC           0 :         return 0;
                               1886                 :                : 
 3278 heikki.linnakangas@i     1887                 :CBC         156 :     bio = BIO_new(bio_method);
 4044                          1888         [ -  + ]:            156 :     if (bio == NULL)
  330 dgustafsson@postgres     1889                 :UBC           0 :         return 0;
                               1890                 :                : 
  330 dgustafsson@postgres     1891                 :CBC         156 :     BIO_set_data(bio, conn);
                               1892                 :            156 :     BIO_set_init(bio, 1);
                               1893                 :                : 
 4044 heikki.linnakangas@i     1894                 :            156 :     SSL_set_bio(conn->ssl, bio, bio);
  330 dgustafsson@postgres     1895                 :            156 :     return 1;
                               1896                 :                : }
                               1897                 :                : 
                               1898                 :                : /*
                               1899                 :                :  * This is the default handler to return a client cert password from
                               1900                 :                :  * conn->sslpassword. Apps may install it explicitly if they want to
                               1901                 :                :  * prevent openssl from ever prompting on stdin.
                               1902                 :                :  */
                               1903                 :                : int
 1939 andrew@dunslane.net      1904                 :              2 : PQdefaultSSLKeyPassHook_OpenSSL(char *buf, int size, PGconn *conn)
                               1905                 :                : {
 1118 tgl@sss.pgh.pa.us        1906   [ +  -  +  - ]:              2 :     if (conn && conn->sslpassword)
                               1907                 :                :     {
 2107 andrew@dunslane.net      1908         [ -  + ]:              2 :         if (strlen(conn->sslpassword) + 1 > size)
 1952 peter@eisentraut.org     1909                 :UBC           0 :             fprintf(stderr, libpq_gettext("WARNING: sslpassword truncated\n"));
 2107 andrew@dunslane.net      1910                 :CBC           2 :         strncpy(buf, conn->sslpassword, size);
 1941 tgl@sss.pgh.pa.us        1911                 :              2 :         buf[size - 1] = '\0';
 2107 andrew@dunslane.net      1912                 :              2 :         return strlen(buf);
                               1913                 :                :     }
                               1914                 :                :     else
                               1915                 :                :     {
 2107 andrew@dunslane.net      1916                 :UBC           0 :         buf[0] = '\0';
                               1917                 :              0 :         return 0;
                               1918                 :                :     }
                               1919                 :                : }
                               1920                 :                : 
                               1921                 :                : PQsslKeyPassHook_OpenSSL_type
 1939 tgl@sss.pgh.pa.us        1922                 :              0 : PQgetSSLKeyPassHook_OpenSSL(void)
                               1923                 :                : {
 2107 andrew@dunslane.net      1924                 :              0 :     return PQsslKeyPassHook;
                               1925                 :                : }
                               1926                 :                : 
                               1927                 :                : void
 1939                          1928                 :              0 : PQsetSSLKeyPassHook_OpenSSL(PQsslKeyPassHook_OpenSSL_type hook)
                               1929                 :                : {
 2107                          1930                 :              0 :     PQsslKeyPassHook = hook;
                               1931                 :              0 : }
                               1932                 :                : 
                               1933                 :                : /*
                               1934                 :                :  * Supply a password to decrypt a client certificate.
                               1935                 :                :  *
                               1936                 :                :  * This must match OpenSSL type pem_password_cb.
                               1937                 :                :  */
                               1938                 :                : static int
 2107 andrew@dunslane.net      1939                 :CBC           2 : PQssl_passwd_cb(char *buf, int size, int rwflag, void *userdata)
                               1940                 :                : {
 1941 tgl@sss.pgh.pa.us        1941                 :              2 :     PGconn     *conn = userdata;
                               1942                 :                : 
 2107 andrew@dunslane.net      1943         [ -  + ]:              2 :     if (PQsslKeyPassHook)
 2107 andrew@dunslane.net      1944                 :UBC           0 :         return PQsslKeyPassHook(buf, size, conn);
                               1945                 :                :     else
 1939 andrew@dunslane.net      1946                 :CBC           2 :         return PQdefaultSSLKeyPassHook_OpenSSL(buf, size, conn);
                               1947                 :                : }
                               1948                 :                : 
                               1949                 :                : /*
                               1950                 :                :  * Convert TLS protocol version string to OpenSSL values
                               1951                 :                :  *
                               1952                 :                :  * If a version is passed that is not supported by the current OpenSSL version,
                               1953                 :                :  * then we return -1. If a non-negative value is returned, subsequent code can
                               1954                 :                :  * assume it is working with a supported version.
                               1955                 :                :  *
                               1956                 :                :  * Note: this is rather similar to the backend routine in be-secure-openssl.c,
                               1957                 :                :  * so make sure to update both routines if changing this one.
                               1958                 :                :  */
                               1959                 :                : static int
 2048 michael@paquier.xyz      1960                 :            160 : ssl_protocol_version_to_openssl(const char *protocol)
                               1961                 :                : {
                               1962         [ -  + ]:            160 :     if (pg_strcasecmp("TLSv1", protocol) == 0)
 2048 michael@paquier.xyz      1963                 :UBC           0 :         return TLS1_VERSION;
                               1964                 :                : 
                               1965                 :                : #ifdef TLS1_1_VERSION
 2048 michael@paquier.xyz      1966         [ -  + ]:CBC         160 :     if (pg_strcasecmp("TLSv1.1", protocol) == 0)
 2048 michael@paquier.xyz      1967                 :UBC           0 :         return TLS1_1_VERSION;
                               1968                 :                : #endif
                               1969                 :                : 
                               1970                 :                : #ifdef TLS1_2_VERSION
 2048 michael@paquier.xyz      1971         [ +  - ]:CBC         160 :     if (pg_strcasecmp("TLSv1.2", protocol) == 0)
                               1972                 :            160 :         return TLS1_2_VERSION;
                               1973                 :                : #endif
                               1974                 :                : 
                               1975                 :                : #ifdef TLS1_3_VERSION
 2048 michael@paquier.xyz      1976         [ #  # ]:UBC           0 :     if (pg_strcasecmp("TLSv1.3", protocol) == 0)
                               1977                 :              0 :         return TLS1_3_VERSION;
                               1978                 :                : #endif
                               1979                 :                : 
                               1980                 :              0 :     return -1;
                               1981                 :                : }
        

Generated by: LCOV version 2.4-beta