LCOV - differential code coverage report
Current view: top level - src/interfaces/libpq - fe-secure-openssl.c (source / functions) Coverage Total Hit UBC GBC GNC CBC DCB
Current: 380a8b2ea024c33a35e7abc8628e7c4f52f9f9f9 vs db5ed03217b9c238703df8b4b286115d6e940488 Lines: 56.7 % 691 392 299 2 2 388 2
Current Date: 2026-05-29 21:51:00 -0400 Functions: 83.9 % 31 26 5 2 24 1
Baseline: lcov-20260530-034037-baseline Branches: 54.0 % 428 231 197 6 225
Baseline Date: 2026-05-29 14:39:03 -0700 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
[..1] days: 100.0 % 2 2 2
(30,360] days: 50.0 % 2 1 1 1
(360..) days: 56.6 % 687 389 298 2 387
Function coverage date bins:
[..1] days: 100.0 % 1 1 1
(360..) days: 83.3 % 30 25 5 1 24
Branch coverage date bins:
(360..) days: 54.0 % 428 231 197 6 225

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

Generated by: LCOV version 2.5.0-beta