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-2021, 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 "common/openssl.h"
40 #include "libpq/libpq.h"
41 #include "miscadmin.h"
42 #include "pgstat.h"
43 #include "storage/fd.h"
44 #include "storage/latch.h"
45 #include "tcop/tcopprot.h"
46 #include "utils/memutils.h"
47 
48 /* default init hook can be overridden by a shared library */
49 static void default_openssl_tls_init(SSL_CTX *context, bool isServerStart);
50 openssl_tls_init_hook_typ openssl_tls_init_hook = default_openssl_tls_init;
51 
52 static int my_sock_read(BIO *h, char *buf, int size);
53 static int my_sock_write(BIO *h, const char *buf, int size);
54 static BIO_METHOD *my_BIO_s_socket(void);
55 static int my_SSL_set_fd(Port *port, int fd);
56 
57 static DH *load_dh_file(char *filename, bool isServerStart);
58 static DH *load_dh_buffer(const char *, size_t);
59 static int ssl_external_passwd_cb(char *buf, int size, int rwflag, void *userdata);
60 static int dummy_ssl_passwd_cb(char *buf, int size, int rwflag, void *userdata);
61 static int verify_cb(int, X509_STORE_CTX *);
62 static void info_cb(const SSL *ssl, int type, int args);
63 static bool initialize_dh(SSL_CTX *context, bool isServerStart);
64 static bool initialize_ecdh(SSL_CTX *context, bool isServerStart);
65 static const char *SSLerrmessage(unsigned long ecode);
66 
67 static char *X509_NAME_to_cstring(X509_NAME *name);
68 
69 static SSL_CTX *SSL_context = NULL;
70 static bool SSL_initialized = false;
71 static bool dummy_ssl_passwd_cb_called = false;
72 static bool ssl_is_server_start;
73 
74 static int ssl_protocol_version_to_openssl(int v);
75 static const char *ssl_protocol_version_to_string(int v);
76 
77 /* ------------------------------------------------------------ */
78 /* Public interface */
79 /* ------------------------------------------------------------ */
80 
81 int
82 be_tls_init(bool isServerStart)
83 {
84  STACK_OF(X509_NAME) * root_cert_list = NULL;
85  SSL_CTX *context;
86  int ssl_ver_min = -1;
87  int ssl_ver_max = -1;
88 
89  /* This stuff need be done only once. */
90  if (!SSL_initialized)
91  {
92 #ifdef HAVE_OPENSSL_INIT_SSL
93  OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL);
94 #else
95  OPENSSL_config(NULL);
96  SSL_library_init();
97  SSL_load_error_strings();
98 #endif
99  SSL_initialized = true;
100  }
101 
102  /*
103  * We use SSLv23_method() because it can negotiate use of the highest
104  * mutually supported protocol version, while alternatives like
105  * TLSv1_2_method() permit only one specific version. Note that we don't
106  * actually allow SSL v2 or v3, only TLS protocols (see below).
107  */
108  context = SSL_CTX_new(SSLv23_method());
109  if (!context)
110  {
111  ereport(isServerStart ? FATAL : LOG,
112  (errmsg("could not create SSL context: %s",
113  SSLerrmessage(ERR_get_error()))));
114  goto error;
115  }
116 
117  /*
118  * Disable OpenSSL's moving-write-buffer sanity check, because it causes
119  * unnecessary failures in nonblocking send cases.
120  */
121  SSL_CTX_set_mode(context, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
122 
123  /*
124  * Call init hook (usually to set password callback)
125  */
126  (*openssl_tls_init_hook) (context, isServerStart);
127 
128  /* used by the callback */
129  ssl_is_server_start = isServerStart;
130 
131  /*
132  * Load and verify server's certificate and private key
133  */
134  if (SSL_CTX_use_certificate_chain_file(context, ssl_cert_file) != 1)
135  {
136  ereport(isServerStart ? FATAL : LOG,
137  (errcode(ERRCODE_CONFIG_FILE_ERROR),
138  errmsg("could not load server certificate file \"%s\": %s",
139  ssl_cert_file, SSLerrmessage(ERR_get_error()))));
140  goto error;
141  }
142 
143  if (!check_ssl_key_file_permissions(ssl_key_file, isServerStart))
144  goto error;
145 
146  /*
147  * OK, try to load the private key file.
148  */
150 
151  if (SSL_CTX_use_PrivateKey_file(context,
152  ssl_key_file,
153  SSL_FILETYPE_PEM) != 1)
154  {
156  ereport(isServerStart ? FATAL : LOG,
157  (errcode(ERRCODE_CONFIG_FILE_ERROR),
158  errmsg("private key file \"%s\" cannot be reloaded because it requires a passphrase",
159  ssl_key_file)));
160  else
161  ereport(isServerStart ? FATAL : LOG,
162  (errcode(ERRCODE_CONFIG_FILE_ERROR),
163  errmsg("could not load private key file \"%s\": %s",
164  ssl_key_file, SSLerrmessage(ERR_get_error()))));
165  goto error;
166  }
167 
168  if (SSL_CTX_check_private_key(context) != 1)
169  {
170  ereport(isServerStart ? FATAL : LOG,
171  (errcode(ERRCODE_CONFIG_FILE_ERROR),
172  errmsg("check of private key failed: %s",
173  SSLerrmessage(ERR_get_error()))));
174  goto error;
175  }
176 
178  {
180 
181  if (ssl_ver_min == -1)
182  {
183  ereport(isServerStart ? FATAL : LOG,
184  /*- translator: first %s is a GUC option name, second %s is its value */
185  (errmsg("\"%s\" setting \"%s\" not supported by this build",
186  "ssl_min_protocol_version",
187  GetConfigOption("ssl_min_protocol_version",
188  false, false))));
189  goto error;
190  }
191 
192  if (!SSL_CTX_set_min_proto_version(context, ssl_ver_min))
193  {
194  ereport(isServerStart ? FATAL : LOG,
195  (errmsg("could not set minimum SSL protocol version")));
196  goto error;
197  }
198  }
199 
201  {
203 
204  if (ssl_ver_max == -1)
205  {
206  ereport(isServerStart ? FATAL : LOG,
207  /*- translator: first %s is a GUC option name, second %s is its value */
208  (errmsg("\"%s\" setting \"%s\" not supported by this build",
209  "ssl_max_protocol_version",
210  GetConfigOption("ssl_max_protocol_version",
211  false, false))));
212  goto error;
213  }
214 
215  if (!SSL_CTX_set_max_proto_version(context, ssl_ver_max))
216  {
217  ereport(isServerStart ? FATAL : LOG,
218  (errmsg("could not set maximum SSL protocol version")));
219  goto error;
220  }
221  }
222 
223  /* Check compatibility of min/max protocols */
226  {
227  /*
228  * No need to check for invalid values (-1) for each protocol number
229  * as the code above would have already generated an error.
230  */
231  if (ssl_ver_min > ssl_ver_max)
232  {
233  ereport(isServerStart ? FATAL : LOG,
234  (errmsg("could not set SSL protocol version range"),
235  errdetail("\"%s\" cannot be higher than \"%s\"",
236  "ssl_min_protocol_version",
237  "ssl_max_protocol_version")));
238  goto error;
239  }
240  }
241 
242  /* disallow SSL session tickets */
243  SSL_CTX_set_options(context, SSL_OP_NO_TICKET);
244 
245  /* disallow SSL session caching, too */
246  SSL_CTX_set_session_cache_mode(context, SSL_SESS_CACHE_OFF);
247 
248  /* set up ephemeral DH and ECDH keys */
249  if (!initialize_dh(context, isServerStart))
250  goto error;
251  if (!initialize_ecdh(context, isServerStart))
252  goto error;
253 
254  /* set up the allowed cipher list */
255  if (SSL_CTX_set_cipher_list(context, SSLCipherSuites) != 1)
256  {
257  ereport(isServerStart ? FATAL : LOG,
258  (errcode(ERRCODE_CONFIG_FILE_ERROR),
259  errmsg("could not set the cipher list (no valid ciphers available)")));
260  goto error;
261  }
262 
263  /* Let server choose order */
265  SSL_CTX_set_options(context, SSL_OP_CIPHER_SERVER_PREFERENCE);
266 
267  /*
268  * Load CA store, so we can verify client certificates if needed.
269  */
270  if (ssl_ca_file[0])
271  {
272  if (SSL_CTX_load_verify_locations(context, ssl_ca_file, NULL) != 1 ||
273  (root_cert_list = SSL_load_client_CA_file(ssl_ca_file)) == NULL)
274  {
275  ereport(isServerStart ? FATAL : LOG,
276  (errcode(ERRCODE_CONFIG_FILE_ERROR),
277  errmsg("could not load root certificate file \"%s\": %s",
278  ssl_ca_file, SSLerrmessage(ERR_get_error()))));
279  goto error;
280  }
281  }
282 
283  /*----------
284  * Load the Certificate Revocation List (CRL).
285  * http://searchsecurity.techtarget.com/sDefinition/0,,sid14_gci803160,00.html
286  *----------
287  */
288  if (ssl_crl_file[0] || ssl_crl_dir[0])
289  {
290  X509_STORE *cvstore = SSL_CTX_get_cert_store(context);
291 
292  if (cvstore)
293  {
294  /* Set the flags to check against the complete CRL chain */
295  if (X509_STORE_load_locations(cvstore,
296  ssl_crl_file[0] ? ssl_crl_file : NULL,
297  ssl_crl_dir[0] ? ssl_crl_dir : NULL)
298  == 1)
299  {
300  X509_STORE_set_flags(cvstore,
301  X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
302  }
303  else if (ssl_crl_dir[0] == 0)
304  {
305  ereport(isServerStart ? FATAL : LOG,
306  (errcode(ERRCODE_CONFIG_FILE_ERROR),
307  errmsg("could not load SSL certificate revocation list file \"%s\": %s",
308  ssl_crl_file, SSLerrmessage(ERR_get_error()))));
309  goto error;
310  }
311  else if (ssl_crl_file[0] == 0)
312  {
313  ereport(isServerStart ? FATAL : LOG,
314  (errcode(ERRCODE_CONFIG_FILE_ERROR),
315  errmsg("could not load SSL certificate revocation list directory \"%s\": %s",
316  ssl_crl_dir, SSLerrmessage(ERR_get_error()))));
317  goto error;
318  }
319  else
320  {
321  ereport(isServerStart ? FATAL : LOG,
322  (errcode(ERRCODE_CONFIG_FILE_ERROR),
323  errmsg("could not load SSL certificate revocation list file \"%s\" or directory \"%s\": %s",
325  SSLerrmessage(ERR_get_error()))));
326  goto error;
327  }
328  }
329  }
330 
331  if (ssl_ca_file[0])
332  {
333  /*
334  * Always ask for SSL client cert, but don't fail if it's not
335  * presented. We might fail such connections later, depending on what
336  * we find in pg_hba.conf.
337  */
338  SSL_CTX_set_verify(context,
339  (SSL_VERIFY_PEER |
340  SSL_VERIFY_CLIENT_ONCE),
341  verify_cb);
342 
343  /*
344  * Tell OpenSSL to send the list of root certs we trust to clients in
345  * CertificateRequests. This lets a client with a keystore select the
346  * appropriate client certificate to send to us.
347  */
348  SSL_CTX_set_client_CA_list(context, root_cert_list);
349  }
350 
351  /*
352  * Success! Replace any existing SSL_context.
353  */
354  if (SSL_context)
355  SSL_CTX_free(SSL_context);
356 
357  SSL_context = context;
358 
359  /*
360  * Set flag to remember whether CA store has been loaded into SSL_context.
361  */
362  if (ssl_ca_file[0])
363  ssl_loaded_verify_locations = true;
364  else
365  ssl_loaded_verify_locations = false;
366 
367  return 0;
368 
369 error:
370  if (context)
371  SSL_CTX_free(context);
372  return -1;
373 }
374 
375 void
377 {
378  if (SSL_context)
379  SSL_CTX_free(SSL_context);
380  SSL_context = NULL;
381  ssl_loaded_verify_locations = false;
382 }
383 
384 int
386 {
387  int r;
388  int err;
389  int waitfor;
390  unsigned long ecode;
391  bool give_proto_hint;
392 
393  Assert(!port->ssl);
394  Assert(!port->peer);
395 
396  if (!SSL_context)
397  {
399  (errcode(ERRCODE_PROTOCOL_VIOLATION),
400  errmsg("could not initialize SSL connection: SSL context not set up")));
401  return -1;
402  }
403 
404  /* set up debugging/info callback */
405  SSL_CTX_set_info_callback(SSL_context, info_cb);
406 
407  if (!(port->ssl = SSL_new(SSL_context)))
408  {
410  (errcode(ERRCODE_PROTOCOL_VIOLATION),
411  errmsg("could not initialize SSL connection: %s",
412  SSLerrmessage(ERR_get_error()))));
413  return -1;
414  }
415  if (!my_SSL_set_fd(port, port->sock))
416  {
418  (errcode(ERRCODE_PROTOCOL_VIOLATION),
419  errmsg("could not set SSL socket: %s",
420  SSLerrmessage(ERR_get_error()))));
421  return -1;
422  }
423  port->ssl_in_use = true;
424 
425 aloop:
426 
427  /*
428  * Prepare to call SSL_get_error() by clearing thread's OpenSSL error
429  * queue. In general, the current thread's error queue must be empty
430  * before the TLS/SSL I/O operation is attempted, or SSL_get_error() will
431  * not work reliably. An extension may have failed to clear the
432  * per-thread error queue following another call to an OpenSSL I/O
433  * routine.
434  */
435  ERR_clear_error();
436  r = SSL_accept(port->ssl);
437  if (r <= 0)
438  {
439  err = SSL_get_error(port->ssl, r);
440 
441  /*
442  * Other clients of OpenSSL in the backend may fail to call
443  * ERR_get_error(), but we always do, so as to not cause problems for
444  * OpenSSL clients that don't call ERR_clear_error() defensively. Be
445  * sure that this happens by calling now. SSL_get_error() relies on
446  * the OpenSSL per-thread error queue being intact, so this is the
447  * earliest possible point ERR_get_error() may be called.
448  */
449  ecode = ERR_get_error();
450  switch (err)
451  {
452  case SSL_ERROR_WANT_READ:
453  case SSL_ERROR_WANT_WRITE:
454  /* not allowed during connection establishment */
455  Assert(!port->noblock);
456 
457  /*
458  * No need to care about timeouts/interrupts here. At this
459  * point authentication_timeout still employs
460  * StartupPacketTimeoutHandler() which directly exits.
461  */
462  if (err == SSL_ERROR_WANT_READ)
464  else
466 
467  (void) WaitLatchOrSocket(MyLatch, waitfor, port->sock, 0,
469  goto aloop;
470  case SSL_ERROR_SYSCALL:
471  if (r < 0)
474  errmsg("could not accept SSL connection: %m")));
475  else
477  (errcode(ERRCODE_PROTOCOL_VIOLATION),
478  errmsg("could not accept SSL connection: EOF detected")));
479  break;
480  case SSL_ERROR_SSL:
481  switch (ERR_GET_REASON(ecode))
482  {
483  /*
484  * UNSUPPORTED_PROTOCOL, WRONG_VERSION_NUMBER, and
485  * TLSV1_ALERT_PROTOCOL_VERSION have been observed
486  * when trying to communicate with an old OpenSSL
487  * library, or when the client and server specify
488  * disjoint protocol ranges. NO_PROTOCOLS_AVAILABLE
489  * occurs if there's a local misconfiguration (which
490  * can happen despite our checks, if openssl.cnf
491  * injects a limit we didn't account for). It's not
492  * very clear what would make OpenSSL return the other
493  * codes listed here, but a hint about protocol
494  * versions seems like it's appropriate for all.
495  */
496  case SSL_R_NO_PROTOCOLS_AVAILABLE:
497  case SSL_R_UNSUPPORTED_PROTOCOL:
498  case SSL_R_BAD_PROTOCOL_VERSION_NUMBER:
499  case SSL_R_UNKNOWN_PROTOCOL:
500  case SSL_R_UNKNOWN_SSL_VERSION:
501  case SSL_R_UNSUPPORTED_SSL_VERSION:
502  case SSL_R_WRONG_SSL_VERSION:
503  case SSL_R_WRONG_VERSION_NUMBER:
504  case SSL_R_TLSV1_ALERT_PROTOCOL_VERSION:
505 #ifdef SSL_R_VERSION_TOO_HIGH
506  case SSL_R_VERSION_TOO_HIGH:
507  case SSL_R_VERSION_TOO_LOW:
508 #endif
509  give_proto_hint = true;
510  break;
511  default:
512  give_proto_hint = false;
513  break;
514  }
516  (errcode(ERRCODE_PROTOCOL_VIOLATION),
517  errmsg("could not accept SSL connection: %s",
518  SSLerrmessage(ecode)),
519  give_proto_hint ?
520  errhint("This may indicate that the client does not support any SSL protocol version between %s and %s.",
523  MIN_OPENSSL_TLS_VERSION,
526  MAX_OPENSSL_TLS_VERSION) : 0));
527  break;
528  case SSL_ERROR_ZERO_RETURN:
530  (errcode(ERRCODE_PROTOCOL_VIOLATION),
531  errmsg("could not accept SSL connection: EOF detected")));
532  break;
533  default:
535  (errcode(ERRCODE_PROTOCOL_VIOLATION),
536  errmsg("unrecognized SSL error code: %d",
537  err)));
538  break;
539  }
540  return -1;
541  }
542 
543  /* Get client certificate, if available. */
544  port->peer = SSL_get_peer_certificate(port->ssl);
545 
546  /* and extract the Common Name from it. */
547  port->peer_cn = NULL;
548  port->peer_cert_valid = false;
549  if (port->peer != NULL)
550  {
551  int len;
552 
553  len = X509_NAME_get_text_by_NID(X509_get_subject_name(port->peer),
554  NID_commonName, NULL, 0);
555  if (len != -1)
556  {
557  char *peer_cn;
558 
559  peer_cn = MemoryContextAlloc(TopMemoryContext, len + 1);
560  r = X509_NAME_get_text_by_NID(X509_get_subject_name(port->peer),
561  NID_commonName, peer_cn, len + 1);
562  peer_cn[len] = '\0';
563  if (r != len)
564  {
565  /* shouldn't happen */
566  pfree(peer_cn);
567  return -1;
568  }
569 
570  /*
571  * Reject embedded NULLs in certificate common name to prevent
572  * attacks like CVE-2009-4034.
573  */
574  if (len != strlen(peer_cn))
575  {
577  (errcode(ERRCODE_PROTOCOL_VIOLATION),
578  errmsg("SSL certificate's common name contains embedded null")));
579  pfree(peer_cn);
580  return -1;
581  }
582 
583  port->peer_cn = peer_cn;
584  }
585  port->peer_cert_valid = true;
586  }
587 
588  return 0;
589 }
590 
591 void
593 {
594  if (port->ssl)
595  {
596  SSL_shutdown(port->ssl);
597  SSL_free(port->ssl);
598  port->ssl = NULL;
599  port->ssl_in_use = false;
600  }
601 
602  if (port->peer)
603  {
604  X509_free(port->peer);
605  port->peer = NULL;
606  }
607 
608  if (port->peer_cn)
609  {
610  pfree(port->peer_cn);
611  port->peer_cn = NULL;
612  }
613 }
614 
615 ssize_t
616 be_tls_read(Port *port, void *ptr, size_t len, int *waitfor)
617 {
618  ssize_t n;
619  int err;
620  unsigned long ecode;
621 
622  errno = 0;
623  ERR_clear_error();
624  n = SSL_read(port->ssl, ptr, len);
625  err = SSL_get_error(port->ssl, n);
626  ecode = (err != SSL_ERROR_NONE || n < 0) ? ERR_get_error() : 0;
627  switch (err)
628  {
629  case SSL_ERROR_NONE:
630  /* a-ok */
631  break;
632  case SSL_ERROR_WANT_READ:
633  *waitfor = WL_SOCKET_READABLE;
634  errno = EWOULDBLOCK;
635  n = -1;
636  break;
637  case SSL_ERROR_WANT_WRITE:
638  *waitfor = WL_SOCKET_WRITEABLE;
639  errno = EWOULDBLOCK;
640  n = -1;
641  break;
642  case SSL_ERROR_SYSCALL:
643  /* leave it to caller to ereport the value of errno */
644  if (n != -1)
645  {
646  errno = ECONNRESET;
647  n = -1;
648  }
649  break;
650  case SSL_ERROR_SSL:
652  (errcode(ERRCODE_PROTOCOL_VIOLATION),
653  errmsg("SSL error: %s", SSLerrmessage(ecode))));
654  errno = ECONNRESET;
655  n = -1;
656  break;
657  case SSL_ERROR_ZERO_RETURN:
658  /* connection was cleanly shut down by peer */
659  n = 0;
660  break;
661  default:
663  (errcode(ERRCODE_PROTOCOL_VIOLATION),
664  errmsg("unrecognized SSL error code: %d",
665  err)));
666  errno = ECONNRESET;
667  n = -1;
668  break;
669  }
670 
671  return n;
672 }
673 
674 ssize_t
675 be_tls_write(Port *port, void *ptr, size_t len, int *waitfor)
676 {
677  ssize_t n;
678  int err;
679  unsigned long ecode;
680 
681  errno = 0;
682  ERR_clear_error();
683  n = SSL_write(port->ssl, ptr, len);
684  err = SSL_get_error(port->ssl, n);
685  ecode = (err != SSL_ERROR_NONE || n < 0) ? ERR_get_error() : 0;
686  switch (err)
687  {
688  case SSL_ERROR_NONE:
689  /* a-ok */
690  break;
691  case SSL_ERROR_WANT_READ:
692  *waitfor = WL_SOCKET_READABLE;
693  errno = EWOULDBLOCK;
694  n = -1;
695  break;
696  case SSL_ERROR_WANT_WRITE:
697  *waitfor = WL_SOCKET_WRITEABLE;
698  errno = EWOULDBLOCK;
699  n = -1;
700  break;
701  case SSL_ERROR_SYSCALL:
702  /* leave it to caller to ereport the value of errno */
703  if (n != -1)
704  {
705  errno = ECONNRESET;
706  n = -1;
707  }
708  break;
709  case SSL_ERROR_SSL:
711  (errcode(ERRCODE_PROTOCOL_VIOLATION),
712  errmsg("SSL error: %s", SSLerrmessage(ecode))));
713  errno = ECONNRESET;
714  n = -1;
715  break;
716  case SSL_ERROR_ZERO_RETURN:
717 
718  /*
719  * the SSL connection was closed, leave it to the caller to
720  * ereport it
721  */
722  errno = ECONNRESET;
723  n = -1;
724  break;
725  default:
727  (errcode(ERRCODE_PROTOCOL_VIOLATION),
728  errmsg("unrecognized SSL error code: %d",
729  err)));
730  errno = ECONNRESET;
731  n = -1;
732  break;
733  }
734 
735  return n;
736 }
737 
738 /* ------------------------------------------------------------ */
739 /* Internal functions */
740 /* ------------------------------------------------------------ */
741 
742 /*
743  * Private substitute BIO: this does the sending and receiving using send() and
744  * recv() instead. This is so that we can enable and disable interrupts
745  * just while calling recv(). We cannot have interrupts occurring while
746  * the bulk of OpenSSL runs, because it uses malloc() and possibly other
747  * non-reentrant libc facilities. We also need to call send() and recv()
748  * directly so it gets passed through the socket/signals layer on Win32.
749  *
750  * These functions are closely modelled on the standard socket BIO in OpenSSL;
751  * see sock_read() and sock_write() in OpenSSL's crypto/bio/bss_sock.c.
752  * XXX OpenSSL 1.0.1e considers many more errcodes than just EINTR as reasons
753  * to retry; do we need to adopt their logic for that?
754  */
755 
756 #ifndef HAVE_BIO_GET_DATA
757 #define BIO_get_data(bio) (bio->ptr)
758 #define BIO_set_data(bio, data) (bio->ptr = data)
759 #endif
760 
761 static BIO_METHOD *my_bio_methods = NULL;
762 
763 static int
764 my_sock_read(BIO *h, char *buf, int size)
765 {
766  int res = 0;
767 
768  if (buf != NULL)
769  {
770  res = secure_raw_read(((Port *) BIO_get_data(h)), buf, size);
771  BIO_clear_retry_flags(h);
772  if (res <= 0)
773  {
774  /* If we were interrupted, tell caller to retry */
775  if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)
776  {
777  BIO_set_retry_read(h);
778  }
779  }
780  }
781 
782  return res;
783 }
784 
785 static int
786 my_sock_write(BIO *h, const char *buf, int size)
787 {
788  int res = 0;
789 
790  res = secure_raw_write(((Port *) BIO_get_data(h)), buf, size);
791  BIO_clear_retry_flags(h);
792  if (res <= 0)
793  {
794  /* If we were interrupted, tell caller to retry */
795  if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)
796  {
797  BIO_set_retry_write(h);
798  }
799  }
800 
801  return res;
802 }
803 
804 static BIO_METHOD *
806 {
807  if (!my_bio_methods)
808  {
809  BIO_METHOD *biom = (BIO_METHOD *) BIO_s_socket();
810 #ifdef HAVE_BIO_METH_NEW
811  int my_bio_index;
812 
813  my_bio_index = BIO_get_new_index();
814  if (my_bio_index == -1)
815  return NULL;
816  my_bio_methods = BIO_meth_new(my_bio_index, "PostgreSQL backend socket");
817  if (!my_bio_methods)
818  return NULL;
819  if (!BIO_meth_set_write(my_bio_methods, my_sock_write) ||
820  !BIO_meth_set_read(my_bio_methods, my_sock_read) ||
821  !BIO_meth_set_gets(my_bio_methods, BIO_meth_get_gets(biom)) ||
822  !BIO_meth_set_puts(my_bio_methods, BIO_meth_get_puts(biom)) ||
823  !BIO_meth_set_ctrl(my_bio_methods, BIO_meth_get_ctrl(biom)) ||
824  !BIO_meth_set_create(my_bio_methods, BIO_meth_get_create(biom)) ||
825  !BIO_meth_set_destroy(my_bio_methods, BIO_meth_get_destroy(biom)) ||
826  !BIO_meth_set_callback_ctrl(my_bio_methods, BIO_meth_get_callback_ctrl(biom)))
827  {
828  BIO_meth_free(my_bio_methods);
829  my_bio_methods = NULL;
830  return NULL;
831  }
832 #else
833  my_bio_methods = malloc(sizeof(BIO_METHOD));
834  if (!my_bio_methods)
835  return NULL;
836  memcpy(my_bio_methods, biom, sizeof(BIO_METHOD));
837  my_bio_methods->bread = my_sock_read;
838  my_bio_methods->bwrite = my_sock_write;
839 #endif
840  }
841  return my_bio_methods;
842 }
843 
844 /* This should exactly match OpenSSL's SSL_set_fd except for using my BIO */
845 static int
847 {
848  int ret = 0;
849  BIO *bio;
850  BIO_METHOD *bio_method;
851 
852  bio_method = my_BIO_s_socket();
853  if (bio_method == NULL)
854  {
855  SSLerr(SSL_F_SSL_SET_FD, ERR_R_BUF_LIB);
856  goto err;
857  }
858  bio = BIO_new(bio_method);
859 
860  if (bio == NULL)
861  {
862  SSLerr(SSL_F_SSL_SET_FD, ERR_R_BUF_LIB);
863  goto err;
864  }
865  BIO_set_data(bio, port);
866 
867  BIO_set_fd(bio, fd, BIO_NOCLOSE);
868  SSL_set_bio(port->ssl, bio, bio);
869  ret = 1;
870 err:
871  return ret;
872 }
873 
874 /*
875  * Load precomputed DH parameters.
876  *
877  * To prevent "downgrade" attacks, we perform a number of checks
878  * to verify that the DBA-generated DH parameters file contains
879  * what we expect it to contain.
880  */
881 static DH *
882 load_dh_file(char *filename, bool isServerStart)
883 {
884  FILE *fp;
885  DH *dh = NULL;
886  int codes;
887 
888  /* attempt to open file. It's not an error if it doesn't exist. */
889  if ((fp = AllocateFile(filename, "r")) == NULL)
890  {
891  ereport(isServerStart ? FATAL : LOG,
893  errmsg("could not open DH parameters file \"%s\": %m",
894  filename)));
895  return NULL;
896  }
897 
898  dh = PEM_read_DHparams(fp, NULL, NULL, NULL);
899  FreeFile(fp);
900 
901  if (dh == NULL)
902  {
903  ereport(isServerStart ? FATAL : LOG,
904  (errcode(ERRCODE_CONFIG_FILE_ERROR),
905  errmsg("could not load DH parameters file: %s",
906  SSLerrmessage(ERR_get_error()))));
907  return NULL;
908  }
909 
910  /* make sure the DH parameters are usable */
911  if (DH_check(dh, &codes) == 0)
912  {
913  ereport(isServerStart ? FATAL : LOG,
914  (errcode(ERRCODE_CONFIG_FILE_ERROR),
915  errmsg("invalid DH parameters: %s",
916  SSLerrmessage(ERR_get_error()))));
917  return NULL;
918  }
919  if (codes & DH_CHECK_P_NOT_PRIME)
920  {
921  ereport(isServerStart ? FATAL : LOG,
922  (errcode(ERRCODE_CONFIG_FILE_ERROR),
923  errmsg("invalid DH parameters: p is not prime")));
924  return NULL;
925  }
926  if ((codes & DH_NOT_SUITABLE_GENERATOR) &&
927  (codes & DH_CHECK_P_NOT_SAFE_PRIME))
928  {
929  ereport(isServerStart ? FATAL : LOG,
930  (errcode(ERRCODE_CONFIG_FILE_ERROR),
931  errmsg("invalid DH parameters: neither suitable generator or safe prime")));
932  return NULL;
933  }
934 
935  return dh;
936 }
937 
938 /*
939  * Load hardcoded DH parameters.
940  *
941  * If DH parameters cannot be loaded from a specified file, we can load
942  * the hardcoded DH parameters supplied with the backend to prevent
943  * problems.
944  */
945 static DH *
946 load_dh_buffer(const char *buffer, size_t len)
947 {
948  BIO *bio;
949  DH *dh = NULL;
950 
951  bio = BIO_new_mem_buf(unconstify(char *, buffer), len);
952  if (bio == NULL)
953  return NULL;
954  dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
955  if (dh == NULL)
956  ereport(DEBUG2,
957  (errmsg_internal("DH load buffer: %s",
958  SSLerrmessage(ERR_get_error()))));
959  BIO_free(bio);
960 
961  return dh;
962 }
963 
964 /*
965  * Passphrase collection callback using ssl_passphrase_command
966  */
967 static int
968 ssl_external_passwd_cb(char *buf, int size, int rwflag, void *userdata)
969 {
970  /* same prompt as OpenSSL uses internally */
971  const char *prompt = "Enter PEM pass phrase:";
972 
973  Assert(rwflag == 0);
974 
975  return run_ssl_passphrase_command(prompt, ssl_is_server_start, buf, size);
976 }
977 
978 /*
979  * Dummy passphrase callback
980  *
981  * If OpenSSL is told to use a passphrase-protected server key, by default
982  * it will issue a prompt on /dev/tty and try to read a key from there.
983  * That's no good during a postmaster SIGHUP cycle, not to mention SSL context
984  * reload in an EXEC_BACKEND postmaster child. So override it with this dummy
985  * function that just returns an empty passphrase, guaranteeing failure.
986  */
987 static int
988 dummy_ssl_passwd_cb(char *buf, int size, int rwflag, void *userdata)
989 {
990  /* Set flag to change the error message we'll report */
992  /* And return empty string */
993  Assert(size > 0);
994  buf[0] = '\0';
995  return 0;
996 }
997 
998 /*
999  * Certificate verification callback
1000  *
1001  * This callback allows us to log intermediate problems during
1002  * verification, but for now we'll see if the final error message
1003  * contains enough information.
1004  *
1005  * This callback also allows us to override the default acceptance
1006  * criteria (e.g., accepting self-signed or expired certs), but
1007  * for now we accept the default checks.
1008  */
1009 static int
1010 verify_cb(int ok, X509_STORE_CTX *ctx)
1011 {
1012  return ok;
1013 }
1014 
1015 /*
1016  * This callback is used to copy SSL information messages
1017  * into the PostgreSQL log.
1018  */
1019 static void
1020 info_cb(const SSL *ssl, int type, int args)
1021 {
1022  const char *desc;
1023 
1024  desc = SSL_state_string_long(ssl);
1025 
1026  switch (type)
1027  {
1028  case SSL_CB_HANDSHAKE_START:
1029  ereport(DEBUG4,
1030  (errmsg_internal("SSL: handshake start: \"%s\"", desc)));
1031  break;
1032  case SSL_CB_HANDSHAKE_DONE:
1033  ereport(DEBUG4,
1034  (errmsg_internal("SSL: handshake done: \"%s\"", desc)));
1035  break;
1036  case SSL_CB_ACCEPT_LOOP:
1037  ereport(DEBUG4,
1038  (errmsg_internal("SSL: accept loop: \"%s\"", desc)));
1039  break;
1040  case SSL_CB_ACCEPT_EXIT:
1041  ereport(DEBUG4,
1042  (errmsg_internal("SSL: accept exit (%d): \"%s\"", args, desc)));
1043  break;
1044  case SSL_CB_CONNECT_LOOP:
1045  ereport(DEBUG4,
1046  (errmsg_internal("SSL: connect loop: \"%s\"", desc)));
1047  break;
1048  case SSL_CB_CONNECT_EXIT:
1049  ereport(DEBUG4,
1050  (errmsg_internal("SSL: connect exit (%d): \"%s\"", args, desc)));
1051  break;
1052  case SSL_CB_READ_ALERT:
1053  ereport(DEBUG4,
1054  (errmsg_internal("SSL: read alert (0x%04x): \"%s\"", args, desc)));
1055  break;
1056  case SSL_CB_WRITE_ALERT:
1057  ereport(DEBUG4,
1058  (errmsg_internal("SSL: write alert (0x%04x): \"%s\"", args, desc)));
1059  break;
1060  }
1061 }
1062 
1063 /*
1064  * Set DH parameters for generating ephemeral DH keys. The
1065  * DH parameters can take a long time to compute, so they must be
1066  * precomputed.
1067  *
1068  * Since few sites will bother to create a parameter file, we also
1069  * provide a fallback to the parameters provided by the OpenSSL
1070  * project.
1071  *
1072  * These values can be static (once loaded or computed) since the
1073  * OpenSSL library can efficiently generate random keys from the
1074  * information provided.
1075  */
1076 static bool
1077 initialize_dh(SSL_CTX *context, bool isServerStart)
1078 {
1079  DH *dh = NULL;
1080 
1081  SSL_CTX_set_options(context, SSL_OP_SINGLE_DH_USE);
1082 
1083  if (ssl_dh_params_file[0])
1084  dh = load_dh_file(ssl_dh_params_file, isServerStart);
1085  if (!dh)
1086  dh = load_dh_buffer(FILE_DH2048, sizeof(FILE_DH2048));
1087  if (!dh)
1088  {
1089  ereport(isServerStart ? FATAL : LOG,
1090  (errcode(ERRCODE_CONFIG_FILE_ERROR),
1091  errmsg("DH: could not load DH parameters")));
1092  return false;
1093  }
1094 
1095  if (SSL_CTX_set_tmp_dh(context, dh) != 1)
1096  {
1097  ereport(isServerStart ? FATAL : LOG,
1098  (errcode(ERRCODE_CONFIG_FILE_ERROR),
1099  errmsg("DH: could not set DH parameters: %s",
1100  SSLerrmessage(ERR_get_error()))));
1101  DH_free(dh);
1102  return false;
1103  }
1104 
1105  DH_free(dh);
1106  return true;
1107 }
1108 
1109 /*
1110  * Set ECDH parameters for generating ephemeral Elliptic Curve DH
1111  * keys. This is much simpler than the DH parameters, as we just
1112  * need to provide the name of the curve to OpenSSL.
1113  */
1114 static bool
1115 initialize_ecdh(SSL_CTX *context, bool isServerStart)
1116 {
1117 #ifndef OPENSSL_NO_ECDH
1118  EC_KEY *ecdh;
1119  int nid;
1120 
1121  nid = OBJ_sn2nid(SSLECDHCurve);
1122  if (!nid)
1123  {
1124  ereport(isServerStart ? FATAL : LOG,
1125  (errcode(ERRCODE_CONFIG_FILE_ERROR),
1126  errmsg("ECDH: unrecognized curve name: %s", SSLECDHCurve)));
1127  return false;
1128  }
1129 
1130  ecdh = EC_KEY_new_by_curve_name(nid);
1131  if (!ecdh)
1132  {
1133  ereport(isServerStart ? FATAL : LOG,
1134  (errcode(ERRCODE_CONFIG_FILE_ERROR),
1135  errmsg("ECDH: could not create key")));
1136  return false;
1137  }
1138 
1139  SSL_CTX_set_options(context, SSL_OP_SINGLE_ECDH_USE);
1140  SSL_CTX_set_tmp_ecdh(context, ecdh);
1141  EC_KEY_free(ecdh);
1142 #endif
1143 
1144  return true;
1145 }
1146 
1147 /*
1148  * Obtain reason string for passed SSL errcode
1149  *
1150  * ERR_get_error() is used by caller to get errcode to pass here.
1151  *
1152  * Some caution is needed here since ERR_reason_error_string will
1153  * return NULL if it doesn't recognize the error code. We don't
1154  * want to return NULL ever.
1155  */
1156 static const char *
1157 SSLerrmessage(unsigned long ecode)
1158 {
1159  const char *errreason;
1160  static char errbuf[36];
1161 
1162  if (ecode == 0)
1163  return _("no SSL error reported");
1164  errreason = ERR_reason_error_string(ecode);
1165  if (errreason != NULL)
1166  return errreason;
1167  snprintf(errbuf, sizeof(errbuf), _("SSL error code %lu"), ecode);
1168  return errbuf;
1169 }
1170 
1171 int
1173 {
1174  int bits;
1175 
1176  if (port->ssl)
1177  {
1178  SSL_get_cipher_bits(port->ssl, &bits);
1179  return bits;
1180  }
1181  else
1182  return 0;
1183 }
1184 
1185 bool
1187 {
1188  if (port->ssl)
1189  return (SSL_get_current_compression(port->ssl) != NULL);
1190  else
1191  return false;
1192 }
1193 
1194 const char *
1196 {
1197  if (port->ssl)
1198  return SSL_get_version(port->ssl);
1199  else
1200  return NULL;
1201 }
1202 
1203 const char *
1205 {
1206  if (port->ssl)
1207  return SSL_get_cipher(port->ssl);
1208  else
1209  return NULL;
1210 }
1211 
1212 void
1213 be_tls_get_peer_subject_name(Port *port, char *ptr, size_t len)
1214 {
1215  if (port->peer)
1216  strlcpy(ptr, X509_NAME_to_cstring(X509_get_subject_name(port->peer)), len);
1217  else
1218  ptr[0] = '\0';
1219 }
1220 
1221 void
1222 be_tls_get_peer_issuer_name(Port *port, char *ptr, size_t len)
1223 {
1224  if (port->peer)
1225  strlcpy(ptr, X509_NAME_to_cstring(X509_get_issuer_name(port->peer)), len);
1226  else
1227  ptr[0] = '\0';
1228 }
1229 
1230 void
1231 be_tls_get_peer_serial(Port *port, char *ptr, size_t len)
1232 {
1233  if (port->peer)
1234  {
1235  ASN1_INTEGER *serial;
1236  BIGNUM *b;
1237  char *decimal;
1238 
1239  serial = X509_get_serialNumber(port->peer);
1240  b = ASN1_INTEGER_to_BN(serial, NULL);
1241  decimal = BN_bn2dec(b);
1242 
1243  BN_free(b);
1244  strlcpy(ptr, decimal, len);
1245  OPENSSL_free(decimal);
1246  }
1247  else
1248  ptr[0] = '\0';
1249 }
1250 
1251 #ifdef HAVE_X509_GET_SIGNATURE_NID
1252 char *
1253 be_tls_get_certificate_hash(Port *port, size_t *len)
1254 {
1255  X509 *server_cert;
1256  char *cert_hash;
1257  const EVP_MD *algo_type = NULL;
1258  unsigned char hash[EVP_MAX_MD_SIZE]; /* size for SHA-512 */
1259  unsigned int hash_size;
1260  int algo_nid;
1261 
1262  *len = 0;
1263  server_cert = SSL_get_certificate(port->ssl);
1264  if (server_cert == NULL)
1265  return NULL;
1266 
1267  /*
1268  * Get the signature algorithm of the certificate to determine the hash
1269  * algorithm to use for the result.
1270  */
1271  if (!OBJ_find_sigid_algs(X509_get_signature_nid(server_cert),
1272  &algo_nid, NULL))
1273  elog(ERROR, "could not determine server certificate signature algorithm");
1274 
1275  /*
1276  * The TLS server's certificate bytes need to be hashed with SHA-256 if
1277  * its signature algorithm is MD5 or SHA-1 as per RFC 5929
1278  * (https://tools.ietf.org/html/rfc5929#section-4.1). If something else
1279  * is used, the same hash as the signature algorithm is used.
1280  */
1281  switch (algo_nid)
1282  {
1283  case NID_md5:
1284  case NID_sha1:
1285  algo_type = EVP_sha256();
1286  break;
1287  default:
1288  algo_type = EVP_get_digestbynid(algo_nid);
1289  if (algo_type == NULL)
1290  elog(ERROR, "could not find digest for NID %s",
1291  OBJ_nid2sn(algo_nid));
1292  break;
1293  }
1294 
1295  /* generate and save the certificate hash */
1296  if (!X509_digest(server_cert, algo_type, hash, &hash_size))
1297  elog(ERROR, "could not generate server certificate hash");
1298 
1299  cert_hash = palloc(hash_size);
1300  memcpy(cert_hash, hash, hash_size);
1301  *len = hash_size;
1302 
1303  return cert_hash;
1304 }
1305 #endif
1306 
1307 /*
1308  * Convert an X509 subject name to a cstring.
1309  *
1310  */
1311 static char *
1313 {
1314  BIO *membuf = BIO_new(BIO_s_mem());
1315  int i,
1316  nid,
1317  count = X509_NAME_entry_count(name);
1318  X509_NAME_ENTRY *e;
1319  ASN1_STRING *v;
1320  const char *field_name;
1321  size_t size;
1322  char nullterm;
1323  char *sp;
1324  char *dp;
1325  char *result;
1326 
1327  if (membuf == NULL)
1328  ereport(ERROR,
1329  (errcode(ERRCODE_OUT_OF_MEMORY),
1330  errmsg("failed to create BIO")));
1331 
1332  (void) BIO_set_close(membuf, BIO_CLOSE);
1333  for (i = 0; i < count; i++)
1334  {
1335  e = X509_NAME_get_entry(name, i);
1336  nid = OBJ_obj2nid(X509_NAME_ENTRY_get_object(e));
1337  if (nid == NID_undef)
1338  ereport(ERROR,
1339  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1340  errmsg("could not get NID for ASN1_OBJECT object")));
1341  v = X509_NAME_ENTRY_get_data(e);
1342  field_name = OBJ_nid2sn(nid);
1343  if (field_name == NULL)
1344  field_name = OBJ_nid2ln(nid);
1345  if (field_name == NULL)
1346  ereport(ERROR,
1347  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1348  errmsg("could not convert NID %d to an ASN1_OBJECT structure", nid)));
1349  BIO_printf(membuf, "/%s=", field_name);
1350  ASN1_STRING_print_ex(membuf, v,
1351  ((ASN1_STRFLGS_RFC2253 & ~ASN1_STRFLGS_ESC_MSB)
1352  | ASN1_STRFLGS_UTF8_CONVERT));
1353  }
1354 
1355  /* ensure null termination of the BIO's content */
1356  nullterm = '\0';
1357  BIO_write(membuf, &nullterm, 1);
1358  size = BIO_get_mem_data(membuf, &sp);
1359  dp = pg_any_to_server(sp, size - 1, PG_UTF8);
1360 
1361  result = pstrdup(dp);
1362  if (dp != sp)
1363  pfree(dp);
1364  if (BIO_free(membuf) != 1)
1365  elog(ERROR, "could not free OpenSSL BIO structure");
1366 
1367  return result;
1368 }
1369 
1370 /*
1371  * Convert TLS protocol version GUC enum to OpenSSL values
1372  *
1373  * This is a straightforward one-to-one mapping, but doing it this way makes
1374  * guc.c independent of OpenSSL availability and version.
1375  *
1376  * If a version is passed that is not supported by the current OpenSSL
1377  * version, then we return -1. If a nonnegative value is returned,
1378  * subsequent code can assume it's working with a supported version.
1379  *
1380  * Note: this is rather similar to libpq's routine in fe-secure-openssl.c,
1381  * so make sure to update both routines if changing this one.
1382  */
1383 static int
1385 {
1386  switch (v)
1387  {
1388  case PG_TLS_ANY:
1389  return 0;
1390  case PG_TLS1_VERSION:
1391  return TLS1_VERSION;
1392  case PG_TLS1_1_VERSION:
1393 #ifdef TLS1_1_VERSION
1394  return TLS1_1_VERSION;
1395 #else
1396  break;
1397 #endif
1398  case PG_TLS1_2_VERSION:
1399 #ifdef TLS1_2_VERSION
1400  return TLS1_2_VERSION;
1401 #else
1402  break;
1403 #endif
1404  case PG_TLS1_3_VERSION:
1405 #ifdef TLS1_3_VERSION
1406  return TLS1_3_VERSION;
1407 #else
1408  break;
1409 #endif
1410  }
1411 
1412  return -1;
1413 }
1414 
1415 /*
1416  * Likewise provide a mapping to strings.
1417  */
1418 static const char *
1420 {
1421  switch (v)
1422  {
1423  case PG_TLS_ANY:
1424  return "any";
1425  case PG_TLS1_VERSION:
1426  return "TLSv1";
1427  case PG_TLS1_1_VERSION:
1428  return "TLSv1.1";
1429  case PG_TLS1_2_VERSION:
1430  return "TLSv1.2";
1431  case PG_TLS1_3_VERSION:
1432  return "TLSv1.3";
1433  }
1434 
1435  return "(unrecognized)";
1436 }
1437 
1438 
1439 static void
1440 default_openssl_tls_init(SSL_CTX *context, bool isServerStart)
1441 {
1442  if (isServerStart)
1443  {
1444  if (ssl_passphrase_command[0])
1445  SSL_CTX_set_default_passwd_cb(context, ssl_external_passwd_cb);
1446  }
1447  else
1448  {
1450  SSL_CTX_set_default_passwd_cb(context, ssl_external_passwd_cb);
1451  else
1452 
1453  /*
1454  * If reloading and no external command is configured, override
1455  * OpenSSL's default handling of passphrase-protected files,
1456  * because we don't want to prompt for a passphrase in an
1457  * already-running server.
1458  */
1459  SSL_CTX_set_default_passwd_cb(context, dummy_ssl_passwd_cb);
1460  }
1461 }
static int ssl_protocol_version_to_openssl(int v)
ssize_t be_tls_read(Port *port, void *ptr, size_t len, int *waitfor)
#define WL_SOCKET_WRITEABLE
Definition: latch.h:127
int errhint(const char *fmt,...)
Definition: elog.c:1152
#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:341
static BIO_METHOD * my_BIO_s_socket(void)
static DH * load_dh_file(char *filename, bool isServerStart)
char * peer_cn
Definition: libpq-be.h:192
#define BIO_get_data(bio)
char * pstrdup(const char *in)
Definition: mcxt.c:1187
bool peer_cert_valid
Definition: libpq-be.h:193
ssize_t secure_raw_write(Port *port, const void *ptr, size_t len)
Definition: be-secure.c:331
static void info_cb(const SSL *ssl, int type, int args)
const char * GetConfigOption(const char *name, bool missing_ok, bool restrict_privileged)
Definition: guc.c:7912
int errcode(int sqlerrcode)
Definition: elog.c:694
Definition: libpq-be.h:120
bool ssl_in_use
Definition: libpq-be.h:191
#define WL_SOCKET_READABLE
Definition: latch.h:126
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:122
int be_tls_init(bool isServerStart)
int ssl_min_protocol_version
Definition: be-secure.c:63
#define malloc(a)
Definition: header.h:50
void pfree(void *pointer)
Definition: mcxt.c:1057
static int my_SSL_set_fd(Port *port, int fd)
#define ERROR
Definition: elog.h:45
bool be_tls_get_compression(Port *port)
#define FATAL
Definition: elog.h:54
char * SSLECDHCurve
Definition: be-secure.c:58
#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:68
int errdetail(const char *fmt,...)
Definition: elog.c:1038
#define COMMERROR
Definition: elog.h:30
int errcode_for_file_access(void)
Definition: elog.c:717
static bool SSL_initialized
char * SSLCipherSuites
Definition: be-secure.c:55
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2354
static int verify_cb(int, X509_STORE_CTX *)
int WaitLatchOrSocket(Latch *latch, int wakeEvents, pgsocket sock, long timeout, uint32 wait_event_info)
Definition: latch.c:500
static DH * load_dh_buffer(const char *, size_t)
void be_tls_destroy(void)
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:48
int errcode_for_socket_access(void)
Definition: elog.c:788
static int port
Definition: pg_regress.c:92
#define unconstify(underlying_type, expr)
Definition: c.h:1243
static int ssl_external_passwd_cb(char *buf, int size, int rwflag, void *userdata)
int be_tls_get_cipher_bits(Port *port)
char * ssl_crl_dir
Definition: be-secure.c:45
static bool ssl_is_server_start
static bool initialize_dh(SSL_CTX *context, bool isServerStart)
char * ssl_ca_file
Definition: be-secure.c:43
#define ereport(elevel,...)
Definition: elog.h:155
int be_tls_open_server(Port *port)
ssize_t secure_raw_read(Port *port, void *ptr, size_t len)
Definition: be-secure.c:235
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
int errmsg_internal(const char *fmt,...)
Definition: elog.c:992
static BIO_METHOD * my_bio_methods
int SSL_CTX_set_min_proto_version(SSL_CTX *ctx, int version)
#define Assert(condition)
Definition: c.h:804
static int my_sock_read(BIO *h, char *buf, int size)
bool noblock
Definition: libpq-be.h:123
#define ECONNRESET
Definition: win32_port.h:353
char * ssl_dh_params_file
Definition: be-secure.c:46
const char * name
Definition: encode.c:515
const char * be_tls_get_version(Port *port)
void be_tls_get_peer_issuer_name(Port *port, char *ptr, size_t len)
int ssl_max_protocol_version
Definition: be-secure.c:64
int FreeFile(FILE *file)
Definition: fd.c:2553
static char * filename
Definition: pg_dumpall.c:91
e
Definition: preproc-init.c:82
void be_tls_get_peer_subject_name(Port *port, char *ptr, size_t len)
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:905
static SSL_CTX * SSL_context
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:797
void be_tls_get_peer_serial(Port *port, char *ptr, size_t len)
static int my_sock_write(BIO *h, const char *buf, int size)
#define elog(elevel,...)
Definition: elog.h:227
int i
static const char * SSLerrmessage(unsigned long ecode)
void be_tls_close(Port *port)
struct Latch * MyLatch
Definition: globals.c:55
const char * be_tls_get_cipher(Port *port)
#define EWOULDBLOCK
Definition: win32_port.h:349
int run_ssl_passphrase_command(const char *prompt, bool is_server_start, char *buf, int size)
char * ssl_passphrase_command
Definition: be-secure.c:47
static const char * ssl_protocol_version_to_string(int v)
#define EINTR
Definition: win32_port.h:343
int SSL_CTX_set_max_proto_version(SSL_CTX *ctx, int version)
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:619
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:717
#define snprintf
Definition: port.h:216
#define _(x)
Definition: elog.c:89
openssl_tls_init_hook_typ openssl_tls_init_hook
char * ssl_key_file
Definition: be-secure.c:42
static void default_openssl_tls_init(SSL_CTX *context, bool isServerStart)
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:130
bool SSLPreferServerCiphers
Definition: be-secure.c:61
bool check_ssl_key_file_permissions(const char *ssl_key_file, bool isServerStart)