PostgreSQL Source Code  git master
fe-secure-openssl.c File Reference
#include "postgres_fe.h"
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include "libpq-fe.h"
#include "fe-auth.h"
#include "fe-secure-common.h"
#include "libpq-int.h"
#include <sys/socket.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include "common/openssl.h"
#include <openssl/conf.h>
#include <openssl/x509v3.h>
Include dependency graph for fe-secure-openssl.c:

Go to the source code of this file.

Macros

#define SSL_ERR_LEN   128
 
#define BIO_get_data(bio)   (bio->ptr)
 
#define BIO_set_data(bio, data)   (bio->ptr = data)
 

Functions

static int verify_cb (int ok, X509_STORE_CTX *ctx)
 
static int openssl_verify_peer_name_matches_certificate_name (PGconn *conn, ASN1_STRING *name, char **store_name)
 
static int openssl_verify_peer_name_matches_certificate_ip (PGconn *conn, ASN1_OCTET_STRING *addr_entry, char **store_name)
 
static void destroy_ssl_system (void)
 
static int initialize_SSL (PGconn *conn)
 
static PostgresPollingStatusType open_client_SSL (PGconn *)
 
static char * SSLerrmessage (unsigned long ecode)
 
static void SSLerrfree (char *buf)
 
static int PQssl_passwd_cb (char *buf, int size, int rwflag, void *userdata)
 
static int my_sock_read (BIO *h, char *buf, int size)
 
static int my_sock_write (BIO *h, const char *buf, int size)
 
static BIO_METHOD * my_BIO_s_socket (void)
 
static int my_SSL_set_fd (PGconn *conn, int fd)
 
static int ssl_protocol_version_to_openssl (const char *protocol)
 
void pgtls_init_library (bool do_ssl, int do_crypto)
 
PostgresPollingStatusType pgtls_open_client (PGconn *conn)
 
ssize_t pgtls_read (PGconn *conn, void *ptr, size_t len)
 
bool pgtls_read_pending (PGconn *conn)
 
ssize_t pgtls_write (PGconn *conn, const void *ptr, size_t len)
 
static bool is_ip_address (const char *host)
 
int pgtls_verify_peer_name_matches_certificate_guts (PGconn *conn, int *names_examined, char **first_name)
 
int pgtls_init (PGconn *conn, bool do_ssl, bool do_crypto)
 
void pgtls_close (PGconn *conn)
 
void * PQgetssl (PGconn *conn)
 
void * PQsslStruct (PGconn *conn, const char *struct_name)
 
const char *const * PQsslAttributeNames (PGconn *conn)
 
const char * PQsslAttribute (PGconn *conn, const char *attribute_name)
 
int PQdefaultSSLKeyPassHook_OpenSSL (char *buf, int size, PGconn *conn)
 
PQsslKeyPassHook_OpenSSL_type PQgetSSLKeyPassHook_OpenSSL (void)
 
void PQsetSSLKeyPassHook_OpenSSL (PQsslKeyPassHook_OpenSSL_type hook)
 

Variables

static bool pq_init_ssl_lib = true
 
static bool pq_init_crypto_lib = true
 
static bool ssl_lib_initialized = false
 
static PQsslKeyPassHook_OpenSSL_type PQsslKeyPassHook = NULL
 
static char ssl_nomem [] = "out of memory allocating error description"
 
static BIO_METHOD * my_bio_methods
 

Macro Definition Documentation

◆ BIO_get_data

#define BIO_get_data (   bio)    (bio->ptr)

Definition at line 1792 of file fe-secure-openssl.c.

◆ BIO_set_data

#define BIO_set_data (   bio,
  data 
)    (bio->ptr = data)

Definition at line 1793 of file fe-secure-openssl.c.

◆ SSL_ERR_LEN

#define SSL_ERR_LEN   128

Definition at line 1674 of file fe-secure-openssl.c.

Function Documentation

◆ destroy_ssl_system()

static void destroy_ssl_system ( void  )
static

Definition at line 870 of file fe-secure-openssl.c.

871 {
872 #if defined(ENABLE_THREAD_SAFETY) && defined(HAVE_CRYPTO_LOCK)
873  /* Mutex is created in pgtls_init() */
874  if (pthread_mutex_lock(&ssl_config_mutex))
875  return;
876 
877  if (pq_init_crypto_lib && crypto_open_connections > 0)
878  --crypto_open_connections;
879 
880  if (pq_init_crypto_lib && crypto_open_connections == 0)
881  {
882  /*
883  * No connections left, unregister libcrypto callbacks, if no one
884  * registered different ones in the meantime.
885  */
886  if (CRYPTO_get_locking_callback() == pq_lockingcallback)
887  CRYPTO_set_locking_callback(NULL);
888  if (CRYPTO_get_id_callback() == pq_threadidcallback)
889  CRYPTO_set_id_callback(NULL);
890 
891  /*
892  * We don't free the lock array. If we get another connection in this
893  * process, we will just re-use them with the existing mutexes.
894  *
895  * This means we leak a little memory on repeated load/unload of the
896  * library.
897  */
898  }
899 
900  pthread_mutex_unlock(&ssl_config_mutex);
901 #endif
902 }
static bool pq_init_crypto_lib
int pthread_mutex_unlock(pthread_mutex_t *mp)
Definition: pthread-win32.c:54
int pthread_mutex_lock(pthread_mutex_t *mp)
Definition: pthread-win32.c:45

References pq_init_crypto_lib, pthread_mutex_lock(), and pthread_mutex_unlock().

Referenced by pgtls_close().

◆ initialize_SSL()

static int initialize_SSL ( PGconn conn)
static

Definition at line 911 of file fe-secure-openssl.c.

912 {
913  SSL_CTX *SSL_context;
914  struct stat buf;
915  char homedir[MAXPGPATH];
916  char fnbuf[MAXPGPATH];
917  char sebuf[PG_STRERROR_R_BUFLEN];
918  bool have_homedir;
919  bool have_cert;
920  bool have_rootcert;
921  EVP_PKEY *pkey = NULL;
922 
923  /*
924  * We'll need the home directory if any of the relevant parameters are
925  * defaulted. If pqGetHomeDirectory fails, act as though none of the
926  * files could be found.
927  */
928  if (!(conn->sslcert && strlen(conn->sslcert) > 0) ||
929  !(conn->sslkey && strlen(conn->sslkey) > 0) ||
930  !(conn->sslrootcert && strlen(conn->sslrootcert) > 0) ||
931  !((conn->sslcrl && strlen(conn->sslcrl) > 0) ||
932  (conn->sslcrldir && strlen(conn->sslcrldir) > 0)))
933  have_homedir = pqGetHomeDirectory(homedir, sizeof(homedir));
934  else /* won't need it */
935  have_homedir = false;
936 
937  /*
938  * Create a new SSL_CTX object.
939  *
940  * We used to share a single SSL_CTX between all connections, but it was
941  * complicated if connections used different certificates. So now we
942  * create a separate context for each connection, and accept the overhead.
943  */
944  SSL_context = SSL_CTX_new(SSLv23_method());
945  if (!SSL_context)
946  {
947  char *err = SSLerrmessage(ERR_get_error());
948 
950  libpq_gettext("could not create SSL context: %s\n"),
951  err);
952  SSLerrfree(err);
953  return -1;
954  }
955 
956  /*
957  * Delegate the client cert password prompt to the libpq wrapper callback
958  * if any is defined.
959  *
960  * If the application hasn't installed its own and the sslpassword
961  * parameter is non-null, we install ours now to make sure we supply
962  * PGconn->sslpassword to OpenSSL instead of letting it prompt on stdin.
963  *
964  * This will replace OpenSSL's default PEM_def_callback (which prompts on
965  * stdin), but we're only setting it for this SSL context so it's
966  * harmless.
967  */
968  if (PQsslKeyPassHook
969  || (conn->sslpassword && strlen(conn->sslpassword) > 0))
970  {
971  SSL_CTX_set_default_passwd_cb(SSL_context, PQssl_passwd_cb);
972  SSL_CTX_set_default_passwd_cb_userdata(SSL_context, conn);
973  }
974 
975  /* Disable old protocol versions */
976  SSL_CTX_set_options(SSL_context, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
977 
978  /* Set the minimum and maximum protocol versions if necessary */
980  strlen(conn->ssl_min_protocol_version) != 0)
981  {
982  int ssl_min_ver;
983 
985 
986  if (ssl_min_ver == -1)
987  {
989  libpq_gettext("invalid value \"%s\" for minimum SSL protocol version\n"),
991  SSL_CTX_free(SSL_context);
992  return -1;
993  }
994 
995  if (!SSL_CTX_set_min_proto_version(SSL_context, ssl_min_ver))
996  {
997  char *err = SSLerrmessage(ERR_get_error());
998 
1000  libpq_gettext("could not set minimum SSL protocol version: %s\n"),
1001  err);
1002  SSLerrfree(err);
1003  SSL_CTX_free(SSL_context);
1004  return -1;
1005  }
1006  }
1007 
1009  strlen(conn->ssl_max_protocol_version) != 0)
1010  {
1011  int ssl_max_ver;
1012 
1014 
1015  if (ssl_max_ver == -1)
1016  {
1018  libpq_gettext("invalid value \"%s\" for maximum SSL protocol version\n"),
1020  SSL_CTX_free(SSL_context);
1021  return -1;
1022  }
1023 
1024  if (!SSL_CTX_set_max_proto_version(SSL_context, ssl_max_ver))
1025  {
1026  char *err = SSLerrmessage(ERR_get_error());
1027 
1029  libpq_gettext("could not set maximum SSL protocol version: %s\n"),
1030  err);
1031  SSLerrfree(err);
1032  SSL_CTX_free(SSL_context);
1033  return -1;
1034  }
1035  }
1036 
1037  /*
1038  * Disable OpenSSL's moving-write-buffer sanity check, because it causes
1039  * unnecessary failures in nonblocking send cases.
1040  */
1041  SSL_CTX_set_mode(SSL_context, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
1042 
1043  /*
1044  * If the root cert file exists, load it so we can perform certificate
1045  * verification. If sslmode is "verify-full" we will also do further
1046  * verification after the connection has been completed.
1047  */
1048  if (conn->sslrootcert && strlen(conn->sslrootcert) > 0)
1049  strlcpy(fnbuf, conn->sslrootcert, sizeof(fnbuf));
1050  else if (have_homedir)
1051  snprintf(fnbuf, sizeof(fnbuf), "%s/%s", homedir, ROOT_CERT_FILE);
1052  else
1053  fnbuf[0] = '\0';
1054 
1055  if (fnbuf[0] != '\0' &&
1056  stat(fnbuf, &buf) == 0)
1057  {
1058  X509_STORE *cvstore;
1059 
1060  if (SSL_CTX_load_verify_locations(SSL_context, fnbuf, NULL) != 1)
1061  {
1062  char *err = SSLerrmessage(ERR_get_error());
1063 
1065  libpq_gettext("could not read root certificate file \"%s\": %s\n"),
1066  fnbuf, err);
1067  SSLerrfree(err);
1068  SSL_CTX_free(SSL_context);
1069  return -1;
1070  }
1071 
1072  if ((cvstore = SSL_CTX_get_cert_store(SSL_context)) != NULL)
1073  {
1074  char *fname = NULL;
1075  char *dname = NULL;
1076 
1077  if (conn->sslcrl && strlen(conn->sslcrl) > 0)
1078  fname = conn->sslcrl;
1079  if (conn->sslcrldir && strlen(conn->sslcrldir) > 0)
1080  dname = conn->sslcrldir;
1081 
1082  /* defaults to use the default CRL file */
1083  if (!fname && !dname && have_homedir)
1084  {
1085  snprintf(fnbuf, sizeof(fnbuf), "%s/%s", homedir, ROOT_CRL_FILE);
1086  fname = fnbuf;
1087  }
1088 
1089  /* Set the flags to check against the complete CRL chain */
1090  if ((fname || dname) &&
1091  X509_STORE_load_locations(cvstore, fname, dname) == 1)
1092  {
1093  X509_STORE_set_flags(cvstore,
1094  X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
1095  }
1096 
1097  /* if not found, silently ignore; we do not require CRL */
1098  ERR_clear_error();
1099  }
1100  have_rootcert = true;
1101  }
1102  else
1103  {
1104  /*
1105  * stat() failed; assume root file doesn't exist. If sslmode is
1106  * verify-ca or verify-full, this is an error. Otherwise, continue
1107  * without performing any server cert verification.
1108  */
1109  if (conn->sslmode[0] == 'v') /* "verify-ca" or "verify-full" */
1110  {
1111  /*
1112  * The only way to reach here with an empty filename is if
1113  * pqGetHomeDirectory failed. That's a sufficiently unusual case
1114  * that it seems worth having a specialized error message for it.
1115  */
1116  if (fnbuf[0] == '\0')
1118  libpq_gettext("could not get home directory to locate root certificate file\n"
1119  "Either provide the file or change sslmode to disable server certificate verification.\n"));
1120  else
1122  libpq_gettext("root certificate file \"%s\" does not exist\n"
1123  "Either provide the file or change sslmode to disable server certificate verification.\n"), fnbuf);
1124  SSL_CTX_free(SSL_context);
1125  return -1;
1126  }
1127  have_rootcert = false;
1128  }
1129 
1130  /* Read the client certificate file */
1131  if (conn->sslcert && strlen(conn->sslcert) > 0)
1132  strlcpy(fnbuf, conn->sslcert, sizeof(fnbuf));
1133  else if (have_homedir)
1134  snprintf(fnbuf, sizeof(fnbuf), "%s/%s", homedir, USER_CERT_FILE);
1135  else
1136  fnbuf[0] = '\0';
1137 
1138  if (fnbuf[0] == '\0')
1139  {
1140  /* no home directory, proceed without a client cert */
1141  have_cert = false;
1142  }
1143  else if (stat(fnbuf, &buf) != 0)
1144  {
1145  /*
1146  * If file is not present, just go on without a client cert; server
1147  * might or might not accept the connection. Any other error,
1148  * however, is grounds for complaint.
1149  */
1150  if (errno != ENOENT && errno != ENOTDIR)
1151  {
1153  libpq_gettext("could not open certificate file \"%s\": %s\n"),
1154  fnbuf, strerror_r(errno, sebuf, sizeof(sebuf)));
1155  SSL_CTX_free(SSL_context);
1156  return -1;
1157  }
1158  have_cert = false;
1159  }
1160  else
1161  {
1162  /*
1163  * Cert file exists, so load it. Since OpenSSL doesn't provide the
1164  * equivalent of "SSL_use_certificate_chain_file", we have to load it
1165  * into the SSL context, rather than the SSL object.
1166  */
1167  if (SSL_CTX_use_certificate_chain_file(SSL_context, fnbuf) != 1)
1168  {
1169  char *err = SSLerrmessage(ERR_get_error());
1170 
1172  libpq_gettext("could not read certificate file \"%s\": %s\n"),
1173  fnbuf, err);
1174  SSLerrfree(err);
1175  SSL_CTX_free(SSL_context);
1176  return -1;
1177  }
1178 
1179  /* need to load the associated private key, too */
1180  have_cert = true;
1181  }
1182 
1183  /*
1184  * The SSL context is now loaded with the correct root and client
1185  * certificates. Create a connection-specific SSL object. The private key
1186  * is loaded directly into the SSL object. (We could load the private key
1187  * into the context, too, but we have done it this way historically, and
1188  * it doesn't really matter.)
1189  */
1190  if (!(conn->ssl = SSL_new(SSL_context)) ||
1191  !SSL_set_app_data(conn->ssl, conn) ||
1193  {
1194  char *err = SSLerrmessage(ERR_get_error());
1195 
1197  libpq_gettext("could not establish SSL connection: %s\n"),
1198  err);
1199  SSLerrfree(err);
1200  SSL_CTX_free(SSL_context);
1201  return -1;
1202  }
1203  conn->ssl_in_use = true;
1204 
1205  /*
1206  * SSL contexts are reference counted by OpenSSL. We can free it as soon
1207  * as we have created the SSL object, and it will stick around for as long
1208  * as it's actually needed.
1209  */
1210  SSL_CTX_free(SSL_context);
1211  SSL_context = NULL;
1212 
1213  /*
1214  * Set Server Name Indication (SNI), if enabled by connection parameters.
1215  * Per RFC 6066, do not set it if the host is a literal IP address (IPv4
1216  * or IPv6).
1217  */
1218  if (conn->sslsni && conn->sslsni[0] == '1')
1219  {
1220  const char *host = conn->connhost[conn->whichhost].host;
1221 
1222  if (host && host[0] &&
1223  !(strspn(host, "0123456789.") == strlen(host) ||
1224  strchr(host, ':')))
1225  {
1226  if (SSL_set_tlsext_host_name(conn->ssl, host) != 1)
1227  {
1228  char *err = SSLerrmessage(ERR_get_error());
1229 
1231  libpq_gettext("could not set SSL Server Name Indication (SNI): %s\n"),
1232  err);
1233  SSLerrfree(err);
1234  return -1;
1235  }
1236  }
1237  }
1238 
1239  /*
1240  * Read the SSL key. If a key is specified, treat it as an engine:key
1241  * combination if there is colon present - we don't support files with
1242  * colon in the name. The exception is if the second character is a colon,
1243  * in which case it can be a Windows filename with drive specification.
1244  */
1245  if (have_cert && conn->sslkey && strlen(conn->sslkey) > 0)
1246  {
1247 #ifdef USE_SSL_ENGINE
1248  if (strchr(conn->sslkey, ':')
1249 #ifdef WIN32
1250  && conn->sslkey[1] != ':'
1251 #endif
1252  )
1253  {
1254  /* Colon, but not in second character, treat as engine:key */
1255  char *engine_str = strdup(conn->sslkey);
1256  char *engine_colon;
1257 
1258  if (engine_str == NULL)
1259  {
1261  libpq_gettext("out of memory\n"));
1262  return -1;
1263  }
1264 
1265  /* cannot return NULL because we already checked before strdup */
1266  engine_colon = strchr(engine_str, ':');
1267 
1268  *engine_colon = '\0'; /* engine_str now has engine name */
1269  engine_colon++; /* engine_colon now has key name */
1270 
1271  conn->engine = ENGINE_by_id(engine_str);
1272  if (conn->engine == NULL)
1273  {
1274  char *err = SSLerrmessage(ERR_get_error());
1275 
1277  libpq_gettext("could not load SSL engine \"%s\": %s\n"),
1278  engine_str, err);
1279  SSLerrfree(err);
1280  free(engine_str);
1281  return -1;
1282  }
1283 
1284  if (ENGINE_init(conn->engine) == 0)
1285  {
1286  char *err = SSLerrmessage(ERR_get_error());
1287 
1289  libpq_gettext("could not initialize SSL engine \"%s\": %s\n"),
1290  engine_str, err);
1291  SSLerrfree(err);
1292  ENGINE_free(conn->engine);
1293  conn->engine = NULL;
1294  free(engine_str);
1295  return -1;
1296  }
1297 
1298  pkey = ENGINE_load_private_key(conn->engine, engine_colon,
1299  NULL, NULL);
1300  if (pkey == NULL)
1301  {
1302  char *err = SSLerrmessage(ERR_get_error());
1303 
1305  libpq_gettext("could not read private SSL key \"%s\" from engine \"%s\": %s\n"),
1306  engine_colon, engine_str, err);
1307  SSLerrfree(err);
1308  ENGINE_finish(conn->engine);
1309  ENGINE_free(conn->engine);
1310  conn->engine = NULL;
1311  free(engine_str);
1312  return -1;
1313  }
1314  if (SSL_use_PrivateKey(conn->ssl, pkey) != 1)
1315  {
1316  char *err = SSLerrmessage(ERR_get_error());
1317 
1319  libpq_gettext("could not load private SSL key \"%s\" from engine \"%s\": %s\n"),
1320  engine_colon, engine_str, err);
1321  SSLerrfree(err);
1322  ENGINE_finish(conn->engine);
1323  ENGINE_free(conn->engine);
1324  conn->engine = NULL;
1325  free(engine_str);
1326  return -1;
1327  }
1328 
1329  free(engine_str);
1330 
1331  fnbuf[0] = '\0'; /* indicate we're not going to load from a
1332  * file */
1333  }
1334  else
1335 #endif /* USE_SSL_ENGINE */
1336  {
1337  /* PGSSLKEY is not an engine, treat it as a filename */
1338  strlcpy(fnbuf, conn->sslkey, sizeof(fnbuf));
1339  }
1340  }
1341  else if (have_homedir)
1342  {
1343  /* No PGSSLKEY specified, load default file */
1344  snprintf(fnbuf, sizeof(fnbuf), "%s/%s", homedir, USER_KEY_FILE);
1345  }
1346  else
1347  fnbuf[0] = '\0';
1348 
1349  if (have_cert && fnbuf[0] != '\0')
1350  {
1351  /* read the client key from file */
1352 
1353  if (stat(fnbuf, &buf) != 0)
1354  {
1355  if (errno == ENOENT)
1357  libpq_gettext("certificate present, but not private key file \"%s\"\n"),
1358  fnbuf);
1359  else
1361  libpq_gettext("could not stat private key file \"%s\": %m\n"),
1362  fnbuf);
1363  return -1;
1364  }
1365 
1366  /* Key file must be a regular file */
1367  if (!S_ISREG(buf.st_mode))
1368  {
1370  libpq_gettext("private key file \"%s\" is not a regular file\n"),
1371  fnbuf);
1372  return -1;
1373  }
1374 
1375  /*
1376  * Refuse to load world-readable key files. We accept root-owned
1377  * files with mode 0640 or less, so that we can access system-wide
1378  * certificates if we have a supplementary group membership that
1379  * allows us to read 'em. For files with non-root ownership, require
1380  * mode 0600 or less. We need not check the file's ownership exactly;
1381  * if we're able to read it despite it having such restrictive
1382  * permissions, it must have the right ownership.
1383  *
1384  * Note: be very careful about tightening these rules. Some people
1385  * expect, for example, that a client process running as root should
1386  * be able to use a non-root-owned key file.
1387  *
1388  * Note that roughly similar checks are performed in
1389  * src/backend/libpq/be-secure-common.c so any changes here may need
1390  * to be made there as well. However, this code caters for the case
1391  * of current user == root, while that code does not.
1392  *
1393  * Ideally we would do similar permissions checks on Windows, but it
1394  * is not clear how that would work since Unix-style permissions may
1395  * not be available.
1396  */
1397 #if !defined(WIN32) && !defined(__CYGWIN__)
1398  if (buf.st_uid == 0 ?
1399  buf.st_mode & (S_IWGRP | S_IXGRP | S_IRWXO) :
1400  buf.st_mode & (S_IRWXG | S_IRWXO))
1401  {
1403  libpq_gettext("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\n"),
1404  fnbuf);
1405  return -1;
1406  }
1407 #endif
1408 
1409  if (SSL_use_PrivateKey_file(conn->ssl, fnbuf, SSL_FILETYPE_PEM) != 1)
1410  {
1411  char *err = SSLerrmessage(ERR_get_error());
1412 
1413  /*
1414  * We'll try to load the file in DER (binary ASN.1) format, and if
1415  * that fails too, report the original error. This could mask
1416  * issues where there's something wrong with a DER-format cert,
1417  * but we'd have to duplicate openssl's format detection to be
1418  * smarter than this. We can't just probe for a leading -----BEGIN
1419  * because PEM can have leading non-matching lines and blanks.
1420  * OpenSSL doesn't expose its get_name(...) and its PEM routines
1421  * don't differentiate between failure modes in enough detail to
1422  * let us tell the difference between "not PEM, try DER" and
1423  * "wrong password".
1424  */
1425  if (SSL_use_PrivateKey_file(conn->ssl, fnbuf, SSL_FILETYPE_ASN1) != 1)
1426  {
1428  libpq_gettext("could not load private key file \"%s\": %s\n"),
1429  fnbuf, err);
1430  SSLerrfree(err);
1431  return -1;
1432  }
1433 
1434  SSLerrfree(err);
1435  }
1436  }
1437 
1438  /* verify that the cert and key go together */
1439  if (have_cert &&
1440  SSL_check_private_key(conn->ssl) != 1)
1441  {
1442  char *err = SSLerrmessage(ERR_get_error());
1443 
1445  libpq_gettext("certificate does not match private key file \"%s\": %s\n"),
1446  fnbuf, err);
1447  SSLerrfree(err);
1448  return -1;
1449  }
1450 
1451  /*
1452  * If a root cert was loaded, also set our certificate verification
1453  * callback.
1454  */
1455  if (have_rootcert)
1456  SSL_set_verify(conn->ssl, SSL_VERIFY_PEER, verify_cb);
1457 
1458  /*
1459  * Set compression option if necessary.
1460  */
1461  if (conn->sslcompression && conn->sslcompression[0] == '0')
1462  SSL_set_options(conn->ssl, SSL_OP_NO_COMPRESSION);
1463  else
1464  SSL_clear_options(conn->ssl, SSL_OP_NO_COMPRESSION);
1465 
1466  return 0;
1467 }
static SSL_CTX * SSL_context
bool pqGetHomeDirectory(char *buf, int bufsize)
Definition: fe-connect.c:7406
static int ssl_protocol_version_to_openssl(const char *protocol)
static int my_SSL_set_fd(PGconn *conn, int fd)
static void SSLerrfree(char *buf)
static PQsslKeyPassHook_OpenSSL_type PQsslKeyPassHook
static int verify_cb(int ok, X509_STORE_CTX *ctx)
static int PQssl_passwd_cb(char *buf, int size, int rwflag, void *userdata)
static char * SSLerrmessage(unsigned long ecode)
#define free(a)
Definition: header.h:65
#define libpq_gettext(x)
Definition: libpq-int.h:878
#define MAXPGPATH
static char * buf
Definition: pg_test_fsync.c:67
#define PG_STRERROR_R_BUFLEN
Definition: port.h:243
#define snprintf
Definition: port.h:225
#define strerror_r
Definition: port.h:242
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
int SSL_CTX_set_max_proto_version(SSL_CTX *ctx, int version)
int SSL_CTX_set_min_proto_version(SSL_CTX *ctx, int version)
PGconn * conn
Definition: streamutil.c:54
char * host
Definition: libpq-int.h:335
char * sslrootcert
Definition: libpq-int.h:384
char * sslcompression
Definition: libpq-int.h:380
pgsocket sock
Definition: libpq-int.h:444
char * sslcrldir
Definition: libpq-int.h:386
char * sslcrl
Definition: libpq-int.h:385
char * ssl_max_protocol_version
Definition: libpq-int.h:394
char * sslcert
Definition: libpq-int.h:382
char * sslpassword
Definition: libpq-int.h:383
char * sslmode
Definition: libpq-int.h:379
char * ssl_min_protocol_version
Definition: libpq-int.h:393
PQExpBufferData errorMessage
Definition: libpq-int.h:585
char * sslkey
Definition: libpq-int.h:381
int whichhost
Definition: libpq-int.h:426
char * sslsni
Definition: libpq-int.h:387
pg_conn_host * connhost
Definition: libpq-int.h:427
bool ssl_in_use
Definition: libpq-int.h:517
#define S_IXGRP
Definition: win32_port.h:306
#define stat
Definition: win32_port.h:283
#define S_IRWXG
Definition: win32_port.h:309
#define S_IRWXO
Definition: win32_port.h:321
#define S_ISREG(m)
Definition: win32_port.h:327
#define S_IWGRP
Definition: win32_port.h:303

References appendPQExpBuffer(), appendPQExpBufferStr(), buf, conn, pg_conn::connhost, pg_conn::errorMessage, free, pg_conn_host::host, libpq_gettext, MAXPGPATH, my_SSL_set_fd(), PG_STRERROR_R_BUFLEN, pqGetHomeDirectory(), PQssl_passwd_cb(), PQsslKeyPassHook, S_IRWXG, S_IRWXO, S_ISREG, S_IWGRP, S_IXGRP, snprintf, pg_conn::sock, SSL_context, SSL_CTX_set_max_proto_version(), SSL_CTX_set_min_proto_version(), pg_conn::ssl_in_use, pg_conn::ssl_max_protocol_version, pg_conn::ssl_min_protocol_version, ssl_protocol_version_to_openssl(), pg_conn::sslcert, pg_conn::sslcompression, pg_conn::sslcrl, pg_conn::sslcrldir, SSLerrfree(), SSLerrmessage(), pg_conn::sslkey, pg_conn::sslmode, pg_conn::sslpassword, pg_conn::sslrootcert, pg_conn::sslsni, stat, strerror_r, strlcpy(), verify_cb(), and pg_conn::whichhost.

Referenced by pgtls_open_client().

◆ is_ip_address()

static bool is_ip_address ( const char *  host)
static

Definition at line 551 of file fe-secure-openssl.c.

552 {
553  struct in_addr dummy4;
554 #ifdef HAVE_INET_PTON
555  struct in6_addr dummy6;
556 #endif
557 
558  return inet_aton(host, &dummy4)
559 #ifdef HAVE_INET_PTON
560  || (inet_pton(AF_INET6, host, &dummy6) == 1)
561 #endif
562  ;
563 }
int inet_aton(const char *cp, struct in_addr *addr)
Definition: inet_aton.c:56

References inet_aton().

Referenced by pgtls_verify_peer_name_matches_certificate_guts().

◆ my_BIO_s_socket()

static BIO_METHOD * my_BIO_s_socket ( void  )
static

Definition at line 1859 of file fe-secure-openssl.c.

1860 {
1861  if (!my_bio_methods)
1862  {
1863  BIO_METHOD *biom = (BIO_METHOD *) BIO_s_socket();
1864 #ifdef HAVE_BIO_METH_NEW
1865  int my_bio_index;
1866 
1867  my_bio_index = BIO_get_new_index();
1868  if (my_bio_index == -1)
1869  return NULL;
1870  my_bio_index |= (BIO_TYPE_DESCRIPTOR | BIO_TYPE_SOURCE_SINK);
1871  my_bio_methods = BIO_meth_new(my_bio_index, "libpq socket");
1872  if (!my_bio_methods)
1873  return NULL;
1874 
1875  /*
1876  * As of this writing, these functions never fail. But check anyway,
1877  * like OpenSSL's own examples do.
1878  */
1879  if (!BIO_meth_set_write(my_bio_methods, my_sock_write) ||
1880  !BIO_meth_set_read(my_bio_methods, my_sock_read) ||
1881  !BIO_meth_set_gets(my_bio_methods, BIO_meth_get_gets(biom)) ||
1882  !BIO_meth_set_puts(my_bio_methods, BIO_meth_get_puts(biom)) ||
1883  !BIO_meth_set_ctrl(my_bio_methods, BIO_meth_get_ctrl(biom)) ||
1884  !BIO_meth_set_create(my_bio_methods, BIO_meth_get_create(biom)) ||
1885  !BIO_meth_set_destroy(my_bio_methods, BIO_meth_get_destroy(biom)) ||
1886  !BIO_meth_set_callback_ctrl(my_bio_methods, BIO_meth_get_callback_ctrl(biom)))
1887  {
1888  BIO_meth_free(my_bio_methods);
1889  my_bio_methods = NULL;
1890  return NULL;
1891  }
1892 #else
1893  my_bio_methods = malloc(sizeof(BIO_METHOD));
1894  if (!my_bio_methods)
1895  return NULL;
1896  memcpy(my_bio_methods, biom, sizeof(BIO_METHOD));
1897  my_bio_methods->bread = my_sock_read;
1898  my_bio_methods->bwrite = my_sock_write;
1899 #endif
1900  }
1901  return my_bio_methods;
1902 }
static BIO_METHOD * my_bio_methods
static int my_sock_write(BIO *h, const char *buf, int size)
static int my_sock_read(BIO *h, char *buf, int size)
#define malloc(a)
Definition: header.h:50

References malloc, my_bio_methods, my_sock_read(), and my_sock_write().

Referenced by my_SSL_set_fd().

◆ my_sock_read()

static int my_sock_read ( BIO *  h,
char *  buf,
int  size 
)
static

Definition at line 1799 of file fe-secure-openssl.c.

1800 {
1801  int res;
1802 
1803  res = pqsecure_raw_read((PGconn *) BIO_get_data(h), buf, size);
1804  BIO_clear_retry_flags(h);
1805  if (res < 0)
1806  {
1807  /* If we were interrupted, tell caller to retry */
1808  switch (SOCK_ERRNO)
1809  {
1810 #ifdef EAGAIN
1811  case EAGAIN:
1812 #endif
1813 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
1814  case EWOULDBLOCK:
1815 #endif
1816  case EINTR:
1817  BIO_set_retry_read(h);
1818  break;
1819 
1820  default:
1821  break;
1822  }
1823  }
1824 
1825  return res;
1826 }
#define BIO_get_data(bio)
ssize_t pqsecure_raw_read(PGconn *conn, void *ptr, size_t len)
Definition: fe-secure.c:232
#define SOCK_ERRNO
Definition: libpq-int.h:891
#define EINTR
Definition: win32_port.h:351
#define EWOULDBLOCK
Definition: win32_port.h:357
#define EAGAIN
Definition: win32_port.h:349

References BIO_get_data, buf, EAGAIN, EINTR, EWOULDBLOCK, pqsecure_raw_read(), res, and SOCK_ERRNO.

Referenced by my_BIO_s_socket().

◆ my_sock_write()

static int my_sock_write ( BIO *  h,
const char *  buf,
int  size 
)
static

Definition at line 1829 of file fe-secure-openssl.c.

1830 {
1831  int res;
1832 
1833  res = pqsecure_raw_write((PGconn *) BIO_get_data(h), buf, size);
1834  BIO_clear_retry_flags(h);
1835  if (res < 0)
1836  {
1837  /* If we were interrupted, tell caller to retry */
1838  switch (SOCK_ERRNO)
1839  {
1840 #ifdef EAGAIN
1841  case EAGAIN:
1842 #endif
1843 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
1844  case EWOULDBLOCK:
1845 #endif
1846  case EINTR:
1847  BIO_set_retry_write(h);
1848  break;
1849 
1850  default:
1851  break;
1852  }
1853  }
1854 
1855  return res;
1856 }
ssize_t pqsecure_raw_write(PGconn *conn, const void *ptr, size_t len)
Definition: fe-secure.c:350

References BIO_get_data, buf, EAGAIN, EINTR, EWOULDBLOCK, pqsecure_raw_write(), res, and SOCK_ERRNO.

Referenced by my_BIO_s_socket().

◆ my_SSL_set_fd()

static int my_SSL_set_fd ( PGconn conn,
int  fd 
)
static

Definition at line 1906 of file fe-secure-openssl.c.

1907 {
1908  int ret = 0;
1909  BIO *bio;
1910  BIO_METHOD *bio_method;
1911 
1912  bio_method = my_BIO_s_socket();
1913  if (bio_method == NULL)
1914  {
1915  SSLerr(SSL_F_SSL_SET_FD, ERR_R_BUF_LIB);
1916  goto err;
1917  }
1918  bio = BIO_new(bio_method);
1919  if (bio == NULL)
1920  {
1921  SSLerr(SSL_F_SSL_SET_FD, ERR_R_BUF_LIB);
1922  goto err;
1923  }
1924  BIO_set_data(bio, conn);
1925 
1926  SSL_set_bio(conn->ssl, bio, bio);
1927  BIO_set_fd(bio, fd, BIO_NOCLOSE);
1928  ret = 1;
1929 err:
1930  return ret;
1931 }
static BIO_METHOD * my_BIO_s_socket(void)
#define BIO_set_data(bio, data)
static int fd(const char *x, int i)
Definition: preproc-init.c:105

References BIO_set_data, conn, fd(), and my_BIO_s_socket().

Referenced by initialize_SSL().

◆ open_client_SSL()

static PostgresPollingStatusType open_client_SSL ( PGconn conn)
static

Definition at line 1473 of file fe-secure-openssl.c.

1474 {
1475  int r;
1476 
1477  ERR_clear_error();
1478  r = SSL_connect(conn->ssl);
1479  if (r <= 0)
1480  {
1481  int err = SSL_get_error(conn->ssl, r);
1482  unsigned long ecode;
1483 
1484  ecode = ERR_get_error();
1485  switch (err)
1486  {
1487  case SSL_ERROR_WANT_READ:
1488  return PGRES_POLLING_READING;
1489 
1490  case SSL_ERROR_WANT_WRITE:
1491  return PGRES_POLLING_WRITING;
1492 
1493  case SSL_ERROR_SYSCALL:
1494  {
1495  char sebuf[PG_STRERROR_R_BUFLEN];
1496 
1497  if (r == -1)
1499  libpq_gettext("SSL SYSCALL error: %s\n"),
1500  SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
1501  else
1503  libpq_gettext("SSL SYSCALL error: EOF detected\n"));
1504  pgtls_close(conn);
1505  return PGRES_POLLING_FAILED;
1506  }
1507  case SSL_ERROR_SSL:
1508  {
1509  char *err = SSLerrmessage(ecode);
1510 
1512  libpq_gettext("SSL error: %s\n"),
1513  err);
1514  SSLerrfree(err);
1515  switch (ERR_GET_REASON(ecode))
1516  {
1517  /*
1518  * UNSUPPORTED_PROTOCOL, WRONG_VERSION_NUMBER, and
1519  * TLSV1_ALERT_PROTOCOL_VERSION have been observed
1520  * when trying to communicate with an old OpenSSL
1521  * library, or when the client and server specify
1522  * disjoint protocol ranges.
1523  * NO_PROTOCOLS_AVAILABLE occurs if there's a
1524  * local misconfiguration (which can happen
1525  * despite our checks, if openssl.cnf injects a
1526  * limit we didn't account for). It's not very
1527  * clear what would make OpenSSL return the other
1528  * codes listed here, but a hint about protocol
1529  * versions seems like it's appropriate for all.
1530  */
1531  case SSL_R_NO_PROTOCOLS_AVAILABLE:
1532  case SSL_R_UNSUPPORTED_PROTOCOL:
1533  case SSL_R_BAD_PROTOCOL_VERSION_NUMBER:
1534  case SSL_R_UNKNOWN_PROTOCOL:
1535  case SSL_R_UNKNOWN_SSL_VERSION:
1536  case SSL_R_UNSUPPORTED_SSL_VERSION:
1537  case SSL_R_WRONG_SSL_VERSION:
1538  case SSL_R_WRONG_VERSION_NUMBER:
1539  case SSL_R_TLSV1_ALERT_PROTOCOL_VERSION:
1540 #ifdef SSL_R_VERSION_TOO_HIGH
1541  case SSL_R_VERSION_TOO_HIGH:
1542  case SSL_R_VERSION_TOO_LOW:
1543 #endif
1545  libpq_gettext("This may indicate that the server does not support any SSL protocol version between %s and %s.\n"),
1548  MIN_OPENSSL_TLS_VERSION,
1551  MAX_OPENSSL_TLS_VERSION);
1552  break;
1553  default:
1554  break;
1555  }
1556  pgtls_close(conn);
1557  return PGRES_POLLING_FAILED;
1558  }
1559 
1560  default:
1562  libpq_gettext("unrecognized SSL error code: %d\n"),
1563  err);
1564  pgtls_close(conn);
1565  return PGRES_POLLING_FAILED;
1566  }
1567  }
1568 
1569  /*
1570  * We already checked the server certificate in initialize_SSL() using
1571  * SSL_CTX_set_verify(), if root.crt exists.
1572  */
1573 
1574  /* get server certificate */
1575  conn->peer = SSL_get_peer_certificate(conn->ssl);
1576  if (conn->peer == NULL)
1577  {
1578  char *err = SSLerrmessage(ERR_get_error());
1579 
1581  libpq_gettext("certificate could not be obtained: %s\n"),
1582  err);
1583  SSLerrfree(err);
1584  pgtls_close(conn);
1585  return PGRES_POLLING_FAILED;
1586  }
1587 
1589  {
1590  pgtls_close(conn);
1591  return PGRES_POLLING_FAILED;
1592  }
1593 
1594  /* SSL handshake is complete */
1595  return PGRES_POLLING_OK;
1596 }
bool pq_verify_peer_name_matches_certificate(PGconn *conn)
void pgtls_close(PGconn *conn)
@ PGRES_POLLING_OK
Definition: libpq-fe.h:89
@ PGRES_POLLING_READING
Definition: libpq-fe.h:87
@ PGRES_POLLING_WRITING
Definition: libpq-fe.h:88
@ PGRES_POLLING_FAILED
Definition: libpq-fe.h:86
#define SOCK_STRERROR
Definition: libpq-int.h:892

References appendPQExpBuffer(), appendPQExpBufferStr(), conn, pg_conn::errorMessage, libpq_gettext, PG_STRERROR_R_BUFLEN, PGRES_POLLING_FAILED, PGRES_POLLING_OK, PGRES_POLLING_READING, PGRES_POLLING_WRITING, pgtls_close(), pq_verify_peer_name_matches_certificate(), SOCK_ERRNO, SOCK_STRERROR, pg_conn::ssl_max_protocol_version, pg_conn::ssl_min_protocol_version, SSLerrfree(), and SSLerrmessage().

Referenced by pgtls_open_client().

◆ openssl_verify_peer_name_matches_certificate_ip()

static int openssl_verify_peer_name_matches_certificate_ip ( PGconn conn,
ASN1_OCTET_STRING *  addr_entry,
char **  store_name 
)
static

Definition at line 521 of file fe-secure-openssl.c.

524 {
525  int len;
526  const unsigned char *addrdata;
527 
528  /* Should not happen... */
529  if (addr_entry == NULL)
530  {
532  libpq_gettext("SSL certificate's address entry is missing\n"));
533  return -1;
534  }
535 
536  /*
537  * GEN_IPADD is an OCTET STRING containing an IP address in network byte
538  * order.
539  */
540 #ifdef HAVE_ASN1_STRING_GET0_DATA
541  addrdata = ASN1_STRING_get0_data(addr_entry);
542 #else
543  addrdata = ASN1_STRING_data(addr_entry);
544 #endif
545  len = ASN1_STRING_length(addr_entry);
546 
547  return pq_verify_peer_name_matches_certificate_ip(conn, addrdata, len, store_name);
548 }
int pq_verify_peer_name_matches_certificate_ip(PGconn *conn, const unsigned char *ipdata, size_t iplen, char **store_name)
const void size_t len

References appendPQExpBufferStr(), conn, pg_conn::errorMessage, len, libpq_gettext, and pq_verify_peer_name_matches_certificate_ip().

Referenced by pgtls_verify_peer_name_matches_certificate_guts().

◆ openssl_verify_peer_name_matches_certificate_name()

static int openssl_verify_peer_name_matches_certificate_name ( PGconn conn,
ASN1_STRING *  name,
char **  store_name 
)
static

Definition at line 487 of file fe-secure-openssl.c.

489 {
490  int len;
491  const unsigned char *namedata;
492 
493  /* Should not happen... */
494  if (name_entry == NULL)
495  {
497  libpq_gettext("SSL certificate's name entry is missing\n"));
498  return -1;
499  }
500 
501  /*
502  * GEN_DNS can be only IA5String, equivalent to US ASCII.
503  */
504 #ifdef HAVE_ASN1_STRING_GET0_DATA
505  namedata = ASN1_STRING_get0_data(name_entry);
506 #else
507  namedata = ASN1_STRING_data(name_entry);
508 #endif
509  len = ASN1_STRING_length(name_entry);
510 
511  /* OK to cast from unsigned to plain char, since it's all ASCII. */
512  return pq_verify_peer_name_matches_certificate_name(conn, (const char *) namedata, len, store_name);
513 }
int pq_verify_peer_name_matches_certificate_name(PGconn *conn, const char *namedata, size_t namelen, char **store_name)

References appendPQExpBufferStr(), conn, pg_conn::errorMessage, len, libpq_gettext, and pq_verify_peer_name_matches_certificate_name().

Referenced by pgtls_verify_peer_name_matches_certificate_guts().

◆ pgtls_close()

void pgtls_close ( PGconn conn)

Definition at line 1599 of file fe-secure-openssl.c.

1600 {
1601  bool destroy_needed = false;
1602 
1603  if (conn->ssl_in_use)
1604  {
1605  if (conn->ssl)
1606  {
1607  /*
1608  * We can't destroy everything SSL-related here due to the
1609  * possible later calls to OpenSSL routines which may need our
1610  * thread callbacks, so set a flag here and check at the end.
1611  */
1612 
1613  SSL_shutdown(conn->ssl);
1614  SSL_free(conn->ssl);
1615  conn->ssl = NULL;
1616  conn->ssl_in_use = false;
1617 
1618  destroy_needed = true;
1619  }
1620 
1621  if (conn->peer)
1622  {
1623  X509_free(conn->peer);
1624  conn->peer = NULL;
1625  }
1626 
1627 #ifdef USE_SSL_ENGINE
1628  if (conn->engine)
1629  {
1630  ENGINE_finish(conn->engine);
1631  ENGINE_free(conn->engine);
1632  conn->engine = NULL;
1633  }
1634 #endif
1635  }
1636  else
1637  {
1638  /*
1639  * In the non-SSL case, just remove the crypto callbacks if the
1640  * connection has then loaded. This code path has no dependency on
1641  * any pending SSL calls.
1642  */
1643  if (conn->crypto_loaded)
1644  destroy_needed = true;
1645  }
1646 
1647  /*
1648  * This will remove our crypto locking hooks if this is the last
1649  * connection using libcrypto which means we must wait to call it until
1650  * after all the potential SSL calls have been made, otherwise we can end
1651  * up with a race condition and possible deadlocks.
1652  *
1653  * See comments above destroy_ssl_system().
1654  */
1655  if (destroy_needed)
1656  {
1658  conn->crypto_loaded = false;
1659  }
1660 }
static void destroy_ssl_system(void)

References conn, destroy_ssl_system(), and pg_conn::ssl_in_use.

Referenced by open_client_SSL(), pgtls_open_client(), and pqsecure_close().

◆ pgtls_init()

int pgtls_init ( PGconn conn,
bool  do_ssl,
bool  do_crypto 
)

Definition at line 766 of file fe-secure-openssl.c.

767 {
768 #ifdef ENABLE_THREAD_SAFETY
769 #ifdef WIN32
770  /* Also see similar code in fe-connect.c, default_threadlock() */
771  if (ssl_config_mutex == NULL)
772  {
773  while (InterlockedExchange(&win32_ssl_create_mutex, 1) == 1)
774  /* loop, another thread own the lock */ ;
775  if (ssl_config_mutex == NULL)
776  {
777  if (pthread_mutex_init(&ssl_config_mutex, NULL))
778  return -1;
779  }
780  InterlockedExchange(&win32_ssl_create_mutex, 0);
781  }
782 #endif
783  if (pthread_mutex_lock(&ssl_config_mutex))
784  return -1;
785 
786 #ifdef HAVE_CRYPTO_LOCK
787  if (pq_init_crypto_lib)
788  {
789  /*
790  * If necessary, set up an array to hold locks for libcrypto.
791  * libcrypto will tell us how big to make this array.
792  */
793  if (pq_lockarray == NULL)
794  {
795  int i;
796 
797  pq_lockarray = malloc(sizeof(pthread_mutex_t) * CRYPTO_num_locks());
798  if (!pq_lockarray)
799  {
800  pthread_mutex_unlock(&ssl_config_mutex);
801  return -1;
802  }
803  for (i = 0; i < CRYPTO_num_locks(); i++)
804  {
805  if (pthread_mutex_init(&pq_lockarray[i], NULL))
806  {
807  free(pq_lockarray);
808  pq_lockarray = NULL;
809  pthread_mutex_unlock(&ssl_config_mutex);
810  return -1;
811  }
812  }
813  }
814 
815  if (do_crypto && !conn->crypto_loaded)
816  {
817  if (crypto_open_connections++ == 0)
818  {
819  /*
820  * These are only required for threaded libcrypto
821  * applications, but make sure we don't stomp on them if
822  * they're already set.
823  */
824  if (CRYPTO_get_id_callback() == NULL)
825  CRYPTO_set_id_callback(pq_threadidcallback);
826  if (CRYPTO_get_locking_callback() == NULL)
827  CRYPTO_set_locking_callback(pq_lockingcallback);
828  }
829 
830  conn->crypto_loaded = true;
831  }
832  }
833 #endif /* HAVE_CRYPTO_LOCK */
834 #endif /* ENABLE_THREAD_SAFETY */
835 
836  if (!ssl_lib_initialized && do_ssl)
837  {
838  if (pq_init_ssl_lib)
839  {
840 #ifdef HAVE_OPENSSL_INIT_SSL
841  OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL);
842 #else
843  OPENSSL_config(NULL);
844  SSL_library_init();
845  SSL_load_error_strings();
846 #endif
847  }
848  ssl_lib_initialized = true;
849  }
850 
851 #ifdef ENABLE_THREAD_SAFETY
852  pthread_mutex_unlock(&ssl_config_mutex);
853 #endif
854  return 0;
855 }
static bool ssl_lib_initialized
static bool pq_init_ssl_lib
int i
Definition: isn.c:73
int pthread_mutex_init(pthread_mutex_t *mp, void *attr)
Definition: pthread-win32.c:35
CRITICAL_SECTION * pthread_mutex_t
Definition: pthread-win32.h:8

References conn, free, i, malloc, pq_init_crypto_lib, pq_init_ssl_lib, pthread_mutex_init(), pthread_mutex_lock(), pthread_mutex_unlock(), and ssl_lib_initialized.

Referenced by pqsecure_initialize().

◆ pgtls_init_library()

void pgtls_init_library ( bool  do_ssl,
int  do_crypto 
)

Definition at line 115 of file fe-secure-openssl.c.

116 {
117 #ifdef ENABLE_THREAD_SAFETY
118 
119  /*
120  * Disallow changing the flags while we have open connections, else we'd
121  * get completely confused.
122  */
123  if (crypto_open_connections != 0)
124  return;
125 #endif
126 
127  pq_init_ssl_lib = do_ssl;
128  pq_init_crypto_lib = do_crypto;
129 }

References pq_init_crypto_lib, and pq_init_ssl_lib.

Referenced by PQinitOpenSSL(), and PQinitSSL().

◆ pgtls_open_client()

PostgresPollingStatusType pgtls_open_client ( PGconn conn)

Definition at line 132 of file fe-secure-openssl.c.

133 {
134  /* First time through? */
135  if (conn->ssl == NULL)
136  {
137  /*
138  * Create a connection-specific SSL object, and load client
139  * certificate, private key, and trusted CA certs.
140  */
141  if (initialize_SSL(conn) != 0)
142  {
143  /* initialize_SSL already put a message in conn->errorMessage */
144  pgtls_close(conn);
145  return PGRES_POLLING_FAILED;
146  }
147  }
148 
149  /* Begin or continue the actual handshake */
150  return open_client_SSL(conn);
151 }
static PostgresPollingStatusType open_client_SSL(PGconn *)
static int initialize_SSL(PGconn *conn)

References conn, initialize_SSL(), open_client_SSL(), PGRES_POLLING_FAILED, and pgtls_close().

Referenced by pqsecure_open_client().

◆ pgtls_read()

ssize_t pgtls_read ( PGconn conn,
void *  ptr,
size_t  len 
)

Definition at line 154 of file fe-secure-openssl.c.

155 {
156  ssize_t n;
157  int result_errno = 0;
158  char sebuf[PG_STRERROR_R_BUFLEN];
159  int err;
160  unsigned long ecode;
161 
162 rloop:
163 
164  /*
165  * Prepare to call SSL_get_error() by clearing thread's OpenSSL error
166  * queue. In general, the current thread's error queue must be empty
167  * before the TLS/SSL I/O operation is attempted, or SSL_get_error() will
168  * not work reliably. Since the possibility exists that other OpenSSL
169  * clients running in the same thread but not under our control will fail
170  * to call ERR_get_error() themselves (after their own I/O operations),
171  * pro-actively clear the per-thread error queue now.
172  */
173  SOCK_ERRNO_SET(0);
174  ERR_clear_error();
175  n = SSL_read(conn->ssl, ptr, len);
176  err = SSL_get_error(conn->ssl, n);
177 
178  /*
179  * Other clients of OpenSSL may fail to call ERR_get_error(), but we
180  * always do, so as to not cause problems for OpenSSL clients that don't
181  * call ERR_clear_error() defensively. Be sure that this happens by
182  * calling now. SSL_get_error() relies on the OpenSSL per-thread error
183  * queue being intact, so this is the earliest possible point
184  * ERR_get_error() may be called.
185  */
186  ecode = (err != SSL_ERROR_NONE || n < 0) ? ERR_get_error() : 0;
187  switch (err)
188  {
189  case SSL_ERROR_NONE:
190  if (n < 0)
191  {
192  /* Not supposed to happen, so we don't translate the msg */
194  "SSL_read failed but did not provide error information\n");
195  /* assume the connection is broken */
196  result_errno = ECONNRESET;
197  }
198  break;
199  case SSL_ERROR_WANT_READ:
200  n = 0;
201  break;
202  case SSL_ERROR_WANT_WRITE:
203 
204  /*
205  * Returning 0 here would cause caller to wait for read-ready,
206  * which is not correct since what SSL wants is wait for
207  * write-ready. The former could get us stuck in an infinite
208  * wait, so don't risk it; busy-loop instead.
209  */
210  goto rloop;
211  case SSL_ERROR_SYSCALL:
212  if (n < 0)
213  {
214  result_errno = SOCK_ERRNO;
215  if (result_errno == EPIPE ||
216  result_errno == ECONNRESET)
218  libpq_gettext("server closed the connection unexpectedly\n"
219  "\tThis probably means the server terminated abnormally\n"
220  "\tbefore or while processing the request.\n"));
221  else
223  libpq_gettext("SSL SYSCALL error: %s\n"),
224  SOCK_STRERROR(result_errno,
225  sebuf, sizeof(sebuf)));
226  }
227  else
228  {
230  libpq_gettext("SSL SYSCALL error: EOF detected\n"));
231  /* assume the connection is broken */
232  result_errno = ECONNRESET;
233  n = -1;
234  }
235  break;
236  case SSL_ERROR_SSL:
237  {
238  char *errm = SSLerrmessage(ecode);
239 
241  libpq_gettext("SSL error: %s\n"), errm);
242  SSLerrfree(errm);
243  /* assume the connection is broken */
244  result_errno = ECONNRESET;
245  n = -1;
246  break;
247  }
248  case SSL_ERROR_ZERO_RETURN:
249 
250  /*
251  * Per OpenSSL documentation, this error code is only returned for
252  * a clean connection closure, so we should not report it as a
253  * server crash.
254  */
256  libpq_gettext("SSL connection has been closed unexpectedly\n"));
257  result_errno = ECONNRESET;
258  n = -1;
259  break;
260  default:
262  libpq_gettext("unrecognized SSL error code: %d\n"),
263  err);
264  /* assume the connection is broken */
265  result_errno = ECONNRESET;
266  n = -1;
267  break;
268  }
269 
270  /* ensure we return the intended errno to caller */
271  SOCK_ERRNO_SET(result_errno);
272 
273  return n;
274 }
#define SOCK_ERRNO_SET(e)
Definition: libpq-int.h:893
#define ECONNRESET
Definition: win32_port.h:361

References appendPQExpBuffer(), appendPQExpBufferStr(), conn, ECONNRESET, pg_conn::errorMessage, len, libpq_gettext, PG_STRERROR_R_BUFLEN, SOCK_ERRNO, SOCK_ERRNO_SET, SOCK_STRERROR, SSLerrfree(), and SSLerrmessage().

Referenced by pqsecure_read().

◆ pgtls_read_pending()

bool pgtls_read_pending ( PGconn conn)

Definition at line 277 of file fe-secure-openssl.c.

278 {
279  return SSL_pending(conn->ssl) > 0;
280 }

References conn.

Referenced by pqSocketCheck().

◆ pgtls_verify_peer_name_matches_certificate_guts()

int pgtls_verify_peer_name_matches_certificate_guts ( PGconn conn,
int *  names_examined,
char **  first_name 
)

Definition at line 571 of file fe-secure-openssl.c.

574 {
575  STACK_OF(GENERAL_NAME) * peer_san;
576  int i;
577  int rc = 0;
578  char *host = conn->connhost[conn->whichhost].host;
579  int host_type;
580  bool check_cn = true;
581 
582  Assert(host && host[0]); /* should be guaranteed by caller */
583 
584  /*
585  * We try to match the NSS behavior here, which is a slight departure from
586  * the spec but seems to make more intuitive sense:
587  *
588  * If connhost contains a DNS name, and the certificate's SANs contain any
589  * dNSName entries, then we'll ignore the Subject Common Name entirely;
590  * otherwise, we fall back to checking the CN. (This behavior matches the
591  * RFC.)
592  *
593  * If connhost contains an IP address, and the SANs contain iPAddress
594  * entries, we again ignore the CN. Otherwise, we allow the CN to match,
595  * EVEN IF there is a dNSName in the SANs. (RFC 6125 prohibits this: "A
596  * client MUST NOT seek a match for a reference identifier of CN-ID if the
597  * presented identifiers include a DNS-ID, SRV-ID, URI-ID, or any
598  * application-specific identifier types supported by the client.")
599  *
600  * NOTE: Prior versions of libpq did not consider iPAddress entries at
601  * all, so this new behavior might break a certificate that has different
602  * IP addresses in the Subject CN and the SANs.
603  */
604  if (is_ip_address(host))
605  host_type = GEN_IPADD;
606  else
607  host_type = GEN_DNS;
608 
609  /*
610  * First, get the Subject Alternative Names (SANs) from the certificate,
611  * and compare them against the originally given hostname.
612  */
613  peer_san = (STACK_OF(GENERAL_NAME) *)
614  X509_get_ext_d2i(conn->peer, NID_subject_alt_name, NULL, NULL);
615 
616  if (peer_san)
617  {
618  int san_len = sk_GENERAL_NAME_num(peer_san);
619 
620  for (i = 0; i < san_len; i++)
621  {
622  const GENERAL_NAME *name = sk_GENERAL_NAME_value(peer_san, i);
623  char *alt_name = NULL;
624 
625  if (name->type == host_type)
626  {
627  /*
628  * This SAN is of the same type (IP or DNS) as our host name,
629  * so don't allow a fallback check of the CN.
630  */
631  check_cn = false;
632  }
633 
634  if (name->type == GEN_DNS)
635  {
636  (*names_examined)++;
638  name->d.dNSName,
639  &alt_name);
640  }
641  else if (name->type == GEN_IPADD)
642  {
643  (*names_examined)++;
645  name->d.iPAddress,
646  &alt_name);
647  }
648 
649  if (alt_name)
650  {
651  if (!*first_name)
652  *first_name = alt_name;
653  else
654  free(alt_name);
655  }
656 
657  if (rc != 0)
658  {
659  /*
660  * Either we hit an error or a match, and either way we should
661  * not fall back to the CN.
662  */
663  check_cn = false;
664  break;
665  }
666  }
667  sk_GENERAL_NAME_pop_free(peer_san, GENERAL_NAME_free);
668  }
669 
670  /*
671  * If there is no subjectAltName extension of the matching type, check the
672  * Common Name.
673  *
674  * (Per RFC 2818 and RFC 6125, if the subjectAltName extension of type
675  * dNSName is present, the CN must be ignored. We break this rule if host
676  * is an IP address; see the comment above.)
677  */
678  if (check_cn)
679  {
680  X509_NAME *subject_name;
681 
682  subject_name = X509_get_subject_name(conn->peer);
683  if (subject_name != NULL)
684  {
685  int cn_index;
686 
687  cn_index = X509_NAME_get_index_by_NID(subject_name,
688  NID_commonName, -1);
689  if (cn_index >= 0)
690  {
691  char *common_name = NULL;
692 
693  (*names_examined)++;
695  X509_NAME_ENTRY_get_data(X509_NAME_get_entry(subject_name, cn_index)),
696  &common_name);
697 
698  if (common_name)
699  {
700  if (!*first_name)
701  *first_name = common_name;
702  else
703  free(common_name);
704  }
705  }
706  }
707  }
708 
709  return rc;
710 }
const char * name
Definition: encode.c:561
static int openssl_verify_peer_name_matches_certificate_ip(PGconn *conn, ASN1_OCTET_STRING *addr_entry, char **store_name)
static int openssl_verify_peer_name_matches_certificate_name(PGconn *conn, ASN1_STRING *name, char **store_name)
static bool is_ip_address(const char *host)
Assert(fmt[strlen(fmt) - 1] !='\n')

References Assert(), conn, pg_conn::connhost, free, pg_conn_host::host, i, is_ip_address(), name, openssl_verify_peer_name_matches_certificate_ip(), openssl_verify_peer_name_matches_certificate_name(), and pg_conn::whichhost.

Referenced by pq_verify_peer_name_matches_certificate().

◆ pgtls_write()

ssize_t pgtls_write ( PGconn conn,
const void *  ptr,
size_t  len 
)

Definition at line 283 of file fe-secure-openssl.c.

284 {
285  ssize_t n;
286  int result_errno = 0;
287  char sebuf[PG_STRERROR_R_BUFLEN];
288  int err;
289  unsigned long ecode;
290 
291  SOCK_ERRNO_SET(0);
292  ERR_clear_error();
293  n = SSL_write(conn->ssl, ptr, len);
294  err = SSL_get_error(conn->ssl, n);
295  ecode = (err != SSL_ERROR_NONE || n < 0) ? ERR_get_error() : 0;
296  switch (err)
297  {
298  case SSL_ERROR_NONE:
299  if (n < 0)
300  {
301  /* Not supposed to happen, so we don't translate the msg */
303  "SSL_write failed but did not provide error information\n");
304  /* assume the connection is broken */
305  result_errno = ECONNRESET;
306  }
307  break;
308  case SSL_ERROR_WANT_READ:
309 
310  /*
311  * Returning 0 here causes caller to wait for write-ready, which
312  * is not really the right thing, but it's the best we can do.
313  */
314  n = 0;
315  break;
316  case SSL_ERROR_WANT_WRITE:
317  n = 0;
318  break;
319  case SSL_ERROR_SYSCALL:
320  if (n < 0)
321  {
322  result_errno = SOCK_ERRNO;
323  if (result_errno == EPIPE || result_errno == ECONNRESET)
325  libpq_gettext("server closed the connection unexpectedly\n"
326  "\tThis probably means the server terminated abnormally\n"
327  "\tbefore or while processing the request.\n"));
328  else
330  libpq_gettext("SSL SYSCALL error: %s\n"),
331  SOCK_STRERROR(result_errno,
332  sebuf, sizeof(sebuf)));
333  }
334  else
335  {
337  libpq_gettext("SSL SYSCALL error: EOF detected\n"));
338  /* assume the connection is broken */
339  result_errno = ECONNRESET;
340  n = -1;
341  }
342  break;
343  case SSL_ERROR_SSL:
344  {
345  char *errm = SSLerrmessage(ecode);
346 
348  libpq_gettext("SSL error: %s\n"), errm);
349  SSLerrfree(errm);
350  /* assume the connection is broken */
351  result_errno = ECONNRESET;
352  n = -1;
353  break;
354  }
355  case SSL_ERROR_ZERO_RETURN:
356 
357  /*
358  * Per OpenSSL documentation, this error code is only returned for
359  * a clean connection closure, so we should not report it as a
360  * server crash.
361  */
363  libpq_gettext("SSL connection has been closed unexpectedly\n"));
364  result_errno = ECONNRESET;
365  n = -1;
366  break;
367  default:
369  libpq_gettext("unrecognized SSL error code: %d\n"),
370  err);
371  /* assume the connection is broken */
372  result_errno = ECONNRESET;
373  n = -1;
374  break;
375  }
376 
377  /* ensure we return the intended errno to caller */
378  SOCK_ERRNO_SET(result_errno);
379 
380  return n;
381 }

References appendPQExpBuffer(), appendPQExpBufferStr(), conn, ECONNRESET, pg_conn::errorMessage, len, libpq_gettext, PG_STRERROR_R_BUFLEN, SOCK_ERRNO, SOCK_ERRNO_SET, SOCK_STRERROR, SSLerrfree(), and SSLerrmessage().

Referenced by pqsecure_write().

◆ PQdefaultSSLKeyPassHook_OpenSSL()

int PQdefaultSSLKeyPassHook_OpenSSL ( char *  buf,
int  size,
PGconn conn 
)

Definition at line 1939 of file fe-secure-openssl.c.

1940 {
1941  if (conn->sslpassword)
1942  {
1943  if (strlen(conn->sslpassword) + 1 > size)
1944  fprintf(stderr, libpq_gettext("WARNING: sslpassword truncated\n"));
1945  strncpy(buf, conn->sslpassword, size);
1946  buf[size - 1] = '\0';
1947  return strlen(buf);
1948  }
1949  else
1950  {
1951  buf[0] = '\0';
1952  return 0;
1953  }
1954 }
#define fprintf
Definition: port.h:229

References buf, conn, fprintf, libpq_gettext, and pg_conn::sslpassword.

Referenced by PQssl_passwd_cb().

◆ PQgetssl()

void* PQgetssl ( PGconn conn)

Definition at line 1715 of file fe-secure-openssl.c.

1716 {
1717  if (!conn)
1718  return NULL;
1719  return conn->ssl;
1720 }

References conn.

◆ PQgetSSLKeyPassHook_OpenSSL()

PQsslKeyPassHook_OpenSSL_type PQgetSSLKeyPassHook_OpenSSL ( void  )

Definition at line 1957 of file fe-secure-openssl.c.

1958 {
1959  return PQsslKeyPassHook;
1960 }

References PQsslKeyPassHook.

◆ PQsetSSLKeyPassHook_OpenSSL()

void PQsetSSLKeyPassHook_OpenSSL ( PQsslKeyPassHook_OpenSSL_type  hook)

Definition at line 1963 of file fe-secure-openssl.c.

1964 {
1965  PQsslKeyPassHook = hook;
1966 }

References PQsslKeyPassHook.

◆ PQssl_passwd_cb()

static int PQssl_passwd_cb ( char *  buf,
int  size,
int  rwflag,
void *  userdata 
)
static

Definition at line 1974 of file fe-secure-openssl.c.

1975 {
1976  PGconn *conn = userdata;
1977 
1978  if (PQsslKeyPassHook)
1979  return PQsslKeyPassHook(buf, size, conn);
1980  else
1981  return PQdefaultSSLKeyPassHook_OpenSSL(buf, size, conn);
1982 }
int PQdefaultSSLKeyPassHook_OpenSSL(char *buf, int size, PGconn *conn)

References buf, conn, PQdefaultSSLKeyPassHook_OpenSSL(), and PQsslKeyPassHook.

Referenced by initialize_SSL().

◆ PQsslAttribute()

const char* PQsslAttribute ( PGconn conn,
const char *  attribute_name 
)

Definition at line 1748 of file fe-secure-openssl.c.

1749 {
1750  if (strcmp(attribute_name, "library") == 0)
1751  return "OpenSSL";
1752 
1753  if (!conn)
1754  return NULL;
1755  if (conn->ssl == NULL)
1756  return NULL;
1757 
1758  if (strcmp(attribute_name, "key_bits") == 0)
1759  {
1760  static char sslbits_str[12];
1761  int sslbits;
1762 
1763  SSL_get_cipher_bits(conn->ssl, &sslbits);
1764  snprintf(sslbits_str, sizeof(sslbits_str), "%d", sslbits);
1765  return sslbits_str;
1766  }
1767 
1768  if (strcmp(attribute_name, "cipher") == 0)
1769  return SSL_get_cipher(conn->ssl);
1770 
1771  if (strcmp(attribute_name, "compression") == 0)
1772  return SSL_get_current_compression(conn->ssl) ? "on" : "off";
1773 
1774  if (strcmp(attribute_name, "protocol") == 0)
1775  return SSL_get_version(conn->ssl);
1776 
1777  return NULL; /* unknown attribute */
1778 }

References conn, and snprintf.

Referenced by print_ssl_library(), and printSSLInfo().

◆ PQsslAttributeNames()

const char* const* PQsslAttributeNames ( PGconn conn)

Definition at line 1733 of file fe-secure-openssl.c.

1734 {
1735  static const char *const result[] = {
1736  "library",
1737  "key_bits",
1738  "cipher",
1739  "compression",
1740  "protocol",
1741  NULL
1742  };
1743 
1744  return result;
1745 }

◆ PQsslStruct()

void* PQsslStruct ( PGconn conn,
const char *  struct_name 
)

Definition at line 1723 of file fe-secure-openssl.c.

1724 {
1725  if (!conn)
1726  return NULL;
1727  if (strcmp(struct_name, "OpenSSL") == 0)
1728  return conn->ssl;
1729  return NULL;
1730 }

References conn.

◆ ssl_protocol_version_to_openssl()

static int ssl_protocol_version_to_openssl ( const char *  protocol)
static

Definition at line 1995 of file fe-secure-openssl.c.

1996 {
1997  if (pg_strcasecmp("TLSv1", protocol) == 0)
1998  return TLS1_VERSION;
1999 
2000 #ifdef TLS1_1_VERSION
2001  if (pg_strcasecmp("TLSv1.1", protocol) == 0)
2002  return TLS1_1_VERSION;
2003 #endif
2004 
2005 #ifdef TLS1_2_VERSION
2006  if (pg_strcasecmp("TLSv1.2", protocol) == 0)
2007  return TLS1_2_VERSION;
2008 #endif
2009 
2010 #ifdef TLS1_3_VERSION
2011  if (pg_strcasecmp("TLSv1.3", protocol) == 0)
2012  return TLS1_3_VERSION;
2013 #endif
2014 
2015  return -1;
2016 }
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36

References pg_strcasecmp().

Referenced by initialize_SSL().

◆ SSLerrfree()

static void SSLerrfree ( char *  buf)
static

Definition at line 1701 of file fe-secure-openssl.c.

1702 {
1703  if (buf != ssl_nomem)
1704  free(buf);
1705 }
static char ssl_nomem[]

References buf, free, and ssl_nomem.

Referenced by initialize_SSL(), open_client_SSL(), pgtls_read(), and pgtls_write().

◆ SSLerrmessage()

static char * SSLerrmessage ( unsigned long  ecode)
static

Definition at line 1677 of file fe-secure-openssl.c.

1678 {
1679  const char *errreason;
1680  char *errbuf;
1681 
1682  errbuf = malloc(SSL_ERR_LEN);
1683  if (!errbuf)
1684  return ssl_nomem;
1685  if (ecode == 0)
1686  {
1687  snprintf(errbuf, SSL_ERR_LEN, libpq_gettext("no SSL error reported"));
1688  return errbuf;
1689  }
1690  errreason = ERR_reason_error_string(ecode);
1691  if (errreason != NULL)
1692  {
1693  strlcpy(errbuf, errreason, SSL_ERR_LEN);
1694  return errbuf;
1695  }
1696  snprintf(errbuf, SSL_ERR_LEN, libpq_gettext("SSL error code %lu"), ecode);
1697  return errbuf;
1698 }
#define SSL_ERR_LEN

References libpq_gettext, malloc, snprintf, SSL_ERR_LEN, ssl_nomem, and strlcpy().

Referenced by initialize_SSL(), open_client_SSL(), pgtls_read(), and pgtls_write().

◆ verify_cb()

static int verify_cb ( int  ok,
X509_STORE_CTX *  ctx 
)
static

Definition at line 475 of file fe-secure-openssl.c.

476 {
477  return ok;
478 }

Referenced by initialize_SSL().

Variable Documentation

◆ my_bio_methods

BIO_METHOD* my_bio_methods
static

Definition at line 1796 of file fe-secure-openssl.c.

Referenced by my_BIO_s_socket().

◆ pq_init_crypto_lib

bool pq_init_crypto_lib = true
static

Definition at line 92 of file fe-secure-openssl.c.

Referenced by destroy_ssl_system(), pgtls_init(), and pgtls_init_library().

◆ pq_init_ssl_lib

bool pq_init_ssl_lib = true
static

Definition at line 91 of file fe-secure-openssl.c.

Referenced by pgtls_init(), and pgtls_init_library().

◆ PQsslKeyPassHook

◆ ssl_lib_initialized

bool ssl_lib_initialized = false
static

Definition at line 94 of file fe-secure-openssl.c.

Referenced by pgtls_init().

◆ ssl_nomem

char ssl_nomem[] = "out of memory allocating error description"
static

Definition at line 1672 of file fe-secure-openssl.c.

Referenced by SSLerrfree(), and SSLerrmessage().