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-2025, 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#include <netinet/tcp.h>
28#include <arpa/inet.h>
29
30#include "common/string.h"
31#include "libpq/libpq.h"
32#include "miscadmin.h"
33#include "pgstat.h"
34#include "storage/fd.h"
35#include "storage/latch.h"
36#include "utils/guc.h"
37#include "utils/memutils.h"
38
39/*
40 * These SSL-related #includes must come after all system-provided headers.
41 * This ensures that OpenSSL can take care of conflicts with Windows'
42 * <wincrypt.h> by #undef'ing the conflicting macros. (We don't directly
43 * include <wincrypt.h>, but some other Windows headers do.)
44 */
45#include "common/openssl.h"
46#include <openssl/bn.h>
47#include <openssl/conf.h>
48#include <openssl/dh.h>
49#ifndef OPENSSL_NO_ECDH
50#include <openssl/ec.h>
51#endif
52#include <openssl/x509v3.h>
53
54
55/* default init hook can be overridden by a shared library */
56static void default_openssl_tls_init(SSL_CTX *context, bool isServerStart);
58
59static int port_bio_read(BIO *h, char *buf, int size);
60static int port_bio_write(BIO *h, const char *buf, int size);
61static BIO_METHOD *port_bio_method(void);
62static int ssl_set_port_bio(Port *port);
63
64static DH *load_dh_file(char *filename, bool isServerStart);
65static DH *load_dh_buffer(const char *buffer, size_t len);
66static int ssl_external_passwd_cb(char *buf, int size, int rwflag, void *userdata);
67static int dummy_ssl_passwd_cb(char *buf, int size, int rwflag, void *userdata);
68static int verify_cb(int ok, X509_STORE_CTX *ctx);
69static void info_cb(const SSL *ssl, int type, int args);
70static int alpn_cb(SSL *ssl,
71 const unsigned char **out,
72 unsigned char *outlen,
73 const unsigned char *in,
74 unsigned int inlen,
75 void *userdata);
76static bool initialize_dh(SSL_CTX *context, bool isServerStart);
77static bool initialize_ecdh(SSL_CTX *context, bool isServerStart);
78static const char *SSLerrmessageExt(unsigned long ecode, const char *replacement);
79static const char *SSLerrmessage(unsigned long ecode);
80
81static char *X509_NAME_to_cstring(X509_NAME *name);
82
83static SSL_CTX *SSL_context = NULL;
84static bool dummy_ssl_passwd_cb_called = false;
86
87static int ssl_protocol_version_to_openssl(int v);
88static const char *ssl_protocol_version_to_string(int v);
89
91{
92 /*
93 * Storage for passing certificate verification error logging from the
94 * callback.
95 */
97};
98
99/* ------------------------------------------------------------ */
100/* Public interface */
101/* ------------------------------------------------------------ */
102
103int
104be_tls_init(bool isServerStart)
105{
106 SSL_CTX *context;
107 int ssl_ver_min = -1;
108 int ssl_ver_max = -1;
109
110 /*
111 * Create a new SSL context into which we'll load all the configuration
112 * settings. If we fail partway through, we can avoid memory leakage by
113 * freeing this context; we don't install it as active until the end.
114 *
115 * We use SSLv23_method() because it can negotiate use of the highest
116 * mutually supported protocol version, while alternatives like
117 * TLSv1_2_method() permit only one specific version. Note that we don't
118 * actually allow SSL v2 or v3, only TLS protocols (see below).
119 */
120 context = SSL_CTX_new(SSLv23_method());
121 if (!context)
122 {
123 ereport(isServerStart ? FATAL : LOG,
124 (errmsg("could not create SSL context: %s",
125 SSLerrmessage(ERR_get_error()))));
126 goto error;
127 }
128
129 /*
130 * Disable OpenSSL's moving-write-buffer sanity check, because it causes
131 * unnecessary failures in nonblocking send cases.
132 */
133 SSL_CTX_set_mode(context, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
134
135 /*
136 * Call init hook (usually to set password callback)
137 */
138 (*openssl_tls_init_hook) (context, isServerStart);
139
140 /* used by the callback */
141 ssl_is_server_start = isServerStart;
142
143 /*
144 * Load and verify server's certificate and private key
145 */
146 if (SSL_CTX_use_certificate_chain_file(context, ssl_cert_file) != 1)
147 {
148 ereport(isServerStart ? FATAL : LOG,
149 (errcode(ERRCODE_CONFIG_FILE_ERROR),
150 errmsg("could not load server certificate file \"%s\": %s",
151 ssl_cert_file, SSLerrmessage(ERR_get_error()))));
152 goto error;
153 }
154
155 if (!check_ssl_key_file_permissions(ssl_key_file, isServerStart))
156 goto error;
157
158 /*
159 * OK, try to load the private key file.
160 */
162
163 if (SSL_CTX_use_PrivateKey_file(context,
165 SSL_FILETYPE_PEM) != 1)
166 {
168 ereport(isServerStart ? FATAL : LOG,
169 (errcode(ERRCODE_CONFIG_FILE_ERROR),
170 errmsg("private key file \"%s\" cannot be reloaded because it requires a passphrase",
171 ssl_key_file)));
172 else
173 ereport(isServerStart ? FATAL : LOG,
174 (errcode(ERRCODE_CONFIG_FILE_ERROR),
175 errmsg("could not load private key file \"%s\": %s",
176 ssl_key_file, SSLerrmessage(ERR_get_error()))));
177 goto error;
178 }
179
180 if (SSL_CTX_check_private_key(context) != 1)
181 {
182 ereport(isServerStart ? FATAL : LOG,
183 (errcode(ERRCODE_CONFIG_FILE_ERROR),
184 errmsg("check of private key failed: %s",
185 SSLerrmessage(ERR_get_error()))));
186 goto error;
187 }
188
190 {
192
193 if (ssl_ver_min == -1)
194 {
195 ereport(isServerStart ? FATAL : LOG,
196 /*- translator: first %s is a GUC option name, second %s is its value */
197 (errmsg("\"%s\" setting \"%s\" not supported by this build",
198 "ssl_min_protocol_version",
199 GetConfigOption("ssl_min_protocol_version",
200 false, false))));
201 goto error;
202 }
203
204 if (!SSL_CTX_set_min_proto_version(context, ssl_ver_min))
205 {
206 ereport(isServerStart ? FATAL : LOG,
207 (errmsg("could not set minimum SSL protocol version")));
208 goto error;
209 }
210 }
211
213 {
215
216 if (ssl_ver_max == -1)
217 {
218 ereport(isServerStart ? FATAL : LOG,
219 /*- translator: first %s is a GUC option name, second %s is its value */
220 (errmsg("\"%s\" setting \"%s\" not supported by this build",
221 "ssl_max_protocol_version",
222 GetConfigOption("ssl_max_protocol_version",
223 false, false))));
224 goto error;
225 }
226
227 if (!SSL_CTX_set_max_proto_version(context, ssl_ver_max))
228 {
229 ereport(isServerStart ? FATAL : LOG,
230 (errmsg("could not set maximum SSL protocol version")));
231 goto error;
232 }
233 }
234
235 /* Check compatibility of min/max protocols */
238 {
239 /*
240 * No need to check for invalid values (-1) for each protocol number
241 * as the code above would have already generated an error.
242 */
243 if (ssl_ver_min > ssl_ver_max)
244 {
245 ereport(isServerStart ? FATAL : LOG,
246 (errcode(ERRCODE_CONFIG_FILE_ERROR),
247 errmsg("could not set SSL protocol version range"),
248 errdetail("\"%s\" cannot be higher than \"%s\"",
249 "ssl_min_protocol_version",
250 "ssl_max_protocol_version")));
251 goto error;
252 }
253 }
254
255 /*
256 * Disallow SSL session tickets. OpenSSL use both stateful and stateless
257 * tickets for TLSv1.3, and stateless ticket for TLSv1.2. SSL_OP_NO_TICKET
258 * is available since 0.9.8f but only turns off stateless tickets. In
259 * order to turn off stateful tickets we need SSL_CTX_set_num_tickets,
260 * which is available since OpenSSL 1.1.1. LibreSSL 3.5.4 (from OpenBSD
261 * 7.1) introduced this API for compatibility, but doesn't support session
262 * tickets at all so it's a no-op there.
263 */
264#ifdef HAVE_SSL_CTX_SET_NUM_TICKETS
265 SSL_CTX_set_num_tickets(context, 0);
266#endif
267 SSL_CTX_set_options(context, SSL_OP_NO_TICKET);
268
269 /* disallow SSL session caching, too */
270 SSL_CTX_set_session_cache_mode(context, SSL_SESS_CACHE_OFF);
271
272 /* disallow SSL compression */
273 SSL_CTX_set_options(context, SSL_OP_NO_COMPRESSION);
274
275 /*
276 * Disallow SSL renegotiation. This concerns only TLSv1.2 and older
277 * protocol versions, as TLSv1.3 has no support for renegotiation.
278 * SSL_OP_NO_RENEGOTIATION is available in OpenSSL since 1.1.0h (via a
279 * backport from 1.1.1). SSL_OP_NO_CLIENT_RENEGOTIATION is available in
280 * LibreSSL since 2.5.1 disallowing all client-initiated renegotiation
281 * (this is usually on by default).
282 */
283#ifdef SSL_OP_NO_RENEGOTIATION
284 SSL_CTX_set_options(context, SSL_OP_NO_RENEGOTIATION);
285#endif
286#ifdef SSL_OP_NO_CLIENT_RENEGOTIATION
287 SSL_CTX_set_options(context, SSL_OP_NO_CLIENT_RENEGOTIATION);
288#endif
289
290 /* set up ephemeral DH and ECDH keys */
291 if (!initialize_dh(context, isServerStart))
292 goto error;
293 if (!initialize_ecdh(context, isServerStart))
294 goto error;
295
296 /* set up the allowed cipher list for TLSv1.2 and below */
297 if (SSL_CTX_set_cipher_list(context, SSLCipherList) != 1)
298 {
299 ereport(isServerStart ? FATAL : LOG,
300 (errcode(ERRCODE_CONFIG_FILE_ERROR),
301 errmsg("could not set the TLSv1.2 cipher list (no valid ciphers available)")));
302 goto error;
303 }
304
305 /*
306 * Set up the allowed cipher suites for TLSv1.3. If the GUC is an empty
307 * string we leave the allowed suites to be the OpenSSL default value.
308 */
309 if (SSLCipherSuites[0])
310 {
311 /* set up the allowed cipher suites */
312 if (SSL_CTX_set_ciphersuites(context, SSLCipherSuites) != 1)
313 {
314 ereport(isServerStart ? FATAL : LOG,
315 (errcode(ERRCODE_CONFIG_FILE_ERROR),
316 errmsg("could not set the TLSv1.3 cipher suites (no valid ciphers available)")));
317 goto error;
318 }
319 }
320
321 /* Let server choose order */
323 SSL_CTX_set_options(context, SSL_OP_CIPHER_SERVER_PREFERENCE);
324
325 /*
326 * Load CA store, so we can verify client certificates if needed.
327 */
328 if (ssl_ca_file[0])
329 {
330 STACK_OF(X509_NAME) * root_cert_list;
331
332 if (SSL_CTX_load_verify_locations(context, ssl_ca_file, NULL) != 1 ||
333 (root_cert_list = SSL_load_client_CA_file(ssl_ca_file)) == NULL)
334 {
335 ereport(isServerStart ? FATAL : LOG,
336 (errcode(ERRCODE_CONFIG_FILE_ERROR),
337 errmsg("could not load root certificate file \"%s\": %s",
338 ssl_ca_file, SSLerrmessage(ERR_get_error()))));
339 goto error;
340 }
341
342 /*
343 * Tell OpenSSL to send the list of root certs we trust to clients in
344 * CertificateRequests. This lets a client with a keystore select the
345 * appropriate client certificate to send to us. Also, this ensures
346 * that the SSL context will "own" the root_cert_list and remember to
347 * free it when no longer needed.
348 */
349 SSL_CTX_set_client_CA_list(context, root_cert_list);
350
351 /*
352 * Always ask for SSL client cert, but don't fail if it's not
353 * presented. We might fail such connections later, depending on what
354 * we find in pg_hba.conf.
355 */
356 SSL_CTX_set_verify(context,
357 (SSL_VERIFY_PEER |
358 SSL_VERIFY_CLIENT_ONCE),
359 verify_cb);
360 }
361
362 /*----------
363 * Load the Certificate Revocation List (CRL).
364 * http://searchsecurity.techtarget.com/sDefinition/0,,sid14_gci803160,00.html
365 *----------
366 */
367 if (ssl_crl_file[0] || ssl_crl_dir[0])
368 {
369 X509_STORE *cvstore = SSL_CTX_get_cert_store(context);
370
371 if (cvstore)
372 {
373 /* Set the flags to check against the complete CRL chain */
374 if (X509_STORE_load_locations(cvstore,
375 ssl_crl_file[0] ? ssl_crl_file : NULL,
376 ssl_crl_dir[0] ? ssl_crl_dir : NULL)
377 == 1)
378 {
379 X509_STORE_set_flags(cvstore,
380 X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
381 }
382 else if (ssl_crl_dir[0] == 0)
383 {
384 ereport(isServerStart ? FATAL : LOG,
385 (errcode(ERRCODE_CONFIG_FILE_ERROR),
386 errmsg("could not load SSL certificate revocation list file \"%s\": %s",
387 ssl_crl_file, SSLerrmessage(ERR_get_error()))));
388 goto error;
389 }
390 else if (ssl_crl_file[0] == 0)
391 {
392 ereport(isServerStart ? FATAL : LOG,
393 (errcode(ERRCODE_CONFIG_FILE_ERROR),
394 errmsg("could not load SSL certificate revocation list directory \"%s\": %s",
395 ssl_crl_dir, SSLerrmessage(ERR_get_error()))));
396 goto error;
397 }
398 else
399 {
400 ereport(isServerStart ? FATAL : LOG,
401 (errcode(ERRCODE_CONFIG_FILE_ERROR),
402 errmsg("could not load SSL certificate revocation list file \"%s\" or directory \"%s\": %s",
404 SSLerrmessage(ERR_get_error()))));
405 goto error;
406 }
407 }
408 }
409
410 /*
411 * Success! Replace any existing SSL_context.
412 */
413 if (SSL_context)
414 SSL_CTX_free(SSL_context);
415
416 SSL_context = context;
417
418 /*
419 * Set flag to remember whether CA store has been loaded into SSL_context.
420 */
421 if (ssl_ca_file[0])
422 ssl_loaded_verify_locations = true;
423 else
424 ssl_loaded_verify_locations = false;
425
426 return 0;
427
428 /* Clean up by releasing working context. */
429error:
430 if (context)
431 SSL_CTX_free(context);
432 return -1;
433}
434
435void
437{
438 if (SSL_context)
439 SSL_CTX_free(SSL_context);
440 SSL_context = NULL;
441 ssl_loaded_verify_locations = false;
442}
443
444int
446{
447 int r;
448 int err;
449 int waitfor;
450 unsigned long ecode;
451 bool give_proto_hint;
452 static struct CallbackErr err_context;
453
454 Assert(!port->ssl);
455 Assert(!port->peer);
456
457 if (!SSL_context)
458 {
460 (errcode(ERRCODE_PROTOCOL_VIOLATION),
461 errmsg("could not initialize SSL connection: SSL context not set up")));
462 return -1;
463 }
464
465 /* set up debugging/info callback */
466 SSL_CTX_set_info_callback(SSL_context, info_cb);
467
468 /* enable ALPN */
469 SSL_CTX_set_alpn_select_cb(SSL_context, alpn_cb, port);
470
471 if (!(port->ssl = SSL_new(SSL_context)))
472 {
474 (errcode(ERRCODE_PROTOCOL_VIOLATION),
475 errmsg("could not initialize SSL connection: %s",
476 SSLerrmessage(ERR_get_error()))));
477 return -1;
478 }
480 {
482 (errcode(ERRCODE_PROTOCOL_VIOLATION),
483 errmsg("could not set SSL socket: %s",
484 SSLerrmessage(ERR_get_error()))));
485 return -1;
486 }
487
488 err_context.cert_errdetail = NULL;
489 SSL_set_ex_data(port->ssl, 0, &err_context);
490
491 port->ssl_in_use = true;
492
493aloop:
494
495 /*
496 * Prepare to call SSL_get_error() by clearing thread's OpenSSL error
497 * queue. In general, the current thread's error queue must be empty
498 * before the TLS/SSL I/O operation is attempted, or SSL_get_error() will
499 * not work reliably. An extension may have failed to clear the
500 * per-thread error queue following another call to an OpenSSL I/O
501 * routine.
502 */
503 errno = 0;
504 ERR_clear_error();
505 r = SSL_accept(port->ssl);
506 if (r <= 0)
507 {
508 err = SSL_get_error(port->ssl, r);
509
510 /*
511 * Other clients of OpenSSL in the backend may fail to call
512 * ERR_get_error(), but we always do, so as to not cause problems for
513 * OpenSSL clients that don't call ERR_clear_error() defensively. Be
514 * sure that this happens by calling now. SSL_get_error() relies on
515 * the OpenSSL per-thread error queue being intact, so this is the
516 * earliest possible point ERR_get_error() may be called.
517 */
518 ecode = ERR_get_error();
519 switch (err)
520 {
521 case SSL_ERROR_WANT_READ:
522 case SSL_ERROR_WANT_WRITE:
523 /* not allowed during connection establishment */
524 Assert(!port->noblock);
525
526 /*
527 * No need to care about timeouts/interrupts here. At this
528 * point authentication_timeout still employs
529 * StartupPacketTimeoutHandler() which directly exits.
530 */
531 if (err == SSL_ERROR_WANT_READ)
533 else
535
536 (void) WaitLatchOrSocket(NULL, waitfor, port->sock, 0,
537 WAIT_EVENT_SSL_OPEN_SERVER);
538 goto aloop;
539 case SSL_ERROR_SYSCALL:
540 if (r < 0 && errno != 0)
543 errmsg("could not accept SSL connection: %m")));
544 else
546 (errcode(ERRCODE_PROTOCOL_VIOLATION),
547 errmsg("could not accept SSL connection: EOF detected")));
548 break;
549 case SSL_ERROR_SSL:
550 switch (ERR_GET_REASON(ecode))
551 {
552 /*
553 * UNSUPPORTED_PROTOCOL, WRONG_VERSION_NUMBER, and
554 * TLSV1_ALERT_PROTOCOL_VERSION have been observed
555 * when trying to communicate with an old OpenSSL
556 * library, or when the client and server specify
557 * disjoint protocol ranges. NO_PROTOCOLS_AVAILABLE
558 * occurs if there's a local misconfiguration (which
559 * can happen despite our checks, if openssl.cnf
560 * injects a limit we didn't account for). It's not
561 * very clear what would make OpenSSL return the other
562 * codes listed here, but a hint about protocol
563 * versions seems like it's appropriate for all.
564 */
565 case SSL_R_NO_PROTOCOLS_AVAILABLE:
566 case SSL_R_UNSUPPORTED_PROTOCOL:
567 case SSL_R_BAD_PROTOCOL_VERSION_NUMBER:
568 case SSL_R_UNKNOWN_PROTOCOL:
569 case SSL_R_UNKNOWN_SSL_VERSION:
570 case SSL_R_UNSUPPORTED_SSL_VERSION:
571 case SSL_R_WRONG_SSL_VERSION:
572 case SSL_R_WRONG_VERSION_NUMBER:
573 case SSL_R_TLSV1_ALERT_PROTOCOL_VERSION:
574#ifdef SSL_R_VERSION_TOO_HIGH
575 case SSL_R_VERSION_TOO_HIGH:
576#endif
577#ifdef SSL_R_VERSION_TOO_LOW
578 case SSL_R_VERSION_TOO_LOW:
579#endif
580 give_proto_hint = true;
581 break;
582 default:
583 give_proto_hint = false;
584 break;
585 }
587 (errcode(ERRCODE_PROTOCOL_VIOLATION),
588 errmsg("could not accept SSL connection: %s",
589 SSLerrmessage(ecode)),
590 err_context.cert_errdetail ? errdetail_internal("%s", err_context.cert_errdetail) : 0,
591 give_proto_hint ?
592 errhint("This may indicate that the client does not support any SSL protocol version between %s and %s.",
595 MIN_OPENSSL_TLS_VERSION,
598 MAX_OPENSSL_TLS_VERSION) : 0));
599 if (err_context.cert_errdetail)
600 pfree(err_context.cert_errdetail);
601 break;
602 case SSL_ERROR_ZERO_RETURN:
604 (errcode(ERRCODE_PROTOCOL_VIOLATION),
605 errmsg("could not accept SSL connection: EOF detected")));
606 break;
607 default:
609 (errcode(ERRCODE_PROTOCOL_VIOLATION),
610 errmsg("unrecognized SSL error code: %d",
611 err)));
612 break;
613 }
614 return -1;
615 }
616
617 /* Get the protocol selected by ALPN */
618 port->alpn_used = false;
619 {
620 const unsigned char *selected;
621 unsigned int len;
622
623 SSL_get0_alpn_selected(port->ssl, &selected, &len);
624
625 /* If ALPN is used, check that we negotiated the expected protocol */
626 if (selected != NULL)
627 {
628 if (len == strlen(PG_ALPN_PROTOCOL) &&
629 memcmp(selected, PG_ALPN_PROTOCOL, strlen(PG_ALPN_PROTOCOL)) == 0)
630 {
631 port->alpn_used = true;
632 }
633 else
634 {
635 /* shouldn't happen */
637 (errcode(ERRCODE_PROTOCOL_VIOLATION),
638 errmsg("received SSL connection request with unexpected ALPN protocol")));
639 }
640 }
641 }
642
643 /* Get client certificate, if available. */
644 port->peer = SSL_get_peer_certificate(port->ssl);
645
646 /* and extract the Common Name and Distinguished Name from it. */
647 port->peer_cn = NULL;
648 port->peer_dn = NULL;
649 port->peer_cert_valid = false;
650 if (port->peer != NULL)
651 {
652 int len;
653 X509_NAME *x509name = X509_get_subject_name(port->peer);
654 char *peer_dn;
655 BIO *bio = NULL;
656 BUF_MEM *bio_buf = NULL;
657
658 len = X509_NAME_get_text_by_NID(x509name, NID_commonName, NULL, 0);
659 if (len != -1)
660 {
661 char *peer_cn;
662
664 r = X509_NAME_get_text_by_NID(x509name, NID_commonName, peer_cn,
665 len + 1);
666 peer_cn[len] = '\0';
667 if (r != len)
668 {
669 /* shouldn't happen */
670 pfree(peer_cn);
671 return -1;
672 }
673
674 /*
675 * Reject embedded NULLs in certificate common name to prevent
676 * attacks like CVE-2009-4034.
677 */
678 if (len != strlen(peer_cn))
679 {
681 (errcode(ERRCODE_PROTOCOL_VIOLATION),
682 errmsg("SSL certificate's common name contains embedded null")));
683 pfree(peer_cn);
684 return -1;
685 }
686
687 port->peer_cn = peer_cn;
688 }
689
690 bio = BIO_new(BIO_s_mem());
691 if (!bio)
692 {
693 if (port->peer_cn != NULL)
694 {
695 pfree(port->peer_cn);
696 port->peer_cn = NULL;
697 }
698 return -1;
699 }
700
701 /*
702 * RFC2253 is the closest thing to an accepted standard format for
703 * DNs. We have documented how to produce this format from a
704 * certificate. It uses commas instead of slashes for delimiters,
705 * which make regular expression matching a bit easier. Also note that
706 * it prints the Subject fields in reverse order.
707 */
708 if (X509_NAME_print_ex(bio, x509name, 0, XN_FLAG_RFC2253) == -1 ||
709 BIO_get_mem_ptr(bio, &bio_buf) <= 0)
710 {
711 BIO_free(bio);
712 if (port->peer_cn != NULL)
713 {
714 pfree(port->peer_cn);
715 port->peer_cn = NULL;
716 }
717 return -1;
718 }
719 peer_dn = MemoryContextAlloc(TopMemoryContext, bio_buf->length + 1);
720 memcpy(peer_dn, bio_buf->data, bio_buf->length);
721 len = bio_buf->length;
722 BIO_free(bio);
723 peer_dn[len] = '\0';
724 if (len != strlen(peer_dn))
725 {
727 (errcode(ERRCODE_PROTOCOL_VIOLATION),
728 errmsg("SSL certificate's distinguished name contains embedded null")));
729 pfree(peer_dn);
730 if (port->peer_cn != NULL)
731 {
732 pfree(port->peer_cn);
733 port->peer_cn = NULL;
734 }
735 return -1;
736 }
737
738 port->peer_dn = peer_dn;
739
740 port->peer_cert_valid = true;
741 }
742
743 return 0;
744}
745
746void
748{
749 if (port->ssl)
750 {
751 SSL_shutdown(port->ssl);
752 SSL_free(port->ssl);
753 port->ssl = NULL;
754 port->ssl_in_use = false;
755 }
756
757 if (port->peer)
758 {
759 X509_free(port->peer);
760 port->peer = NULL;
761 }
762
763 if (port->peer_cn)
764 {
765 pfree(port->peer_cn);
766 port->peer_cn = NULL;
767 }
768
769 if (port->peer_dn)
770 {
771 pfree(port->peer_dn);
772 port->peer_dn = NULL;
773 }
774}
775
776ssize_t
777be_tls_read(Port *port, void *ptr, size_t len, int *waitfor)
778{
779 ssize_t n;
780 int err;
781 unsigned long ecode;
782
783 errno = 0;
784 ERR_clear_error();
785 n = SSL_read(port->ssl, ptr, len);
786 err = SSL_get_error(port->ssl, n);
787 ecode = (err != SSL_ERROR_NONE || n < 0) ? ERR_get_error() : 0;
788 switch (err)
789 {
790 case SSL_ERROR_NONE:
791 /* a-ok */
792 break;
793 case SSL_ERROR_WANT_READ:
794 *waitfor = WL_SOCKET_READABLE;
795 errno = EWOULDBLOCK;
796 n = -1;
797 break;
798 case SSL_ERROR_WANT_WRITE:
799 *waitfor = WL_SOCKET_WRITEABLE;
800 errno = EWOULDBLOCK;
801 n = -1;
802 break;
803 case SSL_ERROR_SYSCALL:
804 /* leave it to caller to ereport the value of errno */
805 if (n != -1 || errno == 0)
806 {
807 errno = ECONNRESET;
808 n = -1;
809 }
810 break;
811 case SSL_ERROR_SSL:
813 (errcode(ERRCODE_PROTOCOL_VIOLATION),
814 errmsg("SSL error: %s", SSLerrmessage(ecode))));
815 errno = ECONNRESET;
816 n = -1;
817 break;
818 case SSL_ERROR_ZERO_RETURN:
819 /* connection was cleanly shut down by peer */
820 n = 0;
821 break;
822 default:
824 (errcode(ERRCODE_PROTOCOL_VIOLATION),
825 errmsg("unrecognized SSL error code: %d",
826 err)));
827 errno = ECONNRESET;
828 n = -1;
829 break;
830 }
831
832 return n;
833}
834
835ssize_t
836be_tls_write(Port *port, const void *ptr, size_t len, int *waitfor)
837{
838 ssize_t n;
839 int err;
840 unsigned long ecode;
841
842 errno = 0;
843 ERR_clear_error();
844 n = SSL_write(port->ssl, ptr, len);
845 err = SSL_get_error(port->ssl, n);
846 ecode = (err != SSL_ERROR_NONE || n < 0) ? ERR_get_error() : 0;
847 switch (err)
848 {
849 case SSL_ERROR_NONE:
850 /* a-ok */
851 break;
852 case SSL_ERROR_WANT_READ:
853 *waitfor = WL_SOCKET_READABLE;
854 errno = EWOULDBLOCK;
855 n = -1;
856 break;
857 case SSL_ERROR_WANT_WRITE:
858 *waitfor = WL_SOCKET_WRITEABLE;
859 errno = EWOULDBLOCK;
860 n = -1;
861 break;
862 case SSL_ERROR_SYSCALL:
863
864 /*
865 * Leave it to caller to ereport the value of errno. However, if
866 * errno is still zero then assume it's a read EOF situation, and
867 * report ECONNRESET. (This seems possible because SSL_write can
868 * also do reads.)
869 */
870 if (n != -1 || errno == 0)
871 {
872 errno = ECONNRESET;
873 n = -1;
874 }
875 break;
876 case SSL_ERROR_SSL:
878 (errcode(ERRCODE_PROTOCOL_VIOLATION),
879 errmsg("SSL error: %s", SSLerrmessage(ecode))));
880 errno = ECONNRESET;
881 n = -1;
882 break;
883 case SSL_ERROR_ZERO_RETURN:
884
885 /*
886 * the SSL connection was closed, leave it to the caller to
887 * ereport it
888 */
889 errno = ECONNRESET;
890 n = -1;
891 break;
892 default:
894 (errcode(ERRCODE_PROTOCOL_VIOLATION),
895 errmsg("unrecognized SSL error code: %d",
896 err)));
897 errno = ECONNRESET;
898 n = -1;
899 break;
900 }
901
902 return n;
903}
904
905/* ------------------------------------------------------------ */
906/* Internal functions */
907/* ------------------------------------------------------------ */
908
909/*
910 * Private substitute BIO: this does the sending and receiving using send() and
911 * recv() instead. This is so that we can enable and disable interrupts
912 * just while calling recv(). We cannot have interrupts occurring while
913 * the bulk of OpenSSL runs, because it uses malloc() and possibly other
914 * non-reentrant libc facilities. We also need to call send() and recv()
915 * directly so it gets passed through the socket/signals layer on Win32.
916 *
917 * These functions are closely modelled on the standard socket BIO in OpenSSL;
918 * see sock_read() and sock_write() in OpenSSL's crypto/bio/bss_sock.c.
919 */
920
921static BIO_METHOD *port_bio_method_ptr = NULL;
922
923static int
924port_bio_read(BIO *h, char *buf, int size)
925{
926 int res = 0;
927 Port *port = (Port *) BIO_get_data(h);
928
929 if (buf != NULL)
930 {
931 res = secure_raw_read(port, buf, size);
932 BIO_clear_retry_flags(h);
933 port->last_read_was_eof = res == 0;
934 if (res <= 0)
935 {
936 /* If we were interrupted, tell caller to retry */
937 if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)
938 {
939 BIO_set_retry_read(h);
940 }
941 }
942 }
943
944 return res;
945}
946
947static int
948port_bio_write(BIO *h, const char *buf, int size)
949{
950 int res = 0;
951
952 res = secure_raw_write(((Port *) BIO_get_data(h)), buf, size);
953 BIO_clear_retry_flags(h);
954 if (res <= 0)
955 {
956 /* If we were interrupted, tell caller to retry */
957 if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)
958 {
959 BIO_set_retry_write(h);
960 }
961 }
962
963 return res;
964}
965
966static long
967port_bio_ctrl(BIO *h, int cmd, long num, void *ptr)
968{
969 long res;
970 Port *port = (Port *) BIO_get_data(h);
971
972 switch (cmd)
973 {
974 case BIO_CTRL_EOF:
975
976 /*
977 * This should not be needed. port_bio_read already has a way to
978 * signal EOF to OpenSSL. However, OpenSSL made an undocumented,
979 * backwards-incompatible change and now expects EOF via BIO_ctrl.
980 * See https://github.com/openssl/openssl/issues/8208
981 */
982 res = port->last_read_was_eof;
983 break;
984 case BIO_CTRL_FLUSH:
985 /* libssl expects all BIOs to support BIO_flush. */
986 res = 1;
987 break;
988 default:
989 res = 0;
990 break;
991 }
992
993 return res;
994}
995
996static BIO_METHOD *
998{
1000 {
1001 int my_bio_index;
1002
1003 my_bio_index = BIO_get_new_index();
1004 if (my_bio_index == -1)
1005 return NULL;
1006 my_bio_index |= BIO_TYPE_SOURCE_SINK;
1007 port_bio_method_ptr = BIO_meth_new(my_bio_index, "PostgreSQL backend socket");
1009 return NULL;
1010 if (!BIO_meth_set_write(port_bio_method_ptr, port_bio_write) ||
1011 !BIO_meth_set_read(port_bio_method_ptr, port_bio_read) ||
1012 !BIO_meth_set_ctrl(port_bio_method_ptr, port_bio_ctrl))
1013 {
1014 BIO_meth_free(port_bio_method_ptr);
1015 port_bio_method_ptr = NULL;
1016 return NULL;
1017 }
1018 }
1019 return port_bio_method_ptr;
1020}
1021
1022static int
1024{
1025 BIO *bio;
1026 BIO_METHOD *bio_method;
1027
1028 bio_method = port_bio_method();
1029 if (bio_method == NULL)
1030 return 0;
1031
1032 bio = BIO_new(bio_method);
1033 if (bio == NULL)
1034 return 0;
1035
1036 BIO_set_data(bio, port);
1037 BIO_set_init(bio, 1);
1038
1039 SSL_set_bio(port->ssl, bio, bio);
1040 return 1;
1041}
1042
1043/*
1044 * Load precomputed DH parameters.
1045 *
1046 * To prevent "downgrade" attacks, we perform a number of checks
1047 * to verify that the DBA-generated DH parameters file contains
1048 * what we expect it to contain.
1049 */
1050static DH *
1051load_dh_file(char *filename, bool isServerStart)
1052{
1053 FILE *fp;
1054 DH *dh = NULL;
1055 int codes;
1056
1057 /* attempt to open file. It's not an error if it doesn't exist. */
1058 if ((fp = AllocateFile(filename, "r")) == NULL)
1059 {
1060 ereport(isServerStart ? FATAL : LOG,
1062 errmsg("could not open DH parameters file \"%s\": %m",
1063 filename)));
1064 return NULL;
1065 }
1066
1067 dh = PEM_read_DHparams(fp, NULL, NULL, NULL);
1068 FreeFile(fp);
1069
1070 if (dh == NULL)
1071 {
1072 ereport(isServerStart ? FATAL : LOG,
1073 (errcode(ERRCODE_CONFIG_FILE_ERROR),
1074 errmsg("could not load DH parameters file: %s",
1075 SSLerrmessage(ERR_get_error()))));
1076 return NULL;
1077 }
1078
1079 /* make sure the DH parameters are usable */
1080 if (DH_check(dh, &codes) == 0)
1081 {
1082 ereport(isServerStart ? FATAL : LOG,
1083 (errcode(ERRCODE_CONFIG_FILE_ERROR),
1084 errmsg("invalid DH parameters: %s",
1085 SSLerrmessage(ERR_get_error()))));
1086 DH_free(dh);
1087 return NULL;
1088 }
1089 if (codes & DH_CHECK_P_NOT_PRIME)
1090 {
1091 ereport(isServerStart ? FATAL : LOG,
1092 (errcode(ERRCODE_CONFIG_FILE_ERROR),
1093 errmsg("invalid DH parameters: p is not prime")));
1094 DH_free(dh);
1095 return NULL;
1096 }
1097 if ((codes & DH_NOT_SUITABLE_GENERATOR) &&
1098 (codes & DH_CHECK_P_NOT_SAFE_PRIME))
1099 {
1100 ereport(isServerStart ? FATAL : LOG,
1101 (errcode(ERRCODE_CONFIG_FILE_ERROR),
1102 errmsg("invalid DH parameters: neither suitable generator or safe prime")));
1103 DH_free(dh);
1104 return NULL;
1105 }
1106
1107 return dh;
1108}
1109
1110/*
1111 * Load hardcoded DH parameters.
1112 *
1113 * If DH parameters cannot be loaded from a specified file, we can load
1114 * the hardcoded DH parameters supplied with the backend to prevent
1115 * problems.
1116 */
1117static DH *
1118load_dh_buffer(const char *buffer, size_t len)
1119{
1120 BIO *bio;
1121 DH *dh = NULL;
1122
1123 bio = BIO_new_mem_buf(buffer, len);
1124 if (bio == NULL)
1125 return NULL;
1126 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
1127 if (dh == NULL)
1129 (errmsg_internal("DH load buffer: %s",
1130 SSLerrmessage(ERR_get_error()))));
1131 BIO_free(bio);
1132
1133 return dh;
1134}
1135
1136/*
1137 * Passphrase collection callback using ssl_passphrase_command
1138 */
1139static int
1140ssl_external_passwd_cb(char *buf, int size, int rwflag, void *userdata)
1141{
1142 /* same prompt as OpenSSL uses internally */
1143 const char *prompt = "Enter PEM pass phrase:";
1144
1145 Assert(rwflag == 0);
1146
1147 return run_ssl_passphrase_command(prompt, ssl_is_server_start, buf, size);
1148}
1149
1150/*
1151 * Dummy passphrase callback
1152 *
1153 * If OpenSSL is told to use a passphrase-protected server key, by default
1154 * it will issue a prompt on /dev/tty and try to read a key from there.
1155 * That's no good during a postmaster SIGHUP cycle, not to mention SSL context
1156 * reload in an EXEC_BACKEND postmaster child. So override it with this dummy
1157 * function that just returns an empty passphrase, guaranteeing failure.
1158 */
1159static int
1160dummy_ssl_passwd_cb(char *buf, int size, int rwflag, void *userdata)
1161{
1162 /* Set flag to change the error message we'll report */
1164 /* And return empty string */
1165 Assert(size > 0);
1166 buf[0] = '\0';
1167 return 0;
1168}
1169
1170/*
1171 * Examines the provided certificate name, and if it's too long to log or
1172 * contains unprintable ASCII, escapes and truncates it. The return value is
1173 * always a new palloc'd string. (The input string is still modified in place,
1174 * for ease of implementation.)
1175 */
1176static char *
1178{
1179 size_t namelen = strlen(name);
1180 char *truncated = name;
1181
1182 /*
1183 * Common Names are 64 chars max, so for a common case where the CN is the
1184 * last field, we can still print the longest possible CN with a
1185 * 7-character prefix (".../CN=[64 chars]"), for a reasonable limit of 71
1186 * characters.
1187 */
1188#define MAXLEN 71
1189
1190 if (namelen > MAXLEN)
1191 {
1192 /*
1193 * Keep the end of the name, not the beginning, since the most
1194 * specific field is likely to give users the most information.
1195 */
1196 truncated = name + namelen - MAXLEN;
1197 truncated[0] = truncated[1] = truncated[2] = '.';
1198 namelen = MAXLEN;
1199 }
1200
1201#undef MAXLEN
1202
1203 return pg_clean_ascii(truncated, 0);
1204}
1205
1206/*
1207 * Certificate verification callback
1208 *
1209 * This callback allows us to examine intermediate problems during
1210 * verification, for later logging.
1211 *
1212 * This callback also allows us to override the default acceptance
1213 * criteria (e.g., accepting self-signed or expired certs), but
1214 * for now we accept the default checks.
1215 */
1216static int
1217verify_cb(int ok, X509_STORE_CTX *ctx)
1218{
1219 int depth;
1220 int errcode;
1221 const char *errstring;
1223 X509 *cert;
1224 SSL *ssl;
1225 struct CallbackErr *cb_err;
1226
1227 if (ok)
1228 {
1229 /* Nothing to do for the successful case. */
1230 return ok;
1231 }
1232
1233 /* Pull all the information we have on the verification failure. */
1234 depth = X509_STORE_CTX_get_error_depth(ctx);
1235 errcode = X509_STORE_CTX_get_error(ctx);
1236 errstring = X509_verify_cert_error_string(errcode);
1237
1238 /*
1239 * Extract the current SSL and CallbackErr object to use for passing error
1240 * detail back from the callback.
1241 */
1242 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
1243 cb_err = (struct CallbackErr *) SSL_get_ex_data(ssl, 0);
1244
1247 _("Client certificate verification failed at depth %d: %s."),
1248 depth, errstring);
1249
1250 cert = X509_STORE_CTX_get_current_cert(ctx);
1251 if (cert)
1252 {
1253 char *subject,
1254 *issuer;
1255 char *sub_prepared,
1256 *iss_prepared;
1257 char *serialno;
1258 ASN1_INTEGER *sn;
1259 BIGNUM *b;
1260
1261 /*
1262 * Get the Subject and Issuer for logging, but don't let maliciously
1263 * huge certs flood the logs, and don't reflect non-ASCII bytes into
1264 * it either.
1265 */
1266 subject = X509_NAME_to_cstring(X509_get_subject_name(cert));
1267 sub_prepared = prepare_cert_name(subject);
1268 pfree(subject);
1269
1270 issuer = X509_NAME_to_cstring(X509_get_issuer_name(cert));
1271 iss_prepared = prepare_cert_name(issuer);
1272 pfree(issuer);
1273
1274 /*
1275 * Pull the serial number, too, in case a Subject is still ambiguous.
1276 * This mirrors be_tls_get_peer_serial().
1277 */
1278 sn = X509_get_serialNumber(cert);
1279 b = ASN1_INTEGER_to_BN(sn, NULL);
1280 serialno = BN_bn2dec(b);
1281
1282 appendStringInfoChar(&str, '\n');
1284 _("Failed certificate data (unverified): subject \"%s\", serial number %s, issuer \"%s\"."),
1285 sub_prepared, serialno ? serialno : _("unknown"),
1286 iss_prepared);
1287
1288 BN_free(b);
1289 OPENSSL_free(serialno);
1290 pfree(iss_prepared);
1291 pfree(sub_prepared);
1292 }
1293
1294 /* Store our detail message to be logged later. */
1295 cb_err->cert_errdetail = str.data;
1296
1297 return ok;
1298}
1299
1300/*
1301 * This callback is used to copy SSL information messages
1302 * into the PostgreSQL log.
1303 */
1304static void
1305info_cb(const SSL *ssl, int type, int args)
1306{
1307 const char *desc;
1308
1309 desc = SSL_state_string_long(ssl);
1310
1311 switch (type)
1312 {
1313 case SSL_CB_HANDSHAKE_START:
1315 (errmsg_internal("SSL: handshake start: \"%s\"", desc)));
1316 break;
1317 case SSL_CB_HANDSHAKE_DONE:
1319 (errmsg_internal("SSL: handshake done: \"%s\"", desc)));
1320 break;
1321 case SSL_CB_ACCEPT_LOOP:
1323 (errmsg_internal("SSL: accept loop: \"%s\"", desc)));
1324 break;
1325 case SSL_CB_ACCEPT_EXIT:
1327 (errmsg_internal("SSL: accept exit (%d): \"%s\"", args, desc)));
1328 break;
1329 case SSL_CB_CONNECT_LOOP:
1331 (errmsg_internal("SSL: connect loop: \"%s\"", desc)));
1332 break;
1333 case SSL_CB_CONNECT_EXIT:
1335 (errmsg_internal("SSL: connect exit (%d): \"%s\"", args, desc)));
1336 break;
1337 case SSL_CB_READ_ALERT:
1339 (errmsg_internal("SSL: read alert (0x%04x): \"%s\"", args, desc)));
1340 break;
1341 case SSL_CB_WRITE_ALERT:
1343 (errmsg_internal("SSL: write alert (0x%04x): \"%s\"", args, desc)));
1344 break;
1345 }
1346}
1347
1348/* See pqcomm.h comments on OpenSSL implementation of ALPN (RFC 7301) */
1349static const unsigned char alpn_protos[] = PG_ALPN_PROTOCOL_VECTOR;
1350
1351/*
1352 * Server callback for ALPN negotiation. We use the standard "helper" function
1353 * even though currently we only accept one value.
1354 */
1355static int
1356alpn_cb(SSL *ssl,
1357 const unsigned char **out,
1358 unsigned char *outlen,
1359 const unsigned char *in,
1360 unsigned int inlen,
1361 void *userdata)
1362{
1363 /*
1364 * Why does OpenSSL provide a helper function that requires a nonconst
1365 * vector when the callback is declared to take a const vector? What are
1366 * we to do with that?
1367 */
1368 int retval;
1369
1370 Assert(userdata != NULL);
1371 Assert(out != NULL);
1372 Assert(outlen != NULL);
1373 Assert(in != NULL);
1374
1375 retval = SSL_select_next_proto((unsigned char **) out, outlen,
1376 alpn_protos, sizeof(alpn_protos),
1377 in, inlen);
1378 if (*out == NULL || *outlen > sizeof(alpn_protos) || *outlen <= 0)
1379 return SSL_TLSEXT_ERR_NOACK; /* can't happen */
1380
1381 if (retval == OPENSSL_NPN_NEGOTIATED)
1382 return SSL_TLSEXT_ERR_OK;
1383 else
1384 {
1385 /*
1386 * The client doesn't support our protocol. Reject the connection
1387 * with TLS "no_application_protocol" alert, per RFC 7301.
1388 */
1389 return SSL_TLSEXT_ERR_ALERT_FATAL;
1390 }
1391}
1392
1393
1394/*
1395 * Set DH parameters for generating ephemeral DH keys. The
1396 * DH parameters can take a long time to compute, so they must be
1397 * precomputed.
1398 *
1399 * Since few sites will bother to create a parameter file, we also
1400 * provide a fallback to the parameters provided by the OpenSSL
1401 * project.
1402 *
1403 * These values can be static (once loaded or computed) since the
1404 * OpenSSL library can efficiently generate random keys from the
1405 * information provided.
1406 */
1407static bool
1408initialize_dh(SSL_CTX *context, bool isServerStart)
1409{
1410 DH *dh = NULL;
1411
1412 SSL_CTX_set_options(context, SSL_OP_SINGLE_DH_USE);
1413
1414 if (ssl_dh_params_file[0])
1415 dh = load_dh_file(ssl_dh_params_file, isServerStart);
1416 if (!dh)
1417 dh = load_dh_buffer(FILE_DH2048, sizeof(FILE_DH2048));
1418 if (!dh)
1419 {
1420 ereport(isServerStart ? FATAL : LOG,
1421 (errcode(ERRCODE_CONFIG_FILE_ERROR),
1422 errmsg("DH: could not load DH parameters")));
1423 return false;
1424 }
1425
1426 if (SSL_CTX_set_tmp_dh(context, dh) != 1)
1427 {
1428 ereport(isServerStart ? FATAL : LOG,
1429 (errcode(ERRCODE_CONFIG_FILE_ERROR),
1430 errmsg("DH: could not set DH parameters: %s",
1431 SSLerrmessage(ERR_get_error()))));
1432 DH_free(dh);
1433 return false;
1434 }
1435
1436 DH_free(dh);
1437 return true;
1438}
1439
1440/*
1441 * Set ECDH parameters for generating ephemeral Elliptic Curve DH
1442 * keys. This is much simpler than the DH parameters, as we just
1443 * need to provide the name of the curve to OpenSSL.
1444 */
1445static bool
1446initialize_ecdh(SSL_CTX *context, bool isServerStart)
1447{
1448#ifndef OPENSSL_NO_ECDH
1449 if (SSL_CTX_set1_groups_list(context, SSLECDHCurve) != 1)
1450 {
1451 /*
1452 * OpenSSL 3.3.0 introduced proper error messages for group parsing
1453 * errors, earlier versions returns "no SSL error reported" which is
1454 * far from helpful. For older versions, we replace with a better
1455 * error message. Injecting the error into the OpenSSL error queue
1456 * need APIs from OpenSSL 3.0.
1457 */
1458 ereport(isServerStart ? FATAL : LOG,
1459 errcode(ERRCODE_CONFIG_FILE_ERROR),
1460 errmsg("could not set group names specified in ssl_groups: %s",
1461 SSLerrmessageExt(ERR_get_error(),
1462 _("No valid groups found"))),
1463 errhint("Ensure that each group name is spelled correctly and supported by the installed version of OpenSSL."));
1464 return false;
1465 }
1466#endif
1467
1468 return true;
1469}
1470
1471/*
1472 * Obtain reason string for passed SSL errcode with replacement
1473 *
1474 * The error message supplied in replacement will be used in case the error
1475 * code from OpenSSL is 0, else the error message from SSLerrmessage() will
1476 * be returned.
1477 *
1478 * Not all versions of OpenSSL place an error on the queue even for failing
1479 * operations, which will yield "no SSL error reported" by SSLerrmessage. This
1480 * function can be used to ensure that a proper error message is displayed for
1481 * versions reporting no error, while using the OpenSSL error via SSLerrmessage
1482 * for versions where there is one.
1483 */
1484static const char *
1485SSLerrmessageExt(unsigned long ecode, const char *replacement)
1486{
1487 if (ecode == 0)
1488 return replacement;
1489 else
1490 return SSLerrmessage(ecode);
1491}
1492
1493/*
1494 * Obtain reason string for passed SSL errcode
1495 *
1496 * ERR_get_error() is used by caller to get errcode to pass here.
1497 *
1498 * Some caution is needed here since ERR_reason_error_string will return NULL
1499 * if it doesn't recognize the error code, or (in OpenSSL >= 3) if the code
1500 * represents a system errno value. We don't want to return NULL ever.
1501 */
1502static const char *
1503SSLerrmessage(unsigned long ecode)
1504{
1505 const char *errreason;
1506 static char errbuf[36];
1507
1508 if (ecode == 0)
1509 return _("no SSL error reported");
1510 errreason = ERR_reason_error_string(ecode);
1511 if (errreason != NULL)
1512 return errreason;
1513
1514 /*
1515 * In OpenSSL 3.0.0 and later, ERR_reason_error_string does not map system
1516 * errno values anymore. (See OpenSSL source code for the explanation.)
1517 * We can cover that shortcoming with this bit of code. Older OpenSSL
1518 * versions don't have the ERR_SYSTEM_ERROR macro, but that's okay because
1519 * they don't have the shortcoming either.
1520 */
1521#ifdef ERR_SYSTEM_ERROR
1522 if (ERR_SYSTEM_ERROR(ecode))
1523 return strerror(ERR_GET_REASON(ecode));
1524#endif
1525
1526 /* No choice but to report the numeric ecode */
1527 snprintf(errbuf, sizeof(errbuf), _("SSL error code %lu"), ecode);
1528 return errbuf;
1529}
1530
1531int
1533{
1534 int bits;
1535
1536 if (port->ssl)
1537 {
1538 SSL_get_cipher_bits(port->ssl, &bits);
1539 return bits;
1540 }
1541 else
1542 return 0;
1543}
1544
1545const char *
1547{
1548 if (port->ssl)
1549 return SSL_get_version(port->ssl);
1550 else
1551 return NULL;
1552}
1553
1554const char *
1556{
1557 if (port->ssl)
1558 return SSL_get_cipher(port->ssl);
1559 else
1560 return NULL;
1561}
1562
1563void
1565{
1566 if (port->peer)
1567 strlcpy(ptr, X509_NAME_to_cstring(X509_get_subject_name(port->peer)), len);
1568 else
1569 ptr[0] = '\0';
1570}
1571
1572void
1574{
1575 if (port->peer)
1576 strlcpy(ptr, X509_NAME_to_cstring(X509_get_issuer_name(port->peer)), len);
1577 else
1578 ptr[0] = '\0';
1579}
1580
1581void
1583{
1584 if (port->peer)
1585 {
1586 ASN1_INTEGER *serial;
1587 BIGNUM *b;
1588 char *decimal;
1589
1590 serial = X509_get_serialNumber(port->peer);
1591 b = ASN1_INTEGER_to_BN(serial, NULL);
1592 decimal = BN_bn2dec(b);
1593
1594 BN_free(b);
1595 strlcpy(ptr, decimal, len);
1596 OPENSSL_free(decimal);
1597 }
1598 else
1599 ptr[0] = '\0';
1600}
1601
1602char *
1604{
1605 X509 *server_cert;
1606 char *cert_hash;
1607 const EVP_MD *algo_type = NULL;
1608 unsigned char hash[EVP_MAX_MD_SIZE]; /* size for SHA-512 */
1609 unsigned int hash_size;
1610 int algo_nid;
1611
1612 *len = 0;
1613 server_cert = SSL_get_certificate(port->ssl);
1614 if (server_cert == NULL)
1615 return NULL;
1616
1617 /*
1618 * Get the signature algorithm of the certificate to determine the hash
1619 * algorithm to use for the result. Prefer X509_get_signature_info(),
1620 * introduced in OpenSSL 1.1.1, which can handle RSA-PSS signatures.
1621 */
1622#if HAVE_X509_GET_SIGNATURE_INFO
1623 if (!X509_get_signature_info(server_cert, &algo_nid, NULL, NULL, NULL))
1624#else
1625 if (!OBJ_find_sigid_algs(X509_get_signature_nid(server_cert),
1626 &algo_nid, NULL))
1627#endif
1628 elog(ERROR, "could not determine server certificate signature algorithm");
1629
1630 /*
1631 * The TLS server's certificate bytes need to be hashed with SHA-256 if
1632 * its signature algorithm is MD5 or SHA-1 as per RFC 5929
1633 * (https://tools.ietf.org/html/rfc5929#section-4.1). If something else
1634 * is used, the same hash as the signature algorithm is used.
1635 */
1636 switch (algo_nid)
1637 {
1638 case NID_md5:
1639 case NID_sha1:
1640 algo_type = EVP_sha256();
1641 break;
1642 default:
1643 algo_type = EVP_get_digestbynid(algo_nid);
1644 if (algo_type == NULL)
1645 elog(ERROR, "could not find digest for NID %s",
1646 OBJ_nid2sn(algo_nid));
1647 break;
1648 }
1649
1650 /* generate and save the certificate hash */
1651 if (!X509_digest(server_cert, algo_type, hash, &hash_size))
1652 elog(ERROR, "could not generate server certificate hash");
1653
1654 cert_hash = palloc(hash_size);
1655 memcpy(cert_hash, hash, hash_size);
1656 *len = hash_size;
1657
1658 return cert_hash;
1659}
1660
1661/*
1662 * Convert an X509 subject name to a cstring.
1663 *
1664 */
1665static char *
1667{
1668 BIO *membuf = BIO_new(BIO_s_mem());
1669 int i,
1670 nid,
1671 count = X509_NAME_entry_count(name);
1672 X509_NAME_ENTRY *e;
1673 ASN1_STRING *v;
1674 const char *field_name;
1675 size_t size;
1676 char nullterm;
1677 char *sp;
1678 char *dp;
1679 char *result;
1680
1681 if (membuf == NULL)
1682 ereport(ERROR,
1683 (errcode(ERRCODE_OUT_OF_MEMORY),
1684 errmsg("could not create BIO")));
1685
1686 (void) BIO_set_close(membuf, BIO_CLOSE);
1687 for (i = 0; i < count; i++)
1688 {
1689 e = X509_NAME_get_entry(name, i);
1690 nid = OBJ_obj2nid(X509_NAME_ENTRY_get_object(e));
1691 if (nid == NID_undef)
1692 ereport(ERROR,
1693 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1694 errmsg("could not get NID for ASN1_OBJECT object")));
1695 v = X509_NAME_ENTRY_get_data(e);
1696 field_name = OBJ_nid2sn(nid);
1697 if (field_name == NULL)
1698 field_name = OBJ_nid2ln(nid);
1699 if (field_name == NULL)
1700 ereport(ERROR,
1701 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1702 errmsg("could not convert NID %d to an ASN1_OBJECT structure", nid)));
1703 BIO_printf(membuf, "/%s=", field_name);
1704 ASN1_STRING_print_ex(membuf, v,
1705 ((ASN1_STRFLGS_RFC2253 & ~ASN1_STRFLGS_ESC_MSB)
1706 | ASN1_STRFLGS_UTF8_CONVERT));
1707 }
1708
1709 /* ensure null termination of the BIO's content */
1710 nullterm = '\0';
1711 BIO_write(membuf, &nullterm, 1);
1712 size = BIO_get_mem_data(membuf, &sp);
1713 dp = pg_any_to_server(sp, size - 1, PG_UTF8);
1714
1715 result = pstrdup(dp);
1716 if (dp != sp)
1717 pfree(dp);
1718 if (BIO_free(membuf) != 1)
1719 elog(ERROR, "could not free OpenSSL BIO structure");
1720
1721 return result;
1722}
1723
1724/*
1725 * Convert TLS protocol version GUC enum to OpenSSL values
1726 *
1727 * This is a straightforward one-to-one mapping, but doing it this way makes
1728 * the definitions of ssl_min_protocol_version and ssl_max_protocol_version
1729 * independent of OpenSSL availability and version.
1730 *
1731 * If a version is passed that is not supported by the current OpenSSL
1732 * version, then we return -1. If a nonnegative value is returned,
1733 * subsequent code can assume it's working with a supported version.
1734 *
1735 * Note: this is rather similar to libpq's routine in fe-secure-openssl.c,
1736 * so make sure to update both routines if changing this one.
1737 */
1738static int
1740{
1741 switch (v)
1742 {
1743 case PG_TLS_ANY:
1744 return 0;
1745 case PG_TLS1_VERSION:
1746 return TLS1_VERSION;
1747 case PG_TLS1_1_VERSION:
1748#ifdef TLS1_1_VERSION
1749 return TLS1_1_VERSION;
1750#else
1751 break;
1752#endif
1753 case PG_TLS1_2_VERSION:
1754#ifdef TLS1_2_VERSION
1755 return TLS1_2_VERSION;
1756#else
1757 break;
1758#endif
1759 case PG_TLS1_3_VERSION:
1760#ifdef TLS1_3_VERSION
1761 return TLS1_3_VERSION;
1762#else
1763 break;
1764#endif
1765 }
1766
1767 return -1;
1768}
1769
1770/*
1771 * Likewise provide a mapping to strings.
1772 */
1773static const char *
1775{
1776 switch (v)
1777 {
1778 case PG_TLS_ANY:
1779 return "any";
1780 case PG_TLS1_VERSION:
1781 return "TLSv1";
1782 case PG_TLS1_1_VERSION:
1783 return "TLSv1.1";
1784 case PG_TLS1_2_VERSION:
1785 return "TLSv1.2";
1786 case PG_TLS1_3_VERSION:
1787 return "TLSv1.3";
1788 }
1789
1790 return "(unrecognized)";
1791}
1792
1793
1794static void
1795default_openssl_tls_init(SSL_CTX *context, bool isServerStart)
1796{
1797 if (isServerStart)
1798 {
1800 SSL_CTX_set_default_passwd_cb(context, ssl_external_passwd_cb);
1801 }
1802 else
1803 {
1805 SSL_CTX_set_default_passwd_cb(context, ssl_external_passwd_cb);
1806 else
1807
1808 /*
1809 * If reloading and no external command is configured, override
1810 * OpenSSL's default handling of passphrase-protected files,
1811 * because we don't want to prompt for a passphrase in an
1812 * already-running server.
1813 */
1814 SSL_CTX_set_default_passwd_cb(context, dummy_ssl_passwd_cb);
1815 }
1816}
bool check_ssl_key_file_permissions(const char *ssl_key_file, bool isServerStart)
int run_ssl_passphrase_command(const char *prompt, bool is_server_start, char *buf, int size)
const char * be_tls_get_version(Port *port)
static const char * ssl_protocol_version_to_string(int v)
static void info_cb(const SSL *ssl, int type, int args)
static const char * SSLerrmessage(unsigned long ecode)
ssize_t be_tls_write(Port *port, const void *ptr, size_t len, int *waitfor)
void be_tls_destroy(void)
int be_tls_init(bool isServerStart)
static long port_bio_ctrl(BIO *h, int cmd, long num, void *ptr)
openssl_tls_init_hook_typ openssl_tls_init_hook
int be_tls_get_cipher_bits(Port *port)
int be_tls_open_server(Port *port)
char * be_tls_get_certificate_hash(Port *port, size_t *len)
static int alpn_cb(SSL *ssl, const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *userdata)
const char * be_tls_get_cipher(Port *port)
static const char * SSLerrmessageExt(unsigned long ecode, const char *replacement)
static DH * load_dh_buffer(const char *buffer, size_t len)
static int dummy_ssl_passwd_cb(char *buf, int size, int rwflag, void *userdata)
static void default_openssl_tls_init(SSL_CTX *context, bool isServerStart)
static int ssl_set_port_bio(Port *port)
void be_tls_get_peer_serial(Port *port, char *ptr, size_t len)
static int ssl_protocol_version_to_openssl(int v)
static bool initialize_dh(SSL_CTX *context, bool isServerStart)
void be_tls_close(Port *port)
void be_tls_get_peer_issuer_name(Port *port, char *ptr, size_t len)
ssize_t be_tls_read(Port *port, void *ptr, size_t len, int *waitfor)
static char * X509_NAME_to_cstring(X509_NAME *name)
static BIO_METHOD * port_bio_method(void)
static int ssl_external_passwd_cb(char *buf, int size, int rwflag, void *userdata)
static char * prepare_cert_name(char *name)
static SSL_CTX * SSL_context
static bool ssl_is_server_start
static int verify_cb(int ok, X509_STORE_CTX *ctx)
static BIO_METHOD * port_bio_method_ptr
static bool initialize_ecdh(SSL_CTX *context, bool isServerStart)
static int port_bio_read(BIO *h, char *buf, int size)
static int port_bio_write(BIO *h, const char *buf, int size)
static bool dummy_ssl_passwd_cb_called
#define MAXLEN
static DH * load_dh_file(char *filename, bool isServerStart)
static const unsigned char alpn_protos[]
void be_tls_get_peer_subject_name(Port *port, char *ptr, size_t len)
char * ssl_crl_dir
Definition: be-secure.c:41
char * ssl_dh_params_file
Definition: be-secure.c:42
int ssl_min_protocol_version
Definition: be-secure.c:60
ssize_t secure_raw_read(Port *port, void *ptr, size_t len)
Definition: be-secure.c:268
char * ssl_cert_file
Definition: be-secure.c:37
bool SSLPreferServerCiphers
Definition: be-secure.c:58
int ssl_max_protocol_version
Definition: be-secure.c:61
char * ssl_passphrase_command
Definition: be-secure.c:43
bool ssl_passphrase_command_supports_reload
Definition: be-secure.c:44
char * SSLCipherSuites
Definition: be-secure.c:51
char * SSLECDHCurve
Definition: be-secure.c:55
char * SSLCipherList
Definition: be-secure.c:52
char * ssl_key_file
Definition: be-secure.c:38
ssize_t secure_raw_write(Port *port, const void *ptr, size_t len)
Definition: be-secure.c:377
char * ssl_crl_file
Definition: be-secure.c:40
char * ssl_ca_file
Definition: be-secure.c:39
int errcode_for_socket_access(void)
Definition: elog.c:963
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1170
int errdetail_internal(const char *fmt,...)
Definition: elog.c:1243
int errcode_for_file_access(void)
Definition: elog.c:886
int errdetail(const char *fmt,...)
Definition: elog.c:1216
int errhint(const char *fmt,...)
Definition: elog.c:1330
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define _(x)
Definition: elog.c:91
#define LOG
Definition: elog.h:31
#define COMMERROR
Definition: elog.h:33
#define FATAL
Definition: elog.h:41
#define DEBUG2
Definition: elog.h:29
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define ereport(elevel,...)
Definition: elog.h:150
#define DEBUG4
Definition: elog.h:27
void err(int eval, const char *fmt,...)
Definition: err.c:43
int FreeFile(FILE *file)
Definition: fd.c:2840
FILE * AllocateFile(const char *name, const char *mode)
Definition: fd.c:2641
const char * GetConfigOption(const char *name, bool missing_ok, bool restrict_privileged)
Definition: guc.c:4219
Assert(PointerIsAligned(start, uint64))
const char * str
int b
Definition: isn.c:74
int i
Definition: isn.c:77
int WaitLatchOrSocket(Latch *latch, int wakeEvents, pgsocket sock, long timeout, uint32 wait_event_info)
Definition: latch.c:223
@ PG_TLS1_VERSION
Definition: libpq.h:149
@ PG_TLS1_3_VERSION
Definition: libpq.h:152
@ PG_TLS1_1_VERSION
Definition: libpq.h:150
@ PG_TLS1_2_VERSION
Definition: libpq.h:151
@ PG_TLS_ANY
Definition: libpq.h:148
char * pg_any_to_server(const char *s, int len, int encoding)
Definition: mbutils.c:677
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1229
char * pstrdup(const char *in)
Definition: mcxt.c:1759
void pfree(void *pointer)
Definition: mcxt.c:1594
MemoryContext TopMemoryContext
Definition: mcxt.c:166
void * palloc(Size size)
Definition: mcxt.c:1365
const void size_t len
static char * filename
Definition: pg_dumpall.c:120
static int port
Definition: pg_regress.c:115
static char * buf
Definition: pg_test_fsync.c:72
@ PG_UTF8
Definition: pg_wchar.h:232
#define strerror
Definition: port.h:252
#define snprintf
Definition: port.h:239
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define PG_ALPN_PROTOCOL
Definition: pqcomm.h:165
#define PG_ALPN_PROTOCOL_VECTOR
Definition: pqcomm.h:166
e
Definition: preproc-init.c:82
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:715
static void error(void)
Definition: sql-dyntest.c:147
char * pg_clean_ascii(const char *str, int alloc_flags)
Definition: string.c:85
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:145
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:242
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97
char * cert_errdetail
Definition: libpq-be.h:129
const char * type
const char * name
#define WL_SOCKET_READABLE
Definition: waiteventset.h:35
#define WL_EXIT_ON_PM_DEATH
Definition: waiteventset.h:39
#define WL_SOCKET_WRITEABLE
Definition: waiteventset.h:36
#define EINTR
Definition: win32_port.h:364
#define EWOULDBLOCK
Definition: win32_port.h:370
#define ECONNRESET
Definition: win32_port.h:374
#define EAGAIN
Definition: win32_port.h:362