PostgreSQL Source Code  git master
be-secure-openssl.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * be-secure-openssl.c
4  * functions for OpenSSL support in the backend.
5  *
6  *
7  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  *
11  * IDENTIFICATION
12  * src/backend/libpq/be-secure-openssl.c
13  *
14  *-------------------------------------------------------------------------
15  */
16 
17 #include "postgres.h"
18 
19 #include <sys/stat.h>
20 #include <signal.h>
21 #include <fcntl.h>
22 #include <ctype.h>
23 #include <sys/socket.h>
24 #include <unistd.h>
25 #include <netdb.h>
26 #include <netinet/in.h>
27 #ifdef HAVE_NETINET_TCP_H
28 #include <netinet/tcp.h>
29 #include <arpa/inet.h>
30 #endif
31 
32 #include <openssl/ssl.h>
33 #include <openssl/dh.h>
34 #include <openssl/conf.h>
35 #ifndef OPENSSL_NO_ECDH
36 #include <openssl/ec.h>
37 #endif
38 
39 #include "libpq/libpq.h"
40 #include "miscadmin.h"
41 #include "pgstat.h"
42 #include "storage/fd.h"
43 #include "storage/latch.h"
44 #include "tcop/tcopprot.h"
45 #include "utils/memutils.h"
46 
47 
48 static int my_sock_read(BIO *h, char *buf, int size);
49 static int my_sock_write(BIO *h, const char *buf, int size);
50 static BIO_METHOD *my_BIO_s_socket(void);
51 static int my_SSL_set_fd(Port *port, int fd);
52 
53 static DH *load_dh_file(char *filename, bool isServerStart);
54 static DH *load_dh_buffer(const char *, size_t);
55 static int ssl_external_passwd_cb(char *buf, int size, int rwflag, void *userdata);
56 static int dummy_ssl_passwd_cb(char *buf, int size, int rwflag, void *userdata);
57 static int verify_cb(int, X509_STORE_CTX *);
58 static void info_cb(const SSL *ssl, int type, int args);
59 static bool initialize_dh(SSL_CTX *context, bool isServerStart);
60 static bool initialize_ecdh(SSL_CTX *context, bool isServerStart);
61 static const char *SSLerrmessage(unsigned long ecode);
62 
63 static char *X509_NAME_to_cstring(X509_NAME *name);
64 
65 static SSL_CTX *SSL_context = NULL;
66 static bool SSL_initialized = false;
67 static bool dummy_ssl_passwd_cb_called = false;
68 static bool ssl_is_server_start;
69 
70 
71 /* ------------------------------------------------------------ */
72 /* Public interface */
73 /* ------------------------------------------------------------ */
74 
75 int
76 be_tls_init(bool isServerStart)
77 {
78  STACK_OF(X509_NAME) *root_cert_list = NULL;
79  SSL_CTX *context;
80 
81  /* This stuff need be done only once. */
82  if (!SSL_initialized)
83  {
84 #ifdef HAVE_OPENSSL_INIT_SSL
85  OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL);
86 #else
87  OPENSSL_config(NULL);
88  SSL_library_init();
89  SSL_load_error_strings();
90 #endif
91  SSL_initialized = true;
92  }
93 
94  /*
95  * We use SSLv23_method() because it can negotiate use of the highest
96  * mutually supported protocol version, while alternatives like
97  * TLSv1_2_method() permit only one specific version. Note that we don't
98  * actually allow SSL v2 or v3, only TLS protocols (see below).
99  */
100  context = SSL_CTX_new(SSLv23_method());
101  if (!context)
102  {
103  ereport(isServerStart ? FATAL : LOG,
104  (errmsg("could not create SSL context: %s",
105  SSLerrmessage(ERR_get_error()))));
106  goto error;
107  }
108 
109  /*
110  * Disable OpenSSL's moving-write-buffer sanity check, because it causes
111  * unnecessary failures in nonblocking send cases.
112  */
113  SSL_CTX_set_mode(context, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
114 
115  /*
116  * Set password callback
117  */
118  if (isServerStart)
119  {
120  if (ssl_passphrase_command[0])
121  SSL_CTX_set_default_passwd_cb(context, ssl_external_passwd_cb);
122  }
123  else
124  {
126  SSL_CTX_set_default_passwd_cb(context, ssl_external_passwd_cb);
127  else
128 
129  /*
130  * If reloading and no external command is configured, override
131  * OpenSSL's default handling of passphrase-protected files,
132  * because we don't want to prompt for a passphrase in an
133  * already-running server.
134  */
135  SSL_CTX_set_default_passwd_cb(context, dummy_ssl_passwd_cb);
136  }
137  /* used by the callback */
138  ssl_is_server_start = isServerStart;
139 
140  /*
141  * Load and verify server's certificate and private key
142  */
143  if (SSL_CTX_use_certificate_chain_file(context, ssl_cert_file) != 1)
144  {
145  ereport(isServerStart ? FATAL : LOG,
146  (errcode(ERRCODE_CONFIG_FILE_ERROR),
147  errmsg("could not load server certificate file \"%s\": %s",
148  ssl_cert_file, SSLerrmessage(ERR_get_error()))));
149  goto error;
150  }
151 
152  if (!check_ssl_key_file_permissions(ssl_key_file, isServerStart))
153  goto error;
154 
155  /*
156  * OK, try to load the private key file.
157  */
159 
160  if (SSL_CTX_use_PrivateKey_file(context,
161  ssl_key_file,
162  SSL_FILETYPE_PEM) != 1)
163  {
165  ereport(isServerStart ? FATAL : LOG,
166  (errcode(ERRCODE_CONFIG_FILE_ERROR),
167  errmsg("private key file \"%s\" cannot be reloaded because it requires a passphrase",
168  ssl_key_file)));
169  else
170  ereport(isServerStart ? FATAL : LOG,
171  (errcode(ERRCODE_CONFIG_FILE_ERROR),
172  errmsg("could not load private key file \"%s\": %s",
173  ssl_key_file, SSLerrmessage(ERR_get_error()))));
174  goto error;
175  }
176 
177  if (SSL_CTX_check_private_key(context) != 1)
178  {
179  ereport(isServerStart ? FATAL : LOG,
180  (errcode(ERRCODE_CONFIG_FILE_ERROR),
181  errmsg("check of private key failed: %s",
182  SSLerrmessage(ERR_get_error()))));
183  goto error;
184  }
185 
186  /* disallow SSL v2/v3 */
187  SSL_CTX_set_options(context, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
188 
189  /* disallow SSL session tickets */
190 #ifdef SSL_OP_NO_TICKET /* added in openssl 0.9.8f */
191  SSL_CTX_set_options(context, SSL_OP_NO_TICKET);
192 #endif
193 
194  /* disallow SSL session caching, too */
195  SSL_CTX_set_session_cache_mode(context, SSL_SESS_CACHE_OFF);
196 
197  /* set up ephemeral DH and ECDH keys */
198  if (!initialize_dh(context, isServerStart))
199  goto error;
200  if (!initialize_ecdh(context, isServerStart))
201  goto error;
202 
203  /* set up the allowed cipher list */
204  if (SSL_CTX_set_cipher_list(context, SSLCipherSuites) != 1)
205  {
206  ereport(isServerStart ? FATAL : LOG,
207  (errcode(ERRCODE_CONFIG_FILE_ERROR),
208  errmsg("could not set the cipher list (no valid ciphers available)")));
209  goto error;
210  }
211 
212  /* Let server choose order */
214  SSL_CTX_set_options(context, SSL_OP_CIPHER_SERVER_PREFERENCE);
215 
216  /*
217  * Load CA store, so we can verify client certificates if needed.
218  */
219  if (ssl_ca_file[0])
220  {
221  if (SSL_CTX_load_verify_locations(context, ssl_ca_file, NULL) != 1 ||
222  (root_cert_list = SSL_load_client_CA_file(ssl_ca_file)) == NULL)
223  {
224  ereport(isServerStart ? FATAL : LOG,
225  (errcode(ERRCODE_CONFIG_FILE_ERROR),
226  errmsg("could not load root certificate file \"%s\": %s",
227  ssl_ca_file, SSLerrmessage(ERR_get_error()))));
228  goto error;
229  }
230  }
231 
232  /*----------
233  * Load the Certificate Revocation List (CRL).
234  * http://searchsecurity.techtarget.com/sDefinition/0,,sid14_gci803160,00.html
235  *----------
236  */
237  if (ssl_crl_file[0])
238  {
239  X509_STORE *cvstore = SSL_CTX_get_cert_store(context);
240 
241  if (cvstore)
242  {
243  /* Set the flags to check against the complete CRL chain */
244  if (X509_STORE_load_locations(cvstore, ssl_crl_file, NULL) == 1)
245  {
246  /* OpenSSL 0.96 does not support X509_V_FLAG_CRL_CHECK */
247 #ifdef X509_V_FLAG_CRL_CHECK
248  X509_STORE_set_flags(cvstore,
249  X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
250 #else
251  ereport(LOG,
252  (errcode(ERRCODE_CONFIG_FILE_ERROR),
253  errmsg("SSL certificate revocation list file \"%s\" ignored",
254  ssl_crl_file),
255  errdetail("SSL library does not support certificate revocation lists.")));
256 #endif
257  }
258  else
259  {
260  ereport(isServerStart ? FATAL : LOG,
261  (errcode(ERRCODE_CONFIG_FILE_ERROR),
262  errmsg("could not load SSL certificate revocation list file \"%s\": %s",
263  ssl_crl_file, SSLerrmessage(ERR_get_error()))));
264  goto error;
265  }
266  }
267  }
268 
269  if (ssl_ca_file[0])
270  {
271  /*
272  * Always ask for SSL client cert, but don't fail if it's not
273  * presented. We might fail such connections later, depending on what
274  * we find in pg_hba.conf.
275  */
276  SSL_CTX_set_verify(context,
277  (SSL_VERIFY_PEER |
278  SSL_VERIFY_CLIENT_ONCE),
279  verify_cb);
280 
281  /*
282  * Tell OpenSSL to send the list of root certs we trust to clients in
283  * CertificateRequests. This lets a client with a keystore select the
284  * appropriate client certificate to send to us.
285  */
286  SSL_CTX_set_client_CA_list(context, root_cert_list);
287  }
288 
289  /*
290  * Success! Replace any existing SSL_context.
291  */
292  if (SSL_context)
293  SSL_CTX_free(SSL_context);
294 
295  SSL_context = context;
296 
297  /*
298  * Set flag to remember whether CA store has been loaded into SSL_context.
299  */
300  if (ssl_ca_file[0])
302  else
304 
305  return 0;
306 
307 error:
308  if (context)
309  SSL_CTX_free(context);
310  return -1;
311 }
312 
313 void
315 {
316  if (SSL_context)
317  SSL_CTX_free(SSL_context);
318  SSL_context = NULL;
320 }
321 
322 int
324 {
325  int r;
326  int err;
327  int waitfor;
328  unsigned long ecode;
329 
330  Assert(!port->ssl);
331  Assert(!port->peer);
332 
333  if (!SSL_context)
334  {
336  (errcode(ERRCODE_PROTOCOL_VIOLATION),
337  errmsg("could not initialize SSL connection: SSL context not set up")));
338  return -1;
339  }
340 
341  if (!(port->ssl = SSL_new(SSL_context)))
342  {
344  (errcode(ERRCODE_PROTOCOL_VIOLATION),
345  errmsg("could not initialize SSL connection: %s",
346  SSLerrmessage(ERR_get_error()))));
347  return -1;
348  }
349  if (!my_SSL_set_fd(port, port->sock))
350  {
352  (errcode(ERRCODE_PROTOCOL_VIOLATION),
353  errmsg("could not set SSL socket: %s",
354  SSLerrmessage(ERR_get_error()))));
355  return -1;
356  }
357  port->ssl_in_use = true;
358 
359 aloop:
360 
361  /*
362  * Prepare to call SSL_get_error() by clearing thread's OpenSSL error
363  * queue. In general, the current thread's error queue must be empty
364  * before the TLS/SSL I/O operation is attempted, or SSL_get_error() will
365  * not work reliably. An extension may have failed to clear the
366  * per-thread error queue following another call to an OpenSSL I/O
367  * routine.
368  */
369  ERR_clear_error();
370  r = SSL_accept(port->ssl);
371  if (r <= 0)
372  {
373  err = SSL_get_error(port->ssl, r);
374 
375  /*
376  * Other clients of OpenSSL in the backend may fail to call
377  * ERR_get_error(), but we always do, so as to not cause problems for
378  * OpenSSL clients that don't call ERR_clear_error() defensively. Be
379  * sure that this happens by calling now. SSL_get_error() relies on
380  * the OpenSSL per-thread error queue being intact, so this is the
381  * earliest possible point ERR_get_error() may be called.
382  */
383  ecode = ERR_get_error();
384  switch (err)
385  {
386  case SSL_ERROR_WANT_READ:
387  case SSL_ERROR_WANT_WRITE:
388  /* not allowed during connection establishment */
389  Assert(!port->noblock);
390 
391  /*
392  * No need to care about timeouts/interrupts here. At this
393  * point authentication_timeout still employs
394  * StartupPacketTimeoutHandler() which directly exits.
395  */
396  if (err == SSL_ERROR_WANT_READ)
397  waitfor = WL_SOCKET_READABLE;
398  else
399  waitfor = WL_SOCKET_WRITEABLE;
400 
401  WaitLatchOrSocket(MyLatch, waitfor, port->sock, 0,
403  goto aloop;
404  case SSL_ERROR_SYSCALL:
405  if (r < 0)
408  errmsg("could not accept SSL connection: %m")));
409  else
411  (errcode(ERRCODE_PROTOCOL_VIOLATION),
412  errmsg("could not accept SSL connection: EOF detected")));
413  break;
414  case SSL_ERROR_SSL:
416  (errcode(ERRCODE_PROTOCOL_VIOLATION),
417  errmsg("could not accept SSL connection: %s",
418  SSLerrmessage(ecode))));
419  break;
420  case SSL_ERROR_ZERO_RETURN:
422  (errcode(ERRCODE_PROTOCOL_VIOLATION),
423  errmsg("could not accept SSL connection: EOF detected")));
424  break;
425  default:
427  (errcode(ERRCODE_PROTOCOL_VIOLATION),
428  errmsg("unrecognized SSL error code: %d",
429  err)));
430  break;
431  }
432  return -1;
433  }
434 
435  /* Get client certificate, if available. */
436  port->peer = SSL_get_peer_certificate(port->ssl);
437 
438  /* and extract the Common Name from it. */
439  port->peer_cn = NULL;
440  port->peer_cert_valid = false;
441  if (port->peer != NULL)
442  {
443  int len;
444 
445  len = X509_NAME_get_text_by_NID(X509_get_subject_name(port->peer),
446  NID_commonName, NULL, 0);
447  if (len != -1)
448  {
449  char *peer_cn;
450 
451  peer_cn = MemoryContextAlloc(TopMemoryContext, len + 1);
452  r = X509_NAME_get_text_by_NID(X509_get_subject_name(port->peer),
453  NID_commonName, peer_cn, len + 1);
454  peer_cn[len] = '\0';
455  if (r != len)
456  {
457  /* shouldn't happen */
458  pfree(peer_cn);
459  return -1;
460  }
461 
462  /*
463  * Reject embedded NULLs in certificate common name to prevent
464  * attacks like CVE-2009-4034.
465  */
466  if (len != strlen(peer_cn))
467  {
469  (errcode(ERRCODE_PROTOCOL_VIOLATION),
470  errmsg("SSL certificate's common name contains embedded null")));
471  pfree(peer_cn);
472  return -1;
473  }
474 
475  port->peer_cn = peer_cn;
476  }
477  port->peer_cert_valid = true;
478  }
479 
480  /* set up debugging/info callback */
481  SSL_CTX_set_info_callback(SSL_context, info_cb);
482 
483  return 0;
484 }
485 
486 void
488 {
489  if (port->ssl)
490  {
491  SSL_shutdown(port->ssl);
492  SSL_free(port->ssl);
493  port->ssl = NULL;
494  port->ssl_in_use = false;
495  }
496 
497  if (port->peer)
498  {
499  X509_free(port->peer);
500  port->peer = NULL;
501  }
502 
503  if (port->peer_cn)
504  {
505  pfree(port->peer_cn);
506  port->peer_cn = NULL;
507  }
508 }
509 
510 ssize_t
511 be_tls_read(Port *port, void *ptr, size_t len, int *waitfor)
512 {
513  ssize_t n;
514  int err;
515  unsigned long ecode;
516 
517  errno = 0;
518  ERR_clear_error();
519  n = SSL_read(port->ssl, ptr, len);
520  err = SSL_get_error(port->ssl, n);
521  ecode = (err != SSL_ERROR_NONE || n < 0) ? ERR_get_error() : 0;
522  switch (err)
523  {
524  case SSL_ERROR_NONE:
525  /* a-ok */
526  break;
527  case SSL_ERROR_WANT_READ:
528  *waitfor = WL_SOCKET_READABLE;
529  errno = EWOULDBLOCK;
530  n = -1;
531  break;
532  case SSL_ERROR_WANT_WRITE:
533  *waitfor = WL_SOCKET_WRITEABLE;
534  errno = EWOULDBLOCK;
535  n = -1;
536  break;
537  case SSL_ERROR_SYSCALL:
538  /* leave it to caller to ereport the value of errno */
539  if (n != -1)
540  {
541  errno = ECONNRESET;
542  n = -1;
543  }
544  break;
545  case SSL_ERROR_SSL:
547  (errcode(ERRCODE_PROTOCOL_VIOLATION),
548  errmsg("SSL error: %s", SSLerrmessage(ecode))));
549  errno = ECONNRESET;
550  n = -1;
551  break;
552  case SSL_ERROR_ZERO_RETURN:
553  /* connection was cleanly shut down by peer */
554  n = 0;
555  break;
556  default:
558  (errcode(ERRCODE_PROTOCOL_VIOLATION),
559  errmsg("unrecognized SSL error code: %d",
560  err)));
561  errno = ECONNRESET;
562  n = -1;
563  break;
564  }
565 
566  return n;
567 }
568 
569 ssize_t
570 be_tls_write(Port *port, void *ptr, size_t len, int *waitfor)
571 {
572  ssize_t n;
573  int err;
574  unsigned long ecode;
575 
576  errno = 0;
577  ERR_clear_error();
578  n = SSL_write(port->ssl, ptr, len);
579  err = SSL_get_error(port->ssl, n);
580  ecode = (err != SSL_ERROR_NONE || n < 0) ? ERR_get_error() : 0;
581  switch (err)
582  {
583  case SSL_ERROR_NONE:
584  /* a-ok */
585  break;
586  case SSL_ERROR_WANT_READ:
587  *waitfor = WL_SOCKET_READABLE;
588  errno = EWOULDBLOCK;
589  n = -1;
590  break;
591  case SSL_ERROR_WANT_WRITE:
592  *waitfor = WL_SOCKET_WRITEABLE;
593  errno = EWOULDBLOCK;
594  n = -1;
595  break;
596  case SSL_ERROR_SYSCALL:
597  /* leave it to caller to ereport the value of errno */
598  if (n != -1)
599  {
600  errno = ECONNRESET;
601  n = -1;
602  }
603  break;
604  case SSL_ERROR_SSL:
606  (errcode(ERRCODE_PROTOCOL_VIOLATION),
607  errmsg("SSL error: %s", SSLerrmessage(ecode))));
608  errno = ECONNRESET;
609  n = -1;
610  break;
611  case SSL_ERROR_ZERO_RETURN:
612 
613  /*
614  * the SSL connection was closed, leave it to the caller to
615  * ereport it
616  */
617  errno = ECONNRESET;
618  n = -1;
619  break;
620  default:
622  (errcode(ERRCODE_PROTOCOL_VIOLATION),
623  errmsg("unrecognized SSL error code: %d",
624  err)));
625  errno = ECONNRESET;
626  n = -1;
627  break;
628  }
629 
630  return n;
631 }
632 
633 /* ------------------------------------------------------------ */
634 /* Internal functions */
635 /* ------------------------------------------------------------ */
636 
637 /*
638  * Private substitute BIO: this does the sending and receiving using send() and
639  * recv() instead. This is so that we can enable and disable interrupts
640  * just while calling recv(). We cannot have interrupts occurring while
641  * the bulk of openssl runs, because it uses malloc() and possibly other
642  * non-reentrant libc facilities. We also need to call send() and recv()
643  * directly so it gets passed through the socket/signals layer on Win32.
644  *
645  * These functions are closely modelled on the standard socket BIO in OpenSSL;
646  * see sock_read() and sock_write() in OpenSSL's crypto/bio/bss_sock.c.
647  * XXX OpenSSL 1.0.1e considers many more errcodes than just EINTR as reasons
648  * to retry; do we need to adopt their logic for that?
649  */
650 
651 #ifndef HAVE_BIO_GET_DATA
652 #define BIO_get_data(bio) (bio->ptr)
653 #define BIO_set_data(bio, data) (bio->ptr = data)
654 #endif
655 
656 static BIO_METHOD *my_bio_methods = NULL;
657 
658 static int
659 my_sock_read(BIO *h, char *buf, int size)
660 {
661  int res = 0;
662 
663  if (buf != NULL)
664  {
665  res = secure_raw_read(((Port *) BIO_get_data(h)), buf, size);
666  BIO_clear_retry_flags(h);
667  if (res <= 0)
668  {
669  /* If we were interrupted, tell caller to retry */
670  if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)
671  {
672  BIO_set_retry_read(h);
673  }
674  }
675  }
676 
677  return res;
678 }
679 
680 static int
681 my_sock_write(BIO *h, const char *buf, int size)
682 {
683  int res = 0;
684 
685  res = secure_raw_write(((Port *) BIO_get_data(h)), buf, size);
686  BIO_clear_retry_flags(h);
687  if (res <= 0)
688  {
689  /* If we were interrupted, tell caller to retry */
690  if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)
691  {
692  BIO_set_retry_write(h);
693  }
694  }
695 
696  return res;
697 }
698 
699 static BIO_METHOD *
701 {
702  if (!my_bio_methods)
703  {
704  BIO_METHOD *biom = (BIO_METHOD *) BIO_s_socket();
705 #ifdef HAVE_BIO_METH_NEW
706  int my_bio_index;
707 
708  my_bio_index = BIO_get_new_index();
709  if (my_bio_index == -1)
710  return NULL;
711  my_bio_methods = BIO_meth_new(my_bio_index, "PostgreSQL backend socket");
712  if (!my_bio_methods)
713  return NULL;
714  if (!BIO_meth_set_write(my_bio_methods, my_sock_write) ||
715  !BIO_meth_set_read(my_bio_methods, my_sock_read) ||
716  !BIO_meth_set_gets(my_bio_methods, BIO_meth_get_gets(biom)) ||
717  !BIO_meth_set_puts(my_bio_methods, BIO_meth_get_puts(biom)) ||
718  !BIO_meth_set_ctrl(my_bio_methods, BIO_meth_get_ctrl(biom)) ||
719  !BIO_meth_set_create(my_bio_methods, BIO_meth_get_create(biom)) ||
720  !BIO_meth_set_destroy(my_bio_methods, BIO_meth_get_destroy(biom)) ||
721  !BIO_meth_set_callback_ctrl(my_bio_methods, BIO_meth_get_callback_ctrl(biom)))
722  {
723  BIO_meth_free(my_bio_methods);
724  my_bio_methods = NULL;
725  return NULL;
726  }
727 #else
728  my_bio_methods = malloc(sizeof(BIO_METHOD));
729  if (!my_bio_methods)
730  return NULL;
731  memcpy(my_bio_methods, biom, sizeof(BIO_METHOD));
732  my_bio_methods->bread = my_sock_read;
733  my_bio_methods->bwrite = my_sock_write;
734 #endif
735  }
736  return my_bio_methods;
737 }
738 
739 /* This should exactly match openssl's SSL_set_fd except for using my BIO */
740 static int
742 {
743  int ret = 0;
744  BIO *bio;
745  BIO_METHOD *bio_method;
746 
747  bio_method = my_BIO_s_socket();
748  if (bio_method == NULL)
749  {
750  SSLerr(SSL_F_SSL_SET_FD, ERR_R_BUF_LIB);
751  goto err;
752  }
753  bio = BIO_new(bio_method);
754 
755  if (bio == NULL)
756  {
757  SSLerr(SSL_F_SSL_SET_FD, ERR_R_BUF_LIB);
758  goto err;
759  }
760  BIO_set_data(bio, port);
761 
762  BIO_set_fd(bio, fd, BIO_NOCLOSE);
763  SSL_set_bio(port->ssl, bio, bio);
764  ret = 1;
765 err:
766  return ret;
767 }
768 
769 /*
770  * Load precomputed DH parameters.
771  *
772  * To prevent "downgrade" attacks, we perform a number of checks
773  * to verify that the DBA-generated DH parameters file contains
774  * what we expect it to contain.
775  */
776 static DH *
777 load_dh_file(char *filename, bool isServerStart)
778 {
779  FILE *fp;
780  DH *dh = NULL;
781  int codes;
782 
783  /* attempt to open file. It's not an error if it doesn't exist. */
784  if ((fp = AllocateFile(filename, "r")) == NULL)
785  {
786  ereport(isServerStart ? FATAL : LOG,
788  errmsg("could not open DH parameters file \"%s\": %m",
789  filename)));
790  return NULL;
791  }
792 
793  dh = PEM_read_DHparams(fp, NULL, NULL, NULL);
794  FreeFile(fp);
795 
796  if (dh == NULL)
797  {
798  ereport(isServerStart ? FATAL : LOG,
799  (errcode(ERRCODE_CONFIG_FILE_ERROR),
800  errmsg("could not load DH parameters file: %s",
801  SSLerrmessage(ERR_get_error()))));
802  return NULL;
803  }
804 
805  /* make sure the DH parameters are usable */
806  if (DH_check(dh, &codes) == 0)
807  {
808  ereport(isServerStart ? FATAL : LOG,
809  (errcode(ERRCODE_CONFIG_FILE_ERROR),
810  errmsg("invalid DH parameters: %s",
811  SSLerrmessage(ERR_get_error()))));
812  return NULL;
813  }
814  if (codes & DH_CHECK_P_NOT_PRIME)
815  {
816  ereport(isServerStart ? FATAL : LOG,
817  (errcode(ERRCODE_CONFIG_FILE_ERROR),
818  errmsg("invalid DH parameters: p is not prime")));
819  return NULL;
820  }
821  if ((codes & DH_NOT_SUITABLE_GENERATOR) &&
822  (codes & DH_CHECK_P_NOT_SAFE_PRIME))
823  {
824  ereport(isServerStart ? FATAL : LOG,
825  (errcode(ERRCODE_CONFIG_FILE_ERROR),
826  errmsg("invalid DH parameters: neither suitable generator or safe prime")));
827  return NULL;
828  }
829 
830  return dh;
831 }
832 
833 /*
834  * Load hardcoded DH parameters.
835  *
836  * To prevent problems if the DH parameters files don't even
837  * exist, we can load DH parameters hardcoded into this file.
838  */
839 static DH *
840 load_dh_buffer(const char *buffer, size_t len)
841 {
842  BIO *bio;
843  DH *dh = NULL;
844 
845  bio = BIO_new_mem_buf((char *) buffer, len);
846  if (bio == NULL)
847  return NULL;
848  dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
849  if (dh == NULL)
850  ereport(DEBUG2,
851  (errmsg_internal("DH load buffer: %s",
852  SSLerrmessage(ERR_get_error()))));
853  BIO_free(bio);
854 
855  return dh;
856 }
857 
858 /*
859  * Passphrase collection callback using ssl_passphrase_command
860  */
861 static int
862 ssl_external_passwd_cb(char *buf, int size, int rwflag, void *userdata)
863 {
864  /* same prompt as OpenSSL uses internally */
865  const char *prompt = "Enter PEM pass phrase:";
866 
867  Assert(rwflag == 0);
868 
869  return run_ssl_passphrase_command(prompt, ssl_is_server_start, buf, size);
870 }
871 
872 /*
873  * Dummy passphrase callback
874  *
875  * If OpenSSL is told to use a passphrase-protected server key, by default
876  * it will issue a prompt on /dev/tty and try to read a key from there.
877  * That's no good during a postmaster SIGHUP cycle, not to mention SSL context
878  * reload in an EXEC_BACKEND postmaster child. So override it with this dummy
879  * function that just returns an empty passphrase, guaranteeing failure.
880  */
881 static int
882 dummy_ssl_passwd_cb(char *buf, int size, int rwflag, void *userdata)
883 {
884  /* Set flag to change the error message we'll report */
886  /* And return empty string */
887  Assert(size > 0);
888  buf[0] = '\0';
889  return 0;
890 }
891 
892 /*
893  * Certificate verification callback
894  *
895  * This callback allows us to log intermediate problems during
896  * verification, but for now we'll see if the final error message
897  * contains enough information.
898  *
899  * This callback also allows us to override the default acceptance
900  * criteria (e.g., accepting self-signed or expired certs), but
901  * for now we accept the default checks.
902  */
903 static int
904 verify_cb(int ok, X509_STORE_CTX *ctx)
905 {
906  return ok;
907 }
908 
909 /*
910  * This callback is used to copy SSL information messages
911  * into the PostgreSQL log.
912  */
913 static void
914 info_cb(const SSL *ssl, int type, int args)
915 {
916  switch (type)
917  {
918  case SSL_CB_HANDSHAKE_START:
919  ereport(DEBUG4,
920  (errmsg_internal("SSL: handshake start")));
921  break;
922  case SSL_CB_HANDSHAKE_DONE:
923  ereport(DEBUG4,
924  (errmsg_internal("SSL: handshake done")));
925  break;
926  case SSL_CB_ACCEPT_LOOP:
927  ereport(DEBUG4,
928  (errmsg_internal("SSL: accept loop")));
929  break;
930  case SSL_CB_ACCEPT_EXIT:
931  ereport(DEBUG4,
932  (errmsg_internal("SSL: accept exit (%d)", args)));
933  break;
934  case SSL_CB_CONNECT_LOOP:
935  ereport(DEBUG4,
936  (errmsg_internal("SSL: connect loop")));
937  break;
938  case SSL_CB_CONNECT_EXIT:
939  ereport(DEBUG4,
940  (errmsg_internal("SSL: connect exit (%d)", args)));
941  break;
942  case SSL_CB_READ_ALERT:
943  ereport(DEBUG4,
944  (errmsg_internal("SSL: read alert (0x%04x)", args)));
945  break;
946  case SSL_CB_WRITE_ALERT:
947  ereport(DEBUG4,
948  (errmsg_internal("SSL: write alert (0x%04x)", args)));
949  break;
950  }
951 }
952 
953 /*
954  * Set DH parameters for generating ephemeral DH keys. The
955  * DH parameters can take a long time to compute, so they must be
956  * precomputed.
957  *
958  * Since few sites will bother to create a parameter file, we also
959  * also provide a fallback to the parameters provided by the
960  * OpenSSL project.
961  *
962  * These values can be static (once loaded or computed) since the
963  * OpenSSL library can efficiently generate random keys from the
964  * information provided.
965  */
966 static bool
967 initialize_dh(SSL_CTX *context, bool isServerStart)
968 {
969  DH *dh = NULL;
970 
971  SSL_CTX_set_options(context, SSL_OP_SINGLE_DH_USE);
972 
973  if (ssl_dh_params_file[0])
974  dh = load_dh_file(ssl_dh_params_file, isServerStart);
975  if (!dh)
976  dh = load_dh_buffer(FILE_DH2048, sizeof(FILE_DH2048));
977  if (!dh)
978  {
979  ereport(isServerStart ? FATAL : LOG,
980  (errcode(ERRCODE_CONFIG_FILE_ERROR),
981  (errmsg("DH: could not load DH parameters"))));
982  return false;
983  }
984 
985  if (SSL_CTX_set_tmp_dh(context, dh) != 1)
986  {
987  ereport(isServerStart ? FATAL : LOG,
988  (errcode(ERRCODE_CONFIG_FILE_ERROR),
989  (errmsg("DH: could not set DH parameters: %s",
990  SSLerrmessage(ERR_get_error())))));
991  return false;
992  }
993  return true;
994 }
995 
996 /*
997  * Set ECDH parameters for generating ephemeral Elliptic Curve DH
998  * keys. This is much simpler than the DH parameters, as we just
999  * need to provide the name of the curve to OpenSSL.
1000  */
1001 static bool
1002 initialize_ecdh(SSL_CTX *context, bool isServerStart)
1003 {
1004 #ifndef OPENSSL_NO_ECDH
1005  EC_KEY *ecdh;
1006  int nid;
1007 
1008  nid = OBJ_sn2nid(SSLECDHCurve);
1009  if (!nid)
1010  {
1011  ereport(isServerStart ? FATAL : LOG,
1012  (errcode(ERRCODE_CONFIG_FILE_ERROR),
1013  errmsg("ECDH: unrecognized curve name: %s", SSLECDHCurve)));
1014  return false;
1015  }
1016 
1017  ecdh = EC_KEY_new_by_curve_name(nid);
1018  if (!ecdh)
1019  {
1020  ereport(isServerStart ? FATAL : LOG,
1021  (errcode(ERRCODE_CONFIG_FILE_ERROR),
1022  errmsg("ECDH: could not create key")));
1023  return false;
1024  }
1025 
1026  SSL_CTX_set_options(context, SSL_OP_SINGLE_ECDH_USE);
1027  SSL_CTX_set_tmp_ecdh(context, ecdh);
1028  EC_KEY_free(ecdh);
1029 #endif
1030 
1031  return true;
1032 }
1033 
1034 /*
1035  * Obtain reason string for passed SSL errcode
1036  *
1037  * ERR_get_error() is used by caller to get errcode to pass here.
1038  *
1039  * Some caution is needed here since ERR_reason_error_string will
1040  * return NULL if it doesn't recognize the error code. We don't
1041  * want to return NULL ever.
1042  */
1043 static const char *
1044 SSLerrmessage(unsigned long ecode)
1045 {
1046  const char *errreason;
1047  static char errbuf[36];
1048 
1049  if (ecode == 0)
1050  return _("no SSL error reported");
1051  errreason = ERR_reason_error_string(ecode);
1052  if (errreason != NULL)
1053  return errreason;
1054  snprintf(errbuf, sizeof(errbuf), _("SSL error code %lu"), ecode);
1055  return errbuf;
1056 }
1057 
1058 int
1060 {
1061  int bits;
1062 
1063  if (port->ssl)
1064  {
1065  SSL_get_cipher_bits(port->ssl, &bits);
1066  return bits;
1067  }
1068  else
1069  return 0;
1070 }
1071 
1072 bool
1074 {
1075  if (port->ssl)
1076  return (SSL_get_current_compression(port->ssl) != NULL);
1077  else
1078  return false;
1079 }
1080 
1081 const char *
1083 {
1084  if (port->ssl)
1085  return SSL_get_version(port->ssl);
1086  else
1087  return NULL;
1088 }
1089 
1090 const char *
1092 {
1093  if (port->ssl)
1094  return SSL_get_cipher(port->ssl);
1095  else
1096  return NULL;
1097 }
1098 
1099 void
1100 be_tls_get_peerdn_name(Port *port, char *ptr, size_t len)
1101 {
1102  if (port->peer)
1103  strlcpy(ptr, X509_NAME_to_cstring(X509_get_subject_name(port->peer)), len);
1104  else
1105  ptr[0] = '\0';
1106 }
1107 
1108 char *
1110 {
1111  char dummy[1];
1112  char *result;
1113 
1114  /*
1115  * OpenSSL does not offer an API to directly get the length of the
1116  * expected TLS Finished message, so just do a dummy call to grab this
1117  * information to allow caller to do an allocation with a correct size.
1118  */
1119  *len = SSL_get_peer_finished(port->ssl, dummy, sizeof(dummy));
1120  result = palloc(*len);
1121  (void) SSL_get_peer_finished(port->ssl, result, *len);
1122 
1123  return result;
1124 }
1125 
1126 char *
1128 {
1129 #ifdef HAVE_X509_GET_SIGNATURE_NID
1130  X509 *server_cert;
1131  char *cert_hash;
1132  const EVP_MD *algo_type = NULL;
1133  unsigned char hash[EVP_MAX_MD_SIZE]; /* size for SHA-512 */
1134  unsigned int hash_size;
1135  int algo_nid;
1136 
1137  *len = 0;
1138  server_cert = SSL_get_certificate(port->ssl);
1139  if (server_cert == NULL)
1140  return NULL;
1141 
1142  /*
1143  * Get the signature algorithm of the certificate to determine the hash
1144  * algorithm to use for the result.
1145  */
1146  if (!OBJ_find_sigid_algs(X509_get_signature_nid(server_cert),
1147  &algo_nid, NULL))
1148  elog(ERROR, "could not determine server certificate signature algorithm");
1149 
1150  /*
1151  * The TLS server's certificate bytes need to be hashed with SHA-256 if
1152  * its signature algorithm is MD5 or SHA-1 as per RFC 5929
1153  * (https://tools.ietf.org/html/rfc5929#section-4.1). If something else
1154  * is used, the same hash as the signature algorithm is used.
1155  */
1156  switch (algo_nid)
1157  {
1158  case NID_md5:
1159  case NID_sha1:
1160  algo_type = EVP_sha256();
1161  break;
1162  default:
1163  algo_type = EVP_get_digestbynid(algo_nid);
1164  if (algo_type == NULL)
1165  elog(ERROR, "could not find digest for NID %s",
1166  OBJ_nid2sn(algo_nid));
1167  break;
1168  }
1169 
1170  /* generate and save the certificate hash */
1171  if (!X509_digest(server_cert, algo_type, hash, &hash_size))
1172  elog(ERROR, "could not generate server certificate hash");
1173 
1174  cert_hash = palloc(hash_size);
1175  memcpy(cert_hash, hash, hash_size);
1176  *len = hash_size;
1177 
1178  return cert_hash;
1179 #else
1180  ereport(ERROR,
1181  (errcode(ERRCODE_PROTOCOL_VIOLATION),
1182  errmsg("channel binding type \"tls-server-end-point\" is not supported by this build")));
1183  return NULL;
1184 #endif
1185 }
1186 
1187 /*
1188  * Convert an X509 subject name to a cstring.
1189  *
1190  */
1191 static char *
1193 {
1194  BIO *membuf = BIO_new(BIO_s_mem());
1195  int i,
1196  nid,
1197  count = X509_NAME_entry_count(name);
1198  X509_NAME_ENTRY *e;
1199  ASN1_STRING *v;
1200  const char *field_name;
1201  size_t size;
1202  char nullterm;
1203  char *sp;
1204  char *dp;
1205  char *result;
1206 
1207  (void) BIO_set_close(membuf, BIO_CLOSE);
1208  for (i = 0; i < count; i++)
1209  {
1210  e = X509_NAME_get_entry(name, i);
1211  nid = OBJ_obj2nid(X509_NAME_ENTRY_get_object(e));
1212  v = X509_NAME_ENTRY_get_data(e);
1213  field_name = OBJ_nid2sn(nid);
1214  if (!field_name)
1215  field_name = OBJ_nid2ln(nid);
1216  BIO_printf(membuf, "/%s=", field_name);
1217  ASN1_STRING_print_ex(membuf, v,
1218  ((ASN1_STRFLGS_RFC2253 & ~ASN1_STRFLGS_ESC_MSB)
1219  | ASN1_STRFLGS_UTF8_CONVERT));
1220  }
1221 
1222  /* ensure null termination of the BIO's content */
1223  nullterm = '\0';
1224  BIO_write(membuf, &nullterm, 1);
1225  size = BIO_get_mem_data(membuf, &sp);
1226  dp = pg_any_to_server(sp, size - 1, PG_UTF8);
1227 
1228  result = pstrdup(dp);
1229  if (dp != sp)
1230  pfree(dp);
1231  BIO_free(membuf);
1232 
1233  return result;
1234 }
ssize_t be_tls_read(Port *port, void *ptr, size_t len, int *waitfor)
#define WL_SOCKET_WRITEABLE
Definition: latch.h:126
#define BIO_set_data(bio, data)
static void error(void)
Definition: sql-dyntest.c:147
static bool dummy_ssl_passwd_cb_called
#define EAGAIN
Definition: win32_port.h:332
static BIO_METHOD * my_BIO_s_socket(void)
static DH * load_dh_file(char *filename, bool isServerStart)
char * peer_cn
Definition: libpq-be.h:182
char * be_tls_get_certificate_hash(Port *port, size_t *len)
#define BIO_get_data(bio)
char * pstrdup(const char *in)
Definition: mcxt.c:1161
bool ssl_loaded_verify_locations
bool peer_cert_valid
Definition: libpq-be.h:183
ssize_t secure_raw_write(Port *port, const void *ptr, size_t len)
Definition: be-secure.c:307
static void info_cb(const SSL *ssl, int type, int args)
char * be_tls_get_peer_finished(Port *port, size_t *len)
int errcode(int sqlerrcode)
Definition: elog.c:575
Definition: libpq-be.h:116
bool ssl_in_use
Definition: libpq-be.h:181
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define WL_SOCKET_READABLE
Definition: latch.h:125
char * ssl_cert_file
Definition: be-secure.c:41
static bool initialize_ecdh(SSL_CTX *context, bool isServerStart)
#define LOG
Definition: elog.h:26
#define DEBUG4
Definition: elog.h:22
char * ssl_crl_file
Definition: be-secure.c:44
static int fd(const char *x, int i)
Definition: preproc-init.c:105
pgsocket sock
Definition: libpq-be.h:118
int be_tls_init(bool isServerStart)
#define malloc(a)
Definition: header.h:50
void pfree(void *pointer)
Definition: mcxt.c:1031
static int my_SSL_set_fd(Port *port, int fd)
#define ERROR
Definition: elog.h:43
bool be_tls_get_compression(Port *port)
#define FATAL
Definition: elog.h:52
char * SSLECDHCurve
Definition: be-secure.c:57
#define DEBUG2
Definition: elog.h:24
ssize_t be_tls_write(Port *port, void *ptr, size_t len, int *waitfor)
static char * buf
Definition: pg_test_fsync.c:67
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define COMMERROR
Definition: elog.h:30
int errcode_for_file_access(void)
Definition: elog.c:598
static bool SSL_initialized
char * SSLCipherSuites
Definition: be-secure.c:54
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2336
static int verify_cb(int, X509_STORE_CTX *)
static DH * load_dh_buffer(const char *, size_t)
void be_tls_destroy(void)
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:44
static char * X509_NAME_to_cstring(X509_NAME *name)
bool ssl_passphrase_command_supports_reload
Definition: be-secure.c:47
int errcode_for_socket_access(void)
Definition: elog.c:669
static int port
Definition: pg_regress.c:90
static int ssl_external_passwd_cb(char *buf, int size, int rwflag, void *userdata)
#define SSL_get_current_compression(x)
Definition: port.h:400
int be_tls_get_cipher_bits(Port *port)
static bool ssl_is_server_start
static bool initialize_dh(SSL_CTX *context, bool isServerStart)
char * ssl_ca_file
Definition: be-secure.c:43
int be_tls_open_server(Port *port)
void be_tls_get_peerdn_name(Port *port, char *ptr, size_t len)
ssize_t secure_raw_read(Port *port, void *ptr, size_t len)
Definition: be-secure.c:221
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
static BIO_METHOD * my_bio_methods
#define Assert(condition)
Definition: c.h:699
int WaitLatchOrSocket(volatile Latch *latch, int wakeEvents, pgsocket sock, long timeout, uint32 wait_event_info)
Definition: latch.c:356
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:215
static int my_sock_read(BIO *h, char *buf, int size)
bool noblock
Definition: libpq-be.h:119
#define ECONNRESET
Definition: win32_port.h:344
char * ssl_dh_params_file
Definition: be-secure.c:45
const char * name
Definition: encode.c:521
const char * be_tls_get_version(Port *port)
int FreeFile(FILE *file)
Definition: fd.c:2528
static char * filename
Definition: pg_dumpall.c:87
e
Definition: preproc-init.c:82
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797
static SSL_CTX * SSL_context
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:771
static int my_sock_write(BIO *h, const char *buf, int size)
int i
static const char * SSLerrmessage(unsigned long ecode)
void be_tls_close(Port *port)
struct Latch * MyLatch
Definition: globals.c:53
const char * be_tls_get_cipher(Port *port)
#define EWOULDBLOCK
Definition: win32_port.h:340
int run_ssl_passphrase_command(const char *prompt, bool is_server_start, char *buf, int size)
char * ssl_passphrase_command
Definition: be-secure.c:46
#define elog
Definition: elog.h:219
#define EINTR
Definition: win32_port.h:334
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:561
static int dummy_ssl_passwd_cb(char *buf, int size, int rwflag, void *userdata)
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:541
#define _(x)
Definition: elog.c:84
char * ssl_key_file
Definition: be-secure.c:42
bool SSLPreferServerCiphers
Definition: be-secure.c:60
bool check_ssl_key_file_permissions(const char *ssl_key_file, bool isServerStart)