blob: a586b62553f00157b28d8ce4ed22bd10b5616d8e [file] [log] [blame]
yanbzhu08ce6ab2015-12-02 13:01:29 -05001
Emeric Brun46591952012-05-18 15:47:34 +02002/*
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003 * SSL/TLS transport layer over SOCK_STREAM sockets
Emeric Brun46591952012-05-18 15:47:34 +02004 *
5 * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Willy Tarreau69845df2012-09-10 09:43:09 +020012 * Acknowledgement:
13 * We'd like to specially thank the Stud project authors for a very clean
14 * and well documented code which helped us understand how the OpenSSL API
15 * ought to be used in non-blocking mode. This is one difficult part which
16 * is not easy to get from the OpenSSL doc, and reading the Stud code made
17 * it much more obvious than the examples in the OpenSSL package. Keep up
18 * the good works, guys !
19 *
20 * Stud is an extremely efficient and scalable SSL/TLS proxy which combines
21 * particularly well with haproxy. For more info about this project, visit :
22 * https://github.com/bumptech/stud
23 *
Emeric Brun46591952012-05-18 15:47:34 +020024 */
25
Willy Tarreau8d164dc2019-05-10 09:35:00 +020026/* Note: do NOT include openssl/xxx.h here, do it in openssl-compat.h */
Emeric Brun46591952012-05-18 15:47:34 +020027#define _GNU_SOURCE
Emeric Brunfc0421f2012-09-07 17:30:07 +020028#include <ctype.h>
29#include <dirent.h>
Emeric Brun46591952012-05-18 15:47:34 +020030#include <errno.h>
31#include <fcntl.h>
32#include <stdio.h>
33#include <stdlib.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020034#include <string.h>
35#include <unistd.h>
Emeric Brun46591952012-05-18 15:47:34 +020036
37#include <sys/socket.h>
38#include <sys/stat.h>
39#include <sys/types.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020040#include <netdb.h>
Emeric Brun46591952012-05-18 15:47:34 +020041#include <netinet/tcp.h>
42
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020043#include <haproxy/api.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020044#include <import/lru.h>
45#include <import/xxhash.h>
46
Willy Tarreau2741c8c2020-06-02 11:28:02 +020047#include <haproxy/dynbuf.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020048#include <haproxy/channel.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020049#include <haproxy/chunk.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020050#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020051#include <haproxy/connection.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020052#include <haproxy/errors.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020053#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020054#include <haproxy/global.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020055#include <haproxy/http_rules.h>
Willy Tarreau6019fab2020-05-27 16:26:00 +020056#include <haproxy/openssl-compat.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020057#include <haproxy/pattern-t.h>
Willy Tarreau334099c2020-06-03 18:38:48 +020058#include <haproxy/shctx.h>
Willy Tarreau47d7f902020-06-04 14:25:47 +020059#include <haproxy/ssl_ckch.h>
Willy Tarreau52d88722020-06-04 14:29:23 +020060#include <haproxy/ssl_crtlist.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020061#include <haproxy/ssl_sock.h>
Willy Tarreaub2bd8652020-06-04 14:21:22 +020062#include <haproxy/ssl_utils.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020063#include <haproxy/stats-t.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020064#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020065#include <haproxy/task.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020066#include <haproxy/tools.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020067#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020068#include <haproxy/time.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020069#include <haproxy/base64.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020070#include <haproxy/vars.h>
Emeric Brun46591952012-05-18 15:47:34 +020071
Willy Tarreau8d2b7772020-05-27 10:58:19 +020072#include <import/ebpttree.h>
73#include <import/ebsttree.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020074
Willy Tarreauaa74c4e2020-06-04 10:19:23 +020075#include <haproxy/arg.h>
Willy Tarreau0f6ffd62020-06-03 19:33:00 +020076#include <haproxy/fd.h>
Willy Tarreau66347942020-06-01 12:18:08 +020077#include <haproxy/freq_ctr.h>
Willy Tarreaufc774542020-06-04 17:31:04 +020078#include <haproxy/proto_tcp.h>
Willy Tarreau92faadf2012-10-10 23:04:25 +020079#include <proto/server.h>
Emeric Brun46591952012-05-18 15:47:34 +020080#include <proto/log.h>
Emeric Brun94324a42012-10-11 14:00:19 +020081#include <proto/proxy.h>
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020082#include <proto/stream.h>
Emeric Brun46591952012-05-18 15:47:34 +020083
Willy Tarreau9356dac2019-05-10 09:22:53 +020084/* ***** READ THIS before adding code here! *****
85 *
86 * Due to API incompatibilities between multiple OpenSSL versions and their
87 * derivatives, it's often tempting to add macros to (re-)define certain
88 * symbols. Please do not do this here, and do it in common/openssl-compat.h
89 * exclusively so that the whole code consistently uses the same macros.
90 *
91 * Whenever possible if a macro is missing in certain versions, it's better
92 * to conditionally define it in openssl-compat.h than using lots of ifdefs.
93 */
94
Willy Tarreau71b734c2014-01-28 15:19:44 +010095int sslconns = 0;
96int totalsslconns = 0;
Emeric Brunece0c332017-12-06 13:51:49 +010097int nb_engines = 0;
Emeric Brune1f38db2012-09-03 20:36:47 +020098
William Lallemande0f3fd52020-02-25 14:53:06 +010099static struct eb_root cert_issuer_tree = EB_ROOT; /* issuers tree from "issuers-chain-path" */
100
William Lallemand7fd8b452020-05-07 15:20:43 +0200101struct global_ssl global_ssl = {
Willy Tarreauef934602016-12-22 23:12:01 +0100102#ifdef LISTEN_DEFAULT_CIPHERS
103 .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS,
104#endif
105#ifdef CONNECT_DEFAULT_CIPHERS
106 .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS,
107#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200108#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200109#ifdef LISTEN_DEFAULT_CIPHERSUITES
110 .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES,
111#endif
112#ifdef CONNECT_DEFAULT_CIPHERSUITES
113 .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES,
114#endif
115#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100116 .listen_default_ssloptions = BC_SSL_O_NONE,
117 .connect_default_ssloptions = SRV_SSL_O_NONE,
118
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200119 .listen_default_sslmethods.flags = MC_SSL_O_ALL,
120 .listen_default_sslmethods.min = CONF_TLSV_NONE,
121 .listen_default_sslmethods.max = CONF_TLSV_NONE,
122 .connect_default_sslmethods.flags = MC_SSL_O_ALL,
123 .connect_default_sslmethods.min = CONF_TLSV_NONE,
124 .connect_default_sslmethods.max = CONF_TLSV_NONE,
125
Willy Tarreauef934602016-12-22 23:12:01 +0100126#ifdef DEFAULT_SSL_MAX_RECORD
127 .max_record = DEFAULT_SSL_MAX_RECORD,
128#endif
129 .default_dh_param = SSL_DEFAULT_DH_PARAM,
130 .ctx_cache = DEFAULT_SSL_CTX_CACHE,
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100131 .capture_cipherlist = 0,
William Lallemand3af48e72020-02-03 17:15:52 +0100132 .extra_files = SSL_GF_ALL,
Willy Tarreauef934602016-12-22 23:12:01 +0100133};
134
Olivier Houcharda8955d52019-04-07 22:00:38 +0200135static BIO_METHOD *ha_meth;
136
Olivier Houchard66ab4982019-02-26 18:37:15 +0100137DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx));
138
Olivier Houchardea8dd942019-05-20 14:02:16 +0200139static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short);
Olivier Houchard000694c2019-05-23 14:45:12 +0200140static int ssl_sock_handshake(struct connection *conn, unsigned int flag);
Olivier Houchardea8dd942019-05-20 14:02:16 +0200141
Olivier Houcharda8955d52019-04-07 22:00:38 +0200142/* Methods to implement OpenSSL BIO */
143static int ha_ssl_write(BIO *h, const char *buf, int num)
144{
145 struct buffer tmpbuf;
146 struct ssl_sock_ctx *ctx;
147 int ret;
148
149 ctx = BIO_get_data(h);
150 tmpbuf.size = num;
151 tmpbuf.area = (void *)(uintptr_t)buf;
152 tmpbuf.data = num;
153 tmpbuf.head = 0;
154 ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200155 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200156 BIO_set_retry_write(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200157 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200158 } else if (ret == 0)
159 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200160 return ret;
161}
162
163static int ha_ssl_gets(BIO *h, char *buf, int size)
164{
165
166 return 0;
167}
168
169static int ha_ssl_puts(BIO *h, const char *str)
170{
171
172 return ha_ssl_write(h, str, strlen(str));
173}
174
175static int ha_ssl_read(BIO *h, char *buf, int size)
176{
177 struct buffer tmpbuf;
178 struct ssl_sock_ctx *ctx;
179 int ret;
180
181 ctx = BIO_get_data(h);
182 tmpbuf.size = size;
183 tmpbuf.area = buf;
184 tmpbuf.data = 0;
185 tmpbuf.head = 0;
186 ret = ctx->xprt->rcv_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, size, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200187 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200188 BIO_set_retry_read(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200189 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200190 } else if (ret == 0)
191 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200192
193 return ret;
194}
195
196static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2)
197{
198 int ret = 0;
199 switch (cmd) {
200 case BIO_CTRL_DUP:
201 case BIO_CTRL_FLUSH:
202 ret = 1;
203 break;
204 }
205 return ret;
206}
207
208static int ha_ssl_new(BIO *h)
209{
210 BIO_set_init(h, 1);
211 BIO_set_data(h, NULL);
212 BIO_clear_flags(h, ~0);
213 return 1;
214}
215
216static int ha_ssl_free(BIO *data)
217{
218
219 return 1;
220}
221
222
Willy Tarreau5db847a2019-05-09 14:13:35 +0200223#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100224
Emeric Brun821bb9b2017-06-15 16:37:39 +0200225static HA_RWLOCK_T *ssl_rwlocks;
226
227
228unsigned long ssl_id_function(void)
229{
230 return (unsigned long)tid;
231}
232
233void ssl_locking_function(int mode, int n, const char * file, int line)
234{
235 if (mode & CRYPTO_LOCK) {
236 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100237 HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200238 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100239 HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200240 }
241 else {
242 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100243 HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200244 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100245 HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200246 }
247}
248
249static int ssl_locking_init(void)
250{
251 int i;
252
253 ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks());
254 if (!ssl_rwlocks)
255 return -1;
256
257 for (i = 0 ; i < CRYPTO_num_locks() ; i++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100258 HA_RWLOCK_INIT(&ssl_rwlocks[i]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200259
260 CRYPTO_set_id_callback(ssl_id_function);
261 CRYPTO_set_locking_callback(ssl_locking_function);
262
263 return 0;
264}
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100265
Emeric Brun821bb9b2017-06-15 16:37:39 +0200266#endif
267
Willy Tarreauaf613e82020-06-05 08:40:51 +0200268__decl_thread(HA_SPINLOCK_T ckch_lock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200269
William Lallemandbc6ca7c2019-10-29 23:48:19 +0100270
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200271/*
Emmanuel Hocdetb270e812019-11-21 19:09:31 +0100272 * deduplicate cafile (and crlfile)
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200273 */
274struct cafile_entry {
275 X509_STORE *ca_store;
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200276 STACK_OF(X509_NAME) *ca_list;
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200277 struct ebmb_node node;
278 char path[0];
279};
280
281static struct eb_root cafile_tree = EB_ROOT_UNIQUE;
282
283static X509_STORE* ssl_store_get0_locations_file(char *path)
284{
285 struct ebmb_node *eb;
286
287 eb = ebst_lookup(&cafile_tree, path);
288 if (eb) {
289 struct cafile_entry *ca_e;
290 ca_e = ebmb_entry(eb, struct cafile_entry, node);
291 return ca_e->ca_store;
292 }
293 return NULL;
294}
295
William Lallemanddad31052020-05-14 17:47:32 +0200296int ssl_store_load_locations_file(char *path)
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200297{
298 if (ssl_store_get0_locations_file(path) == NULL) {
299 struct cafile_entry *ca_e;
300 X509_STORE *store = X509_STORE_new();
301 if (X509_STORE_load_locations(store, path, NULL)) {
302 int pathlen;
303 pathlen = strlen(path);
304 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
305 if (ca_e) {
306 memcpy(ca_e->path, path, pathlen + 1);
307 ca_e->ca_store = store;
308 ebst_insert(&cafile_tree, &ca_e->node);
309 return 1;
310 }
311 }
312 X509_STORE_free(store);
313 return 0;
314 }
315 return 1;
316}
317
318/* mimic what X509_STORE_load_locations do with store_ctx */
319static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path)
320{
321 X509_STORE *store;
322 store = ssl_store_get0_locations_file(path);
323 if (store_ctx && store) {
324 int i;
325 X509_OBJECT *obj;
326 STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store);
327 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
328 obj = sk_X509_OBJECT_value(objs, i);
329 switch (X509_OBJECT_get_type(obj)) {
330 case X509_LU_X509:
331 X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj));
332 break;
333 case X509_LU_CRL:
334 X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj));
335 break;
336 default:
337 break;
338 }
339 }
340 return 1;
341 }
342 return 0;
343}
344
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +0500345/* SSL_CTX_load_verify_locations substitute, internally call X509_STORE_load_locations */
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200346static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path)
347{
348 X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx);
349 return ssl_set_cert_crl_file(store_ctx, path);
350}
351
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200352/*
353 Extract CA_list from CA_file already in tree.
354 Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility.
355 Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX.
356*/
357static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path)
358{
359 struct ebmb_node *eb;
360 struct cafile_entry *ca_e;
361
362 eb = ebst_lookup(&cafile_tree, path);
363 if (!eb)
364 return NULL;
365 ca_e = ebmb_entry(eb, struct cafile_entry, node);
366
367 if (ca_e->ca_list == NULL) {
368 int i;
369 unsigned long key;
370 struct eb_root ca_name_tree = EB_ROOT;
371 struct eb64_node *node, *back;
372 struct {
373 struct eb64_node node;
374 X509_NAME *xname;
375 } *ca_name;
376 STACK_OF(X509_OBJECT) *objs;
377 STACK_OF(X509_NAME) *skn;
378 X509 *x;
379 X509_NAME *xn;
380
381 skn = sk_X509_NAME_new_null();
382 /* take x509 from cafile_tree */
383 objs = X509_STORE_get0_objects(ca_e->ca_store);
384 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
385 x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
386 if (!x)
387 continue;
388 xn = X509_get_subject_name(x);
389 if (!xn)
390 continue;
391 /* Check for duplicates. */
392 key = X509_NAME_hash(xn);
393 for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL;
394 node && ca_name == NULL;
395 node = eb64_next(node)) {
396 ca_name = container_of(node, typeof(*ca_name), node);
397 if (X509_NAME_cmp(xn, ca_name->xname) != 0)
398 ca_name = NULL;
399 }
400 /* find a duplicate */
401 if (ca_name)
402 continue;
403 ca_name = calloc(1, sizeof *ca_name);
404 xn = X509_NAME_dup(xn);
405 if (!ca_name ||
406 !xn ||
407 !sk_X509_NAME_push(skn, xn)) {
408 free(ca_name);
409 X509_NAME_free(xn);
410 sk_X509_NAME_pop_free(skn, X509_NAME_free);
411 sk_X509_NAME_free(skn);
412 skn = NULL;
413 break;
414 }
415 ca_name->node.key = key;
416 ca_name->xname = xn;
417 eb64_insert(&ca_name_tree, &ca_name->node);
418 }
419 ca_e->ca_list = skn;
420 /* remove temporary ca_name tree */
421 node = eb64_first(&ca_name_tree);
422 while (node) {
423 ca_name = container_of(node, typeof(*ca_name), node);
424 back = eb64_next(node);
425 eb64_delete(node);
426 free(ca_name);
427 node = back;
428 }
429 }
430 return ca_e->ca_list;
431}
432
Willy Tarreaubafbe012017-11-24 17:34:44 +0100433struct pool_head *pool_head_ssl_capture = NULL;
William Lallemand15e16942020-05-15 00:25:08 +0200434int ssl_capture_ptr_index = -1;
Thierry FOURNIER28962c92018-06-17 21:37:05 +0200435static int ssl_app_data_index = -1;
Willy Tarreauef934602016-12-22 23:12:01 +0100436
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200437#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
438struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference);
439#endif
440
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200441#ifndef OPENSSL_NO_ENGINE
William Lallemanddad31052020-05-14 17:47:32 +0200442unsigned int openssl_engines_initialized;
Grant Zhang872f9c22017-01-21 01:10:18 +0000443struct list openssl_engines = LIST_HEAD_INIT(openssl_engines);
444struct ssl_engine_list {
445 struct list list;
446 ENGINE *e;
447};
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200448#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000449
Remi Gacogne8de54152014-07-15 11:36:40 +0200450#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +0200451static int ssl_dh_ptr_index = -1;
Remi Gacogne47783ef2015-05-29 15:53:22 +0200452static DH *global_dh = NULL;
Remi Gacogne8de54152014-07-15 11:36:40 +0200453static DH *local_dh_1024 = NULL;
454static DH *local_dh_2048 = NULL;
455static DH *local_dh_4096 = NULL;
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +0100456static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen);
Remi Gacogne8de54152014-07-15 11:36:40 +0200457#endif /* OPENSSL_NO_DH */
458
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100459#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +0200460/* X509V3 Extensions that will be added on generated certificates */
461#define X509V3_EXT_SIZE 5
462static char *x509v3_ext_names[X509V3_EXT_SIZE] = {
463 "basicConstraints",
464 "nsComment",
465 "subjectKeyIdentifier",
466 "authorityKeyIdentifier",
467 "keyUsage",
468};
469static char *x509v3_ext_values[X509V3_EXT_SIZE] = {
470 "CA:FALSE",
471 "\"OpenSSL Generated Certificate\"",
472 "hash",
473 "keyid,issuer:always",
474 "nonRepudiation,digitalSignature,keyEncipherment"
475};
Christopher Faulet31af49d2015-06-09 17:29:50 +0200476/* LRU cache to store generated certificate */
477static struct lru64_head *ssl_ctx_lru_tree = NULL;
478static unsigned int ssl_ctx_lru_seed = 0;
Emeric Brun821bb9b2017-06-15 16:37:39 +0200479static unsigned int ssl_ctx_serial;
Willy Tarreau86abe442018-11-25 20:12:18 +0100480__decl_rwlock(ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200481
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200482#endif // SSL_CTRL_SET_TLSEXT_HOSTNAME
483
Willy Tarreau9a1ab082019-05-09 13:26:41 +0200484#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhube2774d2015-12-10 15:07:30 -0500485/* The order here matters for picking a default context,
486 * keep the most common keytype at the bottom of the list
487 */
488const char *SSL_SOCK_KEYTYPE_NAMES[] = {
489 "dsa",
490 "ecdsa",
491 "rsa"
492};
yanbzhube2774d2015-12-10 15:07:30 -0500493#endif
494
William Lallemandc3cd35f2017-11-28 11:04:43 +0100495static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */
William Lallemand4f45bb92017-10-30 20:08:51 +0100496static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */
497
Dragan Dosen9ac98092020-05-11 15:51:45 +0200498/* Dedicated callback functions for heartbeat and clienthello.
499 */
500#ifdef TLS1_RT_HEARTBEAT
501static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version,
502 int content_type, const void *buf, size_t len,
503 SSL *ssl);
504#endif
505static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version,
506 int content_type, const void *buf, size_t len,
507 SSL *ssl);
508
Dragan Dosen1e7ed042020-05-08 18:30:00 +0200509/* List head of all registered SSL/TLS protocol message callbacks. */
510struct list ssl_sock_msg_callbacks = LIST_HEAD_INIT(ssl_sock_msg_callbacks);
511
512/* Registers the function <func> in order to be called on SSL/TLS protocol
513 * message processing. It will return 0 if the function <func> is not set
514 * or if it fails to allocate memory.
515 */
516int ssl_sock_register_msg_callback(ssl_sock_msg_callback_func func)
517{
518 struct ssl_sock_msg_callback *cbk;
519
520 if (!func)
521 return 0;
522
523 cbk = calloc(1, sizeof(*cbk));
524 if (!cbk) {
525 ha_alert("out of memory in ssl_sock_register_msg_callback().\n");
526 return 0;
527 }
528
529 cbk->func = func;
530
531 LIST_ADDQ(&ssl_sock_msg_callbacks, &cbk->list);
532
533 return 1;
534}
535
Dragan Dosen9ac98092020-05-11 15:51:45 +0200536/* Used to register dedicated SSL/TLS protocol message callbacks.
537 */
538static int ssl_sock_register_msg_callbacks(void)
539{
540#ifdef TLS1_RT_HEARTBEAT
541 if (!ssl_sock_register_msg_callback(ssl_sock_parse_heartbeat))
542 return ERR_ABORT;
543#endif
544 if (global_ssl.capture_cipherlist > 0) {
545 if (!ssl_sock_register_msg_callback(ssl_sock_parse_clienthello))
546 return ERR_ABORT;
547 }
548 return 0;
549}
550
Dragan Dosen1e7ed042020-05-08 18:30:00 +0200551/* Used to free all SSL/TLS protocol message callbacks that were
552 * registered by using ssl_sock_register_msg_callback().
553 */
554static void ssl_sock_unregister_msg_callbacks(void)
555{
556 struct ssl_sock_msg_callback *cbk, *cbkback;
557
558 list_for_each_entry_safe(cbk, cbkback, &ssl_sock_msg_callbacks, list) {
559 LIST_DEL(&cbk->list);
560 free(cbk);
561 }
562}
563
Dragan Doseneb607fe2020-05-11 17:17:06 +0200564SSL *ssl_sock_get_ssl_object(struct connection *conn)
565{
566 if (!ssl_sock_is_ssl(conn))
567 return NULL;
568
569 return ((struct ssl_sock_ctx *)(conn->xprt_ctx))->ssl;
570}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100571/*
572 * This function gives the detail of the SSL error. It is used only
573 * if the debug mode and the verbose mode are activated. It dump all
574 * the SSL error until the stack was empty.
575 */
576static forceinline void ssl_sock_dump_errors(struct connection *conn)
577{
578 unsigned long ret;
579
580 if (unlikely(global.mode & MODE_DEBUG)) {
581 while(1) {
582 ret = ERR_get_error();
583 if (ret == 0)
584 return;
585 fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n",
Willy Tarreau585744b2017-08-24 14:31:19 +0200586 (unsigned short)conn->handle.fd, ret,
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100587 ERR_func_error_string(ret), ERR_reason_error_string(ret));
588 }
589 }
590}
591
yanbzhube2774d2015-12-10 15:07:30 -0500592
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200593#ifndef OPENSSL_NO_ENGINE
William Lallemanddad31052020-05-14 17:47:32 +0200594int ssl_init_single_engine(const char *engine_id, const char *def_algorithms)
Grant Zhang872f9c22017-01-21 01:10:18 +0000595{
596 int err_code = ERR_ABORT;
597 ENGINE *engine;
598 struct ssl_engine_list *el;
599
600 /* grab the structural reference to the engine */
601 engine = ENGINE_by_id(engine_id);
602 if (engine == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100603 ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000604 goto fail_get;
605 }
606
607 if (!ENGINE_init(engine)) {
608 /* the engine couldn't initialise, release it */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100609 ha_alert("ssl-engine %s: failed to initialize\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000610 goto fail_init;
611 }
612
613 if (ENGINE_set_default_string(engine, def_algorithms) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100614 ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000615 goto fail_set_method;
616 }
617
618 el = calloc(1, sizeof(*el));
619 el->e = engine;
620 LIST_ADD(&openssl_engines, &el->list);
Emeric Brunece0c332017-12-06 13:51:49 +0100621 nb_engines++;
622 if (global_ssl.async)
623 global.ssl_used_async_engines = nb_engines;
Grant Zhang872f9c22017-01-21 01:10:18 +0000624 return 0;
625
626fail_set_method:
627 /* release the functional reference from ENGINE_init() */
628 ENGINE_finish(engine);
629
630fail_init:
631 /* release the structural reference from ENGINE_by_id() */
632 ENGINE_free(engine);
633
634fail_get:
635 return err_code;
636}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200637#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000638
Willy Tarreau5db847a2019-05-09 14:13:35 +0200639#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +0200640/*
641 * openssl async fd handler
642 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200643void ssl_async_fd_handler(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000644{
Olivier Houchardea8dd942019-05-20 14:02:16 +0200645 struct ssl_sock_ctx *ctx = fdtab[fd].owner;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000646
Emeric Brun3854e012017-05-17 20:42:48 +0200647 /* fd is an async enfine fd, we must stop
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000648 * to poll this fd until it is requested
649 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000650 fd_stop_recv(fd);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000651 fd_cant_recv(fd);
652
653 /* crypto engine is available, let's notify the associated
654 * connection that it can pursue its processing.
655 */
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200656 ssl_sock_io_cb(NULL, ctx, 0);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000657}
658
Emeric Brun3854e012017-05-17 20:42:48 +0200659/*
660 * openssl async delayed SSL_free handler
661 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200662void ssl_async_fd_free(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000663{
664 SSL *ssl = fdtab[fd].owner;
Emeric Brun3854e012017-05-17 20:42:48 +0200665 OSSL_ASYNC_FD all_fd[32];
666 size_t num_all_fds = 0;
667 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000668
Emeric Brun3854e012017-05-17 20:42:48 +0200669 /* We suppose that the async job for a same SSL *
670 * are serialized. So if we are awake it is
671 * because the running job has just finished
672 * and we can remove all async fds safely
673 */
674 SSL_get_all_async_fds(ssl, NULL, &num_all_fds);
675 if (num_all_fds > 32) {
676 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
677 return;
678 }
679
680 SSL_get_all_async_fds(ssl, all_fd, &num_all_fds);
681 for (i=0 ; i < num_all_fds ; i++)
682 fd_remove(all_fd[i]);
683
684 /* Now we can safely call SSL_free, no more pending job in engines */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000685 SSL_free(ssl);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +0100686 _HA_ATOMIC_SUB(&sslconns, 1);
687 _HA_ATOMIC_SUB(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000688}
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000689/*
Emeric Brun3854e012017-05-17 20:42:48 +0200690 * function used to manage a returned SSL_ERROR_WANT_ASYNC
691 * and enable/disable polling for async fds
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000692 */
Olivier Houchardea8dd942019-05-20 14:02:16 +0200693static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000694{
Willy Tarreaua9786b62018-01-25 07:22:13 +0100695 OSSL_ASYNC_FD add_fd[32];
Emeric Brun3854e012017-05-17 20:42:48 +0200696 OSSL_ASYNC_FD del_fd[32];
Olivier Houchardea8dd942019-05-20 14:02:16 +0200697 SSL *ssl = ctx->ssl;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000698 size_t num_add_fds = 0;
699 size_t num_del_fds = 0;
Emeric Brun3854e012017-05-17 20:42:48 +0200700 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000701
702 SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL,
703 &num_del_fds);
Emeric Brun3854e012017-05-17 20:42:48 +0200704 if (num_add_fds > 32 || num_del_fds > 32) {
705 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000706 return;
707 }
708
Emeric Brun3854e012017-05-17 20:42:48 +0200709 SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000710
Emeric Brun3854e012017-05-17 20:42:48 +0200711 /* We remove unused fds from the fdtab */
712 for (i=0 ; i < num_del_fds ; i++)
713 fd_remove(del_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000714
Emeric Brun3854e012017-05-17 20:42:48 +0200715 /* We add new fds to the fdtab */
716 for (i=0 ; i < num_add_fds ; i++) {
Olivier Houchardea8dd942019-05-20 14:02:16 +0200717 fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000718 }
719
Emeric Brun3854e012017-05-17 20:42:48 +0200720 num_add_fds = 0;
721 SSL_get_all_async_fds(ssl, NULL, &num_add_fds);
722 if (num_add_fds > 32) {
723 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
724 return;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000725 }
Emeric Brun3854e012017-05-17 20:42:48 +0200726
727 /* We activate the polling for all known async fds */
728 SSL_get_all_async_fds(ssl, add_fd, &num_add_fds);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000729 for (i=0 ; i < num_add_fds ; i++) {
Emeric Brun3854e012017-05-17 20:42:48 +0200730 fd_want_recv(add_fd[i]);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000731 /* To ensure that the fd cache won't be used
732 * We'll prefer to catch a real RD event
733 * because handling an EAGAIN on this fd will
734 * result in a context switch and also
735 * some engines uses a fd in blocking mode.
736 */
737 fd_cant_recv(add_fd[i]);
738 }
Emeric Brun3854e012017-05-17 20:42:48 +0200739
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000740}
741#endif
742
William Lallemand104a7a62019-10-14 14:14:59 +0200743#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200744/*
745 * This function returns the number of seconds elapsed
746 * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the
747 * date presented un ASN1_GENERALIZEDTIME.
748 *
749 * In parsing error case, it returns -1.
750 */
751static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d)
752{
753 long epoch;
754 char *p, *end;
755 const unsigned short month_offset[12] = {
756 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
757 };
758 int year, month;
759
760 if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1;
761
762 p = (char *)d->data;
763 end = p + d->length;
764
765 if (end - p < 4) return -1;
766 year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0';
767 p += 4;
768 if (end - p < 2) return -1;
769 month = 10 * (p[0] - '0') + p[1] - '0';
770 if (month < 1 || month > 12) return -1;
771 /* Compute the number of seconds since 1 jan 1970 and the beginning of current month
772 We consider leap years and the current month (<marsh or not) */
773 epoch = ( ((year - 1970) * 365)
774 + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400)
775 - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400)
776 + month_offset[month-1]
777 ) * 24 * 60 * 60;
778 p += 2;
779 if (end - p < 2) return -1;
780 /* Add the number of seconds of completed days of current month */
781 epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60;
782 p += 2;
783 if (end - p < 2) return -1;
784 /* Add the completed hours of the current day */
785 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60;
786 p += 2;
787 if (end - p < 2) return -1;
788 /* Add the completed minutes of the current hour */
789 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60;
790 p += 2;
791 if (p == end) return -1;
792 /* Test if there is available seconds */
793 if (p[0] < '0' || p[0] > '9')
794 goto nosec;
795 if (end - p < 2) return -1;
796 /* Add the seconds of the current minute */
797 epoch += 10 * (p[0] - '0') + p[1] - '0';
798 p += 2;
799 if (p == end) return -1;
800 /* Ignore seconds float part if present */
801 if (p[0] == '.') {
802 do {
803 if (++p == end) return -1;
804 } while (p[0] >= '0' && p[0] <= '9');
805 }
806
807nosec:
808 if (p[0] == 'Z') {
809 if (end - p != 1) return -1;
810 return epoch;
811 }
812 else if (p[0] == '+') {
813 if (end - p != 5) return -1;
814 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700815 return epoch - ((10 * (p[1] - '0') + p[2] - '0') * 60 * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60;
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200816 }
817 else if (p[0] == '-') {
818 if (end - p != 5) return -1;
819 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700820 return epoch + ((10 * (p[1] - '0') + p[2] - '0') * 60 * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60;
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200821 }
822
823 return -1;
824}
825
William Lallemand104a7a62019-10-14 14:14:59 +0200826/*
827 * struct alignment works here such that the key.key is the same as key_data
828 * Do not change the placement of key_data
829 */
830struct certificate_ocsp {
831 struct ebmb_node key;
832 unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH];
833 struct buffer response;
834 long expire;
835};
836
837struct ocsp_cbk_arg {
838 int is_single;
839 int single_kt;
840 union {
841 struct certificate_ocsp *s_ocsp;
842 /*
843 * m_ocsp will have multiple entries dependent on key type
844 * Entry 0 - DSA
845 * Entry 1 - ECDSA
846 * Entry 2 - RSA
847 */
848 struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES];
849 };
850};
851
Emeric Brun1d3865b2014-06-20 15:37:32 +0200852static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200853
854/* This function starts to check if the OCSP response (in DER format) contained
855 * in chunk 'ocsp_response' is valid (else exits on error).
856 * If 'cid' is not NULL, it will be compared to the OCSP certificate ID
857 * contained in the OCSP Response and exits on error if no match.
858 * If it's a valid OCSP Response:
859 * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container
860 * pointed by 'ocsp'.
861 * If 'ocsp' is NULL, the function looks up into the OCSP response's
862 * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted
863 * from the response) and exits on error if not found. Finally, If an OCSP response is
864 * already present in the container, it will be overwritten.
865 *
866 * Note: OCSP response containing more than one OCSP Single response is not
867 * considered valid.
868 *
869 * Returns 0 on success, 1 in error case.
870 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200871static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response,
872 struct certificate_ocsp *ocsp,
873 OCSP_CERTID *cid, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200874{
875 OCSP_RESPONSE *resp;
876 OCSP_BASICRESP *bs = NULL;
877 OCSP_SINGLERESP *sr;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200878 OCSP_CERTID *id;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200879 unsigned char *p = (unsigned char *) ocsp_response->area;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200880 int rc , count_sr;
Emeric Brun13a6b482014-06-20 15:44:34 +0200881 ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200882 int reason;
883 int ret = 1;
884
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200885 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
886 ocsp_response->data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200887 if (!resp) {
888 memprintf(err, "Unable to parse OCSP response");
889 goto out;
890 }
891
892 rc = OCSP_response_status(resp);
893 if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
894 memprintf(err, "OCSP response status not successful");
895 goto out;
896 }
897
898 bs = OCSP_response_get1_basic(resp);
899 if (!bs) {
900 memprintf(err, "Failed to get basic response from OCSP Response");
901 goto out;
902 }
903
904 count_sr = OCSP_resp_count(bs);
905 if (count_sr > 1) {
906 memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr);
907 goto out;
908 }
909
910 sr = OCSP_resp_get0(bs, 0);
911 if (!sr) {
912 memprintf(err, "Failed to get OCSP single response");
913 goto out;
914 }
915
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200916 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
917
Emeric Brun4147b2e2014-06-16 18:36:30 +0200918 rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd);
Emmanuel Hocdetef607052017-10-24 14:57:16 +0200919 if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) {
Emmanuel Hocdet872085c2017-10-10 15:18:52 +0200920 memprintf(err, "OCSP single response: certificate status is unknown");
Emeric Brun4147b2e2014-06-16 18:36:30 +0200921 goto out;
922 }
923
Emeric Brun13a6b482014-06-20 15:44:34 +0200924 if (!nextupd) {
925 memprintf(err, "OCSP single response: missing nextupdate");
926 goto out;
927 }
928
Emeric Brunc8b27b62014-06-19 14:16:17 +0200929 rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200930 if (!rc) {
931 memprintf(err, "OCSP single response: no longer valid.");
932 goto out;
933 }
934
935 if (cid) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200936 if (OCSP_id_cmp(id, cid)) {
Emeric Brun4147b2e2014-06-16 18:36:30 +0200937 memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer");
938 goto out;
939 }
940 }
941
942 if (!ocsp) {
943 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH];
944 unsigned char *p;
945
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200946 rc = i2d_OCSP_CERTID(id, NULL);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200947 if (!rc) {
948 memprintf(err, "OCSP single response: Unable to encode Certificate ID");
949 goto out;
950 }
951
952 if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) {
953 memprintf(err, "OCSP single response: Certificate ID too long");
954 goto out;
955 }
956
957 p = key;
958 memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200959 i2d_OCSP_CERTID(id, &p);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200960 ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH);
961 if (!ocsp) {
962 memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer");
963 goto out;
964 }
965 }
966
967 /* According to comments on "chunk_dup", the
968 previous chunk buffer will be freed */
969 if (!chunk_dup(&ocsp->response, ocsp_response)) {
970 memprintf(err, "OCSP response: Memory allocation error");
971 goto out;
972 }
973
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200974 ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW;
975
Emeric Brun4147b2e2014-06-16 18:36:30 +0200976 ret = 0;
977out:
Janusz Dziemidowicz8d710492017-03-08 16:59:41 +0100978 ERR_clear_error();
979
Emeric Brun4147b2e2014-06-16 18:36:30 +0200980 if (bs)
981 OCSP_BASICRESP_free(bs);
982
983 if (resp)
984 OCSP_RESPONSE_free(resp);
985
986 return ret;
987}
988/*
989 * External function use to update the OCSP response in the OCSP response's
990 * containers tree. The chunk 'ocsp_response' must contain the OCSP response
991 * to update in DER format.
992 *
993 * Returns 0 on success, 1 in error case.
994 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200995int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200996{
997 return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err);
998}
999
William Lallemand4a660132019-10-14 14:51:41 +02001000#endif
1001
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001002#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1003static int ssl_tlsext_ticket_key_cb(SSL *s, unsigned char key_name[16], unsigned char *iv, EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc)
1004{
Christopher Faulet16f45c82018-02-16 11:23:49 +01001005 struct tls_keys_ref *ref;
Emeric Brun9e754772019-01-10 17:51:55 +01001006 union tls_sess_key *keys;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001007 struct connection *conn;
1008 int head;
1009 int i;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001010 int ret = -1; /* error by default */
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001011
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001012 conn = SSL_get_ex_data(s, ssl_app_data_index);
Willy Tarreau07d94e42018-09-20 10:57:52 +02001013 ref = __objt_listener(conn->target)->bind_conf->keys_ref;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001014 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1015
1016 keys = ref->tlskeys;
1017 head = ref->tls_ticket_enc_index;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001018
1019 if (enc) {
1020 memcpy(key_name, keys[head].name, 16);
1021
1022 if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH))
Christopher Faulet16f45c82018-02-16 11:23:49 +01001023 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001024
Emeric Brun9e754772019-01-10 17:51:55 +01001025 if (ref->key_size_bits == 128) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001026
Emeric Brun9e754772019-01-10 17:51:55 +01001027 if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv))
1028 goto end;
1029
Willy Tarreau9356dac2019-05-10 09:22:53 +02001030 HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001031 ret = 1;
1032 }
1033 else if (ref->key_size_bits == 256 ) {
1034
1035 if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv))
1036 goto end;
1037
Willy Tarreau9356dac2019-05-10 09:22:53 +02001038 HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001039 ret = 1;
1040 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001041 } else {
1042 for (i = 0; i < TLS_TICKETS_NO; i++) {
1043 if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16))
1044 goto found;
1045 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01001046 ret = 0;
1047 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001048
Christopher Faulet16f45c82018-02-16 11:23:49 +01001049 found:
Emeric Brun9e754772019-01-10 17:51:55 +01001050 if (ref->key_size_bits == 128) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001051 HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001052 if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv))
1053 goto end;
1054 /* 2 for key renewal, 1 if current key is still valid */
1055 ret = i ? 2 : 1;
1056 }
1057 else if (ref->key_size_bits == 256) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001058 HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001059 if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv))
1060 goto end;
1061 /* 2 for key renewal, 1 if current key is still valid */
1062 ret = i ? 2 : 1;
1063 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001064 }
Emeric Brun9e754772019-01-10 17:51:55 +01001065
Christopher Faulet16f45c82018-02-16 11:23:49 +01001066 end:
1067 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1068 return ret;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001069}
1070
1071struct tls_keys_ref *tlskeys_ref_lookup(const char *filename)
1072{
1073 struct tls_keys_ref *ref;
1074
1075 list_for_each_entry(ref, &tlskeys_reference, list)
1076 if (ref->filename && strcmp(filename, ref->filename) == 0)
1077 return ref;
1078 return NULL;
1079}
1080
1081struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id)
1082{
1083 struct tls_keys_ref *ref;
1084
1085 list_for_each_entry(ref, &tlskeys_reference, list)
1086 if (ref->unique_id == unique_id)
1087 return ref;
1088 return NULL;
1089}
1090
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001091/* Update the key into ref: if keysize doesn't
Emeric Brun9e754772019-01-10 17:51:55 +01001092 * match existing ones, this function returns -1
1093 * else it returns 0 on success.
1094 */
1095int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref,
Willy Tarreau83061a82018-07-13 11:56:34 +02001096 struct buffer *tlskey)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001097{
Emeric Brun9e754772019-01-10 17:51:55 +01001098 if (ref->key_size_bits == 128) {
1099 if (tlskey->data != sizeof(struct tls_sess_key_128))
1100 return -1;
1101 }
1102 else if (ref->key_size_bits == 256) {
1103 if (tlskey->data != sizeof(struct tls_sess_key_256))
1104 return -1;
1105 }
1106 else
1107 return -1;
1108
Christopher Faulet16f45c82018-02-16 11:23:49 +01001109 HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001110 memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)),
1111 tlskey->area, tlskey->data);
Christopher Faulet16f45c82018-02-16 11:23:49 +01001112 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
1113 HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Emeric Brun9e754772019-01-10 17:51:55 +01001114
1115 return 0;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001116}
1117
Willy Tarreau83061a82018-07-13 11:56:34 +02001118int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001119{
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001120 struct tls_keys_ref *ref = tlskeys_ref_lookup(filename);
1121
1122 if(!ref) {
1123 memprintf(err, "Unable to locate the referenced filename: %s", filename);
1124 return 1;
1125 }
Emeric Brun9e754772019-01-10 17:51:55 +01001126 if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) {
1127 memprintf(err, "Invalid key size");
1128 return 1;
1129 }
1130
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001131 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001132}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001133
1134/* This function finalize the configuration parsing. Its set all the
Willy Tarreaud1c57502016-12-22 22:46:15 +01001135 * automatic ids. It's called just after the basic checks. It returns
1136 * 0 on success otherwise ERR_*.
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001137 */
Willy Tarreaud1c57502016-12-22 22:46:15 +01001138static int tlskeys_finalize_config(void)
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001139{
1140 int i = 0;
1141 struct tls_keys_ref *ref, *ref2, *ref3;
1142 struct list tkr = LIST_HEAD_INIT(tkr);
1143
1144 list_for_each_entry(ref, &tlskeys_reference, list) {
1145 if (ref->unique_id == -1) {
1146 /* Look for the first free id. */
1147 while (1) {
1148 list_for_each_entry(ref2, &tlskeys_reference, list) {
1149 if (ref2->unique_id == i) {
1150 i++;
1151 break;
1152 }
1153 }
1154 if (&ref2->list == &tlskeys_reference)
1155 break;
1156 }
1157
1158 /* Uses the unique id and increment it for the next entry. */
1159 ref->unique_id = i;
1160 i++;
1161 }
1162 }
1163
1164 /* This sort the reference list by id. */
1165 list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) {
1166 LIST_DEL(&ref->list);
1167 list_for_each_entry(ref3, &tkr, list) {
1168 if (ref->unique_id < ref3->unique_id) {
1169 LIST_ADDQ(&ref3->list, &ref->list);
1170 break;
1171 }
1172 }
1173 if (&ref3->list == &tkr)
1174 LIST_ADDQ(&tkr, &ref->list);
1175 }
1176
1177 /* swap root */
1178 LIST_ADD(&tkr, &tlskeys_reference);
1179 LIST_DEL(&tkr);
Willy Tarreaud1c57502016-12-22 22:46:15 +01001180 return 0;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001181}
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001182#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
1183
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001184#ifndef OPENSSL_NO_OCSP
yanbzhube2774d2015-12-10 15:07:30 -05001185int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype)
1186{
1187 switch (evp_keytype) {
1188 case EVP_PKEY_RSA:
1189 return 2;
1190 case EVP_PKEY_DSA:
1191 return 0;
1192 case EVP_PKEY_EC:
1193 return 1;
1194 }
1195
1196 return -1;
1197}
1198
Emeric Brun4147b2e2014-06-16 18:36:30 +02001199/*
1200 * Callback used to set OCSP status extension content in server hello.
1201 */
1202int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg)
1203{
yanbzhube2774d2015-12-10 15:07:30 -05001204 struct certificate_ocsp *ocsp;
1205 struct ocsp_cbk_arg *ocsp_arg;
1206 char *ssl_buf;
1207 EVP_PKEY *ssl_pkey;
1208 int key_type;
1209 int index;
1210
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001211 ocsp_arg = arg;
yanbzhube2774d2015-12-10 15:07:30 -05001212
1213 ssl_pkey = SSL_get_privatekey(ssl);
1214 if (!ssl_pkey)
1215 return SSL_TLSEXT_ERR_NOACK;
1216
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001217 key_type = EVP_PKEY_base_id(ssl_pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001218
1219 if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type)
1220 ocsp = ocsp_arg->s_ocsp;
1221 else {
1222 /* For multiple certs per context, we have to find the correct OCSP response based on
1223 * the certificate type
1224 */
1225 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
1226
1227 if (index < 0)
1228 return SSL_TLSEXT_ERR_NOACK;
1229
1230 ocsp = ocsp_arg->m_ocsp[index];
1231
1232 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001233
1234 if (!ocsp ||
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001235 !ocsp->response.area ||
1236 !ocsp->response.data ||
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001237 (ocsp->expire < now.tv_sec))
Emeric Brun4147b2e2014-06-16 18:36:30 +02001238 return SSL_TLSEXT_ERR_NOACK;
1239
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001240 ssl_buf = OPENSSL_malloc(ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001241 if (!ssl_buf)
1242 return SSL_TLSEXT_ERR_NOACK;
1243
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001244 memcpy(ssl_buf, ocsp->response.area, ocsp->response.data);
1245 SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001246
1247 return SSL_TLSEXT_ERR_OK;
1248}
1249
William Lallemand4a660132019-10-14 14:51:41 +02001250#endif
1251
1252#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001253/*
1254 * This function enables the handling of OCSP status extension on 'ctx' if a
William Lallemand246c0242019-10-11 08:59:13 +02001255 * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP
1256 * status extension, the issuer's certificate is mandatory. It should be
1257 * present in ckch->ocsp_issuer.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001258 *
William Lallemand246c0242019-10-11 08:59:13 +02001259 * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an
1260 * OCSP response. If file is empty or content is not a valid OCSP response,
1261 * OCSP status extension is enabled but OCSP response is ignored (a warning is
1262 * displayed).
Emeric Brun4147b2e2014-06-16 18:36:30 +02001263 *
1264 * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is
Joseph Herlant017b3da2018-11-15 09:07:59 -08001265 * successfully enabled, or -1 in other error case.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001266 */
William Lallemand4a660132019-10-14 14:51:41 +02001267#ifndef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001268static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, STACK_OF(X509) *chain)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001269{
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001270 X509 *x, *issuer;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001271 OCSP_CERTID *cid = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001272 int i, ret = -1;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001273 struct certificate_ocsp *ocsp = NULL, *iocsp;
1274 char *warn = NULL;
1275 unsigned char *p;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001276 void (*callback) (void);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001277
Emeric Brun4147b2e2014-06-16 18:36:30 +02001278
William Lallemand246c0242019-10-11 08:59:13 +02001279 x = ckch->cert;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001280 if (!x)
1281 goto out;
1282
William Lallemand246c0242019-10-11 08:59:13 +02001283 issuer = ckch->ocsp_issuer;
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001284 /* take issuer from chain over ocsp_issuer, is what is done historicaly */
1285 if (chain) {
1286 /* check if one of the certificate of the chain is the issuer */
1287 for (i = 0; i < sk_X509_num(chain); i++) {
1288 X509 *ti = sk_X509_value(chain, i);
1289 if (X509_check_issued(ti, x) == X509_V_OK) {
1290 issuer = ti;
1291 break;
1292 }
1293 }
1294 }
William Lallemand246c0242019-10-11 08:59:13 +02001295 if (!issuer)
1296 goto out;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001297
1298 cid = OCSP_cert_to_id(0, x, issuer);
1299 if (!cid)
1300 goto out;
1301
1302 i = i2d_OCSP_CERTID(cid, NULL);
1303 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1304 goto out;
1305
Vincent Bernat02779b62016-04-03 13:48:43 +02001306 ocsp = calloc(1, sizeof(*ocsp));
Emeric Brun4147b2e2014-06-16 18:36:30 +02001307 if (!ocsp)
1308 goto out;
1309
1310 p = ocsp->key_data;
1311 i2d_OCSP_CERTID(cid, &p);
1312
1313 iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH);
1314 if (iocsp == ocsp)
1315 ocsp = NULL;
1316
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001317#ifndef SSL_CTX_get_tlsext_status_cb
1318# define SSL_CTX_get_tlsext_status_cb(ctx, cb) \
1319 *cb = (void (*) (void))ctx->tlsext_status_cb;
1320#endif
1321 SSL_CTX_get_tlsext_status_cb(ctx, &callback);
1322
1323 if (!callback) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001324 struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg));
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001325 EVP_PKEY *pkey;
yanbzhube2774d2015-12-10 15:07:30 -05001326
1327 cb_arg->is_single = 1;
1328 cb_arg->s_ocsp = iocsp;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001329
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001330 pkey = X509_get_pubkey(x);
1331 cb_arg->single_kt = EVP_PKEY_base_id(pkey);
1332 EVP_PKEY_free(pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001333
1334 SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk);
1335 SSL_CTX_set_tlsext_status_arg(ctx, cb_arg);
1336 } else {
1337 /*
1338 * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx
1339 * Update that cb_arg with the new cert's staple
1340 */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001341 struct ocsp_cbk_arg *cb_arg;
yanbzhube2774d2015-12-10 15:07:30 -05001342 struct certificate_ocsp *tmp_ocsp;
1343 int index;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001344 int key_type;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001345 EVP_PKEY *pkey;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001346
1347#ifdef SSL_CTX_get_tlsext_status_arg
1348 SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg);
1349#else
1350 cb_arg = ctx->tlsext_status_arg;
1351#endif
yanbzhube2774d2015-12-10 15:07:30 -05001352
1353 /*
1354 * The following few lines will convert cb_arg from a single ocsp to multi ocsp
1355 * the order of operations below matter, take care when changing it
1356 */
1357 tmp_ocsp = cb_arg->s_ocsp;
1358 index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt);
1359 cb_arg->s_ocsp = NULL;
1360 cb_arg->m_ocsp[index] = tmp_ocsp;
1361 cb_arg->is_single = 0;
1362 cb_arg->single_kt = 0;
1363
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001364 pkey = X509_get_pubkey(x);
1365 key_type = EVP_PKEY_base_id(pkey);
1366 EVP_PKEY_free(pkey);
1367
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001368 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
yanbzhube2774d2015-12-10 15:07:30 -05001369 if (index >= 0 && !cb_arg->m_ocsp[index])
1370 cb_arg->m_ocsp[index] = iocsp;
1371
1372 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001373
1374 ret = 0;
1375
1376 warn = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001377 if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) {
William Lallemand3b5f3602019-10-16 18:05:05 +02001378 memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure");
Christopher Faulet767a84b2017-11-24 16:50:31 +01001379 ha_warning("%s.\n", warn);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001380 }
1381
1382out:
Emeric Brun4147b2e2014-06-16 18:36:30 +02001383 if (cid)
1384 OCSP_CERTID_free(cid);
1385
1386 if (ocsp)
1387 free(ocsp);
1388
1389 if (warn)
1390 free(warn);
1391
Emeric Brun4147b2e2014-06-16 18:36:30 +02001392 return ret;
1393}
William Lallemand4a660132019-10-14 14:51:41 +02001394#else /* OPENSSL_IS_BORINGSSL */
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001395static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, STACK_OF(X509) *chain)
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001396{
William Lallemand4a660132019-10-14 14:51:41 +02001397 return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *)ckch->ocsp_response->area, ckch->ocsp_response->data);
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001398}
1399#endif
1400
William Lallemand4a660132019-10-14 14:51:41 +02001401#endif
1402
1403
Willy Tarreau5db847a2019-05-09 14:13:35 +02001404#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001405
1406#define CT_EXTENSION_TYPE 18
1407
William Lallemand03c331c2020-05-13 10:10:01 +02001408int sctl_ex_index = -1;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001409
1410int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg)
1411{
Willy Tarreau83061a82018-07-13 11:56:34 +02001412 struct buffer *sctl = add_arg;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001413
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001414 *out = (unsigned char *) sctl->area;
1415 *outlen = sctl->data;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001416
1417 return 1;
1418}
1419
1420int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg)
1421{
1422 return 1;
1423}
1424
William Lallemanda17f4112019-10-10 15:16:44 +02001425static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001426{
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001427 int ret = -1;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001428
William Lallemanda17f4112019-10-10 15:16:44 +02001429 if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL))
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001430 goto out;
1431
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001432 SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl);
1433
1434 ret = 0;
1435
1436out:
1437 return ret;
1438}
1439
1440#endif
1441
Emeric Brune1f38db2012-09-03 20:36:47 +02001442void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
1443{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001444 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001445 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001446 BIO *write_bio;
Willy Tarreau622317d2015-02-27 16:36:16 +01001447 (void)ret; /* shut gcc stupid warning */
Emeric Brune1f38db2012-09-03 20:36:47 +02001448
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001449#ifndef SSL_OP_NO_RENEGOTIATION
1450 /* Please note that BoringSSL defines this macro to zero so don't
1451 * change this to #if and do not assign a default value to this macro!
1452 */
Emeric Brune1f38db2012-09-03 20:36:47 +02001453 if (where & SSL_CB_HANDSHAKE_START) {
1454 /* Disable renegotiation (CVE-2009-3555) */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001455 if ((conn->flags & (CO_FL_WAIT_L6_CONN | CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == 0) {
Emeric Brune1f38db2012-09-03 20:36:47 +02001456 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001457 conn->err_code = CO_ER_SSL_RENEG;
1458 }
Emeric Brune1f38db2012-09-03 20:36:47 +02001459 }
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001460#endif
Emeric Brund8b2bb52014-01-28 15:43:53 +01001461
1462 if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001463 if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) {
Emeric Brund8b2bb52014-01-28 15:43:53 +01001464 /* Long certificate chains optimz
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001465 If write and read bios are different, we
Emeric Brund8b2bb52014-01-28 15:43:53 +01001466 consider that the buffering was activated,
1467 so we rise the output buffer size from 4k
1468 to 16k */
1469 write_bio = SSL_get_wbio(ssl);
1470 if (write_bio != SSL_get_rbio(ssl)) {
1471 BIO_set_write_buffer_size(write_bio, 16384);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001472 ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001473 }
1474 }
1475 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001476}
1477
Emeric Brune64aef12012-09-21 13:15:06 +02001478/* Callback is called for each certificate of the chain during a verify
1479 ok is set to 1 if preverify detect no error on current certificate.
1480 Returns 0 to break the handshake, 1 otherwise. */
Evan Broderbe554312013-06-27 00:05:25 -07001481int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +02001482{
1483 SSL *ssl;
1484 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001485 struct ssl_sock_ctx *ctx;
Emeric Brun81c00f02012-09-21 14:31:21 +02001486 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +02001487
1488 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001489 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emeric Brune64aef12012-09-21 13:15:06 +02001490
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001491 ctx = conn->xprt_ctx;
1492
1493 ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +02001494
Emeric Brun81c00f02012-09-21 14:31:21 +02001495 if (ok) /* no errors */
1496 return ok;
1497
1498 depth = X509_STORE_CTX_get_error_depth(x_store);
1499 err = X509_STORE_CTX_get_error(x_store);
1500
1501 /* check if CA error needs to be ignored */
1502 if (depth > 0) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001503 if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) {
1504 ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
1505 ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +02001506 }
1507
Willy Tarreau731248f2020-02-04 14:02:02 +01001508 if (err < 64 && __objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001509 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001510 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001511 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001512 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001513
Willy Tarreau20879a02012-12-03 16:32:10 +01001514 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001515 return 0;
1516 }
1517
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001518 if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st))
1519 ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +02001520
Emeric Brun81c00f02012-09-21 14:31:21 +02001521 /* check if certificate error needs to be ignored */
Willy Tarreau731248f2020-02-04 14:02:02 +01001522 if (err < 64 && __objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001523 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001524 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001525 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001526 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001527
Willy Tarreau20879a02012-12-03 16:32:10 +01001528 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001529 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001530}
1531
Dragan Dosen9ac98092020-05-11 15:51:45 +02001532#ifdef TLS1_RT_HEARTBEAT
1533static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version,
1534 int content_type, const void *buf, size_t len,
1535 SSL *ssl)
1536{
1537 /* test heartbeat received (write_p is set to 0
1538 for a received record) */
1539 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
1540 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
1541 const unsigned char *p = buf;
1542 unsigned int payload;
1543
1544 ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
1545
1546 /* Check if this is a CVE-2014-0160 exploitation attempt. */
1547 if (*p != TLS1_HB_REQUEST)
1548 return;
1549
1550 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
1551 goto kill_it;
1552
1553 payload = (p[1] * 256) + p[2];
1554 if (3 + payload + 16 <= len)
1555 return; /* OK no problem */
1556 kill_it:
1557 /* We have a clear heartbleed attack (CVE-2014-0160), the
1558 * advertised payload is larger than the advertised packet
1559 * length, so we have garbage in the buffer between the
1560 * payload and the end of the buffer (p+len). We can't know
1561 * if the SSL stack is patched, and we don't know if we can
1562 * safely wipe out the area between p+3+len and payload.
1563 * So instead, we prevent the response from being sent by
1564 * setting the max_send_fragment to 0 and we report an SSL
1565 * error, which will kill this connection. It will be reported
1566 * above as SSL_ERROR_SSL while an other handshake failure with
1567 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
1568 */
1569 ssl->max_send_fragment = 0;
1570 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
1571 }
1572}
1573#endif
1574
1575static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version,
1576 int content_type, const void *buf, size_t len,
1577 SSL *ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001578{
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001579 struct ssl_capture *capture;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001580 unsigned char *msg;
1581 unsigned char *end;
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001582 size_t rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001583
1584 /* This function is called for "from client" and "to server"
1585 * connections. The combination of write_p == 0 and content_type == 22
Joseph Herlant017b3da2018-11-15 09:07:59 -08001586 * is only available during "from client" connection.
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001587 */
1588
1589 /* "write_p" is set to 0 is the bytes are received messages,
1590 * otherwise it is set to 1.
1591 */
1592 if (write_p != 0)
1593 return;
1594
1595 /* content_type contains the type of message received or sent
1596 * according with the SSL/TLS protocol spec. This message is
1597 * encoded with one byte. The value 256 (two bytes) is used
1598 * for designing the SSL/TLS record layer. According with the
1599 * rfc6101, the expected message (other than 256) are:
1600 * - change_cipher_spec(20)
1601 * - alert(21)
1602 * - handshake(22)
1603 * - application_data(23)
1604 * - (255)
1605 * We are interessed by the handshake and specially the client
1606 * hello.
1607 */
1608 if (content_type != 22)
1609 return;
1610
1611 /* The message length is at least 4 bytes, containing the
1612 * message type and the message length.
1613 */
1614 if (len < 4)
1615 return;
1616
1617 /* First byte of the handshake message id the type of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001618 * message. The known types are:
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001619 * - hello_request(0)
1620 * - client_hello(1)
1621 * - server_hello(2)
1622 * - certificate(11)
1623 * - server_key_exchange (12)
1624 * - certificate_request(13)
1625 * - server_hello_done(14)
1626 * We are interested by the client hello.
1627 */
1628 msg = (unsigned char *)buf;
1629 if (msg[0] != 1)
1630 return;
1631
1632 /* Next three bytes are the length of the message. The total length
1633 * must be this decoded length + 4. If the length given as argument
1634 * is not the same, we abort the protocol dissector.
1635 */
1636 rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3];
1637 if (len < rec_len + 4)
1638 return;
1639 msg += 4;
1640 end = msg + rec_len;
1641 if (end < msg)
1642 return;
1643
1644 /* Expect 2 bytes for protocol version (1 byte for major and 1 byte
1645 * for minor, the random, composed by 4 bytes for the unix time and
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001646 * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28.
1647 */
1648 msg += 1 + 1 + 4 + 28;
1649 if (msg > end)
1650 return;
1651
1652 /* Next, is session id:
1653 * if present, we have to jump by length + 1 for the size information
1654 * if not present, we have to jump by 1 only
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001655 */
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001656 if (msg[0] > 0)
1657 msg += msg[0];
1658 msg += 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001659 if (msg > end)
1660 return;
1661
1662 /* Next two bytes are the ciphersuite length. */
1663 if (msg + 2 > end)
1664 return;
1665 rec_len = (msg[0] << 8) + msg[1];
1666 msg += 2;
1667 if (msg + rec_len > end || msg + rec_len < msg)
1668 return;
1669
Willy Tarreaubafbe012017-11-24 17:34:44 +01001670 capture = pool_alloc_dirty(pool_head_ssl_capture);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001671 if (!capture)
1672 return;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001673 /* Compute the xxh64 of the ciphersuite. */
1674 capture->xxh64 = XXH64(msg, rec_len, 0);
1675
1676 /* Capture the ciphersuite. */
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001677 capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ?
1678 global_ssl.capture_cipherlist : rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001679 memcpy(capture->ciphersuite, msg, capture->ciphersuite_len);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001680
1681 SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001682}
1683
Emeric Brun29f037d2014-04-25 19:05:36 +02001684/* Callback is called for ssl protocol analyse */
1685void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
1686{
Dragan Dosen1e7ed042020-05-08 18:30:00 +02001687 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
1688 struct ssl_sock_msg_callback *cbk;
1689
Dragan Dosen1e7ed042020-05-08 18:30:00 +02001690 /* Try to call all callback functions that were registered by using
1691 * ssl_sock_register_msg_callback().
1692 */
1693 list_for_each_entry(cbk, &ssl_sock_msg_callbacks, list) {
1694 cbk->func(conn, write_p, version, content_type, buf, len, ssl);
1695 }
Emeric Brun29f037d2014-04-25 19:05:36 +02001696}
1697
Bernard Spil13c53f82018-02-15 13:34:58 +01001698#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchardc7566002018-11-20 23:33:50 +01001699static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen,
1700 const unsigned char *in, unsigned int inlen,
1701 void *arg)
1702{
1703 struct server *srv = arg;
1704
1705 if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str,
1706 srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED)
1707 return SSL_TLSEXT_ERR_OK;
1708 return SSL_TLSEXT_ERR_NOACK;
1709}
1710#endif
1711
1712#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001713/* This callback is used so that the server advertises the list of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001714 * negotiable protocols for NPN.
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001715 */
1716static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
1717 unsigned int *len, void *arg)
1718{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001719 struct ssl_bind_conf *conf = arg;
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001720
1721 *data = (const unsigned char *)conf->npn_str;
1722 *len = conf->npn_len;
1723 return SSL_TLSEXT_ERR_OK;
1724}
1725#endif
1726
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001727#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001728/* This callback is used so that the server advertises the list of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001729 * negotiable protocols for ALPN.
Willy Tarreauab861d32013-04-02 02:30:41 +02001730 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001731static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out,
1732 unsigned char *outlen,
1733 const unsigned char *server,
1734 unsigned int server_len, void *arg)
Willy Tarreauab861d32013-04-02 02:30:41 +02001735{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001736 struct ssl_bind_conf *conf = arg;
Willy Tarreauab861d32013-04-02 02:30:41 +02001737
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001738 if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str,
1739 conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) {
1740 return SSL_TLSEXT_ERR_NOACK;
1741 }
Willy Tarreauab861d32013-04-02 02:30:41 +02001742 return SSL_TLSEXT_ERR_OK;
1743}
1744#endif
1745
Willy Tarreauc8ad3be2015-06-17 15:48:26 +02001746#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001747#ifndef SSL_NO_GENERATE_CERTIFICATES
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001748
Christopher Faulet30548802015-06-11 13:39:32 +02001749/* Create a X509 certificate with the specified servername and serial. This
1750 * function returns a SSL_CTX object or NULL if an error occurs. */
Christopher Faulet7969a332015-10-09 11:15:03 +02001751static SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001752ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001753{
Christopher Faulet7969a332015-10-09 11:15:03 +02001754 X509 *cacert = bind_conf->ca_sign_cert;
1755 EVP_PKEY *capkey = bind_conf->ca_sign_pkey;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001756 SSL_CTX *ssl_ctx = NULL;
1757 X509 *newcrt = NULL;
1758 EVP_PKEY *pkey = NULL;
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001759 SSL *tmp_ssl = NULL;
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001760 CONF *ctmp = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001761 X509_NAME *name;
1762 const EVP_MD *digest;
1763 X509V3_CTX ctx;
1764 unsigned int i;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001765 int key_type;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001766
Christopher Faulet48a83322017-07-28 16:56:09 +02001767 /* Get the private key of the default certificate and use it */
Willy Tarreau5db847a2019-05-09 14:13:35 +02001768#if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L)
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001769 pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx);
1770#else
1771 tmp_ssl = SSL_new(bind_conf->default_ctx);
1772 if (tmp_ssl)
1773 pkey = SSL_get_privatekey(tmp_ssl);
1774#endif
1775 if (!pkey)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001776 goto mkcert_error;
1777
1778 /* Create the certificate */
1779 if (!(newcrt = X509_new()))
1780 goto mkcert_error;
1781
1782 /* Set version number for the certificate (X509v3) and the serial
1783 * number */
1784 if (X509_set_version(newcrt, 2L) != 1)
1785 goto mkcert_error;
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01001786 ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1));
Christopher Faulet31af49d2015-06-09 17:29:50 +02001787
1788 /* Set duration for the certificate */
Rosen Penev68185952018-12-14 08:47:02 -08001789 if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) ||
1790 !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001791 goto mkcert_error;
1792
1793 /* set public key in the certificate */
1794 if (X509_set_pubkey(newcrt, pkey) != 1)
1795 goto mkcert_error;
1796
1797 /* Set issuer name from the CA */
1798 if (!(name = X509_get_subject_name(cacert)))
1799 goto mkcert_error;
1800 if (X509_set_issuer_name(newcrt, name) != 1)
1801 goto mkcert_error;
1802
1803 /* Set the subject name using the same, but the CN */
1804 name = X509_NAME_dup(name);
1805 if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
1806 (const unsigned char *)servername,
1807 -1, -1, 0) != 1) {
1808 X509_NAME_free(name);
1809 goto mkcert_error;
1810 }
1811 if (X509_set_subject_name(newcrt, name) != 1) {
1812 X509_NAME_free(name);
1813 goto mkcert_error;
1814 }
1815 X509_NAME_free(name);
1816
1817 /* Add x509v3 extensions as specified */
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001818 ctmp = NCONF_new(NULL);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001819 X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0);
1820 for (i = 0; i < X509V3_EXT_SIZE; i++) {
1821 X509_EXTENSION *ext;
1822
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001823 if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i])))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001824 goto mkcert_error;
1825 if (!X509_add_ext(newcrt, ext, -1)) {
1826 X509_EXTENSION_free(ext);
1827 goto mkcert_error;
1828 }
1829 X509_EXTENSION_free(ext);
1830 }
1831
1832 /* Sign the certificate with the CA private key */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001833
1834 key_type = EVP_PKEY_base_id(capkey);
1835
1836 if (key_type == EVP_PKEY_DSA)
1837 digest = EVP_sha1();
1838 else if (key_type == EVP_PKEY_RSA)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001839 digest = EVP_sha256();
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001840 else if (key_type == EVP_PKEY_EC)
Christopher Faulet7969a332015-10-09 11:15:03 +02001841 digest = EVP_sha256();
1842 else {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02001843#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet7969a332015-10-09 11:15:03 +02001844 int nid;
1845
1846 if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0)
1847 goto mkcert_error;
1848 if (!(digest = EVP_get_digestbynid(nid)))
1849 goto mkcert_error;
Christopher Faulete7db2162015-10-19 13:59:24 +02001850#else
1851 goto mkcert_error;
1852#endif
Christopher Faulet7969a332015-10-09 11:15:03 +02001853 }
1854
Christopher Faulet31af49d2015-06-09 17:29:50 +02001855 if (!(X509_sign(newcrt, capkey, digest)))
1856 goto mkcert_error;
1857
1858 /* Create and set the new SSL_CTX */
1859 if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method())))
1860 goto mkcert_error;
1861 if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
1862 goto mkcert_error;
1863 if (!SSL_CTX_use_certificate(ssl_ctx, newcrt))
1864 goto mkcert_error;
1865 if (!SSL_CTX_check_private_key(ssl_ctx))
1866 goto mkcert_error;
1867
1868 if (newcrt) X509_free(newcrt);
Christopher Faulet7969a332015-10-09 11:15:03 +02001869
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01001870#ifndef OPENSSL_NO_DH
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001871 SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh);
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01001872#endif
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001873#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
1874 {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001875 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001876 EC_KEY *ecc;
1877 int nid;
1878
1879 if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef)
1880 goto end;
1881 if (!(ecc = EC_KEY_new_by_curve_name(nid)))
1882 goto end;
1883 SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc);
1884 EC_KEY_free(ecc);
1885 }
1886#endif
1887 end:
Christopher Faulet31af49d2015-06-09 17:29:50 +02001888 return ssl_ctx;
1889
1890 mkcert_error:
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001891 if (ctmp) NCONF_free(ctmp);
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001892 if (tmp_ssl) SSL_free(tmp_ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001893 if (ssl_ctx) SSL_CTX_free(ssl_ctx);
1894 if (newcrt) X509_free(newcrt);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001895 return NULL;
1896}
1897
Christopher Faulet7969a332015-10-09 11:15:03 +02001898SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001899ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key)
Christopher Faulet7969a332015-10-09 11:15:03 +02001900{
Willy Tarreau07d94e42018-09-20 10:57:52 +02001901 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
Olivier Houchard66ab4982019-02-26 18:37:15 +01001902 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001903
Olivier Houchard66ab4982019-02-26 18:37:15 +01001904 return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl);
Christopher Faulet7969a332015-10-09 11:15:03 +02001905}
1906
Christopher Faulet30548802015-06-11 13:39:32 +02001907/* Do a lookup for a certificate in the LRU cache used to store generated
Emeric Brun821bb9b2017-06-15 16:37:39 +02001908 * certificates and immediately assign it to the SSL session if not null. */
Christopher Faulet30548802015-06-11 13:39:32 +02001909SSL_CTX *
Emeric Brun821bb9b2017-06-15 16:37:39 +02001910ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet30548802015-06-11 13:39:32 +02001911{
1912 struct lru64 *lru = NULL;
1913
1914 if (ssl_ctx_lru_tree) {
Willy Tarreau03f4ec42018-05-17 10:56:47 +02001915 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001916 lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02001917 if (lru && lru->domain) {
1918 if (ssl)
1919 SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data);
Willy Tarreau03f4ec42018-05-17 10:56:47 +02001920 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02001921 return (SSL_CTX *)lru->data;
Emeric Brun821bb9b2017-06-15 16:37:39 +02001922 }
Willy Tarreau03f4ec42018-05-17 10:56:47 +02001923 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02001924 }
1925 return NULL;
1926}
1927
Emeric Brun821bb9b2017-06-15 16:37:39 +02001928/* Same as <ssl_sock_assign_generated_cert> but without SSL session. This
1929 * function is not thread-safe, it should only be used to check if a certificate
1930 * exists in the lru cache (with no warranty it will not be removed by another
1931 * thread). It is kept for backward compatibility. */
1932SSL_CTX *
1933ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf)
1934{
1935 return ssl_sock_assign_generated_cert(key, bind_conf, NULL);
1936}
1937
Christopher Fauletd2cab922015-07-28 16:03:47 +02001938/* Set a certificate int the LRU cache used to store generated
1939 * certificate. Return 0 on success, otherwise -1 */
1940int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001941ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02001942{
1943 struct lru64 *lru = NULL;
1944
1945 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001946 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001947 lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02001948 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001949 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02001950 return -1;
Emeric Brun821bb9b2017-06-15 16:37:39 +02001951 }
Christopher Faulet30548802015-06-11 13:39:32 +02001952 if (lru->domain && lru->data)
1953 lru->free((SSL_CTX *)lru->data);
Christopher Faulet7969a332015-10-09 11:15:03 +02001954 lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001955 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02001956 return 0;
Christopher Faulet30548802015-06-11 13:39:32 +02001957 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02001958 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02001959}
1960
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001961/* Compute the key of the certificate. */
Christopher Faulet30548802015-06-11 13:39:32 +02001962unsigned int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001963ssl_sock_generated_cert_key(const void *data, size_t len)
Christopher Faulet30548802015-06-11 13:39:32 +02001964{
1965 return XXH32(data, len, ssl_ctx_lru_seed);
1966}
1967
Willy Tarreau2f63ef42015-10-20 15:16:01 +02001968/* Generate a cert and immediately assign it to the SSL session so that the cert's
1969 * refcount is maintained regardless of the cert's presence in the LRU cache.
1970 */
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02001971static int
Christopher Faulet7969a332015-10-09 11:15:03 +02001972ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001973{
1974 X509 *cacert = bind_conf->ca_sign_cert;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001975 SSL_CTX *ssl_ctx = NULL;
1976 struct lru64 *lru = NULL;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001977 unsigned int key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001978
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001979 key = ssl_sock_generated_cert_key(servername, strlen(servername));
Christopher Faulet31af49d2015-06-09 17:29:50 +02001980 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001981 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001982 lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001983 if (lru && lru->domain)
1984 ssl_ctx = (SSL_CTX *)lru->data;
Christopher Fauletd2cab922015-07-28 16:03:47 +02001985 if (!ssl_ctx && lru) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001986 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001987 lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02001988 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02001989 SSL_set_SSL_CTX(ssl, ssl_ctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001990 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02001991 return 1;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001992 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02001993 else {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001994 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Willy Tarreau2f63ef42015-10-20 15:16:01 +02001995 SSL_set_SSL_CTX(ssl, ssl_ctx);
1996 /* No LRU cache, this CTX will be released as soon as the session dies */
1997 SSL_CTX_free(ssl_ctx);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02001998 return 1;
Willy Tarreau2f63ef42015-10-20 15:16:01 +02001999 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002000 return 0;
2001}
2002static int
2003ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl)
2004{
2005 unsigned int key;
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002006 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002007
Willy Tarreauf5bdb642019-07-17 11:29:32 +02002008 if (conn_get_dst(conn)) {
Willy Tarreau085a1512019-07-17 14:47:35 +02002009 key = ssl_sock_generated_cert_key(conn->dst, get_addr_len(conn->dst));
Emeric Brun821bb9b2017-06-15 16:37:39 +02002010 if (ssl_sock_assign_generated_cert(key, bind_conf, ssl))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002011 return 1;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002012 }
2013 return 0;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002014}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002015#endif /* !defined SSL_NO_GENERATE_CERTIFICATES */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002016
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002017#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002018
2019static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002020{
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02002021#if SSL_OP_NO_SSLv3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002022 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002023 : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method());
2024#endif
2025}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002026static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2027 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002028 : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method());
2029}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002030static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002031#if SSL_OP_NO_TLSv1_1
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002032 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002033 : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method());
2034#endif
2035}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002036static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002037#if SSL_OP_NO_TLSv1_2
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002038 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002039 : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method());
2040#endif
2041}
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002042/* TLSv1.2 is the last supported version in this context. */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002043static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {}
2044/* Unusable in this context. */
2045static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {}
2046static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {}
2047static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {}
2048static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {}
2049static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {}
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002050#else /* openssl >= 1.1.0 */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002051
2052static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) {
2053 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002054 : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION);
2055}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002056static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {
2057 c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION)
2058 : SSL_set_min_proto_version(ssl, SSL3_VERSION);
2059}
2060static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2061 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002062 : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2063}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002064static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {
2065 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION)
2066 : SSL_set_min_proto_version(ssl, TLS1_VERSION);
2067}
2068static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
2069 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002070 : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
2071}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002072static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {
2073 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION)
2074 : SSL_set_min_proto_version(ssl, TLS1_1_VERSION);
2075}
2076static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
2077 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002078 : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
2079}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002080static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {
2081 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION)
2082 : SSL_set_min_proto_version(ssl, TLS1_2_VERSION);
2083}
2084static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002085#if SSL_OP_NO_TLSv1_3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002086 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002087 : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
2088#endif
2089}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002090static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {
2091#if SSL_OP_NO_TLSv1_3
2092 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION)
2093 : SSL_set_min_proto_version(ssl, TLS1_3_VERSION);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002094#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002095}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002096#endif
2097static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { }
2098static void ssl_set_None_func(SSL *ssl, set_context_func c) { }
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002099
William Lallemand7fd8b452020-05-07 15:20:43 +02002100struct methodVersions methodVersions[] = {
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002101 {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */
2102 {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */
2103 {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */
2104 {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */
2105 {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */
2106 {SSL_OP_NO_TLSv1_3, MC_SSL_O_NO_TLSV13, ctx_set_TLSv13_func, ssl_set_TLSv13_func, "TLSv1.3"}, /* CONF_TLSV13 */
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002107};
2108
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002109static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx)
2110{
2111 SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk);
2112 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx)));
2113 SSL_set_SSL_CTX(ssl, ctx);
2114}
2115
Willy Tarreau5db847a2019-05-09 14:13:35 +02002116#if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL))
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002117
2118static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv)
2119{
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002120 struct bind_conf *s = priv;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002121 (void)al; /* shut gcc stupid warning */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002122
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002123 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs)
2124 return SSL_TLSEXT_ERR_OK;
2125 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002126}
2127
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002128#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002129static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx)
2130{
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002131 SSL *ssl = ctx->ssl;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002132#else
2133static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg)
2134{
2135#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002136 struct connection *conn;
2137 struct bind_conf *s;
2138 const uint8_t *extension_data;
2139 size_t extension_len;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002140 int has_rsa_sig = 0, has_ecdsa_sig = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002141
2142 char *wildp = NULL;
2143 const uint8_t *servername;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002144 size_t servername_len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002145 struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002146 int allow_early = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002147 int i;
2148
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002149 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Willy Tarreaua8825522018-10-15 13:20:07 +02002150 s = __objt_listener(conn->target)->bind_conf;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002151
Olivier Houchard9679ac92017-10-27 14:58:08 +02002152 if (s->ssl_conf.early_data)
Olivier Houchardc2aae742017-09-22 18:26:28 +02002153 allow_early = 1;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002154#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002155 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
2156 &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002157#else
2158 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) {
2159#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002160 /*
2161 * The server_name extension was given too much extensibility when it
2162 * was written, so parsing the normal case is a bit complex.
2163 */
2164 size_t len;
2165 if (extension_len <= 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002166 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002167 /* Extract the length of the supplied list of names. */
2168 len = (*extension_data++) << 8;
2169 len |= *extension_data++;
2170 if (len + 2 != extension_len)
2171 goto abort;
2172 /*
2173 * The list in practice only has a single element, so we only consider
2174 * the first one.
2175 */
2176 if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name)
2177 goto abort;
2178 extension_len = len - 1;
2179 /* Now we can finally pull out the byte array with the actual hostname. */
2180 if (extension_len <= 2)
2181 goto abort;
2182 len = (*extension_data++) << 8;
2183 len |= *extension_data++;
2184 if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name
2185 || memchr(extension_data, 0, len) != NULL)
2186 goto abort;
2187 servername = extension_data;
2188 servername_len = len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002189 } else {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002190#if (!defined SSL_NO_GENERATE_CERTIFICATES)
2191 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002192 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002193 }
2194#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002195 /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002196 if (!s->strict_sni) {
William Lallemand21724f02019-11-04 17:56:13 +01002197 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002198 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002199 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchardc2aae742017-09-22 18:26:28 +02002200 goto allow_early;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002201 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002202 goto abort;
2203 }
2204
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05002205 /* extract/check clientHello information */
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002206#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002207 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002208#else
2209 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
2210#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002211 uint8_t sign;
2212 size_t len;
2213 if (extension_len < 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002214 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002215 len = (*extension_data++) << 8;
2216 len |= *extension_data++;
2217 if (len + 2 != extension_len)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002218 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002219 if (len % 2 != 0)
2220 goto abort;
2221 for (; len > 0; len -= 2) {
2222 extension_data++; /* hash */
2223 sign = *extension_data++;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002224 switch (sign) {
2225 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002226 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002227 break;
2228 case TLSEXT_signature_ecdsa:
2229 has_ecdsa_sig = 1;
2230 break;
2231 default:
2232 continue;
2233 }
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002234 if (has_ecdsa_sig && has_rsa_sig)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002235 break;
2236 }
2237 } else {
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002238 /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002239 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002240 }
2241 if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002242 const SSL_CIPHER *cipher;
2243 size_t len;
2244 const uint8_t *cipher_suites;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002245 has_ecdsa_sig = 0;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002246#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002247 len = ctx->cipher_suites_len;
2248 cipher_suites = ctx->cipher_suites;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002249#else
2250 len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites);
2251#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002252 if (len % 2 != 0)
2253 goto abort;
2254 for (; len != 0; len -= 2, cipher_suites += 2) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002255#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002256 uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002257 cipher = SSL_get_cipher_by_value(cipher_suite);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002258#else
2259 cipher = SSL_CIPHER_find(ssl, cipher_suites);
2260#endif
Emmanuel Hocdet019f9b12017-10-02 17:12:06 +02002261 if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) {
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002262 has_ecdsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002263 break;
2264 }
2265 }
2266 }
2267
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002268 for (i = 0; i < trash.size && i < servername_len; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002269 trash.area[i] = tolower(servername[i]);
2270 if (!wildp && (trash.area[i] == '.'))
2271 wildp = &trash.area[i];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002272 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002273 trash.area[i] = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002274
William Lallemand150bfa82019-09-19 17:12:49 +02002275 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002276
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002277 for (i = 0; i < 2; i++) {
2278 if (i == 0) /* lookup in full qualified names */
2279 node = ebst_lookup(&s->sni_ctx, trash.area);
2280 else if (i == 1 && wildp) /* lookup in wildcards names */
2281 node = ebst_lookup(&s->sni_w_ctx, wildp);
2282 else
2283 break;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002284 for (n = node; n; n = ebmb_next_dup(n)) {
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002285 /* lookup a not neg filter */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002286 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002287 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002288 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002289 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002290 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002291 break;
2292 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002293 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002294 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002295 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002296 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002297 if (!node_anonymous)
2298 node_anonymous = n;
2299 break;
2300 }
2301 }
2302 }
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002303 /* select by key_signature priority order */
2304 node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa
2305 : ((has_rsa_sig && node_rsa) ? node_rsa
2306 : (node_anonymous ? node_anonymous
2307 : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */
2308 : node_rsa /* no rsa signature case (far far away) */
2309 )));
2310 if (node) {
2311 /* switch ctx */
2312 struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf;
2313 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002314 if (conf) {
2315 methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN);
2316 methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX);
2317 if (conf->early_data)
2318 allow_early = 1;
2319 }
William Lallemand02010472019-10-18 11:02:19 +02002320 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002321 goto allow_early;
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002322 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002323 }
William Lallemand150bfa82019-09-19 17:12:49 +02002324
William Lallemand02010472019-10-18 11:02:19 +02002325 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002326#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002327 if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002328 /* switch ctx done in ssl_sock_generate_certificate */
Olivier Houchardc2aae742017-09-22 18:26:28 +02002329 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002330 }
2331#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002332 if (!s->strict_sni) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002333 /* no certificate match, is the default_ctx */
William Lallemand21724f02019-11-04 17:56:13 +01002334 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002335 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002336 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002337 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02002338allow_early:
2339#ifdef OPENSSL_IS_BORINGSSL
2340 if (allow_early)
2341 SSL_set_early_data_enabled(ssl, 1);
2342#else
2343 if (!allow_early)
2344 SSL_set_max_early_data(ssl, 0);
2345#endif
2346 return 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002347 abort:
2348 /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */
2349 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002350#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002351 return ssl_select_cert_error;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002352#else
2353 *al = SSL_AD_UNRECOGNIZED_NAME;
2354 return 0;
2355#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002356}
2357
2358#else /* OPENSSL_IS_BORINGSSL */
2359
Emeric Brunfc0421f2012-09-07 17:30:07 +02002360/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
2361 * warning when no match is found, which implies the default (first) cert
2362 * will keep being used.
2363 */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002364static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv)
Emeric Brunfc0421f2012-09-07 17:30:07 +02002365{
2366 const char *servername;
2367 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002368 struct ebmb_node *node, *n;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002369 struct bind_conf *s = priv;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002370 int i;
2371 (void)al; /* shut gcc stupid warning */
2372
2373 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002374 if (!servername) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002375#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002376 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl))
2377 return SSL_TLSEXT_ERR_OK;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002378#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002379 if (s->strict_sni)
2380 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002381 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002382 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002383 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002384 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002385 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02002386
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002387 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02002388 if (!servername[i])
2389 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002390 trash.area[i] = tolower(servername[i]);
2391 if (!wildp && (trash.area[i] == '.'))
2392 wildp = &trash.area[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +02002393 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002394 trash.area[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002395
William Lallemand150bfa82019-09-19 17:12:49 +02002396 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002397 node = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002398 /* lookup in full qualified names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002399 for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) {
2400 /* lookup a not neg filter */
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002401 if (!container_of(n, struct sni_ctx, name)->neg) {
2402 node = n;
2403 break;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002404 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002405 }
2406 if (!node && wildp) {
2407 /* lookup in wildcards names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002408 for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) {
2409 /* lookup a not neg filter */
2410 if (!container_of(n, struct sni_ctx, name)->neg) {
2411 node = n;
2412 break;
2413 }
2414 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002415 }
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002416 if (!node) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002417#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002418 if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) {
2419 /* switch ctx done in ssl_sock_generate_certificate */
William Lallemand150bfa82019-09-19 17:12:49 +02002420 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002421 return SSL_TLSEXT_ERR_OK;
2422 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002423#endif
William Lallemand21724f02019-11-04 17:56:13 +01002424 if (s->strict_sni) {
2425 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002426 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002427 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002428 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002429 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002430 return SSL_TLSEXT_ERR_OK;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002431 }
2432
2433 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002434 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
William Lallemand150bfa82019-09-19 17:12:49 +02002435 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002436 return SSL_TLSEXT_ERR_OK;
2437}
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002438#endif /* (!) OPENSSL_IS_BORINGSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002439#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2440
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002441#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002442
2443static DH * ssl_get_dh_1024(void)
2444{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002445 static unsigned char dh1024_p[]={
2446 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7,
2447 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3,
2448 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9,
2449 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96,
2450 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D,
2451 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17,
2452 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B,
2453 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94,
2454 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC,
2455 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E,
2456 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B,
2457 };
2458 static unsigned char dh1024_g[]={
2459 0x02,
2460 };
2461
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002462 BIGNUM *p;
2463 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002464 DH *dh = DH_new();
2465 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002466 p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL);
2467 g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002468
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002469 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002470 DH_free(dh);
2471 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002472 } else {
2473 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002474 }
2475 }
2476 return dh;
2477}
2478
2479static DH *ssl_get_dh_2048(void)
2480{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002481 static unsigned char dh2048_p[]={
2482 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59,
2483 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24,
2484 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66,
2485 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10,
2486 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B,
2487 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23,
2488 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC,
2489 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E,
2490 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77,
2491 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A,
2492 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66,
2493 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74,
2494 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E,
2495 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40,
2496 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93,
2497 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43,
2498 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88,
2499 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1,
2500 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17,
2501 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB,
2502 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41,
2503 0xB7,0x1F,0x77,0xF3,
2504 };
2505 static unsigned char dh2048_g[]={
2506 0x02,
2507 };
2508
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002509 BIGNUM *p;
2510 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002511 DH *dh = DH_new();
2512 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002513 p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL);
2514 g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002515
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002516 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002517 DH_free(dh);
2518 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002519 } else {
2520 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002521 }
2522 }
2523 return dh;
2524}
2525
2526static DH *ssl_get_dh_4096(void)
2527{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002528 static unsigned char dh4096_p[]={
2529 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11,
2530 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68,
2531 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD,
2532 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B,
2533 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B,
2534 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47,
2535 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15,
2536 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35,
2537 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79,
2538 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3,
2539 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC,
2540 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52,
2541 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C,
2542 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A,
2543 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41,
2544 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55,
2545 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52,
2546 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66,
2547 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E,
2548 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47,
2549 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2,
2550 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73,
2551 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13,
2552 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10,
2553 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14,
2554 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD,
2555 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E,
2556 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48,
2557 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01,
2558 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60,
2559 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC,
2560 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41,
2561 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8,
2562 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B,
2563 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23,
2564 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE,
2565 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD,
2566 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03,
2567 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08,
2568 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5,
2569 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F,
2570 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67,
2571 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B,
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002572 };
Remi Gacogned3a341a2015-05-29 16:26:17 +02002573 static unsigned char dh4096_g[]={
2574 0x02,
2575 };
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002576
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002577 BIGNUM *p;
2578 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002579 DH *dh = DH_new();
2580 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002581 p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL);
2582 g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002583
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002584 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002585 DH_free(dh);
2586 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002587 } else {
2588 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002589 }
2590 }
2591 return dh;
2592}
2593
2594/* Returns Diffie-Hellman parameters matching the private key length
Willy Tarreauef934602016-12-22 23:12:01 +01002595 but not exceeding global_ssl.default_dh_param */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002596static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen)
2597{
2598 DH *dh = NULL;
2599 EVP_PKEY *pkey = SSL_get_privatekey(ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002600 int type;
2601
2602 type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002603
2604 /* The keylen supplied by OpenSSL can only be 512 or 1024.
2605 See ssl3_send_server_key_exchange() in ssl/s3_srvr.c
2606 */
2607 if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) {
2608 keylen = EVP_PKEY_bits(pkey);
2609 }
2610
Willy Tarreauef934602016-12-22 23:12:01 +01002611 if (keylen > global_ssl.default_dh_param) {
2612 keylen = global_ssl.default_dh_param;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002613 }
2614
Remi Gacogned3a341a2015-05-29 16:26:17 +02002615 if (keylen >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002616 dh = local_dh_4096;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002617 }
2618 else if (keylen >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002619 dh = local_dh_2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002620 }
2621 else {
Remi Gacogne8de54152014-07-15 11:36:40 +02002622 dh = local_dh_1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002623 }
2624
2625 return dh;
2626}
2627
Remi Gacogne47783ef2015-05-29 15:53:22 +02002628static DH * ssl_sock_get_dh_from_file(const char *filename)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002629{
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002630 DH *dh = NULL;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002631 BIO *in = BIO_new(BIO_s_file());
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002632
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002633 if (in == NULL)
2634 goto end;
2635
Remi Gacogne47783ef2015-05-29 15:53:22 +02002636 if (BIO_read_filename(in, filename) <= 0)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002637 goto end;
2638
Remi Gacogne47783ef2015-05-29 15:53:22 +02002639 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
2640
2641end:
2642 if (in)
2643 BIO_free(in);
2644
Emeric Brune1b4ed42018-08-16 15:14:12 +02002645 ERR_clear_error();
2646
Remi Gacogne47783ef2015-05-29 15:53:22 +02002647 return dh;
2648}
2649
2650int ssl_sock_load_global_dh_param_from_file(const char *filename)
2651{
2652 global_dh = ssl_sock_get_dh_from_file(filename);
2653
2654 if (global_dh) {
2655 return 0;
2656 }
2657
2658 return -1;
2659}
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002660#endif
2661
William Lallemand9117de92019-10-04 00:29:42 +02002662/* This function allocates a sni_ctx and adds it to the ckch_inst */
William Lallemand1d29c742019-10-04 00:53:29 +02002663static int ckch_inst_add_cert_sni(SSL_CTX *ctx, struct ckch_inst *ckch_inst,
William Lallemand9117de92019-10-04 00:29:42 +02002664 struct bind_conf *s, struct ssl_bind_conf *conf,
2665 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002666{
2667 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002668 int wild = 0, neg = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002669
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002670 if (*name == '!') {
2671 neg = 1;
2672 name++;
2673 }
2674 if (*name == '*') {
2675 wild = 1;
2676 name++;
2677 }
2678 /* !* filter is a nop */
2679 if (neg && wild)
2680 return order;
2681 if (*name) {
2682 int j, len;
2683 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002684 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002685 trash.area[j] = tolower(name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002686 if (j >= trash.size)
William Lallemandfe49bb32019-10-03 23:46:33 +02002687 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002688 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002689
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002690 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02002691 if (!sc)
William Lallemandfe49bb32019-10-03 23:46:33 +02002692 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002693 memcpy(sc->name.key, trash.area, len + 1);
William Lallemand02e19a52020-04-08 16:11:26 +02002694 SSL_CTX_up_ref(ctx);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002695 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002696 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002697 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002698 sc->order = order++;
2699 sc->neg = neg;
William Lallemand1d29c742019-10-04 00:53:29 +02002700 sc->wild = wild;
2701 sc->name.node.leaf_p = NULL;
William Lallemandcfca1422020-03-05 10:17:47 +01002702 sc->ckch_inst = ckch_inst;
William Lallemand1d29c742019-10-04 00:53:29 +02002703 LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002704 }
2705 return order;
2706}
2707
William Lallemand6af03992019-07-23 15:00:54 +02002708/*
William Lallemand1d29c742019-10-04 00:53:29 +02002709 * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree
2710 * This function can't return an error.
2711 *
2712 * *CAUTION*: The caller must lock the sni tree if called in multithreading mode
2713 */
William Lallemandc756bbd2020-05-13 17:23:59 +02002714void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf)
William Lallemand1d29c742019-10-04 00:53:29 +02002715{
2716
2717 struct sni_ctx *sc0, *sc0b, *sc1;
2718 struct ebmb_node *node;
William Lallemand21724f02019-11-04 17:56:13 +01002719 int def = 0;
William Lallemand1d29c742019-10-04 00:53:29 +02002720
2721 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
2722
2723 /* ignore if sc0 was already inserted in a tree */
2724 if (sc0->name.node.leaf_p)
2725 continue;
2726
2727 /* Check for duplicates. */
2728 if (sc0->wild)
2729 node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key);
2730 else
2731 node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key);
2732
2733 for (; node; node = ebmb_next_dup(node)) {
2734 sc1 = ebmb_entry(node, struct sni_ctx, name);
2735 if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf
2736 && sc1->neg == sc0->neg && sc1->wild == sc0->wild) {
2737 /* it's a duplicate, we should remove and free it */
2738 LIST_DEL(&sc0->by_ckch_inst);
William Lallemand02e19a52020-04-08 16:11:26 +02002739 SSL_CTX_free(sc0->ctx);
William Lallemand1d29c742019-10-04 00:53:29 +02002740 free(sc0);
2741 sc0 = NULL;
William Lallemande15029b2019-10-14 10:46:58 +02002742 break;
William Lallemand1d29c742019-10-04 00:53:29 +02002743 }
2744 }
2745
2746 /* if duplicate, ignore the insertion */
2747 if (!sc0)
2748 continue;
2749
2750 if (sc0->wild)
2751 ebst_insert(&bind_conf->sni_w_ctx, &sc0->name);
2752 else
2753 ebst_insert(&bind_conf->sni_ctx, &sc0->name);
William Lallemand21724f02019-11-04 17:56:13 +01002754
2755 /* replace the default_ctx if required with the first ctx */
2756 if (ckch_inst->is_default && !def) {
William Lallemand02e19a52020-04-08 16:11:26 +02002757 SSL_CTX_free(bind_conf->default_ctx);
2758 SSL_CTX_up_ref(sc0->ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002759 bind_conf->default_ctx = sc0->ctx;
2760 def = 1;
2761 }
William Lallemand1d29c742019-10-04 00:53:29 +02002762 }
2763}
2764
2765/*
William Lallemande3af8fb2019-10-08 11:36:53 +02002766 * tree used to store the ckchs ordered by filename/bundle name
William Lallemand6af03992019-07-23 15:00:54 +02002767 */
William Lallemande3af8fb2019-10-08 11:36:53 +02002768struct eb_root ckchs_tree = EB_ROOT_UNIQUE;
William Lallemand6af03992019-07-23 15:00:54 +02002769
William Lallemand2954c472020-03-06 21:54:13 +01002770/* tree of crtlist (crt-list/directory) */
William Lallemandc756bbd2020-05-13 17:23:59 +02002771struct eb_root crtlists_tree = EB_ROOT_UNIQUE;
William Lallemandfa892222019-07-23 16:06:08 +02002772
Emeric Brun7a883362019-10-17 13:27:40 +02002773/* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX.
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05002774 * If there is no DH parameter available in the ckchs, the global
Emeric Brun7a883362019-10-17 13:27:40 +02002775 * DH parameter is loaded into the SSL_CTX and if there is no
2776 * DH parameter available in ckchs nor in global, the default
2777 * DH parameters are applied on the SSL_CTX.
2778 * Returns a bitfield containing the flags:
2779 * ERR_FATAL in any fatal error case
2780 * ERR_ALERT if a reason of the error is availabine in err
2781 * ERR_WARN if a warning is available into err
2782 * The value 0 means there is no error nor warning and
2783 * the operation succeed.
2784 */
William Lallemandfa892222019-07-23 16:06:08 +02002785#ifndef OPENSSL_NO_DH
Emeric Brun7a883362019-10-17 13:27:40 +02002786static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch,
2787 const char *path, char **err)
William Lallemandfa892222019-07-23 16:06:08 +02002788{
Emeric Brun7a883362019-10-17 13:27:40 +02002789 int ret = 0;
William Lallemandfa892222019-07-23 16:06:08 +02002790 DH *dh = NULL;
2791
William Lallemanda8c73742019-07-31 18:31:34 +02002792 if (ckch && ckch->dh) {
William Lallemandfa892222019-07-23 16:06:08 +02002793 dh = ckch->dh;
Emeric Bruna9363eb2019-10-17 14:53:03 +02002794 if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
2795 memprintf(err, "%sunable to load the DH parameter specified in '%s'",
2796 err && *err ? *err : "", path);
2797#if defined(SSL_CTX_set_dh_auto)
2798 SSL_CTX_set_dh_auto(ctx, 1);
2799 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
2800 err && *err ? *err : "");
2801#else
2802 memprintf(err, "%s, DH ciphers won't be available.\n",
2803 err && *err ? *err : "");
2804#endif
2805 ret |= ERR_WARN;
2806 goto end;
2807 }
William Lallemandfa892222019-07-23 16:06:08 +02002808
2809 if (ssl_dh_ptr_index >= 0) {
2810 /* store a pointer to the DH params to avoid complaining about
2811 ssl-default-dh-param not being set for this SSL_CTX */
2812 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
2813 }
2814 }
2815 else if (global_dh) {
Emeric Bruna9363eb2019-10-17 14:53:03 +02002816 if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) {
2817 memprintf(err, "%sunable to use the global DH parameter for certificate '%s'",
2818 err && *err ? *err : "", path);
2819#if defined(SSL_CTX_set_dh_auto)
2820 SSL_CTX_set_dh_auto(ctx, 1);
2821 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
2822 err && *err ? *err : "");
2823#else
2824 memprintf(err, "%s, DH ciphers won't be available.\n",
2825 err && *err ? *err : "");
2826#endif
2827 ret |= ERR_WARN;
2828 goto end;
2829 }
William Lallemandfa892222019-07-23 16:06:08 +02002830 }
2831 else {
2832 /* Clear openssl global errors stack */
2833 ERR_clear_error();
2834
2835 if (global_ssl.default_dh_param <= 1024) {
2836 /* we are limited to DH parameter of 1024 bits anyway */
2837 if (local_dh_1024 == NULL)
2838 local_dh_1024 = ssl_get_dh_1024();
2839
Emeric Brun7a883362019-10-17 13:27:40 +02002840 if (local_dh_1024 == NULL) {
2841 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
2842 err && *err ? *err : "", path);
2843 ret |= ERR_ALERT | ERR_FATAL;
William Lallemandfa892222019-07-23 16:06:08 +02002844 goto end;
Emeric Brun7a883362019-10-17 13:27:40 +02002845 }
William Lallemandfa892222019-07-23 16:06:08 +02002846
Emeric Bruna9363eb2019-10-17 14:53:03 +02002847 if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) {
2848 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
2849 err && *err ? *err : "", path);
2850#if defined(SSL_CTX_set_dh_auto)
2851 SSL_CTX_set_dh_auto(ctx, 1);
2852 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
2853 err && *err ? *err : "");
2854#else
2855 memprintf(err, "%s, DH ciphers won't be available.\n",
2856 err && *err ? *err : "");
2857#endif
2858 ret |= ERR_WARN;
2859 goto end;
2860 }
William Lallemandfa892222019-07-23 16:06:08 +02002861 }
2862 else {
2863 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
2864 }
William Lallemand8d0f8932019-10-17 18:03:58 +02002865 }
2866
William Lallemandf9568fc2019-10-16 18:27:58 +02002867end:
William Lallemandf9568fc2019-10-16 18:27:58 +02002868 ERR_clear_error();
William Lallemandf9568fc2019-10-16 18:27:58 +02002869 return ret;
2870}
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02002871#endif
William Lallemandfa892222019-07-23 16:06:08 +02002872
yanbzhu488a4d22015-12-01 15:16:07 -05002873/* Loads the info in ckch into ctx
Emeric Bruna96b5822019-10-17 13:25:14 +02002874 * Returns a bitfield containing the flags:
2875 * ERR_FATAL in any fatal error case
2876 * ERR_ALERT if the reason of the error is available in err
2877 * ERR_WARN if a warning is available into err
2878 * The value 0 means there is no error nor warning and
2879 * the operation succeed.
yanbzhu488a4d22015-12-01 15:16:07 -05002880 */
2881static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
2882{
Emeric Bruna96b5822019-10-17 13:25:14 +02002883 int errcode = 0;
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01002884 STACK_OF(X509) *find_chain = NULL;
Emeric Bruna96b5822019-10-17 13:25:14 +02002885
yanbzhu488a4d22015-12-01 15:16:07 -05002886 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
2887 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
2888 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02002889 errcode |= ERR_ALERT | ERR_FATAL;
2890 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05002891 }
2892
2893 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
2894 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
2895 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02002896 errcode |= ERR_ALERT | ERR_FATAL;
2897 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05002898 }
2899
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01002900 if (ckch->chain) {
2901 find_chain = ckch->chain;
2902 } else {
2903 /* Find Certificate Chain in global */
2904 struct issuer_chain *issuer;
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +01002905 issuer = ssl_get0_issuer_chain(ckch->cert);
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01002906 if (issuer)
2907 find_chain = issuer->chain;
2908 }
William Lallemand85888572020-02-27 14:48:35 +01002909
William Lallemandf187ce62020-06-02 18:27:20 +02002910 /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */
2911 if (find_chain)
2912#ifdef SSL_CTX_set1_chain
2913 if (!SSL_CTX_set1_chain(ctx, find_chain)) {
2914 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n",
2915 err && *err ? *err : "", path);
2916 errcode |= ERR_ALERT | ERR_FATAL;
2917 goto end;
2918 }
2919#else
2920 { /* legacy compat (< openssl 1.0.2) */
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01002921 X509 *ca;
William Lallemandf187ce62020-06-02 18:27:20 +02002922 STACK_OF(X509) *chain;
2923 chain = X509_chain_up_ref(find_chain);
2924 while ((ca = sk_X509_shift(chain)))
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01002925 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01002926 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
2927 err && *err ? *err : "", path);
William Lallemandf187ce62020-06-02 18:27:20 +02002928 X509_free(ca);
2929 sk_X509_pop_free(chain, X509_free);
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01002930 errcode |= ERR_ALERT | ERR_FATAL;
2931 goto end;
2932 }
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01002933 }
William Lallemandf187ce62020-06-02 18:27:20 +02002934#endif
yanbzhu488a4d22015-12-01 15:16:07 -05002935
William Lallemandfa892222019-07-23 16:06:08 +02002936#ifndef OPENSSL_NO_DH
2937 /* store a NULL pointer to indicate we have not yet loaded
2938 a custom DH param file */
2939 if (ssl_dh_ptr_index >= 0) {
2940 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
2941 }
2942
Emeric Brun7a883362019-10-17 13:27:40 +02002943 errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err);
2944 if (errcode & ERR_CODE) {
William Lallemandfa892222019-07-23 16:06:08 +02002945 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
2946 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02002947 goto end;
William Lallemandfa892222019-07-23 16:06:08 +02002948 }
2949#endif
2950
William Lallemanda17f4112019-10-10 15:16:44 +02002951#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
2952 if (sctl_ex_index >= 0 && ckch->sctl) {
2953 if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) {
2954 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01002955 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02002956 errcode |= ERR_ALERT | ERR_FATAL;
2957 goto end;
William Lallemanda17f4112019-10-10 15:16:44 +02002958 }
2959 }
2960#endif
2961
William Lallemand4a660132019-10-14 14:51:41 +02002962#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand246c0242019-10-11 08:59:13 +02002963 /* Load OCSP Info into context */
2964 if (ckch->ocsp_response) {
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01002965 if (ssl_sock_load_ocsp(ctx, ckch, find_chain) < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01002966 memprintf(err, "%s '%s.ocsp' is present and activates OCSP but it is impossible to compute the OCSP certificate ID (maybe the issuer could not be found)'.\n",
2967 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02002968 errcode |= ERR_ALERT | ERR_FATAL;
2969 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02002970 }
2971 }
William Lallemand246c0242019-10-11 08:59:13 +02002972#endif
2973
Emeric Bruna96b5822019-10-17 13:25:14 +02002974 end:
2975 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05002976}
2977
William Lallemandc4ecddf2019-07-31 16:50:08 +02002978#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu08ce6ab2015-12-02 13:01:29 -05002979
William Lallemand28a8fce2019-10-04 17:36:55 +02002980static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
yanbzhu08ce6ab2015-12-02 13:01:29 -05002981{
2982 struct sni_keytype *s_kt = NULL;
2983 struct ebmb_node *node;
2984 int i;
2985
2986 for (i = 0; i < trash.size; i++) {
2987 if (!str[i])
2988 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002989 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05002990 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002991 trash.area[i] = 0;
2992 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05002993 if (!node) {
2994 /* CN not found in tree */
2995 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
2996 /* Using memcpy here instead of strncpy.
2997 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
2998 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
2999 */
William Lallemand28a8fce2019-10-04 17:36:55 +02003000 if (!s_kt)
3001 return -1;
3002
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003003 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003004 s_kt->keytypes = 0;
3005 ebst_insert(sni_keytypes, &s_kt->name);
3006 } else {
3007 /* CN found in tree */
3008 s_kt = container_of(node, struct sni_keytype, name);
3009 }
3010
3011 /* Mark that this CN has the keytype of key_index via keytypes mask */
3012 s_kt->keytypes |= 1<<key_index;
3013
William Lallemand28a8fce2019-10-04 17:36:55 +02003014 return 0;
3015
William Lallemand6af03992019-07-23 15:00:54 +02003016}
3017
William Lallemandc4ecddf2019-07-31 16:50:08 +02003018#endif
William Lallemand36b84632019-07-18 19:28:17 +02003019
William Lallemandc4ecddf2019-07-31 16:50:08 +02003020#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3021
William Lallemand36b84632019-07-18 19:28:17 +02003022/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003023 * Take a ckch_store which contains a multi-certificate bundle.
William Lallemand36b84632019-07-18 19:28:17 +02003024 * Group these certificates into a set of SSL_CTX*
yanbzhu08ce6ab2015-12-02 13:01:29 -05003025 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
3026 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08003027 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05003028 *
Emeric Brun054563d2019-10-17 13:16:58 +02003029 * Returns a bitfield containing the flags:
3030 * ERR_FATAL in any fatal error case
3031 * ERR_ALERT if the reason of the error is available in err
3032 * ERR_WARN if a warning is available into err
William Lallemand36b84632019-07-18 19:28:17 +02003033 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05003034 */
William Lallemandda8584c2020-05-14 10:14:37 +02003035int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
3036 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3037 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003038{
William Lallemand36b84632019-07-18 19:28:17 +02003039 int i = 0, n = 0;
3040 struct cert_key_and_chain *certs_and_keys;
William Lallemand4b989f22019-10-04 18:36:55 +02003041 struct eb_root sni_keytypes_map = EB_ROOT;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003042 struct ebmb_node *node;
3043 struct ebmb_node *next;
3044 /* Array of SSL_CTX pointers corresponding to each possible combo
3045 * of keytypes
3046 */
3047 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Emeric Brun054563d2019-10-17 13:16:58 +02003048 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003049 X509_NAME *xname = NULL;
3050 char *str = NULL;
3051#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3052 STACK_OF(GENERAL_NAME) *names = NULL;
3053#endif
William Lallemand614ca0d2019-10-07 13:52:11 +02003054 struct ckch_inst *ckch_inst;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003055
Emeric Brun054563d2019-10-17 13:16:58 +02003056 *ckchi = NULL;
3057
William Lallemande3af8fb2019-10-08 11:36:53 +02003058 if (!ckchs || !ckchs->ckch || !ckchs->multi) {
William Lallemand36b84632019-07-18 19:28:17 +02003059 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3060 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003061 return ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003062 }
3063
3064 ckch_inst = ckch_inst_new();
3065 if (!ckch_inst) {
3066 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3067 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003068 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003069 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003070 }
3071
William Lallemande3af8fb2019-10-08 11:36:53 +02003072 certs_and_keys = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02003073
yanbzhu08ce6ab2015-12-02 13:01:29 -05003074 /* Process each ckch and update keytypes for each CN/SAN
3075 * for example, if CN/SAN www.a.com is associated with
3076 * certs with keytype 0 and 2, then at the end of the loop,
3077 * www.a.com will have:
3078 * keyindex = 0 | 1 | 4 = 5
3079 */
3080 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003081 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003082
3083 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
3084 continue;
3085
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003086 if (fcount) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003087 for (i = 0; i < fcount; i++) {
3088 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
3089 if (ret < 0) {
3090 memprintf(err, "%sunable to allocate SSL context.\n",
3091 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003092 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003093 goto end;
3094 }
3095 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003096 } else {
3097 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
3098 * so the line that contains logic is marked via comments
3099 */
3100 xname = X509_get_subject_name(certs_and_keys[n].cert);
3101 i = -1;
3102 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3103 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003104 ASN1_STRING *value;
3105 value = X509_NAME_ENTRY_get_data(entry);
3106 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003107 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02003108 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003109
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003110 OPENSSL_free(str);
3111 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003112 if (ret < 0) {
3113 memprintf(err, "%sunable to allocate SSL context.\n",
3114 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003115 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003116 goto end;
3117 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003118 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003119 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003120
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003121 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003122#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003123 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
3124 if (names) {
3125 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3126 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003127
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003128 if (name->type == GEN_DNS) {
3129 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
3130 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02003131 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003132
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003133 OPENSSL_free(str);
3134 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003135 if (ret < 0) {
3136 memprintf(err, "%sunable to allocate SSL context.\n",
3137 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003138 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003139 goto end;
3140 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003141 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003142 }
3143 }
3144 }
3145 }
3146#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
3147 }
3148
3149 /* If no files found, return error */
3150 if (eb_is_empty(&sni_keytypes_map)) {
3151 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3152 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003153 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003154 goto end;
3155 }
3156
3157 /* We now have a map of CN/SAN to keytypes that are loaded in
3158 * Iterate through the map to create the SSL_CTX's (if needed)
3159 * and add each CTX to the SNI tree
3160 *
3161 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08003162 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05003163 * combination is denoted by the key in the map. Each key
3164 * has a value between 1 and 2^n - 1. Conveniently, the array
3165 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
3166 * entry in the array to correspond to the unique combo (key)
3167 * associated with i. This unique key combo (i) will be associated
3168 * with combos[i-1]
3169 */
3170
3171 node = ebmb_first(&sni_keytypes_map);
3172 while (node) {
3173 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00003174 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003175 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05003176
3177 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
3178 i = container_of(node, struct sni_keytype, name)->keytypes;
3179 cur_ctx = key_combos[i-1].ctx;
3180
3181 if (cur_ctx == NULL) {
3182 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003183 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05003184 if (cur_ctx == NULL) {
3185 memprintf(err, "%sunable to allocate SSL context.\n",
3186 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003187 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003188 goto end;
3189 }
3190
yanbzhube2774d2015-12-10 15:07:30 -05003191 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003192 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3193 if (i & (1<<n)) {
3194 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00003195 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Bruna96b5822019-10-17 13:25:14 +02003196 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
3197 if (errcode & ERR_CODE)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003198 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003199 }
3200 }
3201
yanbzhu08ce6ab2015-12-02 13:01:29 -05003202 /* Update key_combos */
3203 key_combos[i-1].ctx = cur_ctx;
3204 }
3205
3206 /* Update SNI Tree */
William Lallemand9117de92019-10-04 00:29:42 +02003207
William Lallemand1d29c742019-10-04 00:53:29 +02003208 key_combos[i-1].order = ckch_inst_add_cert_sni(cur_ctx, ckch_inst, bind_conf, ssl_conf,
William Lallemandfe49bb32019-10-03 23:46:33 +02003209 kinfo, str, key_combos[i-1].order);
3210 if (key_combos[i-1].order < 0) {
3211 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003212 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandfe49bb32019-10-03 23:46:33 +02003213 goto end;
3214 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003215 node = ebmb_next(node);
3216 }
3217
3218
3219 /* Mark a default context if none exists, using the ctx that has the most shared keys */
3220 if (!bind_conf->default_ctx) {
3221 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
3222 if (key_combos[i].ctx) {
3223 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003224 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01003225 ckch_inst->is_default = 1;
William Lallemand02e19a52020-04-08 16:11:26 +02003226 SSL_CTX_up_ref(bind_conf->default_ctx);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003227 break;
3228 }
3229 }
3230 }
3231
William Lallemand614ca0d2019-10-07 13:52:11 +02003232 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02003233 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01003234 ckch_inst->ckch_store = ckchs;
William Lallemand02e19a52020-04-08 16:11:26 +02003235
yanbzhu08ce6ab2015-12-02 13:01:29 -05003236end:
3237
3238 if (names)
3239 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
3240
yanbzhu08ce6ab2015-12-02 13:01:29 -05003241 node = ebmb_first(&sni_keytypes_map);
3242 while (node) {
3243 next = ebmb_next(node);
3244 ebmb_delete(node);
William Lallemand8ed5b962019-10-04 17:24:39 +02003245 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05003246 node = next;
3247 }
3248
William Lallemand02e19a52020-04-08 16:11:26 +02003249 /* we need to free the ctx since we incremented the refcount where it's used */
3250 for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) {
3251 if (key_combos[i].ctx)
3252 SSL_CTX_free(key_combos[i].ctx);
3253 }
3254
Emeric Brun054563d2019-10-17 13:16:58 +02003255 if (errcode & ERR_CODE && ckch_inst) {
William Lallemand02e19a52020-04-08 16:11:26 +02003256 if (ckch_inst->is_default) {
3257 SSL_CTX_free(bind_conf->default_ctx);
3258 bind_conf->default_ctx = NULL;
3259 }
3260
William Lallemandd9d5d1b2020-04-09 16:31:05 +02003261 ckch_inst_free(ckch_inst);
William Lallemand614ca0d2019-10-07 13:52:11 +02003262 ckch_inst = NULL;
William Lallemand0c6d12f2019-10-04 18:38:51 +02003263 }
3264
Emeric Brun054563d2019-10-17 13:16:58 +02003265 *ckchi = ckch_inst;
3266 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003267}
3268#else
3269/* This is a dummy, that just logs an error and returns error */
William Lallemandda8584c2020-05-14 10:14:37 +02003270int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
3271 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3272 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003273{
3274 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
3275 err && *err ? *err : "", path, strerror(errno));
Emeric Brun054563d2019-10-17 13:16:58 +02003276 return ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003277}
3278
Willy Tarreau9a1ab082019-05-09 13:26:41 +02003279#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05003280
William Lallemand614ca0d2019-10-07 13:52:11 +02003281/*
3282 * This function allocate a ckch_inst and create its snis
Emeric Brun054563d2019-10-17 13:16:58 +02003283 *
3284 * Returns a bitfield containing the flags:
3285 * ERR_FATAL in any fatal error case
3286 * ERR_ALERT if the reason of the error is available in err
3287 * ERR_WARN if a warning is available into err
William Lallemand614ca0d2019-10-07 13:52:11 +02003288 */
William Lallemandc756bbd2020-05-13 17:23:59 +02003289int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf,
Emeric Brun054563d2019-10-17 13:16:58 +02003290 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003291{
William Lallemandc9402072019-05-15 15:33:54 +02003292 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02003293 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003294 int order = 0;
3295 X509_NAME *xname;
3296 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003297 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003298 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02003299#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3300 STACK_OF(GENERAL_NAME) *names;
3301#endif
William Lallemand36b84632019-07-18 19:28:17 +02003302 struct cert_key_and_chain *ckch;
William Lallemand614ca0d2019-10-07 13:52:11 +02003303 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02003304 int errcode = 0;
3305
3306 *ckchi = NULL;
William Lallemanda59191b2019-05-15 16:08:56 +02003307
William Lallemande3af8fb2019-10-08 11:36:53 +02003308 if (!ckchs || !ckchs->ckch)
Emeric Brun054563d2019-10-17 13:16:58 +02003309 return ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003310
William Lallemande3af8fb2019-10-08 11:36:53 +02003311 ckch = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02003312
William Lallemandc9402072019-05-15 15:33:54 +02003313 ctx = SSL_CTX_new(SSLv23_server_method());
3314 if (!ctx) {
3315 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3316 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003317 errcode |= ERR_ALERT | ERR_FATAL;
3318 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02003319 }
3320
Emeric Bruna96b5822019-10-17 13:25:14 +02003321 errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err);
3322 if (errcode & ERR_CODE)
William Lallemand614ca0d2019-10-07 13:52:11 +02003323 goto error;
William Lallemand614ca0d2019-10-07 13:52:11 +02003324
3325 ckch_inst = ckch_inst_new();
3326 if (!ckch_inst) {
3327 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3328 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003329 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02003330 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02003331 }
3332
William Lallemand36b84632019-07-18 19:28:17 +02003333 pkey = X509_get_pubkey(ckch->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003334 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003335 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003336 switch(EVP_PKEY_base_id(pkey)) {
3337 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003338 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003339 break;
3340 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003341 kinfo.sig = TLSEXT_signature_ecdsa;
3342 break;
3343 case EVP_PKEY_DSA:
3344 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003345 break;
3346 }
3347 EVP_PKEY_free(pkey);
3348 }
3349
Emeric Brun50bcecc2013-04-22 13:05:23 +02003350 if (fcount) {
William Lallemandfe49bb32019-10-03 23:46:33 +02003351 while (fcount--) {
William Lallemand1d29c742019-10-04 00:53:29 +02003352 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, sni_filter[fcount], order);
William Lallemandfe49bb32019-10-03 23:46:33 +02003353 if (order < 0) {
3354 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003355 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02003356 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02003357 }
3358 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003359 }
3360 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02003361#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand36b84632019-07-18 19:28:17 +02003362 names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003363 if (names) {
3364 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3365 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
3366 if (name->type == GEN_DNS) {
3367 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02003368 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003369 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02003370 if (order < 0) {
3371 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003372 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02003373 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02003374 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003375 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003376 }
3377 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003378 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003379 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003380#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand36b84632019-07-18 19:28:17 +02003381 xname = X509_get_subject_name(ckch->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003382 i = -1;
3383 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3384 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003385 ASN1_STRING *value;
3386
3387 value = X509_NAME_ENTRY_get_data(entry);
3388 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02003389 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003390 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02003391 if (order < 0) {
3392 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003393 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02003394 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02003395 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003396 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003397 }
3398 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003399 /* we must not free the SSL_CTX anymore below, since it's already in
3400 * the tree, so it will be discovered and cleaned in time.
3401 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003402
Emeric Brunfc0421f2012-09-07 17:30:07 +02003403#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003404 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003405 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
3406 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003407 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02003408 goto error;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003409 }
3410#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003411 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003412 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003413 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01003414 ckch_inst->is_default = 1;
William Lallemand02e19a52020-04-08 16:11:26 +02003415 SSL_CTX_up_ref(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003416 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003417
William Lallemand9117de92019-10-04 00:29:42 +02003418 /* everything succeed, the ckch instance can be used */
3419 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02003420 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01003421 ckch_inst->ckch_store = ckchs;
William Lallemand9117de92019-10-04 00:29:42 +02003422
William Lallemand02e19a52020-04-08 16:11:26 +02003423 SSL_CTX_free(ctx); /* we need to free the ctx since we incremented the refcount where it's used */
3424
Emeric Brun054563d2019-10-17 13:16:58 +02003425 *ckchi = ckch_inst;
3426 return errcode;
William Lallemandd9199372019-10-04 15:37:05 +02003427
3428error:
3429 /* free the allocated sni_ctxs */
William Lallemand614ca0d2019-10-07 13:52:11 +02003430 if (ckch_inst) {
William Lallemand02e19a52020-04-08 16:11:26 +02003431 if (ckch_inst->is_default)
3432 SSL_CTX_free(ctx);
3433
William Lallemandd9d5d1b2020-04-09 16:31:05 +02003434 ckch_inst_free(ckch_inst);
William Lallemand614ca0d2019-10-07 13:52:11 +02003435 ckch_inst = NULL;
William Lallemandd9199372019-10-04 15:37:05 +02003436 }
William Lallemandd9199372019-10-04 15:37:05 +02003437 SSL_CTX_free(ctx);
3438
Emeric Brun054563d2019-10-17 13:16:58 +02003439 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003440}
3441
Willy Tarreau8c5414a2019-10-16 17:06:25 +02003442/* Returns a set of ERR_* flags possibly with an error in <err>. */
William Lallemand614ca0d2019-10-07 13:52:11 +02003443static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs,
3444 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
William Lallemand24bde432020-03-09 16:48:43 +01003445 char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err)
William Lallemand614ca0d2019-10-07 13:52:11 +02003446{
Emeric Brun054563d2019-10-17 13:16:58 +02003447 int errcode = 0;
William Lallemand614ca0d2019-10-07 13:52:11 +02003448
3449 /* we found the ckchs in the tree, we can use it directly */
3450 if (ckchs->multi)
William Lallemand24bde432020-03-09 16:48:43 +01003451 errcode |= ckch_inst_new_load_multi_store(path, ckchs, bind_conf, ssl_conf, sni_filter, fcount, ckch_inst, err);
William Lallemand614ca0d2019-10-07 13:52:11 +02003452 else
William Lallemand24bde432020-03-09 16:48:43 +01003453 errcode |= ckch_inst_new_load_store(path, ckchs, bind_conf, ssl_conf, sni_filter, fcount, ckch_inst, err);
William Lallemand614ca0d2019-10-07 13:52:11 +02003454
Emeric Brun054563d2019-10-17 13:16:58 +02003455 if (errcode & ERR_CODE)
3456 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02003457
William Lallemand24bde432020-03-09 16:48:43 +01003458 ssl_sock_load_cert_sni(*ckch_inst, bind_conf);
William Lallemand614ca0d2019-10-07 13:52:11 +02003459
3460 /* succeed, add the instance to the ckch_store's list of instance */
William Lallemand24bde432020-03-09 16:48:43 +01003461 LIST_ADDQ(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs));
Emeric Brun054563d2019-10-17 13:16:58 +02003462 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02003463}
3464
William Lallemand6be66ec2020-03-06 22:26:32 +01003465
William Lallemand4c68bba2020-03-30 18:45:10 +02003466
3467
3468/* Make sure openssl opens /dev/urandom before the chroot. The work is only
3469 * done once. Zero is returned if the operation fails. No error is returned
3470 * if the random is said as not implemented, because we expect that openssl
3471 * will use another method once needed.
3472 */
3473static int ssl_initialize_random()
3474{
3475 unsigned char random;
3476 static int random_initialized = 0;
3477
3478 if (!random_initialized && RAND_bytes(&random, 1) != 0)
3479 random_initialized = 1;
3480
3481 return random_initialized;
3482}
3483
William Lallemand2954c472020-03-06 21:54:13 +01003484/* Load a crt-list file, this is done in 2 parts:
3485 * - store the content of the file in a crtlist structure with crtlist_entry structures
3486 * - generate the instances by iterating on entries in the crtlist struct
3487 *
3488 * Nothing is locked there, this function is used in the configuration parser.
3489 *
3490 * Returns a set of ERR_* flags possibly with an error in <err>.
3491 */
William Lallemand6be66ec2020-03-06 22:26:32 +01003492int ssl_sock_load_cert_list_file(char *file, int dir, struct bind_conf *bind_conf, struct proxy *curproxy, char **err)
William Lallemand2954c472020-03-06 21:54:13 +01003493{
3494 struct crtlist *crtlist = NULL;
3495 struct ebmb_node *eb;
William Lallemand49398312020-03-30 17:01:33 +02003496 struct crtlist_entry *entry = NULL;
William Lallemand79d31ec2020-03-25 15:10:49 +01003497 struct bind_conf_list *bind_conf_node = NULL;
William Lallemand2954c472020-03-06 21:54:13 +01003498 int cfgerr = 0;
William Lallemand41ca9302020-04-08 13:15:18 +02003499 char *end;
William Lallemand2954c472020-03-06 21:54:13 +01003500
William Lallemand79d31ec2020-03-25 15:10:49 +01003501 bind_conf_node = malloc(sizeof(*bind_conf_node));
3502 if (!bind_conf_node) {
3503 memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : "");
3504 cfgerr |= ERR_FATAL | ERR_ALERT;
3505 goto error;
3506 }
3507 bind_conf_node->next = NULL;
3508 bind_conf_node->bind_conf = bind_conf;
3509
William Lallemand41ca9302020-04-08 13:15:18 +02003510 /* strip trailing slashes, including first one */
3511 for (end = file + strlen(file) - 1; end >= file && *end == '/'; end--)
3512 *end = 0;
3513
William Lallemand2954c472020-03-06 21:54:13 +01003514 /* look for an existing crtlist or create one */
3515 eb = ebst_lookup(&crtlists_tree, file);
3516 if (eb) {
3517 crtlist = ebmb_entry(eb, struct crtlist, node);
3518 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01003519 /* load a crt-list OR a directory */
3520 if (dir)
3521 cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err);
3522 else
3523 cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err);
3524
William Lallemand2954c472020-03-06 21:54:13 +01003525 if (!(cfgerr & ERR_CODE))
3526 ebst_insert(&crtlists_tree, &crtlist->node);
3527 }
3528
3529 if (cfgerr & ERR_CODE) {
3530 cfgerr |= ERR_FATAL | ERR_ALERT;
3531 goto error;
3532 }
3533
3534 /* generates ckch instance from the crtlist_entry */
3535 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
3536 struct ckch_store *store;
3537 struct ckch_inst *ckch_inst = NULL;
3538
3539 store = entry->node.key;
3540 cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err);
3541 if (cfgerr & ERR_CODE) {
3542 memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err);
3543 goto error;
3544 }
William Lallemand49398312020-03-30 17:01:33 +02003545 LIST_ADDQ(&entry->ckch_inst, &ckch_inst->by_crtlist_entry);
William Lallemandcaa16192020-04-08 16:29:15 +02003546 ckch_inst->crtlist_entry = entry;
William Lallemand2954c472020-03-06 21:54:13 +01003547 }
William Lallemand2954c472020-03-06 21:54:13 +01003548
William Lallemand79d31ec2020-03-25 15:10:49 +01003549 /* add the bind_conf to the list */
3550 bind_conf_node->next = crtlist->bind_conf;
3551 crtlist->bind_conf = bind_conf_node;
3552
William Lallemand2954c472020-03-06 21:54:13 +01003553 return cfgerr;
3554error:
3555 {
William Lallemand49398312020-03-30 17:01:33 +02003556 struct crtlist_entry *lastentry;
William Lallemand2954c472020-03-06 21:54:13 +01003557 struct ckch_inst *inst, *s_inst;
3558
William Lallemand49398312020-03-30 17:01:33 +02003559 lastentry = entry; /* which entry we tried to generate last */
3560 if (lastentry) {
3561 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
3562 if (entry == lastentry) /* last entry we tried to generate, no need to go further */
3563 break;
3564
3565 list_for_each_entry_safe(inst, s_inst, &entry->ckch_inst, by_crtlist_entry) {
William Lallemand2954c472020-03-06 21:54:13 +01003566
William Lallemand49398312020-03-30 17:01:33 +02003567 /* this was not generated for this bind_conf, skip */
3568 if (inst->bind_conf != bind_conf)
3569 continue;
3570
William Lallemandd9d5d1b2020-04-09 16:31:05 +02003571 /* free the sni_ctx and instance */
3572 ckch_inst_free(inst);
William Lallemand49398312020-03-30 17:01:33 +02003573 }
William Lallemand2954c472020-03-06 21:54:13 +01003574 }
William Lallemand2954c472020-03-06 21:54:13 +01003575 }
William Lallemand79d31ec2020-03-25 15:10:49 +01003576 free(bind_conf_node);
William Lallemand2954c472020-03-06 21:54:13 +01003577 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003578 return cfgerr;
3579}
3580
William Lallemand06b22a82020-03-16 14:45:55 +01003581/* Returns a set of ERR_* flags possibly with an error in <err>. */
3582int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
3583{
3584 struct stat buf;
3585 char fp[MAXPATHLEN+1];
3586 int cfgerr = 0;
3587 struct ckch_store *ckchs;
William Lallemand24bde432020-03-09 16:48:43 +01003588 struct ckch_inst *ckch_inst = NULL;
William Lallemand06b22a82020-03-16 14:45:55 +01003589
3590 if ((ckchs = ckchs_lookup(path))) {
3591 /* we found the ckchs in the tree, we can use it directly */
William Lallemand24bde432020-03-09 16:48:43 +01003592 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01003593 }
3594 if (stat(path, &buf) == 0) {
3595 if (S_ISDIR(buf.st_mode) == 0) {
3596 ckchs = ckchs_load_cert_file(path, 0, err);
3597 if (!ckchs)
3598 return ERR_ALERT | ERR_FATAL;
3599
William Lallemand24bde432020-03-09 16:48:43 +01003600 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01003601 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01003602 return ssl_sock_load_cert_list_file(path, 1, bind_conf, bind_conf->frontend, err);
William Lallemand06b22a82020-03-16 14:45:55 +01003603 }
3604 } else {
3605 /* stat failed, could be a bundle */
3606 if (global_ssl.extra_files & SSL_GF_BUNDLE) {
3607 /* try to load a bundle if it is permitted */
3608 ckchs = ckchs_load_cert_file(path, 1, err);
3609 if (!ckchs)
3610 return ERR_ALERT | ERR_FATAL;
William Lallemand24bde432020-03-09 16:48:43 +01003611 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01003612 } else {
3613 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
3614 err && *err ? *err : "", fp, strerror(errno));
3615 cfgerr |= ERR_ALERT | ERR_FATAL;
3616 }
3617 }
3618
3619 return cfgerr;
3620}
3621
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003622/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003623static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003624ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003625{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003626 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003627 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02003628 SSL_OP_ALL | /* all known workarounds for bugs */
3629 SSL_OP_NO_SSLv2 |
3630 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003631 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02003632 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02003633 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02003634 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02003635 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003636 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02003637 SSL_MODE_ENABLE_PARTIAL_WRITE |
3638 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01003639 SSL_MODE_RELEASE_BUFFERS |
3640 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02003641 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003642 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003643 int flags = MC_SSL_O_ALL;
3644 int cfgerr = 0;
William Lallemand50df1cb2020-06-02 10:52:24 +02003645 const int default_min_ver = CONF_TLSV12;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003646
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003647 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003648 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003649
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003650 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01003651 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
3652 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
3653 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003654 else
3655 flags = conf_ssl_methods->flags;
3656
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02003657 min = conf_ssl_methods->min;
3658 max = conf_ssl_methods->max;
William Lallemand50df1cb2020-06-02 10:52:24 +02003659
3660 /* default minimum is TLSV12, */
3661 if (!min) {
3662 if (!max || (max >= default_min_ver)) {
3663 min = default_min_ver;
3664 } else {
3665 ha_warning("Proxy '%s': Ambiguous configuration for bind '%s' at [%s:%d]: the ssl-min-ver value is not configured and the ssl-max-ver value is lower than the default ssl-min-ver value (%s). "
3666 "Setting the ssl-min-ver to %s. Use 'ssl-min-ver' to fix this.\n",
3667 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, methodVersions[default_min_ver].name, methodVersions[max].name);
3668 min = max;
3669 }
3670 }
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003671 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02003672 if (min)
3673 flags |= (methodVersions[min].flag - 1);
3674 if (max)
3675 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003676 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003677 min = max = CONF_TLSV_NONE;
3678 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003679 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003680 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003681 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003682 if (min) {
3683 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003684 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
3685 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
3686 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
3687 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003688 hole = 0;
3689 }
3690 max = i;
3691 }
3692 else {
3693 min = max = i;
3694 }
3695 }
3696 else {
3697 if (min)
3698 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003699 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003700 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003701 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
3702 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003703 cfgerr += 1;
3704 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02003705 /* save real min/max in bind_conf */
3706 conf_ssl_methods->min = min;
3707 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003708
Willy Tarreau9a1ab082019-05-09 13:26:41 +02003709#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003710 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08003711 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003712 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003713 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003714 else
3715 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
3716 if (flags & methodVersions[i].flag)
3717 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003718#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003719 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003720 methodVersions[min].ctx_set_version(ctx, SET_MIN);
3721 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02003722#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003723
3724 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
3725 options |= SSL_OP_NO_TICKET;
3726 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
3727 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08003728
3729#ifdef SSL_OP_NO_RENEGOTIATION
3730 options |= SSL_OP_NO_RENEGOTIATION;
3731#endif
3732
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003733 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00003734
Willy Tarreau5db847a2019-05-09 14:13:35 +02003735#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00003736 if (global_ssl.async)
3737 mode |= SSL_MODE_ASYNC;
3738#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003739 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003740 if (global_ssl.life_time)
3741 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003742
3743#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3744#ifdef OPENSSL_IS_BORINGSSL
3745 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
3746 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Ilya Shipitsine9ff8992020-01-19 12:20:14 +05003747#elif defined(SSL_OP_NO_ANTI_REPLAY)
Olivier Houchard545989f2019-12-17 15:39:54 +01003748 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01003749 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02003750 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
3751 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003752#else
3753 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003754#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02003755 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003756#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003757 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003758}
3759
William Lallemand4f45bb92017-10-30 20:08:51 +01003760
3761static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
3762{
3763 if (first == block) {
3764 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
3765 if (first->len > 0)
3766 sh_ssl_sess_tree_delete(sh_ssl_sess);
3767 }
3768}
3769
3770/* return first block from sh_ssl_sess */
3771static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
3772{
3773 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
3774
3775}
3776
3777/* store a session into the cache
3778 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
3779 * data: asn1 encoded session
3780 * data_len: asn1 encoded session length
3781 * Returns 1 id session was stored (else 0)
3782 */
3783static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
3784{
3785 struct shared_block *first;
3786 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
3787
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02003788 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01003789 if (!first) {
3790 /* Could not retrieve enough free blocks to store that session */
3791 return 0;
3792 }
3793
3794 /* STORE the key in the first elem */
3795 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
3796 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
3797 first->len = sizeof(struct sh_ssl_sess_hdr);
3798
3799 /* it returns the already existing node
3800 or current node if none, never returns null */
3801 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
3802 if (oldsh_ssl_sess != sh_ssl_sess) {
3803 /* NOTE: Row couldn't be in use because we lock read & write function */
3804 /* release the reserved row */
3805 shctx_row_dec_hot(ssl_shctx, first);
3806 /* replace the previous session already in the tree */
3807 sh_ssl_sess = oldsh_ssl_sess;
3808 /* ignore the previous session data, only use the header */
3809 first = sh_ssl_sess_first_block(sh_ssl_sess);
3810 shctx_row_inc_hot(ssl_shctx, first);
3811 first->len = sizeof(struct sh_ssl_sess_hdr);
3812 }
3813
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02003814 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01003815 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01003816 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01003817 }
3818
3819 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01003820
3821 return 1;
3822}
William Lallemanded0b5ad2017-10-30 19:36:36 +01003823
Olivier Houchardbd84ac82017-11-03 13:43:35 +01003824/* SSL callback used when a new session is created while connecting to a server */
3825static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
3826{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02003827 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01003828 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01003829
Willy Tarreau07d94e42018-09-20 10:57:52 +02003830 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01003831
Olivier Houcharde6060c52017-11-16 17:42:52 +01003832 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
3833 int len;
3834 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01003835
Olivier Houcharde6060c52017-11-16 17:42:52 +01003836 len = i2d_SSL_SESSION(sess, NULL);
3837 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
3838 ptr = s->ssl_ctx.reused_sess[tid].ptr;
3839 } else {
3840 free(s->ssl_ctx.reused_sess[tid].ptr);
3841 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
3842 s->ssl_ctx.reused_sess[tid].allocated_size = len;
3843 }
3844 if (s->ssl_ctx.reused_sess[tid].ptr) {
3845 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
3846 &ptr);
3847 }
3848 } else {
3849 free(s->ssl_ctx.reused_sess[tid].ptr);
3850 s->ssl_ctx.reused_sess[tid].ptr = NULL;
3851 }
3852
3853 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01003854}
3855
Olivier Houcharde6060c52017-11-16 17:42:52 +01003856
William Lallemanded0b5ad2017-10-30 19:36:36 +01003857/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01003858int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01003859{
3860 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
3861 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
3862 unsigned char *p;
3863 int data_len;
Emeric Bruneb469652019-10-08 18:27:37 +02003864 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01003865 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01003866
3867 /* Session id is already stored in to key and session id is known
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05003868 * so we don't store it to keep size.
Emeric Bruneb469652019-10-08 18:27:37 +02003869 * note: SSL_SESSION_set1_id is using
3870 * a memcpy so we need to use a different pointer
3871 * than sid_data or sid_ctx_data to avoid valgrind
3872 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01003873 */
3874
3875 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Bruneb469652019-10-08 18:27:37 +02003876
3877 /* copy value in an other buffer */
3878 memcpy(encid, sid_data, sid_length);
3879
3880 /* pad with 0 */
3881 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
3882 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
3883
3884 /* force length to zero to avoid ASN1 encoding */
3885 SSL_SESSION_set1_id(sess, encid, 0);
3886
3887 /* force length to zero to avoid ASN1 encoding */
3888 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01003889
3890 /* check if buffer is large enough for the ASN1 encoded session */
3891 data_len = i2d_SSL_SESSION(sess, NULL);
3892 if (data_len > SHSESS_MAX_DATA_LEN)
3893 goto err;
3894
3895 p = encsess;
3896
3897 /* process ASN1 session encoding before the lock */
3898 i2d_SSL_SESSION(sess, &p);
3899
William Lallemanded0b5ad2017-10-30 19:36:36 +01003900
William Lallemanda3c77cf2017-10-30 23:44:40 +01003901 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01003902 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01003903 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01003904 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01003905err:
3906 /* reset original length values */
Emeric Bruneb469652019-10-08 18:27:37 +02003907 SSL_SESSION_set1_id(sess, encid, sid_length);
3908 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01003909
3910 return 0; /* do not increment session reference count */
3911}
3912
3913/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01003914SSL_SESSION *sh_ssl_sess_get_cb(SSL *ssl, __OPENSSL_110_CONST__ unsigned char *key, int key_len, int *do_copy)
William Lallemanded0b5ad2017-10-30 19:36:36 +01003915{
William Lallemand4f45bb92017-10-30 20:08:51 +01003916 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01003917 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
3918 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01003919 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01003920 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01003921
3922 global.shctx_lookups++;
3923
3924 /* allow the session to be freed automatically by openssl */
3925 *do_copy = 0;
3926
3927 /* tree key is zeros padded sessionid */
3928 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
3929 memcpy(tmpkey, key, key_len);
3930 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
3931 key = tmpkey;
3932 }
3933
3934 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01003935 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01003936
3937 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01003938 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
3939 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01003940 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01003941 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01003942 global.shctx_misses++;
3943 return NULL;
3944 }
3945
William Lallemand4f45bb92017-10-30 20:08:51 +01003946 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
3947 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01003948
William Lallemand4f45bb92017-10-30 20:08:51 +01003949 shctx_row_data_get(ssl_shctx, first, data, sizeof(struct sh_ssl_sess_hdr), first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01003950
William Lallemanda3c77cf2017-10-30 23:44:40 +01003951 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01003952
3953 /* decode ASN1 session */
3954 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01003955 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01003956 /* Reset session id and session id contenxt */
3957 if (sess) {
3958 SSL_SESSION_set1_id(sess, key, key_len);
3959 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
3960 }
3961
3962 return sess;
3963}
3964
William Lallemand4f45bb92017-10-30 20:08:51 +01003965
William Lallemanded0b5ad2017-10-30 19:36:36 +01003966/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01003967void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01003968{
William Lallemand4f45bb92017-10-30 20:08:51 +01003969 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01003970 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
3971 unsigned int sid_length;
3972 const unsigned char *sid_data;
3973 (void)ctx;
3974
3975 sid_data = SSL_SESSION_get_id(sess, &sid_length);
3976 /* tree key is zeros padded sessionid */
3977 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
3978 memcpy(tmpkey, sid_data, sid_length);
3979 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
3980 sid_data = tmpkey;
3981 }
3982
William Lallemanda3c77cf2017-10-30 23:44:40 +01003983 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01003984
3985 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01003986 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
3987 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01003988 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01003989 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01003990 }
3991
3992 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01003993 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01003994}
3995
3996/* Set session cache mode to server and disable openssl internal cache.
3997 * Set shared cache callbacks on an ssl context.
3998 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01003999void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004000{
4001 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4002
4003 if (!ssl_shctx) {
4004 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
4005 return;
4006 }
4007
4008 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
4009 SSL_SESS_CACHE_NO_INTERNAL |
4010 SSL_SESS_CACHE_NO_AUTO_CLEAR);
4011
4012 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01004013 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
4014 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
4015 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004016}
4017
William Lallemand8b453912019-11-21 15:48:10 +01004018/*
4019 * This function applies the SSL configuration on a SSL_CTX
4020 * It returns an error code and fills the <err> buffer
4021 */
4022int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx, char **err)
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004023{
4024 struct proxy *curproxy = bind_conf->frontend;
4025 int cfgerr = 0;
4026 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01004027 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004028 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004029#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004030 const char *conf_ciphersuites;
4031#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004032 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004033
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004034 if (ssl_conf) {
4035 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
4036 int i, min, max;
4037 int flags = MC_SSL_O_ALL;
4038
4039 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004040 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
4041 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004042 if (min)
4043 flags |= (methodVersions[min].flag - 1);
4044 if (max)
4045 flags |= ~((methodVersions[max].flag << 1) - 1);
4046 min = max = CONF_TLSV_NONE;
4047 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4048 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
4049 if (min)
4050 max = i;
4051 else
4052 min = max = i;
4053 }
4054 /* save real min/max */
4055 conf_ssl_methods->min = min;
4056 conf_ssl_methods->max = max;
4057 if (!min) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004058 memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
4059 err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004060 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004061 }
4062 }
4063
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004064 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01004065 case SSL_SOCK_VERIFY_NONE:
4066 verify = SSL_VERIFY_NONE;
4067 break;
4068 case SSL_SOCK_VERIFY_OPTIONAL:
4069 verify = SSL_VERIFY_PEER;
4070 break;
4071 case SSL_SOCK_VERIFY_REQUIRED:
4072 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
4073 break;
4074 }
4075 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
4076 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004077 char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file;
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01004078 char *ca_verify_file = (ssl_conf && ssl_conf->ca_verify_file) ? ssl_conf->ca_verify_file : bind_conf->ssl_conf.ca_verify_file;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004079 char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file;
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01004080 if (ca_file || ca_verify_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02004081 /* set CAfile to verify */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01004082 if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02004083 memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01004084 err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004085 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02004086 }
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01004087 if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) {
4088 memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n",
4089 err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line);
4090 cfgerr |= ERR_ALERT | ERR_FATAL;
4091 }
4092 if (ca_file && !((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02004093 /* set CA names for client cert request, function returns void */
Emmanuel Hocdet129d3282019-10-24 18:08:51 +02004094 SSL_CTX_set_client_CA_list(ctx, SSL_dup_CA_list(ssl_get_client_ca_file(ca_file)));
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02004095 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02004096 }
Emeric Brun850efd52014-01-29 12:24:34 +01004097 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01004098 memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
4099 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004100 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun850efd52014-01-29 12:24:34 +01004101 }
Emeric Brun051cdab2012-10-02 19:25:50 +02004102#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004103 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02004104 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
4105
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01004106 if (!ssl_set_cert_crl_file(store, crl_file)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004107 memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
4108 err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004109 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02004110 }
Emeric Brun561e5742012-10-02 15:20:55 +02004111 else {
4112 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
4113 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02004114 }
Emeric Brun051cdab2012-10-02 19:25:50 +02004115#endif
Emeric Brun644cde02012-12-14 11:21:13 +01004116 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02004117 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004118#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02004119 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004120 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004121 memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
4122 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004123 cfgerr |= ERR_ALERT | ERR_FATAL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004124 }
4125 }
4126#endif
4127
William Lallemand4f45bb92017-10-30 20:08:51 +01004128 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004129 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
4130 if (conf_ciphers &&
4131 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004132 memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
4133 err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004134 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004135 }
4136
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004137#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004138 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
4139 if (conf_ciphersuites &&
4140 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004141 memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
4142 err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004143 cfgerr |= ERR_ALERT | ERR_FATAL;
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004144 }
4145#endif
4146
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01004147#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02004148 /* If tune.ssl.default-dh-param has not been set,
4149 neither has ssl-default-dh-file and no static DH
4150 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01004151 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02004152 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02004153 (ssl_dh_ptr_index == -1 ||
4154 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Willy Tarreau3ba77d22020-05-08 09:31:18 +02004155 /* default to dh-param 2048 */
4156 global_ssl.default_dh_param = 2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004157 }
Remi Gacogne8de54152014-07-15 11:36:40 +02004158
Willy Tarreauef934602016-12-22 23:12:01 +01004159 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004160 if (local_dh_1024 == NULL) {
4161 local_dh_1024 = ssl_get_dh_1024();
4162 }
Willy Tarreauef934602016-12-22 23:12:01 +01004163 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004164 if (local_dh_2048 == NULL) {
4165 local_dh_2048 = ssl_get_dh_2048();
4166 }
Willy Tarreauef934602016-12-22 23:12:01 +01004167 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004168 if (local_dh_4096 == NULL) {
4169 local_dh_4096 = ssl_get_dh_4096();
4170 }
Remi Gacogne8de54152014-07-15 11:36:40 +02004171 }
4172 }
4173 }
4174#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004175
Emeric Brunfc0421f2012-09-07 17:30:07 +02004176 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004177#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02004178 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02004179#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02004180
Bernard Spil13c53f82018-02-15 13:34:58 +01004181#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004182 ssl_conf_cur = NULL;
4183 if (ssl_conf && ssl_conf->npn_str)
4184 ssl_conf_cur = ssl_conf;
4185 else if (bind_conf->ssl_conf.npn_str)
4186 ssl_conf_cur = &bind_conf->ssl_conf;
4187 if (ssl_conf_cur)
4188 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02004189#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01004190#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004191 ssl_conf_cur = NULL;
4192 if (ssl_conf && ssl_conf->alpn_str)
4193 ssl_conf_cur = ssl_conf;
4194 else if (bind_conf->ssl_conf.alpn_str)
4195 ssl_conf_cur = &bind_conf->ssl_conf;
4196 if (ssl_conf_cur)
4197 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02004198#endif
Lukas Tribusd14b49c2019-11-24 18:20:40 +01004199#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004200 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
4201 if (conf_curves) {
4202 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004203 memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
4204 err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004205 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004206 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01004207 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004208 }
4209#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02004210#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004211 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02004212 int i;
4213 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004214#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004215 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02004216 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
4217 NULL);
4218
4219 if (ecdhe == NULL) {
Eric Salama3c8bde82019-11-20 11:33:40 +01004220 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02004221 return cfgerr;
4222 }
4223#else
4224 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
4225 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
4226 ECDHE_DEFAULT_CURVE);
4227#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02004228
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004229 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02004230 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004231 memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
4232 err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004233 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun2b58d042012-09-20 17:10:03 +02004234 }
4235 else {
4236 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
4237 EC_KEY_free(ecdh);
4238 }
4239 }
4240#endif
4241
Emeric Brunfc0421f2012-09-07 17:30:07 +02004242 return cfgerr;
4243}
4244
Evan Broderbe554312013-06-27 00:05:25 -07004245static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
4246{
4247 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
4248 size_t prefixlen, suffixlen;
4249
4250 /* Trivial case */
4251 if (strcmp(pattern, hostname) == 0)
4252 return 1;
4253
Evan Broderbe554312013-06-27 00:05:25 -07004254 /* The rest of this logic is based on RFC 6125, section 6.4.3
4255 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
4256
Emeric Bruna848dae2013-10-08 11:27:28 +02004257 pattern_wildcard = NULL;
4258 pattern_left_label_end = pattern;
4259 while (*pattern_left_label_end != '.') {
4260 switch (*pattern_left_label_end) {
4261 case 0:
4262 /* End of label not found */
4263 return 0;
4264 case '*':
4265 /* If there is more than one wildcards */
4266 if (pattern_wildcard)
4267 return 0;
4268 pattern_wildcard = pattern_left_label_end;
4269 break;
4270 }
4271 pattern_left_label_end++;
4272 }
4273
4274 /* If it's not trivial and there is no wildcard, it can't
4275 * match */
4276 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07004277 return 0;
4278
4279 /* Make sure all labels match except the leftmost */
4280 hostname_left_label_end = strchr(hostname, '.');
4281 if (!hostname_left_label_end
4282 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
4283 return 0;
4284
4285 /* Make sure the leftmost label of the hostname is long enough
4286 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02004287 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07004288 return 0;
4289
4290 /* Finally compare the string on either side of the
4291 * wildcard */
4292 prefixlen = pattern_wildcard - pattern;
4293 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02004294 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
4295 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07004296 return 0;
4297
4298 return 1;
4299}
4300
4301static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
4302{
4303 SSL *ssl;
4304 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01004305 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004306 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02004307 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07004308
4309 int depth;
4310 X509 *cert;
4311 STACK_OF(GENERAL_NAME) *alt_names;
4312 int i;
4313 X509_NAME *cert_subject;
4314 char *str;
4315
4316 if (ok == 0)
4317 return ok;
4318
4319 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02004320 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01004321 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07004322
Willy Tarreauad92a9a2017-07-28 11:38:41 +02004323 /* We're checking if the provided hostnames match the desired one. The
4324 * desired hostname comes from the SNI we presented if any, or if not
4325 * provided then it may have been explicitly stated using a "verifyhost"
4326 * directive. If neither is set, we don't care about the name so the
4327 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02004328 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01004329 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02004330 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004331 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02004332 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004333 if (!servername)
4334 return ok;
4335 }
Evan Broderbe554312013-06-27 00:05:25 -07004336
4337 /* We only need to verify the CN on the actual server cert,
4338 * not the indirect CAs */
4339 depth = X509_STORE_CTX_get_error_depth(ctx);
4340 if (depth != 0)
4341 return ok;
4342
4343 /* At this point, the cert is *not* OK unless we can find a
4344 * hostname match */
4345 ok = 0;
4346
4347 cert = X509_STORE_CTX_get_current_cert(ctx);
4348 /* It seems like this might happen if verify peer isn't set */
4349 if (!cert)
4350 return ok;
4351
4352 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
4353 if (alt_names) {
4354 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
4355 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
4356 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004357#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02004358 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
4359#else
Evan Broderbe554312013-06-27 00:05:25 -07004360 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02004361#endif
Evan Broderbe554312013-06-27 00:05:25 -07004362 ok = ssl_sock_srv_hostcheck(str, servername);
4363 OPENSSL_free(str);
4364 }
4365 }
4366 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02004367 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07004368 }
4369
4370 cert_subject = X509_get_subject_name(cert);
4371 i = -1;
4372 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
4373 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004374 ASN1_STRING *value;
4375 value = X509_NAME_ENTRY_get_data(entry);
4376 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07004377 ok = ssl_sock_srv_hostcheck(str, servername);
4378 OPENSSL_free(str);
4379 }
4380 }
4381
Willy Tarreau71d058c2017-07-26 20:09:56 +02004382 /* report the mismatch and indicate if SNI was used or not */
4383 if (!ok && !conn->err_code)
4384 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07004385 return ok;
4386}
4387
Emeric Brun94324a42012-10-11 14:00:19 +02004388/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01004389int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02004390{
Willy Tarreau03209342016-12-22 17:08:28 +01004391 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02004392 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02004393 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02004394 SSL_OP_ALL | /* all known workarounds for bugs */
4395 SSL_OP_NO_SSLv2 |
4396 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02004397 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02004398 SSL_MODE_ENABLE_PARTIAL_WRITE |
4399 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01004400 SSL_MODE_RELEASE_BUFFERS |
4401 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01004402 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004403 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004404 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004405 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004406 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02004407
Thierry Fournier383085f2013-01-24 14:15:43 +01004408 /* Make sure openssl opens /dev/urandom before the chroot */
4409 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004410 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01004411 cfgerr++;
4412 }
4413
Willy Tarreaufce03112015-01-15 21:32:40 +01004414 /* Automatic memory computations need to know we use SSL there */
4415 global.ssl_used_backend = 1;
4416
4417 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02004418 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01004419 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004420 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
4421 curproxy->id, srv->id,
4422 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02004423 cfgerr++;
4424 return cfgerr;
4425 }
4426 }
Christopher Fauletf61f33a2020-03-27 18:55:49 +01004427 if (srv->use_ssl == 1)
Emeric Brun94324a42012-10-11 14:00:19 +02004428 srv->xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02004429
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004430 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004431 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004432 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
4433 proxy_type_str(curproxy), curproxy->id,
4434 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02004435 cfgerr++;
4436 return cfgerr;
4437 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004438
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004439 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01004440 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
4441 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4442 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004443 else
4444 flags = conf_ssl_methods->flags;
4445
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004446 /* Real min and max should be determinate with configuration and openssl's capabilities */
4447 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004448 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004449 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004450 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004451
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004452 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004453 min = max = CONF_TLSV_NONE;
4454 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004455 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004456 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004457 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004458 if (min) {
4459 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004460 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
4461 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4462 proxy_type_str(curproxy), curproxy->id, srv->id,
4463 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004464 hole = 0;
4465 }
4466 max = i;
4467 }
4468 else {
4469 min = max = i;
4470 }
4471 }
4472 else {
4473 if (min)
4474 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004475 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004476 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004477 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
4478 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004479 cfgerr += 1;
4480 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004481
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004482#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004483 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08004484 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004485 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004486 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004487 else
4488 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4489 if (flags & methodVersions[i].flag)
4490 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004491#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004492 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004493 methodVersions[min].ctx_set_version(ctx, SET_MIN);
4494 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004495#endif
4496
4497 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
4498 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004499 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004500
Willy Tarreau5db847a2019-05-09 14:13:35 +02004501#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004502 if (global_ssl.async)
4503 mode |= SSL_MODE_ASYNC;
4504#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004505 SSL_CTX_set_mode(ctx, mode);
4506 srv->ssl_ctx.ctx = ctx;
4507
Emeric Bruna7aa3092012-10-26 12:58:00 +02004508 if (srv->ssl_ctx.client_crt) {
4509 if (SSL_CTX_use_PrivateKey_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt, SSL_FILETYPE_PEM) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004510 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
4511 proxy_type_str(curproxy), curproxy->id,
4512 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02004513 cfgerr++;
4514 }
4515 else if (SSL_CTX_use_certificate_chain_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004516 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
4517 proxy_type_str(curproxy), curproxy->id,
4518 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02004519 cfgerr++;
4520 }
4521 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004522 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
4523 proxy_type_str(curproxy), curproxy->id,
4524 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02004525 cfgerr++;
4526 }
4527 }
Emeric Brun94324a42012-10-11 14:00:19 +02004528
Emeric Brun850efd52014-01-29 12:24:34 +01004529 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
4530 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01004531 switch (srv->ssl_ctx.verify) {
4532 case SSL_SOCK_VERIFY_NONE:
4533 verify = SSL_VERIFY_NONE;
4534 break;
4535 case SSL_SOCK_VERIFY_REQUIRED:
4536 verify = SSL_VERIFY_PEER;
4537 break;
4538 }
Evan Broderbe554312013-06-27 00:05:25 -07004539 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01004540 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02004541 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01004542 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02004543 if (srv->ssl_ctx.ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02004544 /* set CAfile to verify */
4545 if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) {
4546 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to set CA file '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004547 curproxy->id, srv->id,
4548 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02004549 cfgerr++;
4550 }
4551 }
Emeric Brun850efd52014-01-29 12:24:34 +01004552 else {
4553 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01004554 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled by default but no CA file specified. If you're running on a LAN where you're certain to trust the server's certificate, please set an explicit 'verify none' statement on the 'server' line, or use 'ssl-server-verify none' in the global section to disable server-side verifications by default.\n",
4555 curproxy->id, srv->id,
4556 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01004557 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01004558 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
4559 curproxy->id, srv->id,
4560 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01004561 cfgerr++;
4562 }
Emeric Brunef42d922012-10-11 16:11:36 +02004563#ifdef X509_V_FLAG_CRL_CHECK
4564 if (srv->ssl_ctx.crl_file) {
4565 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
4566
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01004567 if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004568 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
4569 curproxy->id, srv->id,
4570 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02004571 cfgerr++;
4572 }
4573 else {
4574 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
4575 }
4576 }
4577#endif
4578 }
4579
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004580 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
4581 SSL_SESS_CACHE_NO_INTERNAL_STORE);
4582 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02004583 if (srv->ssl_ctx.ciphers &&
4584 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004585 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
4586 curproxy->id, srv->id,
4587 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02004588 cfgerr++;
4589 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004590
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004591#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004592 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00004593 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004594 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
4595 curproxy->id, srv->id,
4596 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
4597 cfgerr++;
4598 }
4599#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01004600#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
4601 if (srv->ssl_ctx.npn_str)
4602 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
4603#endif
4604#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4605 if (srv->ssl_ctx.alpn_str)
4606 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
4607#endif
4608
Emeric Brun94324a42012-10-11 14:00:19 +02004609
4610 return cfgerr;
4611}
4612
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004613/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02004614 * be NULL, in which case nothing is done. Returns the number of errors
4615 * encountered.
4616 */
Willy Tarreau03209342016-12-22 17:08:28 +01004617int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004618{
4619 struct ebmb_node *node;
4620 struct sni_ctx *sni;
4621 int err = 0;
William Lallemand8b453912019-11-21 15:48:10 +01004622 int errcode = 0;
4623 char *errmsg = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004624
Willy Tarreaufce03112015-01-15 21:32:40 +01004625 /* Automatic memory computations need to know we use SSL there */
4626 global.ssl_used_frontend = 1;
4627
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004628 /* Make sure openssl opens /dev/urandom before the chroot */
4629 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004630 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004631 err++;
4632 }
4633 /* Create initial_ctx used to start the ssl connection before do switchctx */
4634 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004635 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004636 /* It should not be necessary to call this function, but it's
4637 necessary first to check and move all initialisation related
4638 to initial_ctx in ssl_sock_initial_ctx. */
William Lallemand8b453912019-11-21 15:48:10 +01004639 errcode |= ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx, &errmsg);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004640 }
Emeric Brun0bed9942014-10-30 19:25:24 +01004641 if (bind_conf->default_ctx)
William Lallemand8b453912019-11-21 15:48:10 +01004642 errcode |= ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, &errmsg);
Emeric Brun0bed9942014-10-30 19:25:24 +01004643
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004644 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004645 while (node) {
4646 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01004647 if (!sni->order && sni->ctx != bind_conf->default_ctx)
4648 /* only initialize the CTX on its first occurrence and
4649 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01004650 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004651 node = ebmb_next(node);
4652 }
4653
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004654 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004655 while (node) {
4656 sni = ebmb_entry(node, struct sni_ctx, name);
William Lallemand8b453912019-11-21 15:48:10 +01004657 if (!sni->order && sni->ctx != bind_conf->default_ctx) {
Emeric Brun0bed9942014-10-30 19:25:24 +01004658 /* only initialize the CTX on its first occurrence and
4659 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01004660 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
4661 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004662 node = ebmb_next(node);
4663 }
William Lallemand8b453912019-11-21 15:48:10 +01004664
4665 if (errcode & ERR_WARN) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01004666 ha_warning("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01004667 } else if (errcode & ERR_CODE) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01004668 ha_alert("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01004669 err++;
4670 }
4671
4672 free(errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004673 return err;
4674}
4675
Willy Tarreau55d37912016-12-21 23:38:39 +01004676/* Prepares all the contexts for a bind_conf and allocates the shared SSL
4677 * context if needed. Returns < 0 on error, 0 on success. The warnings and
4678 * alerts are directly emitted since the rest of the stack does it below.
4679 */
4680int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
4681{
4682 struct proxy *px = bind_conf->frontend;
4683 int alloc_ctx;
4684 int err;
4685
4686 if (!bind_conf->is_ssl) {
4687 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004688 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
4689 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01004690 }
4691 return 0;
4692 }
4693 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02004694 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004695 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
4696 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02004697 }
4698 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004699 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
4700 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02004701 return -1;
4702 }
Willy Tarreau55d37912016-12-21 23:38:39 +01004703 }
William Lallemandc61c0b32017-12-04 18:46:39 +01004704 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01004705 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02004706 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01004707 sizeof(*sh_ssl_sess_tree),
4708 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02004709 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01004710 if (alloc_ctx == SHCTX_E_INIT_LOCK)
4711 ha_alert("Unable to initialize the lock for the shared SSL session cache. You can retry using the global statement 'tune.ssl.force-private-cache' but it could increase CPU usage due to renegotiations if nbproc > 1.\n");
4712 else
4713 ha_alert("Unable to allocate SSL session cache.\n");
4714 return -1;
4715 }
4716 /* free block callback */
4717 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
4718 /* init the root tree within the extra space */
4719 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
4720 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01004721 }
Willy Tarreau55d37912016-12-21 23:38:39 +01004722 err = 0;
4723 /* initialize all certificate contexts */
4724 err += ssl_sock_prepare_all_ctx(bind_conf);
4725
4726 /* initialize CA variables if the certificates generation is enabled */
4727 err += ssl_sock_load_ca(bind_conf);
4728
4729 return -err;
4730}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02004731
4732/* release ssl context allocated for servers. */
4733void ssl_sock_free_srv_ctx(struct server *srv)
4734{
Olivier Houchardc7566002018-11-20 23:33:50 +01004735#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4736 if (srv->ssl_ctx.alpn_str)
4737 free(srv->ssl_ctx.alpn_str);
4738#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01004739#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01004740 if (srv->ssl_ctx.npn_str)
4741 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01004742#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02004743 if (srv->ssl_ctx.ctx)
4744 SSL_CTX_free(srv->ssl_ctx.ctx);
4745}
4746
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004747/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02004748 * be NULL, in which case nothing is done. The default_ctx is nullified too.
4749 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004750void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004751{
4752 struct ebmb_node *node, *back;
4753 struct sni_ctx *sni;
4754
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004755 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004756 while (node) {
4757 sni = ebmb_entry(node, struct sni_ctx, name);
4758 back = ebmb_next(node);
4759 ebmb_delete(node);
William Lallemand02e19a52020-04-08 16:11:26 +02004760 SSL_CTX_free(sni->ctx);
4761 if (!sni->order) { /* only free the CTX conf on its first occurrence */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004762 ssl_sock_free_ssl_conf(sni->conf);
4763 free(sni->conf);
4764 sni->conf = NULL;
4765 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004766 free(sni);
4767 node = back;
4768 }
4769
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004770 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004771 while (node) {
4772 sni = ebmb_entry(node, struct sni_ctx, name);
4773 back = ebmb_next(node);
4774 ebmb_delete(node);
William Lallemand02e19a52020-04-08 16:11:26 +02004775 SSL_CTX_free(sni->ctx);
4776 if (!sni->order) { /* only free the SSL conf its first occurrence */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004777 ssl_sock_free_ssl_conf(sni->conf);
4778 free(sni->conf);
4779 sni->conf = NULL;
4780 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004781 free(sni);
4782 node = back;
4783 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004784 SSL_CTX_free(bind_conf->initial_ctx);
4785 bind_conf->initial_ctx = NULL;
William Lallemand02e19a52020-04-08 16:11:26 +02004786 SSL_CTX_free(bind_conf->default_ctx);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004787 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004788 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02004789}
4790
Willy Tarreau795cdab2016-12-22 17:30:54 +01004791/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
4792void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
4793{
4794 ssl_sock_free_ca(bind_conf);
4795 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004796 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01004797 free(bind_conf->ca_sign_file);
4798 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02004799 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01004800 free(bind_conf->keys_ref->filename);
4801 free(bind_conf->keys_ref->tlskeys);
4802 LIST_DEL(&bind_conf->keys_ref->list);
4803 free(bind_conf->keys_ref);
4804 }
4805 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01004806 bind_conf->ca_sign_pass = NULL;
4807 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01004808}
4809
Christopher Faulet31af49d2015-06-09 17:29:50 +02004810/* Load CA cert file and private key used to generate certificates */
4811int
Willy Tarreau03209342016-12-22 17:08:28 +01004812ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02004813{
Willy Tarreau03209342016-12-22 17:08:28 +01004814 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02004815 FILE *fp;
4816 X509 *cacert = NULL;
4817 EVP_PKEY *capkey = NULL;
4818 int err = 0;
4819
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02004820 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02004821 return err;
4822
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01004823#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02004824 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01004825 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02004826 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02004827 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02004828 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02004829#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02004830
Christopher Faulet31af49d2015-06-09 17:29:50 +02004831 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004832 ha_alert("Proxy '%s': cannot enable certificate generation, "
4833 "no CA certificate File configured at [%s:%d].\n",
4834 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02004835 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004836 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02004837
4838 /* read in the CA certificate */
4839 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004840 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
4841 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02004842 goto load_error;
4843 }
4844 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004845 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
4846 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004847 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02004848 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004849 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02004850 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004851 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
4852 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004853 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02004854 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02004855
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004856 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02004857 bind_conf->ca_sign_cert = cacert;
4858 bind_conf->ca_sign_pkey = capkey;
4859 return err;
4860
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004861 read_error:
4862 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02004863 if (capkey) EVP_PKEY_free(capkey);
4864 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004865 load_error:
4866 bind_conf->generate_certs = 0;
4867 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02004868 return err;
4869}
4870
4871/* Release CA cert and private key used to generate certificated */
4872void
4873ssl_sock_free_ca(struct bind_conf *bind_conf)
4874{
Christopher Faulet31af49d2015-06-09 17:29:50 +02004875 if (bind_conf->ca_sign_pkey)
4876 EVP_PKEY_free(bind_conf->ca_sign_pkey);
4877 if (bind_conf->ca_sign_cert)
4878 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01004879 bind_conf->ca_sign_pkey = NULL;
4880 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02004881}
4882
Emeric Brun46591952012-05-18 15:47:34 +02004883/*
4884 * This function is called if SSL * context is not yet allocated. The function
4885 * is designed to be called before any other data-layer operation and sets the
4886 * handshake flag on the connection. It is safe to call it multiple times.
4887 * It returns 0 on success and -1 in error case.
4888 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01004889static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02004890{
Olivier Houchard66ab4982019-02-26 18:37:15 +01004891 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02004892 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01004893 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02004894 return 0;
4895
Willy Tarreau3c728722014-01-23 13:50:42 +01004896 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02004897 return 0;
4898
Olivier Houchard66ab4982019-02-26 18:37:15 +01004899 ctx = pool_alloc(ssl_sock_ctx_pool);
4900 if (!ctx) {
4901 conn->err_code = CO_ER_SSL_NO_MEM;
4902 return -1;
4903 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004904 ctx->wait_event.tasklet = tasklet_new();
4905 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02004906 conn->err_code = CO_ER_SSL_NO_MEM;
4907 pool_free(ssl_sock_ctx_pool, ctx);
4908 return -1;
4909 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004910 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
4911 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02004912 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01004913 ctx->sent_early_data = 0;
Olivier Houchard54907bb2019-12-19 15:02:39 +01004914 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02004915 ctx->conn = conn;
Willy Tarreau113d52b2020-01-10 09:20:26 +01004916 ctx->subs = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02004917 ctx->xprt_st = 0;
4918 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02004919
4920 /* Only work with sockets for now, this should be adapted when we'll
4921 * add QUIC support.
4922 */
4923 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02004924 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02004925 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
4926 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02004927 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01004928
Willy Tarreau20879a02012-12-03 16:32:10 +01004929 if (global.maxsslconn && sslconns >= global.maxsslconn) {
4930 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02004931 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01004932 }
Willy Tarreau403edff2012-09-06 11:58:37 +02004933
Emeric Brun46591952012-05-18 15:47:34 +02004934 /* If it is in client mode initiate SSL session
4935 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004936 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004937 int may_retry = 1;
4938
4939 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02004940 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01004941 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
4942 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004943 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01004944 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004945 goto retry_connect;
4946 }
Willy Tarreau20879a02012-12-03 16:32:10 +01004947 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01004948 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01004949 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02004950 ctx->bio = BIO_new(ha_meth);
4951 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01004952 SSL_free(ctx->ssl);
4953 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004954 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01004955 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004956 goto retry_connect;
4957 }
Emeric Brun55476152014-11-12 17:35:37 +01004958 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01004959 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01004960 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02004961 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02004962 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02004963
Evan Broderbe554312013-06-27 00:05:25 -07004964 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01004965 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
4966 SSL_free(ctx->ssl);
4967 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01004968 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004969 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01004970 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004971 goto retry_connect;
4972 }
Emeric Brun55476152014-11-12 17:35:37 +01004973 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01004974 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01004975 }
4976
Olivier Houchard66ab4982019-02-26 18:37:15 +01004977 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02004978 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
4979 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
4980 SSL_SESSION *sess = d2i_SSL_SESSION(NULL, &ptr, __objt_server(conn->target)->ssl_ctx.reused_sess[tid].size);
Olivier Houchard66ab4982019-02-26 18:37:15 +01004981 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01004982 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02004983 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
4984 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01004985 } else if (sess) {
4986 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01004987 }
4988 }
Evan Broderbe554312013-06-27 00:05:25 -07004989
Emeric Brun46591952012-05-18 15:47:34 +02004990 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02004991 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02004992
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01004993 _HA_ATOMIC_ADD(&sslconns, 1);
4994 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01004995 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02004996 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004997 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02004998 return 0;
4999 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005000 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005001 int may_retry = 1;
5002
5003 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02005004 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005005 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
5006 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005007 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005008 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005009 goto retry_accept;
5010 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005011 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005012 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005013 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005014 ctx->bio = BIO_new(ha_meth);
5015 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01005016 SSL_free(ctx->ssl);
5017 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005018 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005019 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005020 goto retry_accept;
5021 }
Emeric Brun55476152014-11-12 17:35:37 +01005022 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005023 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005024 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005025 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02005026 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02005027
Emeric Brune1f38db2012-09-03 20:36:47 +02005028 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005029 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
5030 SSL_free(ctx->ssl);
5031 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005032 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005033 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005034 goto retry_accept;
5035 }
Emeric Brun55476152014-11-12 17:35:37 +01005036 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005037 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005038 }
5039
Frédéric Lécaille3139c1b2020-01-24 14:56:18 +01005040#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
5041 if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) {
5042 b_alloc(&ctx->early_buf);
5043 SSL_set_max_early_data(ctx->ssl,
5044 /* Only allow early data if we managed to allocate
5045 * a buffer.
5046 */
5047 (!b_is_null(&ctx->early_buf)) ?
5048 global.tune.bufsize - global.tune.maxrewrite : 0);
5049 }
5050#endif
5051
Olivier Houchard66ab4982019-02-26 18:37:15 +01005052 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02005053
Emeric Brun46591952012-05-18 15:47:34 +02005054 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005055 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02005056#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02005057 conn->flags |= CO_FL_EARLY_SSL_HS;
5058#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02005059
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005060 _HA_ATOMIC_ADD(&sslconns, 1);
5061 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005062 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005063 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005064 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02005065 return 0;
5066 }
5067 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01005068 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005069err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005070 if (ctx && ctx->wait_event.tasklet)
5071 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005072 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02005073 return -1;
5074}
5075
5076
5077/* This is the callback which is used when an SSL handshake is pending. It
5078 * updates the FD status if it wants some polling before being called again.
5079 * It returns 0 if it fails in a fatal way or needs to poll to go further,
5080 * otherwise it returns non-zero and removes itself from the connection's
5081 * flags (the bit is provided in <flag> by the caller).
5082 */
Olivier Houchard000694c2019-05-23 14:45:12 +02005083static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02005084{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005085 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005086 int ret;
5087
Willy Tarreau3c728722014-01-23 13:50:42 +01005088 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02005089 return 0;
5090
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005091 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005092 goto out_error;
5093
Willy Tarreau5db847a2019-05-09 14:13:35 +02005094#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02005095 /*
5096 * Check if we have early data. If we do, we have to read them
5097 * before SSL_do_handshake() is called, And there's no way to
5098 * detect early data, except to try to read them
5099 */
5100 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchard54907bb2019-12-19 15:02:39 +01005101 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02005102
Olivier Houchard54907bb2019-12-19 15:02:39 +01005103 while (1) {
5104 ret = SSL_read_early_data(ctx->ssl,
5105 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
5106 &read_data);
5107 if (ret == SSL_READ_EARLY_DATA_ERROR)
5108 goto check_error;
5109 if (read_data > 0) {
5110 conn->flags |= CO_FL_EARLY_DATA;
5111 b_add(&ctx->early_buf, read_data);
5112 }
5113 if (ret == SSL_READ_EARLY_DATA_FINISH) {
5114 conn->flags &= ~CO_FL_EARLY_SSL_HS;
5115 if (!b_data(&ctx->early_buf))
5116 b_free(&ctx->early_buf);
5117 break;
5118 }
5119 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02005120 }
5121#endif
Emeric Brun674b7432012-11-08 19:21:55 +01005122 /* If we use SSL_do_handshake to process a reneg initiated by
5123 * the remote peer, it sometimes returns SSL_ERROR_SSL.
5124 * Usually SSL_write and SSL_read are used and process implicitly
5125 * the reneg handshake.
5126 * Here we use SSL_peek as a workaround for reneg.
5127 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01005128 if (!(conn->flags & CO_FL_WAIT_L6_CONN) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01005129 char c;
5130
Olivier Houchard66ab4982019-02-26 18:37:15 +01005131 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01005132 if (ret <= 0) {
5133 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005134 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005135
Emeric Brun674b7432012-11-08 19:21:55 +01005136 if (ret == SSL_ERROR_WANT_WRITE) {
5137 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02005138 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005139 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01005140 return 0;
5141 }
5142 else if (ret == SSL_ERROR_WANT_READ) {
5143 /* handshake may have been completed but we have
5144 * no more data to read.
5145 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005146 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01005147 ret = 1;
5148 goto reneg_ok;
5149 }
5150 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02005151 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005152 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01005153 return 0;
5154 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02005155#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005156 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005157 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005158 return 0;
5159 }
5160#endif
Emeric Brun674b7432012-11-08 19:21:55 +01005161 else if (ret == SSL_ERROR_SYSCALL) {
5162 /* if errno is null, then connection was successfully established */
5163 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
5164 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01005165 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02005166#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
5167 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005168 conn->err_code = CO_ER_SSL_HANDSHAKE;
5169#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005170 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005171#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02005172 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005173 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005174 empty_handshake = state == TLS_ST_BEFORE;
5175#else
Lukas Tribus49799162019-07-08 14:29:15 +02005176 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
5177 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005178#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005179 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02005180 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005181 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005182 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5183 else
5184 conn->err_code = CO_ER_SSL_EMPTY;
5185 }
5186 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005187 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005188 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5189 else
5190 conn->err_code = CO_ER_SSL_ABORT;
5191 }
5192 }
5193 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005194 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005195 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01005196 else
Emeric Brun29f037d2014-04-25 19:05:36 +02005197 conn->err_code = CO_ER_SSL_HANDSHAKE;
5198 }
Lukas Tribus49799162019-07-08 14:29:15 +02005199#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01005200 }
Emeric Brun674b7432012-11-08 19:21:55 +01005201 goto out_error;
5202 }
5203 else {
5204 /* Fail on all other handshake errors */
5205 /* Note: OpenSSL may leave unread bytes in the socket's
5206 * buffer, causing an RST to be emitted upon close() on
5207 * TCP sockets. We first try to drain possibly pending
5208 * data to avoid this as much as possible.
5209 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01005210 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01005211 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005212 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02005213 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01005214 goto out_error;
5215 }
5216 }
5217 /* read some data: consider handshake completed */
5218 goto reneg_ok;
5219 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005220 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005221check_error:
Emeric Brun46591952012-05-18 15:47:34 +02005222 if (ret != 1) {
5223 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005224 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02005225
5226 if (ret == SSL_ERROR_WANT_WRITE) {
5227 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02005228 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005229 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02005230 return 0;
5231 }
5232 else if (ret == SSL_ERROR_WANT_READ) {
5233 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02005234 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005235 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
5236 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02005237 return 0;
5238 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02005239#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005240 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005241 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005242 return 0;
5243 }
5244#endif
Willy Tarreau89230192012-09-28 20:22:13 +02005245 else if (ret == SSL_ERROR_SYSCALL) {
5246 /* if errno is null, then connection was successfully established */
5247 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
5248 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005249 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02005250#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
5251 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005252 conn->err_code = CO_ER_SSL_HANDSHAKE;
5253#else
5254 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005255#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02005256 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005257 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005258 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005259#else
Lukas Tribus49799162019-07-08 14:29:15 +02005260 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
5261 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005262#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005263 if (empty_handshake) {
5264 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005265 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005266 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5267 else
5268 conn->err_code = CO_ER_SSL_EMPTY;
5269 }
5270 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005271 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005272 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5273 else
5274 conn->err_code = CO_ER_SSL_ABORT;
5275 }
Emeric Brun29f037d2014-04-25 19:05:36 +02005276 }
5277 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005278 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005279 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5280 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005281 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02005282 }
Lukas Tribus49799162019-07-08 14:29:15 +02005283#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02005284 }
Willy Tarreau89230192012-09-28 20:22:13 +02005285 goto out_error;
5286 }
Emeric Brun46591952012-05-18 15:47:34 +02005287 else {
5288 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02005289 /* Note: OpenSSL may leave unread bytes in the socket's
5290 * buffer, causing an RST to be emitted upon close() on
5291 * TCP sockets. We first try to drain possibly pending
5292 * data to avoid this as much as possible.
5293 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01005294 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01005295 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005296 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02005297 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02005298 goto out_error;
5299 }
5300 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02005301#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01005302 else {
5303 /*
5304 * If the server refused the early data, we have to send a
5305 * 425 to the client, as we no longer have the data to sent
5306 * them again.
5307 */
5308 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005309 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01005310 conn->err_code = CO_ER_SSL_EARLY_FAILED;
5311 goto out_error;
5312 }
5313 }
5314 }
5315#endif
5316
Emeric Brun46591952012-05-18 15:47:34 +02005317
Emeric Brun674b7432012-11-08 19:21:55 +01005318reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00005319
Willy Tarreau5db847a2019-05-09 14:13:35 +02005320#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005321 /* ASYNC engine API doesn't support moving read/write
5322 * buffers. So we disable ASYNC mode right after
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05005323 * the handshake to avoid buffer overflow.
Emeric Brunb5e42a82017-06-06 12:35:14 +00005324 */
5325 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005326 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005327#endif
Emeric Brun46591952012-05-18 15:47:34 +02005328 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005329 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02005330 if (objt_server(conn->target)) {
5331 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
5332 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
5333 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02005334 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02005335 else {
5336 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
5337 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
5338 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
5339 }
Emeric Brun46591952012-05-18 15:47:34 +02005340 }
5341
5342 /* The connection is now established at both layers, it's time to leave */
5343 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
5344 return 1;
5345
5346 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01005347 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02005348 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01005349 ERR_clear_error();
5350
Emeric Brun9fa89732012-10-04 17:09:56 +02005351 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02005352 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
5353 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
5354 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02005355 }
5356
Emeric Brun46591952012-05-18 15:47:34 +02005357 /* Fail on all other handshake errors */
5358 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01005359 if (!conn->err_code)
5360 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02005361 return 0;
5362}
5363
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01005364/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5365 * event subscriber <es> is not allowed to change from a previous call as long
5366 * as at least one event is still subscribed. The <event_type> must only be a
5367 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0,
5368 * unless the transport layer was already released.
5369 */
5370static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01005371{
Olivier Houchardea8dd942019-05-20 14:02:16 +02005372 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005373
Olivier Houchard0ff28652019-06-24 18:57:39 +02005374 if (!ctx)
5375 return -1;
5376
Willy Tarreau113d52b2020-01-10 09:20:26 +01005377 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01005378 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005379
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01005380 ctx->subs = es;
5381 es->events |= event_type;
Willy Tarreau113d52b2020-01-10 09:20:26 +01005382
5383 /* we may have to subscribe to lower layers for new events */
5384 event_type &= ~ctx->wait_event.events;
5385 if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS))
5386 ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005387 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01005388}
5389
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01005390/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5391 * The <es> pointer is not allowed to differ from the one passed to the
5392 * subscribe() call. It always returns zero.
5393 */
5394static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01005395{
Olivier Houchardea8dd942019-05-20 14:02:16 +02005396 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005397
Willy Tarreau113d52b2020-01-10 09:20:26 +01005398 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01005399 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005400
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01005401 es->events &= ~event_type;
5402 if (!es->events)
Willy Tarreau113d52b2020-01-10 09:20:26 +01005403 ctx->subs = NULL;
5404
5405 /* If we subscribed, and we're not doing the handshake,
5406 * then we subscribed because the upper layer asked for it,
5407 * as the upper layer is no longer interested, we can
5408 * unsubscribe too.
5409 */
5410 event_type &= ctx->wait_event.events;
5411 if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS))
5412 conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005413
5414 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01005415}
5416
Olivier Houchard2e055482019-05-27 19:50:12 +02005417/* Use the provided XPRT as an underlying XPRT, and provide the old one.
5418 * Returns 0 on success, and non-zero on failure.
5419 */
5420static int ssl_add_xprt(struct connection *conn, void *xprt_ctx, void *toadd_ctx, const struct xprt_ops *toadd_ops, void **oldxprt_ctx, const struct xprt_ops **oldxprt_ops)
5421{
5422 struct ssl_sock_ctx *ctx = xprt_ctx;
5423
5424 if (oldxprt_ops != NULL)
5425 *oldxprt_ops = ctx->xprt;
5426 if (oldxprt_ctx != NULL)
5427 *oldxprt_ctx = ctx->xprt_ctx;
5428 ctx->xprt = toadd_ops;
5429 ctx->xprt_ctx = toadd_ctx;
5430 return 0;
5431}
5432
Olivier Houchard5149b592019-05-23 17:47:36 +02005433/* Remove the specified xprt. If if it our underlying XPRT, remove it and
5434 * return 0, otherwise just call the remove_xprt method from the underlying
5435 * XPRT.
5436 */
5437static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
5438{
5439 struct ssl_sock_ctx *ctx = xprt_ctx;
5440
5441 if (ctx->xprt_ctx == toremove_ctx) {
5442 ctx->xprt_ctx = newctx;
5443 ctx->xprt = newops;
5444 return 0;
5445 }
5446 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
5447}
5448
Olivier Houchardea8dd942019-05-20 14:02:16 +02005449static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
5450{
5451 struct ssl_sock_ctx *ctx = context;
5452
5453 /* First if we're doing an handshake, try that */
5454 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
5455 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
5456 /* If we had an error, or the handshake is done and I/O is available,
5457 * let the upper layer know.
Olivier Houchard477902b2020-01-22 18:08:48 +01005458 * If no mux was set up yet, then call conn_create_mux()
Olivier Houchardea8dd942019-05-20 14:02:16 +02005459 * we can't be sure conn_fd_handler() will be called again.
5460 */
5461 if ((ctx->conn->flags & CO_FL_ERROR) ||
5462 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
5463 int ret = 0;
5464 int woke = 0;
5465
5466 /* On error, wake any waiter */
Willy Tarreau113d52b2020-01-10 09:20:26 +01005467 if (ctx->subs) {
5468 tasklet_wakeup(ctx->subs->tasklet);
5469 ctx->subs->events = 0;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005470 woke = 1;
Willy Tarreau113d52b2020-01-10 09:20:26 +01005471 ctx->subs = NULL;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005472 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01005473
Olivier Houchardea8dd942019-05-20 14:02:16 +02005474 /* If we're the first xprt for the connection, let the
Olivier Houchard477902b2020-01-22 18:08:48 +01005475 * upper layers know. If we have no mux, create it,
5476 * and once we have a mux, call its wake method if we didn't
5477 * woke a tasklet already.
Olivier Houchardea8dd942019-05-20 14:02:16 +02005478 */
5479 if (ctx->conn->xprt_ctx == ctx) {
Olivier Houchard477902b2020-01-22 18:08:48 +01005480 if (!ctx->conn->mux)
5481 ret = conn_create_mux(ctx->conn);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005482 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
5483 ctx->conn->mux->wake(ctx->conn);
5484 return NULL;
5485 }
5486 }
Olivier Houchard54907bb2019-12-19 15:02:39 +01005487#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
5488 /* If we have early data and somebody wants to receive, let them */
Willy Tarreau113d52b2020-01-10 09:20:26 +01005489 else if (b_data(&ctx->early_buf) && ctx->subs &&
5490 ctx->subs->events & SUB_RETRY_RECV) {
5491 tasklet_wakeup(ctx->subs->tasklet);
5492 ctx->subs->events &= ~SUB_RETRY_RECV;
5493 if (!ctx->subs->events)
5494 ctx->subs = NULL;
Olivier Houchard54907bb2019-12-19 15:02:39 +01005495 }
5496#endif
Olivier Houchardea8dd942019-05-20 14:02:16 +02005497 return NULL;
5498}
5499
Emeric Brun46591952012-05-18 15:47:34 +02005500/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01005501 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02005502 * buffer wraps, in which case a second call may be performed. The connection's
5503 * flags are updated with whatever special event is detected (error, read0,
5504 * empty). The caller is responsible for taking care of those events and
5505 * avoiding the call if inappropriate. The function does not call the
5506 * connection's polling update function, so the caller is responsible for this.
5507 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005508static size_t ssl_sock_to_buf(struct connection *conn, void *xprt_ctx, struct buffer *buf, size_t count, int flags)
Emeric Brun46591952012-05-18 15:47:34 +02005509{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005510 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02005511 ssize_t ret;
5512 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02005513
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005514 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005515 goto out_error;
5516
Olivier Houchard54907bb2019-12-19 15:02:39 +01005517#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
5518 if (b_data(&ctx->early_buf)) {
5519 try = b_contig_space(buf);
5520 if (try > b_data(&ctx->early_buf))
5521 try = b_data(&ctx->early_buf);
5522 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
5523 b_add(buf, try);
5524 b_del(&ctx->early_buf, try);
5525 if (b_data(&ctx->early_buf) == 0)
5526 b_free(&ctx->early_buf);
5527 return try;
5528 }
5529#endif
5530
Willy Tarreau911db9b2020-01-23 16:27:54 +01005531 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02005532 /* a handshake was requested */
5533 return 0;
5534
Emeric Brun46591952012-05-18 15:47:34 +02005535 /* read the largest possible block. For this, we perform only one call
5536 * to recv() unless the buffer wraps and we exactly fill the first hunk,
5537 * in which case we accept to do it once again. A new attempt is made on
5538 * EINTR too.
5539 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01005540 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02005541
Willy Tarreau591d4452018-06-15 17:21:00 +02005542 try = b_contig_space(buf);
5543 if (!try)
5544 break;
5545
Willy Tarreauabf08d92014-01-14 11:31:27 +01005546 if (try > count)
5547 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02005548
Olivier Houchard66ab4982019-02-26 18:37:15 +01005549 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02005550
Emeric Brune1f38db2012-09-03 20:36:47 +02005551 if (conn->flags & CO_FL_ERROR) {
5552 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01005553 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02005554 }
Emeric Brun46591952012-05-18 15:47:34 +02005555 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02005556 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02005557 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02005558 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02005559 }
Emeric Brun46591952012-05-18 15:47:34 +02005560 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005561 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02005562 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01005563 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02005564 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005565 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02005566#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005567 /* Async mode can be re-enabled, because we're leaving data state.*/
5568 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005569 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005570#endif
Emeric Brun46591952012-05-18 15:47:34 +02005571 break;
5572 }
5573 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005574 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005575 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
5576 SUB_RETRY_RECV,
5577 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01005578 /* handshake is running, and it may need to re-enable read */
5579 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005580#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005581 /* Async mode can be re-enabled, because we're leaving data state.*/
5582 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005583 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005584#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01005585 break;
5586 }
Emeric Brun46591952012-05-18 15:47:34 +02005587 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02005588 } else if (ret == SSL_ERROR_ZERO_RETURN)
5589 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01005590 /* For SSL_ERROR_SYSCALL, make sure to clear the error
5591 * stack before shutting down the connection for
5592 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01005593 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
5594 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02005595 /* otherwise it's a real error */
5596 goto out_error;
5597 }
5598 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005599 leave:
Emeric Brun46591952012-05-18 15:47:34 +02005600 return done;
5601
Christopher Faulet4ac77a92018-02-19 14:25:15 +01005602 clear_ssl_error:
5603 /* Clear openssl global errors stack */
5604 ssl_sock_dump_errors(conn);
5605 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02005606 read0:
5607 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005608 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01005609
Emeric Brun46591952012-05-18 15:47:34 +02005610 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01005611 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01005612 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02005613 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01005614 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005615 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02005616}
5617
5618
Willy Tarreau787db9a2018-06-14 18:31:46 +02005619/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
5620 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
5621 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02005622 * Only one call to send() is performed, unless the buffer wraps, in which case
5623 * a second call may be performed. The connection's flags are updated with
5624 * whatever special event is detected (error, empty). The caller is responsible
5625 * for taking care of those events and avoiding the call if inappropriate. The
5626 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02005627 * is responsible for this. The buffer's output is not adjusted, it's up to the
5628 * caller to take care of this. It's up to the caller to update the buffer's
5629 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02005630 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005631static size_t ssl_sock_from_buf(struct connection *conn, void *xprt_ctx, const struct buffer *buf, size_t count, int flags)
Emeric Brun46591952012-05-18 15:47:34 +02005632{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005633 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02005634 ssize_t ret;
5635 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02005636
5637 done = 0;
5638
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005639 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005640 goto out_error;
5641
Willy Tarreau911db9b2020-01-23 16:27:54 +01005642 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02005643 /* a handshake was requested */
5644 return 0;
5645
5646 /* send the largest possible block. For this we perform only one call
5647 * to send() unless the buffer wraps and we exactly fill the first hunk,
5648 * in which case we accept to do it once again.
5649 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02005650 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02005651#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02005652 size_t written_data;
5653#endif
5654
Willy Tarreau787db9a2018-06-14 18:31:46 +02005655 try = b_contig_data(buf, done);
5656 if (try > count)
5657 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01005658
Willy Tarreau7bed9452014-02-02 02:00:24 +01005659 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005660 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01005661 global_ssl.max_record && try > global_ssl.max_record) {
5662 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01005663 }
5664 else {
5665 /* we need to keep the information about the fact that
5666 * we're not limiting the upcoming send(), because if it
5667 * fails, we'll have to retry with at least as many data.
5668 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005669 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01005670 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01005671
Willy Tarreau5db847a2019-05-09 14:13:35 +02005672#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02005673 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02005674 unsigned int max_early;
5675
Olivier Houchard522eea72017-11-03 16:27:47 +01005676 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01005677 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01005678 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005679 if (SSL_get0_session(ctx->ssl))
5680 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01005681 else
5682 max_early = 0;
5683 }
5684
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005685 if (try + ctx->sent_early_data > max_early) {
5686 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01005687 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02005688 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02005689 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005690 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01005691 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02005692 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005693 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005694 if (ret == 1) {
5695 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005696 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02005697 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01005698 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02005699 /* Initiate the handshake, now */
5700 tasklet_wakeup(ctx->wait_event.tasklet);
5701 }
Olivier Houchard522eea72017-11-03 16:27:47 +01005702
Olivier Houchardc2aae742017-09-22 18:26:28 +02005703 }
5704
5705 } else
5706#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01005707 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01005708
Emeric Brune1f38db2012-09-03 20:36:47 +02005709 if (conn->flags & CO_FL_ERROR) {
5710 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01005711 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02005712 }
Emeric Brun46591952012-05-18 15:47:34 +02005713 if (ret > 0) {
Willy Tarreauc192b0a2020-01-23 09:11:58 +01005714 /* A send succeeded, so we can consider ourself connected */
5715 conn->flags &= ~CO_FL_WAIT_L4L6;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005716 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02005717 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02005718 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02005719 }
5720 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005721 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005722
Emeric Brun46591952012-05-18 15:47:34 +02005723 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005724 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01005725 /* handshake is running, and it may need to re-enable write */
5726 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005727 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02005728#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005729 /* Async mode can be re-enabled, because we're leaving data state.*/
5730 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005731 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005732#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01005733 break;
5734 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02005735
Emeric Brun46591952012-05-18 15:47:34 +02005736 break;
5737 }
5738 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01005739 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02005740 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005741 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
5742 SUB_RETRY_RECV,
5743 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02005744#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005745 /* Async mode can be re-enabled, because we're leaving data state.*/
5746 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005747 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005748#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005749 break;
5750 }
Emeric Brun46591952012-05-18 15:47:34 +02005751 goto out_error;
5752 }
5753 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005754 leave:
Emeric Brun46591952012-05-18 15:47:34 +02005755 return done;
5756
5757 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01005758 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02005759 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01005760 ERR_clear_error();
5761
Emeric Brun46591952012-05-18 15:47:34 +02005762 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005763 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02005764}
5765
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005766static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02005767
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005768 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005769
Olivier Houchardea8dd942019-05-20 14:02:16 +02005770
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005771 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005772 if (ctx->wait_event.events != 0)
5773 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
5774 ctx->wait_event.events,
5775 &ctx->wait_event);
Willy Tarreau113d52b2020-01-10 09:20:26 +01005776 if (ctx->subs) {
5777 ctx->subs->events = 0;
5778 tasklet_wakeup(ctx->subs->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005779 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01005780
Olivier Houchard692c1d02019-05-23 18:41:47 +02005781 if (ctx->xprt->close)
5782 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02005783#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02005784 if (global_ssl.async) {
5785 OSSL_ASYNC_FD all_fd[32], afd;
5786 size_t num_all_fds = 0;
5787 int i;
5788
Olivier Houchard66ab4982019-02-26 18:37:15 +01005789 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02005790 if (num_all_fds > 32) {
5791 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
5792 return;
5793 }
5794
Olivier Houchard66ab4982019-02-26 18:37:15 +01005795 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02005796
5797 /* If an async job is pending, we must try to
5798 to catch the end using polling before calling
5799 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005800 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02005801 for (i=0 ; i < num_all_fds ; i++) {
5802 /* switch on an handler designed to
5803 * handle the SSL_free
5804 */
5805 afd = all_fd[i];
5806 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005807 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02005808 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00005809 /* To ensure that the fd cache won't be used
5810 * and we'll catch a real RD event.
5811 */
5812 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02005813 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005814 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005815 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005816 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005817 return;
5818 }
Emeric Brun3854e012017-05-17 20:42:48 +02005819 /* Else we can remove the fds from the fdtab
5820 * and call SSL_free.
5821 * note: we do a fd_remove and not a delete
5822 * because the fd is owned by the engine.
5823 * the engine is responsible to close
5824 */
5825 for (i=0 ; i < num_all_fds ; i++)
5826 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005827 }
5828#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01005829 SSL_free(ctx->ssl);
Olivier Houchard54907bb2019-12-19 15:02:39 +01005830 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005831 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005832 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005833 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02005834 }
Emeric Brun46591952012-05-18 15:47:34 +02005835}
5836
5837/* This function tries to perform a clean shutdown on an SSL connection, and in
5838 * any case, flags the connection as reusable if no handshake was in progress.
5839 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005840static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02005841{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005842 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005843
Willy Tarreau911db9b2020-01-23 16:27:54 +01005844 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02005845 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01005846 if (!clean)
5847 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005848 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02005849 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005850 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01005851 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02005852 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01005853 ERR_clear_error();
5854 }
Emeric Brun46591952012-05-18 15:47:34 +02005855}
5856
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01005857
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05005858/* used for ppv2 pkey algo (can be used for logging) */
William Lallemandd4f946c2019-12-05 10:26:40 +01005859int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
5860{
5861 struct ssl_sock_ctx *ctx;
5862 X509 *crt;
5863
5864 if (!ssl_sock_is_ssl(conn))
5865 return 0;
5866
5867 ctx = conn->xprt_ctx;
5868
5869 crt = SSL_get_certificate(ctx->ssl);
5870 if (!crt)
5871 return 0;
5872
5873 return cert_get_pkey_algo(crt, out);
5874}
5875
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01005876/* used for ppv2 cert signature (can be used for logging) */
5877const char *ssl_sock_get_cert_sig(struct connection *conn)
5878{
Christopher Faulet82004142019-09-10 10:12:03 +02005879 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005880
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01005881 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
5882 X509 *crt;
5883
5884 if (!ssl_sock_is_ssl(conn))
5885 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02005886 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005887 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01005888 if (!crt)
5889 return NULL;
5890 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
5891 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
5892}
5893
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01005894/* used for ppv2 authority */
5895const char *ssl_sock_get_sni(struct connection *conn)
5896{
5897#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02005898 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005899
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01005900 if (!ssl_sock_is_ssl(conn))
5901 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02005902 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005903 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01005904#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01005905 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01005906#endif
5907}
5908
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02005909/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02005910const char *ssl_sock_get_cipher_name(struct connection *conn)
5911{
Christopher Faulet82004142019-09-10 10:12:03 +02005912 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005913
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02005914 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02005915 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02005916 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005917 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02005918}
5919
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02005920/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02005921const char *ssl_sock_get_proto_version(struct connection *conn)
5922{
Christopher Faulet82004142019-09-10 10:12:03 +02005923 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005924
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02005925 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02005926 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02005927 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005928 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02005929}
5930
Olivier Houchardab28a322018-12-21 19:45:40 +01005931void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
5932{
5933#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet82004142019-09-10 10:12:03 +02005934 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005935
Olivier Houcharde488ea82019-06-28 14:10:33 +02005936 if (!ssl_sock_is_ssl(conn))
5937 return;
Christopher Faulet82004142019-09-10 10:12:03 +02005938 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005939 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01005940#endif
5941}
5942
Willy Tarreau119a4082016-12-22 21:58:38 +01005943/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
5944 * to disable SNI.
5945 */
Willy Tarreau63076412015-07-10 11:33:32 +02005946void ssl_sock_set_servername(struct connection *conn, const char *hostname)
5947{
5948#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02005949 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005950
Willy Tarreau119a4082016-12-22 21:58:38 +01005951 char *prev_name;
5952
Willy Tarreau63076412015-07-10 11:33:32 +02005953 if (!ssl_sock_is_ssl(conn))
5954 return;
Christopher Faulet82004142019-09-10 10:12:03 +02005955 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02005956
Willy Tarreau119a4082016-12-22 21:58:38 +01005957 /* if the SNI changes, we must destroy the reusable context so that a
5958 * new connection will present a new SNI. As an optimization we could
5959 * later imagine having a small cache of ssl_ctx to hold a few SNI per
5960 * server.
5961 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005962 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01005963 if ((!prev_name && hostname) ||
5964 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01005965 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01005966
Olivier Houchard66ab4982019-02-26 18:37:15 +01005967 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02005968#endif
5969}
5970
Emeric Brun0abf8362014-06-24 18:26:41 +02005971/* Extract peer certificate's common name into the chunk dest
5972 * Returns
5973 * the len of the extracted common name
5974 * or 0 if no CN found in DN
5975 * or -1 on error case (i.e. no peer certificate)
5976 */
Willy Tarreau83061a82018-07-13 11:56:34 +02005977int ssl_sock_get_remote_common_name(struct connection *conn,
5978 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04005979{
Christopher Faulet82004142019-09-10 10:12:03 +02005980 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04005981 X509 *crt = NULL;
5982 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04005983 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02005984 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005985 .area = (char *)&find_cn,
5986 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04005987 };
Emeric Brun0abf8362014-06-24 18:26:41 +02005988 int result = -1;
David Safb76832014-05-08 23:42:08 -04005989
5990 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02005991 goto out;
Christopher Faulet82004142019-09-10 10:12:03 +02005992 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04005993
5994 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005995 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04005996 if (!crt)
5997 goto out;
5998
5999 name = X509_get_subject_name(crt);
6000 if (!name)
6001 goto out;
David Safb76832014-05-08 23:42:08 -04006002
Emeric Brun0abf8362014-06-24 18:26:41 +02006003 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
6004out:
David Safb76832014-05-08 23:42:08 -04006005 if (crt)
6006 X509_free(crt);
6007
6008 return result;
6009}
6010
Dave McCowan328fb582014-07-30 10:39:13 -04006011/* returns 1 if client passed a certificate for this session, 0 if not */
6012int ssl_sock_get_cert_used_sess(struct connection *conn)
6013{
Christopher Faulet82004142019-09-10 10:12:03 +02006014 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04006015 X509 *crt = NULL;
6016
6017 if (!ssl_sock_is_ssl(conn))
6018 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02006019 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04006020
6021 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006022 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04006023 if (!crt)
6024 return 0;
6025
6026 X509_free(crt);
6027 return 1;
6028}
6029
6030/* returns 1 if client passed a certificate for this connection, 0 if not */
6031int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04006032{
Christopher Faulet82004142019-09-10 10:12:03 +02006033 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006034
David Safb76832014-05-08 23:42:08 -04006035 if (!ssl_sock_is_ssl(conn))
6036 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02006037 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006038 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04006039}
6040
6041/* returns result from SSL verify */
6042unsigned int ssl_sock_get_verify_result(struct connection *conn)
6043{
Christopher Faulet82004142019-09-10 10:12:03 +02006044 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006045
David Safb76832014-05-08 23:42:08 -04006046 if (!ssl_sock_is_ssl(conn))
6047 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet82004142019-09-10 10:12:03 +02006048 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006049 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04006050}
6051
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006052/* Returns the application layer protocol name in <str> and <len> when known.
6053 * Zero is returned if the protocol name was not found, otherwise non-zero is
6054 * returned. The string is allocated in the SSL context and doesn't have to be
6055 * freed by the caller. NPN is also checked if available since older versions
6056 * of openssl (1.0.1) which are more common in field only support this one.
6057 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006058static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006059{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006060#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
6061 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006062 struct ssl_sock_ctx *ctx = xprt_ctx;
6063 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006064 return 0;
6065
6066 *str = NULL;
6067
6068#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01006069 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006070 if (*str)
6071 return 1;
6072#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01006073#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006074 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006075 if (*str)
6076 return 1;
6077#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006078#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006079 return 0;
6080}
6081
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01006082/* "issuers-chain-path" load chain certificate in global */
William Lallemanddad31052020-05-14 17:47:32 +02006083int ssl_load_global_issuer_from_BIO(BIO *in, char *fp, char **err)
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01006084{
6085 X509 *ca;
6086 X509_NAME *name = NULL;
6087 ASN1_OCTET_STRING *skid = NULL;
6088 STACK_OF(X509) *chain = NULL;
6089 struct issuer_chain *issuer;
6090 struct eb64_node *node;
6091 char *path;
6092 u64 key;
6093 int ret = 0;
6094
6095 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
6096 if (chain == NULL) {
6097 chain = sk_X509_new_null();
6098 skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL);
6099 name = X509_get_subject_name(ca);
6100 }
6101 if (!sk_X509_push(chain, ca)) {
6102 X509_free(ca);
6103 goto end;
6104 }
6105 }
6106 if (!chain) {
6107 memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp);
6108 goto end;
6109 }
6110 if (!skid) {
6111 memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp);
6112 goto end;
6113 }
6114 if (!name) {
6115 memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp);
6116 goto end;
6117 }
6118 key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0);
William Lallemande0f3fd52020-02-25 14:53:06 +01006119 for (node = eb64_lookup(&cert_issuer_tree, key); node; node = eb64_next(node)) {
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01006120 issuer = container_of(node, typeof(*issuer), node);
6121 if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) {
6122 memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path);
6123 goto end;
6124 }
6125 }
6126 issuer = calloc(1, sizeof *issuer);
6127 path = strdup(fp);
6128 if (!issuer || !path) {
6129 free(issuer);
6130 free(path);
6131 goto end;
6132 }
6133 issuer->node.key = key;
6134 issuer->path = path;
6135 issuer->chain = chain;
6136 chain = NULL;
William Lallemande0f3fd52020-02-25 14:53:06 +01006137 eb64_insert(&cert_issuer_tree, &issuer->node);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01006138 ret = 1;
6139 end:
6140 if (skid)
6141 ASN1_OCTET_STRING_free(skid);
6142 if (chain)
6143 sk_X509_pop_free(chain, X509_free);
6144 return ret;
6145}
6146
William Lallemandda8584c2020-05-14 10:14:37 +02006147 struct issuer_chain* ssl_get0_issuer_chain(X509 *cert)
Emmanuel Hocdet75a7aa12020-02-18 15:19:24 +01006148{
6149 AUTHORITY_KEYID *akid;
6150 struct issuer_chain *issuer = NULL;
6151
6152 akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL);
6153 if (akid) {
6154 struct eb64_node *node;
6155 u64 hk;
6156 hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0);
6157 for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) {
6158 struct issuer_chain *ti = container_of(node, typeof(*issuer), node);
6159 if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) {
6160 issuer = ti;
6161 break;
6162 }
6163 }
6164 AUTHORITY_KEYID_free(akid);
6165 }
6166 return issuer;
6167}
6168
William Lallemanddad31052020-05-14 17:47:32 +02006169void ssl_free_global_issuers(void)
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01006170{
6171 struct eb64_node *node, *back;
6172 struct issuer_chain *issuer;
6173
William Lallemande0f3fd52020-02-25 14:53:06 +01006174 node = eb64_first(&cert_issuer_tree);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01006175 while (node) {
6176 issuer = container_of(node, typeof(*issuer), node);
6177 back = eb64_next(node);
6178 eb64_delete(node);
6179 free(issuer->path);
6180 sk_X509_pop_free(issuer->chain, X509_free);
6181 free(issuer);
6182 node = back;
6183 }
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006184}
6185
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02006186#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006187static int ssl_check_async_engine_count(void) {
6188 int err_code = 0;
6189
Emeric Brun3854e012017-05-17 20:42:48 +02006190 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006191 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006192 err_code = ERR_ABORT;
6193 }
6194 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01006195}
Willy Tarreau9ceda382016-12-21 23:13:03 +01006196#endif
6197
William Lallemand32af2032016-10-29 18:09:35 +02006198/* This function is used with TLS ticket keys management. It permits to browse
6199 * each reference. The variable <getnext> must contain the current node,
6200 * <end> point to the root node.
6201 */
6202#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
6203static inline
6204struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
6205{
6206 struct tls_keys_ref *ref = getnext;
6207
6208 while (1) {
6209
6210 /* Get next list entry. */
6211 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
6212
6213 /* If the entry is the last of the list, return NULL. */
6214 if (&ref->list == end)
6215 return NULL;
6216
6217 return ref;
6218 }
6219}
6220
6221static inline
6222struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
6223{
6224 int id;
6225 char *error;
6226
6227 /* If the reference starts by a '#', this is numeric id. */
6228 if (reference[0] == '#') {
6229 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
6230 id = strtol(reference + 1, &error, 10);
6231 if (*error != '\0')
6232 return NULL;
6233
6234 /* Perform the unique id lookup. */
6235 return tlskeys_ref_lookupid(id);
6236 }
6237
6238 /* Perform the string lookup. */
6239 return tlskeys_ref_lookup(reference);
6240}
6241#endif
6242
6243
6244#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
6245
6246static int cli_io_handler_tlskeys_files(struct appctx *appctx);
6247
6248static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
6249 return cli_io_handler_tlskeys_files(appctx);
6250}
6251
Willy Tarreauf5f26e82016-12-16 18:47:27 +01006252/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
6253 * (next index to be dumped), and cli.p0 (next key reference).
6254 */
William Lallemand32af2032016-10-29 18:09:35 +02006255static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
6256
6257 struct stream_interface *si = appctx->owner;
6258
6259 switch (appctx->st2) {
6260 case STAT_ST_INIT:
6261 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -08006262 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +02006263 * later and restart at the state "STAT_ST_INIT".
6264 */
6265 chunk_reset(&trash);
6266
6267 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
6268 chunk_appendf(&trash, "# id secret\n");
6269 else
6270 chunk_appendf(&trash, "# id (file)\n");
6271
Willy Tarreau06d80a92017-10-19 14:32:15 +02006272 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01006273 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02006274 return 0;
6275 }
6276
William Lallemand32af2032016-10-29 18:09:35 +02006277 /* Now, we start the browsing of the references lists.
6278 * Note that the following call to LIST_ELEM return bad pointer. The only
6279 * available field of this pointer is <list>. It is used with the function
6280 * tlskeys_list_get_next() for retruning the first available entry
6281 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01006282 if (appctx->ctx.cli.p0 == NULL) {
6283 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
6284 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +02006285 }
6286
6287 appctx->st2 = STAT_ST_LIST;
6288 /* fall through */
6289
6290 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +01006291 while (appctx->ctx.cli.p0) {
6292 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +02006293
6294 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +01006295 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +02006296 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +01006297
6298 if (appctx->ctx.cli.i1 == 0)
6299 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
6300
William Lallemand32af2032016-10-29 18:09:35 +02006301 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +01006302 int head;
6303
6304 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
6305 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +01006306 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +02006307 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +02006308
6309 chunk_reset(t2);
6310 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +01006311 if (ref->key_size_bits == 128) {
6312 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
6313 sizeof(struct tls_sess_key_128),
6314 t2->area, t2->size);
6315 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
6316 t2->area);
6317 }
6318 else if (ref->key_size_bits == 256) {
6319 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
6320 sizeof(struct tls_sess_key_256),
6321 t2->area, t2->size);
6322 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
6323 t2->area);
6324 }
6325 else {
6326 /* This case should never happen */
6327 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
6328 }
William Lallemand32af2032016-10-29 18:09:35 +02006329
Willy Tarreau06d80a92017-10-19 14:32:15 +02006330 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +02006331 /* let's try again later from this stream. We add ourselves into
6332 * this stream's users so that it can remove us upon termination.
6333 */
Christopher Faulet16f45c82018-02-16 11:23:49 +01006334 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +01006335 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02006336 return 0;
6337 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +01006338 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +02006339 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01006340 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +01006341 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +02006342 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02006343 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +02006344 /* let's try again later from this stream. We add ourselves into
6345 * this stream's users so that it can remove us upon termination.
6346 */
Willy Tarreaudb398432018-11-15 11:08:52 +01006347 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02006348 return 0;
6349 }
6350
Willy Tarreauf5f26e82016-12-16 18:47:27 +01006351 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +02006352 break;
6353
6354 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01006355 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +02006356 }
6357
6358 appctx->st2 = STAT_ST_FIN;
6359 /* fall through */
6360
6361 default:
6362 appctx->st2 = STAT_ST_FIN;
6363 return 1;
6364 }
6365 return 0;
6366}
6367
Willy Tarreauf5f26e82016-12-16 18:47:27 +01006368/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02006369static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +02006370{
William Lallemand32af2032016-10-29 18:09:35 +02006371 /* no parameter, shows only file list */
6372 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +01006373 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +02006374 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +01006375 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02006376 }
6377
6378 if (args[2][0] == '*') {
6379 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01006380 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +02006381 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +01006382 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +02006383 if (!appctx->ctx.cli.p0)
6384 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +02006385 }
William Lallemand32af2032016-10-29 18:09:35 +02006386 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +01006387 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02006388}
6389
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02006390static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +02006391{
Willy Tarreauf5f26e82016-12-16 18:47:27 +01006392 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +02006393 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +01006394
William Lallemand32af2032016-10-29 18:09:35 +02006395 /* Expect two parameters: the filename and the new new TLS key in encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +02006396 if (!*args[3] || !*args[4])
6397 return cli_err(appctx, "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n");
William Lallemand32af2032016-10-29 18:09:35 +02006398
Willy Tarreauf5f26e82016-12-16 18:47:27 +01006399 ref = tlskeys_ref_lookup_ref(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02006400 if (!ref)
6401 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +02006402
Willy Tarreau1c913e42018-08-22 05:26:57 +02006403 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +02006404 if (ret < 0)
6405 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Emeric Brun9e754772019-01-10 17:51:55 +01006406
Willy Tarreau1c913e42018-08-22 05:26:57 +02006407 trash.data = ret;
Willy Tarreau9d008692019-08-09 11:21:01 +02006408 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
6409 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
William Lallemand32af2032016-10-29 18:09:35 +02006410
Willy Tarreau9d008692019-08-09 11:21:01 +02006411 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +02006412}
William Lallemandd4f946c2019-12-05 10:26:40 +01006413#endif
William Lallemand419e6342020-04-08 12:05:39 +02006414
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02006415static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +02006416{
6417#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
6418 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +02006419 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +02006420
6421 if (!payload)
6422 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +02006423
6424 /* Expect one parameter: the new response in base64 encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +02006425 if (!*payload)
6426 return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n");
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +02006427
6428 /* remove \r and \n from the payload */
6429 for (i = 0, j = 0; payload[i]; i++) {
6430 if (payload[i] == '\r' || payload[i] == '\n')
6431 continue;
6432 payload[j++] = payload[i];
6433 }
6434 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +02006435
Willy Tarreau1c913e42018-08-22 05:26:57 +02006436 ret = base64dec(payload, j, trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +02006437 if (ret < 0)
6438 return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n");
William Lallemand32af2032016-10-29 18:09:35 +02006439
Willy Tarreau1c913e42018-08-22 05:26:57 +02006440 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +02006441 if (ssl_sock_update_ocsp_response(&trash, &err)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02006442 if (err)
6443 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
6444 else
6445 return cli_err(appctx, "Failed to update OCSP response.\n");
William Lallemand32af2032016-10-29 18:09:35 +02006446 }
Willy Tarreau9d008692019-08-09 11:21:01 +02006447
6448 return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +02006449#else
Willy Tarreau9d008692019-08-09 11:21:01 +02006450 return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n");
William Lallemand32af2032016-10-29 18:09:35 +02006451#endif
6452
Elliot Otchet71f82972020-01-15 08:12:14 -05006453}
6454
William Lallemand32af2032016-10-29 18:09:35 +02006455/* register cli keywords */
6456static struct cli_kw_list cli_kws = {{ },{
6457#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
6458 { { "show", "tls-keys", NULL }, "show tls-keys [id|*]: show tls keys references or dump tls ticket keys when id specified", cli_parse_show_tlskeys, NULL },
Lukas Tribusf4bbc432017-10-24 12:26:31 +02006459 { { "set", "ssl", "tls-key", NULL }, "set ssl tls-key [id|keyfile] <tlskey>: set the next TLS key for the <id> or <keyfile> listener to <tlskey>", cli_parse_set_tlskeys, NULL },
William Lallemand32af2032016-10-29 18:09:35 +02006460#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006461 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemand32af2032016-10-29 18:09:35 +02006462 { { NULL }, NULL, NULL, NULL }
6463}};
6464
Willy Tarreau0108d902018-11-25 19:14:37 +01006465INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +02006466
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006467/* transport-layer operations for SSL sockets */
William Lallemanddad31052020-05-14 17:47:32 +02006468struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +02006469 .snd_buf = ssl_sock_from_buf,
6470 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +01006471 .subscribe = ssl_subscribe,
6472 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +02006473 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +02006474 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +02006475 .rcv_pipe = NULL,
6476 .snd_pipe = NULL,
6477 .shutr = NULL,
6478 .shutw = ssl_sock_shutw,
6479 .close = ssl_sock_close,
6480 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +01006481 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +01006482 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +01006483 .prepare_srv = ssl_sock_prepare_srv_ctx,
6484 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006485 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +01006486 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +02006487};
6488
Olivier Houchardccaa7de2017-10-02 11:51:03 +02006489enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
6490 struct session *sess, struct stream *s, int flags)
6491{
6492 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +01006493 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +02006494
6495 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +01006496 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +02006497
Olivier Houchard6fa63d92017-11-27 18:41:32 +01006498 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +02006499 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +01006500 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +02006501 s->req.flags |= CF_READ_NULL;
6502 return ACT_RET_YIELD;
6503 }
6504 }
6505 return (ACT_RET_CONT);
6506}
6507
6508static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
6509{
6510 rule->action_ptr = ssl_action_wait_for_hs;
6511
6512 return ACT_RET_PRS_OK;
6513}
6514
6515static struct action_kw_list http_req_actions = {ILH, {
6516 { "wait-for-handshake", ssl_parse_wait_for_hs },
6517 { /* END */ }
6518}};
6519
Willy Tarreau0108d902018-11-25 19:14:37 +01006520INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
6521
Willy Tarreau5db847a2019-05-09 14:13:35 +02006522#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01006523
6524static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
6525{
6526 if (ptr) {
6527 chunk_destroy(ptr);
6528 free(ptr);
6529 }
6530}
6531
6532#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +01006533static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
6534{
Willy Tarreaubafbe012017-11-24 17:34:44 +01006535 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +01006536}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01006537
Emeric Brun46591952012-05-18 15:47:34 +02006538__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +02006539static void __ssl_sock_init(void)
6540{
Ilya Shipitsin0590f442019-05-25 19:30:50 +05006541#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +02006542 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +05006543 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +05006544#endif
Emeric Brun46591952012-05-18 15:47:34 +02006545
Willy Tarreauef934602016-12-22 23:12:01 +01006546 if (global_ssl.listen_default_ciphers)
6547 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
6548 if (global_ssl.connect_default_ciphers)
6549 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02006550#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006551 if (global_ssl.listen_default_ciphersuites)
6552 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
6553 if (global_ssl.connect_default_ciphersuites)
6554 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
6555#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +01006556
Willy Tarreau13e14102016-12-22 20:25:26 +01006557 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02006558#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +02006559 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -08006560#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +05006561#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +02006562 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +05006563 n = sk_SSL_COMP_num(cm);
6564 while (n--) {
6565 (void) sk_SSL_COMP_pop(cm);
6566 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +05006567#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +05006568
Willy Tarreau5db847a2019-05-09 14:13:35 +02006569#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +02006570 ssl_locking_init();
6571#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +02006572#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01006573 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
6574#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +02006575 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +02006576 ssl_capture_ptr_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_capture_free_func);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02006577#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +00006578 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006579 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02006580#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +01006581#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
6582 hap_register_post_check(tlskeys_finalize_config);
6583#endif
Willy Tarreau80713382018-11-26 10:19:54 +01006584
6585 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
6586 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
6587
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01006588 hap_register_post_deinit(ssl_free_global_issuers);
6589
Willy Tarreau80713382018-11-26 10:19:54 +01006590#ifndef OPENSSL_NO_DH
6591 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
6592 hap_register_post_deinit(ssl_free_dh);
6593#endif
6594#ifndef OPENSSL_NO_ENGINE
6595 hap_register_post_deinit(ssl_free_engines);
6596#endif
6597 /* Load SSL string for the verbose & debug mode. */
6598 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +02006599 ha_meth = BIO_meth_new(0x666, "ha methods");
6600 BIO_meth_set_write(ha_meth, ha_ssl_write);
6601 BIO_meth_set_read(ha_meth, ha_ssl_read);
6602 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
6603 BIO_meth_set_create(ha_meth, ha_ssl_new);
6604 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
6605 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
6606 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
William Lallemand150bfa82019-09-19 17:12:49 +02006607
6608 HA_SPIN_INIT(&ckch_lock);
Dragan Dosen1e7ed042020-05-08 18:30:00 +02006609
Dragan Dosen9ac98092020-05-11 15:51:45 +02006610 /* Try to register dedicated SSL/TLS protocol message callbacks for
6611 * heartbleed attack (CVE-2014-0160) and clienthello.
6612 */
6613 hap_register_post_check(ssl_sock_register_msg_callbacks);
6614
Dragan Dosen1e7ed042020-05-08 18:30:00 +02006615 /* Try to free all callbacks that were registered by using
6616 * ssl_sock_register_msg_callback().
6617 */
6618 hap_register_post_deinit(ssl_sock_unregister_msg_callbacks);
Willy Tarreau80713382018-11-26 10:19:54 +01006619}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +01006620
Willy Tarreau80713382018-11-26 10:19:54 +01006621/* Compute and register the version string */
6622static void ssl_register_build_options()
6623{
6624 char *ptr = NULL;
6625 int i;
6626
Willy Tarreauc2c0b612016-12-21 19:23:20 +01006627 memprintf(&ptr, "Built with OpenSSL version : "
6628#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +01006629 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +01006630#else /* OPENSSL_IS_BORINGSSL */
6631 OPENSSL_VERSION_TEXT
6632 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -08006633 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +02006634 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +01006635#endif
6636 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +02006637#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +01006638 "no (library version too old)"
6639#elif defined(OPENSSL_NO_TLSEXT)
6640 "no (disabled via OPENSSL_NO_TLSEXT)"
6641#else
6642 "yes"
6643#endif
6644 "", ptr);
6645
6646 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
6647#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
6648 "yes"
6649#else
6650#ifdef OPENSSL_NO_TLSEXT
6651 "no (because of OPENSSL_NO_TLSEXT)"
6652#else
6653 "no (version might be too old, 0.9.8f min needed)"
6654#endif
6655#endif
6656 "", ptr);
6657
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +02006658 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
6659 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
6660 if (methodVersions[i].option)
6661 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +01006662
Willy Tarreauc2c0b612016-12-21 19:23:20 +01006663 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +01006664}
Willy Tarreauc2c0b612016-12-21 19:23:20 +01006665
Willy Tarreau80713382018-11-26 10:19:54 +01006666INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +02006667
Emeric Brun46591952012-05-18 15:47:34 +02006668
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02006669#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +00006670void ssl_free_engines(void) {
6671 struct ssl_engine_list *wl, *wlb;
6672 /* free up engine list */
6673 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
6674 ENGINE_finish(wl->e);
6675 ENGINE_free(wl->e);
6676 LIST_DEL(&wl->list);
6677 free(wl);
6678 }
6679}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02006680#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +02006681
Remi Gacogned3a23c32015-05-28 16:39:47 +02006682#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +00006683void ssl_free_dh(void) {
6684 if (local_dh_1024) {
6685 DH_free(local_dh_1024);
6686 local_dh_1024 = NULL;
6687 }
6688 if (local_dh_2048) {
6689 DH_free(local_dh_2048);
6690 local_dh_2048 = NULL;
6691 }
6692 if (local_dh_4096) {
6693 DH_free(local_dh_4096);
6694 local_dh_4096 = NULL;
6695 }
Remi Gacogne47783ef2015-05-29 15:53:22 +02006696 if (global_dh) {
6697 DH_free(global_dh);
6698 global_dh = NULL;
6699 }
Grant Zhang872f9c22017-01-21 01:10:18 +00006700}
6701#endif
6702
6703__attribute__((destructor))
6704static void __ssl_sock_deinit(void)
6705{
6706#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02006707 if (ssl_ctx_lru_tree) {
6708 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01006709 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +02006710 }
Remi Gacogned3a23c32015-05-28 16:39:47 +02006711#endif
6712
Willy Tarreau5db847a2019-05-09 14:13:35 +02006713#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +02006714 ERR_remove_state(0);
6715 ERR_free_strings();
6716
6717 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -08006718#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +02006719
Willy Tarreau5db847a2019-05-09 14:13:35 +02006720#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +02006721 CRYPTO_cleanup_all_ex_data();
6722#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +02006723 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +02006724}
6725
Emeric Brun46591952012-05-18 15:47:34 +02006726/*
6727 * Local variables:
6728 * c-indent-level: 8
6729 * c-basic-offset: 8
6730 * End:
6731 */