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>
52#include <openssl/x509v3.h>
71 const unsigned char **out,
72 unsigned char *outlen,
73 const unsigned char *in,
124 (
errmsg(
"could not create SSL context: %s",
150 errmsg(
"could not load server certificate file \"%s\": %s",
170 errmsg(
"private key file \"%s\" cannot be reloaded because it requires a passphrase",
175 errmsg(
"could not load private key file \"%s\": %s",
184 errmsg(
"check of private key failed: %s",
197 (
errmsg(
"\"%s\" setting \"%s\" not supported by this build",
198 "ssl_min_protocol_version",
207 (
errmsg(
"could not set minimum SSL protocol version")));
220 (
errmsg(
"\"%s\" setting \"%s\" not supported by this build",
221 "ssl_max_protocol_version",
230 (
errmsg(
"could not set maximum SSL protocol version")));
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")));
264#ifdef HAVE_SSL_CTX_SET_NUM_TICKETS
283#ifdef SSL_OP_NO_RENEGOTIATION
286#ifdef SSL_OP_NO_CLIENT_RENEGOTIATION
301 errmsg(
"could not set the TLSv1.2 cipher list (no valid ciphers available)")));
316 errmsg(
"could not set the TLSv1.3 cipher suites (no valid ciphers available)")));
337 errmsg(
"could not load root certificate file \"%s\": %s",
386 errmsg(
"could not load SSL certificate revocation list file \"%s\": %s",
394 errmsg(
"could not load SSL certificate revocation list directory \"%s\": %s",
402 errmsg(
"could not load SSL certificate revocation list file \"%s\" or directory \"%s\": %s",
461 errmsg(
"could not initialize SSL connection: SSL context not set up")));
475 errmsg(
"could not initialize SSL connection: %s",
483 errmsg(
"could not set SSL socket: %s",
491 port->ssl_in_use =
true;
540 if (r < 0 &&
errno != 0)
543 errmsg(
"could not accept SSL connection: %m")));
547 errmsg(
"could not accept SSL connection: EOF detected")));
574#ifdef SSL_R_VERSION_TOO_HIGH
577#ifdef SSL_R_VERSION_TOO_LOW
588 errmsg(
"could not accept SSL connection: %s",
592 errhint(
"This may indicate that the client does not support any SSL protocol version between %s and %s.",
605 errmsg(
"could not accept SSL connection: EOF detected")));
610 errmsg(
"unrecognized SSL error code: %d",
618 port->alpn_used =
false;
620 const unsigned char *selected;
626 if (selected !=
NULL)
631 port->alpn_used =
true;
638 errmsg(
"received SSL connection request with unexpected ALPN protocol")));
649 port->peer_cert_valid =
false;
682 errmsg(
"SSL certificate's common name contains embedded null")));
687 port->peer_cn = peer_cn;
728 errmsg(
"SSL certificate's distinguished name contains embedded null")));
738 port->peer_dn = peer_dn;
740 port->peer_cert_valid =
true;
754 port->ssl_in_use =
false;
805 if (n != -1 ||
errno == 0)
825 errmsg(
"unrecognized SSL error code: %d",
870 if (n != -1 ||
errno == 0)
895 errmsg(
"unrecognized SSL error code: %d",
933 port->last_read_was_eof = res == 0;
1062 errmsg(
"could not open DH parameters file \"%s\": %m",
1074 errmsg(
"could not load DH parameters file: %s",
1084 errmsg(
"invalid DH parameters: %s",
1093 errmsg(
"invalid DH parameters: p is not prime")));
1102 errmsg(
"invalid DH parameters: neither suitable generator or safe prime")));
1143 const char *
prompt =
"Enter PEM pass phrase:";
1180 char *truncated =
name;
1197 truncated[0] = truncated[1] = truncated[2] =
'.';
1247 _(
"Client certificate verification failed at depth %d: %s."),
1284 _(
"Failed certificate data (unverified): subject \"%s\", serial number %s, issuer \"%s\"."),
1357 const unsigned char **out,
1358 unsigned char *outlen,
1359 const unsigned char *in,
1422 errmsg(
"DH: could not load DH parameters")));
1430 errmsg(
"DH: could not set DH parameters: %s",
1448#ifndef OPENSSL_NO_ECDH
1460 errmsg(
"could not set group names specified in ssl_groups: %s",
1462 _(
"No valid groups found"))),
1463 errhint(
"Ensure that each group name is spelled correctly and supported by the installed version of OpenSSL."));
1505 const char *errreason;
1506 static char errbuf[36];
1509 return _(
"no SSL error reported");
1511 if (errreason !=
NULL)
1521#ifdef ERR_SYSTEM_ERROR
1527 snprintf(errbuf,
sizeof(errbuf),
_(
"SSL error code %lu"),
ecode);
1622#if HAVE_X509_GET_SIGNATURE_INFO
1628 elog(
ERROR,
"could not determine server certificate signature algorithm");
1645 elog(
ERROR,
"could not find digest for NID %s",
1652 elog(
ERROR,
"could not generate server certificate hash");
1684 errmsg(
"could not create BIO")));
1687 for (
i = 0;
i < count;
i++)
1694 errmsg(
"could not get NID for ASN1_OBJECT object")));
1702 errmsg(
"could not convert NID %d to an ASN1_OBJECT structure",
nid)));
1719 elog(
ERROR,
"could not free OpenSSL BIO structure");
1748#ifdef TLS1_1_VERSION
1754#ifdef TLS1_2_VERSION
1760#ifdef TLS1_3_VERSION
1790 return "(unrecognized)";
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
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_dh_params_file
int ssl_min_protocol_version
ssize_t secure_raw_read(Port *port, void *ptr, size_t len)
bool SSLPreferServerCiphers
int ssl_max_protocol_version
char * ssl_passphrase_command
bool ssl_passphrase_command_supports_reload
ssize_t secure_raw_write(Port *port, const void *ptr, size_t len)
#define Assert(condition)
int errcode_for_socket_access(void)
int errmsg_internal(const char *fmt,...)
int errdetail_internal(const char *fmt,...)
int errcode_for_file_access(void)
int errdetail(const char *fmt,...)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
void err(int eval, const char *fmt,...)
FILE * AllocateFile(const char *name, const char *mode)
const char * GetConfigOption(const char *name, bool missing_ok, bool restrict_privileged)
int WaitLatchOrSocket(Latch *latch, int wakeEvents, pgsocket sock, long timeout, uint32 wait_event_info)
char * pg_any_to_server(const char *s, int len, int encoding)
void * MemoryContextAlloc(MemoryContext context, Size size)
char * pstrdup(const char *in)
void pfree(void *pointer)
MemoryContext TopMemoryContext
static char buf[DEFAULT_XLOG_SEG_SIZE]
size_t strlcpy(char *dst, const char *src, size_t siz)
#define PG_ALPN_PROTOCOL_VECTOR
static unsigned hash(unsigned *uv, int n)
char * pg_clean_ascii(const char *str, int alloc_flags)
void appendStringInfo(StringInfo str, const char *fmt,...)
void appendStringInfoChar(StringInfo str, char ch)
void initStringInfo(StringInfo str)
#define WL_SOCKET_READABLE
#define WL_EXIT_ON_PM_DEATH
#define WL_SOCKET_WRITEABLE