PostgreSQL Source Code  git master
fe-secure-openssl.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * fe-secure-openssl.c
4  * OpenSSL support
5  *
6  *
7  * Portions Copyright (c) 1996-2022, 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 #ifdef HAVE_NETINET_TCP_H
42 #include <netinet/tcp.h>
43 #endif
44 #include <arpa/inet.h>
45 #endif
46 
47 #include <sys/stat.h>
48 
49 #ifdef ENABLE_THREAD_SAFETY
50 #ifdef WIN32
51 #include "pthread-win32.h"
52 #else
53 #include <pthread.h>
54 #endif
55 #endif
56 
57 /*
58  * These SSL-related #includes must come after all system-provided headers.
59  * This ensures that OpenSSL can take care of conflicts with Windows'
60  * <wincrypt.h> by #undef'ing the conflicting macros. (We don't directly
61  * include <wincrypt.h>, but some other Windows headers do.)
62  */
63 #include "common/openssl.h"
64 #include <openssl/conf.h>
65 #ifdef USE_SSL_ENGINE
66 #include <openssl/engine.h>
67 #endif
68 #include <openssl/x509v3.h>
69 
70 
71 static int verify_cb(int ok, X509_STORE_CTX *ctx);
73  ASN1_STRING *name,
74  char **store_name);
76  ASN1_OCTET_STRING *addr_entry,
77  char **store_name);
78 static void destroy_ssl_system(void);
79 static int initialize_SSL(PGconn *conn);
81 static char *SSLerrmessage(unsigned long ecode);
82 static void SSLerrfree(char *buf);
83 static int PQssl_passwd_cb(char *buf, int size, int rwflag, void *userdata);
84 
85 static int my_sock_read(BIO *h, char *buf, int size);
86 static int my_sock_write(BIO *h, const char *buf, int size);
87 static BIO_METHOD *my_BIO_s_socket(void);
88 static int my_SSL_set_fd(PGconn *conn, int fd);
89 
90 
91 static bool pq_init_ssl_lib = true;
92 static bool pq_init_crypto_lib = true;
93 
94 static bool ssl_lib_initialized = false;
95 
96 #ifdef ENABLE_THREAD_SAFETY
97 static long crypto_open_connections = 0;
98 
99 #ifndef WIN32
100 static pthread_mutex_t ssl_config_mutex = PTHREAD_MUTEX_INITIALIZER;
101 #else
102 static pthread_mutex_t ssl_config_mutex = NULL;
103 static long win32_ssl_create_mutex = 0;
104 #endif
105 #endif /* ENABLE_THREAD_SAFETY */
106 
108 static int ssl_protocol_version_to_openssl(const char *protocol);
109 
110 /* ------------------------------------------------------------ */
111 /* Procedures common to all secure sessions */
112 /* ------------------------------------------------------------ */
113 
114 void
115 pgtls_init_library(bool do_ssl, int do_crypto)
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 }
130 
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 }
152 
153 ssize_t
154 pgtls_read(PGconn *conn, void *ptr, size_t len)
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 }
275 
276 bool
278 {
279  return SSL_pending(conn->ssl) > 0;
280 }
281 
282 ssize_t
283 pgtls_write(PGconn *conn, const void *ptr, size_t len)
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 }
382 
383 #ifdef HAVE_X509_GET_SIGNATURE_NID
384 char *
385 pgtls_get_peer_certificate_hash(PGconn *conn, size_t *len)
386 {
387  X509 *peer_cert;
388  const EVP_MD *algo_type;
389  unsigned char hash[EVP_MAX_MD_SIZE]; /* size for SHA-512 */
390  unsigned int hash_size;
391  int algo_nid;
392  char *cert_hash;
393 
394  *len = 0;
395 
396  if (!conn->peer)
397  return NULL;
398 
399  peer_cert = conn->peer;
400 
401  /*
402  * Get the signature algorithm of the certificate to determine the hash
403  * algorithm to use for the result.
404  */
405  if (!OBJ_find_sigid_algs(X509_get_signature_nid(peer_cert),
406  &algo_nid, NULL))
407  {
409  libpq_gettext("could not determine server certificate signature algorithm\n"));
410  return NULL;
411  }
412 
413  /*
414  * The TLS server's certificate bytes need to be hashed with SHA-256 if
415  * its signature algorithm is MD5 or SHA-1 as per RFC 5929
416  * (https://tools.ietf.org/html/rfc5929#section-4.1). If something else
417  * is used, the same hash as the signature algorithm is used.
418  */
419  switch (algo_nid)
420  {
421  case NID_md5:
422  case NID_sha1:
423  algo_type = EVP_sha256();
424  break;
425  default:
426  algo_type = EVP_get_digestbynid(algo_nid);
427  if (algo_type == NULL)
428  {
430  libpq_gettext("could not find digest for NID %s\n"),
431  OBJ_nid2sn(algo_nid));
432  return NULL;
433  }
434  break;
435  }
436 
437  if (!X509_digest(peer_cert, algo_type, hash, &hash_size))
438  {
440  libpq_gettext("could not generate peer certificate hash\n"));
441  return NULL;
442  }
443 
444  /* save result */
445  cert_hash = malloc(hash_size);
446  if (cert_hash == NULL)
447  {
449  libpq_gettext("out of memory\n"));
450  return NULL;
451  }
452  memcpy(cert_hash, hash, hash_size);
453  *len = hash_size;
454 
455  return cert_hash;
456 }
457 #endif /* HAVE_X509_GET_SIGNATURE_NID */
458 
459 /* ------------------------------------------------------------ */
460 /* OpenSSL specific code */
461 /* ------------------------------------------------------------ */
462 
463 /*
464  * Certificate verification callback
465  *
466  * This callback allows us to log intermediate problems during
467  * verification, but there doesn't seem to be a clean way to get
468  * our PGconn * structure. So we can't log anything!
469  *
470  * This callback also allows us to override the default acceptance
471  * criteria (e.g., accepting self-signed or expired certs), but
472  * for now we accept the default checks.
473  */
474 static int
475 verify_cb(int ok, X509_STORE_CTX *ctx)
476 {
477  return ok;
478 }
479 
480 
481 /*
482  * OpenSSL-specific wrapper around
483  * pq_verify_peer_name_matches_certificate_name(), converting the ASN1_STRING
484  * into a plain C string.
485  */
486 static int
488  char **store_name)
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 }
514 
515 /*
516  * OpenSSL-specific wrapper around
517  * pq_verify_peer_name_matches_certificate_ip(), converting the
518  * ASN1_OCTET_STRING into a plain C string.
519  */
520 static int
522  ASN1_OCTET_STRING *addr_entry,
523  char **store_name)
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 }
549 
550 static bool
551 is_ip_address(const char *host)
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 }
564 
565 /*
566  * Verify that the server certificate matches the hostname we connected to.
567  *
568  * The certificate's Common Name and Subject Alternative Names are considered.
569  */
570 int
572  int *names_examined,
573  char **first_name)
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 }
711 
712 #if defined(ENABLE_THREAD_SAFETY) && defined(HAVE_CRYPTO_LOCK)
713 /*
714  * Callback functions for OpenSSL internal locking. (OpenSSL 1.1.0
715  * does its own locking, and doesn't need these anymore. The
716  * CRYPTO_lock() function was removed in 1.1.0, when the callbacks
717  * were made obsolete, so we assume that if CRYPTO_lock() exists,
718  * the callbacks are still required.)
719  */
720 
721 static unsigned long
722 pq_threadidcallback(void)
723 {
724  /*
725  * This is not standards-compliant. pthread_self() returns pthread_t, and
726  * shouldn't be cast to unsigned long, but CRYPTO_set_id_callback requires
727  * it, so we have to do it.
728  */
729  return (unsigned long) pthread_self();
730 }
731 
732 static pthread_mutex_t *pq_lockarray;
733 
734 static void
735 pq_lockingcallback(int mode, int n, const char *file, int line)
736 {
737  /*
738  * There's no way to report a mutex-primitive failure, so we just Assert
739  * in development builds, and ignore any errors otherwise. Fortunately
740  * this is all obsolete in modern OpenSSL.
741  */
742  if (mode & CRYPTO_LOCK)
743  {
744  if (pthread_mutex_lock(&pq_lockarray[n]))
745  Assert(false);
746  }
747  else
748  {
749  if (pthread_mutex_unlock(&pq_lockarray[n]))
750  Assert(false);
751  }
752 }
753 #endif /* ENABLE_THREAD_SAFETY && HAVE_CRYPTO_LOCK */
754 
755 /*
756  * Initialize SSL library.
757  *
758  * In threadsafe mode, this includes setting up libcrypto callback functions
759  * to do thread locking.
760  *
761  * If the caller has told us (through PQinitOpenSSL) that he's taking care
762  * of libcrypto, we expect that callbacks are already set, and won't try to
763  * override it.
764  */
765 int
766 pgtls_init(PGconn *conn, bool do_ssl, bool do_crypto)
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 }
856 
857 /*
858  * This function is needed because if the libpq library is unloaded
859  * from the application, the callback functions will no longer exist when
860  * libcrypto is used by other parts of the system. For this reason,
861  * we unregister the callback functions when the last libpq
862  * connection is closed. (The same would apply for OpenSSL callbacks
863  * if we had any.)
864  *
865  * Callbacks are only set when we're compiled in threadsafe mode, so
866  * we only need to remove them in this case. They are also not needed
867  * with OpenSSL 1.1.0 anymore.
868  */
869 static void
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 }
903 
904 /*
905  * Create per-connection SSL object, and load the client certificate,
906  * private key, and trusted CA certs.
907  *
908  * Returns 0 if OK, -1 on failure (with a message in conn->errorMessage).
909  */
910 static int
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 key files owned by users other than us or root, and
1377  * require no public access to the key file. If the file is owned by
1378  * us, require mode 0600 or less. If owned by root, require 0640 or
1379  * less to allow read access through either our gid or a supplementary
1380  * gid that allows us to read system-wide certificates.
1381  *
1382  * Note that similar checks are performed in
1383  * src/backend/libpq/be-secure-common.c so any changes here may need
1384  * to be made there as well.
1385  *
1386  * Ideally we would do similar permissions checks on Windows, but it
1387  * is not clear how that would work since Unix-style permissions may
1388  * not be available.
1389  */
1390 #if !defined(WIN32) && !defined(__CYGWIN__)
1391  if (buf.st_uid != geteuid() && buf.st_uid != 0)
1392  {
1394  libpq_gettext("private key file \"%s\" must be owned by the current user or root\n"),
1395  fnbuf);
1396  return -1;
1397  }
1398 
1399  if ((buf.st_uid == geteuid() && buf.st_mode & (S_IRWXG | S_IRWXO)) ||
1400  (buf.st_uid == 0 && buf.st_mode & (S_IWGRP | S_IXGRP | 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 }
1468 
1469 /*
1470  * Attempt to negotiate SSL connection.
1471  */
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 }
1597 
1598 void
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 }
1661 
1662 
1663 /*
1664  * Obtain reason string for passed SSL errcode
1665  *
1666  * ERR_get_error() is used by caller to get errcode to pass here.
1667  *
1668  * Some caution is needed here since ERR_reason_error_string will
1669  * return NULL if it doesn't recognize the error code. We don't
1670  * want to return NULL ever.
1671  */
1672 static char ssl_nomem[] = "out of memory allocating error description";
1673 
1674 #define SSL_ERR_LEN 128
1675 
1676 static char *
1677 SSLerrmessage(unsigned long ecode)
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 }
1699 
1700 static void
1702 {
1703  if (buf != ssl_nomem)
1704  free(buf);
1705 }
1706 
1707 /* ------------------------------------------------------------ */
1708 /* SSL information functions */
1709 /* ------------------------------------------------------------ */
1710 
1711 /*
1712  * Return pointer to OpenSSL object.
1713  */
1714 void *
1716 {
1717  if (!conn)
1718  return NULL;
1719  return conn->ssl;
1720 }
1721 
1722 void *
1723 PQsslStruct(PGconn *conn, const char *struct_name)
1724 {
1725  if (!conn)
1726  return NULL;
1727  if (strcmp(struct_name, "OpenSSL") == 0)
1728  return conn->ssl;
1729  return NULL;
1730 }
1731 
1732 const char *const *
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 }
1746 
1747 const char *
1748 PQsslAttribute(PGconn *conn, const char *attribute_name)
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 }
1779 
1780 /*
1781  * Private substitute BIO: this does the sending and receiving using
1782  * pqsecure_raw_write() and pqsecure_raw_read() instead, to allow those
1783  * functions to disable SIGPIPE and give better error messages on I/O errors.
1784  *
1785  * These functions are closely modelled on the standard socket BIO in OpenSSL;
1786  * see sock_read() and sock_write() in OpenSSL's crypto/bio/bss_sock.c.
1787  * XXX OpenSSL 1.0.1e considers many more errcodes than just EINTR as reasons
1788  * to retry; do we need to adopt their logic for that?
1789  */
1790 
1791 #ifndef HAVE_BIO_GET_DATA
1792 #define BIO_get_data(bio) (bio->ptr)
1793 #define BIO_set_data(bio, data) (bio->ptr = data)
1794 #endif
1795 
1796 static BIO_METHOD *my_bio_methods;
1797 
1798 static int
1799 my_sock_read(BIO *h, char *buf, int size)
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 }
1827 
1828 static int
1829 my_sock_write(BIO *h, const char *buf, int size)
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 }
1857 
1858 static BIO_METHOD *
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 }
1903 
1904 /* This should exactly match OpenSSL's SSL_set_fd except for using my BIO */
1905 static int
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 }
1932 
1933 /*
1934  * This is the default handler to return a client cert password from
1935  * conn->sslpassword. Apps may install it explicitly if they want to
1936  * prevent openssl from ever prompting on stdin.
1937  */
1938 int
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 }
1955 
1958 {
1959  return PQsslKeyPassHook;
1960 }
1961 
1962 void
1964 {
1965  PQsslKeyPassHook = hook;
1966 }
1967 
1968 /*
1969  * Supply a password to decrypt a client certificate.
1970  *
1971  * This must match OpenSSL type pem_password_cb.
1972  */
1973 static int
1974 PQssl_passwd_cb(char *buf, int size, int rwflag, void *userdata)
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 }
1983 
1984 /*
1985  * Convert TLS protocol version string to OpenSSL values
1986  *
1987  * If a version is passed that is not supported by the current OpenSSL version,
1988  * then we return -1. If a non-negative value is returned, subsequent code can
1989  * assume it is working with a supported version.
1990  *
1991  * Note: this is rather similar to the backend routine in be-secure-openssl.c,
1992  * so make sure to update both routines if changing this one.
1993  */
1994 static int
1995 ssl_protocol_version_to_openssl(const char *protocol)
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 }
static SSL_CTX * SSL_context
const char * name
Definition: encode.c:561
bool pqGetHomeDirectory(char *buf, int bufsize)
Definition: fe-connect.c:7406
int pq_verify_peer_name_matches_certificate_name(PGconn *conn, const char *namedata, size_t namelen, char **store_name)
int pq_verify_peer_name_matches_certificate_ip(PGconn *conn, const unsigned char *ipdata, size_t iplen, char **store_name)
bool pq_verify_peer_name_matches_certificate(PGconn *conn)
static char ssl_nomem[]
static int ssl_protocol_version_to_openssl(const char *protocol)
static void destroy_ssl_system(void)
static int my_SSL_set_fd(PGconn *conn, int fd)
static void SSLerrfree(char *buf)
PQsslKeyPassHook_OpenSSL_type PQgetSSLKeyPassHook_OpenSSL(void)
int pgtls_verify_peer_name_matches_certificate_guts(PGconn *conn, int *names_examined, char **first_name)
PostgresPollingStatusType pgtls_open_client(PGconn *conn)
bool pgtls_read_pending(PGconn *conn)
static bool pq_init_crypto_lib
int PQdefaultSSLKeyPassHook_OpenSSL(char *buf, int size, PGconn *conn)
static BIO_METHOD * my_bio_methods
void pgtls_init_library(bool do_ssl, int do_crypto)
#define BIO_get_data(bio)
ssize_t pgtls_read(PGconn *conn, void *ptr, size_t len)
static bool ssl_lib_initialized
static PostgresPollingStatusType open_client_SSL(PGconn *)
ssize_t pgtls_write(PGconn *conn, const void *ptr, size_t len)
static PQsslKeyPassHook_OpenSSL_type PQsslKeyPassHook
#define SSL_ERR_LEN
int pgtls_init(PGconn *conn, bool do_ssl, bool do_crypto)
static BIO_METHOD * my_BIO_s_socket(void)
const char *const * PQsslAttributeNames(PGconn *conn)
static bool pq_init_ssl_lib
#define BIO_set_data(bio, data)
void * PQgetssl(PGconn *conn)
void * PQsslStruct(PGconn *conn, const char *struct_name)
static int initialize_SSL(PGconn *conn)
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)
static int openssl_verify_peer_name_matches_certificate_ip(PGconn *conn, ASN1_OCTET_STRING *addr_entry, char **store_name)
const char * PQsslAttribute(PGconn *conn, const char *attribute_name)
static int openssl_verify_peer_name_matches_certificate_name(PGconn *conn, ASN1_STRING *name, char **store_name)
void pgtls_close(PGconn *conn)
void PQsetSSLKeyPassHook_OpenSSL(PQsslKeyPassHook_OpenSSL_type hook)
static int my_sock_write(BIO *h, const char *buf, int size)
static int my_sock_read(BIO *h, char *buf, int size)
static bool is_ip_address(const char *host)
ssize_t pqsecure_raw_read(PGconn *conn, void *ptr, size_t len)
Definition: fe-secure.c:232
ssize_t pqsecure_raw_write(PGconn *conn, const void *ptr, size_t len)
Definition: fe-secure.c:350
#define free(a)
Definition: header.h:65
#define malloc(a)
Definition: header.h:50
int i
Definition: isn.c:73
int(* PQsslKeyPassHook_OpenSSL_type)(char *buf, int size, PGconn *conn)
Definition: libpq-fe.h:665
PostgresPollingStatusType
Definition: libpq-fe.h:85
@ 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 libpq_gettext(x)
Definition: libpq-int.h:878
#define SOCK_STRERROR
Definition: libpq-int.h:892
#define SOCK_ERRNO
Definition: libpq-int.h:891
#define SOCK_ERRNO_SET(e)
Definition: libpq-int.h:893
Assert(fmt[strlen(fmt) - 1] !='\n')
static PgChecksumMode mode
Definition: pg_checksums.c:65
#define MAXPGPATH
const void size_t len
static char * buf
Definition: pg_test_fsync.c:67
#define PG_STRERROR_R_BUFLEN
Definition: port.h:243
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
int inet_aton(const char *cp, struct in_addr *addr)
Definition: inet_aton.c:56
#define snprintf
Definition: port.h:225
#define fprintf
Definition: port.h:229
#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
static int fd(const char *x, int i)
Definition: preproc-init.c:105
int SSL_CTX_set_max_proto_version(SSL_CTX *ctx, int version)
int SSL_CTX_set_min_proto_version(SSL_CTX *ctx, int version)
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
DWORD pthread_self(void)
Definition: pthread-win32.c:18
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
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:715
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 EINTR
Definition: win32_port.h:351
#define EWOULDBLOCK
Definition: win32_port.h:357
#define S_IRWXO
Definition: win32_port.h:321
#define S_ISREG(m)
Definition: win32_port.h:327
#define ECONNRESET
Definition: win32_port.h:361
#define S_IWGRP
Definition: win32_port.h:303
#define EAGAIN
Definition: win32_port.h:349