Age Owner Branch data TLA Line data Source code
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 */
56 : : static void default_openssl_tls_init(SSL_CTX *context, bool isServerStart);
57 : : openssl_tls_init_hook_typ openssl_tls_init_hook = default_openssl_tls_init;
58 : :
59 : : static int port_bio_read(BIO *h, char *buf, int size);
60 : : static int port_bio_write(BIO *h, const char *buf, int size);
61 : : static BIO_METHOD *port_bio_method(void);
62 : : static int ssl_set_port_bio(Port *port);
63 : :
64 : : static DH *load_dh_file(char *filename, bool isServerStart);
65 : : static DH *load_dh_buffer(const char *buffer, size_t len);
66 : : static int ssl_external_passwd_cb(char *buf, int size, int rwflag, void *userdata);
67 : : static int dummy_ssl_passwd_cb(char *buf, int size, int rwflag, void *userdata);
68 : : static int verify_cb(int ok, X509_STORE_CTX *ctx);
69 : : static void info_cb(const SSL *ssl, int type, int args);
70 : : static 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);
76 : : static bool initialize_dh(SSL_CTX *context, bool isServerStart);
77 : : static bool initialize_ecdh(SSL_CTX *context, bool isServerStart);
78 : : static const char *SSLerrmessageExt(unsigned long ecode, const char *replacement);
79 : : static const char *SSLerrmessage(unsigned long ecode);
80 : :
81 : : static char *X509_NAME_to_cstring(X509_NAME *name);
82 : :
83 : : static SSL_CTX *SSL_context = NULL;
84 : : static bool dummy_ssl_passwd_cb_called = false;
85 : : static bool ssl_is_server_start;
86 : :
87 : : static int ssl_protocol_version_to_openssl(int v);
88 : : static const char *ssl_protocol_version_to_string(int v);
89 : :
90 : : struct CallbackErr
91 : : {
92 : : /*
93 : : * Storage for passing certificate verification error logging from the
94 : : * callback.
95 : : */
96 : : char *cert_errdetail;
97 : : };
98 : :
99 : : /* ------------------------------------------------------------ */
100 : : /* Public interface */
101 : : /* ------------------------------------------------------------ */
102 : :
103 : : int
3268 tgl@sss.pgh.pa.us 104 :CBC 33 : be_tls_init(bool isServerStart)
105 : : {
106 : : SSL_CTX *context;
2094 michael@paquier.xyz 107 : 33 : int ssl_ver_min = -1;
108 : 33 : 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 : : */
3270 tgl@sss.pgh.pa.us 120 : 33 : context = SSL_CTX_new(SSLv23_method());
121 [ - + ]: 33 : if (!context)
122 : : {
3268 tgl@sss.pgh.pa.us 123 [ # # # # ]:UBC 0 : ereport(isServerStart ? FATAL : LOG,
124 : : (errmsg("could not create SSL context: %s",
125 : : SSLerrmessage(ERR_get_error()))));
3270 126 : 0 : 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 : : */
3270 tgl@sss.pgh.pa.us 133 :CBC 33 : SSL_CTX_set_mode(context, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
134 : :
135 : : /*
136 : : * Call init hook (usually to set password callback)
137 : : */
2042 138 : 33 : (*openssl_tls_init_hook) (context, isServerStart);
139 : :
140 : : /* used by the callback */
2850 peter_e@gmx.net 141 : 33 : ssl_is_server_start = isServerStart;
142 : :
143 : : /*
144 : : * Load and verify server's certificate and private key
145 : : */
3270 tgl@sss.pgh.pa.us 146 [ - + ]: 33 : if (SSL_CTX_use_certificate_chain_file(context, ssl_cert_file) != 1)
147 : : {
3268 tgl@sss.pgh.pa.us 148 [ # # # # ]:UBC 0 : 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()))));
3270 152 : 0 : goto error;
153 : : }
154 : :
2888 peter_e@gmx.net 155 [ - + ]:CBC 33 : if (!check_ssl_key_file_permissions(ssl_key_file, isServerStart))
3270 tgl@sss.pgh.pa.us 156 :UBC 0 : goto error;
157 : :
158 : : /*
159 : : * OK, try to load the private key file.
160 : : */
2850 peter_e@gmx.net 161 :CBC 33 : dummy_ssl_passwd_cb_called = false;
162 : :
3270 tgl@sss.pgh.pa.us 163 [ + + ]: 33 : if (SSL_CTX_use_PrivateKey_file(context,
164 : : ssl_key_file,
165 : : SSL_FILETYPE_PEM) != 1)
166 : : {
2850 peter_e@gmx.net 167 [ + + ]: 3 : if (dummy_ssl_passwd_cb_called)
3268 tgl@sss.pgh.pa.us 168 [ - + + - ]:GBC 1 : 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
3268 tgl@sss.pgh.pa.us 173 [ + - + - ]:CBC 2 : 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()))));
3270 tgl@sss.pgh.pa.us 177 :GBC 1 : goto error;
178 : : }
179 : :
3270 tgl@sss.pgh.pa.us 180 [ - + ]:CBC 30 : if (SSL_CTX_check_private_key(context) != 1)
181 : : {
3268 tgl@sss.pgh.pa.us 182 [ # # # # ]:UBC 0 : ereport(isServerStart ? FATAL : LOG,
183 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
184 : : errmsg("check of private key failed: %s",
185 : : SSLerrmessage(ERR_get_error()))));
3270 186 : 0 : goto error;
187 : : }
188 : :
2583 peter_e@gmx.net 189 [ + - ]:CBC 30 : if (ssl_min_protocol_version)
190 : : {
2094 michael@paquier.xyz 191 : 30 : ssl_ver_min = ssl_protocol_version_to_openssl(ssl_min_protocol_version);
192 : :
193 [ - + ]: 30 : if (ssl_ver_min == -1)
194 : : {
2094 michael@paquier.xyz 195 [ # # # # ]:UBC 0 : 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))));
2503 peter@eisentraut.org 201 : 0 : goto error;
202 : : }
203 : :
2094 michael@paquier.xyz 204 [ - + ]:CBC 30 : if (!SSL_CTX_set_min_proto_version(context, ssl_ver_min))
205 : : {
2271 peter@eisentraut.org 206 [ # # # # ]:UBC 0 : ereport(isServerStart ? FATAL : LOG,
207 : : (errmsg("could not set minimum SSL protocol version")));
208 : 0 : goto error;
209 : : }
210 : : }
211 : :
2583 peter_e@gmx.net 212 [ + + ]:CBC 30 : if (ssl_max_protocol_version)
213 : : {
2094 michael@paquier.xyz 214 : 1 : ssl_ver_max = ssl_protocol_version_to_openssl(ssl_max_protocol_version);
215 : :
216 [ - + ]: 1 : if (ssl_ver_max == -1)
217 : : {
2094 michael@paquier.xyz 218 [ # # # # ]:UBC 0 : 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))));
2503 peter@eisentraut.org 224 : 0 : goto error;
225 : : }
226 : :
2094 michael@paquier.xyz 227 [ - + ]:CBC 1 : if (!SSL_CTX_set_max_proto_version(context, ssl_ver_max))
228 : : {
2271 peter@eisentraut.org 229 [ # # # # ]:UBC 0 : ereport(isServerStart ? FATAL : LOG,
230 : : (errmsg("could not set maximum SSL protocol version")));
231 : 0 : goto error;
232 : : }
233 : : }
234 : :
235 : : /* Check compatibility of min/max protocols */
2094 michael@paquier.xyz 236 [ + - + + ]:CBC 30 : if (ssl_min_protocol_version &&
237 : : ssl_max_protocol_version)
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 [ + - ]: 1 : if (ssl_ver_min > ssl_ver_max)
244 : : {
245 [ + - + - ]: 1 : 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")));
2056 michael@paquier.xyz 251 :UBC 0 : 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
508 dgustafsson@postgres 265 :CBC 29 : SSL_CTX_set_num_tickets(context, 0);
266 : : #endif
484 267 : 29 : SSL_CTX_set_options(context, SSL_OP_NO_TICKET);
268 : :
269 : : /* disallow SSL session caching, too */
3056 tgl@sss.pgh.pa.us 270 : 29 : SSL_CTX_set_session_cache_mode(context, SSL_SESS_CACHE_OFF);
271 : :
272 : : /* disallow SSL compression */
1743 michael@paquier.xyz 273 : 29 : 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
1666 284 : 29 : 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 */
3060 heikki.linnakangas@i 291 [ - + ]: 29 : if (!initialize_dh(context, isServerStart))
3060 heikki.linnakangas@i 292 :UBC 0 : goto error;
3268 tgl@sss.pgh.pa.us 293 [ - + ]:CBC 29 : if (!initialize_ecdh(context, isServerStart))
3270 tgl@sss.pgh.pa.us 294 :UBC 0 : goto error;
295 : :
296 : : /* set up the allowed cipher list for TLSv1.2 and below */
418 dgustafsson@postgres 297 [ - + ]:CBC 27 : if (SSL_CTX_set_cipher_list(context, SSLCipherList) != 1)
298 : : {
3268 tgl@sss.pgh.pa.us 299 [ # # # # ]:UBC 0 : ereport(isServerStart ? FATAL : LOG,
300 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
301 : : errmsg("could not set the TLSv1.2 cipher list (no valid ciphers available)")));
3270 302 : 0 : 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 : : */
418 dgustafsson@postgres 309 [ + + ]:CBC 27 : if (SSLCipherSuites[0])
310 : : {
311 : : /* set up the allowed cipher suites */
312 [ - + ]: 23 : if (SSL_CTX_set_ciphersuites(context, SSLCipherSuites) != 1)
313 : : {
418 dgustafsson@postgres 314 [ # # # # ]:UBC 0 : 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 : 0 : goto error;
318 : : }
319 : : }
320 : :
321 : : /* Let server choose order */
4138 heikki.linnakangas@i 322 [ + - ]:CBC 27 : if (SSLPreferServerCiphers)
3270 tgl@sss.pgh.pa.us 323 : 27 : 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 : : */
4138 heikki.linnakangas@i 328 [ + + ]: 27 : if (ssl_ca_file[0])
329 : : {
330 : : STACK_OF(X509_NAME) * root_cert_list;
331 : :
3270 tgl@sss.pgh.pa.us 332 [ + - - + ]: 46 : if (SSL_CTX_load_verify_locations(context, ssl_ca_file, NULL) != 1 ||
4138 heikki.linnakangas@i 333 : 23 : (root_cert_list = SSL_load_client_CA_file(ssl_ca_file)) == NULL)
334 : : {
3268 tgl@sss.pgh.pa.us 335 [ # # # # ]:UBC 0 : 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()))));
3270 339 : 0 : 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 : : */
1736 tgl@sss.pgh.pa.us 349 :CBC 23 : 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 : 23 : 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 : : */
1762 peter@eisentraut.org 367 [ + + - + ]: 27 : if (ssl_crl_file[0] || ssl_crl_dir[0])
368 : : {
3270 tgl@sss.pgh.pa.us 369 : 23 : X509_STORE *cvstore = SSL_CTX_get_cert_store(context);
370 : :
4138 heikki.linnakangas@i 371 [ + - ]: 23 : if (cvstore)
372 : : {
373 : : /* Set the flags to check against the complete CRL chain */
1762 peter@eisentraut.org 374 [ + - ]: 46 : if (X509_STORE_load_locations(cvstore,
375 [ + - ]: 23 : ssl_crl_file[0] ? ssl_crl_file : NULL,
1736 tgl@sss.pgh.pa.us 376 [ + + ]: 23 : ssl_crl_dir[0] ? ssl_crl_dir : NULL)
377 : : == 1)
378 : : {
4138 heikki.linnakangas@i 379 : 23 : X509_STORE_set_flags(cvstore,
380 : : X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL);
381 : : }
1762 peter@eisentraut.org 382 [ # # ]:UBC 0 : else if (ssl_crl_dir[0] == 0)
383 : : {
3268 tgl@sss.pgh.pa.us 384 [ # # # # ]: 0 : 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()))));
3270 388 : 0 : goto error;
389 : : }
1762 peter@eisentraut.org 390 [ # # ]: 0 : else if (ssl_crl_file[0] == 0)
391 : : {
392 [ # # # # ]: 0 : 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 : 0 : goto error;
397 : : }
398 : : else
399 : : {
400 [ # # # # ]: 0 : 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",
403 : : ssl_crl_file, ssl_crl_dir,
404 : : SSLerrmessage(ERR_get_error()))));
405 : 0 : goto error;
406 : : }
407 : : }
408 : : }
409 : :
410 : : /*
411 : : * Success! Replace any existing SSL_context.
412 : : */
3270 tgl@sss.pgh.pa.us 413 [ + + ]:CBC 27 : if (SSL_context)
3270 tgl@sss.pgh.pa.us 414 :GBC 1 : SSL_CTX_free(SSL_context);
415 : :
3270 tgl@sss.pgh.pa.us 416 :CBC 27 : SSL_context = context;
417 : :
418 : : /*
419 : : * Set flag to remember whether CA store has been loaded into SSL_context.
420 : : */
421 [ + + ]: 27 : if (ssl_ca_file[0])
422 : 23 : ssl_loaded_verify_locations = true;
423 : : else
424 : 4 : ssl_loaded_verify_locations = false;
425 : :
426 : 27 : return 0;
427 : :
428 : : /* Clean up by releasing working context. */
3270 tgl@sss.pgh.pa.us 429 :GBC 1 : error:
430 [ + - ]: 1 : if (context)
431 : 1 : SSL_CTX_free(context);
432 : 1 : return -1;
433 : : }
434 : :
435 : : void
3270 tgl@sss.pgh.pa.us 436 :CBC 135 : be_tls_destroy(void)
437 : : {
438 [ + + ]: 135 : if (SSL_context)
439 : 1 : SSL_CTX_free(SSL_context);
440 : 135 : SSL_context = NULL;
441 : 135 : ssl_loaded_verify_locations = false;
4145 heikki.linnakangas@i 442 : 135 : }
443 : :
444 : : int
4138 445 : 153 : be_tls_open_server(Port *port)
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 [ - + ]: 153 : Assert(!port->ssl);
455 [ - + ]: 153 : Assert(!port->peer);
456 : :
3270 tgl@sss.pgh.pa.us 457 [ - + ]: 153 : if (!SSL_context)
458 : : {
3270 tgl@sss.pgh.pa.us 459 [ # # ]:UBC 0 : ereport(COMMERROR,
460 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
461 : : errmsg("could not initialize SSL connection: SSL context not set up")));
462 : 0 : return -1;
463 : : }
464 : :
465 : : /* set up debugging/info callback */
1789 michael@paquier.xyz 466 :CBC 153 : SSL_CTX_set_info_callback(SSL_context, info_cb);
467 : :
468 : : /* enable ALPN */
617 heikki.linnakangas@i 469 : 153 : SSL_CTX_set_alpn_select_cb(SSL_context, alpn_cb, port);
470 : :
4138 471 [ - + ]: 153 : if (!(port->ssl = SSL_new(SSL_context)))
472 : : {
4138 heikki.linnakangas@i 473 [ # # ]:UBC 0 : ereport(COMMERROR,
474 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
475 : : errmsg("could not initialize SSL connection: %s",
476 : : SSLerrmessage(ERR_get_error()))));
477 : 0 : return -1;
478 : : }
431 dgustafsson@postgres 479 [ - + ]:CBC 153 : if (!ssl_set_port_bio(port))
480 : : {
4138 heikki.linnakangas@i 481 [ # # ]:UBC 0 : ereport(COMMERROR,
482 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
483 : : errmsg("could not set SSL socket: %s",
484 : : SSLerrmessage(ERR_get_error()))));
485 : 0 : return -1;
486 : : }
487 : :
34 dgustafsson@postgres 488 :GNC 153 : err_context.cert_errdetail = NULL;
489 : 153 : SSL_set_ex_data(port->ssl, 0, &err_context);
490 : :
4138 heikki.linnakangas@i 491 :CBC 153 : port->ssl_in_use = true;
492 : :
493 : 423 : aloop:
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 : : */
736 tgl@sss.pgh.pa.us 503 : 423 : errno = 0;
3539 peter_e@gmx.net 504 : 423 : ERR_clear_error();
4138 heikki.linnakangas@i 505 : 423 : r = SSL_accept(port->ssl);
506 [ + + ]: 423 : if (r <= 0)
507 : : {
508 : 289 : 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 : : */
3539 peter_e@gmx.net 518 : 289 : ecode = ERR_get_error();
4138 heikki.linnakangas@i 519 [ + - + - : 289 : switch (err)
- ]
520 : : {
521 : 270 : case SSL_ERROR_WANT_READ:
522 : : case SSL_ERROR_WANT_WRITE:
523 : : /* not allowed during connection establishment */
3969 andres@anarazel.de 524 [ - + ]: 270 : 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 [ + - ]: 270 : if (err == SSL_ERROR_WANT_READ)
2578 tmunro@postgresql.or 532 : 270 : waitfor = WL_SOCKET_READABLE | WL_EXIT_ON_PM_DEATH;
533 : : else
2578 tmunro@postgresql.or 534 :UBC 0 : waitfor = WL_SOCKET_WRITEABLE | WL_EXIT_ON_PM_DEATH;
535 : :
437 heikki.linnakangas@i 536 :CBC 270 : (void) WaitLatchOrSocket(NULL, waitfor, port->sock, 0,
537 : : WAIT_EVENT_SSL_OPEN_SERVER);
4138 538 : 270 : goto aloop;
4138 heikki.linnakangas@i 539 :UBC 0 : case SSL_ERROR_SYSCALL:
736 tgl@sss.pgh.pa.us 540 [ # # # # ]: 0 : if (r < 0 && errno != 0)
4138 heikki.linnakangas@i 541 [ # # ]: 0 : ereport(COMMERROR,
542 : : (errcode_for_socket_access(),
543 : : errmsg("could not accept SSL connection: %m")));
544 : : else
545 [ # # ]: 0 : ereport(COMMERROR,
546 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
547 : : errmsg("could not accept SSL connection: EOF detected")));
548 : 0 : break;
4138 heikki.linnakangas@i 549 :CBC 19 : case SSL_ERROR_SSL:
1998 tgl@sss.pgh.pa.us 550 [ - + ]: 19 : 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 : : */
1998 tgl@sss.pgh.pa.us 565 :UBC 0 : 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 : 0 : give_proto_hint = true;
581 : 0 : break;
1998 tgl@sss.pgh.pa.us 582 :CBC 19 : default:
583 : 19 : give_proto_hint = false;
584 : 19 : break;
585 : : }
4138 heikki.linnakangas@i 586 [ + - + + : 19 : ereport(COMMERROR,
- + - - -
- ]
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.",
593 : : ssl_min_protocol_version ?
594 : : ssl_protocol_version_to_string(ssl_min_protocol_version) :
595 : : MIN_OPENSSL_TLS_VERSION,
596 : : ssl_max_protocol_version ?
597 : : ssl_protocol_version_to_string(ssl_max_protocol_version) :
598 : : MAX_OPENSSL_TLS_VERSION) : 0));
34 dgustafsson@postgres 599 [ + + ]:GNC 19 : if (err_context.cert_errdetail)
600 : 6 : pfree(err_context.cert_errdetail);
4138 heikki.linnakangas@i 601 :CBC 19 : break;
4138 heikki.linnakangas@i 602 :UBC 0 : case SSL_ERROR_ZERO_RETURN:
603 [ # # ]: 0 : ereport(COMMERROR,
604 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
605 : : errmsg("could not accept SSL connection: EOF detected")));
606 : 0 : break;
607 : 0 : default:
608 [ # # ]: 0 : ereport(COMMERROR,
609 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
610 : : errmsg("unrecognized SSL error code: %d",
611 : : err)));
612 : 0 : break;
613 : : }
4138 heikki.linnakangas@i 614 :CBC 19 : return -1;
615 : : }
616 : :
617 : : /* Get the protocol selected by ALPN */
617 618 : 134 : port->alpn_used = false;
619 : : {
620 : : const unsigned char *selected;
621 : : unsigned int len;
622 : :
623 : 134 : SSL_get0_alpn_selected(port->ssl, &selected, &len);
624 : :
625 : : /* If ALPN is used, check that we negotiated the expected protocol */
626 [ + - ]: 134 : if (selected != NULL)
627 : : {
628 [ + - ]: 134 : if (len == strlen(PG_ALPN_PROTOCOL) &&
629 [ + - ]: 134 : memcmp(selected, PG_ALPN_PROTOCOL, strlen(PG_ALPN_PROTOCOL)) == 0)
630 : : {
631 : 134 : port->alpn_used = true;
632 : : }
633 : : else
634 : : {
635 : : /* shouldn't happen */
617 heikki.linnakangas@i 636 [ # # ]:UBC 0 : ereport(COMMERROR,
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. */
4138 heikki.linnakangas@i 644 :CBC 134 : port->peer = SSL_get_peer_certificate(port->ssl);
645 : :
646 : : /* and extract the Common Name and Distinguished Name from it. */
647 : 134 : port->peer_cn = NULL;
1723 andrew@dunslane.net 648 : 134 : port->peer_dn = NULL;
4138 heikki.linnakangas@i 649 : 134 : port->peer_cert_valid = false;
650 [ + + ]: 134 : if (port->peer != NULL)
651 : : {
652 : : int len;
1723 andrew@dunslane.net 653 : 29 : X509_NAME *x509name = X509_get_subject_name(port->peer);
654 : : char *peer_dn;
655 : 29 : BIO *bio = NULL;
656 : 29 : BUF_MEM *bio_buf = NULL;
657 : :
658 : 29 : len = X509_NAME_get_text_by_NID(x509name, NID_commonName, NULL, 0);
4138 heikki.linnakangas@i 659 [ + - ]: 29 : if (len != -1)
660 : : {
661 : : char *peer_cn;
662 : :
663 : 29 : peer_cn = MemoryContextAlloc(TopMemoryContext, len + 1);
1723 andrew@dunslane.net 664 : 29 : r = X509_NAME_get_text_by_NID(x509name, NID_commonName, peer_cn,
665 : : len + 1);
4138 heikki.linnakangas@i 666 : 29 : peer_cn[len] = '\0';
667 [ - + ]: 29 : if (r != len)
668 : : {
669 : : /* shouldn't happen */
4138 heikki.linnakangas@i 670 :UBC 0 : pfree(peer_cn);
671 : 0 : return -1;
672 : : }
673 : :
674 : : /*
675 : : * Reject embedded NULLs in certificate common name to prevent
676 : : * attacks like CVE-2009-4034.
677 : : */
4138 heikki.linnakangas@i 678 [ - + ]:CBC 29 : if (len != strlen(peer_cn))
679 : : {
4138 heikki.linnakangas@i 680 [ # # ]:UBC 0 : ereport(COMMERROR,
681 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
682 : : errmsg("SSL certificate's common name contains embedded null")));
683 : 0 : pfree(peer_cn);
684 : 0 : return -1;
685 : : }
686 : :
4138 heikki.linnakangas@i 687 :CBC 29 : port->peer_cn = peer_cn;
688 : : }
689 : :
1723 andrew@dunslane.net 690 : 29 : bio = BIO_new(BIO_s_mem());
691 [ - + ]: 29 : if (!bio)
692 : : {
816 dgustafsson@postgres 693 [ # # ]:UBC 0 : if (port->peer_cn != NULL)
694 : : {
695 : 0 : pfree(port->peer_cn);
696 : 0 : port->peer_cn = NULL;
697 : : }
1723 andrew@dunslane.net 698 : 0 : 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 : : */
816 dgustafsson@postgres 708 [ + - - + ]:CBC 58 : if (X509_NAME_print_ex(bio, x509name, 0, XN_FLAG_RFC2253) == -1 ||
709 : 29 : BIO_get_mem_ptr(bio, &bio_buf) <= 0)
710 : : {
1723 andrew@dunslane.net 711 :UBC 0 : BIO_free(bio);
816 dgustafsson@postgres 712 [ # # ]: 0 : if (port->peer_cn != NULL)
713 : : {
714 : 0 : pfree(port->peer_cn);
715 : 0 : port->peer_cn = NULL;
716 : : }
1723 andrew@dunslane.net 717 : 0 : return -1;
718 : : }
1723 andrew@dunslane.net 719 :CBC 29 : peer_dn = MemoryContextAlloc(TopMemoryContext, bio_buf->length + 1);
720 : 29 : memcpy(peer_dn, bio_buf->data, bio_buf->length);
721 : 29 : len = bio_buf->length;
722 : 29 : BIO_free(bio);
723 : 29 : peer_dn[len] = '\0';
724 [ - + ]: 29 : if (len != strlen(peer_dn))
725 : : {
1723 andrew@dunslane.net 726 [ # # ]:UBC 0 : ereport(COMMERROR,
727 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
728 : : errmsg("SSL certificate's distinguished name contains embedded null")));
729 : 0 : pfree(peer_dn);
816 dgustafsson@postgres 730 [ # # ]: 0 : if (port->peer_cn != NULL)
731 : : {
732 : 0 : pfree(port->peer_cn);
733 : 0 : port->peer_cn = NULL;
734 : : }
1723 andrew@dunslane.net 735 : 0 : return -1;
736 : : }
737 : :
1723 andrew@dunslane.net 738 :CBC 29 : port->peer_dn = peer_dn;
739 : :
4138 heikki.linnakangas@i 740 : 29 : port->peer_cert_valid = true;
741 : : }
742 : :
743 : 134 : return 0;
744 : : }
745 : :
746 : : void
747 : 153 : be_tls_close(Port *port)
748 : : {
749 [ + - ]: 153 : if (port->ssl)
750 : : {
751 : 153 : SSL_shutdown(port->ssl);
752 : 153 : SSL_free(port->ssl);
753 : 153 : port->ssl = NULL;
754 : 153 : port->ssl_in_use = false;
755 : : }
756 : :
757 [ + + ]: 153 : if (port->peer)
758 : : {
759 : 29 : X509_free(port->peer);
760 : 29 : port->peer = NULL;
761 : : }
762 : :
763 [ + + ]: 153 : if (port->peer_cn)
764 : : {
765 : 29 : pfree(port->peer_cn);
766 : 29 : port->peer_cn = NULL;
767 : : }
768 : :
1723 andrew@dunslane.net 769 [ + + ]: 153 : if (port->peer_dn)
770 : : {
771 : 29 : pfree(port->peer_dn);
772 : 29 : port->peer_dn = NULL;
773 : : }
4145 heikki.linnakangas@i 774 : 153 : }
775 : :
776 : : ssize_t
3959 777 : 642 : be_tls_read(Port *port, void *ptr, size_t len, int *waitfor)
778 : : {
779 : : ssize_t n;
780 : : int err;
781 : : unsigned long ecode;
782 : :
4138 783 : 642 : errno = 0;
3539 peter_e@gmx.net 784 : 642 : ERR_clear_error();
4138 heikki.linnakangas@i 785 : 642 : n = SSL_read(port->ssl, ptr, len);
786 : 642 : err = SSL_get_error(port->ssl, n);
3539 peter_e@gmx.net 787 [ + + - + ]: 642 : ecode = (err != SSL_ERROR_NONE || n < 0) ? ERR_get_error() : 0;
4138 heikki.linnakangas@i 788 [ + + - - : 642 : switch (err)
- + - ]
789 : : {
790 : 339 : case SSL_ERROR_NONE:
791 : : /* a-ok */
4145 792 : 339 : break;
4138 793 : 286 : case SSL_ERROR_WANT_READ:
3959 794 : 286 : *waitfor = WL_SOCKET_READABLE;
795 : 286 : errno = EWOULDBLOCK;
796 : 286 : n = -1;
797 : 286 : break;
4138 heikki.linnakangas@i 798 :UBC 0 : case SSL_ERROR_WANT_WRITE:
3959 799 : 0 : *waitfor = WL_SOCKET_WRITEABLE;
800 : 0 : errno = EWOULDBLOCK;
801 : 0 : n = -1;
802 : 0 : break;
4138 803 : 0 : case SSL_ERROR_SYSCALL:
804 : : /* leave it to caller to ereport the value of errno */
736 tgl@sss.pgh.pa.us 805 [ # # # # ]: 0 : if (n != -1 || errno == 0)
806 : : {
4138 heikki.linnakangas@i 807 : 0 : errno = ECONNRESET;
808 : 0 : n = -1;
809 : : }
4145 810 : 0 : break;
4138 811 : 0 : case SSL_ERROR_SSL:
812 [ # # ]: 0 : ereport(COMMERROR,
813 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
814 : : errmsg("SSL error: %s", SSLerrmessage(ecode))));
815 : 0 : errno = ECONNRESET;
816 : 0 : n = -1;
4145 817 : 0 : break;
3088 heikki.linnakangas@i 818 :CBC 17 : case SSL_ERROR_ZERO_RETURN:
819 : : /* connection was cleanly shut down by peer */
820 : 17 : n = 0;
821 : 17 : break;
4145 heikki.linnakangas@i 822 :UBC 0 : default:
4138 823 [ # # ]: 0 : ereport(COMMERROR,
824 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
825 : : errmsg("unrecognized SSL error code: %d",
826 : : err)));
827 : 0 : errno = ECONNRESET;
828 : 0 : n = -1;
4145 829 : 0 : break;
830 : : }
831 : :
4138 heikki.linnakangas@i 832 :CBC 642 : return n;
833 : : }
834 : :
835 : : ssize_t
310 peter@eisentraut.org 836 : 237 : be_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 : :
4138 heikki.linnakangas@i 842 : 237 : errno = 0;
3539 peter_e@gmx.net 843 : 237 : ERR_clear_error();
4138 heikki.linnakangas@i 844 : 237 : n = SSL_write(port->ssl, ptr, len);
845 : 237 : err = SSL_get_error(port->ssl, n);
3539 peter_e@gmx.net 846 [ + - - + ]: 237 : ecode = (err != SSL_ERROR_NONE || n < 0) ? ERR_get_error() : 0;
4138 heikki.linnakangas@i 847 [ + - - - : 237 : switch (err)
- - - ]
848 : : {
849 : 237 : case SSL_ERROR_NONE:
850 : : /* a-ok */
851 : 237 : break;
4138 heikki.linnakangas@i 852 :UBC 0 : case SSL_ERROR_WANT_READ:
3959 853 : 0 : *waitfor = WL_SOCKET_READABLE;
854 : 0 : errno = EWOULDBLOCK;
855 : 0 : n = -1;
856 : 0 : break;
4138 857 : 0 : case SSL_ERROR_WANT_WRITE:
3959 858 : 0 : *waitfor = WL_SOCKET_WRITEABLE;
859 : 0 : errno = EWOULDBLOCK;
860 : 0 : n = -1;
861 : 0 : break;
4138 862 : 0 : 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 : : */
736 tgl@sss.pgh.pa.us 870 [ # # # # ]: 0 : if (n != -1 || errno == 0)
871 : : {
4138 heikki.linnakangas@i 872 : 0 : errno = ECONNRESET;
873 : 0 : n = -1;
874 : : }
875 : 0 : break;
876 : 0 : case SSL_ERROR_SSL:
877 [ # # ]: 0 : ereport(COMMERROR,
878 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
879 : : errmsg("SSL error: %s", SSLerrmessage(ecode))));
3088 880 : 0 : errno = ECONNRESET;
881 : 0 : n = -1;
882 : 0 : break;
4138 883 : 0 : case SSL_ERROR_ZERO_RETURN:
884 : :
885 : : /*
886 : : * the SSL connection was closed, leave it to the caller to
887 : : * ereport it
888 : : */
889 : 0 : errno = ECONNRESET;
890 : 0 : n = -1;
891 : 0 : break;
892 : 0 : default:
893 [ # # ]: 0 : ereport(COMMERROR,
894 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
895 : : errmsg("unrecognized SSL error code: %d",
896 : : err)));
897 : 0 : errno = ECONNRESET;
898 : 0 : n = -1;
899 : 0 : break;
900 : : }
901 : :
4138 heikki.linnakangas@i 902 :CBC 237 : 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 : :
921 : : static BIO_METHOD *port_bio_method_ptr = NULL;
922 : :
923 : : static int
431 dgustafsson@postgres 924 : 2410 : port_bio_read(BIO *h, char *buf, int size)
925 : : {
4138 heikki.linnakangas@i 926 : 2410 : int res = 0;
431 dgustafsson@postgres 927 : 2410 : Port *port = (Port *) BIO_get_data(h);
928 : :
4138 heikki.linnakangas@i 929 [ + - ]: 2410 : if (buf != NULL)
930 : : {
431 dgustafsson@postgres 931 : 2410 : res = secure_raw_read(port, buf, size);
4138 heikki.linnakangas@i 932 : 2410 : BIO_clear_retry_flags(h);
431 dgustafsson@postgres 933 : 2410 : port->last_read_was_eof = res == 0;
4138 heikki.linnakangas@i 934 [ + + ]: 2410 : if (res <= 0)
935 : : {
936 : : /* If we were interrupted, tell caller to retry */
3969 andres@anarazel.de 937 [ + - + + : 560 : if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)
- + ]
938 : : {
4138 heikki.linnakangas@i 939 : 556 : BIO_set_retry_read(h);
940 : : }
941 : : }
942 : : }
943 : :
944 : 2410 : return res;
945 : : }
946 : :
947 : : static int
431 dgustafsson@postgres 948 : 532 : port_bio_write(BIO *h, const char *buf, int size)
949 : : {
4138 heikki.linnakangas@i 950 : 532 : int res = 0;
951 : :
431 dgustafsson@postgres 952 : 532 : res = secure_raw_write(((Port *) BIO_get_data(h)), buf, size);
4138 heikki.linnakangas@i 953 : 532 : BIO_clear_retry_flags(h);
954 [ - + ]: 532 : if (res <= 0)
955 : : {
956 : : /* If we were interrupted, tell caller to retry */
3969 andres@anarazel.de 957 [ # # # # :UBC 0 : if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN)
# # ]
958 : : {
4138 heikki.linnakangas@i 959 : 0 : BIO_set_retry_write(h);
960 : : }
961 : : }
962 : :
4138 heikki.linnakangas@i 963 :CBC 532 : return res;
964 : : }
965 : :
966 : : static long
431 dgustafsson@postgres 967 : 17165 : port_bio_ctrl(BIO *h, int cmd, long num, void *ptr)
968 : : {
969 : : long res;
970 : 17165 : Port *port = (Port *) BIO_get_data(h);
971 : :
972 [ + + + ]: 17165 : switch (cmd)
973 : : {
974 : 4 : 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 : 4 : res = port->last_read_was_eof;
983 : 4 : break;
984 : 295 : case BIO_CTRL_FLUSH:
985 : : /* libssl expects all BIOs to support BIO_flush. */
986 : 295 : res = 1;
987 : 295 : break;
988 : 16866 : default:
989 : 16866 : res = 0;
990 : 16866 : break;
991 : : }
992 : :
993 : 17165 : return res;
994 : : }
995 : :
996 : : static BIO_METHOD *
997 : 153 : port_bio_method(void)
998 : : {
999 [ + - ]: 153 : if (!port_bio_method_ptr)
1000 : : {
1001 : : int my_bio_index;
1002 : :
3379 heikki.linnakangas@i 1003 : 153 : my_bio_index = BIO_get_new_index();
1004 [ - + ]: 153 : if (my_bio_index == -1)
3379 heikki.linnakangas@i 1005 :UBC 0 : return NULL;
431 dgustafsson@postgres 1006 :CBC 153 : my_bio_index |= BIO_TYPE_SOURCE_SINK;
1007 : 153 : port_bio_method_ptr = BIO_meth_new(my_bio_index, "PostgreSQL backend socket");
1008 [ - + ]: 153 : if (!port_bio_method_ptr)
3379 heikki.linnakangas@i 1009 :UBC 0 : return NULL;
431 dgustafsson@postgres 1010 [ + - + - ]:CBC 306 : if (!BIO_meth_set_write(port_bio_method_ptr, port_bio_write) ||
1011 [ - + ]: 306 : !BIO_meth_set_read(port_bio_method_ptr, port_bio_read) ||
1012 : 153 : !BIO_meth_set_ctrl(port_bio_method_ptr, port_bio_ctrl))
1013 : : {
431 dgustafsson@postgres 1014 :UBC 0 : BIO_meth_free(port_bio_method_ptr);
1015 : 0 : port_bio_method_ptr = NULL;
3379 heikki.linnakangas@i 1016 : 0 : return NULL;
1017 : : }
1018 : : }
431 dgustafsson@postgres 1019 :CBC 153 : return port_bio_method_ptr;
1020 : : }
1021 : :
1022 : : static int
1023 : 153 : ssl_set_port_bio(Port *port)
1024 : : {
1025 : : BIO *bio;
1026 : : BIO_METHOD *bio_method;
1027 : :
1028 : 153 : bio_method = port_bio_method();
3379 heikki.linnakangas@i 1029 [ - + ]: 153 : if (bio_method == NULL)
431 dgustafsson@postgres 1030 :UBC 0 : return 0;
1031 : :
431 dgustafsson@postgres 1032 :CBC 153 : bio = BIO_new(bio_method);
4138 heikki.linnakangas@i 1033 [ - + ]: 153 : if (bio == NULL)
431 dgustafsson@postgres 1034 :UBC 0 : return 0;
1035 : :
431 dgustafsson@postgres 1036 :CBC 153 : BIO_set_data(bio, port);
1037 : 153 : BIO_set_init(bio, 1);
1038 : :
4138 heikki.linnakangas@i 1039 : 153 : SSL_set_bio(port->ssl, bio, bio);
431 dgustafsson@postgres 1040 : 153 : 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 : : */
1050 : : static DH *
3060 heikki.linnakangas@i 1051 :UBC 0 : load_dh_file(char *filename, bool isServerStart)
1052 : : {
1053 : : FILE *fp;
4138 1054 : 0 : DH *dh = NULL;
1055 : : int codes;
1056 : :
1057 : : /* attempt to open file. It's not an error if it doesn't exist. */
3060 1058 [ # # ]: 0 : if ((fp = AllocateFile(filename, "r")) == NULL)
1059 : : {
1060 [ # # # # ]: 0 : ereport(isServerStart ? FATAL : LOG,
1061 : : (errcode_for_file_access(),
1062 : : errmsg("could not open DH parameters file \"%s\": %m",
1063 : : filename)));
4138 1064 : 0 : return NULL;
1065 : : }
1066 : :
1067 : 0 : dh = PEM_read_DHparams(fp, NULL, NULL, NULL);
3060 1068 : 0 : FreeFile(fp);
1069 : :
1070 [ # # ]: 0 : if (dh == NULL)
1071 : : {
1072 [ # # # # ]: 0 : 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 : 0 : return NULL;
1077 : : }
1078 : :
1079 : : /* make sure the DH parameters are usable */
1080 [ # # ]: 0 : if (DH_check(dh, &codes) == 0)
1081 : : {
1082 [ # # # # ]: 0 : ereport(isServerStart ? FATAL : LOG,
1083 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1084 : : errmsg("invalid DH parameters: %s",
1085 : : SSLerrmessage(ERR_get_error()))));
1732 tgl@sss.pgh.pa.us 1086 : 0 : DH_free(dh);
3060 heikki.linnakangas@i 1087 : 0 : return NULL;
1088 : : }
1089 [ # # ]: 0 : if (codes & DH_CHECK_P_NOT_PRIME)
1090 : : {
1091 [ # # # # ]: 0 : ereport(isServerStart ? FATAL : LOG,
1092 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1093 : : errmsg("invalid DH parameters: p is not prime")));
1732 tgl@sss.pgh.pa.us 1094 : 0 : DH_free(dh);
3060 heikki.linnakangas@i 1095 : 0 : return NULL;
1096 : : }
1097 [ # # ]: 0 : if ((codes & DH_NOT_SUITABLE_GENERATOR) &&
1098 [ # # ]: 0 : (codes & DH_CHECK_P_NOT_SAFE_PRIME))
1099 : : {
1100 [ # # # # ]: 0 : ereport(isServerStart ? FATAL : LOG,
1101 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1102 : : errmsg("invalid DH parameters: neither suitable generator or safe prime")));
1732 tgl@sss.pgh.pa.us 1103 : 0 : DH_free(dh);
3060 heikki.linnakangas@i 1104 : 0 : return NULL;
1105 : : }
1106 : :
4138 1107 : 0 : 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 : : */
1117 : : static DH *
4138 heikki.linnakangas@i 1118 :CBC 29 : load_dh_buffer(const char *buffer, size_t len)
1119 : : {
1120 : : BIO *bio;
1121 : 29 : DH *dh = NULL;
1122 : :
461 peter@eisentraut.org 1123 : 29 : bio = BIO_new_mem_buf(buffer, len);
4138 heikki.linnakangas@i 1124 [ - + ]: 29 : if (bio == NULL)
4138 heikki.linnakangas@i 1125 :UBC 0 : return NULL;
4138 heikki.linnakangas@i 1126 :CBC 29 : dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
1127 [ - + ]: 29 : if (dh == NULL)
4138 heikki.linnakangas@i 1128 [ # # ]:UBC 0 : ereport(DEBUG2,
1129 : : (errmsg_internal("DH load buffer: %s",
1130 : : SSLerrmessage(ERR_get_error()))));
4138 heikki.linnakangas@i 1131 :CBC 29 : BIO_free(bio);
1132 : :
1133 : 29 : return dh;
1134 : : }
1135 : :
1136 : : /*
1137 : : * Passphrase collection callback using ssl_passphrase_command
1138 : : */
1139 : : static int
2850 peter_e@gmx.net 1140 : 8 : ssl_external_passwd_cb(char *buf, int size, int rwflag, void *userdata)
1141 : : {
1142 : : /* same prompt as OpenSSL uses internally */
1143 : 8 : const char *prompt = "Enter PEM pass phrase:";
1144 : :
1145 [ - + ]: 8 : Assert(rwflag == 0);
1146 : :
1147 : 8 : 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 : : */
1159 : : static int
2850 peter_e@gmx.net 1160 :GBC 1 : dummy_ssl_passwd_cb(char *buf, int size, int rwflag, void *userdata)
1161 : : {
1162 : : /* Set flag to change the error message we'll report */
1163 : 1 : dummy_ssl_passwd_cb_called = true;
1164 : : /* And return empty string */
3269 tgl@sss.pgh.pa.us 1165 [ - + ]: 1 : Assert(size > 0);
1166 : 1 : buf[0] = '\0';
1167 : 1 : 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 : : */
1176 : : static char *
1190 peter@eisentraut.org 1177 :CBC 12 : prepare_cert_name(char *name)
1178 : : {
1250 1179 : 12 : size_t namelen = strlen(name);
1190 1180 : 12 : 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 [ + + ]: 12 : 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 : 1 : truncated = name + namelen - MAXLEN;
1197 : 1 : truncated[0] = truncated[1] = truncated[2] = '.';
1198 : 1 : namelen = MAXLEN;
1199 : : }
1200 : :
1201 : : #undef MAXLEN
1202 : :
1203 : 12 : 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 : : */
1216 : : static int
4138 heikki.linnakangas@i 1217 : 93 : verify_cb(int ok, X509_STORE_CTX *ctx)
1218 : : {
1219 : : int depth;
1220 : : int errcode;
1221 : : const char *errstring;
1222 : : StringInfoData str;
1223 : : X509 *cert;
1224 : : SSL *ssl;
1225 : : struct CallbackErr *cb_err;
1226 : :
1250 peter@eisentraut.org 1227 [ + + ]: 93 : if (ok)
1228 : : {
1229 : : /* Nothing to do for the successful case. */
1230 : 87 : return ok;
1231 : : }
1232 : :
1233 : : /* Pull all the information we have on the verification failure. */
1234 : 6 : depth = X509_STORE_CTX_get_error_depth(ctx);
1235 : 6 : errcode = X509_STORE_CTX_get_error(ctx);
1236 : 6 : 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 : : */
34 dgustafsson@postgres 1242 :GNC 6 : ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
1243 : 6 : cb_err = (struct CallbackErr *) SSL_get_ex_data(ssl, 0);
1244 : :
1250 peter@eisentraut.org 1245 :CBC 6 : initStringInfo(&str);
1246 : 6 : appendStringInfo(&str,
1247 : 6 : _("Client certificate verification failed at depth %d: %s."),
1248 : : depth, errstring);
1249 : :
1250 : 6 : cert = X509_STORE_CTX_get_current_cert(ctx);
1251 [ + - ]: 6 : 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 : 6 : subject = X509_NAME_to_cstring(X509_get_subject_name(cert));
1190 1267 : 6 : sub_prepared = prepare_cert_name(subject);
1268 : 6 : pfree(subject);
1269 : :
1250 1270 : 6 : issuer = X509_NAME_to_cstring(X509_get_issuer_name(cert));
1190 1271 : 6 : iss_prepared = prepare_cert_name(issuer);
1272 : 6 : 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 : : */
1250 1278 : 6 : sn = X509_get_serialNumber(cert);
1279 : 6 : b = ASN1_INTEGER_to_BN(sn, NULL);
1280 : 6 : serialno = BN_bn2dec(b);
1281 : :
1282 : 6 : appendStringInfoChar(&str, '\n');
1283 [ - + ]: 6 : appendStringInfo(&str,
1284 : 6 : _("Failed certificate data (unverified): subject \"%s\", serial number %s, issuer \"%s\"."),
1190 peter@eisentraut.org 1285 :UBC 0 : sub_prepared, serialno ? serialno : _("unknown"),
1286 : : iss_prepared);
1287 : :
1250 peter@eisentraut.org 1288 :CBC 6 : BN_free(b);
1289 : 6 : OPENSSL_free(serialno);
1190 1290 : 6 : pfree(iss_prepared);
1291 : 6 : pfree(sub_prepared);
1292 : : }
1293 : :
1294 : : /* Store our detail message to be logged later. */
34 dgustafsson@postgres 1295 :GNC 6 : cb_err->cert_errdetail = str.data;
1296 : :
4138 heikki.linnakangas@i 1297 :CBC 6 : return ok;
1298 : : }
1299 : :
1300 : : /*
1301 : : * This callback is used to copy SSL information messages
1302 : : * into the PostgreSQL log.
1303 : : */
1304 : : static void
1305 : 2894 : info_cb(const SSL *ssl, int type, int args)
1306 : : {
1307 : : const char *desc;
1308 : :
1789 michael@paquier.xyz 1309 : 2894 : desc = SSL_state_string_long(ssl);
1310 : :
4138 heikki.linnakangas@i 1311 [ + + + + : 2894 : switch (type)
- - + +
- ]
1312 : : {
1313 : 153 : case SSL_CB_HANDSHAKE_START:
1314 [ - + ]: 153 : ereport(DEBUG4,
1315 : : (errmsg_internal("SSL: handshake start: \"%s\"", desc)));
4145 1316 : 153 : break;
4138 1317 : 134 : case SSL_CB_HANDSHAKE_DONE:
1318 [ - + ]: 134 : ereport(DEBUG4,
1319 : : (errmsg_internal("SSL: handshake done: \"%s\"", desc)));
4145 1320 : 134 : break;
4138 1321 : 2014 : case SSL_CB_ACCEPT_LOOP:
1322 [ - + ]: 2014 : ereport(DEBUG4,
1323 : : (errmsg_internal("SSL: accept loop: \"%s\"", desc)));
4145 1324 : 2014 : break;
4138 1325 : 423 : case SSL_CB_ACCEPT_EXIT:
1326 [ - + ]: 423 : ereport(DEBUG4,
1327 : : (errmsg_internal("SSL: accept exit (%d): \"%s\"", args, desc)));
1328 : 423 : break;
4138 heikki.linnakangas@i 1329 :UBC 0 : case SSL_CB_CONNECT_LOOP:
1330 [ # # ]: 0 : ereport(DEBUG4,
1331 : : (errmsg_internal("SSL: connect loop: \"%s\"", desc)));
1332 : 0 : break;
1333 : 0 : case SSL_CB_CONNECT_EXIT:
1334 [ # # ]: 0 : ereport(DEBUG4,
1335 : : (errmsg_internal("SSL: connect exit (%d): \"%s\"", args, desc)));
1336 : 0 : break;
4138 heikki.linnakangas@i 1337 :CBC 26 : case SSL_CB_READ_ALERT:
1338 [ - + ]: 26 : ereport(DEBUG4,
1339 : : (errmsg_internal("SSL: read alert (0x%04x): \"%s\"", args, desc)));
1340 : 26 : break;
1341 : 144 : case SSL_CB_WRITE_ALERT:
1342 [ - + ]: 144 : ereport(DEBUG4,
1343 : : (errmsg_internal("SSL: write alert (0x%04x): \"%s\"", args, desc)));
4145 1344 : 144 : break;
1345 : : }
4138 1346 : 2894 : }
1347 : :
1348 : : /* See pqcomm.h comments on OpenSSL implementation of ALPN (RFC 7301) */
1349 : : static 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 : : */
1355 : : static int
617 1356 : 149 : alpn_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 [ - + ]: 149 : Assert(userdata != NULL);
1371 [ - + ]: 149 : Assert(out != NULL);
1372 [ - + ]: 149 : Assert(outlen != NULL);
1373 [ - + ]: 149 : Assert(in != NULL);
1374 : :
1375 : 149 : retval = SSL_select_next_proto((unsigned char **) out, outlen,
1376 : : alpn_protos, sizeof(alpn_protos),
1377 : : in, inlen);
1378 [ + - + - : 149 : if (*out == NULL || *outlen > sizeof(alpn_protos) || *outlen <= 0)
- + ]
617 heikki.linnakangas@i 1379 :UBC 0 : return SSL_TLSEXT_ERR_NOACK; /* can't happen */
1380 : :
617 heikki.linnakangas@i 1381 [ + - ]:CBC 149 : if (retval == OPENSSL_NPN_NEGOTIATED)
1382 : 149 : 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 : : */
596 heikki.linnakangas@i 1389 :UBC 0 : 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 : : */
1407 : : static bool
3060 heikki.linnakangas@i 1408 :CBC 29 : initialize_dh(SSL_CTX *context, bool isServerStart)
1409 : : {
1410 : 29 : DH *dh = NULL;
1411 : :
1412 : 29 : SSL_CTX_set_options(context, SSL_OP_SINGLE_DH_USE);
1413 : :
1414 [ - + ]: 29 : if (ssl_dh_params_file[0])
3060 heikki.linnakangas@i 1415 :UBC 0 : dh = load_dh_file(ssl_dh_params_file, isServerStart);
3060 heikki.linnakangas@i 1416 [ + - ]:CBC 29 : if (!dh)
2888 peter_e@gmx.net 1417 : 29 : dh = load_dh_buffer(FILE_DH2048, sizeof(FILE_DH2048));
3060 heikki.linnakangas@i 1418 [ - + ]: 29 : if (!dh)
1419 : : {
3060 heikki.linnakangas@i 1420 [ # # # # ]:UBC 0 : ereport(isServerStart ? FATAL : LOG,
1421 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1422 : : errmsg("DH: could not load DH parameters")));
1423 : 0 : return false;
1424 : : }
1425 : :
3060 heikki.linnakangas@i 1426 [ - + ]:CBC 29 : if (SSL_CTX_set_tmp_dh(context, dh) != 1)
1427 : : {
3060 heikki.linnakangas@i 1428 [ # # # # ]:UBC 0 : ereport(isServerStart ? FATAL : LOG,
1429 : : (errcode(ERRCODE_CONFIG_FILE_ERROR),
1430 : : errmsg("DH: could not set DH parameters: %s",
1431 : : SSLerrmessage(ERR_get_error()))));
2194 michael@paquier.xyz 1432 : 0 : DH_free(dh);
3060 heikki.linnakangas@i 1433 : 0 : return false;
1434 : : }
1435 : :
2194 michael@paquier.xyz 1436 :CBC 29 : DH_free(dh);
3060 heikki.linnakangas@i 1437 : 29 : 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 : : */
1445 : : static bool
3268 tgl@sss.pgh.pa.us 1446 : 29 : initialize_ecdh(SSL_CTX *context, bool isServerStart)
1447 : : {
1448 : : #ifndef OPENSSL_NO_ECDH
418 dgustafsson@postgres 1449 [ + + ]: 29 : 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 : : */
3268 tgl@sss.pgh.pa.us 1458 [ + - + - ]: 2 : 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."));
3270 tgl@sss.pgh.pa.us 1464 :UBC 0 : return false;
1465 : : }
1466 : : #endif
1467 : :
3270 tgl@sss.pgh.pa.us 1468 :CBC 27 : 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 : : */
1484 : : static const char *
418 dgustafsson@postgres 1485 : 2 : SSLerrmessageExt(unsigned long ecode, const char *replacement)
1486 : : {
1487 [ - + ]: 2 : if (ecode == 0)
418 dgustafsson@postgres 1488 :UBC 0 : return replacement;
1489 : : else
418 dgustafsson@postgres 1490 :CBC 2 : 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 : : */
1502 : : static const char *
3539 peter_e@gmx.net 1503 : 23 : SSLerrmessage(unsigned long ecode)
1504 : : {
1505 : : const char *errreason;
1506 : : static char errbuf[36];
1507 : :
1508 [ - + ]: 23 : if (ecode == 0)
4145 heikki.linnakangas@i 1509 :UBC 0 : return _("no SSL error reported");
3539 peter_e@gmx.net 1510 :CBC 23 : errreason = ERR_reason_error_string(ecode);
4145 heikki.linnakangas@i 1511 [ + - ]: 23 : if (errreason != NULL)
1512 : 23 : 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
649 tgl@sss.pgh.pa.us 1522 [ # # ]:UBC 0 : if (ERR_SYSTEM_ERROR(ecode))
1523 : 0 : return strerror(ERR_GET_REASON(ecode));
1524 : : #endif
1525 : :
1526 : : /* No choice but to report the numeric ecode */
3539 peter_e@gmx.net 1527 : 0 : snprintf(errbuf, sizeof(errbuf), _("SSL error code %lu"), ecode);
4145 heikki.linnakangas@i 1528 : 0 : return errbuf;
1529 : : }
1530 : :
1531 : : int
3901 magnus@hagander.net 1532 :CBC 210 : be_tls_get_cipher_bits(Port *port)
1533 : : {
1534 : : int bits;
1535 : :
1536 [ + - ]: 210 : if (port->ssl)
1537 : : {
1538 : 210 : SSL_get_cipher_bits(port->ssl, &bits);
1539 : 210 : return bits;
1540 : : }
1541 : : else
3901 magnus@hagander.net 1542 :UBC 0 : return 0;
1543 : : }
1544 : :
1545 : : const char *
2882 peter_e@gmx.net 1546 :CBC 211 : be_tls_get_version(Port *port)
1547 : : {
3901 magnus@hagander.net 1548 [ + - ]: 211 : if (port->ssl)
2882 peter_e@gmx.net 1549 : 211 : return SSL_get_version(port->ssl);
1550 : : else
2882 peter_e@gmx.net 1551 :UBC 0 : return NULL;
1552 : : }
1553 : :
1554 : : const char *
2882 peter_e@gmx.net 1555 :CBC 211 : be_tls_get_cipher(Port *port)
1556 : : {
3901 magnus@hagander.net 1557 [ + - ]: 211 : if (port->ssl)
2882 peter_e@gmx.net 1558 : 211 : return SSL_get_cipher(port->ssl);
1559 : : else
2882 peter_e@gmx.net 1560 :UBC 0 : return NULL;
1561 : : }
1562 : :
1563 : : void
2510 peter@eisentraut.org 1564 :CBC 105 : be_tls_get_peer_subject_name(Port *port, char *ptr, size_t len)
1565 : : {
3901 magnus@hagander.net 1566 [ + + ]: 105 : if (port->peer)
1567 : 27 : strlcpy(ptr, X509_NAME_to_cstring(X509_get_subject_name(port->peer)), len);
1568 : : else
1569 : 78 : ptr[0] = '\0';
1570 : 105 : }
1571 : :
1572 : : void
2510 peter@eisentraut.org 1573 : 106 : be_tls_get_peer_issuer_name(Port *port, char *ptr, size_t len)
1574 : : {
1575 [ + + ]: 106 : if (port->peer)
1576 : 28 : strlcpy(ptr, X509_NAME_to_cstring(X509_get_issuer_name(port->peer)), len);
1577 : : else
1578 : 78 : ptr[0] = '\0';
1579 : 106 : }
1580 : :
1581 : : void
1582 : 106 : be_tls_get_peer_serial(Port *port, char *ptr, size_t len)
1583 : : {
1584 [ + + ]: 106 : if (port->peer)
1585 : : {
1586 : : ASN1_INTEGER *serial;
1587 : : BIGNUM *b;
1588 : : char *decimal;
1589 : :
1590 : 28 : serial = X509_get_serialNumber(port->peer);
1591 : 28 : b = ASN1_INTEGER_to_BN(serial, NULL);
1592 : 28 : decimal = BN_bn2dec(b);
1593 : :
1594 : 28 : BN_free(b);
1595 : 28 : strlcpy(ptr, decimal, len);
1596 : 28 : OPENSSL_free(decimal);
1597 : : }
1598 : : else
1599 : 78 : ptr[0] = '\0';
1600 : 106 : }
1601 : :
1602 : : char *
2903 peter_e@gmx.net 1603 : 4 : be_tls_get_certificate_hash(Port *port, size_t *len)
1604 : : {
1605 : : X509 *server_cert;
1606 : : char *cert_hash;
1607 : 4 : 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 : 4 : *len = 0;
1613 : 4 : server_cert = SSL_get_certificate(port->ssl);
1614 [ - + ]: 4 : if (server_cert == NULL)
2903 peter_e@gmx.net 1615 :UBC 0 : 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
1035 michael@paquier.xyz 1623 [ - + ]:CBC 4 : 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
2903 peter_e@gmx.net 1628 [ # # ]:UBC 0 : 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 : : */
2903 peter_e@gmx.net 1636 [ - + ]:CBC 4 : switch (algo_nid)
1637 : : {
2903 peter_e@gmx.net 1638 :UBC 0 : case NID_md5:
1639 : : case NID_sha1:
1640 : 0 : algo_type = EVP_sha256();
1641 : 0 : break;
2903 peter_e@gmx.net 1642 :CBC 4 : default:
1643 : 4 : algo_type = EVP_get_digestbynid(algo_nid);
1644 [ - + ]: 4 : if (algo_type == NULL)
2903 peter_e@gmx.net 1645 [ # # ]:UBC 0 : elog(ERROR, "could not find digest for NID %s",
1646 : : OBJ_nid2sn(algo_nid));
2903 peter_e@gmx.net 1647 :CBC 4 : break;
1648 : : }
1649 : :
1650 : : /* generate and save the certificate hash */
1651 [ - + ]: 4 : if (!X509_digest(server_cert, algo_type, hash, &hash_size))
2903 peter_e@gmx.net 1652 [ # # ]:UBC 0 : elog(ERROR, "could not generate server certificate hash");
1653 : :
2903 peter_e@gmx.net 1654 :CBC 4 : cert_hash = palloc(hash_size);
1655 : 4 : memcpy(cert_hash, hash, hash_size);
1656 : 4 : *len = hash_size;
1657 : :
1658 : 4 : return cert_hash;
1659 : : }
1660 : :
1661 : : /*
1662 : : * Convert an X509 subject name to a cstring.
1663 : : *
1664 : : */
1665 : : static char *
3901 magnus@hagander.net 1666 : 67 : X509_NAME_to_cstring(X509_NAME *name)
1667 : : {
1668 : 67 : BIO *membuf = BIO_new(BIO_s_mem());
1669 : : int i,
1670 : : nid,
1671 : 67 : 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 : :
1869 1681 [ - + ]: 67 : if (membuf == NULL)
1869 magnus@hagander.net 1682 [ # # ]:UBC 0 : ereport(ERROR,
1683 : : (errcode(ERRCODE_OUT_OF_MEMORY),
1684 : : errmsg("could not create BIO")));
1685 : :
3901 magnus@hagander.net 1686 :CBC 67 : (void) BIO_set_close(membuf, BIO_CLOSE);
1687 [ + + ]: 145 : for (i = 0; i < count; i++)
1688 : : {
1689 : 78 : e = X509_NAME_get_entry(name, i);
1690 : 78 : nid = OBJ_obj2nid(X509_NAME_ENTRY_get_object(e));
1869 1691 [ - + ]: 78 : if (nid == NID_undef)
1869 magnus@hagander.net 1692 [ # # ]:UBC 0 : ereport(ERROR,
1693 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1694 : : errmsg("could not get NID for ASN1_OBJECT object")));
3901 magnus@hagander.net 1695 :CBC 78 : v = X509_NAME_ENTRY_get_data(e);
1696 : 78 : field_name = OBJ_nid2sn(nid);
1869 1697 [ - + ]: 78 : if (field_name == NULL)
3901 magnus@hagander.net 1698 :UBC 0 : field_name = OBJ_nid2ln(nid);
1869 magnus@hagander.net 1699 [ - + ]:CBC 78 : if (field_name == NULL)
1869 magnus@hagander.net 1700 [ # # ]:UBC 0 : ereport(ERROR,
1701 : : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1702 : : errmsg("could not convert NID %d to an ASN1_OBJECT structure", nid)));
3901 magnus@hagander.net 1703 :CBC 78 : BIO_printf(membuf, "/%s=", field_name);
1704 : 78 : 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 : 67 : nullterm = '\0';
1711 : 67 : BIO_write(membuf, &nullterm, 1);
1712 : 67 : size = BIO_get_mem_data(membuf, &sp);
1713 : 67 : dp = pg_any_to_server(sp, size - 1, PG_UTF8);
1714 : :
1715 : 67 : result = pstrdup(dp);
1716 [ - + ]: 67 : if (dp != sp)
3901 magnus@hagander.net 1717 :UBC 0 : pfree(dp);
1869 magnus@hagander.net 1718 [ - + ]:CBC 67 : if (BIO_free(membuf) != 1)
1869 magnus@hagander.net 1719 [ # # ]:UBC 0 : elog(ERROR, "could not free OpenSSL BIO structure");
1720 : :
3901 magnus@hagander.net 1721 :CBC 67 : 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 : : */
1738 : : static int
2094 michael@paquier.xyz 1739 : 31 : ssl_protocol_version_to_openssl(int v)
1740 : : {
2583 peter_e@gmx.net 1741 [ - - + + : 31 : switch (v)
- - ]
1742 : : {
2583 peter_e@gmx.net 1743 :UBC 0 : case PG_TLS_ANY:
1744 : 0 : return 0;
1745 : 0 : case PG_TLS1_VERSION:
1746 : 0 : return TLS1_VERSION;
2583 peter_e@gmx.net 1747 :CBC 1 : case PG_TLS1_1_VERSION:
1748 : : #ifdef TLS1_1_VERSION
1749 : 1 : return TLS1_1_VERSION;
1750 : : #else
1751 : : break;
1752 : : #endif
1753 : 30 : case PG_TLS1_2_VERSION:
1754 : : #ifdef TLS1_2_VERSION
1755 : 30 : return TLS1_2_VERSION;
1756 : : #else
1757 : : break;
1758 : : #endif
2583 peter_e@gmx.net 1759 :UBC 0 : case PG_TLS1_3_VERSION:
1760 : : #ifdef TLS1_3_VERSION
1761 : 0 : return TLS1_3_VERSION;
1762 : : #else
1763 : : break;
1764 : : #endif
1765 : : }
1766 : :
1767 : 0 : return -1;
1768 : : }
1769 : :
1770 : : /*
1771 : : * Likewise provide a mapping to strings.
1772 : : */
1773 : : static const char *
1998 tgl@sss.pgh.pa.us 1774 : 0 : ssl_protocol_version_to_string(int v)
1775 : : {
1776 [ # # # # : 0 : switch (v)
# # ]
1777 : : {
1778 : 0 : case PG_TLS_ANY:
1779 : 0 : return "any";
1780 : 0 : case PG_TLS1_VERSION:
1781 : 0 : return "TLSv1";
1782 : 0 : case PG_TLS1_1_VERSION:
1783 : 0 : return "TLSv1.1";
1784 : 0 : case PG_TLS1_2_VERSION:
1785 : 0 : return "TLSv1.2";
1786 : 0 : case PG_TLS1_3_VERSION:
1787 : 0 : return "TLSv1.3";
1788 : : }
1789 : :
1790 : 0 : return "(unrecognized)";
1791 : : }
1792 : :
1793 : :
1794 : : static void
2092 andrew@dunslane.net 1795 :CBC 30 : default_openssl_tls_init(SSL_CTX *context, bool isServerStart)
1796 : : {
1797 [ + + ]: 30 : if (isServerStart)
1798 : : {
1799 [ + + ]: 26 : if (ssl_passphrase_command[0])
1800 : 7 : SSL_CTX_set_default_passwd_cb(context, ssl_external_passwd_cb);
1801 : : }
1802 : : else
1803 : : {
1804 [ + + + + ]: 4 : if (ssl_passphrase_command[0] && ssl_passphrase_command_supports_reload)
2092 andrew@dunslane.net 1805 :GBC 1 : 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 : : */
2092 andrew@dunslane.net 1814 :CBC 3 : SSL_CTX_set_default_passwd_cb(context, dummy_ssl_passwd_cb);
1815 : : }
1816 : 30 : }
|