blob: e637b0423a9af81111a9823152ee51eca46eb728 [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>
Emeric Brun46591952012-05-18 15:47:34 +020031#include <stdio.h>
32#include <stdlib.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020033#include <string.h>
34#include <unistd.h>
Emeric Brun46591952012-05-18 15:47:34 +020035
36#include <sys/socket.h>
37#include <sys/stat.h>
38#include <sys/types.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020039#include <netdb.h>
Emeric Brun46591952012-05-18 15:47:34 +020040#include <netinet/tcp.h>
41
Willy Tarreaub2551052020-06-09 09:07:15 +020042#include <import/ebpttree.h>
43#include <import/ebsttree.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020044#include <import/lru.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020045
Willy Tarreaub2551052020-06-09 09:07:15 +020046#include <haproxy/api.h>
Willy Tarreaua9380522022-05-05 08:50:17 +020047#include <haproxy/applet.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020048#include <haproxy/arg.h>
49#include <haproxy/base64.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020050#include <haproxy/channel.h>
Willy Tarreauc13ed532020-06-02 10:22:45 +020051#include <haproxy/chunk.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020052#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020053#include <haproxy/connection.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020054#include <haproxy/dynbuf.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020055#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020056#include <haproxy/fd.h>
57#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020058#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020059#include <haproxy/global.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020060#include <haproxy/http_rules.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020061#include <haproxy/log.h>
Willy Tarreau6019fab2020-05-27 16:26:00 +020062#include <haproxy/openssl-compat.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020063#include <haproxy/pattern-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020064#include <haproxy/proto_tcp.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020065#include <haproxy/proxy.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020066#include <haproxy/sample.h>
67#include <haproxy/sc_strm.h>
Amaury Denoyelle92fa63f2022-09-30 18:11:13 +020068#include <haproxy/quic_conn.h>
Frédéric Lécaille748ece62022-05-21 23:58:40 +020069#include <haproxy/quic_tp.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020070#include <haproxy/server.h>
Willy Tarreau334099c2020-06-03 18:38:48 +020071#include <haproxy/shctx.h>
Willy Tarreau47d7f902020-06-04 14:25:47 +020072#include <haproxy/ssl_ckch.h>
Willy Tarreau52d88722020-06-04 14:29:23 +020073#include <haproxy/ssl_crtlist.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020074#include <haproxy/ssl_sock.h>
Willy Tarreaub2bd8652020-06-04 14:21:22 +020075#include <haproxy/ssl_utils.h>
Amaury Denoyelle9963fa72020-11-03 17:10:00 +010076#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020077#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020078#include <haproxy/stream-t.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020079#include <haproxy/task.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020080#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020081#include <haproxy/time.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020082#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020083#include <haproxy/vars.h>
Tim Duesterhusd5fc8fc2021-09-11 17:51:13 +020084#include <haproxy/xxhash.h>
Remi Tricot-Le Breton2e7d1eb2022-01-11 10:11:10 +010085#include <haproxy/istbuf.h>
Remi Tricot-Le Bretonc8d814e2022-12-20 11:11:17 +010086#include <haproxy/ssl_ocsp.h>
Emeric Brun46591952012-05-18 15:47:34 +020087
Emeric Brun46591952012-05-18 15:47:34 +020088
Willy Tarreau9356dac2019-05-10 09:22:53 +020089/* ***** READ THIS before adding code here! *****
90 *
91 * Due to API incompatibilities between multiple OpenSSL versions and their
92 * derivatives, it's often tempting to add macros to (re-)define certain
93 * symbols. Please do not do this here, and do it in common/openssl-compat.h
94 * exclusively so that the whole code consistently uses the same macros.
95 *
96 * Whenever possible if a macro is missing in certain versions, it's better
97 * to conditionally define it in openssl-compat.h than using lots of ifdefs.
98 */
99
Emeric Brunece0c332017-12-06 13:51:49 +0100100int nb_engines = 0;
Emeric Brune1f38db2012-09-03 20:36:47 +0200101
William Lallemande0f3fd52020-02-25 14:53:06 +0100102static struct eb_root cert_issuer_tree = EB_ROOT; /* issuers tree from "issuers-chain-path" */
103
William Lallemand7fd8b452020-05-07 15:20:43 +0200104struct global_ssl global_ssl = {
Willy Tarreauef934602016-12-22 23:12:01 +0100105#ifdef LISTEN_DEFAULT_CIPHERS
106 .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS,
107#endif
108#ifdef CONNECT_DEFAULT_CIPHERS
109 .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS,
110#endif
Ilya Shipitsinf34ed0b2020-11-21 14:37:34 +0500111#ifdef HAVE_SSL_CTX_SET_CIPHERSUITES
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200112 .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES,
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200113 .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES,
114#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100115 .listen_default_ssloptions = BC_SSL_O_NONE,
116 .connect_default_ssloptions = SRV_SSL_O_NONE,
117
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200118 .listen_default_sslmethods.flags = MC_SSL_O_ALL,
119 .listen_default_sslmethods.min = CONF_TLSV_NONE,
120 .listen_default_sslmethods.max = CONF_TLSV_NONE,
121 .connect_default_sslmethods.flags = MC_SSL_O_ALL,
122 .connect_default_sslmethods.min = CONF_TLSV_NONE,
123 .connect_default_sslmethods.max = CONF_TLSV_NONE,
124
Willy Tarreauef934602016-12-22 23:12:01 +0100125#ifdef DEFAULT_SSL_MAX_RECORD
126 .max_record = DEFAULT_SSL_MAX_RECORD,
127#endif
Thomas Prückl10243932022-04-27 13:04:54 +0200128 .hard_max_record = 0,
Willy Tarreauef934602016-12-22 23:12:01 +0100129 .default_dh_param = SSL_DEFAULT_DH_PARAM,
130 .ctx_cache = DEFAULT_SSL_CTX_CACHE,
Marcin Deranek310a2602021-07-13 19:04:24 +0200131 .capture_buffer_size = 0,
William Lallemand3af48e72020-02-03 17:15:52 +0100132 .extra_files = SSL_GF_ALL,
William Lallemand8e8581e2020-10-20 17:36:46 +0200133 .extra_files_noext = 0,
William Lallemand722180a2021-06-09 16:46:12 +0200134#ifdef HAVE_SSL_KEYLOG
Remi Tricot-Le Breton58432372023-02-28 17:46:29 +0100135 .keylog = 0,
136#endif
137#ifndef OPENSSL_NO_OCSP
138 .ocsp_update.delay_max = SSL_OCSP_UPDATE_DELAY_MAX,
139 .ocsp_update.delay_min = SSL_OCSP_UPDATE_DELAY_MIN,
William Lallemand7d42ef52020-07-06 11:41:30 +0200140#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100141};
142
Olivier Houcharda8955d52019-04-07 22:00:38 +0200143static BIO_METHOD *ha_meth;
144
Willy Tarreaub8dec4a2022-06-23 11:02:08 +0200145DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx", sizeof(struct ssl_sock_ctx));
Olivier Houchard66ab4982019-02-26 18:37:15 +0100146
Willy Tarreaub8dec4a2022-06-23 11:02:08 +0200147DECLARE_STATIC_POOL(ssl_sock_client_sni_pool, "ssl_sock_client_sni", TLSEXT_MAXLEN_host_name + 1);
Remi Tricot-Le Bretona9967632022-01-07 17:12:01 +0100148
Amaury Denoyelle9963fa72020-11-03 17:10:00 +0100149/* ssl stats module */
150enum {
Amaury Denoyelled0447a72020-11-03 17:10:02 +0100151 SSL_ST_SESS,
152 SSL_ST_REUSED_SESS,
Amaury Denoyelle034c1622020-11-13 16:05:00 +0100153 SSL_ST_FAILED_HANDSHAKE,
Amaury Denoyellefbc33772020-11-03 17:10:01 +0100154
Amaury Denoyelle9963fa72020-11-03 17:10:00 +0100155 SSL_ST_STATS_COUNT /* must be the last member of the enum */
156};
157
158static struct name_desc ssl_stats[] = {
Amaury Denoyelle034c1622020-11-13 16:05:00 +0100159 [SSL_ST_SESS] = { .name = "ssl_sess",
160 .desc = "Total number of ssl sessions established" },
161 [SSL_ST_REUSED_SESS] = { .name = "ssl_reused_sess",
162 .desc = "Total number of ssl sessions reused" },
163 [SSL_ST_FAILED_HANDSHAKE] = { .name = "ssl_failed_handshake",
164 .desc = "Total number of failed handshake" },
Amaury Denoyelle9963fa72020-11-03 17:10:00 +0100165};
166
167static struct ssl_counters {
Amaury Denoyelled0447a72020-11-03 17:10:02 +0100168 long long sess;
169 long long reused_sess;
Amaury Denoyelle034c1622020-11-13 16:05:00 +0100170 long long failed_handshake;
Amaury Denoyelle9963fa72020-11-03 17:10:00 +0100171} ssl_counters;
172
173static void ssl_fill_stats(void *data, struct field *stats)
174{
Amaury Denoyellefbc33772020-11-03 17:10:01 +0100175 struct ssl_counters *counters = data;
176
Amaury Denoyelle034c1622020-11-13 16:05:00 +0100177 stats[SSL_ST_SESS] = mkf_u64(FN_COUNTER, counters->sess);
178 stats[SSL_ST_REUSED_SESS] = mkf_u64(FN_COUNTER, counters->reused_sess);
179 stats[SSL_ST_FAILED_HANDSHAKE] = mkf_u64(FN_COUNTER, counters->failed_handshake);
Amaury Denoyelle9963fa72020-11-03 17:10:00 +0100180}
181
182static struct stats_module ssl_stats_module = {
183 .name = "ssl",
184 .fill_stats = ssl_fill_stats,
185 .stats = ssl_stats,
186 .stats_count = SSL_ST_STATS_COUNT,
187 .counters = &ssl_counters,
188 .counters_size = sizeof(ssl_counters),
189 .domain_flags = MK_STATS_PROXY_DOMAIN(STATS_PX_CAP_FE|STATS_PX_CAP_LI|STATS_PX_CAP_BE|STATS_PX_CAP_SRV),
190 .clearable = 1,
191};
192
193INITCALL1(STG_REGISTER, stats_register_module, &ssl_stats_module);
194
Willy Tarreaua9380522022-05-05 08:50:17 +0200195/* CLI context for "show tls-keys" */
196struct show_keys_ctx {
197 struct tls_keys_ref *next_ref; /* next reference to be dumped */
198 int names_only; /* non-zero = only show file names */
199 int next_index; /* next index to be dumped */
Willy Tarreaubd338642022-05-05 08:59:17 +0200200 int dump_entries; /* dump entries also */
Willy Tarreau9c5a38c2022-05-05 09:03:44 +0200201 enum {
202 SHOW_KEYS_INIT = 0,
203 SHOW_KEYS_LIST,
204 SHOW_KEYS_DONE,
205 } state; /* phase of the current dump */
Willy Tarreaua9380522022-05-05 08:50:17 +0200206};
207
Willy Tarreau691d5032021-01-20 14:55:01 +0100208/* ssl_sock_io_cb is exported to see it resolved in "show fd" */
Willy Tarreau144f84a2021-03-02 16:09:26 +0100209struct task *ssl_sock_io_cb(struct task *, void *, unsigned int);
Olivier Houchard000694c2019-05-23 14:45:12 +0200210static int ssl_sock_handshake(struct connection *conn, unsigned int flag);
Olivier Houchardea8dd942019-05-20 14:02:16 +0200211
Olivier Houcharda8955d52019-04-07 22:00:38 +0200212/* Methods to implement OpenSSL BIO */
213static int ha_ssl_write(BIO *h, const char *buf, int num)
214{
215 struct buffer tmpbuf;
216 struct ssl_sock_ctx *ctx;
Willy Tarreauac78c4f2023-03-17 16:13:05 +0100217 uint flags;
Olivier Houcharda8955d52019-04-07 22:00:38 +0200218 int ret;
219
220 ctx = BIO_get_data(h);
221 tmpbuf.size = num;
222 tmpbuf.area = (void *)(uintptr_t)buf;
223 tmpbuf.data = num;
224 tmpbuf.head = 0;
Willy Tarreauac78c4f2023-03-17 16:13:05 +0100225 flags = (ctx->xprt_st & SSL_SOCK_SEND_MORE) ? CO_SFL_MSG_MORE : 0;
226 ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, flags);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200227 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200228 BIO_set_retry_write(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200229 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200230 } else if (ret == 0)
231 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200232 return ret;
233}
234
235static int ha_ssl_gets(BIO *h, char *buf, int size)
236{
237
238 return 0;
239}
240
241static int ha_ssl_puts(BIO *h, const char *str)
242{
243
244 return ha_ssl_write(h, str, strlen(str));
245}
246
247static int ha_ssl_read(BIO *h, char *buf, int size)
248{
249 struct buffer tmpbuf;
250 struct ssl_sock_ctx *ctx;
251 int ret;
252
253 ctx = BIO_get_data(h);
254 tmpbuf.size = size;
255 tmpbuf.area = buf;
256 tmpbuf.data = 0;
257 tmpbuf.head = 0;
258 ret = ctx->xprt->rcv_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, size, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200259 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200260 BIO_set_retry_read(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200261 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200262 } else if (ret == 0)
263 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200264
265 return ret;
266}
267
268static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2)
269{
270 int ret = 0;
271 switch (cmd) {
272 case BIO_CTRL_DUP:
273 case BIO_CTRL_FLUSH:
274 ret = 1;
275 break;
276 }
277 return ret;
278}
279
280static int ha_ssl_new(BIO *h)
281{
282 BIO_set_init(h, 1);
283 BIO_set_data(h, NULL);
284 BIO_clear_flags(h, ~0);
285 return 1;
286}
287
288static int ha_ssl_free(BIO *data)
289{
290
291 return 1;
292}
293
294
Willy Tarreau5db847a2019-05-09 14:13:35 +0200295#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100296
Emeric Brun821bb9b2017-06-15 16:37:39 +0200297static HA_RWLOCK_T *ssl_rwlocks;
298
299
300unsigned long ssl_id_function(void)
301{
302 return (unsigned long)tid;
303}
304
305void ssl_locking_function(int mode, int n, const char * file, int line)
306{
307 if (mode & CRYPTO_LOCK) {
308 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100309 HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200310 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100311 HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200312 }
313 else {
314 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100315 HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200316 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100317 HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200318 }
319}
320
321static int ssl_locking_init(void)
322{
323 int i;
324
325 ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks());
326 if (!ssl_rwlocks)
327 return -1;
328
329 for (i = 0 ; i < CRYPTO_num_locks() ; i++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100330 HA_RWLOCK_INIT(&ssl_rwlocks[i]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200331
332 CRYPTO_set_id_callback(ssl_id_function);
333 CRYPTO_set_locking_callback(ssl_locking_function);
334
335 return 0;
336}
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100337
Emeric Brun821bb9b2017-06-15 16:37:39 +0200338#endif
339
Willy Tarreauaf613e82020-06-05 08:40:51 +0200340__decl_thread(HA_SPINLOCK_T ckch_lock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200341
William Lallemandbc6ca7c2019-10-29 23:48:19 +0100342
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200343
344/* mimic what X509_STORE_load_locations do with store_ctx */
345static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path)
346{
Remi Tricot-Le Bretond75b99e2021-05-17 11:45:55 +0200347 X509_STORE *store = NULL;
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +0100348 struct cafile_entry *ca_e = ssl_store_get_cafile_entry(path, 0);
349 if (ca_e)
350 store = ca_e->ca_store;
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200351 if (store_ctx && store) {
352 int i;
353 X509_OBJECT *obj;
354 STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store);
355 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
356 obj = sk_X509_OBJECT_value(objs, i);
357 switch (X509_OBJECT_get_type(obj)) {
358 case X509_LU_X509:
359 X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj));
360 break;
361 case X509_LU_CRL:
362 X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj));
363 break;
364 default:
365 break;
366 }
367 }
368 return 1;
369 }
370 return 0;
371}
372
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +0500373/* SSL_CTX_load_verify_locations substitute, internally call X509_STORE_load_locations */
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200374static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path)
375{
376 X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx);
377 return ssl_set_cert_crl_file(store_ctx, path);
378}
379
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200380/*
381 Extract CA_list from CA_file already in tree.
382 Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility.
383 Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX.
384*/
385static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path)
386{
387 struct ebmb_node *eb;
388 struct cafile_entry *ca_e;
389
390 eb = ebst_lookup(&cafile_tree, path);
391 if (!eb)
392 return NULL;
393 ca_e = ebmb_entry(eb, struct cafile_entry, node);
394
395 if (ca_e->ca_list == NULL) {
396 int i;
397 unsigned long key;
398 struct eb_root ca_name_tree = EB_ROOT;
399 struct eb64_node *node, *back;
400 struct {
401 struct eb64_node node;
402 X509_NAME *xname;
403 } *ca_name;
404 STACK_OF(X509_OBJECT) *objs;
405 STACK_OF(X509_NAME) *skn;
406 X509 *x;
407 X509_NAME *xn;
408
409 skn = sk_X509_NAME_new_null();
410 /* take x509 from cafile_tree */
411 objs = X509_STORE_get0_objects(ca_e->ca_store);
412 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
413 x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
414 if (!x)
415 continue;
416 xn = X509_get_subject_name(x);
417 if (!xn)
418 continue;
419 /* Check for duplicates. */
420 key = X509_NAME_hash(xn);
421 for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL;
422 node && ca_name == NULL;
423 node = eb64_next(node)) {
424 ca_name = container_of(node, typeof(*ca_name), node);
425 if (X509_NAME_cmp(xn, ca_name->xname) != 0)
426 ca_name = NULL;
427 }
428 /* find a duplicate */
429 if (ca_name)
430 continue;
431 ca_name = calloc(1, sizeof *ca_name);
432 xn = X509_NAME_dup(xn);
433 if (!ca_name ||
434 !xn ||
435 !sk_X509_NAME_push(skn, xn)) {
436 free(ca_name);
437 X509_NAME_free(xn);
438 sk_X509_NAME_pop_free(skn, X509_NAME_free);
439 sk_X509_NAME_free(skn);
440 skn = NULL;
441 break;
442 }
443 ca_name->node.key = key;
444 ca_name->xname = xn;
445 eb64_insert(&ca_name_tree, &ca_name->node);
446 }
447 ca_e->ca_list = skn;
448 /* remove temporary ca_name tree */
449 node = eb64_first(&ca_name_tree);
450 while (node) {
451 ca_name = container_of(node, typeof(*ca_name), node);
452 back = eb64_next(node);
453 eb64_delete(node);
454 free(ca_name);
455 node = back;
456 }
457 }
458 return ca_e->ca_list;
459}
460
Willy Tarreauff882702021-04-10 17:23:00 +0200461struct pool_head *pool_head_ssl_capture __read_mostly = NULL;
William Lallemand15e16942020-05-15 00:25:08 +0200462int ssl_capture_ptr_index = -1;
Frédéric Lécaille901ee2f2020-11-23 11:19:04 +0100463int ssl_app_data_index = -1;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +0100464#ifdef USE_QUIC
465int ssl_qc_app_data_index = -1;
466#endif /* USE_QUIC */
Willy Tarreauef934602016-12-22 23:12:01 +0100467
William Lallemand722180a2021-06-09 16:46:12 +0200468#ifdef HAVE_SSL_KEYLOG
William Lallemand7d42ef52020-07-06 11:41:30 +0200469int ssl_keylog_index = -1;
Willy Tarreauff882702021-04-10 17:23:00 +0200470struct pool_head *pool_head_ssl_keylog __read_mostly = NULL;
471struct pool_head *pool_head_ssl_keylog_str __read_mostly = NULL;
William Lallemand7d42ef52020-07-06 11:41:30 +0200472#endif
473
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +0200474int ssl_client_crt_ref_index = -1;
475
Remi Tricot-Le Bretona9967632022-01-07 17:12:01 +0100476/* Used to store the client's SNI in case of ClientHello callback error */
477int ssl_client_sni_index = -1;
478
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200479#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
480struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference);
481#endif
482
William Lallemandd7bfbe22022-04-11 18:41:24 +0200483#if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE)
William Lallemanddad31052020-05-14 17:47:32 +0200484unsigned int openssl_engines_initialized;
Grant Zhang872f9c22017-01-21 01:10:18 +0000485struct list openssl_engines = LIST_HEAD_INIT(openssl_engines);
486struct ssl_engine_list {
487 struct list list;
488 ENGINE *e;
489};
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200490#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000491
Remi Tricot-Le Breton1746a382022-05-16 16:24:33 +0200492#ifdef HAVE_SSL_PROVIDERS
493struct list openssl_providers = LIST_HEAD_INIT(openssl_providers);
494struct ssl_provider_list {
495 struct list list;
496 OSSL_PROVIDER *provider;
497};
498#endif
499
Remi Gacogne8de54152014-07-15 11:36:40 +0200500#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +0200501static int ssl_dh_ptr_index = -1;
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +0100502static HASSL_DH *global_dh = NULL;
503static HASSL_DH *local_dh_1024 = NULL;
504static HASSL_DH *local_dh_2048 = NULL;
505static HASSL_DH *local_dh_4096 = NULL;
Remi Tricot-Le Breton88c56952022-02-11 12:04:56 +0100506#if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL)
Remi Tricot-Le Breton292a88c2022-02-11 12:04:49 +0100507static DH *ssl_get_tmp_dh_cbk(SSL *ssl, int export, int keylen);
Remi Tricot-Le Breton88c56952022-02-11 12:04:56 +0100508#else
509static void ssl_sock_set_tmp_dh_from_pkey(SSL_CTX *ctx, EVP_PKEY *pkey);
510#endif
Remi Gacogne8de54152014-07-15 11:36:40 +0200511#endif /* OPENSSL_NO_DH */
512
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100513#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +0200514/* X509V3 Extensions that will be added on generated certificates */
515#define X509V3_EXT_SIZE 5
516static char *x509v3_ext_names[X509V3_EXT_SIZE] = {
517 "basicConstraints",
518 "nsComment",
519 "subjectKeyIdentifier",
520 "authorityKeyIdentifier",
521 "keyUsage",
522};
523static char *x509v3_ext_values[X509V3_EXT_SIZE] = {
524 "CA:FALSE",
525 "\"OpenSSL Generated Certificate\"",
526 "hash",
527 "keyid,issuer:always",
528 "nonRepudiation,digitalSignature,keyEncipherment"
529};
Christopher Faulet31af49d2015-06-09 17:29:50 +0200530/* LRU cache to store generated certificate */
531static struct lru64_head *ssl_ctx_lru_tree = NULL;
532static unsigned int ssl_ctx_lru_seed = 0;
Emeric Brun821bb9b2017-06-15 16:37:39 +0200533static unsigned int ssl_ctx_serial;
Willy Tarreau86abe442018-11-25 20:12:18 +0100534__decl_rwlock(ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200535
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200536#endif // SSL_CTRL_SET_TLSEXT_HOSTNAME
537
yanbzhube2774d2015-12-10 15:07:30 -0500538/* The order here matters for picking a default context,
539 * keep the most common keytype at the bottom of the list
540 */
541const char *SSL_SOCK_KEYTYPE_NAMES[] = {
542 "dsa",
543 "ecdsa",
544 "rsa"
545};
yanbzhube2774d2015-12-10 15:07:30 -0500546
William Lallemandc3cd35f2017-11-28 11:04:43 +0100547static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */
William Lallemand4f45bb92017-10-30 20:08:51 +0100548static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */
549
Dragan Dosen9ac98092020-05-11 15:51:45 +0200550/* Dedicated callback functions for heartbeat and clienthello.
551 */
552#ifdef TLS1_RT_HEARTBEAT
553static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version,
554 int content_type, const void *buf, size_t len,
555 SSL *ssl);
556#endif
557static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version,
558 int content_type, const void *buf, size_t len,
559 SSL *ssl);
560
William Lallemand722180a2021-06-09 16:46:12 +0200561#ifdef HAVE_SSL_KEYLOG
William Lallemand7d42ef52020-07-06 11:41:30 +0200562static void ssl_init_keylog(struct connection *conn, int write_p, int version,
563 int content_type, const void *buf, size_t len,
564 SSL *ssl);
565#endif
566
Dragan Dosen1e7ed042020-05-08 18:30:00 +0200567/* List head of all registered SSL/TLS protocol message callbacks. */
568struct list ssl_sock_msg_callbacks = LIST_HEAD_INIT(ssl_sock_msg_callbacks);
569
570/* Registers the function <func> in order to be called on SSL/TLS protocol
571 * message processing. It will return 0 if the function <func> is not set
572 * or if it fails to allocate memory.
573 */
574int ssl_sock_register_msg_callback(ssl_sock_msg_callback_func func)
575{
576 struct ssl_sock_msg_callback *cbk;
577
578 if (!func)
579 return 0;
580
581 cbk = calloc(1, sizeof(*cbk));
582 if (!cbk) {
583 ha_alert("out of memory in ssl_sock_register_msg_callback().\n");
584 return 0;
585 }
586
587 cbk->func = func;
588
Willy Tarreau2b718102021-04-21 07:32:39 +0200589 LIST_APPEND(&ssl_sock_msg_callbacks, &cbk->list);
Dragan Dosen1e7ed042020-05-08 18:30:00 +0200590
591 return 1;
592}
593
Dragan Dosen9ac98092020-05-11 15:51:45 +0200594/* Used to register dedicated SSL/TLS protocol message callbacks.
595 */
596static int ssl_sock_register_msg_callbacks(void)
597{
598#ifdef TLS1_RT_HEARTBEAT
599 if (!ssl_sock_register_msg_callback(ssl_sock_parse_heartbeat))
600 return ERR_ABORT;
601#endif
Marcin Deranek310a2602021-07-13 19:04:24 +0200602 if (global_ssl.capture_buffer_size > 0) {
Dragan Dosen9ac98092020-05-11 15:51:45 +0200603 if (!ssl_sock_register_msg_callback(ssl_sock_parse_clienthello))
604 return ERR_ABORT;
605 }
William Lallemand722180a2021-06-09 16:46:12 +0200606#ifdef HAVE_SSL_KEYLOG
William Lallemand7d42ef52020-07-06 11:41:30 +0200607 if (global_ssl.keylog > 0) {
608 if (!ssl_sock_register_msg_callback(ssl_init_keylog))
609 return ERR_ABORT;
610 }
611#endif
612
Christopher Fauletfc633b62020-11-06 15:24:23 +0100613 return ERR_NONE;
Dragan Dosen9ac98092020-05-11 15:51:45 +0200614}
615
Dragan Dosen1e7ed042020-05-08 18:30:00 +0200616/* Used to free all SSL/TLS protocol message callbacks that were
617 * registered by using ssl_sock_register_msg_callback().
618 */
619static void ssl_sock_unregister_msg_callbacks(void)
620{
621 struct ssl_sock_msg_callback *cbk, *cbkback;
622
623 list_for_each_entry_safe(cbk, cbkback, &ssl_sock_msg_callbacks, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200624 LIST_DELETE(&cbk->list);
Dragan Dosen1e7ed042020-05-08 18:30:00 +0200625 free(cbk);
626 }
627}
628
Willy Tarreaude827952022-04-11 10:43:28 +0200629static struct ssl_sock_ctx *ssl_sock_get_ctx(struct connection *conn)
630{
631 if (!conn || conn->xprt != xprt_get(XPRT_SSL) || !conn->xprt_ctx)
632 return NULL;
633
634 return (struct ssl_sock_ctx *)conn->xprt_ctx;
635}
636
Dragan Doseneb607fe2020-05-11 17:17:06 +0200637SSL *ssl_sock_get_ssl_object(struct connection *conn)
638{
Willy Tarreau939b0bf2022-04-11 11:29:11 +0200639 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
Dragan Doseneb607fe2020-05-11 17:17:06 +0200640
Willy Tarreau939b0bf2022-04-11 11:29:11 +0200641 return ctx ? ctx->ssl : NULL;
Dragan Doseneb607fe2020-05-11 17:17:06 +0200642}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100643/*
644 * This function gives the detail of the SSL error. It is used only
645 * if the debug mode and the verbose mode are activated. It dump all
646 * the SSL error until the stack was empty.
647 */
Frédéric Lécaille2be0ac52022-09-06 19:37:08 +0200648static forceinline void ssl_sock_dump_errors(struct connection *conn,
649 struct quic_conn *qc)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100650{
651 unsigned long ret;
652
653 if (unlikely(global.mode & MODE_DEBUG)) {
654 while(1) {
Remi Tricot-Le Breton1effd9a2022-02-11 12:04:44 +0100655 const char *func = NULL;
656 ERR_peek_error_func(&func);
657
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100658 ret = ERR_get_error();
659 if (ret == 0)
660 return;
Frédéric Lécaille2be0ac52022-09-06 19:37:08 +0200661 if (conn) {
662 fprintf(stderr, "fd[%#x] OpenSSL error[0x%lx] %s: %s\n",
663 conn_fd(conn), ret,
664 func, ERR_reason_error_string(ret));
665 }
666#ifdef USE_QUIC
667 else {
668 /* TODO: we are not sure <conn> is always initialized for QUIC connections */
669 fprintf(stderr, "qc @%p OpenSSL error[0x%lx] %s: %s\n", qc, ret,
670 func, ERR_reason_error_string(ret));
671 }
672#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100673 }
674 }
675}
676
yanbzhube2774d2015-12-10 15:07:30 -0500677
William Lallemandd7bfbe22022-04-11 18:41:24 +0200678#if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE)
William Lallemanddad31052020-05-14 17:47:32 +0200679int ssl_init_single_engine(const char *engine_id, const char *def_algorithms)
Grant Zhang872f9c22017-01-21 01:10:18 +0000680{
681 int err_code = ERR_ABORT;
682 ENGINE *engine;
683 struct ssl_engine_list *el;
684
685 /* grab the structural reference to the engine */
686 engine = ENGINE_by_id(engine_id);
687 if (engine == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100688 ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000689 goto fail_get;
690 }
691
692 if (!ENGINE_init(engine)) {
693 /* the engine couldn't initialise, release it */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100694 ha_alert("ssl-engine %s: failed to initialize\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000695 goto fail_init;
696 }
697
698 if (ENGINE_set_default_string(engine, def_algorithms) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100699 ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000700 goto fail_set_method;
701 }
702
703 el = calloc(1, sizeof(*el));
Remi Tricot-Le Breton612b2c32021-05-12 17:45:21 +0200704 if (!el)
705 goto fail_alloc;
Grant Zhang872f9c22017-01-21 01:10:18 +0000706 el->e = engine;
Willy Tarreau2b718102021-04-21 07:32:39 +0200707 LIST_INSERT(&openssl_engines, &el->list);
Emeric Brunece0c332017-12-06 13:51:49 +0100708 nb_engines++;
709 if (global_ssl.async)
710 global.ssl_used_async_engines = nb_engines;
Grant Zhang872f9c22017-01-21 01:10:18 +0000711 return 0;
712
Remi Tricot-Le Breton612b2c32021-05-12 17:45:21 +0200713fail_alloc:
Grant Zhang872f9c22017-01-21 01:10:18 +0000714fail_set_method:
715 /* release the functional reference from ENGINE_init() */
716 ENGINE_finish(engine);
717
718fail_init:
719 /* release the structural reference from ENGINE_by_id() */
720 ENGINE_free(engine);
721
722fail_get:
723 return err_code;
724}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200725#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000726
Remi Tricot-Le Breton1746a382022-05-16 16:24:33 +0200727#ifdef HAVE_SSL_PROVIDERS
728int ssl_init_provider(const char *provider_name)
729{
730 int err_code = ERR_ABORT;
731 struct ssl_provider_list *prov = NULL;
732
733 prov = calloc(1, sizeof(*prov));
734 if (!prov) {
735 ha_alert("ssl-provider %s: memory allocation failure\n", provider_name);
736 goto error;
737 }
738
739 if ((prov->provider = OSSL_PROVIDER_load(NULL, provider_name)) == NULL) {
740 ha_alert("ssl-provider %s: unknown provider\n", provider_name);
741 goto error;
742 }
743
744 LIST_INSERT(&openssl_providers, &prov->list);
745
746 return 0;
747
748error:
749 ha_free(&prov);
750 return err_code;
751}
752#endif /* HAVE_SSL_PROVIDERS */
753
Ilya Shipitsinbdec3ba2020-11-14 01:56:34 +0500754#ifdef SSL_MODE_ASYNC
Emeric Brun3854e012017-05-17 20:42:48 +0200755/*
756 * openssl async fd handler
757 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200758void ssl_async_fd_handler(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000759{
Olivier Houchardea8dd942019-05-20 14:02:16 +0200760 struct ssl_sock_ctx *ctx = fdtab[fd].owner;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000761
Emeric Brun3854e012017-05-17 20:42:48 +0200762 /* fd is an async enfine fd, we must stop
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000763 * to poll this fd until it is requested
764 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000765 fd_stop_recv(fd);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000766 fd_cant_recv(fd);
767
768 /* crypto engine is available, let's notify the associated
769 * connection that it can pursue its processing.
770 */
Olivier Houcharda4598262020-09-15 22:16:02 +0200771 tasklet_wakeup(ctx->wait_event.tasklet);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000772}
773
Emeric Brun3854e012017-05-17 20:42:48 +0200774/*
775 * openssl async delayed SSL_free handler
776 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200777void ssl_async_fd_free(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000778{
779 SSL *ssl = fdtab[fd].owner;
Emeric Brun3854e012017-05-17 20:42:48 +0200780 OSSL_ASYNC_FD all_fd[32];
781 size_t num_all_fds = 0;
782 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000783
Emeric Brun3854e012017-05-17 20:42:48 +0200784 /* We suppose that the async job for a same SSL *
785 * are serialized. So if we are awake it is
786 * because the running job has just finished
787 * and we can remove all async fds safely
788 */
789 SSL_get_all_async_fds(ssl, NULL, &num_all_fds);
790 if (num_all_fds > 32) {
791 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
792 return;
793 }
794
795 SSL_get_all_async_fds(ssl, all_fd, &num_all_fds);
Emeric Brun7d392a52022-07-01 17:36:50 +0200796 for (i=0 ; i < num_all_fds ; i++) {
797 /* We want to remove the fd from the fdtab
798 * but we flag it to disown because the
799 * close is performed by the engine itself
800 */
801 fdtab[all_fd[i]].state |= FD_DISOWN;
802 fd_delete(all_fd[i]);
803 }
Emeric Brun3854e012017-05-17 20:42:48 +0200804
805 /* Now we can safely call SSL_free, no more pending job in engines */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000806 SSL_free(ssl);
Willy Tarreau82531f62021-10-06 12:15:18 +0200807 _HA_ATOMIC_DEC(&global.sslconns);
Willy Tarreau4781b152021-04-06 13:53:36 +0200808 _HA_ATOMIC_DEC(&jobs);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000809}
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000810/*
Emeric Brun3854e012017-05-17 20:42:48 +0200811 * function used to manage a returned SSL_ERROR_WANT_ASYNC
812 * and enable/disable polling for async fds
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000813 */
Olivier Houchardea8dd942019-05-20 14:02:16 +0200814static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000815{
Willy Tarreaua9786b62018-01-25 07:22:13 +0100816 OSSL_ASYNC_FD add_fd[32];
Emeric Brun3854e012017-05-17 20:42:48 +0200817 OSSL_ASYNC_FD del_fd[32];
Olivier Houchardea8dd942019-05-20 14:02:16 +0200818 SSL *ssl = ctx->ssl;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000819 size_t num_add_fds = 0;
820 size_t num_del_fds = 0;
Emeric Brun3854e012017-05-17 20:42:48 +0200821 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000822
823 SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL,
824 &num_del_fds);
Emeric Brun3854e012017-05-17 20:42:48 +0200825 if (num_add_fds > 32 || num_del_fds > 32) {
826 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 +0000827 return;
828 }
829
Emeric Brun3854e012017-05-17 20:42:48 +0200830 SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000831
Emeric Brun3854e012017-05-17 20:42:48 +0200832 /* We remove unused fds from the fdtab */
Emeric Brun7d392a52022-07-01 17:36:50 +0200833 for (i=0 ; i < num_del_fds ; i++) {
834 /* We want to remove the fd from the fdtab
835 * but we flag it to disown because the
836 * close is performed by the engine itself
837 */
838 fdtab[del_fd[i]].state |= FD_DISOWN;
839 fd_delete(del_fd[i]);
840 }
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000841
Emeric Brun3854e012017-05-17 20:42:48 +0200842 /* We add new fds to the fdtab */
843 for (i=0 ; i < num_add_fds ; i++) {
Willy Tarreau27a32452022-07-07 08:29:00 +0200844 fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tgid, ti->ltid_bit);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000845 }
846
Emeric Brun3854e012017-05-17 20:42:48 +0200847 num_add_fds = 0;
848 SSL_get_all_async_fds(ssl, NULL, &num_add_fds);
849 if (num_add_fds > 32) {
850 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
851 return;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000852 }
Emeric Brun3854e012017-05-17 20:42:48 +0200853
854 /* We activate the polling for all known async fds */
855 SSL_get_all_async_fds(ssl, add_fd, &num_add_fds);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000856 for (i=0 ; i < num_add_fds ; i++) {
Emeric Brun3854e012017-05-17 20:42:48 +0200857 fd_want_recv(add_fd[i]);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000858 /* To ensure that the fd cache won't be used
859 * We'll prefer to catch a real RD event
860 * because handling an EAGAIN on this fd will
861 * result in a context switch and also
862 * some engines uses a fd in blocking mode.
863 */
864 fd_cant_recv(add_fd[i]);
865 }
Emeric Brun3854e012017-05-17 20:42:48 +0200866
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000867}
868#endif
869
Remi Tricot-Le Bretonc9414e22022-02-08 17:45:59 +0100870
871/*
872 * Initialize an HMAC context <hctx> using the <key> and <md> parameters.
873 * Returns -1 in case of error, 1 otherwise.
874 */
875static int ssl_hmac_init(MAC_CTX *hctx, unsigned char *key, int key_len, const EVP_MD *md)
876{
877#ifdef HAVE_OSSL_PARAM
878 OSSL_PARAM params[3];
879
880 params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, key, key_len);
881 params[1] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, (char*)EVP_MD_name(md), 0);
882 params[2] = OSSL_PARAM_construct_end();
883 if (EVP_MAC_CTX_set_params(hctx, params) == 0)
884 return -1; /* error in mac initialisation */
885
886#else
887 HMAC_Init_ex(hctx, key, key_len, md, NULL);
888#endif
889 return 1;
890}
891
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100892#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Remi Tricot-Le Breton8ea1f5f2022-02-08 17:45:58 +0100893
894static int ssl_tlsext_ticket_key_cb(SSL *s, unsigned char key_name[16], unsigned char *iv, EVP_CIPHER_CTX *ectx, MAC_CTX *hctx, int enc)
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100895{
Frédéric Lécaille6aec1f32022-09-06 17:04:55 +0200896 struct tls_keys_ref *ref = NULL;
Emeric Brun9e754772019-01-10 17:51:55 +0100897 union tls_sess_key *keys;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100898 int head;
899 int i;
Christopher Faulet16f45c82018-02-16 11:23:49 +0100900 int ret = -1; /* error by default */
Frédéric Lécaille6aec1f32022-09-06 17:04:55 +0200901 struct connection *conn = SSL_get_ex_data(s, ssl_app_data_index);
902#ifdef USE_QUIC
903 struct quic_conn *qc = SSL_get_ex_data(s, ssl_qc_app_data_index);
904#endif
905
906 if (conn)
907 ref = __objt_listener(conn->target)->bind_conf->keys_ref;
908#ifdef USE_QUIC
909 else if (qc)
910 ref = qc->li->bind_conf->keys_ref;
911#endif
912
913 if (!ref) {
914 /* must never happen */
915 ABORT_NOW();
916 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100917
Christopher Faulet16f45c82018-02-16 11:23:49 +0100918 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
919
920 keys = ref->tlskeys;
921 head = ref->tls_ticket_enc_index;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100922
923 if (enc) {
924 memcpy(key_name, keys[head].name, 16);
925
926 if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH))
Christopher Faulet16f45c82018-02-16 11:23:49 +0100927 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100928
Emeric Brun9e754772019-01-10 17:51:55 +0100929 if (ref->key_size_bits == 128) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100930
Emeric Brun9e754772019-01-10 17:51:55 +0100931 if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv))
932 goto end;
933
Remi Tricot-Le Bretonc9414e22022-02-08 17:45:59 +0100934 if (ssl_hmac_init(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT()) < 0)
935 goto end;
Emeric Brun9e754772019-01-10 17:51:55 +0100936 ret = 1;
937 }
938 else if (ref->key_size_bits == 256 ) {
939
940 if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv))
941 goto end;
942
Remi Tricot-Le Bretonc9414e22022-02-08 17:45:59 +0100943 if (ssl_hmac_init(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT()) < 0)
944 goto end;
Emeric Brun9e754772019-01-10 17:51:55 +0100945 ret = 1;
946 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100947 } else {
948 for (i = 0; i < TLS_TICKETS_NO; i++) {
949 if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16))
950 goto found;
951 }
Christopher Faulet16f45c82018-02-16 11:23:49 +0100952 ret = 0;
953 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100954
Christopher Faulet16f45c82018-02-16 11:23:49 +0100955 found:
Emeric Brun9e754772019-01-10 17:51:55 +0100956 if (ref->key_size_bits == 128) {
Remi Tricot-Le Bretonc9414e22022-02-08 17:45:59 +0100957 if (ssl_hmac_init(hctx, keys[(head + i) % TLS_TICKETS_NO].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT()) < 0)
958 goto end;
Emeric Brun9e754772019-01-10 17:51:55 +0100959 if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv))
960 goto end;
961 /* 2 for key renewal, 1 if current key is still valid */
962 ret = i ? 2 : 1;
963 }
964 else if (ref->key_size_bits == 256) {
Remi Tricot-Le Bretonc9414e22022-02-08 17:45:59 +0100965 if (ssl_hmac_init(hctx, keys[(head + i) % TLS_TICKETS_NO].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT()) < 0)
966 goto end;
Emeric Brun9e754772019-01-10 17:51:55 +0100967 if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv))
968 goto end;
969 /* 2 for key renewal, 1 if current key is still valid */
970 ret = i ? 2 : 1;
971 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +0100972 }
Emeric Brun9e754772019-01-10 17:51:55 +0100973
Christopher Faulet16f45c82018-02-16 11:23:49 +0100974 end:
975 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
976 return ret;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200977}
978
979struct tls_keys_ref *tlskeys_ref_lookup(const char *filename)
980{
981 struct tls_keys_ref *ref;
982
983 list_for_each_entry(ref, &tlskeys_reference, list)
984 if (ref->filename && strcmp(filename, ref->filename) == 0)
985 return ref;
986 return NULL;
987}
988
989struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id)
990{
991 struct tls_keys_ref *ref;
992
993 list_for_each_entry(ref, &tlskeys_reference, list)
994 if (ref->unique_id == unique_id)
995 return ref;
996 return NULL;
997}
998
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +0500999/* Update the key into ref: if keysize doesn't
Emeric Brun9e754772019-01-10 17:51:55 +01001000 * match existing ones, this function returns -1
1001 * else it returns 0 on success.
1002 */
1003int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref,
Willy Tarreau83061a82018-07-13 11:56:34 +02001004 struct buffer *tlskey)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001005{
Emeric Brun9e754772019-01-10 17:51:55 +01001006 if (ref->key_size_bits == 128) {
1007 if (tlskey->data != sizeof(struct tls_sess_key_128))
1008 return -1;
1009 }
1010 else if (ref->key_size_bits == 256) {
1011 if (tlskey->data != sizeof(struct tls_sess_key_256))
1012 return -1;
1013 }
1014 else
1015 return -1;
1016
Christopher Faulet16f45c82018-02-16 11:23:49 +01001017 HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001018 memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)),
1019 tlskey->area, tlskey->data);
Christopher Faulet16f45c82018-02-16 11:23:49 +01001020 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
1021 HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Emeric Brun9e754772019-01-10 17:51:55 +01001022
1023 return 0;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001024}
1025
Willy Tarreau83061a82018-07-13 11:56:34 +02001026int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001027{
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001028 struct tls_keys_ref *ref = tlskeys_ref_lookup(filename);
1029
1030 if(!ref) {
1031 memprintf(err, "Unable to locate the referenced filename: %s", filename);
1032 return 1;
1033 }
Emeric Brun9e754772019-01-10 17:51:55 +01001034 if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) {
1035 memprintf(err, "Invalid key size");
1036 return 1;
1037 }
1038
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001039 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001040}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001041
1042/* This function finalize the configuration parsing. Its set all the
Willy Tarreaud1c57502016-12-22 22:46:15 +01001043 * automatic ids. It's called just after the basic checks. It returns
1044 * 0 on success otherwise ERR_*.
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001045 */
Willy Tarreaud1c57502016-12-22 22:46:15 +01001046static int tlskeys_finalize_config(void)
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001047{
1048 int i = 0;
1049 struct tls_keys_ref *ref, *ref2, *ref3;
1050 struct list tkr = LIST_HEAD_INIT(tkr);
1051
1052 list_for_each_entry(ref, &tlskeys_reference, list) {
1053 if (ref->unique_id == -1) {
1054 /* Look for the first free id. */
1055 while (1) {
1056 list_for_each_entry(ref2, &tlskeys_reference, list) {
1057 if (ref2->unique_id == i) {
1058 i++;
1059 break;
1060 }
1061 }
1062 if (&ref2->list == &tlskeys_reference)
1063 break;
1064 }
1065
1066 /* Uses the unique id and increment it for the next entry. */
1067 ref->unique_id = i;
1068 i++;
1069 }
1070 }
1071
1072 /* This sort the reference list by id. */
1073 list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001074 LIST_DELETE(&ref->list);
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001075 list_for_each_entry(ref3, &tkr, list) {
1076 if (ref->unique_id < ref3->unique_id) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001077 LIST_APPEND(&ref3->list, &ref->list);
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001078 break;
1079 }
1080 }
1081 if (&ref3->list == &tkr)
Willy Tarreau2b718102021-04-21 07:32:39 +02001082 LIST_APPEND(&tkr, &ref->list);
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001083 }
1084
1085 /* swap root */
William Lallemand930afdf2023-05-09 14:15:57 +02001086 LIST_SPLICE(&tlskeys_reference, &tkr);
Christopher Fauletfc633b62020-11-06 15:24:23 +01001087 return ERR_NONE;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001088}
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001089#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
1090
William Lallemand4a660132019-10-14 14:51:41 +02001091
Ilya Shipitsinb3201a32020-10-18 09:11:50 +05001092#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001093/*
1094 * This function enables the handling of OCSP status extension on 'ctx' if a
William Lallemand246c0242019-10-11 08:59:13 +02001095 * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP
1096 * status extension, the issuer's certificate is mandatory. It should be
1097 * present in ckch->ocsp_issuer.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001098 *
William Lallemand246c0242019-10-11 08:59:13 +02001099 * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an
1100 * OCSP response. If file is empty or content is not a valid OCSP response,
1101 * OCSP status extension is enabled but OCSP response is ignored (a warning is
1102 * displayed).
Emeric Brun4147b2e2014-06-16 18:36:30 +02001103 *
1104 * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is
Joseph Herlant017b3da2018-11-15 09:07:59 -08001105 * successfully enabled, or -1 in other error case.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001106 */
Remi Tricot-Le Breton0c96ee42023-03-01 16:11:50 +01001107static int ssl_sock_load_ocsp(const char *path, SSL_CTX *ctx, struct ckch_data *data, STACK_OF(X509) *chain)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001108{
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001109 X509 *x, *issuer;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001110 int i, ret = -1;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001111 struct certificate_ocsp *ocsp = NULL, *iocsp;
1112 char *warn = NULL;
1113 unsigned char *p;
Uriah Pollock3cbf09e2022-11-23 16:41:25 +01001114#ifndef USE_OPENSSL_WOLFSSL
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001115 void (*callback) (void);
Uriah Pollock3cbf09e2022-11-23 16:41:25 +01001116#else
1117 tlsextStatusCb callback;
1118#endif
Remi Tricot-Le Bretonb55be8c2022-12-20 11:11:12 +01001119 struct buffer *ocsp_uri = get_trash_chunk();
Remi Tricot-Le Bretona6c0a592023-03-13 15:56:32 +01001120 char *err = NULL;
Willy Tarreau6d4c0c22023-04-07 17:49:37 +02001121 size_t path_len;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001122
William Lallemand52ddd992022-11-22 11:51:53 +01001123 x = data->cert;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001124 if (!x)
1125 goto out;
1126
Remi Tricot-Le Bretona6c0a592023-03-13 15:56:32 +01001127 ssl_ocsp_get_uri_from_cert(x, ocsp_uri, &err);
1128 /* We should have an "OCSP URI" field in order for auto update to work. */
1129 if (data->ocsp_update_mode == SSL_SOCK_OCSP_UPDATE_ON && b_data(ocsp_uri) == 0)
1130 goto out;
Remi Tricot-Le Bretonb55be8c2022-12-20 11:11:12 +01001131
1132 /* In case of ocsp update mode set to 'on', this function might be
1133 * called with no known ocsp response. If no ocsp uri can be found in
1134 * the certificate, nothing needs to be done here. */
1135 if (!data->ocsp_response) {
1136 if (data->ocsp_update_mode != SSL_SOCK_OCSP_UPDATE_ON || b_data(ocsp_uri) == 0) {
1137 ret = 0;
1138 goto out;
1139 }
1140 }
1141
William Lallemand52ddd992022-11-22 11:51:53 +01001142 issuer = data->ocsp_issuer;
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001143 /* take issuer from chain over ocsp_issuer, is what is done historicaly */
1144 if (chain) {
1145 /* check if one of the certificate of the chain is the issuer */
1146 for (i = 0; i < sk_X509_num(chain); i++) {
1147 X509 *ti = sk_X509_value(chain, i);
1148 if (X509_check_issued(ti, x) == X509_V_OK) {
1149 issuer = ti;
1150 break;
1151 }
1152 }
1153 }
William Lallemand246c0242019-10-11 08:59:13 +02001154 if (!issuer)
1155 goto out;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001156
Remi Tricot-Le Breton2d1daa82023-01-09 12:02:47 +01001157 if (!data->ocsp_cid)
1158 data->ocsp_cid = OCSP_cert_to_id(0, x, issuer);
Remi Tricot-Le Bretoncc346672022-12-20 11:11:08 +01001159 if (!data->ocsp_cid)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001160 goto out;
1161
Remi Tricot-Le Bretoncc346672022-12-20 11:11:08 +01001162 i = i2d_OCSP_CERTID(data->ocsp_cid, NULL);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001163 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1164 goto out;
1165
Willy Tarreau6d4c0c22023-04-07 17:49:37 +02001166 path_len = strlen(path);
1167 ocsp = calloc(1, sizeof(*ocsp) + path_len + 1);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001168 if (!ocsp)
1169 goto out;
1170
1171 p = ocsp->key_data;
Remi Tricot-Le Bretoncc346672022-12-20 11:11:08 +01001172 ocsp->key_length = i2d_OCSP_CERTID(data->ocsp_cid, &p);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001173
Remi Tricot-Le Breton2b963642022-12-20 11:11:02 +01001174 HA_SPIN_LOCK(OCSP_LOCK, &ocsp_tree_lock);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001175 iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH);
1176 if (iocsp == ocsp)
1177 ocsp = NULL;
1178
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001179#ifndef SSL_CTX_get_tlsext_status_cb
1180# define SSL_CTX_get_tlsext_status_cb(ctx, cb) \
1181 *cb = (void (*) (void))ctx->tlsext_status_cb;
1182#endif
1183 SSL_CTX_get_tlsext_status_cb(ctx, &callback);
1184
1185 if (!callback) {
William Lallemanda560c062020-07-31 11:43:20 +02001186 struct ocsp_cbk_arg *cb_arg;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001187 EVP_PKEY *pkey;
yanbzhube2774d2015-12-10 15:07:30 -05001188
William Lallemanda560c062020-07-31 11:43:20 +02001189 cb_arg = calloc(1, sizeof(*cb_arg));
1190 if (!cb_arg)
1191 goto out;
1192
yanbzhube2774d2015-12-10 15:07:30 -05001193 cb_arg->is_single = 1;
1194 cb_arg->s_ocsp = iocsp;
William Lallemand76b4a122020-08-04 17:41:39 +02001195 iocsp->refcount++;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001196
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001197 pkey = X509_get_pubkey(x);
1198 cb_arg->single_kt = EVP_PKEY_base_id(pkey);
1199 EVP_PKEY_free(pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001200
1201 SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk);
William Lallemand76b4a122020-08-04 17:41:39 +02001202 SSL_CTX_set_ex_data(ctx, ocsp_ex_index, cb_arg); /* we use the ex_data instead of the cb_arg function here, so we can use the cleanup callback to free */
1203
yanbzhube2774d2015-12-10 15:07:30 -05001204 } else {
1205 /*
1206 * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx
1207 * Update that cb_arg with the new cert's staple
1208 */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001209 struct ocsp_cbk_arg *cb_arg;
yanbzhube2774d2015-12-10 15:07:30 -05001210 struct certificate_ocsp *tmp_ocsp;
1211 int index;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001212 int key_type;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001213 EVP_PKEY *pkey;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001214
William Lallemand76b4a122020-08-04 17:41:39 +02001215 cb_arg = SSL_CTX_get_ex_data(ctx, ocsp_ex_index);
yanbzhube2774d2015-12-10 15:07:30 -05001216
1217 /*
1218 * The following few lines will convert cb_arg from a single ocsp to multi ocsp
1219 * the order of operations below matter, take care when changing it
1220 */
1221 tmp_ocsp = cb_arg->s_ocsp;
1222 index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt);
1223 cb_arg->s_ocsp = NULL;
1224 cb_arg->m_ocsp[index] = tmp_ocsp;
1225 cb_arg->is_single = 0;
1226 cb_arg->single_kt = 0;
1227
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001228 pkey = X509_get_pubkey(x);
1229 key_type = EVP_PKEY_base_id(pkey);
1230 EVP_PKEY_free(pkey);
1231
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001232 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
William Lallemand76b4a122020-08-04 17:41:39 +02001233 if (index >= 0 && !cb_arg->m_ocsp[index]) {
yanbzhube2774d2015-12-10 15:07:30 -05001234 cb_arg->m_ocsp[index] = iocsp;
William Lallemand76b4a122020-08-04 17:41:39 +02001235 iocsp->refcount++;
1236 }
yanbzhube2774d2015-12-10 15:07:30 -05001237 }
Remi Tricot-Le Breton2b963642022-12-20 11:11:02 +01001238 HA_SPIN_UNLOCK(OCSP_LOCK, &ocsp_tree_lock);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001239
1240 ret = 0;
1241
1242 warn = NULL;
Remi Tricot-Le Bretonb55be8c2022-12-20 11:11:12 +01001243 if (data->ocsp_response && ssl_sock_load_ocsp_response(data->ocsp_response, iocsp, data->ocsp_cid, &warn)) {
William Lallemand3b5f3602019-10-16 18:05:05 +02001244 memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure");
Christopher Faulet767a84b2017-11-24 16:50:31 +01001245 ha_warning("%s.\n", warn);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001246 }
1247
Remi Tricot-Le Bretonb55be8c2022-12-20 11:11:12 +01001248
Remi Tricot-Le Bretona6c0a592023-03-13 15:56:32 +01001249 /* Do not insert the same certificate_ocsp structure in the
1250 * update tree more than once. */
1251 if (!ocsp) {
1252 /* Issuer certificate is not included in the certificate
1253 * chain, it will have to be treated separately during
1254 * ocsp response validation. */
1255 if (issuer == data->ocsp_issuer) {
1256 iocsp->issuer = issuer;
1257 X509_up_ref(issuer);
1258 }
1259 if (data->chain)
1260 iocsp->chain = X509_chain_up_ref(data->chain);
Remi Tricot-Le Bretonb55be8c2022-12-20 11:11:12 +01001261
Remi Tricot-Le Bretona6c0a592023-03-13 15:56:32 +01001262 iocsp->uri = calloc(1, sizeof(*iocsp->uri));
1263 if (!chunk_dup(iocsp->uri, ocsp_uri)) {
1264 ha_free(&iocsp->uri);
1265 goto out;
1266 }
Remi Tricot-Le Bretonb55be8c2022-12-20 11:11:12 +01001267
Willy Tarreau6d4c0c22023-04-07 17:49:37 +02001268 /* Note: if we arrive here, ocsp==NULL because iocsp==ocsp
1269 * after the ebmb_insert(), which indicates that we've
1270 * just inserted this new node and that it's the one for
1271 * which we previously allocated enough room for path_len+1
1272 * chars.
1273 */
1274 memcpy(iocsp->path, path, path_len + 1);
Remi Tricot-Le Breton0c96ee42023-03-01 16:11:50 +01001275
Remi Tricot-Le Bretona6c0a592023-03-13 15:56:32 +01001276 if (data->ocsp_update_mode == SSL_SOCK_OCSP_UPDATE_ON) {
Remi Tricot-Le Bretonb55be8c2022-12-20 11:11:12 +01001277 ssl_ocsp_update_insert(iocsp);
Remi Tricot-Le Breton86d1e0b2023-03-02 15:49:53 +01001278 /* If we are during init the update task is not
1279 * scheduled yet so a wakeup won't do anything.
1280 * Otherwise, if the OCSP was added through the CLI, we
1281 * wake the task up to manage the case of a new entry
1282 * that needs to be updated before the previous first
1283 * entry.
1284 */
1285 if (ocsp_update_task)
1286 task_wakeup(ocsp_update_task, TASK_WOKEN_MSG);
Remi Tricot-Le Bretonb55be8c2022-12-20 11:11:12 +01001287 }
1288 }
1289
Emeric Brun4147b2e2014-06-16 18:36:30 +02001290out:
Remi Tricot-Le Bretoncc346672022-12-20 11:11:08 +01001291 if (ret && data->ocsp_cid)
1292 OCSP_CERTID_free(data->ocsp_cid);
1293
1294 if (!ret && data->ocsp_response) {
1295 ha_free(&data->ocsp_response->area);
1296 ha_free(&data->ocsp_response);
1297 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001298
1299 if (ocsp)
Remi Tricot-Le Bretonaa529f72022-11-03 15:16:49 +01001300 ssl_sock_free_ocsp(ocsp);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001301
1302 if (warn)
1303 free(warn);
1304
Tim Duesterhusb39c24b2023-03-19 16:07:47 +01001305 free(err);
1306
Emeric Brun4147b2e2014-06-16 18:36:30 +02001307 return ret;
1308}
Remi Tricot-Le Bretonc8d814e2022-12-20 11:11:17 +01001309
Emmanuel Hocdeta73a2222020-10-26 13:55:30 +01001310#endif
1311
1312#ifdef OPENSSL_IS_BORINGSSL
Remi Tricot-Le Breton0c96ee42023-03-01 16:11:50 +01001313static int ssl_sock_load_ocsp(const char *path, SSL_CTX *ctx, struct ckch_data *data, STACK_OF(X509) *chain)
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001314{
William Lallemand4a660132019-10-14 14:51:41 +02001315 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 +02001316}
1317#endif
1318
William Lallemand4a660132019-10-14 14:51:41 +02001319
Ilya Shipitsin7bbf5862021-02-06 18:55:27 +05001320#ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001321
1322#define CT_EXTENSION_TYPE 18
1323
William Lallemand03c331c2020-05-13 10:10:01 +02001324int sctl_ex_index = -1;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001325
1326int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg)
1327{
Willy Tarreau83061a82018-07-13 11:56:34 +02001328 struct buffer *sctl = add_arg;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001329
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001330 *out = (unsigned char *) sctl->area;
1331 *outlen = sctl->data;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001332
1333 return 1;
1334}
1335
1336int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg)
1337{
1338 return 1;
1339}
1340
William Lallemanda17f4112019-10-10 15:16:44 +02001341static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001342{
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001343 int ret = -1;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001344
William Lallemanda17f4112019-10-10 15:16:44 +02001345 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 +01001346 goto out;
1347
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001348 SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl);
1349
1350 ret = 0;
1351
1352out:
1353 return ret;
1354}
1355
1356#endif
1357
Emeric Brune1f38db2012-09-03 20:36:47 +02001358void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
1359{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001360 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001361#ifdef USE_QUIC
1362 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
1363#endif /* USE_QUIC */
1364 struct ssl_sock_ctx *ctx = NULL;
1365
Emeric Brund8b2bb52014-01-28 15:43:53 +01001366 BIO *write_bio;
Willy Tarreau622317d2015-02-27 16:36:16 +01001367 (void)ret; /* shut gcc stupid warning */
Emeric Brune1f38db2012-09-03 20:36:47 +02001368
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001369 if (conn)
Willy Tarreau939b0bf2022-04-11 11:29:11 +02001370 ctx = conn_get_ssl_sock_ctx(conn);
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001371#ifdef USE_QUIC
1372 else if (qc)
1373 ctx = qc->xprt_ctx;
1374#endif /* USE_QUIC */
Amaury Denoyelle7c564bf2022-01-24 11:04:05 +01001375
1376 if (!ctx) {
1377 /* must never happen */
1378 ABORT_NOW();
1379 return;
1380 }
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001381
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001382#ifndef SSL_OP_NO_RENEGOTIATION
1383 /* Please note that BoringSSL defines this macro to zero so don't
1384 * change this to #if and do not assign a default value to this macro!
1385 */
Emeric Brune1f38db2012-09-03 20:36:47 +02001386 if (where & SSL_CB_HANDSHAKE_START) {
1387 /* Disable renegotiation (CVE-2009-3555) */
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001388 if (conn && (conn->flags & (CO_FL_WAIT_L6_CONN | CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == 0) {
Emeric Brune1f38db2012-09-03 20:36:47 +02001389 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001390 conn->err_code = CO_ER_SSL_RENEG;
1391 }
Emeric Brune1f38db2012-09-03 20:36:47 +02001392 }
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001393#endif
Emeric Brund8b2bb52014-01-28 15:43:53 +01001394
1395 if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001396 if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) {
Emeric Brund8b2bb52014-01-28 15:43:53 +01001397 /* Long certificate chains optimz
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001398 If write and read bios are different, we
Emeric Brund8b2bb52014-01-28 15:43:53 +01001399 consider that the buffering was activated,
1400 so we rise the output buffer size from 4k
1401 to 16k */
1402 write_bio = SSL_get_wbio(ssl);
1403 if (write_bio != SSL_get_rbio(ssl)) {
1404 BIO_set_write_buffer_size(write_bio, 16384);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001405 ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001406 }
1407 }
1408 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001409}
1410
Emeric Brune64aef12012-09-21 13:15:06 +02001411/* Callback is called for each certificate of the chain during a verify
1412 ok is set to 1 if preverify detect no error on current certificate.
1413 Returns 0 to break the handshake, 1 otherwise. */
Evan Broderbe554312013-06-27 00:05:25 -07001414int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +02001415{
1416 SSL *ssl;
1417 struct connection *conn;
Frédéric Lécaille2be0ac52022-09-06 19:37:08 +02001418 struct ssl_sock_ctx *ctx = NULL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001419 int err, depth;
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001420 X509 *client_crt;
1421 STACK_OF(X509) *certs;
Frédéric Lécaille48e46f92022-10-14 09:34:00 +02001422 struct bind_conf *bind_conf = NULL;
Frédéric Lécaille2be0ac52022-09-06 19:37:08 +02001423 struct quic_conn *qc = NULL;
Emeric Brune64aef12012-09-21 13:15:06 +02001424
1425 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001426 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001427 client_crt = SSL_get_ex_data(ssl, ssl_client_crt_ref_index);
Emeric Brune64aef12012-09-21 13:15:06 +02001428
Frédéric Lécaille2be0ac52022-09-06 19:37:08 +02001429 if (conn) {
1430 bind_conf = __objt_listener(conn->target)->bind_conf;
1431 ctx = __conn_get_ssl_sock_ctx(conn);
1432 }
1433#ifdef USE_QUIC
1434 else {
1435 qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
Amaury Denoyelleba303de2022-10-17 18:46:49 +02001436 BUG_ON(!qc); /* Must never happen */
1437 bind_conf = qc->li->bind_conf;
1438 ctx = qc->xprt_ctx;
Frédéric Lécaille2be0ac52022-09-06 19:37:08 +02001439 }
1440#endif
1441
Frédéric Lécaille48e46f92022-10-14 09:34:00 +02001442 BUG_ON(!ctx || !bind_conf);
Christopher Faulet881cce92022-11-23 09:27:13 +01001443 ALREADY_CHECKED(ctx);
1444 ALREADY_CHECKED(bind_conf);
Frédéric Lécaille2be0ac52022-09-06 19:37:08 +02001445
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001446 ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +02001447
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001448 depth = X509_STORE_CTX_get_error_depth(x_store);
1449 err = X509_STORE_CTX_get_error(x_store);
1450
Emeric Brun81c00f02012-09-21 14:31:21 +02001451 if (ok) /* no errors */
1452 return ok;
1453
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001454 /* Keep a reference to the client's certificate in order to be able to
1455 * dump some fetches values in a log even when the verification process
1456 * fails. */
1457 if (depth == 0) {
1458 X509_free(client_crt);
1459 client_crt = X509_STORE_CTX_get0_cert(x_store);
1460 if (client_crt) {
1461 X509_up_ref(client_crt);
1462 SSL_set_ex_data(ssl, ssl_client_crt_ref_index, client_crt);
1463 }
1464 }
1465 else {
1466 /* An error occurred on a CA certificate of the certificate
1467 * chain, we might never call this verify callback on the client
1468 * certificate's depth (which is 0) so we try to store the
1469 * reference right now. */
Remi Tricot-Le Bretonf95c2952021-08-20 09:51:23 +02001470 certs = X509_STORE_CTX_get1_chain(x_store);
1471 if (certs) {
1472 client_crt = sk_X509_value(certs, 0);
1473 if (client_crt) {
1474 X509_up_ref(client_crt);
1475 SSL_set_ex_data(ssl, ssl_client_crt_ref_index, client_crt);
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02001476 }
1477 sk_X509_pop_free(certs, X509_free);
1478 }
1479 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001480
1481 /* check if CA error needs to be ignored */
1482 if (depth > 0) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001483 if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) {
1484 ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
1485 ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +02001486 }
1487
Remi Tricot-Le Breton9b259822022-11-10 10:48:58 +01001488 if (err <= SSL_MAX_VFY_ERROR_CODE &&
William Lallemand46396892022-11-10 16:45:24 +01001489 cert_ignerr_bitfield_get(bind_conf->ca_ignerr_bitfield, err))
Frédéric Lécaille2be0ac52022-09-06 19:37:08 +02001490 goto err_ignored;
Emeric Brun81c00f02012-09-21 14:31:21 +02001491
Frédéric Lécaille2be0ac52022-09-06 19:37:08 +02001492 /* TODO: for QUIC connection, this error code is lost */
1493 if (conn)
1494 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001495 return 0;
1496 }
1497
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001498 if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st))
1499 ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +02001500
Emeric Brun81c00f02012-09-21 14:31:21 +02001501 /* check if certificate error needs to be ignored */
Remi Tricot-Le Breton9b259822022-11-10 10:48:58 +01001502 if (err <= SSL_MAX_VFY_ERROR_CODE &&
William Lallemand46396892022-11-10 16:45:24 +01001503 cert_ignerr_bitfield_get(bind_conf->crt_ignerr_bitfield, err))
Frédéric Lécaille2be0ac52022-09-06 19:37:08 +02001504 goto err_ignored;
Emeric Brun81c00f02012-09-21 14:31:21 +02001505
Frédéric Lécaille2be0ac52022-09-06 19:37:08 +02001506 /* TODO: for QUIC connection, this error code is lost */
1507 if (conn)
1508 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001509 return 0;
Frédéric Lécaille2be0ac52022-09-06 19:37:08 +02001510
1511 err_ignored:
1512 ssl_sock_dump_errors(conn, qc);
1513 ERR_clear_error();
1514 return 1;
Emeric Brune64aef12012-09-21 13:15:06 +02001515}
1516
Dragan Dosen9ac98092020-05-11 15:51:45 +02001517#ifdef TLS1_RT_HEARTBEAT
1518static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version,
1519 int content_type, const void *buf, size_t len,
1520 SSL *ssl)
1521{
1522 /* test heartbeat received (write_p is set to 0
1523 for a received record) */
1524 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
Willy Tarreau3a0a0d62022-04-12 07:31:06 +02001525 struct ssl_sock_ctx *ctx = __conn_get_ssl_sock_ctx(conn);
Dragan Dosen9ac98092020-05-11 15:51:45 +02001526 const unsigned char *p = buf;
1527 unsigned int payload;
1528
1529 ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
1530
1531 /* Check if this is a CVE-2014-0160 exploitation attempt. */
1532 if (*p != TLS1_HB_REQUEST)
1533 return;
1534
1535 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
1536 goto kill_it;
1537
1538 payload = (p[1] * 256) + p[2];
1539 if (3 + payload + 16 <= len)
1540 return; /* OK no problem */
1541 kill_it:
1542 /* We have a clear heartbleed attack (CVE-2014-0160), the
1543 * advertised payload is larger than the advertised packet
1544 * length, so we have garbage in the buffer between the
1545 * payload and the end of the buffer (p+len). We can't know
1546 * if the SSL stack is patched, and we don't know if we can
1547 * safely wipe out the area between p+3+len and payload.
1548 * So instead, we prevent the response from being sent by
1549 * setting the max_send_fragment to 0 and we report an SSL
1550 * error, which will kill this connection. It will be reported
1551 * above as SSL_ERROR_SSL while an other handshake failure with
1552 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
1553 */
1554 ssl->max_send_fragment = 0;
1555 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
1556 }
1557}
1558#endif
1559
1560static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version,
1561 int content_type, const void *buf, size_t len,
1562 SSL *ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001563{
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001564 struct ssl_capture *capture;
Marcin Deranek769fd2e2021-07-12 14:16:55 +02001565 uchar *msg;
1566 uchar *end;
1567 uchar *extensions_end;
1568 uchar *ec_start = NULL;
1569 uchar *ec_formats_start = NULL;
1570 uchar *list_end;
1571 ushort protocol_version;
1572 ushort extension_id;
1573 ushort ec_len = 0;
1574 uchar ec_formats_len = 0;
1575 int offset = 0;
1576 int rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001577
1578 /* This function is called for "from client" and "to server"
1579 * connections. The combination of write_p == 0 and content_type == 22
Joseph Herlant017b3da2018-11-15 09:07:59 -08001580 * is only available during "from client" connection.
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001581 */
1582
1583 /* "write_p" is set to 0 is the bytes are received messages,
1584 * otherwise it is set to 1.
1585 */
1586 if (write_p != 0)
1587 return;
1588
1589 /* content_type contains the type of message received or sent
1590 * according with the SSL/TLS protocol spec. This message is
1591 * encoded with one byte. The value 256 (two bytes) is used
1592 * for designing the SSL/TLS record layer. According with the
1593 * rfc6101, the expected message (other than 256) are:
1594 * - change_cipher_spec(20)
1595 * - alert(21)
1596 * - handshake(22)
1597 * - application_data(23)
1598 * - (255)
1599 * We are interessed by the handshake and specially the client
1600 * hello.
1601 */
1602 if (content_type != 22)
1603 return;
1604
1605 /* The message length is at least 4 bytes, containing the
1606 * message type and the message length.
1607 */
1608 if (len < 4)
1609 return;
1610
1611 /* First byte of the handshake message id the type of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001612 * message. The known types are:
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001613 * - hello_request(0)
1614 * - client_hello(1)
1615 * - server_hello(2)
1616 * - certificate(11)
1617 * - server_key_exchange (12)
1618 * - certificate_request(13)
1619 * - server_hello_done(14)
1620 * We are interested by the client hello.
1621 */
1622 msg = (unsigned char *)buf;
1623 if (msg[0] != 1)
1624 return;
1625
1626 /* Next three bytes are the length of the message. The total length
1627 * must be this decoded length + 4. If the length given as argument
1628 * is not the same, we abort the protocol dissector.
1629 */
1630 rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3];
1631 if (len < rec_len + 4)
1632 return;
1633 msg += 4;
1634 end = msg + rec_len;
1635 if (end < msg)
1636 return;
1637
Marcin Deranek769fd2e2021-07-12 14:16:55 +02001638 /* Expect 2 bytes for protocol version
1639 * (1 byte for major and 1 byte for minor)
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001640 */
Marcin Deranek769fd2e2021-07-12 14:16:55 +02001641 if (msg + 2 > end)
1642 return;
1643 protocol_version = (msg[0] << 8) + msg[1];
1644 msg += 2;
1645
1646 /* Expect the random, composed by 4 bytes for the unix time and
1647 * 28 bytes for unix payload. So we jump 4 + 28.
1648 */
1649 msg += 4 + 28;
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001650 if (msg > end)
1651 return;
1652
1653 /* Next, is session id:
1654 * if present, we have to jump by length + 1 for the size information
1655 * if not present, we have to jump by 1 only
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001656 */
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001657 if (msg[0] > 0)
1658 msg += msg[0];
1659 msg += 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001660 if (msg > end)
1661 return;
1662
1663 /* Next two bytes are the ciphersuite length. */
1664 if (msg + 2 > end)
1665 return;
1666 rec_len = (msg[0] << 8) + msg[1];
1667 msg += 2;
1668 if (msg + rec_len > end || msg + rec_len < msg)
1669 return;
1670
Marcin Deranek769fd2e2021-07-12 14:16:55 +02001671 capture = pool_zalloc(pool_head_ssl_capture);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001672 if (!capture)
1673 return;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001674 /* Compute the xxh64 of the ciphersuite. */
1675 capture->xxh64 = XXH64(msg, rec_len, 0);
1676
1677 /* Capture the ciphersuite. */
Marcin Deranek310a2602021-07-13 19:04:24 +02001678 capture->ciphersuite_len = MIN(global_ssl.capture_buffer_size, rec_len);
Marcin Deranek769fd2e2021-07-12 14:16:55 +02001679 capture->ciphersuite_offset = 0;
1680 memcpy(capture->data, msg, capture->ciphersuite_len);
1681 msg += rec_len;
1682 offset += capture->ciphersuite_len;
1683
1684 /* Initialize other data */
1685 capture->protocol_version = protocol_version;
1686
1687 /* Next, compression methods:
1688 * if present, we have to jump by length + 1 for the size information
1689 * if not present, we have to jump by 1 only
1690 */
1691 if (msg[0] > 0)
1692 msg += msg[0];
1693 msg += 1;
1694 if (msg > end)
1695 goto store_capture;
1696
1697 /* We reached extensions */
1698 if (msg + 2 > end)
1699 goto store_capture;
1700 rec_len = (msg[0] << 8) + msg[1];
1701 msg += 2;
1702 if (msg + rec_len > end || msg + rec_len < msg)
1703 goto store_capture;
1704 extensions_end = msg + rec_len;
1705 capture->extensions_offset = offset;
1706
1707 /* Parse each extension */
1708 while (msg + 4 < extensions_end) {
1709 /* Add 2 bytes of extension_id */
Marcin Deranek310a2602021-07-13 19:04:24 +02001710 if (global_ssl.capture_buffer_size >= offset + 2) {
Marcin Deranek769fd2e2021-07-12 14:16:55 +02001711 capture->data[offset++] = msg[0];
1712 capture->data[offset++] = msg[1];
1713 capture->extensions_len += 2;
1714 }
1715 else
1716 break;
1717 extension_id = (msg[0] << 8) + msg[1];
1718 /* Length of the extension */
1719 rec_len = (msg[2] << 8) + msg[3];
1720
1721 /* Expect 2 bytes extension id + 2 bytes extension size */
1722 msg += 2 + 2;
1723 if (msg + rec_len > extensions_end || msg + rec_len < msg)
1724 goto store_capture;
1725 /* TLS Extensions
1726 * https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml */
1727 if (extension_id == 0x000a) {
1728 /* Elliptic Curves:
1729 * https://www.rfc-editor.org/rfc/rfc8422.html
1730 * https://www.rfc-editor.org/rfc/rfc7919.html */
1731 list_end = msg + rec_len;
1732 if (msg + 2 > list_end)
1733 goto store_capture;
1734 rec_len = (msg[0] << 8) + msg[1];
1735 msg += 2;
1736
1737 if (msg + rec_len > list_end || msg + rec_len < msg)
1738 goto store_capture;
1739 /* Store location/size of the list */
1740 ec_start = msg;
1741 ec_len = rec_len;
1742 }
1743 else if (extension_id == 0x000b) {
1744 /* Elliptic Curves Point Formats:
1745 * https://www.rfc-editor.org/rfc/rfc8422.html */
1746 list_end = msg + rec_len;
1747 if (msg + 1 > list_end)
1748 goto store_capture;
1749 rec_len = msg[0];
1750 msg += 1;
1751
1752 if (msg + rec_len > list_end || msg + rec_len < msg)
1753 goto store_capture;
1754 /* Store location/size of the list */
1755 ec_formats_start = msg;
1756 ec_formats_len = rec_len;
1757 }
1758 msg += rec_len;
1759 }
1760
1761 if (ec_start) {
1762 rec_len = ec_len;
Marcin Deranek310a2602021-07-13 19:04:24 +02001763 if (offset + rec_len > global_ssl.capture_buffer_size)
1764 rec_len = global_ssl.capture_buffer_size - offset;
Marcin Deranek769fd2e2021-07-12 14:16:55 +02001765 memcpy(capture->data + offset, ec_start, rec_len);
1766 capture->ec_offset = offset;
1767 capture->ec_len = rec_len;
1768 offset += rec_len;
1769 }
1770 if (ec_formats_start) {
1771 rec_len = ec_formats_len;
Marcin Deranek310a2602021-07-13 19:04:24 +02001772 if (offset + rec_len > global_ssl.capture_buffer_size)
1773 rec_len = global_ssl.capture_buffer_size - offset;
Marcin Deranek769fd2e2021-07-12 14:16:55 +02001774 memcpy(capture->data + offset, ec_formats_start, rec_len);
1775 capture->ec_formats_offset = offset;
1776 capture->ec_formats_len = rec_len;
1777 offset += rec_len;
1778 }
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001779
Marcin Deranek769fd2e2021-07-12 14:16:55 +02001780 store_capture:
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001781 SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001782}
William Lallemand7d42ef52020-07-06 11:41:30 +02001783
1784
William Lallemand722180a2021-06-09 16:46:12 +02001785#ifdef HAVE_SSL_KEYLOG
William Lallemand7d42ef52020-07-06 11:41:30 +02001786static void ssl_init_keylog(struct connection *conn, int write_p, int version,
1787 int content_type, const void *buf, size_t len,
1788 SSL *ssl)
1789{
1790 struct ssl_keylog *keylog;
1791
1792 if (SSL_get_ex_data(ssl, ssl_keylog_index))
1793 return;
1794
Willy Tarreauf208ac02021-03-22 21:10:12 +01001795 keylog = pool_zalloc(pool_head_ssl_keylog);
William Lallemand7d42ef52020-07-06 11:41:30 +02001796 if (!keylog)
1797 return;
1798
William Lallemand7d42ef52020-07-06 11:41:30 +02001799 if (!SSL_set_ex_data(ssl, ssl_keylog_index, keylog)) {
1800 pool_free(pool_head_ssl_keylog, keylog);
1801 return;
1802 }
1803}
1804#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001805
Emeric Brun29f037d2014-04-25 19:05:36 +02001806/* Callback is called for ssl protocol analyse */
1807void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
1808{
Dragan Dosen1e7ed042020-05-08 18:30:00 +02001809 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
1810 struct ssl_sock_msg_callback *cbk;
1811
Dragan Dosen1e7ed042020-05-08 18:30:00 +02001812 /* Try to call all callback functions that were registered by using
1813 * ssl_sock_register_msg_callback().
1814 */
1815 list_for_each_entry(cbk, &ssl_sock_msg_callbacks, list) {
1816 cbk->func(conn, write_p, version, content_type, buf, len, ssl);
1817 }
Emeric Brun29f037d2014-04-25 19:05:36 +02001818}
1819
Bernard Spil13c53f82018-02-15 13:34:58 +01001820#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchardc7566002018-11-20 23:33:50 +01001821static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen,
1822 const unsigned char *in, unsigned int inlen,
1823 void *arg)
1824{
1825 struct server *srv = arg;
1826
1827 if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str,
1828 srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED)
1829 return SSL_TLSEXT_ERR_OK;
1830 return SSL_TLSEXT_ERR_NOACK;
1831}
1832#endif
1833
1834#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001835/* This callback is used so that the server advertises the list of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001836 * negotiable protocols for NPN.
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001837 */
1838static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
1839 unsigned int *len, void *arg)
1840{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001841 struct ssl_bind_conf *conf = arg;
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001842
1843 *data = (const unsigned char *)conf->npn_str;
1844 *len = conf->npn_len;
1845 return SSL_TLSEXT_ERR_OK;
1846}
1847#endif
1848
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001849#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001850/* This callback is used so that the server advertises the list of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001851 * negotiable protocols for ALPN.
Willy Tarreauab861d32013-04-02 02:30:41 +02001852 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001853static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out,
1854 unsigned char *outlen,
1855 const unsigned char *server,
1856 unsigned int server_len, void *arg)
Willy Tarreauab861d32013-04-02 02:30:41 +02001857{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001858 struct ssl_bind_conf *conf = arg;
Frédéric Lécaille1761fdf2021-12-14 19:40:04 +01001859#ifdef USE_QUIC
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001860 struct quic_conn *qc = SSL_get_ex_data(s, ssl_qc_app_data_index);
Frédéric Lécaille1761fdf2021-12-14 19:40:04 +01001861#endif
Willy Tarreauab861d32013-04-02 02:30:41 +02001862
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001863 if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str,
1864 conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) {
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001865#ifdef USE_QUIC
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001866 if (qc)
1867 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
Frédéric Lécaille067a82b2021-11-19 17:02:20 +01001868#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001869 return SSL_TLSEXT_ERR_NOACK;
1870 }
Frédéric Lécaille1761fdf2021-12-14 19:40:04 +01001871
1872#ifdef USE_QUIC
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01001873 if (qc && !quic_set_app_ops(qc, *out, *outlen)) {
1874 quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL);
Frédéric Lécaille1761fdf2021-12-14 19:40:04 +01001875 return SSL_TLSEXT_ERR_NOACK;
1876 }
1877#endif
1878
Willy Tarreauab861d32013-04-02 02:30:41 +02001879 return SSL_TLSEXT_ERR_OK;
1880}
1881#endif
1882
Willy Tarreauc8ad3be2015-06-17 15:48:26 +02001883#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001884#ifndef SSL_NO_GENERATE_CERTIFICATES
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001885
cui flitera94bedc2022-08-29 14:42:57 +08001886/* Configure a DNS SAN extension on a certificate. */
Shimi Gersneradabbfe2020-08-23 13:58:13 +03001887int ssl_sock_add_san_ext(X509V3_CTX* ctx, X509* cert, const char *servername) {
1888 int failure = 0;
1889 X509_EXTENSION *san_ext = NULL;
1890 CONF *conf = NULL;
1891 struct buffer *san_name = get_trash_chunk();
1892
1893 conf = NCONF_new(NULL);
1894 if (!conf) {
1895 failure = 1;
1896 goto cleanup;
1897 }
1898
1899 /* Build an extension based on the DNS entry above */
1900 chunk_appendf(san_name, "DNS:%s", servername);
1901 san_ext = X509V3_EXT_nconf_nid(conf, ctx, NID_subject_alt_name, san_name->area);
1902 if (!san_ext) {
1903 failure = 1;
1904 goto cleanup;
1905 }
1906
1907 /* Add the extension */
1908 if (!X509_add_ext(cert, san_ext, -1 /* Add to end */)) {
1909 failure = 1;
1910 goto cleanup;
1911 }
1912
1913 /* Success */
1914 failure = 0;
1915
1916cleanup:
1917 if (NULL != san_ext) X509_EXTENSION_free(san_ext);
1918 if (NULL != conf) NCONF_free(conf);
1919
1920 return failure;
1921}
1922
Christopher Faulet30548802015-06-11 13:39:32 +02001923/* Create a X509 certificate with the specified servername and serial. This
1924 * function returns a SSL_CTX object or NULL if an error occurs. */
Christopher Faulet7969a332015-10-09 11:15:03 +02001925static SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001926ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001927{
Shimi Gersner5846c492020-08-23 13:58:12 +03001928 X509 *cacert = bind_conf->ca_sign_ckch->cert;
1929 EVP_PKEY *capkey = bind_conf->ca_sign_ckch->key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001930 SSL_CTX *ssl_ctx = NULL;
1931 X509 *newcrt = NULL;
1932 EVP_PKEY *pkey = NULL;
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001933 SSL *tmp_ssl = NULL;
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001934 CONF *ctmp = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001935 X509_NAME *name;
1936 const EVP_MD *digest;
1937 X509V3_CTX ctx;
1938 unsigned int i;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001939 int key_type;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001940
Christopher Faulet48a83322017-07-28 16:56:09 +02001941 /* Get the private key of the default certificate and use it */
Ilya Shipitsinaf204882020-12-19 03:12:12 +05001942#ifdef HAVE_SSL_CTX_get0_privatekey
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001943 pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx);
1944#else
1945 tmp_ssl = SSL_new(bind_conf->default_ctx);
1946 if (tmp_ssl)
1947 pkey = SSL_get_privatekey(tmp_ssl);
1948#endif
1949 if (!pkey)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001950 goto mkcert_error;
1951
1952 /* Create the certificate */
1953 if (!(newcrt = X509_new()))
1954 goto mkcert_error;
1955
1956 /* Set version number for the certificate (X509v3) and the serial
1957 * number */
1958 if (X509_set_version(newcrt, 2L) != 1)
1959 goto mkcert_error;
Willy Tarreau1db42732021-04-06 11:44:07 +02001960 ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD_FETCH(&ssl_ctx_serial, 1));
Christopher Faulet31af49d2015-06-09 17:29:50 +02001961
1962 /* Set duration for the certificate */
Rosen Penev68185952018-12-14 08:47:02 -08001963 if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) ||
1964 !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001965 goto mkcert_error;
1966
1967 /* set public key in the certificate */
1968 if (X509_set_pubkey(newcrt, pkey) != 1)
1969 goto mkcert_error;
1970
1971 /* Set issuer name from the CA */
1972 if (!(name = X509_get_subject_name(cacert)))
1973 goto mkcert_error;
1974 if (X509_set_issuer_name(newcrt, name) != 1)
1975 goto mkcert_error;
1976
1977 /* Set the subject name using the same, but the CN */
1978 name = X509_NAME_dup(name);
1979 if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
1980 (const unsigned char *)servername,
1981 -1, -1, 0) != 1) {
1982 X509_NAME_free(name);
1983 goto mkcert_error;
1984 }
1985 if (X509_set_subject_name(newcrt, name) != 1) {
1986 X509_NAME_free(name);
1987 goto mkcert_error;
1988 }
1989 X509_NAME_free(name);
1990
1991 /* Add x509v3 extensions as specified */
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001992 ctmp = NCONF_new(NULL);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001993 X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0);
1994 for (i = 0; i < X509V3_EXT_SIZE; i++) {
1995 X509_EXTENSION *ext;
1996
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001997 if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i])))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001998 goto mkcert_error;
1999 if (!X509_add_ext(newcrt, ext, -1)) {
2000 X509_EXTENSION_free(ext);
2001 goto mkcert_error;
2002 }
2003 X509_EXTENSION_free(ext);
2004 }
2005
Shimi Gersneradabbfe2020-08-23 13:58:13 +03002006 /* Add SAN extension */
2007 if (ssl_sock_add_san_ext(&ctx, newcrt, servername)) {
2008 goto mkcert_error;
2009 }
2010
Christopher Faulet31af49d2015-06-09 17:29:50 +02002011 /* Sign the certificate with the CA private key */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002012
2013 key_type = EVP_PKEY_base_id(capkey);
2014
2015 if (key_type == EVP_PKEY_DSA)
2016 digest = EVP_sha1();
2017 else if (key_type == EVP_PKEY_RSA)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002018 digest = EVP_sha256();
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002019 else if (key_type == EVP_PKEY_EC)
Christopher Faulet7969a332015-10-09 11:15:03 +02002020 digest = EVP_sha256();
2021 else {
Ilya Shipitsinec36c912021-01-07 11:57:42 +05002022#ifdef ASN1_PKEY_CTRL_DEFAULT_MD_NID
Christopher Faulet7969a332015-10-09 11:15:03 +02002023 int nid;
2024
2025 if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0)
2026 goto mkcert_error;
2027 if (!(digest = EVP_get_digestbynid(nid)))
2028 goto mkcert_error;
Christopher Faulete7db2162015-10-19 13:59:24 +02002029#else
2030 goto mkcert_error;
2031#endif
Christopher Faulet7969a332015-10-09 11:15:03 +02002032 }
2033
Christopher Faulet31af49d2015-06-09 17:29:50 +02002034 if (!(X509_sign(newcrt, capkey, digest)))
2035 goto mkcert_error;
2036
2037 /* Create and set the new SSL_CTX */
2038 if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method())))
2039 goto mkcert_error;
2040 if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
2041 goto mkcert_error;
2042 if (!SSL_CTX_use_certificate(ssl_ctx, newcrt))
2043 goto mkcert_error;
2044 if (!SSL_CTX_check_private_key(ssl_ctx))
2045 goto mkcert_error;
2046
Shimi Gersner5846c492020-08-23 13:58:12 +03002047 /* Build chaining the CA cert and the rest of the chain, keep these order */
2048#if defined(SSL_CTX_add1_chain_cert)
2049 if (!SSL_CTX_add1_chain_cert(ssl_ctx, bind_conf->ca_sign_ckch->cert)) {
2050 goto mkcert_error;
2051 }
2052
2053 if (bind_conf->ca_sign_ckch->chain) {
2054 for (i = 0; i < sk_X509_num(bind_conf->ca_sign_ckch->chain); i++) {
2055 X509 *chain_cert = sk_X509_value(bind_conf->ca_sign_ckch->chain, i);
2056 if (!SSL_CTX_add1_chain_cert(ssl_ctx, chain_cert)) {
2057 goto mkcert_error;
2058 }
2059 }
2060 }
2061#endif
2062
Christopher Faulet31af49d2015-06-09 17:29:50 +02002063 if (newcrt) X509_free(newcrt);
Christopher Faulet7969a332015-10-09 11:15:03 +02002064
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002065#ifndef OPENSSL_NO_DH
Remi Tricot-Le Breton88c56952022-02-11 12:04:56 +01002066#if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL)
Remi Tricot-Le Breton292a88c2022-02-11 12:04:49 +01002067 SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh_cbk);
Remi Tricot-Le Breton88c56952022-02-11 12:04:56 +01002068#else
2069 ssl_sock_set_tmp_dh_from_pkey(ssl_ctx, pkey);
2070#endif
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002071#endif
Remi Tricot-Le Bretonc11e7e12022-02-08 17:45:56 +01002072
2073#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
2074#if defined(SSL_CTX_set1_curves_list)
2075 {
2076 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
2077 if (!SSL_CTX_set1_curves_list(ssl_ctx, ecdhe))
2078 goto end;
2079 }
2080#endif
2081#else
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002082#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
2083 {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002084 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002085 EC_KEY *ecc;
2086 int nid;
2087
2088 if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef)
2089 goto end;
2090 if (!(ecc = EC_KEY_new_by_curve_name(nid)))
2091 goto end;
2092 SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc);
2093 EC_KEY_free(ecc);
2094 }
Remi Tricot-Le Bretonc11e7e12022-02-08 17:45:56 +01002095#endif /* defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) */
2096#endif /* HA_OPENSSL_VERSION_NUMBER >= 0x10101000L */
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002097 end:
Christopher Faulet31af49d2015-06-09 17:29:50 +02002098 return ssl_ctx;
2099
2100 mkcert_error:
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002101 if (ctmp) NCONF_free(ctmp);
Emmanuel Hocdet15969292017-08-11 10:56:00 +02002102 if (tmp_ssl) SSL_free(tmp_ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002103 if (ssl_ctx) SSL_CTX_free(ssl_ctx);
2104 if (newcrt) X509_free(newcrt);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002105 return NULL;
2106}
2107
Christopher Faulet7969a332015-10-09 11:15:03 +02002108
Christopher Faulet30548802015-06-11 13:39:32 +02002109/* Do a lookup for a certificate in the LRU cache used to store generated
Emeric Brun821bb9b2017-06-15 16:37:39 +02002110 * certificates and immediately assign it to the SSL session if not null. */
Christopher Faulet30548802015-06-11 13:39:32 +02002111SSL_CTX *
Emeric Brun821bb9b2017-06-15 16:37:39 +02002112ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet30548802015-06-11 13:39:32 +02002113{
2114 struct lru64 *lru = NULL;
2115
2116 if (ssl_ctx_lru_tree) {
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002117 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Shimi Gersner5846c492020-08-23 13:58:12 +03002118 lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_ckch->cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002119 if (lru && lru->domain) {
2120 if (ssl)
2121 SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data);
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002122 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002123 return (SSL_CTX *)lru->data;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002124 }
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002125 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002126 }
2127 return NULL;
2128}
2129
Emeric Brun821bb9b2017-06-15 16:37:39 +02002130/* Same as <ssl_sock_assign_generated_cert> but without SSL session. This
2131 * function is not thread-safe, it should only be used to check if a certificate
2132 * exists in the lru cache (with no warranty it will not be removed by another
2133 * thread). It is kept for backward compatibility. */
2134SSL_CTX *
2135ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf)
2136{
2137 return ssl_sock_assign_generated_cert(key, bind_conf, NULL);
2138}
2139
Christopher Fauletd2cab922015-07-28 16:03:47 +02002140/* Set a certificate int the LRU cache used to store generated
2141 * certificate. Return 0 on success, otherwise -1 */
2142int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002143ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02002144{
2145 struct lru64 *lru = NULL;
2146
2147 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002148 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Shimi Gersner5846c492020-08-23 13:58:12 +03002149 lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_ckch->cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002150 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002151 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002152 return -1;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002153 }
Christopher Faulet30548802015-06-11 13:39:32 +02002154 if (lru->domain && lru->data)
2155 lru->free((SSL_CTX *)lru->data);
Shimi Gersner5846c492020-08-23 13:58:12 +03002156 lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_ckch->cert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002157 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002158 return 0;
Christopher Faulet30548802015-06-11 13:39:32 +02002159 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02002160 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02002161}
2162
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002163/* Compute the key of the certificate. */
Christopher Faulet30548802015-06-11 13:39:32 +02002164unsigned int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002165ssl_sock_generated_cert_key(const void *data, size_t len)
Christopher Faulet30548802015-06-11 13:39:32 +02002166{
2167 return XXH32(data, len, ssl_ctx_lru_seed);
2168}
2169
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002170/* Generate a cert and immediately assign it to the SSL session so that the cert's
2171 * refcount is maintained regardless of the cert's presence in the LRU cache.
2172 */
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002173static int
Christopher Faulet7969a332015-10-09 11:15:03 +02002174ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002175{
Shimi Gersner5846c492020-08-23 13:58:12 +03002176 X509 *cacert = bind_conf->ca_sign_ckch->cert;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002177 SSL_CTX *ssl_ctx = NULL;
2178 struct lru64 *lru = NULL;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002179 unsigned int key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002180
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002181 key = ssl_sock_generated_cert_key(servername, strlen(servername));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002182 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002183 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002184 lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002185 if (lru && lru->domain)
2186 ssl_ctx = (SSL_CTX *)lru->data;
Christopher Fauletd2cab922015-07-28 16:03:47 +02002187 if (!ssl_ctx && lru) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002188 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002189 lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002190 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002191 SSL_set_SSL_CTX(ssl, ssl_ctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002192 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002193 return 1;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002194 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002195 else {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002196 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002197 SSL_set_SSL_CTX(ssl, ssl_ctx);
2198 /* No LRU cache, this CTX will be released as soon as the session dies */
2199 SSL_CTX_free(ssl_ctx);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002200 return 1;
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002201 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002202 return 0;
2203}
2204static int
2205ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl)
2206{
2207 unsigned int key;
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002208 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002209
Willy Tarreauf5bdb642019-07-17 11:29:32 +02002210 if (conn_get_dst(conn)) {
Willy Tarreau085a1512019-07-17 14:47:35 +02002211 key = ssl_sock_generated_cert_key(conn->dst, get_addr_len(conn->dst));
Emeric Brun821bb9b2017-06-15 16:37:39 +02002212 if (ssl_sock_assign_generated_cert(key, bind_conf, ssl))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002213 return 1;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002214 }
2215 return 0;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002216}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002217#endif /* !defined SSL_NO_GENERATE_CERTIFICATES */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002218
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002219#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002220
2221static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002222{
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02002223#if SSL_OP_NO_SSLv3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002224 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002225 : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method());
2226#endif
2227}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002228static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2229 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002230 : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method());
2231}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002232static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002233#if SSL_OP_NO_TLSv1_1
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002234 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002235 : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method());
2236#endif
2237}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002238static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002239#if SSL_OP_NO_TLSv1_2
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002240 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002241 : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method());
2242#endif
2243}
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002244/* TLSv1.2 is the last supported version in this context. */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002245static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {}
2246/* Unusable in this context. */
2247static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {}
2248static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {}
2249static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {}
2250static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {}
2251static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {}
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002252#else /* openssl >= 1.1.0 */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002253
2254static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) {
2255 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002256 : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION);
2257}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002258static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {
2259 c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION)
2260 : SSL_set_min_proto_version(ssl, SSL3_VERSION);
2261}
2262static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2263 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002264 : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2265}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002266static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {
2267 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION)
2268 : SSL_set_min_proto_version(ssl, TLS1_VERSION);
2269}
2270static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
2271 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002272 : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
2273}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002274static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {
2275 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION)
2276 : SSL_set_min_proto_version(ssl, TLS1_1_VERSION);
2277}
2278static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
2279 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002280 : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
2281}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002282static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {
2283 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION)
2284 : SSL_set_min_proto_version(ssl, TLS1_2_VERSION);
2285}
2286static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {
William Lallemandf22b0322021-06-02 16:09:11 +02002287#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002288 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002289 : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
2290#endif
2291}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002292static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {
William Lallemandf22b0322021-06-02 16:09:11 +02002293#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002294 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION)
2295 : SSL_set_min_proto_version(ssl, TLS1_3_VERSION);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002296#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002297}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002298#endif
2299static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { }
2300static void ssl_set_None_func(SSL *ssl, set_context_func c) { }
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002301
William Lallemand7fd8b452020-05-07 15:20:43 +02002302struct methodVersions methodVersions[] = {
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002303 {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */
2304 {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */
2305 {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */
2306 {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */
2307 {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */
2308 {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 +02002309};
2310
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002311static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx)
2312{
2313 SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk);
2314 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx)));
2315 SSL_set_SSL_CTX(ssl, ctx);
2316}
2317
Ilya Shipitsin1fc44d42021-01-23 00:09:14 +05002318#ifdef HAVE_SSL_CLIENT_HELLO_CB
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002319
Frédéric Lécaille901ee2f2020-11-23 11:19:04 +01002320int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002321{
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002322 struct bind_conf *s = priv;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002323 (void)al; /* shut gcc stupid warning */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002324
Willy Tarreau1ea6e6a2022-05-20 16:03:18 +02002325 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || (s->options & BC_O_GENERATE_CERTS))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002326 return SSL_TLSEXT_ERR_OK;
2327 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002328}
2329
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002330#ifdef OPENSSL_IS_BORINGSSL
Frédéric Lécaille901ee2f2020-11-23 11:19:04 +01002331int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002332{
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002333 SSL *ssl = ctx->ssl;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002334#else
Frédéric Lécaille901ee2f2020-11-23 11:19:04 +01002335int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg)
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002336{
2337#endif
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01002338 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
2339#ifdef USE_QUIC
2340 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
2341#endif /* USE_QUIC */
2342 struct bind_conf *s = NULL;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002343 const uint8_t *extension_data;
2344 size_t extension_len;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002345 int has_rsa_sig = 0, has_ecdsa_sig = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002346
2347 char *wildp = NULL;
2348 const uint8_t *servername;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002349 size_t servername_len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002350 struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002351 int allow_early = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002352 int i;
2353
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01002354 if (conn)
2355 s = __objt_listener(conn->target)->bind_conf;
2356#ifdef USE_QUIC
2357 else if (qc)
2358 s = qc->li->bind_conf;
2359#endif /* USE_QUIC */
Amaury Denoyelle7c564bf2022-01-24 11:04:05 +01002360
2361 if (!s) {
2362 /* must never happen */
2363 ABORT_NOW();
2364 return 0;
2365 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002366
Frédéric Lécaillee9473c72020-11-23 15:37:11 +01002367#ifdef USE_QUIC
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01002368 if (qc) {
Frédéric Lécaillee9473c72020-11-23 15:37:11 +01002369 /* Look for the QUIC transport parameters. */
2370#ifdef OPENSSL_IS_BORINGSSL
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01002371 if (!SSL_early_callback_ctx_extension_get(ctx, qc->tps_tls_ext,
Frédéric Lécaillee9473c72020-11-23 15:37:11 +01002372 &extension_data, &extension_len))
2373#else
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01002374 if (!SSL_client_hello_get0_ext(ssl, qc->tps_tls_ext,
Frédéric Lécaillee9473c72020-11-23 15:37:11 +01002375 &extension_data, &extension_len))
2376#endif
Frédéric Lécailleb5b52472021-11-22 15:55:16 +01002377 {
2378 /* This is not redundant. It we only return 0 without setting
2379 * <*al>, this has as side effect to generate another TLS alert
2380 * which would be set after calling quic_set_tls_alert().
2381 */
2382 *al = SSL_AD_MISSING_EXTENSION;
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01002383 quic_set_tls_alert(qc, SSL_AD_MISSING_EXTENSION);
Frédéric Lécailleb5b52472021-11-22 15:55:16 +01002384 return 0;
2385 }
Frédéric Lécaillee9473c72020-11-23 15:37:11 +01002386
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01002387 if (!quic_transport_params_store(qc, 0, extension_data,
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002388 extension_data + extension_len))
Frédéric Lécaillee9473c72020-11-23 15:37:11 +01002389 goto abort;
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002390
2391 qc->flags |= QUIC_FL_CONN_TX_TP_RECEIVED;
Frédéric Lécaillee9473c72020-11-23 15:37:11 +01002392 }
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01002393#endif /* USE_QUIC */
Frédéric Lécaillee9473c72020-11-23 15:37:11 +01002394
Olivier Houchard9679ac92017-10-27 14:58:08 +02002395 if (s->ssl_conf.early_data)
Olivier Houchardc2aae742017-09-22 18:26:28 +02002396 allow_early = 1;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002397#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002398 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
2399 &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002400#else
2401 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) {
2402#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002403 /*
2404 * The server_name extension was given too much extensibility when it
2405 * was written, so parsing the normal case is a bit complex.
2406 */
2407 size_t len;
2408 if (extension_len <= 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002409 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002410 /* Extract the length of the supplied list of names. */
2411 len = (*extension_data++) << 8;
2412 len |= *extension_data++;
2413 if (len + 2 != extension_len)
2414 goto abort;
2415 /*
2416 * The list in practice only has a single element, so we only consider
2417 * the first one.
2418 */
2419 if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name)
2420 goto abort;
2421 extension_len = len - 1;
2422 /* Now we can finally pull out the byte array with the actual hostname. */
2423 if (extension_len <= 2)
2424 goto abort;
2425 len = (*extension_data++) << 8;
2426 len |= *extension_data++;
2427 if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name
2428 || memchr(extension_data, 0, len) != NULL)
2429 goto abort;
2430 servername = extension_data;
2431 servername_len = len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002432 } else {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002433#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau1ea6e6a2022-05-20 16:03:18 +02002434 if (s->options & BC_O_GENERATE_CERTS && ssl_sock_generate_certificate_from_conn(s, ssl)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002435 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002436 }
2437#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002438 /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002439 if (!s->strict_sni) {
William Lallemand21724f02019-11-04 17:56:13 +01002440 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002441 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002442 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchardc2aae742017-09-22 18:26:28 +02002443 goto allow_early;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002444 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002445 goto abort;
2446 }
2447
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05002448 /* extract/check clientHello information */
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002449#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002450 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002451#else
2452 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
2453#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002454 uint8_t sign;
2455 size_t len;
2456 if (extension_len < 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002457 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002458 len = (*extension_data++) << 8;
2459 len |= *extension_data++;
2460 if (len + 2 != extension_len)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002461 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002462 if (len % 2 != 0)
2463 goto abort;
2464 for (; len > 0; len -= 2) {
2465 extension_data++; /* hash */
2466 sign = *extension_data++;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002467 switch (sign) {
2468 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002469 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002470 break;
2471 case TLSEXT_signature_ecdsa:
2472 has_ecdsa_sig = 1;
2473 break;
2474 default:
2475 continue;
2476 }
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002477 if (has_ecdsa_sig && has_rsa_sig)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002478 break;
2479 }
2480 } else {
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002481 /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002482 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002483 }
2484 if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002485 const SSL_CIPHER *cipher;
2486 size_t len;
2487 const uint8_t *cipher_suites;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002488 has_ecdsa_sig = 0;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002489#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002490 len = ctx->cipher_suites_len;
2491 cipher_suites = ctx->cipher_suites;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002492#else
2493 len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites);
2494#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002495 if (len % 2 != 0)
2496 goto abort;
2497 for (; len != 0; len -= 2, cipher_suites += 2) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002498#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002499 uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002500 cipher = SSL_get_cipher_by_value(cipher_suite);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002501#else
2502 cipher = SSL_CIPHER_find(ssl, cipher_suites);
2503#endif
Emmanuel Hocdet019f9b12017-10-02 17:12:06 +02002504 if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) {
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002505 has_ecdsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002506 break;
2507 }
2508 }
2509 }
2510
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002511 for (i = 0; i < trash.size && i < servername_len; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002512 trash.area[i] = tolower(servername[i]);
2513 if (!wildp && (trash.area[i] == '.'))
2514 wildp = &trash.area[i];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002515 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002516 trash.area[i] = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002517
William Lallemand150bfa82019-09-19 17:12:49 +02002518 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002519
William Lallemand94bd3192020-08-14 14:43:35 +02002520 /* Look for an ECDSA, RSA and DSA certificate, first in the single
2521 * name and if not found in the wildcard */
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002522 for (i = 0; i < 2; i++) {
2523 if (i == 0) /* lookup in full qualified names */
2524 node = ebst_lookup(&s->sni_ctx, trash.area);
William Lallemand30f9e092020-08-17 14:31:19 +02002525 else if (i == 1 && wildp) /* lookup in wildcards names */
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002526 node = ebst_lookup(&s->sni_w_ctx, wildp);
2527 else
2528 break;
William Lallemand30f9e092020-08-17 14:31:19 +02002529
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002530 for (n = node; n; n = ebmb_next_dup(n)) {
William Lallemand30f9e092020-08-17 14:31:19 +02002531
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002532 /* lookup a not neg filter */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002533 if (!container_of(n, struct sni_ctx, name)->neg) {
William Lallemand30f9e092020-08-17 14:31:19 +02002534 struct sni_ctx *sni, *sni_tmp;
2535 int skip = 0;
2536
2537 if (i == 1 && wildp) { /* wildcard */
2538 /* If this is a wildcard, look for an exclusion on the same crt-list line */
2539 sni = container_of(n, struct sni_ctx, name);
2540 list_for_each_entry(sni_tmp, &sni->ckch_inst->sni_ctx, by_ckch_inst) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01002541 if (sni_tmp->neg && (strcmp((const char *)sni_tmp->name.key, trash.area) == 0)) {
William Lallemand30f9e092020-08-17 14:31:19 +02002542 skip = 1;
2543 break;
2544 }
2545 }
2546 if (skip)
2547 continue;
2548 }
2549
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002550 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002551 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002552 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002553 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002554 break;
2555 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002556 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002557 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002558 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002559 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002560 if (!node_anonymous)
2561 node_anonymous = n;
2562 break;
2563 }
2564 }
2565 }
William Lallemand94bd3192020-08-14 14:43:35 +02002566 }
2567 /* Once the certificates are found, select them depending on what is
2568 * supported in the client and by key_signature priority order: EDSA >
2569 * RSA > DSA */
William Lallemand5b1d1f62020-08-14 15:30:13 +02002570 if (has_ecdsa_sig && node_ecdsa)
2571 node = node_ecdsa;
2572 else if (has_rsa_sig && node_rsa)
2573 node = node_rsa;
2574 else if (node_anonymous)
2575 node = node_anonymous;
2576 else if (node_ecdsa)
2577 node = node_ecdsa; /* no ecdsa signature case (< TLSv1.2) */
2578 else
2579 node = node_rsa; /* no rsa signature case (far far away) */
2580
William Lallemand94bd3192020-08-14 14:43:35 +02002581 if (node) {
2582 /* switch ctx */
2583 struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf;
2584 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
2585 if (conf) {
2586 methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN);
2587 methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX);
2588 if (conf->early_data)
2589 allow_early = 1;
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002590 }
William Lallemand94bd3192020-08-14 14:43:35 +02002591 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
2592 goto allow_early;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002593 }
William Lallemand150bfa82019-09-19 17:12:49 +02002594
William Lallemand02010472019-10-18 11:02:19 +02002595 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002596#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau1ea6e6a2022-05-20 16:03:18 +02002597 if (s->options & BC_O_GENERATE_CERTS && ssl_sock_generate_certificate(trash.area, s, ssl)) {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002598 /* switch ctx done in ssl_sock_generate_certificate */
Olivier Houchardc2aae742017-09-22 18:26:28 +02002599 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002600 }
2601#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002602 if (!s->strict_sni) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002603 /* no certificate match, is the default_ctx */
William Lallemand21724f02019-11-04 17:56:13 +01002604 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002605 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002606 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
William Lallemand7980dff2021-11-18 17:46:26 +01002607 goto allow_early;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002608 }
William Lallemand7980dff2021-11-18 17:46:26 +01002609
Remi Tricot-Le Bretona9967632022-01-07 17:12:01 +01002610 /* We are about to raise an handshake error so the servername extension
2611 * callback will never be called and the SNI will never be stored in the
2612 * SSL context. In order for the ssl_fc_sni sample fetch to still work
2613 * in such a case, we store the SNI ourselves as an ex_data information
2614 * in the SSL context.
2615 */
2616 {
2617 char *client_sni = pool_alloc(ssl_sock_client_sni_pool);
2618 if (client_sni) {
2619 strncpy(client_sni, trash.area, TLSEXT_MAXLEN_host_name);
2620 client_sni[TLSEXT_MAXLEN_host_name] = '\0';
2621 SSL_set_ex_data(ssl, ssl_client_sni_index, client_sni);
2622 }
2623 }
2624
William Lallemand7980dff2021-11-18 17:46:26 +01002625 /* other cases fallback on abort, if strict-sni is set but no node was found */
2626
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002627 abort:
2628 /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01002629 if (conn)
2630 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002631#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002632 return ssl_select_cert_error;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002633#else
2634 *al = SSL_AD_UNRECOGNIZED_NAME;
2635 return 0;
2636#endif
William Lallemand7980dff2021-11-18 17:46:26 +01002637
2638allow_early:
2639#ifdef OPENSSL_IS_BORINGSSL
2640 if (allow_early)
2641 SSL_set_early_data_enabled(ssl, 1);
2642#else
2643 if (!allow_early)
2644 SSL_set_max_early_data(ssl, 0);
2645#endif
2646 return 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002647}
2648
William Lallemand002e2062021-11-18 15:25:16 +01002649#else /* ! HAVE_SSL_CLIENT_HELLO_CB */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002650
Emeric Brunfc0421f2012-09-07 17:30:07 +02002651/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
2652 * warning when no match is found, which implies the default (first) cert
2653 * will keep being used.
2654 */
William Lallemand844009d2022-09-02 15:27:32 +02002655int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv)
Emeric Brunfc0421f2012-09-07 17:30:07 +02002656{
2657 const char *servername;
2658 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002659 struct ebmb_node *node, *n;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002660 struct bind_conf *s = priv;
William Lallemand70a6e632022-09-07 11:21:34 +02002661#ifdef USE_QUIC
2662 const uint8_t *extension_data;
2663 size_t extension_len;
2664 struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index);
2665#endif /* USE_QUIC */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002666 int i;
2667 (void)al; /* shut gcc stupid warning */
2668
William Lallemand70a6e632022-09-07 11:21:34 +02002669#ifdef USE_QUIC
2670 if (qc) {
2671
2672 /* Look for the QUIC transport parameters. */
2673 SSL_get_peer_quic_transport_params(ssl, &extension_data, &extension_len);
2674 if (extension_len == 0) {
2675 /* This is not redundant. It we only return 0 without setting
2676 * <*al>, this has as side effect to generate another TLS alert
2677 * which would be set after calling quic_set_tls_alert().
2678 */
2679 *al = SSL_AD_MISSING_EXTENSION;
2680 quic_set_tls_alert(qc, SSL_AD_MISSING_EXTENSION);
2681 return SSL_TLSEXT_ERR_NOACK;
2682 }
2683
2684 if (!quic_transport_params_store(qc, 0, extension_data,
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002685 extension_data + extension_len))
William Lallemand70a6e632022-09-07 11:21:34 +02002686 return SSL_TLSEXT_ERR_NOACK;
Frédéric Lécailleaf25a692023-02-01 17:56:57 +01002687
2688 qc->flags |= QUIC_FL_CONN_TX_TP_RECEIVED;
William Lallemand70a6e632022-09-07 11:21:34 +02002689 }
2690#endif /* USE_QUIC */
2691
Emeric Brunfc0421f2012-09-07 17:30:07 +02002692 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002693 if (!servername) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002694#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau1ea6e6a2022-05-20 16:03:18 +02002695 if (s->options & BC_O_GENERATE_CERTS && ssl_sock_generate_certificate_from_conn(s, ssl))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002696 return SSL_TLSEXT_ERR_OK;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002697#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002698 if (s->strict_sni)
2699 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002700 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002701 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002702 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002703 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002704 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02002705
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002706 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02002707 if (!servername[i])
2708 break;
Willy Tarreauf278eec2020-07-05 21:46:32 +02002709 trash.area[i] = tolower((unsigned char)servername[i]);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002710 if (!wildp && (trash.area[i] == '.'))
2711 wildp = &trash.area[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +02002712 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002713 trash.area[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002714
William Lallemand150bfa82019-09-19 17:12:49 +02002715 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002716 node = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002717 /* lookup in full qualified names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002718 for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) {
2719 /* lookup a not neg filter */
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002720 if (!container_of(n, struct sni_ctx, name)->neg) {
2721 node = n;
2722 break;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002723 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002724 }
2725 if (!node && wildp) {
2726 /* lookup in wildcards names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002727 for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) {
2728 /* lookup a not neg filter */
2729 if (!container_of(n, struct sni_ctx, name)->neg) {
2730 node = n;
2731 break;
2732 }
2733 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002734 }
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002735 if (!node) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002736#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau1ea6e6a2022-05-20 16:03:18 +02002737 if (s->options & BC_O_GENERATE_CERTS && ssl_sock_generate_certificate(servername, s, ssl)) {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002738 /* switch ctx done in ssl_sock_generate_certificate */
William Lallemand150bfa82019-09-19 17:12:49 +02002739 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002740 return SSL_TLSEXT_ERR_OK;
2741 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002742#endif
William Lallemand21724f02019-11-04 17:56:13 +01002743 if (s->strict_sni) {
2744 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002745 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002746 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002747 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002748 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002749 return SSL_TLSEXT_ERR_OK;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002750 }
2751
2752 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002753 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
William Lallemand150bfa82019-09-19 17:12:49 +02002754 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002755 return SSL_TLSEXT_ERR_OK;
2756}
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002757#endif /* (!) OPENSSL_IS_BORINGSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002758#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2759
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002760#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002761
Remi Tricot-Le Breton7f6425a2022-02-11 12:04:52 +01002762static inline HASSL_DH *ssl_new_dh_fromdata(BIGNUM *p, BIGNUM *g)
2763{
2764#if (HA_OPENSSL_VERSION_NUMBER >= 0x3000000fL)
2765 OSSL_PARAM_BLD *tmpl = NULL;
2766 OSSL_PARAM *params = NULL;
2767 EVP_PKEY_CTX *ctx = NULL;
2768 EVP_PKEY *pkey = NULL;
2769
2770 if ((tmpl = OSSL_PARAM_BLD_new()) == NULL
2771 || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p)
2772 || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g)
2773 || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
2774 goto end;
2775 }
2776 ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
2777 if (ctx == NULL
2778 || !EVP_PKEY_fromdata_init(ctx)
2779 || !EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params)) {
2780 goto end;
2781 }
2782
2783end:
2784 EVP_PKEY_CTX_free(ctx);
2785 OSSL_PARAM_free(params);
2786 OSSL_PARAM_BLD_free(tmpl);
Remi Tricot-Le Bretona2c21db2022-11-03 15:16:47 +01002787 BN_free(p);
2788 BN_free(g);
Remi Tricot-Le Breton7f6425a2022-02-11 12:04:52 +01002789 return pkey;
2790#else
2791
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +01002792 HASSL_DH *dh = DH_new();
Remi Tricot-Le Breton7f6425a2022-02-11 12:04:52 +01002793
2794 if (!dh)
2795 return NULL;
2796
2797 DH_set0_pqg(dh, p, NULL, g);
2798
2799 return dh;
2800#endif
2801}
2802
Remi Tricot-Le Bretonc69be7c2022-04-20 18:30:17 +02002803#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Remi Tricot-Le Breton528b3fd2022-04-12 11:31:54 +02002804static inline HASSL_DH *ssl_get_dh_by_nid(int nid)
2805{
2806#if (HA_OPENSSL_VERSION_NUMBER >= 0x3000000fL)
2807 OSSL_PARAM params[2];
2808 EVP_PKEY *pkey = NULL;
2809 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
2810 const char *named_group = NULL;
2811
2812 if (!pctx)
2813 goto end;
2814
2815 named_group = OBJ_nid2ln(nid);
2816
2817 if (!named_group)
2818 goto end;
2819
2820 params[0] = OSSL_PARAM_construct_utf8_string("group", (char*)named_group, 0);
2821 params[1] = OSSL_PARAM_construct_end();
2822
2823 if (EVP_PKEY_keygen_init(pctx) && EVP_PKEY_CTX_set_params(pctx, params))
2824 EVP_PKEY_generate(pctx, &pkey);
2825
2826end:
2827 EVP_PKEY_CTX_free(pctx);
2828 return pkey;
2829#else
2830
2831 HASSL_DH *dh = NULL;
Remi Tricot-Le Breton528b3fd2022-04-12 11:31:54 +02002832 dh = DH_new_by_nid(nid);
Remi Tricot-Le Breton528b3fd2022-04-12 11:31:54 +02002833 return dh;
2834#endif
2835}
Remi Tricot-Le Bretonc69be7c2022-04-20 18:30:17 +02002836#endif
Remi Tricot-Le Breton528b3fd2022-04-12 11:31:54 +02002837
Remi Tricot-Le Breton7f6425a2022-02-11 12:04:52 +01002838
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +01002839static HASSL_DH * ssl_get_dh_1024(void)
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002840{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002841 static unsigned char dh1024_p[]={
2842 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7,
2843 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3,
2844 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9,
2845 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96,
2846 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D,
2847 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17,
2848 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B,
2849 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94,
2850 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC,
2851 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E,
2852 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B,
2853 };
2854 static unsigned char dh1024_g[]={
2855 0x02,
2856 };
2857
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002858 BIGNUM *p;
2859 BIGNUM *g;
Remi Gacogned3a341a2015-05-29 16:26:17 +02002860
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +01002861 HASSL_DH *dh = NULL;
2862
2863 p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL);
2864 g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL);
2865
2866 if (p && g)
2867 dh = ssl_new_dh_fromdata(p, g);
2868
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002869 return dh;
2870}
2871
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +01002872static HASSL_DH *ssl_get_dh_2048(void)
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002873{
Remi Tricot-Le Breton528b3fd2022-04-12 11:31:54 +02002874#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L)
Remi Gacogned3a341a2015-05-29 16:26:17 +02002875 static unsigned char dh2048_p[]={
2876 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59,
2877 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24,
2878 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66,
2879 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10,
2880 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B,
2881 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23,
2882 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC,
2883 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E,
2884 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77,
2885 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A,
2886 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66,
2887 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74,
2888 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E,
2889 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40,
2890 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93,
2891 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43,
2892 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88,
2893 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1,
2894 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17,
2895 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB,
2896 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41,
2897 0xB7,0x1F,0x77,0xF3,
2898 };
2899 static unsigned char dh2048_g[]={
2900 0x02,
2901 };
2902
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002903 BIGNUM *p;
2904 BIGNUM *g;
Remi Gacogned3a341a2015-05-29 16:26:17 +02002905
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +01002906 HASSL_DH *dh = NULL;
2907
2908 p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL);
2909 g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL);
2910
2911 if (p && g)
2912 dh = ssl_new_dh_fromdata(p, g);
2913
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002914 return dh;
Remi Tricot-Le Breton528b3fd2022-04-12 11:31:54 +02002915#else
2916 return ssl_get_dh_by_nid(NID_ffdhe2048);
2917#endif
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002918}
2919
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +01002920static HASSL_DH *ssl_get_dh_4096(void)
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002921{
Remi Tricot-Le Breton528b3fd2022-04-12 11:31:54 +02002922#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L)
Remi Gacogned3a341a2015-05-29 16:26:17 +02002923 static unsigned char dh4096_p[]={
2924 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11,
2925 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68,
2926 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD,
2927 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B,
2928 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B,
2929 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47,
2930 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15,
2931 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35,
2932 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79,
2933 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3,
2934 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC,
2935 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52,
2936 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C,
2937 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A,
2938 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41,
2939 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55,
2940 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52,
2941 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66,
2942 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E,
2943 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47,
2944 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2,
2945 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73,
2946 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13,
2947 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10,
2948 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14,
2949 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD,
2950 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E,
2951 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48,
2952 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01,
2953 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60,
2954 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC,
2955 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41,
2956 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8,
2957 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B,
2958 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23,
2959 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE,
2960 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD,
2961 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03,
2962 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08,
2963 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5,
2964 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F,
2965 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67,
2966 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B,
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002967 };
Remi Gacogned3a341a2015-05-29 16:26:17 +02002968 static unsigned char dh4096_g[]={
2969 0x02,
2970 };
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002971
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002972 BIGNUM *p;
2973 BIGNUM *g;
Remi Gacogned3a341a2015-05-29 16:26:17 +02002974
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +01002975 HASSL_DH *dh = NULL;
2976
2977 p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL);
2978 g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL);
2979
2980 if (p && g)
2981 dh = ssl_new_dh_fromdata(p, g);
2982
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002983 return dh;
Remi Tricot-Le Breton528b3fd2022-04-12 11:31:54 +02002984#else
2985 return ssl_get_dh_by_nid(NID_ffdhe4096);
2986#endif
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002987}
2988
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +01002989static HASSL_DH *ssl_get_tmp_dh(EVP_PKEY *pkey)
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002990{
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +01002991 HASSL_DH *dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002992 int type;
Remi Tricot-Le Breton292a88c2022-02-11 12:04:49 +01002993 int keylen = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002994
2995 type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002996
Ilya Shipitsin52f2ff52022-07-23 23:55:19 +05002997 if (type == EVP_PKEY_EC) {
2998 keylen = global_ssl.default_dh_param;
2999 }
3000
Remi Gacognef46cd6e2014-06-12 14:58:40 +02003001 /* The keylen supplied by OpenSSL can only be 512 or 1024.
3002 See ssl3_send_server_key_exchange() in ssl/s3_srvr.c
3003 */
3004 if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) {
3005 keylen = EVP_PKEY_bits(pkey);
3006 }
3007
Willy Tarreauef934602016-12-22 23:12:01 +01003008 if (keylen > global_ssl.default_dh_param) {
3009 keylen = global_ssl.default_dh_param;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02003010 }
3011
Remi Gacogned3a341a2015-05-29 16:26:17 +02003012 if (keylen >= 4096) {
Remi Tricot-Le Bretonbed72632022-02-11 12:04:53 +01003013 if (!local_dh_4096)
3014 local_dh_4096 = ssl_get_dh_4096();
Remi Gacogne8de54152014-07-15 11:36:40 +02003015 dh = local_dh_4096;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02003016 }
3017 else if (keylen >= 2048) {
Remi Tricot-Le Bretonbed72632022-02-11 12:04:53 +01003018 if (!local_dh_2048)
3019 local_dh_2048 = ssl_get_dh_2048();
Remi Gacogne8de54152014-07-15 11:36:40 +02003020 dh = local_dh_2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02003021 }
3022 else {
Remi Tricot-Le Bretonbed72632022-02-11 12:04:53 +01003023 if (!local_dh_1024)
3024 local_dh_1024 = ssl_get_dh_1024();
Remi Gacogne8de54152014-07-15 11:36:40 +02003025 dh = local_dh_1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02003026 }
3027
3028 return dh;
3029}
3030
Remi Tricot-Le Breton88c56952022-02-11 12:04:56 +01003031#if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL)
Remi Tricot-Le Breton292a88c2022-02-11 12:04:49 +01003032/* Returns Diffie-Hellman parameters matching the private key length
3033 but not exceeding global_ssl.default_dh_param */
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +01003034static HASSL_DH *ssl_get_tmp_dh_cbk(SSL *ssl, int export, int keylen)
Remi Tricot-Le Breton292a88c2022-02-11 12:04:49 +01003035{
3036 EVP_PKEY *pkey = SSL_get_privatekey(ssl);
3037
3038 return ssl_get_tmp_dh(pkey);
3039}
Remi Tricot-Le Breton88c56952022-02-11 12:04:56 +01003040#endif
Remi Tricot-Le Breton292a88c2022-02-11 12:04:49 +01003041
Remi Tricot-Le Breton846eda92022-02-11 12:04:50 +01003042static int ssl_sock_set_tmp_dh(SSL_CTX *ctx, HASSL_DH *dh)
3043{
3044#if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL)
3045 return SSL_CTX_set_tmp_dh(ctx, dh);
3046#else
3047 int retval = 0;
3048 HASSL_DH_up_ref(dh);
3049
3050 retval = SSL_CTX_set0_tmp_dh_pkey(ctx, dh);
3051
3052 if (!retval)
3053 HASSL_DH_free(dh);
3054
3055 return retval;
3056#endif
3057}
3058
Remi Tricot-Le Breton5f179302022-02-11 12:04:51 +01003059#if (HA_OPENSSL_VERSION_NUMBER >= 0x3000000fL)
3060static void ssl_sock_set_tmp_dh_from_pkey(SSL_CTX *ctx, EVP_PKEY *pkey)
3061{
3062 HASSL_DH *dh = NULL;
3063 if (pkey && (dh = ssl_get_tmp_dh(pkey))) {
3064 HASSL_DH_up_ref(dh);
3065 if (!SSL_CTX_set0_tmp_dh_pkey(ctx, dh))
3066 HASSL_DH_free(dh);
3067 }
Remi Tricot-Le Breton5f179302022-02-11 12:04:51 +01003068}
3069#endif
3070
Remi Tricot-Le Breton09ebb332022-02-11 12:04:48 +01003071HASSL_DH *ssl_sock_get_dh_from_bio(BIO *bio)
3072{
3073#if (HA_OPENSSL_VERSION_NUMBER >= 0x3000000fL)
3074 HASSL_DH *dh = NULL;
3075 OSSL_DECODER_CTX *dctx = NULL;
3076 const char *format = "PEM";
3077 const char *keytype = "DH";
3078
3079 dctx = OSSL_DECODER_CTX_new_for_pkey(&dh, format, NULL, keytype,
3080 OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
3081 NULL, NULL);
3082
3083 if (dctx == NULL || OSSL_DECODER_CTX_get_num_decoders(dctx) == 0)
3084 goto end;
3085
3086 /* The DH parameters might not be the first section found in the PEM
3087 * file so we need to iterate over all of them until we find the right
3088 * one.
3089 */
3090 while (!BIO_eof(bio) && !dh)
3091 OSSL_DECODER_from_bio(dctx, bio);
3092
3093end:
3094 OSSL_DECODER_CTX_free(dctx);
3095 return dh;
3096#else
3097 HASSL_DH *dh = NULL;
3098
3099 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
3100
3101 return dh;
3102#endif
3103}
3104
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +01003105static HASSL_DH * ssl_sock_get_dh_from_file(const char *filename)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003106{
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +01003107 HASSL_DH *dh = NULL;
Remi Gacogne47783ef2015-05-29 15:53:22 +02003108 BIO *in = BIO_new(BIO_s_file());
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003109
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003110 if (in == NULL)
3111 goto end;
3112
Remi Gacogne47783ef2015-05-29 15:53:22 +02003113 if (BIO_read_filename(in, filename) <= 0)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003114 goto end;
3115
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +01003116 dh = ssl_sock_get_dh_from_bio(in);
Remi Gacogne47783ef2015-05-29 15:53:22 +02003117
3118end:
3119 if (in)
3120 BIO_free(in);
3121
Emeric Brune1b4ed42018-08-16 15:14:12 +02003122 ERR_clear_error();
3123
Remi Gacogne47783ef2015-05-29 15:53:22 +02003124 return dh;
3125}
3126
3127int ssl_sock_load_global_dh_param_from_file(const char *filename)
3128{
3129 global_dh = ssl_sock_get_dh_from_file(filename);
3130
3131 if (global_dh) {
3132 return 0;
3133 }
3134
3135 return -1;
3136}
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003137#endif
3138
William Lallemand9117de92019-10-04 00:29:42 +02003139/* This function allocates a sni_ctx and adds it to the ckch_inst */
William Lallemand1d29c742019-10-04 00:53:29 +02003140static int ckch_inst_add_cert_sni(SSL_CTX *ctx, struct ckch_inst *ckch_inst,
William Lallemand9117de92019-10-04 00:29:42 +02003141 struct bind_conf *s, struct ssl_bind_conf *conf,
3142 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003143{
3144 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003145 int wild = 0, neg = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003146
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003147 if (*name == '!') {
3148 neg = 1;
3149 name++;
3150 }
3151 if (*name == '*') {
3152 wild = 1;
3153 name++;
3154 }
3155 /* !* filter is a nop */
3156 if (neg && wild)
3157 return order;
3158 if (*name) {
3159 int j, len;
3160 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02003161 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreauf278eec2020-07-05 21:46:32 +02003162 trash.area[j] = tolower((unsigned char)name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02003163 if (j >= trash.size)
William Lallemandfe49bb32019-10-03 23:46:33 +02003164 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003165 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02003166
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003167 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02003168 if (!sc)
William Lallemandfe49bb32019-10-03 23:46:33 +02003169 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003170 memcpy(sc->name.key, trash.area, len + 1);
William Lallemand02e19a52020-04-08 16:11:26 +02003171 SSL_CTX_up_ref(ctx);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003172 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003173 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003174 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003175 sc->order = order++;
3176 sc->neg = neg;
William Lallemand1d29c742019-10-04 00:53:29 +02003177 sc->wild = wild;
3178 sc->name.node.leaf_p = NULL;
William Lallemandcfca1422020-03-05 10:17:47 +01003179 sc->ckch_inst = ckch_inst;
Willy Tarreau2b718102021-04-21 07:32:39 +02003180 LIST_APPEND(&ckch_inst->sni_ctx, &sc->by_ckch_inst);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003181 }
3182 return order;
3183}
3184
William Lallemand6af03992019-07-23 15:00:54 +02003185/*
William Lallemand1d29c742019-10-04 00:53:29 +02003186 * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree
3187 * This function can't return an error.
3188 *
3189 * *CAUTION*: The caller must lock the sni tree if called in multithreading mode
3190 */
William Lallemandc756bbd2020-05-13 17:23:59 +02003191void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf)
William Lallemand1d29c742019-10-04 00:53:29 +02003192{
3193
3194 struct sni_ctx *sc0, *sc0b, *sc1;
3195 struct ebmb_node *node;
3196
3197 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
3198
3199 /* ignore if sc0 was already inserted in a tree */
3200 if (sc0->name.node.leaf_p)
3201 continue;
3202
3203 /* Check for duplicates. */
3204 if (sc0->wild)
3205 node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key);
3206 else
3207 node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key);
3208
3209 for (; node; node = ebmb_next_dup(node)) {
3210 sc1 = ebmb_entry(node, struct sni_ctx, name);
3211 if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf
3212 && sc1->neg == sc0->neg && sc1->wild == sc0->wild) {
3213 /* it's a duplicate, we should remove and free it */
Willy Tarreau2b718102021-04-21 07:32:39 +02003214 LIST_DELETE(&sc0->by_ckch_inst);
William Lallemand02e19a52020-04-08 16:11:26 +02003215 SSL_CTX_free(sc0->ctx);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003216 ha_free(&sc0);
William Lallemande15029b2019-10-14 10:46:58 +02003217 break;
William Lallemand1d29c742019-10-04 00:53:29 +02003218 }
3219 }
3220
3221 /* if duplicate, ignore the insertion */
3222 if (!sc0)
3223 continue;
3224
3225 if (sc0->wild)
3226 ebst_insert(&bind_conf->sni_w_ctx, &sc0->name);
3227 else
3228 ebst_insert(&bind_conf->sni_ctx, &sc0->name);
Remi Tricot-Le Breton8218aed2021-03-17 14:56:54 +01003229 }
William Lallemand21724f02019-11-04 17:56:13 +01003230
Remi Tricot-Le Breton8218aed2021-03-17 14:56:54 +01003231 /* replace the default_ctx if required with the instance's ctx. */
3232 if (ckch_inst->is_default) {
3233 SSL_CTX_free(bind_conf->default_ctx);
3234 SSL_CTX_up_ref(ckch_inst->ctx);
3235 bind_conf->default_ctx = ckch_inst->ctx;
Remi Tricot-Le Breton40ddea82021-04-13 16:07:29 +02003236 bind_conf->default_inst = ckch_inst;
William Lallemand1d29c742019-10-04 00:53:29 +02003237 }
3238}
3239
3240/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003241 * tree used to store the ckchs ordered by filename/bundle name
William Lallemand6af03992019-07-23 15:00:54 +02003242 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003243struct eb_root ckchs_tree = EB_ROOT_UNIQUE;
William Lallemand6af03992019-07-23 15:00:54 +02003244
William Lallemand2954c472020-03-06 21:54:13 +01003245/* tree of crtlist (crt-list/directory) */
William Lallemandc756bbd2020-05-13 17:23:59 +02003246struct eb_root crtlists_tree = EB_ROOT_UNIQUE;
William Lallemandfa892222019-07-23 16:06:08 +02003247
Emeric Brun7a883362019-10-17 13:27:40 +02003248/* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX.
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05003249 * If there is no DH parameter available in the ckchs, the global
Emeric Brun7a883362019-10-17 13:27:40 +02003250 * DH parameter is loaded into the SSL_CTX and if there is no
3251 * DH parameter available in ckchs nor in global, the default
3252 * DH parameters are applied on the SSL_CTX.
3253 * Returns a bitfield containing the flags:
3254 * ERR_FATAL in any fatal error case
3255 * ERR_ALERT if a reason of the error is availabine in err
3256 * ERR_WARN if a warning is available into err
3257 * The value 0 means there is no error nor warning and
3258 * the operation succeed.
3259 */
William Lallemandfa892222019-07-23 16:06:08 +02003260#ifndef OPENSSL_NO_DH
William Lallemand52ddd992022-11-22 11:51:53 +01003261static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct ckch_data *data,
Emeric Brun7a883362019-10-17 13:27:40 +02003262 const char *path, char **err)
William Lallemandfa892222019-07-23 16:06:08 +02003263{
Emeric Brun7a883362019-10-17 13:27:40 +02003264 int ret = 0;
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +01003265 HASSL_DH *dh = NULL;
William Lallemandfa892222019-07-23 16:06:08 +02003266
William Lallemand52ddd992022-11-22 11:51:53 +01003267 if (data && data->dh) {
3268 dh = data->dh;
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +01003269 if (!ssl_sock_set_tmp_dh(ctx, dh)) {
Emeric Bruna9363eb2019-10-17 14:53:03 +02003270 memprintf(err, "%sunable to load the DH parameter specified in '%s'",
3271 err && *err ? *err : "", path);
Emeric Bruna9363eb2019-10-17 14:53:03 +02003272 memprintf(err, "%s, DH ciphers won't be available.\n",
3273 err && *err ? *err : "");
Emeric Bruna9363eb2019-10-17 14:53:03 +02003274 ret |= ERR_WARN;
3275 goto end;
3276 }
William Lallemandfa892222019-07-23 16:06:08 +02003277
3278 if (ssl_dh_ptr_index >= 0) {
3279 /* store a pointer to the DH params to avoid complaining about
3280 ssl-default-dh-param not being set for this SSL_CTX */
3281 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
3282 }
3283 }
3284 else if (global_dh) {
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +01003285 if (!ssl_sock_set_tmp_dh(ctx, global_dh)) {
Emeric Bruna9363eb2019-10-17 14:53:03 +02003286 memprintf(err, "%sunable to use the global DH parameter for certificate '%s'",
3287 err && *err ? *err : "", path);
Emeric Bruna9363eb2019-10-17 14:53:03 +02003288 memprintf(err, "%s, DH ciphers won't be available.\n",
3289 err && *err ? *err : "");
Emeric Bruna9363eb2019-10-17 14:53:03 +02003290 ret |= ERR_WARN;
3291 goto end;
3292 }
William Lallemandfa892222019-07-23 16:06:08 +02003293 }
3294 else {
3295 /* Clear openssl global errors stack */
3296 ERR_clear_error();
3297
Remi Tricot-Le Breton1d6338e2022-04-12 11:31:55 +02003298 /* We do not want DHE ciphers to be added to the cipher list
3299 * unless there is an explicit global dh option in the conf.
3300 */
3301 if (global_ssl.default_dh_param) {
3302 if (global_ssl.default_dh_param <= 1024) {
3303 /* we are limited to DH parameter of 1024 bits anyway */
3304 if (local_dh_1024 == NULL)
3305 local_dh_1024 = ssl_get_dh_1024();
William Lallemandfa892222019-07-23 16:06:08 +02003306
Remi Tricot-Le Breton1d6338e2022-04-12 11:31:55 +02003307 if (local_dh_1024 == NULL) {
3308 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3309 err && *err ? *err : "", path);
3310 ret |= ERR_ALERT | ERR_FATAL;
3311 goto end;
3312 }
William Lallemandfa892222019-07-23 16:06:08 +02003313
Remi Tricot-Le Breton1d6338e2022-04-12 11:31:55 +02003314 if (!ssl_sock_set_tmp_dh(ctx, local_dh_1024)) {
3315 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3316 err && *err ? *err : "", path);
3317 memprintf(err, "%s, DH ciphers won't be available.\n",
3318 err && *err ? *err : "");
3319 ret |= ERR_WARN;
3320 goto end;
3321 }
Emeric Bruna9363eb2019-10-17 14:53:03 +02003322 }
Remi Tricot-Le Breton1d6338e2022-04-12 11:31:55 +02003323 else {
Remi Tricot-Le Breton88c56952022-02-11 12:04:56 +01003324#if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL)
Remi Tricot-Le Breton1d6338e2022-04-12 11:31:55 +02003325 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh_cbk);
Remi Tricot-Le Breton88c56952022-02-11 12:04:56 +01003326#else
William Lallemand52ddd992022-11-22 11:51:53 +01003327 ssl_sock_set_tmp_dh_from_pkey(ctx, data ? data->key : NULL);
Remi Tricot-Le Breton88c56952022-02-11 12:04:56 +01003328#endif
Remi Tricot-Le Breton1d6338e2022-04-12 11:31:55 +02003329 }
William Lallemandfa892222019-07-23 16:06:08 +02003330 }
William Lallemand8d0f8932019-10-17 18:03:58 +02003331 }
3332
William Lallemandf9568fc2019-10-16 18:27:58 +02003333end:
William Lallemandf9568fc2019-10-16 18:27:58 +02003334 ERR_clear_error();
William Lallemandf9568fc2019-10-16 18:27:58 +02003335 return ret;
3336}
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003337#endif
William Lallemandfa892222019-07-23 16:06:08 +02003338
Remi Tricot-Le Bretonec805a32021-01-25 17:19:42 +01003339
3340/* Load a certificate chain into an SSL context.
Emeric Bruna96b5822019-10-17 13:25:14 +02003341 * Returns a bitfield containing the flags:
3342 * ERR_FATAL in any fatal error case
3343 * ERR_ALERT if the reason of the error is available in err
3344 * ERR_WARN if a warning is available into err
Remi Tricot-Le Breton4cf0d3f2022-12-15 15:44:37 +01003345 * The caller is responsible of freeing the newly built or newly refcounted
3346 * find_chain element.
Emeric Bruna96b5822019-10-17 13:25:14 +02003347 * The value 0 means there is no error nor warning and
3348 * the operation succeed.
yanbzhu488a4d22015-12-01 15:16:07 -05003349 */
William Lallemand52ddd992022-11-22 11:51:53 +01003350static int ssl_sock_load_cert_chain(const char *path, const struct ckch_data *data,
Remi Tricot-Le Bretonec805a32021-01-25 17:19:42 +01003351 SSL_CTX *ctx, STACK_OF(X509) **find_chain, char **err)
yanbzhu488a4d22015-12-01 15:16:07 -05003352{
Emeric Bruna96b5822019-10-17 13:25:14 +02003353 int errcode = 0;
William Lallemand45fed2c2022-11-15 16:56:03 +01003354 int ret;
3355
3356 ERR_clear_error();
Emeric Bruna96b5822019-10-17 13:25:14 +02003357
Remi Tricot-Le Bretonec805a32021-01-25 17:19:42 +01003358 if (find_chain == NULL) {
3359 errcode |= ERR_FATAL;
3360 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003361 }
3362
William Lallemand52ddd992022-11-22 11:51:53 +01003363 if (!SSL_CTX_use_certificate(ctx, data->cert)) {
William Lallemand45fed2c2022-11-15 16:56:03 +01003364 ret = ERR_get_error();
3365 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s': %s.\n",
3366 err && *err ? *err : "", path, ERR_reason_error_string(ret));
Emeric Bruna96b5822019-10-17 13:25:14 +02003367 errcode |= ERR_ALERT | ERR_FATAL;
3368 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003369 }
3370
William Lallemand52ddd992022-11-22 11:51:53 +01003371 if (data->chain) {
Remi Tricot-Le Breton4cf0d3f2022-12-15 15:44:37 +01003372 *find_chain = X509_chain_up_ref(data->chain);
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003373 } else {
3374 /* Find Certificate Chain in global */
3375 struct issuer_chain *issuer;
William Lallemand52ddd992022-11-22 11:51:53 +01003376 issuer = ssl_get0_issuer_chain(data->cert);
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003377 if (issuer)
Remi Tricot-Le Breton4cf0d3f2022-12-15 15:44:37 +01003378 *find_chain = X509_chain_up_ref(issuer->chain);
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003379 }
William Lallemand85888572020-02-27 14:48:35 +01003380
Remi Tricot-Le Bretonec805a32021-01-25 17:19:42 +01003381 if (!*find_chain) {
William Lallemand935d8292020-08-12 20:02:10 +02003382 /* always put a null chain stack in the SSL_CTX so it does not
3383 * try to build the chain from the verify store */
Remi Tricot-Le Bretonec805a32021-01-25 17:19:42 +01003384 *find_chain = sk_X509_new_null();
William Lallemand935d8292020-08-12 20:02:10 +02003385 }
3386
William Lallemand52ddd992022-11-22 11:51:53 +01003387 /* Load all certs in the data into the ctx_chain for the ssl_ctx */
William Lallemandf187ce62020-06-02 18:27:20 +02003388#ifdef SSL_CTX_set1_chain
Remi Tricot-Le Bretonec805a32021-01-25 17:19:42 +01003389 if (!SSL_CTX_set1_chain(ctx, *find_chain)) {
William Lallemand45fed2c2022-11-15 16:56:03 +01003390 ret = ERR_get_error();
William Lallemanda21ca742023-04-17 14:32:25 +02003391 memprintf(err, "%sunable to load chain certificate into SSL Context '%s': %s.\n",
William Lallemand45fed2c2022-11-15 16:56:03 +01003392 err && *err ? *err : "", path, ERR_reason_error_string(ret));
William Lallemand935d8292020-08-12 20:02:10 +02003393 errcode |= ERR_ALERT | ERR_FATAL;
3394 goto end;
3395 }
William Lallemandf187ce62020-06-02 18:27:20 +02003396#else
3397 { /* legacy compat (< openssl 1.0.2) */
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003398 X509 *ca;
Remi Tricot-Le Breton4cf0d3f2022-12-15 15:44:37 +01003399 while ((ca = sk_X509_shift(*find_chain)))
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003400 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003401 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
3402 err && *err ? *err : "", path);
William Lallemandf187ce62020-06-02 18:27:20 +02003403 X509_free(ca);
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003404 errcode |= ERR_ALERT | ERR_FATAL;
3405 goto end;
3406 }
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003407 }
William Lallemandf187ce62020-06-02 18:27:20 +02003408#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003409
William Lallemand9a1d8392020-08-10 17:28:23 +02003410#ifdef SSL_CTX_build_cert_chain
William Lallemandbf298af2020-08-10 16:18:45 +02003411 /* remove the Root CA from the SSL_CTX if the option is activated */
3412 if (global_ssl.skip_self_issued_ca) {
3413 if (!SSL_CTX_build_cert_chain(ctx, SSL_BUILD_CHAIN_FLAG_NO_ROOT|SSL_BUILD_CHAIN_FLAG_UNTRUSTED|SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR)) {
3414 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
3415 err && *err ? *err : "", path);
3416 errcode |= ERR_ALERT | ERR_FATAL;
3417 goto end;
3418 }
3419 }
William Lallemand9a1d8392020-08-10 17:28:23 +02003420#endif
William Lallemandbf298af2020-08-10 16:18:45 +02003421
Remi Tricot-Le Bretonec805a32021-01-25 17:19:42 +01003422end:
3423 return errcode;
3424}
3425
3426
3427/* Loads the info in ckch into ctx
3428 * Returns a bitfield containing the flags:
3429 * ERR_FATAL in any fatal error case
3430 * ERR_ALERT if the reason of the error is available in err
3431 * ERR_WARN if a warning is available into err
3432 * The value 0 means there is no error nor warning and
3433 * the operation succeed.
3434 */
Remi Tricot-Le Bretoncc346672022-12-20 11:11:08 +01003435static int ssl_sock_put_ckch_into_ctx(const char *path, struct ckch_data *data, SSL_CTX *ctx, char **err)
Remi Tricot-Le Bretonec805a32021-01-25 17:19:42 +01003436{
3437 int errcode = 0;
3438 STACK_OF(X509) *find_chain = NULL;
3439
William Lallemand5de49512022-10-27 14:41:07 +02003440 ERR_clear_error();
3441
William Lallemand52ddd992022-11-22 11:51:53 +01003442 if (SSL_CTX_use_PrivateKey(ctx, data->key) <= 0) {
William Lallemand5de49512022-10-27 14:41:07 +02003443 int ret;
3444
3445 ret = ERR_get_error();
3446 memprintf(err, "%sunable to load SSL private key into SSL Context '%s': %s.\n",
3447 err && *err ? *err : "", path, ERR_reason_error_string(ret));
Remi Tricot-Le Bretonec805a32021-01-25 17:19:42 +01003448 errcode |= ERR_ALERT | ERR_FATAL;
3449 return errcode;
3450 }
3451
3452 /* Load certificate chain */
William Lallemand52ddd992022-11-22 11:51:53 +01003453 errcode |= ssl_sock_load_cert_chain(path, data, ctx, &find_chain, err);
Remi Tricot-Le Bretonec805a32021-01-25 17:19:42 +01003454 if (errcode & ERR_CODE)
3455 goto end;
3456
William Lallemandfa892222019-07-23 16:06:08 +02003457#ifndef OPENSSL_NO_DH
3458 /* store a NULL pointer to indicate we have not yet loaded
3459 a custom DH param file */
3460 if (ssl_dh_ptr_index >= 0) {
3461 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3462 }
3463
William Lallemand52ddd992022-11-22 11:51:53 +01003464 errcode |= ssl_sock_load_dh_params(ctx, data, path, err);
Emeric Brun7a883362019-10-17 13:27:40 +02003465 if (errcode & ERR_CODE) {
William Lallemandfa892222019-07-23 16:06:08 +02003466 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3467 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003468 goto end;
William Lallemandfa892222019-07-23 16:06:08 +02003469 }
3470#endif
3471
Ilya Shipitsin7bbf5862021-02-06 18:55:27 +05003472#ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT
William Lallemand52ddd992022-11-22 11:51:53 +01003473 if (sctl_ex_index >= 0 && data->sctl) {
3474 if (ssl_sock_load_sctl(ctx, data->sctl) < 0) {
William Lallemanda17f4112019-10-10 15:16:44 +02003475 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003476 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003477 errcode |= ERR_ALERT | ERR_FATAL;
3478 goto end;
William Lallemanda17f4112019-10-10 15:16:44 +02003479 }
3480 }
3481#endif
3482
Emmanuel Hocdeta73a2222020-10-26 13:55:30 +01003483#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Remi Tricot-Le Bretonb55be8c2022-12-20 11:11:12 +01003484 /* Load OCSP Info into context
3485 * If OCSP update mode is set to 'on', an entry will be created in the
3486 * ocsp tree even if no ocsp_response was known during init, unless the
Ilya Shipitsin07be66d2023-04-01 12:26:42 +02003487 * frontend's conf disables ocsp update explicitly.
Remi Tricot-Le Bretonb55be8c2022-12-20 11:11:12 +01003488 */
Remi Tricot-Le Breton0c96ee42023-03-01 16:11:50 +01003489 if (ssl_sock_load_ocsp(path, ctx, data, find_chain) < 0) {
Remi Tricot-Le Bretonb55be8c2022-12-20 11:11:12 +01003490 if (data->ocsp_response)
Tim Duesterhus93128532019-11-23 23:45:10 +01003491 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",
Remi Tricot-Le Bretonb55be8c2022-12-20 11:11:12 +01003492 err && *err ? *err : "", path);
3493 else
3494 memprintf(err, "%s '%s' has an OCSP URI and OCSP auto-update is set to 'on' but an error occurred (maybe the issuer could not be found)'.\n",
3495 err && *err ? *err : "", path);
3496 errcode |= ERR_ALERT | ERR_FATAL;
3497 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003498 }
William Lallemand246c0242019-10-11 08:59:13 +02003499#endif
3500
Emeric Bruna96b5822019-10-17 13:25:14 +02003501 end:
Remi Tricot-Le Breton4cf0d3f2022-12-15 15:44:37 +01003502 sk_X509_pop_free(find_chain, X509_free);
Emeric Bruna96b5822019-10-17 13:25:14 +02003503 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003504}
3505
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01003506
3507/* Loads the info of a ckch built out of a backend certificate into an SSL ctx
3508 * Returns a bitfield containing the flags:
3509 * ERR_FATAL in any fatal error case
3510 * ERR_ALERT if the reason of the error is available in err
3511 * ERR_WARN if a warning is available into err
3512 * The value 0 means there is no error nor warning and
3513 * the operation succeed.
3514 */
William Lallemand52ddd992022-11-22 11:51:53 +01003515static int ssl_sock_put_srv_ckch_into_ctx(const char *path, const struct ckch_data *data,
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01003516 SSL_CTX *ctx, char **err)
3517{
3518 int errcode = 0;
3519 STACK_OF(X509) *find_chain = NULL;
3520
3521 /* Load the private key */
William Lallemand52ddd992022-11-22 11:51:53 +01003522 if (SSL_CTX_use_PrivateKey(ctx, data->key) <= 0) {
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01003523 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
3524 err && *err ? *err : "", path);
3525 errcode |= ERR_ALERT | ERR_FATAL;
3526 }
3527
3528 /* Load certificate chain */
William Lallemand52ddd992022-11-22 11:51:53 +01003529 errcode |= ssl_sock_load_cert_chain(path, data, ctx, &find_chain, err);
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01003530 if (errcode & ERR_CODE)
3531 goto end;
3532
3533 if (SSL_CTX_check_private_key(ctx) <= 0) {
3534 memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n",
3535 err && *err ? *err : "", path);
3536 errcode |= ERR_ALERT | ERR_FATAL;
3537 }
3538
3539end:
Remi Tricot-Le Breton4cf0d3f2022-12-15 15:44:37 +01003540 sk_X509_pop_free(find_chain, X509_free);
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01003541 return errcode;
3542}
3543
3544
William Lallemand614ca0d2019-10-07 13:52:11 +02003545/*
3546 * This function allocate a ckch_inst and create its snis
Emeric Brun054563d2019-10-17 13:16:58 +02003547 *
3548 * Returns a bitfield containing the flags:
3549 * ERR_FATAL in any fatal error case
3550 * ERR_ALERT if the reason of the error is available in err
3551 * ERR_WARN if a warning is available into err
William Lallemand614ca0d2019-10-07 13:52:11 +02003552 */
William Lallemandc756bbd2020-05-13 17:23:59 +02003553int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf,
Emeric Brun054563d2019-10-17 13:16:58 +02003554 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003555{
William Lallemandc9402072019-05-15 15:33:54 +02003556 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02003557 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003558 int order = 0;
3559 X509_NAME *xname;
3560 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003561 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003562 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02003563#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3564 STACK_OF(GENERAL_NAME) *names;
3565#endif
William Lallemand52ddd992022-11-22 11:51:53 +01003566 struct ckch_data *data;
William Lallemand614ca0d2019-10-07 13:52:11 +02003567 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02003568 int errcode = 0;
3569
3570 *ckchi = NULL;
William Lallemanda59191b2019-05-15 16:08:56 +02003571
William Lallemand52ddd992022-11-22 11:51:53 +01003572 if (!ckchs || !ckchs->data)
Emeric Brun054563d2019-10-17 13:16:58 +02003573 return ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003574
William Lallemand52ddd992022-11-22 11:51:53 +01003575 data = ckchs->data;
William Lallemand36b84632019-07-18 19:28:17 +02003576
William Lallemandc9402072019-05-15 15:33:54 +02003577 ctx = SSL_CTX_new(SSLv23_server_method());
3578 if (!ctx) {
3579 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3580 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003581 errcode |= ERR_ALERT | ERR_FATAL;
3582 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02003583 }
3584
William Lallemand52ddd992022-11-22 11:51:53 +01003585 errcode |= ssl_sock_put_ckch_into_ctx(path, data, ctx, err);
Emeric Bruna96b5822019-10-17 13:25:14 +02003586 if (errcode & ERR_CODE)
William Lallemand614ca0d2019-10-07 13:52:11 +02003587 goto error;
William Lallemand614ca0d2019-10-07 13:52:11 +02003588
3589 ckch_inst = ckch_inst_new();
3590 if (!ckch_inst) {
3591 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3592 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003593 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02003594 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02003595 }
3596
William Lallemand52ddd992022-11-22 11:51:53 +01003597 pkey = X509_get_pubkey(data->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003598 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003599 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003600 switch(EVP_PKEY_base_id(pkey)) {
3601 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003602 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003603 break;
3604 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003605 kinfo.sig = TLSEXT_signature_ecdsa;
3606 break;
3607 case EVP_PKEY_DSA:
3608 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003609 break;
3610 }
3611 EVP_PKEY_free(pkey);
3612 }
3613
Emeric Brun50bcecc2013-04-22 13:05:23 +02003614 if (fcount) {
William Lallemandfe49bb32019-10-03 23:46:33 +02003615 while (fcount--) {
William Lallemand1d29c742019-10-04 00:53:29 +02003616 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 +02003617 if (order < 0) {
3618 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003619 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02003620 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02003621 }
3622 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003623 }
3624 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02003625#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand52ddd992022-11-22 11:51:53 +01003626 names = X509_get_ext_d2i(data->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003627 if (names) {
3628 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3629 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
3630 if (name->type == GEN_DNS) {
3631 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02003632 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003633 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02003634 if (order < 0) {
3635 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003636 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02003637 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02003638 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003639 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003640 }
3641 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003642 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003643 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003644#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand52ddd992022-11-22 11:51:53 +01003645 xname = X509_get_subject_name(data->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003646 i = -1;
3647 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3648 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003649 ASN1_STRING *value;
3650
3651 value = X509_NAME_ENTRY_get_data(entry);
3652 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02003653 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003654 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02003655 if (order < 0) {
3656 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003657 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02003658 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02003659 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003660 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003661 }
3662 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003663 /* we must not free the SSL_CTX anymore below, since it's already in
3664 * the tree, so it will be discovered and cleaned in time.
3665 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003666
Emeric Brunfc0421f2012-09-07 17:30:07 +02003667#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003668 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003669 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
3670 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003671 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02003672 goto error;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003673 }
3674#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003675 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003676 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003677 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01003678 ckch_inst->is_default = 1;
William Lallemand02e19a52020-04-08 16:11:26 +02003679 SSL_CTX_up_ref(ctx);
Remi Tricot-Le Breton40ddea82021-04-13 16:07:29 +02003680 bind_conf->default_inst = ckch_inst;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003681 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003682
Remi Tricot-Le Breton8218aed2021-03-17 14:56:54 +01003683 /* Always keep a reference to the newly constructed SSL_CTX in the
3684 * instance. This way if the instance has no SNIs, the SSL_CTX will
3685 * still be linked. */
3686 SSL_CTX_up_ref(ctx);
3687 ckch_inst->ctx = ctx;
3688
William Lallemand9117de92019-10-04 00:29:42 +02003689 /* everything succeed, the ckch instance can be used */
3690 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02003691 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01003692 ckch_inst->ckch_store = ckchs;
William Lallemand9117de92019-10-04 00:29:42 +02003693
William Lallemand02e19a52020-04-08 16:11:26 +02003694 SSL_CTX_free(ctx); /* we need to free the ctx since we incremented the refcount where it's used */
3695
Emeric Brun054563d2019-10-17 13:16:58 +02003696 *ckchi = ckch_inst;
3697 return errcode;
William Lallemandd9199372019-10-04 15:37:05 +02003698
3699error:
3700 /* free the allocated sni_ctxs */
William Lallemand614ca0d2019-10-07 13:52:11 +02003701 if (ckch_inst) {
William Lallemand02e19a52020-04-08 16:11:26 +02003702 if (ckch_inst->is_default)
3703 SSL_CTX_free(ctx);
3704
William Lallemandd9d5d1b2020-04-09 16:31:05 +02003705 ckch_inst_free(ckch_inst);
William Lallemand614ca0d2019-10-07 13:52:11 +02003706 ckch_inst = NULL;
William Lallemandd9199372019-10-04 15:37:05 +02003707 }
William Lallemandd9199372019-10-04 15:37:05 +02003708 SSL_CTX_free(ctx);
3709
Emeric Brun054563d2019-10-17 13:16:58 +02003710 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003711}
3712
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01003713
3714/*
3715 * This function allocate a ckch_inst that will be used on the backend side
3716 * (server line)
3717 *
3718 * Returns a bitfield containing the flags:
3719 * ERR_FATAL in any fatal error case
3720 * ERR_ALERT if the reason of the error is available in err
3721 * ERR_WARN if a warning is available into err
3722 */
3723int ckch_inst_new_load_srv_store(const char *path, struct ckch_store *ckchs,
William Lallemand795bd9b2021-01-26 11:27:42 +01003724 struct ckch_inst **ckchi, char **err)
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01003725{
3726 SSL_CTX *ctx;
William Lallemand52ddd992022-11-22 11:51:53 +01003727 struct ckch_data *data;
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01003728 struct ckch_inst *ckch_inst = NULL;
3729 int errcode = 0;
3730
3731 *ckchi = NULL;
3732
William Lallemand52ddd992022-11-22 11:51:53 +01003733 if (!ckchs || !ckchs->data)
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01003734 return ERR_FATAL;
3735
William Lallemand52ddd992022-11-22 11:51:53 +01003736 data = ckchs->data;
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01003737
3738 ctx = SSL_CTX_new(SSLv23_client_method());
3739 if (!ctx) {
3740 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3741 err && *err ? *err : "", path);
3742 errcode |= ERR_ALERT | ERR_FATAL;
3743 goto error;
3744 }
3745
William Lallemand52ddd992022-11-22 11:51:53 +01003746 errcode |= ssl_sock_put_srv_ckch_into_ctx(path, data, ctx, err);
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01003747 if (errcode & ERR_CODE)
3748 goto error;
3749
3750 ckch_inst = ckch_inst_new();
3751 if (!ckch_inst) {
3752 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3753 err && *err ? *err : "", path);
3754 errcode |= ERR_ALERT | ERR_FATAL;
3755 goto error;
3756 }
3757
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01003758 /* everything succeed, the ckch instance can be used */
3759 ckch_inst->bind_conf = NULL;
3760 ckch_inst->ssl_conf = NULL;
3761 ckch_inst->ckch_store = ckchs;
William Lallemand795bd9b2021-01-26 11:27:42 +01003762 ckch_inst->ctx = ctx;
William Lallemanddb26e2b2021-01-26 12:01:46 +01003763 ckch_inst->is_server_instance = 1;
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01003764
3765 *ckchi = ckch_inst;
3766 return errcode;
3767
3768error:
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01003769 SSL_CTX_free(ctx);
3770
3771 return errcode;
3772}
3773
Willy Tarreau8c5414a2019-10-16 17:06:25 +02003774/* Returns a set of ERR_* flags possibly with an error in <err>. */
William Lallemand614ca0d2019-10-07 13:52:11 +02003775static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs,
3776 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
William Lallemand24bde432020-03-09 16:48:43 +01003777 char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err)
William Lallemand614ca0d2019-10-07 13:52:11 +02003778{
Emeric Brun054563d2019-10-17 13:16:58 +02003779 int errcode = 0;
William Lallemand614ca0d2019-10-07 13:52:11 +02003780
3781 /* we found the ckchs in the tree, we can use it directly */
William Lallemande7eb1fe2020-09-16 16:17:51 +02003782 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 +02003783
Emeric Brun054563d2019-10-17 13:16:58 +02003784 if (errcode & ERR_CODE)
3785 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02003786
William Lallemand24bde432020-03-09 16:48:43 +01003787 ssl_sock_load_cert_sni(*ckch_inst, bind_conf);
William Lallemand614ca0d2019-10-07 13:52:11 +02003788
3789 /* succeed, add the instance to the ckch_store's list of instance */
Willy Tarreau2b718102021-04-21 07:32:39 +02003790 LIST_APPEND(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs));
Emeric Brun054563d2019-10-17 13:16:58 +02003791 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02003792}
3793
William Lallemanddb26e2b2021-01-26 12:01:46 +01003794/* This function generates a <struct ckch_inst *> for a <struct server *>, and
3795 * fill the SSL_CTX of the server.
3796 *
3797 * Returns a set of ERR_* flags possibly with an error in <err>. */
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01003798static int ssl_sock_load_srv_ckchs(const char *path, struct ckch_store *ckchs,
William Lallemanddb26e2b2021-01-26 12:01:46 +01003799 struct server *server, struct ckch_inst **ckch_inst, char **err)
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01003800{
3801 int errcode = 0;
3802
3803 /* we found the ckchs in the tree, we can use it directly */
William Lallemand795bd9b2021-01-26 11:27:42 +01003804 errcode |= ckch_inst_new_load_srv_store(path, ckchs, ckch_inst, err);
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01003805
3806 if (errcode & ERR_CODE)
3807 return errcode;
3808
William Lallemanddb26e2b2021-01-26 12:01:46 +01003809 (*ckch_inst)->server = server;
3810 /* Keep the reference to the SSL_CTX in the server. */
3811 SSL_CTX_up_ref((*ckch_inst)->ctx);
3812 server->ssl_ctx.ctx = (*ckch_inst)->ctx;
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01003813 /* succeed, add the instance to the ckch_store's list of instance */
Willy Tarreau2b718102021-04-21 07:32:39 +02003814 LIST_APPEND(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs));
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01003815 return errcode;
3816}
3817
William Lallemand6be66ec2020-03-06 22:26:32 +01003818
William Lallemand4c68bba2020-03-30 18:45:10 +02003819
3820
3821/* Make sure openssl opens /dev/urandom before the chroot. The work is only
3822 * done once. Zero is returned if the operation fails. No error is returned
3823 * if the random is said as not implemented, because we expect that openssl
3824 * will use another method once needed.
3825 */
Amaury Denoyellec593bcd2021-05-19 15:35:29 +02003826int ssl_initialize_random(void)
William Lallemand4c68bba2020-03-30 18:45:10 +02003827{
3828 unsigned char random;
3829 static int random_initialized = 0;
3830
3831 if (!random_initialized && RAND_bytes(&random, 1) != 0)
3832 random_initialized = 1;
3833
3834 return random_initialized;
3835}
3836
William Lallemand2954c472020-03-06 21:54:13 +01003837/* Load a crt-list file, this is done in 2 parts:
3838 * - store the content of the file in a crtlist structure with crtlist_entry structures
3839 * - generate the instances by iterating on entries in the crtlist struct
3840 *
3841 * Nothing is locked there, this function is used in the configuration parser.
3842 *
3843 * Returns a set of ERR_* flags possibly with an error in <err>.
3844 */
William Lallemand6be66ec2020-03-06 22:26:32 +01003845int 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 +01003846{
3847 struct crtlist *crtlist = NULL;
3848 struct ebmb_node *eb;
William Lallemand49398312020-03-30 17:01:33 +02003849 struct crtlist_entry *entry = NULL;
William Lallemand79d31ec2020-03-25 15:10:49 +01003850 struct bind_conf_list *bind_conf_node = NULL;
William Lallemand2954c472020-03-06 21:54:13 +01003851 int cfgerr = 0;
William Lallemand41ca9302020-04-08 13:15:18 +02003852 char *end;
William Lallemand2954c472020-03-06 21:54:13 +01003853
William Lallemand79d31ec2020-03-25 15:10:49 +01003854 bind_conf_node = malloc(sizeof(*bind_conf_node));
3855 if (!bind_conf_node) {
3856 memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : "");
3857 cfgerr |= ERR_FATAL | ERR_ALERT;
3858 goto error;
3859 }
3860 bind_conf_node->next = NULL;
3861 bind_conf_node->bind_conf = bind_conf;
3862
William Lallemand41ca9302020-04-08 13:15:18 +02003863 /* strip trailing slashes, including first one */
3864 for (end = file + strlen(file) - 1; end >= file && *end == '/'; end--)
3865 *end = 0;
3866
William Lallemand2954c472020-03-06 21:54:13 +01003867 /* look for an existing crtlist or create one */
3868 eb = ebst_lookup(&crtlists_tree, file);
3869 if (eb) {
3870 crtlist = ebmb_entry(eb, struct crtlist, node);
3871 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01003872 /* load a crt-list OR a directory */
3873 if (dir)
3874 cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err);
3875 else
3876 cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err);
3877
William Lallemand2954c472020-03-06 21:54:13 +01003878 if (!(cfgerr & ERR_CODE))
3879 ebst_insert(&crtlists_tree, &crtlist->node);
3880 }
3881
3882 if (cfgerr & ERR_CODE) {
3883 cfgerr |= ERR_FATAL | ERR_ALERT;
3884 goto error;
3885 }
3886
3887 /* generates ckch instance from the crtlist_entry */
3888 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
3889 struct ckch_store *store;
3890 struct ckch_inst *ckch_inst = NULL;
3891
3892 store = entry->node.key;
3893 cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err);
3894 if (cfgerr & ERR_CODE) {
3895 memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err);
3896 goto error;
3897 }
Willy Tarreau2b718102021-04-21 07:32:39 +02003898 LIST_APPEND(&entry->ckch_inst, &ckch_inst->by_crtlist_entry);
William Lallemandcaa16192020-04-08 16:29:15 +02003899 ckch_inst->crtlist_entry = entry;
William Lallemand2954c472020-03-06 21:54:13 +01003900 }
William Lallemand2954c472020-03-06 21:54:13 +01003901
William Lallemand79d31ec2020-03-25 15:10:49 +01003902 /* add the bind_conf to the list */
3903 bind_conf_node->next = crtlist->bind_conf;
3904 crtlist->bind_conf = bind_conf_node;
3905
William Lallemand2954c472020-03-06 21:54:13 +01003906 return cfgerr;
3907error:
3908 {
William Lallemand49398312020-03-30 17:01:33 +02003909 struct crtlist_entry *lastentry;
William Lallemand2954c472020-03-06 21:54:13 +01003910 struct ckch_inst *inst, *s_inst;
3911
William Lallemand49398312020-03-30 17:01:33 +02003912 lastentry = entry; /* which entry we tried to generate last */
3913 if (lastentry) {
3914 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
3915 if (entry == lastentry) /* last entry we tried to generate, no need to go further */
3916 break;
3917
3918 list_for_each_entry_safe(inst, s_inst, &entry->ckch_inst, by_crtlist_entry) {
William Lallemand2954c472020-03-06 21:54:13 +01003919
William Lallemand49398312020-03-30 17:01:33 +02003920 /* this was not generated for this bind_conf, skip */
3921 if (inst->bind_conf != bind_conf)
3922 continue;
3923
William Lallemandd9d5d1b2020-04-09 16:31:05 +02003924 /* free the sni_ctx and instance */
3925 ckch_inst_free(inst);
William Lallemand49398312020-03-30 17:01:33 +02003926 }
William Lallemand2954c472020-03-06 21:54:13 +01003927 }
William Lallemand2954c472020-03-06 21:54:13 +01003928 }
William Lallemand79d31ec2020-03-25 15:10:49 +01003929 free(bind_conf_node);
William Lallemand2954c472020-03-06 21:54:13 +01003930 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003931 return cfgerr;
3932}
3933
William Lallemand06b22a82020-03-16 14:45:55 +01003934/* Returns a set of ERR_* flags possibly with an error in <err>. */
3935int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
3936{
3937 struct stat buf;
William Lallemand06b22a82020-03-16 14:45:55 +01003938 int cfgerr = 0;
3939 struct ckch_store *ckchs;
William Lallemand24bde432020-03-09 16:48:43 +01003940 struct ckch_inst *ckch_inst = NULL;
William Lallemand06ce84a2020-11-20 15:36:13 +01003941 int found = 0; /* did we found a file to load ? */
William Lallemand06b22a82020-03-16 14:45:55 +01003942
3943 if ((ckchs = ckchs_lookup(path))) {
3944 /* we found the ckchs in the tree, we can use it directly */
William Lallemand06ce84a2020-11-20 15:36:13 +01003945 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
3946 found++;
3947 } else if (stat(path, &buf) == 0) {
3948 found++;
William Lallemand06b22a82020-03-16 14:45:55 +01003949 if (S_ISDIR(buf.st_mode) == 0) {
William Lallemandbd8e6ed2020-09-16 16:08:08 +02003950 ckchs = ckchs_load_cert_file(path, err);
William Lallemand06b22a82020-03-16 14:45:55 +01003951 if (!ckchs)
William Lallemand06ce84a2020-11-20 15:36:13 +01003952 cfgerr |= ERR_ALERT | ERR_FATAL;
3953 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01003954 } else {
William Lallemand06ce84a2020-11-20 15:36:13 +01003955 cfgerr |= ssl_sock_load_cert_list_file(path, 1, bind_conf, bind_conf->frontend, err);
William Lallemand06b22a82020-03-16 14:45:55 +01003956 }
3957 } else {
3958 /* stat failed, could be a bundle */
3959 if (global_ssl.extra_files & SSL_GF_BUNDLE) {
William Lallemanddfa93be2020-09-16 14:48:52 +02003960 char fp[MAXPATHLEN+1] = {0};
3961 int n = 0;
3962
3963 /* Load all possible certs and keys in separate ckch_store */
3964 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3965 struct stat buf;
3966 int ret;
3967
3968 ret = snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
3969 if (ret > sizeof(fp))
3970 continue;
3971
3972 if ((ckchs = ckchs_lookup(fp))) {
3973 cfgerr |= ssl_sock_load_ckchs(fp, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06ce84a2020-11-20 15:36:13 +01003974 found++;
William Lallemanddfa93be2020-09-16 14:48:52 +02003975 } else {
3976 if (stat(fp, &buf) == 0) {
William Lallemand06ce84a2020-11-20 15:36:13 +01003977 found++;
William Lallemandbd8e6ed2020-09-16 16:08:08 +02003978 ckchs = ckchs_load_cert_file(fp, err);
William Lallemanddfa93be2020-09-16 14:48:52 +02003979 if (!ckchs)
William Lallemand06ce84a2020-11-20 15:36:13 +01003980 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemanddfa93be2020-09-16 14:48:52 +02003981 cfgerr |= ssl_sock_load_ckchs(fp, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
3982 }
3983 }
3984 }
William Lallemandb7fdfdf2020-12-04 15:45:02 +01003985#if HA_OPENSSL_VERSION_NUMBER < 0x10101000L
3986 if (found) {
3987 memprintf(err, "%sCan't load '%s'. Loading a multi certificates bundle requires OpenSSL >= 1.1.1\n",
3988 err && *err ? *err : "", path);
3989 cfgerr |= ERR_ALERT | ERR_FATAL;
3990 }
3991#endif
William Lallemand06b22a82020-03-16 14:45:55 +01003992 }
3993 }
William Lallemand06ce84a2020-11-20 15:36:13 +01003994 if (!found) {
3995 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
3996 err && *err ? *err : "", path, strerror(errno));
3997 cfgerr |= ERR_ALERT | ERR_FATAL;
3998 }
William Lallemand06b22a82020-03-16 14:45:55 +01003999
4000 return cfgerr;
4001}
4002
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01004003
4004/* Create a full ssl context and ckch instance that will be used for a specific
4005 * backend server (server configuration line).
4006 * Returns a set of ERR_* flags possibly with an error in <err>.
4007 */
Amaury Denoyelle36aa4512021-05-21 16:22:11 +02004008int ssl_sock_load_srv_cert(char *path, struct server *server, int create_if_none, char **err)
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01004009{
4010 struct stat buf;
4011 int cfgerr = 0;
4012 struct ckch_store *ckchs;
4013 int found = 0; /* did we found a file to load ? */
4014
4015 if ((ckchs = ckchs_lookup(path))) {
4016 /* we found the ckchs in the tree, we can use it directly */
William Lallemanddb26e2b2021-01-26 12:01:46 +01004017 cfgerr |= ssl_sock_load_srv_ckchs(path, ckchs, server, &server->ssl_ctx.inst, err);
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01004018 found++;
Amaury Denoyelle36aa4512021-05-21 16:22:11 +02004019 } else {
4020 if (!create_if_none) {
4021 memprintf(err, "%sunable to stat SSL certificate '%s'.\n",
4022 err && *err ? *err : "", path);
4023 cfgerr |= ERR_ALERT | ERR_FATAL;
4024 goto out;
4025 }
4026
4027 if (stat(path, &buf) == 0) {
4028 /* We do not manage directories on backend side. */
4029 if (S_ISDIR(buf.st_mode) == 0) {
4030 ++found;
4031 ckchs = ckchs_load_cert_file(path, err);
4032 if (!ckchs)
4033 cfgerr |= ERR_ALERT | ERR_FATAL;
4034 cfgerr |= ssl_sock_load_srv_ckchs(path, ckchs, server, &server->ssl_ctx.inst, err);
4035 }
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01004036 }
4037 }
4038 if (!found) {
4039 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4040 err && *err ? *err : "", path, strerror(errno));
4041 cfgerr |= ERR_ALERT | ERR_FATAL;
4042 }
4043
Amaury Denoyelle36aa4512021-05-21 16:22:11 +02004044out:
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01004045 return cfgerr;
4046}
4047
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004048/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004049static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004050ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004051{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004052 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004053 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02004054 SSL_OP_ALL | /* all known workarounds for bugs */
4055 SSL_OP_NO_SSLv2 |
4056 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004057 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02004058 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02004059 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02004060 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02004061 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004062 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02004063 SSL_MODE_ENABLE_PARTIAL_WRITE |
4064 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01004065 SSL_MODE_RELEASE_BUFFERS |
4066 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004067 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004068 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004069 int flags = MC_SSL_O_ALL;
4070 int cfgerr = 0;
William Lallemand50df1cb2020-06-02 10:52:24 +02004071 const int default_min_ver = CONF_TLSV12;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004072
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004073 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004074 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004075
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004076 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01004077 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
4078 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4079 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004080 else
4081 flags = conf_ssl_methods->flags;
4082
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02004083 min = conf_ssl_methods->min;
4084 max = conf_ssl_methods->max;
William Lallemand50df1cb2020-06-02 10:52:24 +02004085
4086 /* default minimum is TLSV12, */
4087 if (!min) {
4088 if (!max || (max >= default_min_ver)) {
4089 min = default_min_ver;
4090 } else {
4091 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). "
4092 "Setting the ssl-min-ver to %s. Use 'ssl-min-ver' to fix this.\n",
4093 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, methodVersions[default_min_ver].name, methodVersions[max].name);
4094 min = max;
4095 }
4096 }
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004097 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02004098 if (min)
4099 flags |= (methodVersions[min].flag - 1);
4100 if (max)
4101 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004102 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004103 min = max = CONF_TLSV_NONE;
4104 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004105 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004106 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004107 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004108 if (min) {
4109 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004110 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
4111 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4112 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
4113 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004114 hole = 0;
4115 }
4116 max = i;
4117 }
4118 else {
4119 min = max = i;
4120 }
4121 }
4122 else {
4123 if (min)
4124 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004125 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004126 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004127 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
4128 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004129 cfgerr += 1;
4130 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004131 /* save real min/max in bind_conf */
4132 conf_ssl_methods->min = min;
4133 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004134
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004135#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004136 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08004137 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004138 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004139 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004140 else
William Lallemandd0712f32020-06-11 17:34:00 +02004141 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) {
4142 /* clear every version flags in case SSL_CTX_new()
4143 * returns an SSL_CTX with disabled versions */
4144 SSL_CTX_clear_options(ctx, methodVersions[i].option);
4145
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004146 if (flags & methodVersions[i].flag)
4147 options |= methodVersions[i].option;
William Lallemandd0712f32020-06-11 17:34:00 +02004148
4149 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004150#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004151 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004152 methodVersions[min].ctx_set_version(ctx, SET_MIN);
4153 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02004154#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004155
4156 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
4157 options |= SSL_OP_NO_TICKET;
4158 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
4159 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08004160
4161#ifdef SSL_OP_NO_RENEGOTIATION
4162 options |= SSL_OP_NO_RENEGOTIATION;
4163#endif
4164
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004165 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004166
Ilya Shipitsinbdec3ba2020-11-14 01:56:34 +05004167#ifdef SSL_MODE_ASYNC
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004168 if (global_ssl.async)
4169 mode |= SSL_MODE_ASYNC;
4170#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004171 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004172 if (global_ssl.life_time)
4173 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004174
4175#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand4b7938d2022-09-07 10:54:17 +02004176# ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004177 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
4178 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
William Lallemand4b7938d2022-09-07 10:54:17 +02004179# elif defined(HAVE_SSL_CLIENT_HELLO_CB)
4180# if defined(SSL_OP_NO_ANTI_REPLAY)
Olivier Houchard545989f2019-12-17 15:39:54 +01004181 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01004182 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
William Lallemand4b7938d2022-09-07 10:54:17 +02004183# endif /* ! SSL_OP_NO_ANTI_REPLAY */
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02004184 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
4185 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
William Lallemand4b7938d2022-09-07 10:54:17 +02004186# else /* ! OPENSSL_IS_BORINGSSL && ! HAVE_SSL_CLIENT_HELLO_CB */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004187 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
William Lallemand4b7938d2022-09-07 10:54:17 +02004188# endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02004189 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
William Lallemand4b7938d2022-09-07 10:54:17 +02004190#endif /* ! SSL_CTRL_SET_TLSEXT_HOSTNAME */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004191 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004192}
4193
William Lallemand4f45bb92017-10-30 20:08:51 +01004194
4195static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
4196{
4197 if (first == block) {
4198 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4199 if (first->len > 0)
4200 sh_ssl_sess_tree_delete(sh_ssl_sess);
4201 }
4202}
4203
4204/* return first block from sh_ssl_sess */
4205static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
4206{
Ilya Shipitsin2ca01582023-04-15 23:39:43 +02004207 return (struct shared_block *)((unsigned char *)sh_ssl_sess - offsetof(struct shared_block, data));
William Lallemand4f45bb92017-10-30 20:08:51 +01004208
4209}
4210
4211/* store a session into the cache
4212 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
4213 * data: asn1 encoded session
4214 * data_len: asn1 encoded session length
4215 * Returns 1 id session was stored (else 0)
4216 */
4217static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
4218{
4219 struct shared_block *first;
4220 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
4221
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004222 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01004223 if (!first) {
4224 /* Could not retrieve enough free blocks to store that session */
4225 return 0;
4226 }
4227
4228 /* STORE the key in the first elem */
4229 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
4230 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
4231 first->len = sizeof(struct sh_ssl_sess_hdr);
4232
4233 /* it returns the already existing node
4234 or current node if none, never returns null */
4235 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
4236 if (oldsh_ssl_sess != sh_ssl_sess) {
4237 /* NOTE: Row couldn't be in use because we lock read & write function */
4238 /* release the reserved row */
William Lallemand222e5a22023-01-31 14:12:28 +01004239 first->len = 0; /* the len must be liberated in order not to call the release callback on it */
William Lallemand4f45bb92017-10-30 20:08:51 +01004240 shctx_row_dec_hot(ssl_shctx, first);
4241 /* replace the previous session already in the tree */
4242 sh_ssl_sess = oldsh_ssl_sess;
4243 /* ignore the previous session data, only use the header */
4244 first = sh_ssl_sess_first_block(sh_ssl_sess);
4245 shctx_row_inc_hot(ssl_shctx, first);
4246 first->len = sizeof(struct sh_ssl_sess_hdr);
4247 }
4248
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02004249 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01004250 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004251 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01004252 }
4253
4254 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01004255
4256 return 1;
4257}
William Lallemanded0b5ad2017-10-30 19:36:36 +01004258
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004259/* SSL callback used when a new session is created while connecting to a server */
4260static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
4261{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02004262 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01004263 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004264
Willy Tarreau07d94e42018-09-20 10:57:52 +02004265 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004266
William Lallemand3ce6eed2021-02-08 10:43:44 +01004267 /* RWLOCK: only read lock the SSL cache even when writing in it because there is
4268 * one cache per thread, it only prevents to flush it from the CLI in
4269 * another thread */
4270
Olivier Houcharde6060c52017-11-16 17:42:52 +01004271 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
4272 int len;
4273 unsigned char *ptr;
William Lallemande18d4e82021-11-17 02:59:21 +01004274 const char *sni;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004275
Olivier Houcharde6060c52017-11-16 17:42:52 +01004276 len = i2d_SSL_SESSION(sess, NULL);
William Lallemande18d4e82021-11-17 02:59:21 +01004277 sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
William Lallemand3ce6eed2021-02-08 10:43:44 +01004278 HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock);
Olivier Houcharde6060c52017-11-16 17:42:52 +01004279 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
4280 ptr = s->ssl_ctx.reused_sess[tid].ptr;
4281 } else {
Willy Tarreau3bda3f42021-02-26 21:05:08 +01004282 ptr = realloc(s->ssl_ctx.reused_sess[tid].ptr, len);
4283 s->ssl_ctx.reused_sess[tid].ptr = ptr;
Olivier Houcharde6060c52017-11-16 17:42:52 +01004284 s->ssl_ctx.reused_sess[tid].allocated_size = len;
4285 }
4286 if (s->ssl_ctx.reused_sess[tid].ptr) {
4287 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
4288 &ptr);
4289 }
William Lallemande18d4e82021-11-17 02:59:21 +01004290
4291 if (s->ssl_ctx.reused_sess[tid].sni) {
4292 /* if the new sni is empty or isn' t the same as the old one */
4293 if ((!sni) || strcmp(s->ssl_ctx.reused_sess[tid].sni, sni) != 0) {
4294 ha_free(&s->ssl_ctx.reused_sess[tid].sni);
4295 if (sni)
4296 s->ssl_ctx.reused_sess[tid].sni = strdup(sni);
4297 }
4298 } else if (sni) {
4299 /* if there wasn't an old sni but there is a new one */
4300 s->ssl_ctx.reused_sess[tid].sni = strdup(sni);
4301 }
William Lallemand3ce6eed2021-02-08 10:43:44 +01004302 HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock);
Olivier Houcharde6060c52017-11-16 17:42:52 +01004303 } else {
William Lallemand3ce6eed2021-02-08 10:43:44 +01004304 HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01004305 ha_free(&s->ssl_ctx.reused_sess[tid].ptr);
William Lallemand3ce6eed2021-02-08 10:43:44 +01004306 HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock);
Olivier Houcharde6060c52017-11-16 17:42:52 +01004307 }
4308
4309 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004310}
4311
Olivier Houcharde6060c52017-11-16 17:42:52 +01004312
William Lallemanded0b5ad2017-10-30 19:36:36 +01004313/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01004314int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004315{
4316 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
4317 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
4318 unsigned char *p;
4319 int data_len;
Emeric Bruneb469652019-10-08 18:27:37 +02004320 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004321 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004322
4323 /* Session id is already stored in to key and session id is known
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05004324 * so we don't store it to keep size.
Emeric Bruneb469652019-10-08 18:27:37 +02004325 * note: SSL_SESSION_set1_id is using
4326 * a memcpy so we need to use a different pointer
4327 * than sid_data or sid_ctx_data to avoid valgrind
4328 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01004329 */
4330
4331 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Bruneb469652019-10-08 18:27:37 +02004332
4333 /* copy value in an other buffer */
4334 memcpy(encid, sid_data, sid_length);
4335
4336 /* pad with 0 */
4337 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
4338 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
4339
4340 /* force length to zero to avoid ASN1 encoding */
4341 SSL_SESSION_set1_id(sess, encid, 0);
4342
4343 /* force length to zero to avoid ASN1 encoding */
4344 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004345
4346 /* check if buffer is large enough for the ASN1 encoded session */
4347 data_len = i2d_SSL_SESSION(sess, NULL);
4348 if (data_len > SHSESS_MAX_DATA_LEN)
4349 goto err;
4350
4351 p = encsess;
4352
4353 /* process ASN1 session encoding before the lock */
4354 i2d_SSL_SESSION(sess, &p);
4355
William Lallemanded0b5ad2017-10-30 19:36:36 +01004356
William Lallemanda3c77cf2017-10-30 23:44:40 +01004357 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004358 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004359 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01004360 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004361err:
4362 /* reset original length values */
Emeric Bruneb469652019-10-08 18:27:37 +02004363 SSL_SESSION_set1_id(sess, encid, sid_length);
4364 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004365
4366 return 0; /* do not increment session reference count */
4367}
4368
4369/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004370SSL_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 +01004371{
William Lallemand4f45bb92017-10-30 20:08:51 +01004372 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004373 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
4374 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01004375 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01004376 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004377
Willy Tarreau4c19e992021-06-15 16:39:22 +02004378 _HA_ATOMIC_INC(&global.shctx_lookups);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004379
4380 /* allow the session to be freed automatically by openssl */
4381 *do_copy = 0;
4382
4383 /* tree key is zeros padded sessionid */
4384 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4385 memcpy(tmpkey, key, key_len);
4386 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
4387 key = tmpkey;
4388 }
4389
4390 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004391 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004392
4393 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004394 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
4395 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004396 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004397 shctx_unlock(ssl_shctx);
Willy Tarreau4c19e992021-06-15 16:39:22 +02004398 _HA_ATOMIC_INC(&global.shctx_misses);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004399 return NULL;
4400 }
4401
William Lallemand4f45bb92017-10-30 20:08:51 +01004402 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
4403 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004404
William Lallemand4f45bb92017-10-30 20:08:51 +01004405 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 +01004406
William Lallemanda3c77cf2017-10-30 23:44:40 +01004407 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004408
4409 /* decode ASN1 session */
4410 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01004411 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01004412 /* Reset session id and session id contenxt */
4413 if (sess) {
4414 SSL_SESSION_set1_id(sess, key, key_len);
4415 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4416 }
4417
4418 return sess;
4419}
4420
William Lallemand4f45bb92017-10-30 20:08:51 +01004421
William Lallemanded0b5ad2017-10-30 19:36:36 +01004422/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01004423void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004424{
William Lallemand4f45bb92017-10-30 20:08:51 +01004425 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01004426 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
4427 unsigned int sid_length;
4428 const unsigned char *sid_data;
4429 (void)ctx;
4430
4431 sid_data = SSL_SESSION_get_id(sess, &sid_length);
4432 /* tree key is zeros padded sessionid */
4433 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
4434 memcpy(tmpkey, sid_data, sid_length);
4435 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
4436 sid_data = tmpkey;
4437 }
4438
William Lallemanda3c77cf2017-10-30 23:44:40 +01004439 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004440
4441 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004442 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
4443 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004444 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004445 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004446 }
4447
4448 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004449 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004450}
4451
4452/* Set session cache mode to server and disable openssl internal cache.
4453 * Set shared cache callbacks on an ssl context.
4454 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01004455void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004456{
4457 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4458
4459 if (!ssl_shctx) {
4460 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
4461 return;
4462 }
4463
4464 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
4465 SSL_SESS_CACHE_NO_INTERNAL |
4466 SSL_SESS_CACHE_NO_AUTO_CLEAR);
4467
4468 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01004469 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
4470 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
4471 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004472}
William Lallemand7d42ef52020-07-06 11:41:30 +02004473
4474/*
4475 * https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format
4476 *
4477 * The format is:
4478 * * <Label> <space> <ClientRandom> <space> <Secret>
4479 * We only need to copy the secret as there is a sample fetch for the ClientRandom
4480 */
4481
William Lallemand722180a2021-06-09 16:46:12 +02004482#ifdef HAVE_SSL_KEYLOG
William Lallemand7d42ef52020-07-06 11:41:30 +02004483void SSL_CTX_keylog(const SSL *ssl, const char *line)
4484{
4485 struct ssl_keylog *keylog;
4486 char *lastarg = NULL;
4487 char *dst = NULL;
4488
4489 keylog = SSL_get_ex_data(ssl, ssl_keylog_index);
4490 if (!keylog)
4491 return;
4492
4493 lastarg = strrchr(line, ' ');
4494 if (lastarg == NULL || ++lastarg == NULL)
4495 return;
4496
4497 dst = pool_alloc(pool_head_ssl_keylog_str);
4498 if (!dst)
4499 return;
4500
4501 strncpy(dst, lastarg, SSL_KEYLOG_MAX_SECRET_SIZE-1);
4502 dst[SSL_KEYLOG_MAX_SECRET_SIZE-1] = '\0';
4503
4504 if (strncmp(line, "CLIENT_RANDOM ", strlen("CLIENT RANDOM ")) == 0) {
4505 if (keylog->client_random)
4506 goto error;
4507 keylog->client_random = dst;
4508
4509 } else if (strncmp(line, "CLIENT_EARLY_TRAFFIC_SECRET ", strlen("CLIENT_EARLY_TRAFFIC_SECRET ")) == 0) {
4510 if (keylog->client_early_traffic_secret)
4511 goto error;
4512 keylog->client_early_traffic_secret = dst;
4513
4514 } else if (strncmp(line, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ", strlen("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")) == 0) {
4515 if(keylog->client_handshake_traffic_secret)
4516 goto error;
4517 keylog->client_handshake_traffic_secret = dst;
4518
4519 } else if (strncmp(line, "SERVER_HANDSHAKE_TRAFFIC_SECRET ", strlen("SERVER_HANDSHAKE_TRAFFIC_SECRET ")) == 0) {
4520 if (keylog->server_handshake_traffic_secret)
4521 goto error;
4522 keylog->server_handshake_traffic_secret = dst;
4523
4524 } else if (strncmp(line, "CLIENT_TRAFFIC_SECRET_0 ", strlen("CLIENT_TRAFFIC_SECRET_0 ")) == 0) {
4525 if (keylog->client_traffic_secret_0)
4526 goto error;
4527 keylog->client_traffic_secret_0 = dst;
4528
4529 } else if (strncmp(line, "SERVER_TRAFFIC_SECRET_0 ", strlen("SERVER_TRAFFIC_SECRET_0 ")) == 0) {
4530 if (keylog->server_traffic_secret_0)
4531 goto error;
4532 keylog->server_traffic_secret_0 = dst;
4533
4534 } else if (strncmp(line, "EARLY_EXPORTER_SECRET ", strlen("EARLY_EXPORTER_SECRET ")) == 0) {
4535 if (keylog->early_exporter_secret)
4536 goto error;
4537 keylog->early_exporter_secret = dst;
4538
4539 } else if (strncmp(line, "EXPORTER_SECRET ", strlen("EXPORTER_SECRET ")) == 0) {
4540 if (keylog->exporter_secret)
4541 goto error;
4542 keylog->exporter_secret = dst;
4543 } else {
4544 goto error;
4545 }
4546
4547 return;
4548
4549error:
4550 pool_free(pool_head_ssl_keylog_str, dst);
4551
4552 return;
4553}
4554#endif
William Lallemanded0b5ad2017-10-30 19:36:36 +01004555
William Lallemand8b453912019-11-21 15:48:10 +01004556/*
4557 * This function applies the SSL configuration on a SSL_CTX
4558 * It returns an error code and fills the <err> buffer
4559 */
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01004560static int 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 +01004561{
4562 struct proxy *curproxy = bind_conf->frontend;
4563 int cfgerr = 0;
4564 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01004565 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004566 const char *conf_ciphers;
Ilya Shipitsinf34ed0b2020-11-21 14:37:34 +05004567#ifdef HAVE_SSL_CTX_SET_CIPHERSUITES
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004568 const char *conf_ciphersuites;
4569#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004570 const char *conf_curves = NULL;
William Lallemand64a77e32023-05-02 18:26:46 +02004571 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
William Lallemand1d3c8222023-05-04 15:33:55 +02004572#if defined(SSL_CTX_set1_sigalgs_list)
4573 const char *conf_sigalgs = NULL;
4574#endif
William Lallemandb6ae2aa2023-05-05 00:05:46 +02004575#if defined(SSL_CTX_set1_client_sigalgs_list)
4576 const char *conf_client_sigalgs = NULL;
4577#endif
Emeric Brunfc0421f2012-09-07 17:30:07 +02004578
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004579 if (ssl_conf) {
4580 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
4581 int i, min, max;
4582 int flags = MC_SSL_O_ALL;
4583
4584 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004585 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
4586 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004587 if (min)
4588 flags |= (methodVersions[min].flag - 1);
4589 if (max)
4590 flags |= ~((methodVersions[max].flag << 1) - 1);
4591 min = max = CONF_TLSV_NONE;
4592 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4593 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
4594 if (min)
4595 max = i;
4596 else
4597 min = max = i;
4598 }
4599 /* save real min/max */
4600 conf_ssl_methods->min = min;
4601 conf_ssl_methods->max = max;
4602 if (!min) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004603 memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
4604 err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004605 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004606 }
4607 }
4608
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004609 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01004610 case SSL_SOCK_VERIFY_NONE:
4611 verify = SSL_VERIFY_NONE;
4612 break;
4613 case SSL_SOCK_VERIFY_OPTIONAL:
4614 verify = SSL_VERIFY_PEER;
4615 break;
4616 case SSL_SOCK_VERIFY_REQUIRED:
4617 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
4618 break;
4619 }
4620 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
4621 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004622 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 +01004623 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 +01004624 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 +01004625 if (ca_file || ca_verify_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02004626 /* set CAfile to verify */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01004627 if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02004628 memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01004629 err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004630 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02004631 }
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01004632 if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) {
4633 memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n",
4634 err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line);
4635 cfgerr |= ERR_ALERT | ERR_FATAL;
4636 }
4637 if (ca_file && !((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02004638 /* set CA names for client cert request, function returns void */
Emmanuel Hocdet129d3282019-10-24 18:08:51 +02004639 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 +02004640 }
William Lallemand64a77e32023-05-02 18:26:46 +02004641#ifdef USE_OPENSSL_WOLFSSL
4642 /* WolfSSL activates CRL checks by default so we need to disable it */
4643 X509_STORE_set_flags(store, 0) ;
4644#endif
Emeric Brund94b3fe2012-09-20 18:23:56 +02004645 }
Emeric Brun850efd52014-01-29 12:24:34 +01004646 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01004647 memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
4648 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004649 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun850efd52014-01-29 12:24:34 +01004650 }
Emeric Brun051cdab2012-10-02 19:25:50 +02004651#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004652 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02004653
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01004654 if (!ssl_set_cert_crl_file(store, crl_file)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004655 memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
4656 err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004657 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02004658 }
Emeric Brun561e5742012-10-02 15:20:55 +02004659 else {
4660 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
4661 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02004662 }
Emeric Brun051cdab2012-10-02 19:25:50 +02004663#endif
Emeric Brun644cde02012-12-14 11:21:13 +01004664 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02004665 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004666#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02004667 if(bind_conf->keys_ref) {
Remi Tricot-Le Breton8ea1f5f2022-02-08 17:45:58 +01004668 if (!SSL_CTX_set_tlsext_ticket_key_evp_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004669 memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
4670 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004671 cfgerr |= ERR_ALERT | ERR_FATAL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004672 }
4673 }
4674#endif
4675
William Lallemand4f45bb92017-10-30 20:08:51 +01004676 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004677 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
4678 if (conf_ciphers &&
4679 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004680 memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
4681 err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004682 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004683 }
4684
Ilya Shipitsinf34ed0b2020-11-21 14:37:34 +05004685#ifdef HAVE_SSL_CTX_SET_CIPHERSUITES
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004686 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
4687 if (conf_ciphersuites &&
4688 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004689 memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
4690 err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004691 cfgerr |= ERR_ALERT | ERR_FATAL;
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004692 }
4693#endif
4694
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01004695#ifndef OPENSSL_NO_DH
Remi Tricot-Le Breton1d6338e2022-04-12 11:31:55 +02004696 if (!local_dh_1024)
4697 local_dh_1024 = ssl_get_dh_1024();
4698 if (!local_dh_2048)
4699 local_dh_2048 = ssl_get_dh_2048();
4700 if (!local_dh_4096)
4701 local_dh_4096 = ssl_get_dh_4096();
Remi Gacogne8de54152014-07-15 11:36:40 +02004702#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004703
Emeric Brunfc0421f2012-09-07 17:30:07 +02004704 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Ilya Shipitsin7ff77472021-02-08 16:55:06 +05004705#ifdef SSL_CTRL_SET_MSG_CALLBACK
Emeric Brun29f037d2014-04-25 19:05:36 +02004706 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02004707#endif
William Lallemand722180a2021-06-09 16:46:12 +02004708#ifdef HAVE_SSL_KEYLOG
William Lallemandb60a77b2022-11-18 15:00:15 +01004709 /* only activate the keylog callback if it was required to prevent performance loss */
4710 if (global_ssl.keylog > 0)
4711 SSL_CTX_set_keylog_callback(ctx, SSL_CTX_keylog);
William Lallemand7d42ef52020-07-06 11:41:30 +02004712#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02004713
Bernard Spil13c53f82018-02-15 13:34:58 +01004714#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004715 ssl_conf_cur = NULL;
4716 if (ssl_conf && ssl_conf->npn_str)
4717 ssl_conf_cur = ssl_conf;
4718 else if (bind_conf->ssl_conf.npn_str)
4719 ssl_conf_cur = &bind_conf->ssl_conf;
4720 if (ssl_conf_cur)
4721 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02004722#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01004723#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004724 ssl_conf_cur = NULL;
4725 if (ssl_conf && ssl_conf->alpn_str)
4726 ssl_conf_cur = ssl_conf;
4727 else if (bind_conf->ssl_conf.alpn_str)
4728 ssl_conf_cur = &bind_conf->ssl_conf;
Willy Tarreaua2a09552023-04-19 09:05:49 +02004729 if (ssl_conf_cur && ssl_conf_cur->alpn_len)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004730 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02004731#endif
Ilya Shipitsin0aa8c292020-11-04 00:39:07 +05004732#if defined(SSL_CTX_set1_curves_list)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004733 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
4734 if (conf_curves) {
4735 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004736 memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
4737 err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004738 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004739 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01004740 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004741 }
Remi Tricot-Le Bretonff4c3c42022-02-08 17:45:54 +01004742#endif /* defined(SSL_CTX_set1_curves_list) */
4743
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004744 if (!conf_curves) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004745#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Remi Tricot-Le Bretonff4c3c42022-02-08 17:45:54 +01004746#if defined(SSL_CTX_set1_curves_list)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004747 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02004748 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
4749 NULL);
4750
Remi Tricot-Le Bretonff4c3c42022-02-08 17:45:54 +01004751 if (ecdhe && SSL_CTX_set1_curves_list(ctx, ecdhe) == 0) {
4752 memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
4753 err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
4754 cfgerr |= ERR_ALERT | ERR_FATAL;
Olivier Houchardc2aae742017-09-22 18:26:28 +02004755 }
Remi Tricot-Le Bretonff4c3c42022-02-08 17:45:54 +01004756#endif /* defined(SSL_CTX_set1_curves_list) */
Olivier Houchardc2aae742017-09-22 18:26:28 +02004757#else
Remi Tricot-Le Bretonff4c3c42022-02-08 17:45:54 +01004758#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
4759 int i;
4760 EC_KEY *ecdh;
4761
Olivier Houchardc2aae742017-09-22 18:26:28 +02004762 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
4763 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
4764 ECDHE_DEFAULT_CURVE);
Emeric Brun2b58d042012-09-20 17:10:03 +02004765
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004766 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02004767 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004768 memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
Remi Tricot-Le Bretonff4c3c42022-02-08 17:45:54 +01004769 err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004770 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun2b58d042012-09-20 17:10:03 +02004771 }
4772 else {
4773 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
4774 EC_KEY_free(ecdh);
4775 }
Remi Tricot-Le Bretonff4c3c42022-02-08 17:45:54 +01004776#endif /* defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) */
4777#endif /* HA_OPENSSL_VERSION_NUMBER >= 0x10101000L */
Emeric Brun2b58d042012-09-20 17:10:03 +02004778 }
William Lallemand1d3c8222023-05-04 15:33:55 +02004779
4780#if defined(SSL_CTX_set1_sigalgs_list)
4781 conf_sigalgs = (ssl_conf && ssl_conf->sigalgs) ? ssl_conf->sigalgs : bind_conf->ssl_conf.sigalgs;
4782 if (conf_sigalgs) {
4783 if (!SSL_CTX_set1_sigalgs_list(ctx, conf_sigalgs)) {
4784 memprintf(err, "%sProxy '%s': unable to set SSL Signature Algorithm list to '%s' for bind '%s' at [%s:%d].\n",
4785 err && *err ? *err : "", curproxy->id, conf_sigalgs, bind_conf->arg, bind_conf->file, bind_conf->line);
4786 cfgerr |= ERR_ALERT | ERR_FATAL;
4787 }
4788 }
4789#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02004790
William Lallemandb6ae2aa2023-05-05 00:05:46 +02004791#if defined(SSL_CTX_set1_client_sigalgs_list)
4792 conf_client_sigalgs = (ssl_conf && ssl_conf->client_sigalgs) ? ssl_conf->client_sigalgs : bind_conf->ssl_conf.client_sigalgs;
4793 if (conf_client_sigalgs) {
4794 if (!SSL_CTX_set1_client_sigalgs_list(ctx, conf_client_sigalgs)) {
4795 memprintf(err, "%sProxy '%s': unable to set SSL Signature Algorithm list to '%s' for bind '%s' at [%s:%d].\n",
4796 err && *err ? *err : "", curproxy->id, conf_client_sigalgs, bind_conf->arg, bind_conf->file, bind_conf->line);
4797 cfgerr |= ERR_ALERT | ERR_FATAL;
4798 }
4799 }
4800#endif
4801
Emeric Brunfc0421f2012-09-07 17:30:07 +02004802 return cfgerr;
4803}
4804
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01004805
4806/*
4807 * Prepare the SSL_CTX based on the bind line configuration.
4808 * Since the CA file loading is made depending on the verify option of the bind
4809 * line, the link between the SSL_CTX and the CA file tree entry is made here.
4810 * If we want to create a link between the CA file entry and the corresponding
4811 * ckch instance (for CA file hot update), it needs to be done after
4812 * ssl_sock_prepare_ctx.
4813 * Returns 0 in case of success.
4814 */
4815int ssl_sock_prep_ctx_and_inst(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4816 SSL_CTX *ctx, struct ckch_inst *ckch_inst, char **err)
4817{
4818 int errcode = 0;
4819
4820 errcode |= ssl_sock_prepare_ctx(bind_conf, ssl_conf, ctx, err);
4821 if (!errcode && ckch_inst)
4822 ckch_inst_add_cafile_link(ckch_inst, bind_conf, ssl_conf, NULL);
4823
4824 return errcode;
4825}
4826
Evan Broderbe554312013-06-27 00:05:25 -07004827static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
4828{
4829 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
4830 size_t prefixlen, suffixlen;
4831
4832 /* Trivial case */
William Lallemand2d6fd0a2020-09-14 15:20:10 +02004833 if (strcasecmp(pattern, hostname) == 0)
Evan Broderbe554312013-06-27 00:05:25 -07004834 return 1;
4835
Evan Broderbe554312013-06-27 00:05:25 -07004836 /* The rest of this logic is based on RFC 6125, section 6.4.3
4837 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
4838
Emeric Bruna848dae2013-10-08 11:27:28 +02004839 pattern_wildcard = NULL;
4840 pattern_left_label_end = pattern;
4841 while (*pattern_left_label_end != '.') {
4842 switch (*pattern_left_label_end) {
4843 case 0:
4844 /* End of label not found */
4845 return 0;
4846 case '*':
4847 /* If there is more than one wildcards */
4848 if (pattern_wildcard)
4849 return 0;
4850 pattern_wildcard = pattern_left_label_end;
4851 break;
4852 }
4853 pattern_left_label_end++;
4854 }
4855
4856 /* If it's not trivial and there is no wildcard, it can't
4857 * match */
4858 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07004859 return 0;
4860
4861 /* Make sure all labels match except the leftmost */
4862 hostname_left_label_end = strchr(hostname, '.');
4863 if (!hostname_left_label_end
William Lallemand2d6fd0a2020-09-14 15:20:10 +02004864 || strcasecmp(pattern_left_label_end, hostname_left_label_end) != 0)
Evan Broderbe554312013-06-27 00:05:25 -07004865 return 0;
4866
4867 /* Make sure the leftmost label of the hostname is long enough
4868 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02004869 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07004870 return 0;
4871
4872 /* Finally compare the string on either side of the
4873 * wildcard */
4874 prefixlen = pattern_wildcard - pattern;
4875 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
William Lallemand2d6fd0a2020-09-14 15:20:10 +02004876 if ((prefixlen && (strncasecmp(pattern, hostname, prefixlen) != 0))
4877 || (suffixlen && (strncasecmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07004878 return 0;
4879
4880 return 1;
4881}
4882
4883static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
4884{
4885 SSL *ssl;
4886 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01004887 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004888 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02004889 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07004890
4891 int depth;
4892 X509 *cert;
4893 STACK_OF(GENERAL_NAME) *alt_names;
4894 int i;
4895 X509_NAME *cert_subject;
4896 char *str;
4897
4898 if (ok == 0)
4899 return ok;
4900
4901 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02004902 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Willy Tarreau3a0a0d62022-04-12 07:31:06 +02004903 ssl_ctx = __conn_get_ssl_sock_ctx(conn);
Evan Broderbe554312013-06-27 00:05:25 -07004904
Willy Tarreauad92a9a2017-07-28 11:38:41 +02004905 /* We're checking if the provided hostnames match the desired one. The
4906 * desired hostname comes from the SNI we presented if any, or if not
4907 * provided then it may have been explicitly stated using a "verifyhost"
4908 * directive. If neither is set, we don't care about the name so the
4909 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02004910 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01004911 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02004912 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004913 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02004914 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004915 if (!servername)
4916 return ok;
4917 }
Evan Broderbe554312013-06-27 00:05:25 -07004918
4919 /* We only need to verify the CN on the actual server cert,
4920 * not the indirect CAs */
4921 depth = X509_STORE_CTX_get_error_depth(ctx);
4922 if (depth != 0)
4923 return ok;
4924
4925 /* At this point, the cert is *not* OK unless we can find a
4926 * hostname match */
4927 ok = 0;
4928
4929 cert = X509_STORE_CTX_get_current_cert(ctx);
4930 /* It seems like this might happen if verify peer isn't set */
4931 if (!cert)
4932 return ok;
4933
4934 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
4935 if (alt_names) {
4936 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
4937 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
4938 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004939#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02004940 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
4941#else
Evan Broderbe554312013-06-27 00:05:25 -07004942 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02004943#endif
Evan Broderbe554312013-06-27 00:05:25 -07004944 ok = ssl_sock_srv_hostcheck(str, servername);
4945 OPENSSL_free(str);
4946 }
4947 }
4948 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02004949 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07004950 }
4951
4952 cert_subject = X509_get_subject_name(cert);
4953 i = -1;
4954 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
4955 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004956 ASN1_STRING *value;
4957 value = X509_NAME_ENTRY_get_data(entry);
4958 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07004959 ok = ssl_sock_srv_hostcheck(str, servername);
4960 OPENSSL_free(str);
4961 }
4962 }
4963
Willy Tarreau71d058c2017-07-26 20:09:56 +02004964 /* report the mismatch and indicate if SNI was used or not */
4965 if (!ok && !conn->err_code)
4966 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07004967 return ok;
4968}
4969
Emeric Brun94324a42012-10-11 14:00:19 +02004970/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01004971int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02004972{
4973 int cfgerr = 0;
William Lallemand2c776f12021-12-28 18:47:17 +01004974 SSL_CTX *ctx;
William Lallemand0adafb32022-12-13 18:17:44 +01004975 /* Automatic memory computations need to know we use SSL there
4976 * If this is an internal proxy, don't use it for the computation */
William Lallemand04007cb2022-12-14 10:34:36 +01004977 if (!(srv->proxy->cap & PR_CAP_INT))
William Lallemand0adafb32022-12-13 18:17:44 +01004978 global.ssl_used_backend = 1;
Willy Tarreaufce03112015-01-15 21:32:40 +01004979
4980 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02004981 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01004982 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Amaury Denoyellee74cbc32021-05-28 10:34:01 +02004983 ha_alert("out of memory.\n");
Emeric Brun821bb9b2017-06-15 16:37:39 +02004984 cfgerr++;
4985 return cfgerr;
4986 }
4987 }
Christopher Fauletf61f33a2020-03-27 18:55:49 +01004988 if (srv->use_ssl == 1)
Emeric Brun94324a42012-10-11 14:00:19 +02004989 srv->xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02004990
William Lallemand2c776f12021-12-28 18:47:17 +01004991 if (srv->ssl_ctx.client_crt) {
4992 const int create_if_none = srv->flags & SRV_F_DYNAMIC ? 0 : 1;
4993 char *err = NULL;
4994 int err_code = 0;
4995
4996 /* If there is a crt keyword there, the SSL_CTX will be created here. */
4997 err_code = ssl_sock_load_srv_cert(srv->ssl_ctx.client_crt, srv, create_if_none, &err);
4998 if (err_code != ERR_NONE) {
4999 if ((err_code & ERR_WARN) && !(err_code & ERR_ALERT))
5000 ha_warning("%s", err);
5001 else
5002 ha_alert("%s", err);
5003
5004 if (err_code & (ERR_FATAL|ERR_ABORT))
5005 cfgerr++;
5006 }
5007 ha_free(&err);
5008 }
5009
5010 ctx = srv->ssl_ctx.ctx;
5011
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01005012 /* The context will be uninitialized if there wasn't any "cert" option
5013 * in the server line. */
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005014 if (!ctx) {
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01005015 ctx = SSL_CTX_new(SSLv23_client_method());
5016 if (!ctx) {
Amaury Denoyellee74cbc32021-05-28 10:34:01 +02005017 ha_alert("unable to allocate ssl context.\n");
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01005018 cfgerr++;
5019 return cfgerr;
5020 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005021
Remi Tricot-Le Bretond817dc72021-01-25 17:19:43 +01005022 srv->ssl_ctx.ctx = ctx;
5023 }
Remi Tricot-Le Breton442b7f22021-01-25 17:19:41 +01005024
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01005025 cfgerr += ssl_sock_prep_srv_ctx_and_inst(srv, srv->ssl_ctx.ctx, srv->ssl_ctx.inst);
Remi Tricot-Le Breton442b7f22021-01-25 17:19:41 +01005026
5027 return cfgerr;
5028}
5029
Remi Tricot-Le Breton442b7f22021-01-25 17:19:41 +01005030/* Initialize an SSL context that will be used on the backend side.
5031 * Returns an error count.
5032 */
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01005033static int ssl_sock_prepare_srv_ssl_ctx(const struct server *srv, SSL_CTX *ctx)
Remi Tricot-Le Breton442b7f22021-01-25 17:19:41 +01005034{
5035 struct proxy *curproxy = srv->proxy;
5036 int cfgerr = 0;
5037 long options =
5038 SSL_OP_ALL | /* all known workarounds for bugs */
5039 SSL_OP_NO_SSLv2 |
5040 SSL_OP_NO_COMPRESSION;
5041 long mode =
5042 SSL_MODE_ENABLE_PARTIAL_WRITE |
5043 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
5044 SSL_MODE_RELEASE_BUFFERS |
5045 SSL_MODE_SMALL_BUFFERS;
5046 int verify = SSL_VERIFY_NONE;
5047 const struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
5048 int i, min, max, hole;
5049 int flags = MC_SSL_O_ALL;
5050
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005051 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Amaury Denoyellee74cbc32021-05-28 10:34:01 +02005052 ha_warning("no-sslv3/no-tlsv1x are ignored for this server. "
5053 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n");
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005054 else
5055 flags = conf_ssl_methods->flags;
5056
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005057 /* Real min and max should be determinate with configuration and openssl's capabilities */
5058 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005059 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005060 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005061 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005062
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005063 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005064 min = max = CONF_TLSV_NONE;
5065 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005066 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005067 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005068 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005069 if (min) {
5070 if (hole) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02005071 ha_warning("%s '%s': SSL/TLS versions range not contiguous for server '%s'. "
Christopher Faulet767a84b2017-11-24 16:50:31 +01005072 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5073 proxy_type_str(curproxy), curproxy->id, srv->id,
5074 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005075 hole = 0;
5076 }
5077 max = i;
5078 }
5079 else {
5080 min = max = i;
5081 }
5082 }
5083 else {
5084 if (min)
5085 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005086 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005087 if (!min) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02005088 ha_alert("%s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01005089 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005090 cfgerr += 1;
5091 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005092
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005093#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005094 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005095 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005096 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005097 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005098 else
5099 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5100 if (flags & methodVersions[i].flag)
5101 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005102#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005103 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005104 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5105 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005106#endif
5107
5108 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
5109 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005110 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005111
Ilya Shipitsinbdec3ba2020-11-14 01:56:34 +05005112#ifdef SSL_MODE_ASYNC
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005113 if (global_ssl.async)
5114 mode |= SSL_MODE_ASYNC;
5115#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005116 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005117
Emeric Brun850efd52014-01-29 12:24:34 +01005118 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
5119 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01005120 switch (srv->ssl_ctx.verify) {
5121 case SSL_SOCK_VERIFY_NONE:
5122 verify = SSL_VERIFY_NONE;
5123 break;
5124 case SSL_SOCK_VERIFY_REQUIRED:
5125 verify = SSL_VERIFY_PEER;
5126 break;
5127 }
Remi Tricot-Le Breton442b7f22021-01-25 17:19:41 +01005128 SSL_CTX_set_verify(ctx, verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02005129 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01005130 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02005131 if (srv->ssl_ctx.ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005132 /* set CAfile to verify */
Remi Tricot-Le Breton442b7f22021-01-25 17:19:41 +01005133 if (!ssl_set_verify_locations_file(ctx, srv->ssl_ctx.ca_file)) {
Amaury Denoyellee74cbc32021-05-28 10:34:01 +02005134 ha_alert("unable to set CA file '%s'.\n",
5135 srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005136 cfgerr++;
5137 }
5138 }
Emeric Brun850efd52014-01-29 12:24:34 +01005139 else {
5140 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Amaury Denoyellee74cbc32021-05-28 10:34:01 +02005141 ha_alert("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");
Emeric Brun850efd52014-01-29 12:24:34 +01005142 else
Amaury Denoyellee74cbc32021-05-28 10:34:01 +02005143 ha_alert("verify is enabled but no CA file specified.\n");
Emeric Brun850efd52014-01-29 12:24:34 +01005144 cfgerr++;
5145 }
Emeric Brunef42d922012-10-11 16:11:36 +02005146#ifdef X509_V_FLAG_CRL_CHECK
5147 if (srv->ssl_ctx.crl_file) {
Remi Tricot-Le Breton442b7f22021-01-25 17:19:41 +01005148 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
Emeric Brunef42d922012-10-11 16:11:36 +02005149
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005150 if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) {
Amaury Denoyellee74cbc32021-05-28 10:34:01 +02005151 ha_alert("unable to configure CRL file '%s'.\n",
5152 srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005153 cfgerr++;
5154 }
5155 else {
5156 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5157 }
5158 }
5159#endif
5160 }
5161
Remi Tricot-Le Breton442b7f22021-01-25 17:19:41 +01005162 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL_STORE);
5163 SSL_CTX_sess_set_new_cb(ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02005164 if (srv->ssl_ctx.ciphers &&
Remi Tricot-Le Breton442b7f22021-01-25 17:19:41 +01005165 !SSL_CTX_set_cipher_list(ctx, srv->ssl_ctx.ciphers)) {
Amaury Denoyellee74cbc32021-05-28 10:34:01 +02005166 ha_alert("unable to set SSL cipher list to '%s'.\n",
5167 srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02005168 cfgerr++;
5169 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005170
Ilya Shipitsinf34ed0b2020-11-21 14:37:34 +05005171#ifdef HAVE_SSL_CTX_SET_CIPHERSUITES
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005172 if (srv->ssl_ctx.ciphersuites &&
Remi Tricot-Le Breton442b7f22021-01-25 17:19:41 +01005173 !SSL_CTX_set_ciphersuites(ctx, srv->ssl_ctx.ciphersuites)) {
Amaury Denoyellee74cbc32021-05-28 10:34:01 +02005174 ha_alert("unable to set TLS 1.3 cipher suites to '%s'.\n",
5175 srv->ssl_ctx.ciphersuites);
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005176 cfgerr++;
5177 }
5178#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01005179#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
5180 if (srv->ssl_ctx.npn_str)
Remi Tricot-Le Breton442b7f22021-01-25 17:19:41 +01005181 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, (struct server*)srv);
Olivier Houchardc7566002018-11-20 23:33:50 +01005182#endif
5183#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreaua2a09552023-04-19 09:05:49 +02005184 if (srv->ssl_ctx.alpn_str && srv->ssl_ctx.alpn_len)
Olivier Houchardc7566002018-11-20 23:33:50 +01005185 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
5186#endif
5187
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01005188
5189 return cfgerr;
5190}
5191
5192/*
5193 * Prepare the frontend's SSL_CTX based on the server line configuration.
5194 * Since the CA file loading is made depending on the verify option of the
5195 * server line, the link between the SSL_CTX and the CA file tree entry is
5196 * made here.
5197 * If we want to create a link between the CA file entry and the corresponding
5198 * ckch instance (for CA file hot update), it needs to be done after
5199 * ssl_sock_prepare_srv_ssl_ctx.
5200 * Returns an error count.
5201 */
5202int ssl_sock_prep_srv_ctx_and_inst(const struct server *srv, SSL_CTX *ctx,
5203 struct ckch_inst *ckch_inst)
5204{
5205 int cfgerr = 0;
5206
5207 cfgerr += ssl_sock_prepare_srv_ssl_ctx(srv, ctx);
5208 if (!cfgerr && ckch_inst)
5209 ckch_inst_add_cafile_link(ckch_inst, NULL, NULL, srv);
Emeric Brun94324a42012-10-11 14:00:19 +02005210
5211 return cfgerr;
5212}
5213
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01005214
Frédéric Lécailleec216522020-11-23 14:33:30 +01005215/*
5216 * Create an initial CTX used to start the SSL connections.
5217 * May be used by QUIC xprt which makes usage of SSL sessions initialized from SSL_CTXs.
5218 * Returns 0 if succeeded, or something >0 if not.
5219 */
5220#ifdef USE_QUIC
5221static int ssl_initial_ctx(struct bind_conf *bind_conf)
5222{
5223 if (bind_conf->xprt == xprt_get(XPRT_QUIC))
5224 return ssl_quic_initial_ctx(bind_conf);
5225 else
5226 return ssl_sock_initial_ctx(bind_conf);
5227}
5228#else
5229static int ssl_initial_ctx(struct bind_conf *bind_conf)
5230{
5231 return ssl_sock_initial_ctx(bind_conf);
5232}
5233#endif
5234
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005235/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005236 * be NULL, in which case nothing is done. Returns the number of errors
5237 * encountered.
5238 */
Willy Tarreau03209342016-12-22 17:08:28 +01005239int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005240{
5241 struct ebmb_node *node;
5242 struct sni_ctx *sni;
5243 int err = 0;
William Lallemand8b453912019-11-21 15:48:10 +01005244 int errcode = 0;
5245 char *errmsg = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005246
Willy Tarreaufce03112015-01-15 21:32:40 +01005247 /* Automatic memory computations need to know we use SSL there */
5248 global.ssl_used_frontend = 1;
5249
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005250 /* Create initial_ctx used to start the ssl connection before do switchctx */
5251 if (!bind_conf->initial_ctx) {
Frédéric Lécailleec216522020-11-23 14:33:30 +01005252 err += ssl_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005253 /* It should not be necessary to call this function, but it's
5254 necessary first to check and move all initialisation related
Frédéric Lécailleec216522020-11-23 14:33:30 +01005255 to initial_ctx in ssl_initial_ctx. */
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01005256 errcode |= ssl_sock_prep_ctx_and_inst(bind_conf, NULL, bind_conf->initial_ctx, NULL, &errmsg);
5257 }
5258 if (bind_conf->default_ctx) {
Remi Tricot-Le Breton40ddea82021-04-13 16:07:29 +02005259 errcode |= ssl_sock_prep_ctx_and_inst(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, bind_conf->default_inst, &errmsg);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005260 }
Emeric Brun0bed9942014-10-30 19:25:24 +01005261
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005262 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005263 while (node) {
5264 sni = ebmb_entry(node, struct sni_ctx, name);
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01005265 if (!sni->order && sni->ctx != bind_conf->default_ctx) {
Emeric Brun0bed9942014-10-30 19:25:24 +01005266 /* only initialize the CTX on its first occurrence and
5267 if it is not the default_ctx */
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01005268 errcode |= ssl_sock_prep_ctx_and_inst(bind_conf, sni->conf, sni->ctx, sni->ckch_inst, &errmsg);
5269 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005270 node = ebmb_next(node);
5271 }
5272
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005273 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005274 while (node) {
5275 sni = ebmb_entry(node, struct sni_ctx, name);
William Lallemand8b453912019-11-21 15:48:10 +01005276 if (!sni->order && sni->ctx != bind_conf->default_ctx) {
Emeric Brun0bed9942014-10-30 19:25:24 +01005277 /* only initialize the CTX on its first occurrence and
5278 if it is not the default_ctx */
Remi Tricot-Le Breton4458b972021-02-19 17:41:55 +01005279 errcode |= ssl_sock_prep_ctx_and_inst(bind_conf, sni->conf, sni->ctx, sni->ckch_inst, &errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01005280 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02005281 node = ebmb_next(node);
5282 }
William Lallemand8b453912019-11-21 15:48:10 +01005283
5284 if (errcode & ERR_WARN) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01005285 ha_warning("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01005286 } else if (errcode & ERR_CODE) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01005287 ha_alert("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01005288 err++;
5289 }
5290
5291 free(errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005292 return err;
5293}
5294
Willy Tarreau55d37912016-12-21 23:38:39 +01005295/* Prepares all the contexts for a bind_conf and allocates the shared SSL
5296 * context if needed. Returns < 0 on error, 0 on success. The warnings and
5297 * alerts are directly emitted since the rest of the stack does it below.
5298 */
5299int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
5300{
5301 struct proxy *px = bind_conf->frontend;
5302 int alloc_ctx;
5303 int err;
5304
Willy Tarreau11ba4042022-05-20 15:56:32 +02005305 if (!(bind_conf->options & BC_O_USE_SSL)) {
Willy Tarreau55d37912016-12-21 23:38:39 +01005306 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005307 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
5308 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01005309 }
5310 return 0;
5311 }
5312 if (!bind_conf->default_ctx) {
Willy Tarreau1ea6e6a2022-05-20 16:03:18 +02005313 if (bind_conf->strict_sni && !(bind_conf->options & BC_O_GENERATE_CERTS)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005314 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
5315 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005316 }
5317 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005318 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
5319 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02005320 return -1;
5321 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005322 }
William Lallemandc61c0b32017-12-04 18:46:39 +01005323 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005324 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02005325 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
Willy Tarreau91358592021-06-15 08:08:04 +02005326 sizeof(*sh_ssl_sess_tree), (global.nbthread > 1));
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02005327 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01005328 if (alloc_ctx == SHCTX_E_INIT_LOCK)
5329 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");
5330 else
5331 ha_alert("Unable to allocate SSL session cache.\n");
5332 return -1;
5333 }
5334 /* free block callback */
5335 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
5336 /* init the root tree within the extra space */
5337 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
5338 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01005339 }
Willy Tarreau55d37912016-12-21 23:38:39 +01005340 err = 0;
5341 /* initialize all certificate contexts */
5342 err += ssl_sock_prepare_all_ctx(bind_conf);
5343
5344 /* initialize CA variables if the certificates generation is enabled */
5345 err += ssl_sock_load_ca(bind_conf);
5346
5347 return -err;
5348}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005349
William Lallemand231610a2021-12-30 11:25:43 +01005350/* release ssl context allocated for servers. Most of the field free here
5351 * must also be allocated in srv_ssl_settings_cpy() */
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005352void ssl_sock_free_srv_ctx(struct server *srv)
5353{
Olivier Houchardc7566002018-11-20 23:33:50 +01005354#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
William Lallemand231610a2021-12-30 11:25:43 +01005355 ha_free(&srv->ssl_ctx.alpn_str);
Olivier Houchardc7566002018-11-20 23:33:50 +01005356#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01005357#ifdef OPENSSL_NPN_NEGOTIATED
William Lallemand231610a2021-12-30 11:25:43 +01005358 ha_free(&srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01005359#endif
Christopher Faulet58feb492020-10-07 13:20:23 +02005360 if (srv->ssl_ctx.reused_sess) {
5361 int i;
5362
William Lallemande18d4e82021-11-17 02:59:21 +01005363 for (i = 0; i < global.nbthread; i++) {
Willy Tarreaue709e822021-02-26 21:06:32 +01005364 ha_free(&srv->ssl_ctx.reused_sess[i].ptr);
William Lallemande18d4e82021-11-17 02:59:21 +01005365 ha_free(&srv->ssl_ctx.reused_sess[i].sni);
5366 }
Willy Tarreaue709e822021-02-26 21:06:32 +01005367 ha_free(&srv->ssl_ctx.reused_sess);
Christopher Faulet58feb492020-10-07 13:20:23 +02005368 }
5369
Willy Tarreaue709e822021-02-26 21:06:32 +01005370 if (srv->ssl_ctx.ctx) {
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005371 SSL_CTX_free(srv->ssl_ctx.ctx);
Willy Tarreaue709e822021-02-26 21:06:32 +01005372 srv->ssl_ctx.ctx = NULL;
5373 }
William Lallemand231610a2021-12-30 11:25:43 +01005374
5375 ha_free(&srv->ssl_ctx.ca_file);
5376 ha_free(&srv->ssl_ctx.crl_file);
5377 ha_free(&srv->ssl_ctx.client_crt);
5378 ha_free(&srv->ssl_ctx.verify_host);
5379#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
5380 ha_free(&srv->sni_expr);
William Lallemand43c2ce42022-03-16 17:48:19 +01005381 release_sample_expr(srv->ssl_ctx.sni);
5382 srv->ssl_ctx.sni = NULL;
William Lallemand231610a2021-12-30 11:25:43 +01005383#endif
5384 ha_free(&srv->ssl_ctx.ciphers);
5385#ifdef HAVE_SSL_CTX_SET_CIPHERSUITES
5386 ha_free(&srv->ssl_ctx.ciphersuites);
5387#endif
William Lallemande69563f2021-12-30 14:45:19 +01005388 /* If there is a certificate we must unlink the ckch instance */
5389 ckch_inst_free(srv->ssl_ctx.inst);
Christopher Faulet77fe80c2015-07-29 13:02:40 +02005390}
5391
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005392/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02005393 * be NULL, in which case nothing is done. The default_ctx is nullified too.
5394 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005395void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005396{
5397 struct ebmb_node *node, *back;
5398 struct sni_ctx *sni;
5399
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005400 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005401 while (node) {
5402 sni = ebmb_entry(node, struct sni_ctx, name);
5403 back = ebmb_next(node);
5404 ebmb_delete(node);
William Lallemand02e19a52020-04-08 16:11:26 +02005405 SSL_CTX_free(sni->ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +02005406 LIST_DELETE(&sni->by_ckch_inst);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005407 free(sni);
5408 node = back;
5409 }
5410
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005411 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005412 while (node) {
5413 sni = ebmb_entry(node, struct sni_ctx, name);
5414 back = ebmb_next(node);
5415 ebmb_delete(node);
William Lallemand02e19a52020-04-08 16:11:26 +02005416 SSL_CTX_free(sni->ctx);
Willy Tarreau2b718102021-04-21 07:32:39 +02005417 LIST_DELETE(&sni->by_ckch_inst);
Emeric Brunfc0421f2012-09-07 17:30:07 +02005418 free(sni);
5419 node = back;
5420 }
William Lallemandb2408692020-06-24 09:54:29 +02005421
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005422 SSL_CTX_free(bind_conf->initial_ctx);
5423 bind_conf->initial_ctx = NULL;
William Lallemand02e19a52020-04-08 16:11:26 +02005424 SSL_CTX_free(bind_conf->default_ctx);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02005425 bind_conf->default_ctx = NULL;
Remi Tricot-Le Breton40ddea82021-04-13 16:07:29 +02005426 bind_conf->default_inst = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005427 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02005428}
William Lallemandb2408692020-06-24 09:54:29 +02005429
5430
5431void ssl_sock_deinit()
5432{
5433 crtlist_deinit(); /* must be free'd before the ckchs */
5434 ckch_deinit();
5435}
5436REGISTER_POST_DEINIT(ssl_sock_deinit);
Emeric Brune1f38db2012-09-03 20:36:47 +02005437
Willy Tarreau795cdab2016-12-22 17:30:54 +01005438/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
5439void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
5440{
5441 ssl_sock_free_ca(bind_conf);
5442 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005443 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01005444 free(bind_conf->ca_sign_file);
5445 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02005446 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01005447 free(bind_conf->keys_ref->filename);
5448 free(bind_conf->keys_ref->tlskeys);
Willy Tarreau2b718102021-04-21 07:32:39 +02005449 LIST_DELETE(&bind_conf->keys_ref->list);
Willy Tarreau795cdab2016-12-22 17:30:54 +01005450 free(bind_conf->keys_ref);
5451 }
5452 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005453 bind_conf->ca_sign_pass = NULL;
5454 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01005455}
5456
Christopher Faulet31af49d2015-06-09 17:29:50 +02005457/* Load CA cert file and private key used to generate certificates */
5458int
Willy Tarreau03209342016-12-22 17:08:28 +01005459ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02005460{
Willy Tarreau03209342016-12-22 17:08:28 +01005461 struct proxy *px = bind_conf->frontend;
William Lallemand52ddd992022-11-22 11:51:53 +01005462 struct ckch_data *data = NULL;
Shimi Gersner5846c492020-08-23 13:58:12 +03005463 int ret = 0;
5464 char *err = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005465
Willy Tarreau1ea6e6a2022-05-20 16:03:18 +02005466 if (!(bind_conf->options & BC_O_GENERATE_CERTS))
Shimi Gersner5846c492020-08-23 13:58:12 +03005467 return ret;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005468
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005469#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02005470 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01005471 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005472 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02005473 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005474 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02005475#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02005476
Christopher Faulet31af49d2015-06-09 17:29:50 +02005477 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005478 ha_alert("Proxy '%s': cannot enable certificate generation, "
5479 "no CA certificate File configured at [%s:%d].\n",
5480 px->id, bind_conf->file, bind_conf->line);
Shimi Gersner5846c492020-08-23 13:58:12 +03005481 goto failed;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02005482 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005483
Shimi Gersner5846c492020-08-23 13:58:12 +03005484 /* Allocate cert structure */
William Lallemand52ddd992022-11-22 11:51:53 +01005485 data = calloc(1, sizeof(*data));
5486 if (!data) {
Shimi Gersner5846c492020-08-23 13:58:12 +03005487 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain allocation failure\n",
5488 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
5489 goto failed;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005490 }
Shimi Gersner5846c492020-08-23 13:58:12 +03005491
5492 /* Try to parse file */
William Lallemand52ddd992022-11-22 11:51:53 +01005493 if (ssl_sock_load_files_into_ckch(bind_conf->ca_sign_file, data, &err)) {
Shimi Gersner5846c492020-08-23 13:58:12 +03005494 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain loading failed: %s\n",
5495 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line, err);
Willy Tarreau01acf562021-02-26 21:12:15 +01005496 free(err);
Shimi Gersner5846c492020-08-23 13:58:12 +03005497 goto failed;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005498 }
Shimi Gersner5846c492020-08-23 13:58:12 +03005499
5500 /* Fail if missing cert or pkey */
William Lallemand52ddd992022-11-22 11:51:53 +01005501 if ((!data->cert) || (!data->key)) {
Shimi Gersner5846c492020-08-23 13:58:12 +03005502 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain missing certificate or private key\n",
5503 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
5504 goto failed;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005505 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005506
Shimi Gersner5846c492020-08-23 13:58:12 +03005507 /* Final assignment to bind */
William Lallemand52ddd992022-11-22 11:51:53 +01005508 bind_conf->ca_sign_ckch = data;
Shimi Gersner5846c492020-08-23 13:58:12 +03005509 return ret;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005510
Shimi Gersner5846c492020-08-23 13:58:12 +03005511 failed:
William Lallemand52ddd992022-11-22 11:51:53 +01005512 if (data) {
5513 ssl_sock_free_cert_key_and_chain_contents(data);
5514 free(data);
Shimi Gersner5846c492020-08-23 13:58:12 +03005515 }
5516
Willy Tarreau1ea6e6a2022-05-20 16:03:18 +02005517 bind_conf->options &= ~BC_O_GENERATE_CERTS;
Shimi Gersner5846c492020-08-23 13:58:12 +03005518 ret++;
5519 return ret;
Christopher Faulet31af49d2015-06-09 17:29:50 +02005520}
5521
5522/* Release CA cert and private key used to generate certificated */
5523void
5524ssl_sock_free_ca(struct bind_conf *bind_conf)
5525{
Shimi Gersner5846c492020-08-23 13:58:12 +03005526 if (bind_conf->ca_sign_ckch) {
5527 ssl_sock_free_cert_key_and_chain_contents(bind_conf->ca_sign_ckch);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005528 ha_free(&bind_conf->ca_sign_ckch);
Shimi Gersner5846c492020-08-23 13:58:12 +03005529 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02005530}
5531
Emeric Brun46591952012-05-18 15:47:34 +02005532/*
Frédéric Lécaille5aa92412020-11-09 15:59:23 +01005533 * Try to allocate the BIO and SSL session objects of <conn> connection with <bio> and
5534 * <ssl> as addresses, <bio_meth> as BIO method and <ssl_ctx> as SSL context inherited settings.
5535 * Connect the allocated BIO to the allocated SSL session. Also set <ctx> as address of custom
5536 * data for the BIO and store <conn> as user data of the SSL session object.
Ilya Shipitsin1e9a6662021-01-05 22:10:46 +05005537 * This is the responsibility of the caller to check the validity of all the pointers passed
Frédéric Lécaille5aa92412020-11-09 15:59:23 +01005538 * as parameters to this function.
5539 * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <conn> to
5540 * CO_ER_SSL_NO_MEM.
5541 */
5542int ssl_bio_and_sess_init(struct connection *conn, SSL_CTX *ssl_ctx,
5543 SSL **ssl, BIO **bio, BIO_METHOD *bio_meth, void *ctx)
5544{
5545 int retry = 1;
5546
5547 retry:
5548 /* Alloc a new SSL session. */
5549 *ssl = SSL_new(ssl_ctx);
5550 if (!*ssl) {
5551 if (!retry--)
5552 goto err;
5553
5554 pool_gc(NULL);
5555 goto retry;
5556 }
5557
5558 *bio = BIO_new(bio_meth);
5559 if (!*bio) {
5560 SSL_free(*ssl);
5561 *ssl = NULL;
5562 if (!retry--)
5563 goto err;
5564
5565 pool_gc(NULL);
5566 goto retry;
5567 }
5568
5569 BIO_set_data(*bio, ctx);
5570 SSL_set_bio(*ssl, *bio, *bio);
5571
5572 /* set connection pointer. */
5573 if (!SSL_set_ex_data(*ssl, ssl_app_data_index, conn)) {
5574 SSL_free(*ssl);
5575 *ssl = NULL;
5576 if (!retry--)
5577 goto err;
5578
5579 pool_gc(NULL);
5580 goto retry;
5581 }
5582
5583 return 0;
5584
5585 err:
5586 conn->err_code = CO_ER_SSL_NO_MEM;
5587 return -1;
5588}
5589
Olivier Houchardbc5ce922021-03-05 23:47:00 +01005590/* This function is called when all the XPRT have been initialized. We can
5591 * now attempt to start the SSL handshake.
5592 */
5593static int ssl_sock_start(struct connection *conn, void *xprt_ctx)
5594{
5595 struct ssl_sock_ctx *ctx = xprt_ctx;
5596
5597 if (ctx->xprt->start) {
5598 int ret;
5599
5600 ret = ctx->xprt->start(conn, ctx->xprt_ctx);
5601 if (ret < 0)
5602 return ret;
5603 }
5604 tasklet_wakeup(ctx->wait_event.tasklet);
5605
5606 return 0;
5607}
5608
Frédéric Lécaille5aa92412020-11-09 15:59:23 +01005609/*
Emeric Brun46591952012-05-18 15:47:34 +02005610 * This function is called if SSL * context is not yet allocated. The function
5611 * is designed to be called before any other data-layer operation and sets the
5612 * handshake flag on the connection. It is safe to call it multiple times.
5613 * It returns 0 on success and -1 in error case.
5614 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005615static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005616{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005617 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005618 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005619 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005620 return 0;
5621
Olivier Houchard66ab4982019-02-26 18:37:15 +01005622 ctx = pool_alloc(ssl_sock_ctx_pool);
5623 if (!ctx) {
5624 conn->err_code = CO_ER_SSL_NO_MEM;
5625 return -1;
5626 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005627 ctx->wait_event.tasklet = tasklet_new();
5628 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005629 conn->err_code = CO_ER_SSL_NO_MEM;
5630 pool_free(ssl_sock_ctx_pool, ctx);
5631 return -1;
5632 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005633 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
5634 ctx->wait_event.tasklet->context = ctx;
Willy Tarreau9205ab32021-02-25 15:31:00 +01005635 ctx->wait_event.tasklet->state |= TASK_HEAVY; // assign it to the bulk queue during handshake
Olivier Houchardea8dd942019-05-20 14:02:16 +02005636 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005637 ctx->sent_early_data = 0;
Olivier Houchard54907bb2019-12-19 15:02:39 +01005638 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005639 ctx->conn = conn;
Willy Tarreau113d52b2020-01-10 09:20:26 +01005640 ctx->subs = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02005641 ctx->xprt_st = 0;
5642 ctx->xprt_ctx = NULL;
Remi Tricot-Le Breton1fe0fad2021-09-29 18:56:52 +02005643 ctx->error_code = 0;
Olivier Houcharda8955d52019-04-07 22:00:38 +02005644
5645 /* Only work with sockets for now, this should be adapted when we'll
5646 * add QUIC support.
5647 */
5648 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02005649 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005650 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
5651 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02005652 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005653
Willy Tarreau82531f62021-10-06 12:15:18 +02005654 if (global.maxsslconn && global.sslconns >= global.maxsslconn) {
Willy Tarreau20879a02012-12-03 16:32:10 +01005655 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005656 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005657 }
Willy Tarreau403edff2012-09-06 11:58:37 +02005658
Emeric Brun46591952012-05-18 15:47:34 +02005659 /* If it is in client mode initiate SSL session
5660 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005661 if (objt_server(conn->target)) {
Frédéric Lécaille5aa92412020-11-09 15:59:23 +01005662 if (ssl_bio_and_sess_init(conn, __objt_server(conn->target)->ssl_ctx.ctx,
5663 &ctx->ssl, &ctx->bio, ha_meth, ctx) == -1)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005664 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005665
Olivier Houchard66ab4982019-02-26 18:37:15 +01005666 SSL_set_connect_state(ctx->ssl);
William Lallemand3ce6eed2021-02-08 10:43:44 +01005667 HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &(__objt_server(conn->target)->ssl_ctx.lock));
Willy Tarreau07d94e42018-09-20 10:57:52 +02005668 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
5669 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
5670 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 +01005671 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005672 SSL_SESSION_free(sess);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01005673 ha_free(&__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
Olivier Houcharde6060c52017-11-16 17:42:52 +01005674 } else if (sess) {
5675 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01005676 }
5677 }
William Lallemand3ce6eed2021-02-08 10:43:44 +01005678 HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &(__objt_server(conn->target)->ssl_ctx.lock));
Evan Broderbe554312013-06-27 00:05:25 -07005679
Emeric Brun46591952012-05-18 15:47:34 +02005680 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005681 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02005682
Willy Tarreau82531f62021-10-06 12:15:18 +02005683 _HA_ATOMIC_INC(&global.sslconns);
5684 _HA_ATOMIC_INC(&global.totalsslconns);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005685 *xprt_ctx = ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005686 return 0;
5687 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005688 else if (objt_listener(conn->target)) {
Frédéric Lécaille5aa92412020-11-09 15:59:23 +01005689 struct bind_conf *bc = __objt_listener(conn->target)->bind_conf;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005690
Frédéric Lécaille5aa92412020-11-09 15:59:23 +01005691 if (ssl_bio_and_sess_init(conn, bc->initial_ctx,
5692 &ctx->ssl, &ctx->bio, ha_meth, ctx) == -1)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005693 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005694
Ilya Shipitsinb9b84a42020-10-24 23:42:30 +05005695#ifdef SSL_READ_EARLY_DATA_SUCCESS
Frédéric Lécaille5aa92412020-11-09 15:59:23 +01005696 if (bc->ssl_conf.early_data) {
Frédéric Lécaille3139c1b2020-01-24 14:56:18 +01005697 b_alloc(&ctx->early_buf);
5698 SSL_set_max_early_data(ctx->ssl,
5699 /* Only allow early data if we managed to allocate
5700 * a buffer.
5701 */
5702 (!b_is_null(&ctx->early_buf)) ?
5703 global.tune.bufsize - global.tune.maxrewrite : 0);
5704 }
5705#endif
5706
Olivier Houchard66ab4982019-02-26 18:37:15 +01005707 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02005708
Emeric Brun46591952012-05-18 15:47:34 +02005709 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005710 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Ilya Shipitsinb9b84a42020-10-24 23:42:30 +05005711#ifdef SSL_READ_EARLY_DATA_SUCCESS
Willy Tarreaua84986a2021-02-03 11:21:38 +01005712 if (bc->ssl_conf.early_data)
5713 conn->flags |= CO_FL_EARLY_SSL_HS;
Olivier Houchardc2aae742017-09-22 18:26:28 +02005714#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02005715
Willy Tarreau82531f62021-10-06 12:15:18 +02005716 _HA_ATOMIC_INC(&global.sslconns);
5717 _HA_ATOMIC_INC(&global.totalsslconns);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005718 *xprt_ctx = ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005719 return 0;
5720 }
5721 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01005722 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005723err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005724 if (ctx && ctx->wait_event.tasklet)
5725 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005726 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02005727 return -1;
5728}
5729
5730
5731/* This is the callback which is used when an SSL handshake is pending. It
5732 * updates the FD status if it wants some polling before being called again.
5733 * It returns 0 if it fails in a fatal way or needs to poll to go further,
5734 * otherwise it returns non-zero and removes itself from the connection's
5735 * flags (the bit is provided in <flag> by the caller).
5736 */
Olivier Houchard000694c2019-05-23 14:45:12 +02005737static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02005738{
Willy Tarreau939b0bf2022-04-11 11:29:11 +02005739 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
Emeric Brun46591952012-05-18 15:47:34 +02005740 int ret;
Willy Tarreau42995282020-11-06 13:19:18 +01005741 struct ssl_counters *counters = NULL;
5742 struct ssl_counters *counters_px = NULL;
Amaury Denoyelled0447a72020-11-03 17:10:02 +01005743 struct listener *li;
5744 struct server *srv;
Willy Tarreau06300382021-02-02 15:42:25 +01005745 socklen_t lskerr;
5746 int skerr;
5747
Emeric Brun46591952012-05-18 15:47:34 +02005748
Willy Tarreau3c728722014-01-23 13:50:42 +01005749 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02005750 return 0;
5751
Amaury Denoyelle034c1622020-11-13 16:05:00 +01005752 /* get counters */
5753 switch (obj_type(conn->target)) {
5754 case OBJ_TYPE_LISTENER:
Amaury Denoyelle2bf5d412021-07-26 09:59:06 +02005755 li = __objt_listener(conn->target);
Amaury Denoyelle034c1622020-11-13 16:05:00 +01005756 counters = EXTRA_COUNTERS_GET(li->extra_counters, &ssl_stats_module);
5757 counters_px = EXTRA_COUNTERS_GET(li->bind_conf->frontend->extra_counters_fe,
5758 &ssl_stats_module);
5759 break;
5760
5761 case OBJ_TYPE_SERVER:
Amaury Denoyelle2bf5d412021-07-26 09:59:06 +02005762 srv = __objt_server(conn->target);
Amaury Denoyelle034c1622020-11-13 16:05:00 +01005763 counters = EXTRA_COUNTERS_GET(srv->extra_counters, &ssl_stats_module);
5764 counters_px = EXTRA_COUNTERS_GET(srv->proxy->extra_counters_be,
5765 &ssl_stats_module);
5766 break;
5767
5768 default:
5769 break;
5770 }
5771
Willy Tarreau939b0bf2022-04-11 11:29:11 +02005772 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005773 goto out_error;
5774
Willy Tarreau06300382021-02-02 15:42:25 +01005775 /* don't start calculating a handshake on a dead connection */
5776 if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH))
5777 goto out_error;
5778
5779 /* FIXME/WT: for now we don't have a clear way to inspect the connection
5780 * status from the lower layers, so let's check the FD directly. Ideally
5781 * the xprt layers should provide some status indicating their knowledge
5782 * of shutdowns or error.
5783 */
Willy Tarreau07ecfc52022-04-11 18:07:03 +02005784 BUG_ON(conn->flags & CO_FL_FDLESS);
5785
Willy Tarreau06300382021-02-02 15:42:25 +01005786 skerr = 0;
5787 lskerr = sizeof(skerr);
5788 if ((getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) < 0) ||
5789 skerr != 0)
5790 goto out_error;
5791
Ilya Shipitsinb9b84a42020-10-24 23:42:30 +05005792#ifdef SSL_READ_EARLY_DATA_SUCCESS
Olivier Houchardc2aae742017-09-22 18:26:28 +02005793 /*
5794 * Check if we have early data. If we do, we have to read them
5795 * before SSL_do_handshake() is called, And there's no way to
5796 * detect early data, except to try to read them
5797 */
5798 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchard54907bb2019-12-19 15:02:39 +01005799 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02005800
Olivier Houchard54907bb2019-12-19 15:02:39 +01005801 while (1) {
5802 ret = SSL_read_early_data(ctx->ssl,
5803 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
5804 &read_data);
5805 if (ret == SSL_READ_EARLY_DATA_ERROR)
5806 goto check_error;
5807 if (read_data > 0) {
5808 conn->flags |= CO_FL_EARLY_DATA;
5809 b_add(&ctx->early_buf, read_data);
5810 }
5811 if (ret == SSL_READ_EARLY_DATA_FINISH) {
5812 conn->flags &= ~CO_FL_EARLY_SSL_HS;
5813 if (!b_data(&ctx->early_buf))
5814 b_free(&ctx->early_buf);
5815 break;
5816 }
5817 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02005818 }
5819#endif
Emeric Brun674b7432012-11-08 19:21:55 +01005820 /* If we use SSL_do_handshake to process a reneg initiated by
5821 * the remote peer, it sometimes returns SSL_ERROR_SSL.
5822 * Usually SSL_write and SSL_read are used and process implicitly
5823 * the reneg handshake.
5824 * Here we use SSL_peek as a workaround for reneg.
5825 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01005826 if (!(conn->flags & CO_FL_WAIT_L6_CONN) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01005827 char c;
5828
Olivier Houchard66ab4982019-02-26 18:37:15 +01005829 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01005830 if (ret <= 0) {
5831 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005832 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005833
Emeric Brun674b7432012-11-08 19:21:55 +01005834 if (ret == SSL_ERROR_WANT_WRITE) {
5835 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02005836 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005837 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01005838 return 0;
5839 }
5840 else if (ret == SSL_ERROR_WANT_READ) {
5841 /* handshake may have been completed but we have
5842 * no more data to read.
5843 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005844 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01005845 ret = 1;
5846 goto reneg_ok;
5847 }
5848 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02005849 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005850 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01005851 return 0;
5852 }
Ilya Shipitsinbdec3ba2020-11-14 01:56:34 +05005853#ifdef SSL_MODE_ASYNC
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005854 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005855 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005856 return 0;
5857 }
5858#endif
Emeric Brun674b7432012-11-08 19:21:55 +01005859 else if (ret == SSL_ERROR_SYSCALL) {
5860 /* if errno is null, then connection was successfully established */
5861 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
5862 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01005863 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02005864#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
5865 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005866 conn->err_code = CO_ER_SSL_HANDSHAKE;
5867#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005868 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005869#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02005870 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005871 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005872 empty_handshake = state == TLS_ST_BEFORE;
5873#else
Lukas Tribus49799162019-07-08 14:29:15 +02005874 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
5875 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005876#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005877 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02005878 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005879 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005880 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5881 else
5882 conn->err_code = CO_ER_SSL_EMPTY;
5883 }
5884 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005885 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005886 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5887 else
5888 conn->err_code = CO_ER_SSL_ABORT;
5889 }
5890 }
5891 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005892 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005893 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01005894 else
Emeric Brun29f037d2014-04-25 19:05:36 +02005895 conn->err_code = CO_ER_SSL_HANDSHAKE;
5896 }
Lukas Tribus49799162019-07-08 14:29:15 +02005897#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01005898 }
Emeric Brun674b7432012-11-08 19:21:55 +01005899 goto out_error;
5900 }
5901 else {
5902 /* Fail on all other handshake errors */
5903 /* Note: OpenSSL may leave unread bytes in the socket's
5904 * buffer, causing an RST to be emitted upon close() on
5905 * TCP sockets. We first try to drain possibly pending
5906 * data to avoid this as much as possible.
5907 */
Willy Tarreau2ded48d2020-12-11 16:20:34 +01005908 conn_ctrl_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01005909 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005910 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02005911 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01005912 goto out_error;
5913 }
5914 }
5915 /* read some data: consider handshake completed */
5916 goto reneg_ok;
5917 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005918 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005919check_error:
Emeric Brun46591952012-05-18 15:47:34 +02005920 if (ret != 1) {
5921 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005922 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02005923
Remi Tricot-Le Breton1fe0fad2021-09-29 18:56:52 +02005924 if (!ctx->error_code)
5925 ctx->error_code = ERR_peek_error();
Remi Tricot-Le Breton7c6898e2021-07-29 09:45:51 +02005926
Emeric Brun46591952012-05-18 15:47:34 +02005927 if (ret == SSL_ERROR_WANT_WRITE) {
5928 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02005929 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005930 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02005931 return 0;
5932 }
5933 else if (ret == SSL_ERROR_WANT_READ) {
5934 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02005935 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005936 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
5937 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02005938 return 0;
5939 }
Ilya Shipitsinbdec3ba2020-11-14 01:56:34 +05005940#ifdef SSL_MODE_ASYNC
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005941 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005942 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005943 return 0;
5944 }
5945#endif
Willy Tarreau89230192012-09-28 20:22:13 +02005946 else if (ret == SSL_ERROR_SYSCALL) {
5947 /* if errno is null, then connection was successfully established */
5948 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
5949 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005950 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02005951#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
5952 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005953 conn->err_code = CO_ER_SSL_HANDSHAKE;
5954#else
5955 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005956#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02005957 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005958 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005959 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005960#else
Lukas Tribus49799162019-07-08 14:29:15 +02005961 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
5962 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005963#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005964 if (empty_handshake) {
5965 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005966 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005967 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5968 else
5969 conn->err_code = CO_ER_SSL_EMPTY;
5970 }
5971 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005972 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005973 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5974 else
5975 conn->err_code = CO_ER_SSL_ABORT;
5976 }
Emeric Brun29f037d2014-04-25 19:05:36 +02005977 }
5978 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005979 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005980 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5981 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005982 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02005983 }
Lukas Tribus49799162019-07-08 14:29:15 +02005984#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02005985 }
Willy Tarreau89230192012-09-28 20:22:13 +02005986 goto out_error;
5987 }
Emeric Brun46591952012-05-18 15:47:34 +02005988 else {
5989 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02005990 /* Note: OpenSSL may leave unread bytes in the socket's
5991 * buffer, causing an RST to be emitted upon close() on
5992 * TCP sockets. We first try to drain possibly pending
5993 * data to avoid this as much as possible.
5994 */
Willy Tarreau2ded48d2020-12-11 16:20:34 +01005995 conn_ctrl_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01005996 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005997 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02005998 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02005999 goto out_error;
6000 }
6001 }
Ilya Shipitsinb9b84a42020-10-24 23:42:30 +05006002#ifdef SSL_READ_EARLY_DATA_SUCCESS
Olivier Houchard522eea72017-11-03 16:27:47 +01006003 else {
6004 /*
6005 * If the server refused the early data, we have to send a
6006 * 425 to the client, as we no longer have the data to sent
6007 * them again.
6008 */
6009 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006010 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006011 conn->err_code = CO_ER_SSL_EARLY_FAILED;
6012 goto out_error;
6013 }
6014 }
6015 }
6016#endif
6017
Emeric Brun46591952012-05-18 15:47:34 +02006018
Emeric Brun674b7432012-11-08 19:21:55 +01006019reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00006020
Ilya Shipitsinbdec3ba2020-11-14 01:56:34 +05006021#ifdef SSL_MODE_ASYNC
Emeric Brunb5e42a82017-06-06 12:35:14 +00006022 /* ASYNC engine API doesn't support moving read/write
6023 * buffers. So we disable ASYNC mode right after
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05006024 * the handshake to avoid buffer overflow.
Emeric Brunb5e42a82017-06-06 12:35:14 +00006025 */
6026 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006027 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006028#endif
Emeric Brun46591952012-05-18 15:47:34 +02006029 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006030 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006031 if (objt_server(conn->target)) {
6032 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
6033 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
6034 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02006035 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006036 else {
6037 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
6038 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
6039 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
6040 }
Amaury Denoyelled0447a72020-11-03 17:10:02 +01006041
Willy Tarreau42995282020-11-06 13:19:18 +01006042 if (counters) {
Willy Tarreauc5e7cf92021-11-22 17:46:13 +01006043 HA_ATOMIC_INC(&counters->sess);
6044 HA_ATOMIC_INC(&counters_px->sess);
Willy Tarreau42995282020-11-06 13:19:18 +01006045 }
Amaury Denoyelled0447a72020-11-03 17:10:02 +01006046 }
Willy Tarreau42995282020-11-06 13:19:18 +01006047 else if (counters) {
Willy Tarreauc5e7cf92021-11-22 17:46:13 +01006048 HA_ATOMIC_INC(&counters->reused_sess);
6049 HA_ATOMIC_INC(&counters_px->reused_sess);
Emeric Brun46591952012-05-18 15:47:34 +02006050 }
6051
6052 /* The connection is now established at both layers, it's time to leave */
6053 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
6054 return 1;
6055
6056 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006057 /* Clear openssl global errors stack */
Frédéric Lécaille2be0ac52022-09-06 19:37:08 +02006058 ssl_sock_dump_errors(conn, NULL);
Emeric Brun644cde02012-12-14 11:21:13 +01006059 ERR_clear_error();
6060
Emeric Brun9fa89732012-10-04 17:09:56 +02006061 /* free resumed session if exists */
William Lallemand3ce6eed2021-02-08 10:43:44 +01006062 if (objt_server(conn->target)) {
6063 struct server *s = __objt_server(conn->target);
6064 /* RWLOCK: only rdlock the SSL cache even when writing in it because there is
6065 * one cache per thread, it only prevents to flush it from the CLI in
6066 * another thread */
6067
6068 HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01006069 if (s->ssl_ctx.reused_sess[tid].ptr)
6070 ha_free(&s->ssl_ctx.reused_sess[tid].ptr);
William Lallemand3ce6eed2021-02-08 10:43:44 +01006071 HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock);
Emeric Brun9fa89732012-10-04 17:09:56 +02006072 }
6073
Amaury Denoyelle034c1622020-11-13 16:05:00 +01006074 if (counters) {
Willy Tarreauc5e7cf92021-11-22 17:46:13 +01006075 HA_ATOMIC_INC(&counters->failed_handshake);
6076 HA_ATOMIC_INC(&counters_px->failed_handshake);
Amaury Denoyelle034c1622020-11-13 16:05:00 +01006077 }
6078
Emeric Brun46591952012-05-18 15:47:34 +02006079 /* Fail on all other handshake errors */
6080 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01006081 if (!conn->err_code)
6082 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006083 return 0;
6084}
6085
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006086/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6087 * event subscriber <es> is not allowed to change from a previous call as long
6088 * as at least one event is still subscribed. The <event_type> must only be a
6089 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0,
6090 * unless the transport layer was already released.
6091 */
6092static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006093{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006094 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006095
Olivier Houchard0ff28652019-06-24 18:57:39 +02006096 if (!ctx)
6097 return -1;
6098
Willy Tarreau113d52b2020-01-10 09:20:26 +01006099 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006100 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006101
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006102 ctx->subs = es;
6103 es->events |= event_type;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006104
6105 /* we may have to subscribe to lower layers for new events */
6106 event_type &= ~ctx->wait_event.events;
6107 if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS))
6108 ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006109 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006110}
6111
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006112/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6113 * The <es> pointer is not allowed to differ from the one passed to the
6114 * subscribe() call. It always returns zero.
6115 */
6116static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006117{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006118 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006119
Willy Tarreau113d52b2020-01-10 09:20:26 +01006120 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006121 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006122
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006123 es->events &= ~event_type;
6124 if (!es->events)
Willy Tarreau113d52b2020-01-10 09:20:26 +01006125 ctx->subs = NULL;
6126
6127 /* If we subscribed, and we're not doing the handshake,
6128 * then we subscribed because the upper layer asked for it,
6129 * as the upper layer is no longer interested, we can
6130 * unsubscribe too.
6131 */
6132 event_type &= ctx->wait_event.events;
6133 if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS))
6134 conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006135
6136 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006137}
6138
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02006139/* The connection has been taken over, so destroy the old tasklet and create
6140 * a new one. The original thread ID must be passed into orig_tid
6141 * It should be called with the takeover lock for the old thread held.
6142 * Returns 0 on success, and -1 on failure
6143 */
6144static int ssl_takeover(struct connection *conn, void *xprt_ctx, int orig_tid)
6145{
6146 struct ssl_sock_ctx *ctx = xprt_ctx;
6147 struct tasklet *tl = tasklet_new();
6148
6149 if (!tl)
6150 return -1;
6151
6152 ctx->wait_event.tasklet->context = NULL;
6153 tasklet_wakeup_on(ctx->wait_event.tasklet, orig_tid);
6154 ctx->wait_event.tasklet = tl;
6155 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
6156 ctx->wait_event.tasklet->context = ctx;
6157 return 0;
6158}
6159
Willy Tarreau41491682021-03-02 17:29:56 +01006160/* notify the next xprt that the connection is about to become idle and that it
6161 * may be stolen at any time after the function returns and that any tasklet in
6162 * the chain must be careful before dereferencing its context.
6163 */
6164static void ssl_set_idle(struct connection *conn, void *xprt_ctx)
6165{
6166 struct ssl_sock_ctx *ctx = xprt_ctx;
6167
6168 if (!ctx || !ctx->wait_event.tasklet)
6169 return;
6170
6171 HA_ATOMIC_OR(&ctx->wait_event.tasklet->state, TASK_F_USR1);
6172 if (ctx->xprt)
6173 xprt_set_idle(conn, ctx->xprt, ctx->xprt_ctx);
6174}
6175
6176/* notify the next xprt that the connection is not idle anymore and that it may
6177 * not be stolen before the next xprt_set_idle().
6178 */
6179static void ssl_set_used(struct connection *conn, void *xprt_ctx)
6180{
6181 struct ssl_sock_ctx *ctx = xprt_ctx;
6182
6183 if (!ctx || !ctx->wait_event.tasklet)
6184 return;
6185
6186 HA_ATOMIC_OR(&ctx->wait_event.tasklet->state, TASK_F_USR1);
6187 if (ctx->xprt)
6188 xprt_set_used(conn, ctx->xprt, ctx->xprt_ctx);
6189}
6190
Olivier Houchard2e055482019-05-27 19:50:12 +02006191/* Use the provided XPRT as an underlying XPRT, and provide the old one.
6192 * Returns 0 on success, and non-zero on failure.
6193 */
6194static 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)
6195{
6196 struct ssl_sock_ctx *ctx = xprt_ctx;
6197
6198 if (oldxprt_ops != NULL)
6199 *oldxprt_ops = ctx->xprt;
6200 if (oldxprt_ctx != NULL)
6201 *oldxprt_ctx = ctx->xprt_ctx;
6202 ctx->xprt = toadd_ops;
6203 ctx->xprt_ctx = toadd_ctx;
6204 return 0;
6205}
6206
Olivier Houchard5149b592019-05-23 17:47:36 +02006207/* Remove the specified xprt. If if it our underlying XPRT, remove it and
6208 * return 0, otherwise just call the remove_xprt method from the underlying
6209 * XPRT.
6210 */
6211static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
6212{
6213 struct ssl_sock_ctx *ctx = xprt_ctx;
6214
6215 if (ctx->xprt_ctx == toremove_ctx) {
6216 ctx->xprt_ctx = newctx;
6217 ctx->xprt = newops;
6218 return 0;
6219 }
6220 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
6221}
6222
Willy Tarreau144f84a2021-03-02 16:09:26 +01006223struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned int state)
Olivier Houchardea8dd942019-05-20 14:02:16 +02006224{
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02006225 struct tasklet *tl = (struct tasklet *)t;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006226 struct ssl_sock_ctx *ctx = context;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02006227 struct connection *conn;
6228 int conn_in_list;
6229 int ret = 0;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006230
Willy Tarreau41491682021-03-02 17:29:56 +01006231 if (state & TASK_F_USR1) {
6232 /* the tasklet was idling on an idle connection, it might have
6233 * been stolen, let's be careful!
6234 */
6235 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
6236 if (tl->context == NULL) {
6237 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
6238 tasklet_free(tl);
6239 return NULL;
6240 }
6241 conn = ctx->conn;
Christopher Faulet3a7b5392023-03-16 11:43:05 +01006242 conn_in_list = conn_get_idle_flag(conn);
Willy Tarreau41491682021-03-02 17:29:56 +01006243 if (conn_in_list)
6244 conn_delete_from_tree(&conn->hash_node->node);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01006245 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Willy Tarreau41491682021-03-02 17:29:56 +01006246 } else {
6247 conn = ctx->conn;
6248 conn_in_list = 0;
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02006249 }
Willy Tarreau41491682021-03-02 17:29:56 +01006250
Olivier Houchardea8dd942019-05-20 14:02:16 +02006251 /* First if we're doing an handshake, try that */
Willy Tarreau9205ab32021-02-25 15:31:00 +01006252 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006253 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
Willy Tarreau9205ab32021-02-25 15:31:00 +01006254 if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
6255 /* handshake completed, leave the bulk queue */
Willy Tarreau4c48edb2021-03-09 17:58:02 +01006256 _HA_ATOMIC_AND(&tl->state, ~TASK_HEAVY);
Willy Tarreau9205ab32021-02-25 15:31:00 +01006257 }
6258 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02006259 /* If we had an error, or the handshake is done and I/O is available,
6260 * let the upper layer know.
Olivier Houchard477902b2020-01-22 18:08:48 +01006261 * If no mux was set up yet, then call conn_create_mux()
Olivier Houchardea8dd942019-05-20 14:02:16 +02006262 * we can't be sure conn_fd_handler() will be called again.
6263 */
6264 if ((ctx->conn->flags & CO_FL_ERROR) ||
6265 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006266 int woke = 0;
6267
6268 /* On error, wake any waiter */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006269 if (ctx->subs) {
6270 tasklet_wakeup(ctx->subs->tasklet);
6271 ctx->subs->events = 0;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006272 woke = 1;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006273 ctx->subs = NULL;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006274 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006275
Olivier Houchardea8dd942019-05-20 14:02:16 +02006276 /* If we're the first xprt for the connection, let the
Olivier Houchard477902b2020-01-22 18:08:48 +01006277 * upper layers know. If we have no mux, create it,
6278 * and once we have a mux, call its wake method if we didn't
6279 * woke a tasklet already.
Olivier Houchardea8dd942019-05-20 14:02:16 +02006280 */
6281 if (ctx->conn->xprt_ctx == ctx) {
Olivier Houchard477902b2020-01-22 18:08:48 +01006282 if (!ctx->conn->mux)
6283 ret = conn_create_mux(ctx->conn);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006284 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02006285 ret = ctx->conn->mux->wake(ctx->conn);
6286 goto leave;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006287 }
6288 }
Ilya Shipitsin761d64c2021-01-07 11:59:58 +05006289#ifdef SSL_READ_EARLY_DATA_SUCCESS
Olivier Houchard54907bb2019-12-19 15:02:39 +01006290 /* If we have early data and somebody wants to receive, let them */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006291 else if (b_data(&ctx->early_buf) && ctx->subs &&
6292 ctx->subs->events & SUB_RETRY_RECV) {
6293 tasklet_wakeup(ctx->subs->tasklet);
6294 ctx->subs->events &= ~SUB_RETRY_RECV;
6295 if (!ctx->subs->events)
6296 ctx->subs = NULL;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006297 }
6298#endif
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02006299leave:
6300 if (!ret && conn_in_list) {
6301 struct server *srv = objt_server(conn->target);
6302
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01006303 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02006304 if (conn_in_list == CO_FL_SAFE_LIST)
Willy Tarreau85223482022-09-29 20:32:43 +02006305 eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node);
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02006306 else
Willy Tarreau85223482022-09-29 20:32:43 +02006307 eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node);
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01006308 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02006309 }
Willy Tarreau74163142021-03-13 11:30:19 +01006310 return t;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006311}
6312
Emeric Brun46591952012-05-18 15:47:34 +02006313/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01006314 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02006315 * buffer wraps, in which case a second call may be performed. The connection's
6316 * flags are updated with whatever special event is detected (error, read0,
6317 * empty). The caller is responsible for taking care of those events and
6318 * avoiding the call if inappropriate. The function does not call the
6319 * connection's polling update function, so the caller is responsible for this.
6320 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006321static 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 +02006322{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006323 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02006324 ssize_t ret;
6325 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02006326
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006327 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006328 goto out_error;
6329
Ilya Shipitsin761d64c2021-01-07 11:59:58 +05006330#ifdef SSL_READ_EARLY_DATA_SUCCESS
Olivier Houchard54907bb2019-12-19 15:02:39 +01006331 if (b_data(&ctx->early_buf)) {
6332 try = b_contig_space(buf);
6333 if (try > b_data(&ctx->early_buf))
6334 try = b_data(&ctx->early_buf);
6335 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
6336 b_add(buf, try);
6337 b_del(&ctx->early_buf, try);
6338 if (b_data(&ctx->early_buf) == 0)
6339 b_free(&ctx->early_buf);
6340 return try;
6341 }
6342#endif
6343
Willy Tarreau911db9b2020-01-23 16:27:54 +01006344 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02006345 /* a handshake was requested */
6346 return 0;
6347
Emeric Brun46591952012-05-18 15:47:34 +02006348 /* read the largest possible block. For this, we perform only one call
6349 * to recv() unless the buffer wraps and we exactly fill the first hunk,
6350 * in which case we accept to do it once again. A new attempt is made on
6351 * EINTR too.
6352 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01006353 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006354
Willy Tarreau591d4452018-06-15 17:21:00 +02006355 try = b_contig_space(buf);
6356 if (!try)
6357 break;
6358
Willy Tarreauabf08d92014-01-14 11:31:27 +01006359 if (try > count)
6360 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02006361
Olivier Houchard66ab4982019-02-26 18:37:15 +01006362 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006363
Emeric Brune1f38db2012-09-03 20:36:47 +02006364 if (conn->flags & CO_FL_ERROR) {
6365 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006366 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006367 }
Emeric Brun46591952012-05-18 15:47:34 +02006368 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02006369 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006370 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006371 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006372 }
Emeric Brun46591952012-05-18 15:47:34 +02006373 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006374 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006375 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006376 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02006377 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006378 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Ilya Shipitsinbdec3ba2020-11-14 01:56:34 +05006379#ifdef SSL_MODE_ASYNC
Emeric Brunb5e42a82017-06-06 12:35:14 +00006380 /* Async mode can be re-enabled, because we're leaving data state.*/
6381 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006382 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006383#endif
Emeric Brun46591952012-05-18 15:47:34 +02006384 break;
6385 }
6386 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006387 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006388 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6389 SUB_RETRY_RECV,
6390 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01006391 /* handshake is running, and it may need to re-enable read */
6392 conn->flags |= CO_FL_SSL_WAIT_HS;
Ilya Shipitsinbdec3ba2020-11-14 01:56:34 +05006393#ifdef SSL_MODE_ASYNC
Emeric Brunb5e42a82017-06-06 12:35:14 +00006394 /* Async mode can be re-enabled, because we're leaving data state.*/
6395 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006396 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006397#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006398 break;
6399 }
Emeric Brun46591952012-05-18 15:47:34 +02006400 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006401 } else if (ret == SSL_ERROR_ZERO_RETURN)
6402 goto read0;
Remi Tricot-Le Breton61944f72021-09-29 18:56:51 +02006403 else if (ret == SSL_ERROR_SSL) {
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006404 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
6405 if (ctx && !ctx->error_code)
Remi Tricot-Le Breton9543d5a2021-09-29 18:56:53 +02006406 ctx->error_code = ERR_peek_error();
Remi Tricot-Le Breton61944f72021-09-29 18:56:51 +02006407 conn->err_code = CO_ERR_SSL_FATAL;
6408 }
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006409 /* For SSL_ERROR_SYSCALL, make sure to clear the error
6410 * stack before shutting down the connection for
6411 * reading. */
Willy Tarreauacef5e22022-04-25 20:32:15 +02006412 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN || errno == EWOULDBLOCK))
Olivier Houchard7e2e5052018-02-13 15:17:23 +01006413 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02006414 /* otherwise it's a real error */
6415 goto out_error;
6416 }
6417 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006418 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006419 return done;
6420
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006421 clear_ssl_error:
6422 /* Clear openssl global errors stack */
Frédéric Lécaille2be0ac52022-09-06 19:37:08 +02006423 ssl_sock_dump_errors(conn, NULL);
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006424 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02006425 read0:
6426 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006427 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01006428
Emeric Brun46591952012-05-18 15:47:34 +02006429 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01006430 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01006431 /* Clear openssl global errors stack */
Frédéric Lécaille2be0ac52022-09-06 19:37:08 +02006432 ssl_sock_dump_errors(conn, NULL);
Emeric Brun644cde02012-12-14 11:21:13 +01006433 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006434 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006435}
6436
6437
Willy Tarreau787db9a2018-06-14 18:31:46 +02006438/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
6439 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
6440 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02006441 * Only one call to send() is performed, unless the buffer wraps, in which case
6442 * a second call may be performed. The connection's flags are updated with
6443 * whatever special event is detected (error, empty). The caller is responsible
6444 * for taking care of those events and avoiding the call if inappropriate. The
6445 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02006446 * is responsible for this. The buffer's output is not adjusted, it's up to the
6447 * caller to take care of this. It's up to the caller to update the buffer's
6448 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02006449 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006450static 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 +02006451{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006452 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02006453 ssize_t ret;
6454 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02006455
6456 done = 0;
6457
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006458 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006459 goto out_error;
6460
Willy Tarreau911db9b2020-01-23 16:27:54 +01006461 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02006462 /* a handshake was requested */
6463 return 0;
6464
6465 /* send the largest possible block. For this we perform only one call
6466 * to send() unless the buffer wraps and we exactly fill the first hunk,
6467 * in which case we accept to do it once again.
6468 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02006469 while (count) {
Ilya Shipitsinb9b84a42020-10-24 23:42:30 +05006470#ifdef SSL_READ_EARLY_DATA_SUCCESS
Olivier Houchardc2aae742017-09-22 18:26:28 +02006471 size_t written_data;
6472#endif
6473
Willy Tarreau787db9a2018-06-14 18:31:46 +02006474 try = b_contig_data(buf, done);
6475 if (try > count)
6476 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01006477
Thomas Prückl10243932022-04-27 13:04:54 +02006478 if (global_ssl.hard_max_record && try > global_ssl.hard_max_record)
6479 try = global_ssl.hard_max_record;
6480
Willy Tarreau7bed9452014-02-02 02:00:24 +01006481 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006482 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01006483 global_ssl.max_record && try > global_ssl.max_record) {
6484 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01006485 }
6486 else {
6487 /* we need to keep the information about the fact that
6488 * we're not limiting the upcoming send(), because if it
6489 * fails, we'll have to retry with at least as many data.
6490 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006491 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01006492 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01006493
Willy Tarreauac78c4f2023-03-17 16:13:05 +01006494 if (try < count || flags & CO_SFL_MSG_MORE)
6495 ctx->xprt_st |= SSL_SOCK_SEND_MORE;
6496 else
6497 ctx->xprt_st &= ~SSL_SOCK_SEND_MORE;
6498
Ilya Shipitsinb9b84a42020-10-24 23:42:30 +05006499#ifdef SSL_READ_EARLY_DATA_SUCCESS
Olivier Houchard010941f2019-05-03 20:56:19 +02006500 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006501 unsigned int max_early;
6502
Olivier Houchard522eea72017-11-03 16:27:47 +01006503 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01006504 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01006505 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006506 if (SSL_get0_session(ctx->ssl))
6507 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01006508 else
6509 max_early = 0;
6510 }
6511
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006512 if (try + ctx->sent_early_data > max_early) {
6513 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01006514 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02006515 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006516 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006517 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01006518 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006519 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006520 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006521 if (ret == 1) {
6522 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006523 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006524 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006525 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02006526 /* Initiate the handshake, now */
6527 tasklet_wakeup(ctx->wait_event.tasklet);
6528 }
Olivier Houchard522eea72017-11-03 16:27:47 +01006529
Olivier Houchardc2aae742017-09-22 18:26:28 +02006530 }
6531
6532 } else
6533#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006534 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01006535
Emeric Brune1f38db2012-09-03 20:36:47 +02006536 if (conn->flags & CO_FL_ERROR) {
6537 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006538 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006539 }
Emeric Brun46591952012-05-18 15:47:34 +02006540 if (ret > 0) {
Willy Tarreauc192b0a2020-01-23 09:11:58 +01006541 /* A send succeeded, so we can consider ourself connected */
6542 conn->flags &= ~CO_FL_WAIT_L4L6;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006543 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02006544 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006545 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006546 }
6547 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006548 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006549
Emeric Brun46591952012-05-18 15:47:34 +02006550 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006551 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01006552 /* handshake is running, and it may need to re-enable write */
6553 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006554 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Ilya Shipitsinbdec3ba2020-11-14 01:56:34 +05006555#ifdef SSL_MODE_ASYNC
Emeric Brunb5e42a82017-06-06 12:35:14 +00006556 /* Async mode can be re-enabled, because we're leaving data state.*/
6557 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006558 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006559#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01006560 break;
6561 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02006562
Emeric Brun46591952012-05-18 15:47:34 +02006563 break;
6564 }
6565 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01006566 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02006567 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006568 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6569 SUB_RETRY_RECV,
6570 &ctx->wait_event);
Ilya Shipitsinbdec3ba2020-11-14 01:56:34 +05006571#ifdef SSL_MODE_ASYNC
Emeric Brunb5e42a82017-06-06 12:35:14 +00006572 /* Async mode can be re-enabled, because we're leaving data state.*/
6573 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006574 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006575#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006576 break;
6577 }
Remi Tricot-Le Breton61944f72021-09-29 18:56:51 +02006578 else if (ret == SSL_ERROR_SSL || ret == SSL_ERROR_SYSCALL) {
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006579 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
6580
Willy Tarreau3a0a0d62022-04-12 07:31:06 +02006581 if (ctx && !ctx->error_code)
Remi Tricot-Le Breton9543d5a2021-09-29 18:56:53 +02006582 ctx->error_code = ERR_peek_error();
Remi Tricot-Le Breton61944f72021-09-29 18:56:51 +02006583 conn->err_code = CO_ERR_SSL_FATAL;
6584 }
Emeric Brun46591952012-05-18 15:47:34 +02006585 goto out_error;
6586 }
6587 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006588 leave:
Emeric Brun46591952012-05-18 15:47:34 +02006589 return done;
6590
6591 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006592 /* Clear openssl global errors stack */
Frédéric Lécaille2be0ac52022-09-06 19:37:08 +02006593 ssl_sock_dump_errors(conn, NULL);
Emeric Brun644cde02012-12-14 11:21:13 +01006594 ERR_clear_error();
6595
Emeric Brun46591952012-05-18 15:47:34 +02006596 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02006597 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02006598}
6599
Willy Tarreau832e2422021-05-13 10:11:03 +02006600void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02006601
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006602 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006603
Olivier Houchardea8dd942019-05-20 14:02:16 +02006604
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006605 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006606 if (ctx->wait_event.events != 0)
6607 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
6608 ctx->wait_event.events,
6609 &ctx->wait_event);
Willy Tarreau113d52b2020-01-10 09:20:26 +01006610 if (ctx->subs) {
6611 ctx->subs->events = 0;
6612 tasklet_wakeup(ctx->subs->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006613 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006614
Olivier Houchard692c1d02019-05-23 18:41:47 +02006615 if (ctx->xprt->close)
6616 ctx->xprt->close(conn, ctx->xprt_ctx);
Ilya Shipitsinbdec3ba2020-11-14 01:56:34 +05006617#ifdef SSL_MODE_ASYNC
Emeric Brun3854e012017-05-17 20:42:48 +02006618 if (global_ssl.async) {
6619 OSSL_ASYNC_FD all_fd[32], afd;
6620 size_t num_all_fds = 0;
6621 int i;
6622
Olivier Houchard66ab4982019-02-26 18:37:15 +01006623 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006624 if (num_all_fds > 32) {
6625 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
6626 return;
6627 }
6628
Olivier Houchard66ab4982019-02-26 18:37:15 +01006629 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02006630
6631 /* If an async job is pending, we must try to
6632 to catch the end using polling before calling
6633 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006634 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02006635 for (i=0 ; i < num_all_fds ; i++) {
6636 /* switch on an handler designed to
6637 * handle the SSL_free
6638 */
6639 afd = all_fd[i];
6640 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006641 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02006642 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00006643 /* To ensure that the fd cache won't be used
6644 * and we'll catch a real RD event.
6645 */
6646 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02006647 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006648 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006649 pool_free(ssl_sock_ctx_pool, ctx);
Willy Tarreau4781b152021-04-06 13:53:36 +02006650 _HA_ATOMIC_INC(&jobs);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006651 return;
6652 }
Emeric Brun3854e012017-05-17 20:42:48 +02006653 /* Else we can remove the fds from the fdtab
6654 * and call SSL_free.
Willy Tarreau67672452020-08-26 11:44:17 +02006655 * note: we do a fd_stop_both and not a delete
Emeric Brun3854e012017-05-17 20:42:48 +02006656 * because the fd is owned by the engine.
6657 * the engine is responsible to close
6658 */
Emeric Brun7d392a52022-07-01 17:36:50 +02006659 for (i=0 ; i < num_all_fds ; i++) {
6660 /* We want to remove the fd from the fdtab
6661 * but we flag it to disown because the
6662 * close is performed by the engine itself
6663 */
6664 fdtab[all_fd[i]].state |= FD_DISOWN;
6665 fd_delete(all_fd[i]);
6666 }
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006667 }
6668#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01006669 SSL_free(ctx->ssl);
Olivier Houchard54907bb2019-12-19 15:02:39 +01006670 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006671 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006672 pool_free(ssl_sock_ctx_pool, ctx);
Willy Tarreau82531f62021-10-06 12:15:18 +02006673 _HA_ATOMIC_DEC(&global.sslconns);
Emeric Brun46591952012-05-18 15:47:34 +02006674 }
Emeric Brun46591952012-05-18 15:47:34 +02006675}
6676
6677/* This function tries to perform a clean shutdown on an SSL connection, and in
6678 * any case, flags the connection as reusable if no handshake was in progress.
6679 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006680static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02006681{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006682 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006683
Willy Tarreau911db9b2020-01-23 16:27:54 +01006684 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02006685 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01006686 if (!clean)
6687 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006688 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02006689 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006690 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01006691 /* Clear openssl global errors stack */
Frédéric Lécaille2be0ac52022-09-06 19:37:08 +02006692 ssl_sock_dump_errors(conn, NULL);
Emeric Brun644cde02012-12-14 11:21:13 +01006693 ERR_clear_error();
6694 }
Emeric Brun46591952012-05-18 15:47:34 +02006695}
6696
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01006697
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05006698/* used for ppv2 pkey algo (can be used for logging) */
William Lallemandd4f946c2019-12-05 10:26:40 +01006699int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
6700{
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006701 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
William Lallemandd4f946c2019-12-05 10:26:40 +01006702 X509 *crt;
6703
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006704 if (!ctx)
William Lallemandd4f946c2019-12-05 10:26:40 +01006705 return 0;
William Lallemandd4f946c2019-12-05 10:26:40 +01006706 crt = SSL_get_certificate(ctx->ssl);
6707 if (!crt)
6708 return 0;
6709
6710 return cert_get_pkey_algo(crt, out);
6711}
6712
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006713/* used for ppv2 cert signature (can be used for logging) */
6714const char *ssl_sock_get_cert_sig(struct connection *conn)
6715{
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006716 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006717
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006718 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
6719 X509 *crt;
6720
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006721 if (!ctx)
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006722 return NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006723 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01006724 if (!crt)
6725 return NULL;
6726 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
6727 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
6728}
6729
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006730/* used for ppv2 authority */
6731const char *ssl_sock_get_sni(struct connection *conn)
6732{
6733#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006734 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006735
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006736 if (!ctx)
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006737 return NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006738 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006739#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01006740 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01006741#endif
6742}
6743
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006744/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006745const char *ssl_sock_get_cipher_name(struct connection *conn)
6746{
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006747 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006748
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006749 if (!ctx)
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006750 return NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006751 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006752}
6753
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02006754/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006755const char *ssl_sock_get_proto_version(struct connection *conn)
6756{
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006757 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006758
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006759 if (!ctx)
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006760 return NULL;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006761 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02006762}
6763
Olivier Houchardab28a322018-12-21 19:45:40 +01006764void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
6765{
6766#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006767 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006768
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006769 if (!ctx)
Olivier Houcharde488ea82019-06-28 14:10:33 +02006770 return;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006771 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01006772#endif
6773}
6774
Willy Tarreau119a4082016-12-22 21:58:38 +01006775/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
6776 * to disable SNI.
6777 */
Willy Tarreau63076412015-07-10 11:33:32 +02006778void ssl_sock_set_servername(struct connection *conn, const char *hostname)
6779{
6780#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006781 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
William Lallemande18d4e82021-11-17 02:59:21 +01006782 struct server *s;
Willy Tarreau119a4082016-12-22 21:58:38 +01006783 char *prev_name;
6784
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006785 if (!ctx)
Willy Tarreau63076412015-07-10 11:33:32 +02006786 return;
Willy Tarreau77bfa662021-12-23 11:12:13 +01006787
6788 BUG_ON(!(conn->flags & CO_FL_WAIT_L6_CONN));
6789 BUG_ON(!(conn->flags & CO_FL_SSL_WAIT_HS));
6790
William Lallemande18d4e82021-11-17 02:59:21 +01006791 s = __objt_server(conn->target);
Willy Tarreau63076412015-07-10 11:33:32 +02006792
Willy Tarreau119a4082016-12-22 21:58:38 +01006793 /* if the SNI changes, we must destroy the reusable context so that a
William Lallemande18d4e82021-11-17 02:59:21 +01006794 * new connection will present a new SNI. compare with the SNI
6795 * previously stored in the reused_sess */
6796 /* the RWLOCK is used to ensure that we are not trying to flush the
6797 * cache from the CLI */
6798
6799 HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock);
6800 prev_name = s->ssl_ctx.reused_sess[tid].sni;
Willy Tarreau119a4082016-12-22 21:58:38 +01006801 if ((!prev_name && hostname) ||
6802 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01006803 SSL_set_session(ctx->ssl, NULL);
William Lallemande18d4e82021-11-17 02:59:21 +01006804 HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock);
Willy Tarreau119a4082016-12-22 21:58:38 +01006805
Olivier Houchard66ab4982019-02-26 18:37:15 +01006806 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02006807#endif
6808}
6809
Emeric Brun0abf8362014-06-24 18:26:41 +02006810/* Extract peer certificate's common name into the chunk dest
6811 * Returns
6812 * the len of the extracted common name
6813 * or 0 if no CN found in DN
6814 * or -1 on error case (i.e. no peer certificate)
6815 */
Willy Tarreau83061a82018-07-13 11:56:34 +02006816int ssl_sock_get_remote_common_name(struct connection *conn,
6817 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04006818{
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006819 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
David Safb76832014-05-08 23:42:08 -04006820 X509 *crt = NULL;
6821 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04006822 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02006823 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006824 .area = (char *)&find_cn,
6825 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04006826 };
Emeric Brun0abf8362014-06-24 18:26:41 +02006827 int result = -1;
David Safb76832014-05-08 23:42:08 -04006828
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006829 if (!ctx)
Emeric Brun0abf8362014-06-24 18:26:41 +02006830 goto out;
David Safb76832014-05-08 23:42:08 -04006831
6832 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006833 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04006834 if (!crt)
6835 goto out;
6836
6837 name = X509_get_subject_name(crt);
6838 if (!name)
6839 goto out;
David Safb76832014-05-08 23:42:08 -04006840
Emeric Brun0abf8362014-06-24 18:26:41 +02006841 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
6842out:
David Safb76832014-05-08 23:42:08 -04006843 if (crt)
6844 X509_free(crt);
6845
6846 return result;
6847}
6848
Dave McCowan328fb582014-07-30 10:39:13 -04006849/* returns 1 if client passed a certificate for this session, 0 if not */
6850int ssl_sock_get_cert_used_sess(struct connection *conn)
6851{
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006852 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
Dave McCowan328fb582014-07-30 10:39:13 -04006853 X509 *crt = NULL;
6854
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006855 if (!ctx)
Dave McCowan328fb582014-07-30 10:39:13 -04006856 return 0;
6857
6858 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006859 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04006860 if (!crt)
6861 return 0;
6862
6863 X509_free(crt);
6864 return 1;
6865}
6866
6867/* returns 1 if client passed a certificate for this connection, 0 if not */
6868int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04006869{
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006870 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006871
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006872 if (!ctx)
David Safb76832014-05-08 23:42:08 -04006873 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006874 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04006875}
6876
6877/* returns result from SSL verify */
6878unsigned int ssl_sock_get_verify_result(struct connection *conn)
6879{
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006880 struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006881
Willy Tarreau939b0bf2022-04-11 11:29:11 +02006882 if (!ctx)
David Safb76832014-05-08 23:42:08 -04006883 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006884 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04006885}
6886
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006887/* Returns the application layer protocol name in <str> and <len> when known.
6888 * Zero is returned if the protocol name was not found, otherwise non-zero is
6889 * returned. The string is allocated in the SSL context and doesn't have to be
6890 * freed by the caller. NPN is also checked if available since older versions
6891 * of openssl (1.0.1) which are more common in field only support this one.
6892 */
Amaury Denoyelle71e588c2021-11-12 11:23:29 +01006893int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006894{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006895#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
6896 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006897 struct ssl_sock_ctx *ctx = xprt_ctx;
6898 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006899 return 0;
6900
6901 *str = NULL;
6902
6903#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01006904 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006905 if (*str)
6906 return 1;
6907#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01006908#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006909 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006910 if (*str)
6911 return 1;
6912#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006913#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006914 return 0;
6915}
6916
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01006917/* "issuers-chain-path" load chain certificate in global */
William Lallemanddad31052020-05-14 17:47:32 +02006918int ssl_load_global_issuer_from_BIO(BIO *in, char *fp, char **err)
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01006919{
6920 X509 *ca;
6921 X509_NAME *name = NULL;
6922 ASN1_OCTET_STRING *skid = NULL;
6923 STACK_OF(X509) *chain = NULL;
6924 struct issuer_chain *issuer;
6925 struct eb64_node *node;
6926 char *path;
6927 u64 key;
6928 int ret = 0;
6929
6930 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
6931 if (chain == NULL) {
6932 chain = sk_X509_new_null();
6933 skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL);
6934 name = X509_get_subject_name(ca);
6935 }
6936 if (!sk_X509_push(chain, ca)) {
6937 X509_free(ca);
6938 goto end;
6939 }
6940 }
6941 if (!chain) {
6942 memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp);
6943 goto end;
6944 }
6945 if (!skid) {
6946 memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp);
6947 goto end;
6948 }
6949 if (!name) {
6950 memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp);
6951 goto end;
6952 }
Dragan Dosen967e7e72020-12-22 13:22:34 +01006953 key = XXH3(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0);
William Lallemande0f3fd52020-02-25 14:53:06 +01006954 for (node = eb64_lookup(&cert_issuer_tree, key); node; node = eb64_next(node)) {
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01006955 issuer = container_of(node, typeof(*issuer), node);
6956 if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) {
6957 memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path);
6958 goto end;
6959 }
6960 }
6961 issuer = calloc(1, sizeof *issuer);
6962 path = strdup(fp);
6963 if (!issuer || !path) {
6964 free(issuer);
6965 free(path);
6966 goto end;
6967 }
6968 issuer->node.key = key;
6969 issuer->path = path;
6970 issuer->chain = chain;
6971 chain = NULL;
William Lallemande0f3fd52020-02-25 14:53:06 +01006972 eb64_insert(&cert_issuer_tree, &issuer->node);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01006973 ret = 1;
6974 end:
6975 if (skid)
6976 ASN1_OCTET_STRING_free(skid);
6977 if (chain)
6978 sk_X509_pop_free(chain, X509_free);
6979 return ret;
6980}
6981
William Lallemandda8584c2020-05-14 10:14:37 +02006982 struct issuer_chain* ssl_get0_issuer_chain(X509 *cert)
Emmanuel Hocdet75a7aa12020-02-18 15:19:24 +01006983{
6984 AUTHORITY_KEYID *akid;
6985 struct issuer_chain *issuer = NULL;
6986
6987 akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL);
William Lallemandf69cd682020-11-19 16:24:13 +01006988 if (akid && akid->keyid) {
Emmanuel Hocdet75a7aa12020-02-18 15:19:24 +01006989 struct eb64_node *node;
6990 u64 hk;
Dragan Dosen967e7e72020-12-22 13:22:34 +01006991 hk = XXH3(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0);
Emmanuel Hocdet75a7aa12020-02-18 15:19:24 +01006992 for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) {
6993 struct issuer_chain *ti = container_of(node, typeof(*issuer), node);
6994 if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) {
6995 issuer = ti;
6996 break;
6997 }
6998 }
Emmanuel Hocdet75a7aa12020-02-18 15:19:24 +01006999 }
Remi Tricot-Le Breton1621dc12022-11-03 15:16:48 +01007000 AUTHORITY_KEYID_free(akid);
Emmanuel Hocdet75a7aa12020-02-18 15:19:24 +01007001 return issuer;
7002}
7003
William Lallemanddad31052020-05-14 17:47:32 +02007004void ssl_free_global_issuers(void)
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01007005{
7006 struct eb64_node *node, *back;
7007 struct issuer_chain *issuer;
7008
William Lallemande0f3fd52020-02-25 14:53:06 +01007009 node = eb64_first(&cert_issuer_tree);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01007010 while (node) {
7011 issuer = container_of(node, typeof(*issuer), node);
7012 back = eb64_next(node);
7013 eb64_delete(node);
7014 free(issuer->path);
7015 sk_X509_pop_free(issuer->chain, X509_free);
7016 free(issuer);
7017 node = back;
7018 }
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007019}
7020
William Lallemandd7bfbe22022-04-11 18:41:24 +02007021#if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007022static int ssl_check_async_engine_count(void) {
Christopher Fauletfc633b62020-11-06 15:24:23 +01007023 int err_code = ERR_NONE;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007024
Emeric Brun3854e012017-05-17 20:42:48 +02007025 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01007026 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007027 err_code = ERR_ABORT;
7028 }
7029 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01007030}
Willy Tarreau9ceda382016-12-21 23:13:03 +01007031#endif
7032
Willy Tarreaude5675a2021-01-20 14:41:29 +01007033/* "show fd" helper to dump ssl internals. Warning: the output buffer is often
Willy Tarreau4bd5d632021-01-21 08:53:50 +01007034 * the common trash! It returns non-zero if the connection entry looks suspicious.
Willy Tarreaude5675a2021-01-20 14:41:29 +01007035 */
Willy Tarreau8050efe2021-01-21 08:26:06 +01007036static int ssl_sock_show_fd(struct buffer *buf, const struct connection *conn, const void *ctx)
Willy Tarreaude5675a2021-01-20 14:41:29 +01007037{
7038 const struct ssl_sock_ctx *sctx = ctx;
Willy Tarreau4bd5d632021-01-21 08:53:50 +01007039 int ret = 0;
Willy Tarreaude5675a2021-01-20 14:41:29 +01007040
7041 if (!sctx)
Willy Tarreau4bd5d632021-01-21 08:53:50 +01007042 return ret;
Willy Tarreaude5675a2021-01-20 14:41:29 +01007043
Willy Tarreau4bd5d632021-01-21 08:53:50 +01007044 if (sctx->conn != conn) {
7045 chunk_appendf(&trash, " xctx.conn=%p(BOGUS)", sctx->conn);
7046 ret = 1;
7047 }
Christopher Fauletf19c6392023-03-14 15:51:33 +01007048 chunk_appendf(&trash, " xctx.st=%d .err=%ld", sctx->xprt_st, sctx->error_code);
Willy Tarreaude5675a2021-01-20 14:41:29 +01007049
7050 if (sctx->xprt) {
7051 chunk_appendf(&trash, " .xprt=%s", sctx->xprt->name);
7052 if (sctx->xprt_ctx)
7053 chunk_appendf(&trash, " .xctx=%p", sctx->xprt_ctx);
7054 }
7055
7056 chunk_appendf(&trash, " .wait.ev=%d", sctx->wait_event.events);
Willy Tarreau4bd5d632021-01-21 08:53:50 +01007057
7058 /* as soon as a shutdown is reported the lower layer unregisters its
7059 * subscriber, so the situations below are transient and rare enough to
7060 * be reported as suspicious. In any case they shouldn't last.
7061 */
7062 if ((sctx->wait_event.events & 1) && (conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_ERROR)))
7063 ret = 1;
7064 if ((sctx->wait_event.events & 2) && (conn->flags & (CO_FL_SOCK_WR_SH|CO_FL_ERROR)))
7065 ret = 1;
7066
Willy Tarreaude5675a2021-01-20 14:41:29 +01007067 chunk_appendf(&trash, " .subs=%p", sctx->subs);
7068 if (sctx->subs) {
7069 chunk_appendf(&trash, "(ev=%d tl=%p", sctx->subs->events, sctx->subs->tasklet);
Willy Tarreau4bd5d632021-01-21 08:53:50 +01007070 if (sctx->subs->tasklet->calls >= 1000000)
7071 ret = 1;
Willy Tarreaude5675a2021-01-20 14:41:29 +01007072 chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=",
7073 sctx->subs->tasklet->calls,
7074 sctx->subs->tasklet->context);
7075 resolve_sym_name(&trash, NULL, sctx->subs->tasklet->process);
7076 chunk_appendf(&trash, ")");
7077 }
7078 chunk_appendf(&trash, " .sent_early=%d", sctx->sent_early_data);
7079 chunk_appendf(&trash, " .early_in=%d", (int)sctx->early_buf.data);
Willy Tarreau4bd5d632021-01-21 08:53:50 +01007080 return ret;
Willy Tarreaude5675a2021-01-20 14:41:29 +01007081}
7082
Willy Tarreaub6fc5242021-01-05 10:44:30 +01007083#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
William Lallemand32af2032016-10-29 18:09:35 +02007084/* This function is used with TLS ticket keys management. It permits to browse
Willy Tarreaub6fc5242021-01-05 10:44:30 +01007085 * each reference. The variable <ref> must point to the current node's list
7086 * element (which starts by the root), and <end> must point to the root node.
William Lallemand32af2032016-10-29 18:09:35 +02007087 */
William Lallemand32af2032016-10-29 18:09:35 +02007088static inline
Willy Tarreaub6fc5242021-01-05 10:44:30 +01007089struct tls_keys_ref *tlskeys_list_get_next(struct list *ref, struct list *end)
William Lallemand32af2032016-10-29 18:09:35 +02007090{
Tim Duesterhus2c7bb332021-01-03 01:29:55 +01007091 /* Get next list entry. */
Willy Tarreaub6fc5242021-01-05 10:44:30 +01007092 ref = ref->n;
William Lallemand32af2032016-10-29 18:09:35 +02007093
Tim Duesterhus2c7bb332021-01-03 01:29:55 +01007094 /* If the entry is the last of the list, return NULL. */
Willy Tarreaub6fc5242021-01-05 10:44:30 +01007095 if (ref == end)
Tim Duesterhus2c7bb332021-01-03 01:29:55 +01007096 return NULL;
William Lallemand32af2032016-10-29 18:09:35 +02007097
Willy Tarreaub6fc5242021-01-05 10:44:30 +01007098 return LIST_ELEM(ref, struct tls_keys_ref *, list);
William Lallemand32af2032016-10-29 18:09:35 +02007099}
7100
7101static inline
7102struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
7103{
7104 int id;
7105 char *error;
7106
7107 /* If the reference starts by a '#', this is numeric id. */
7108 if (reference[0] == '#') {
7109 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
7110 id = strtol(reference + 1, &error, 10);
7111 if (*error != '\0')
7112 return NULL;
7113
7114 /* Perform the unique id lookup. */
7115 return tlskeys_ref_lookupid(id);
7116 }
7117
7118 /* Perform the string lookup. */
7119 return tlskeys_ref_lookup(reference);
7120}
7121#endif
7122
7123
7124#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
7125
Willy Tarreaua9380522022-05-05 08:50:17 +02007126/* dumps all tls keys. Relies on the show_keys_ctx context from the appctx. */
Willy Tarreaubd338642022-05-05 08:59:17 +02007127static int cli_io_handler_tlskeys_files(struct appctx *appctx)
7128{
Willy Tarreaua9380522022-05-05 08:50:17 +02007129 struct show_keys_ctx *ctx = appctx->svcctx;
William Lallemand32af2032016-10-29 18:09:35 +02007130
Willy Tarreau9c5a38c2022-05-05 09:03:44 +02007131 switch (ctx->state) {
7132 case SHOW_KEYS_INIT:
William Lallemand32af2032016-10-29 18:09:35 +02007133 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -08007134 * quit the function with returning 0. The function is called
Willy Tarreau9c5a38c2022-05-05 09:03:44 +02007135 * later and restart at the state "SHOW_KEYS_INIT".
William Lallemand32af2032016-10-29 18:09:35 +02007136 */
7137 chunk_reset(&trash);
7138
Willy Tarreaubd338642022-05-05 08:59:17 +02007139 if (ctx->dump_entries)
William Lallemand32af2032016-10-29 18:09:35 +02007140 chunk_appendf(&trash, "# id secret\n");
7141 else
7142 chunk_appendf(&trash, "# id (file)\n");
7143
Willy Tarreaud0a06d52022-05-18 15:07:19 +02007144 if (applet_putchk(appctx, &trash) == -1)
William Lallemand32af2032016-10-29 18:09:35 +02007145 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02007146
William Lallemand32af2032016-10-29 18:09:35 +02007147 /* Now, we start the browsing of the references lists.
7148 * Note that the following call to LIST_ELEM return bad pointer. The only
7149 * available field of this pointer is <list>. It is used with the function
7150 * tlskeys_list_get_next() for retruning the first available entry
7151 */
Willy Tarreaua9380522022-05-05 08:50:17 +02007152 if (ctx->next_ref == NULL)
7153 ctx->next_ref = tlskeys_list_get_next(&tlskeys_reference, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +02007154
Willy Tarreau9c5a38c2022-05-05 09:03:44 +02007155 ctx->state = SHOW_KEYS_LIST;
Willy Tarreaua551f4f2022-11-14 07:34:43 +01007156 __fallthrough;
William Lallemand32af2032016-10-29 18:09:35 +02007157
Willy Tarreau9c5a38c2022-05-05 09:03:44 +02007158 case SHOW_KEYS_LIST:
Willy Tarreaua9380522022-05-05 08:50:17 +02007159 while (ctx->next_ref) {
7160 struct tls_keys_ref *ref = ctx->next_ref;
William Lallemand32af2032016-10-29 18:09:35 +02007161
7162 chunk_reset(&trash);
Willy Tarreaubd338642022-05-05 08:59:17 +02007163 if (ctx->dump_entries && ctx->next_index == 0)
William Lallemand32af2032016-10-29 18:09:35 +02007164 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007165
Willy Tarreaua9380522022-05-05 08:50:17 +02007166 if (ctx->next_index == 0)
Willy Tarreauf5f26e82016-12-16 18:47:27 +01007167 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
7168
Willy Tarreaubd338642022-05-05 08:59:17 +02007169 if (ctx->dump_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +01007170 int head;
7171
7172 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
7173 head = ref->tls_ticket_enc_index;
Willy Tarreaua9380522022-05-05 08:50:17 +02007174 while (ctx->next_index < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +02007175 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +02007176
7177 chunk_reset(t2);
7178 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +01007179 if (ref->key_size_bits == 128) {
Willy Tarreaua9380522022-05-05 08:50:17 +02007180 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + ctx->next_index) % TLS_TICKETS_NO),
Emeric Brun9e754772019-01-10 17:51:55 +01007181 sizeof(struct tls_sess_key_128),
7182 t2->area, t2->size);
Willy Tarreaua9380522022-05-05 08:50:17 +02007183 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, ctx->next_index,
Emeric Brun9e754772019-01-10 17:51:55 +01007184 t2->area);
7185 }
7186 else if (ref->key_size_bits == 256) {
Willy Tarreaua9380522022-05-05 08:50:17 +02007187 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + ctx->next_index) % TLS_TICKETS_NO),
Emeric Brun9e754772019-01-10 17:51:55 +01007188 sizeof(struct tls_sess_key_256),
7189 t2->area, t2->size);
Willy Tarreaua9380522022-05-05 08:50:17 +02007190 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, ctx->next_index,
Emeric Brun9e754772019-01-10 17:51:55 +01007191 t2->area);
7192 }
7193 else {
7194 /* This case should never happen */
Willy Tarreaua9380522022-05-05 08:50:17 +02007195 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, ctx->next_index);
Emeric Brun9e754772019-01-10 17:51:55 +01007196 }
William Lallemand32af2032016-10-29 18:09:35 +02007197
Willy Tarreaud0a06d52022-05-18 15:07:19 +02007198 if (applet_putchk(appctx, &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +02007199 /* let's try again later from this stream. We add ourselves into
7200 * this stream's users so that it can remove us upon termination.
7201 */
Christopher Faulet16f45c82018-02-16 11:23:49 +01007202 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
William Lallemand32af2032016-10-29 18:09:35 +02007203 return 0;
7204 }
Willy Tarreaua9380522022-05-05 08:50:17 +02007205 ctx->next_index++;
William Lallemand32af2032016-10-29 18:09:35 +02007206 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01007207 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaua9380522022-05-05 08:50:17 +02007208 ctx->next_index = 0;
William Lallemand32af2032016-10-29 18:09:35 +02007209 }
Willy Tarreaud0a06d52022-05-18 15:07:19 +02007210 if (applet_putchk(appctx, &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +02007211 /* let's try again later from this stream. We add ourselves into
7212 * this stream's users so that it can remove us upon termination.
7213 */
William Lallemand32af2032016-10-29 18:09:35 +02007214 return 0;
7215 }
7216
Willy Tarreaua9380522022-05-05 08:50:17 +02007217 if (ctx->names_only == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +02007218 break;
7219
7220 /* get next list entry and check the end of the list */
Willy Tarreaua9380522022-05-05 08:50:17 +02007221 ctx->next_ref = tlskeys_list_get_next(&ref->list, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +02007222 }
Willy Tarreau9c5a38c2022-05-05 09:03:44 +02007223 ctx->state = SHOW_KEYS_DONE;
Willy Tarreaua551f4f2022-11-14 07:34:43 +01007224 __fallthrough;
William Lallemand32af2032016-10-29 18:09:35 +02007225
7226 default:
William Lallemand32af2032016-10-29 18:09:35 +02007227 return 1;
7228 }
7229 return 0;
7230}
7231
Willy Tarreaua9380522022-05-05 08:50:17 +02007232/* Prepares a "show_keys_ctx" and sets the appropriate io_handler if needed */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007233static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +02007234{
Willy Tarreaua9380522022-05-05 08:50:17 +02007235 struct show_keys_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
7236
William Lallemand32af2032016-10-29 18:09:35 +02007237 /* no parameter, shows only file list */
7238 if (!*args[2]) {
Willy Tarreaua9380522022-05-05 08:50:17 +02007239 ctx->names_only = 1;
Willy Tarreau3067bfa2016-12-05 14:50:15 +01007240 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02007241 }
7242
7243 if (args[2][0] == '*') {
Remi Tricot-Le Bretonc8d814e2022-12-20 11:11:17 +01007244 /* list every TLS ticket keys */
7245 ctx->names_only = 1;
Remi Tricot-Le Bretond92fd112021-06-10 13:51:13 +02007246 } else {
Remi Tricot-Le Bretonc8d814e2022-12-20 11:11:17 +01007247 ctx->next_ref = tlskeys_ref_lookup_ref(args[2]);
7248 if (!ctx->next_ref)
7249 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
Remi Tricot-Le Bretond92fd112021-06-10 13:51:13 +02007250 }
7251
Remi Tricot-Le Bretonc8d814e2022-12-20 11:11:17 +01007252 ctx->dump_entries = 1;
7253 return 0;
7254}
Remi Tricot-Le Bretond92fd112021-06-10 13:51:13 +02007255
Remi Tricot-Le Bretonc8d814e2022-12-20 11:11:17 +01007256static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
7257{
7258 struct tls_keys_ref *ref;
7259 int ret;
Remi Tricot-Le Bretond92fd112021-06-10 13:51:13 +02007260
Remi Tricot-Le Bretonc8d814e2022-12-20 11:11:17 +01007261 /* Expect two parameters: the filename and the new new TLS key in encoding */
7262 if (!*args[3] || !*args[4])
7263 return cli_err(appctx, "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n");
Remi Tricot-Le Bretond92fd112021-06-10 13:51:13 +02007264
Remi Tricot-Le Bretonc8d814e2022-12-20 11:11:17 +01007265 ref = tlskeys_ref_lookup_ref(args[3]);
7266 if (!ref)
7267 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
Remi Tricot-Le Bretond92fd112021-06-10 13:51:13 +02007268
Remi Tricot-Le Bretonc8d814e2022-12-20 11:11:17 +01007269 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
7270 if (ret < 0)
7271 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Remi Tricot-Le Bretond92fd112021-06-10 13:51:13 +02007272
Remi Tricot-Le Bretonc8d814e2022-12-20 11:11:17 +01007273 trash.data = ret;
7274 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
7275 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
Remi Tricot-Le Bretond92fd112021-06-10 13:51:13 +02007276
Remi Tricot-Le Bretonc8d814e2022-12-20 11:11:17 +01007277 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
Remi Tricot-Le Bretond92fd112021-06-10 13:51:13 +02007278}
Remi Tricot-Le Bretonc8d814e2022-12-20 11:11:17 +01007279#endif
7280
Remi Tricot-Le Bretond92fd112021-06-10 13:51:13 +02007281
Remi Tricot-Le Bretonf87c67e2022-04-21 12:06:41 +02007282#ifdef HAVE_SSL_PROVIDERS
7283struct provider_name {
7284 const char *name;
7285 struct list list;
7286};
7287
7288
7289static int ssl_provider_get_name_cb(OSSL_PROVIDER *provider, void *cbdata)
7290{
7291 struct list *provider_names = cbdata;
7292 struct provider_name *item = NULL;
7293 const char *name = OSSL_PROVIDER_get0_name(provider);
7294
7295 if (!provider_names)
7296 return 0;
7297
7298 item = calloc(1, sizeof(*item));
7299
7300 if (!item)
7301 return 0;
7302
7303 item->name = name;
7304 LIST_APPEND(provider_names, &item->list);
7305
7306 return 1;
7307}
7308
7309static void ssl_provider_get_name_list(struct list *provider_names)
7310{
7311 if (!provider_names)
7312 return;
7313
7314 OSSL_PROVIDER_do_all(NULL, ssl_provider_get_name_cb, provider_names);
7315}
7316
7317static void ssl_provider_clear_name_list(struct list *provider_names)
7318{
7319 struct provider_name *item = NULL, *item_s = NULL;
7320
7321 if (provider_names) {
7322 list_for_each_entry_safe(item, item_s, provider_names, list) {
7323 LIST_DELETE(&item->list);
7324 free(item);
7325 }
7326 }
7327}
7328
7329static int cli_io_handler_show_providers(struct appctx *appctx)
7330{
7331 struct buffer *trash = get_trash_chunk();
Remi Tricot-Le Bretonf87c67e2022-04-21 12:06:41 +02007332 struct list provider_names;
7333 struct provider_name *name;
7334
7335 LIST_INIT(&provider_names);
7336
7337 chunk_appendf(trash, "Loaded providers : \n");
7338
7339 ssl_provider_get_name_list(&provider_names);
7340
7341 list_for_each_entry(name, &provider_names, list) {
7342 chunk_appendf(trash, "\t- %s\n", name->name);
7343 }
7344
7345 ssl_provider_clear_name_list(&provider_names);
7346
Willy Tarreaud0a06d52022-05-18 15:07:19 +02007347 if (applet_putchk(appctx, trash) == -1)
Remi Tricot-Le Bretonf87c67e2022-04-21 12:06:41 +02007348 goto yield;
Remi Tricot-Le Bretonf87c67e2022-04-21 12:06:41 +02007349
7350 return 1;
7351
7352yield:
7353 return 0;
7354}
7355#endif
Remi Tricot-Le Bretoneeaa29b2022-12-20 11:11:07 +01007356
Remi Tricot-Le Bretond92fd112021-06-10 13:51:13 +02007357
William Lallemand32af2032016-10-29 18:09:35 +02007358/* register cli keywords */
7359static struct cli_kw_list cli_kws = {{ },{
7360#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Remi Tricot-Le Bretoneeaa29b2022-12-20 11:11:07 +01007361 { { "show", "tls-keys", NULL }, "show tls-keys [id|*] : show tls keys references or dump tls ticket keys when id specified", cli_parse_show_tlskeys, cli_io_handler_tlskeys_files },
7362 { { "set", "ssl", "tls-key", NULL }, "set ssl tls-key [id|file] <key> : set the next TLS key for the <id> or <file> listener to <key>", cli_parse_set_tlskeys, NULL },
William Lallemand32af2032016-10-29 18:09:35 +02007363#endif
Remi Tricot-Le Bretonf87c67e2022-04-21 12:06:41 +02007364#ifdef HAVE_SSL_PROVIDERS
7365 { { "show", "ssl", "providers", NULL }, "show ssl providers : show loaded SSL providers", NULL, cli_io_handler_show_providers },
7366#endif
William Lallemand32af2032016-10-29 18:09:35 +02007367 { { NULL }, NULL, NULL, NULL }
7368}};
7369
Willy Tarreau0108d902018-11-25 19:14:37 +01007370INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +02007371
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02007372/* transport-layer operations for SSL sockets */
William Lallemanddad31052020-05-14 17:47:32 +02007373struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +02007374 .snd_buf = ssl_sock_from_buf,
7375 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +01007376 .subscribe = ssl_subscribe,
7377 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +02007378 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +02007379 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +02007380 .rcv_pipe = NULL,
7381 .snd_pipe = NULL,
7382 .shutr = NULL,
7383 .shutw = ssl_sock_shutw,
7384 .close = ssl_sock_close,
7385 .init = ssl_sock_init,
Olivier Houchardbc5ce922021-03-05 23:47:00 +01007386 .start = ssl_sock_start,
Willy Tarreau55d37912016-12-21 23:38:39 +01007387 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +01007388 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +01007389 .prepare_srv = ssl_sock_prepare_srv_ctx,
7390 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007391 .get_alpn = ssl_sock_get_alpn,
Olivier Houcharda74bb7e2020-07-03 14:01:21 +02007392 .takeover = ssl_takeover,
Willy Tarreau41491682021-03-02 17:29:56 +01007393 .set_idle = ssl_set_idle,
7394 .set_used = ssl_set_used,
Willy Tarreaude827952022-04-11 10:43:28 +02007395 .get_ssl_sock_ctx = ssl_sock_get_ctx,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +01007396 .name = "SSL",
Willy Tarreaude5675a2021-01-20 14:41:29 +01007397 .show_fd = ssl_sock_show_fd,
Emeric Brun46591952012-05-18 15:47:34 +02007398};
7399
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007400enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
7401 struct session *sess, struct stream *s, int flags)
7402{
7403 struct connection *conn;
7404
7405 conn = objt_conn(sess->origin);
7406
Willy Tarreaub52d4d22022-05-27 10:44:39 +02007407 if (conn) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007408 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Willy Tarreaub52d4d22022-05-27 10:44:39 +02007409 sc_ep_set(s->scf, SE_FL_WAIT_FOR_HS);
Christopher Faulet6e1bbc42022-12-12 08:08:15 +01007410 s->req.flags |= CF_READ_EVENT;
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007411 return ACT_RET_YIELD;
7412 }
7413 }
7414 return (ACT_RET_CONT);
7415}
7416
7417static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
7418{
7419 rule->action_ptr = ssl_action_wait_for_hs;
7420
7421 return ACT_RET_PRS_OK;
7422}
7423
7424static struct action_kw_list http_req_actions = {ILH, {
7425 { "wait-for-handshake", ssl_parse_wait_for_hs },
7426 { /* END */ }
7427}};
7428
Willy Tarreau0108d902018-11-25 19:14:37 +01007429INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
7430
Ilya Shipitsinf00cdb12021-02-06 18:59:22 +05007431#ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01007432
7433static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
7434{
7435 if (ptr) {
7436 chunk_destroy(ptr);
7437 free(ptr);
7438 }
7439}
7440
7441#endif
William Lallemand76b4a122020-08-04 17:41:39 +02007442
William Lallemand76b4a122020-08-04 17:41:39 +02007443
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +01007444static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
7445{
Willy Tarreaubafbe012017-11-24 17:34:44 +01007446 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +01007447}
William Lallemand7d42ef52020-07-06 11:41:30 +02007448
William Lallemand722180a2021-06-09 16:46:12 +02007449#ifdef HAVE_SSL_KEYLOG
William Lallemand7d42ef52020-07-06 11:41:30 +02007450static void ssl_sock_keylog_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
7451{
7452 struct ssl_keylog *keylog;
7453
7454 if (!ptr)
7455 return;
7456
7457 keylog = ptr;
7458
7459 pool_free(pool_head_ssl_keylog_str, keylog->client_random);
7460 pool_free(pool_head_ssl_keylog_str, keylog->client_early_traffic_secret);
7461 pool_free(pool_head_ssl_keylog_str, keylog->client_handshake_traffic_secret);
7462 pool_free(pool_head_ssl_keylog_str, keylog->server_handshake_traffic_secret);
7463 pool_free(pool_head_ssl_keylog_str, keylog->client_traffic_secret_0);
7464 pool_free(pool_head_ssl_keylog_str, keylog->server_traffic_secret_0);
7465 pool_free(pool_head_ssl_keylog_str, keylog->exporter_secret);
7466 pool_free(pool_head_ssl_keylog_str, keylog->early_exporter_secret);
7467
7468 pool_free(pool_head_ssl_keylog, ptr);
7469}
7470#endif
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01007471
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02007472static void ssl_sock_clt_crt_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
7473{
7474 if (!ptr)
7475 return;
7476
7477 X509_free((X509*)ptr);
7478}
7479
Remi Tricot-Le Bretona9967632022-01-07 17:12:01 +01007480static void ssl_sock_clt_sni_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
7481{
7482 pool_free(ssl_sock_client_sni_pool, ptr);
7483}
7484
Willy Tarreau92faadf2012-10-10 23:04:25 +02007485static void __ssl_sock_init(void)
7486{
Ilya Shipitsin0590f442019-05-25 19:30:50 +05007487#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +02007488 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +05007489 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +05007490#endif
Emeric Brun46591952012-05-18 15:47:34 +02007491
Willy Tarreauef934602016-12-22 23:12:01 +01007492 if (global_ssl.listen_default_ciphers)
7493 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
7494 if (global_ssl.connect_default_ciphers)
7495 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Ilya Shipitsinf34ed0b2020-11-21 14:37:34 +05007496#ifdef HAVE_SSL_CTX_SET_CIPHERSUITES
Dirkjan Bussink415150f2018-09-14 11:14:21 +02007497 if (global_ssl.listen_default_ciphersuites)
7498 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
7499 if (global_ssl.connect_default_ciphersuites)
7500 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
7501#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +01007502
Willy Tarreau13e14102016-12-22 20:25:26 +01007503 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007504#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +02007505 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -08007506#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +05007507#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +02007508 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +05007509 n = sk_SSL_COMP_num(cm);
7510 while (n--) {
7511 (void) sk_SSL_COMP_pop(cm);
7512 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +05007513#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +05007514
Willy Tarreau5db847a2019-05-09 14:13:35 +02007515#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +02007516 ssl_locking_init();
7517#endif
Ilya Shipitsinf00cdb12021-02-06 18:59:22 +05007518#ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01007519 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
7520#endif
William Lallemand76b4a122020-08-04 17:41:39 +02007521
7522#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL)
7523 ocsp_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_ocsp_free_func);
7524#endif
7525
Thierry FOURNIER28962c92018-06-17 21:37:05 +02007526 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +02007527 ssl_capture_ptr_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_capture_free_func);
Amaury Denoyelle9320dd52022-01-19 10:03:30 +01007528#ifdef USE_QUIC
7529 ssl_qc_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
7530#endif /* USE_QUIC */
William Lallemand722180a2021-06-09 16:46:12 +02007531#ifdef HAVE_SSL_KEYLOG
William Lallemand7d42ef52020-07-06 11:41:30 +02007532 ssl_keylog_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_keylog_free_func);
7533#endif
Remi Tricot-Le Breton74f6ab62021-08-19 18:06:30 +02007534 ssl_client_crt_ref_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_clt_crt_free_func);
Remi Tricot-Le Bretona9967632022-01-07 17:12:01 +01007535 ssl_client_sni_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_clt_sni_free_func);
William Lallemandd7bfbe22022-04-11 18:41:24 +02007536#if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE)
Grant Zhang872f9c22017-01-21 01:10:18 +00007537 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007538 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02007539#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +01007540#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
7541 hap_register_post_check(tlskeys_finalize_config);
7542#endif
Willy Tarreau80713382018-11-26 10:19:54 +01007543
7544 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
7545 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
7546
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01007547 hap_register_post_deinit(ssl_free_global_issuers);
7548
Willy Tarreau80713382018-11-26 10:19:54 +01007549#ifndef OPENSSL_NO_DH
7550 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
7551 hap_register_post_deinit(ssl_free_dh);
7552#endif
William Lallemandd7bfbe22022-04-11 18:41:24 +02007553#if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE)
Willy Tarreau80713382018-11-26 10:19:54 +01007554 hap_register_post_deinit(ssl_free_engines);
7555#endif
Remi Tricot-Le Breton1746a382022-05-16 16:24:33 +02007556#ifdef HAVE_SSL_PROVIDERS
7557 hap_register_post_deinit(ssl_unload_providers);
7558#endif
Remi Tricot-Le Breton78a36e32022-02-11 12:04:45 +01007559#if HA_OPENSSL_VERSION_NUMBER < 0x3000000fL
Willy Tarreau80713382018-11-26 10:19:54 +01007560 /* Load SSL string for the verbose & debug mode. */
7561 ERR_load_SSL_strings();
Remi Tricot-Le Breton78a36e32022-02-11 12:04:45 +01007562#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +02007563 ha_meth = BIO_meth_new(0x666, "ha methods");
7564 BIO_meth_set_write(ha_meth, ha_ssl_write);
7565 BIO_meth_set_read(ha_meth, ha_ssl_read);
7566 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
7567 BIO_meth_set_create(ha_meth, ha_ssl_new);
7568 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
7569 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
7570 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
William Lallemand150bfa82019-09-19 17:12:49 +02007571
7572 HA_SPIN_INIT(&ckch_lock);
Dragan Dosen1e7ed042020-05-08 18:30:00 +02007573
Remi Tricot-Le Breton2b963642022-12-20 11:11:02 +01007574 HA_SPIN_INIT(&ocsp_tree_lock);
7575
Dragan Dosen9ac98092020-05-11 15:51:45 +02007576 /* Try to register dedicated SSL/TLS protocol message callbacks for
7577 * heartbleed attack (CVE-2014-0160) and clienthello.
7578 */
7579 hap_register_post_check(ssl_sock_register_msg_callbacks);
7580
Dragan Dosen1e7ed042020-05-08 18:30:00 +02007581 /* Try to free all callbacks that were registered by using
7582 * ssl_sock_register_msg_callback().
7583 */
7584 hap_register_post_deinit(ssl_sock_unregister_msg_callbacks);
Willy Tarreau80713382018-11-26 10:19:54 +01007585}
Willy Tarreau79367f92022-04-25 19:18:24 +02007586INITCALL0(STG_REGISTER, __ssl_sock_init);
Willy Tarreaud92aa5c2015-01-15 21:34:39 +01007587
Willy Tarreau80713382018-11-26 10:19:54 +01007588/* Compute and register the version string */
7589static void ssl_register_build_options()
7590{
7591 char *ptr = NULL;
7592 int i;
7593
Willy Tarreauc2c0b612016-12-21 19:23:20 +01007594 memprintf(&ptr, "Built with OpenSSL version : "
7595#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +01007596 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +01007597#else /* OPENSSL_IS_BORINGSSL */
7598 OPENSSL_VERSION_TEXT
7599 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -08007600 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +02007601 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +01007602#endif
7603 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007604#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +01007605 "no (library version too old)"
7606#elif defined(OPENSSL_NO_TLSEXT)
7607 "no (disabled via OPENSSL_NO_TLSEXT)"
7608#else
7609 "yes"
7610#endif
7611 "", ptr);
7612
7613 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
7614#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
7615 "yes"
7616#else
7617#ifdef OPENSSL_NO_TLSEXT
7618 "no (because of OPENSSL_NO_TLSEXT)"
7619#else
7620 "no (version might be too old, 0.9.8f min needed)"
7621#endif
7622#endif
7623 "", ptr);
7624
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +02007625 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
7626 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
7627 if (methodVersions[i].option)
7628 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +01007629
Remi Tricot-Le Bretonf87c67e2022-04-21 12:06:41 +02007630#ifdef HAVE_SSL_PROVIDERS
7631 {
7632 struct list provider_names;
7633 struct provider_name *name;
7634 LIST_INIT(&provider_names);
7635 ssl_provider_get_name_list(&provider_names);
7636
7637 memprintf(&ptr, "%s\nOpenSSL providers loaded :", ptr);
7638
7639 list_for_each_entry(name, &provider_names, list) {
7640 memprintf(&ptr, "%s %s", ptr, name->name);
7641 }
7642
7643 ssl_provider_clear_name_list(&provider_names);
7644 }
7645#endif
7646
Willy Tarreauc2c0b612016-12-21 19:23:20 +01007647 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +01007648}
Willy Tarreauc2c0b612016-12-21 19:23:20 +01007649
Willy Tarreau80713382018-11-26 10:19:54 +01007650INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +02007651
William Lallemandd7bfbe22022-04-11 18:41:24 +02007652#if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE)
Grant Zhang872f9c22017-01-21 01:10:18 +00007653void ssl_free_engines(void) {
7654 struct ssl_engine_list *wl, *wlb;
7655 /* free up engine list */
7656 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
7657 ENGINE_finish(wl->e);
7658 ENGINE_free(wl->e);
Willy Tarreau2b718102021-04-21 07:32:39 +02007659 LIST_DELETE(&wl->list);
Grant Zhang872f9c22017-01-21 01:10:18 +00007660 free(wl);
7661 }
7662}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02007663#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +02007664
Remi Tricot-Le Breton1746a382022-05-16 16:24:33 +02007665#ifdef HAVE_SSL_PROVIDERS
7666void ssl_unload_providers(void) {
7667 struct ssl_provider_list *prov, *provb;
7668 list_for_each_entry_safe(prov, provb, &openssl_providers, list) {
7669 OSSL_PROVIDER_unload(prov->provider);
7670 LIST_DELETE(&prov->list);
7671 free(prov);
7672 }
7673}
7674#endif
7675
Remi Gacogned3a23c32015-05-28 16:39:47 +02007676#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +00007677void ssl_free_dh(void) {
7678 if (local_dh_1024) {
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +01007679 HASSL_DH_free(local_dh_1024);
Grant Zhang872f9c22017-01-21 01:10:18 +00007680 local_dh_1024 = NULL;
7681 }
7682 if (local_dh_2048) {
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +01007683 HASSL_DH_free(local_dh_2048);
Grant Zhang872f9c22017-01-21 01:10:18 +00007684 local_dh_2048 = NULL;
7685 }
7686 if (local_dh_4096) {
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +01007687 HASSL_DH_free(local_dh_4096);
Grant Zhang872f9c22017-01-21 01:10:18 +00007688 local_dh_4096 = NULL;
7689 }
Remi Gacogne47783ef2015-05-29 15:53:22 +02007690 if (global_dh) {
Remi Tricot-Le Bretonc76c3c42022-02-11 12:04:55 +01007691 HASSL_DH_free(global_dh);
Remi Gacogne47783ef2015-05-29 15:53:22 +02007692 global_dh = NULL;
7693 }
Grant Zhang872f9c22017-01-21 01:10:18 +00007694}
7695#endif
7696
Grant Zhang872f9c22017-01-21 01:10:18 +00007697static void __ssl_sock_deinit(void)
7698{
7699#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02007700 if (ssl_ctx_lru_tree) {
7701 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01007702 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +02007703 }
Remi Gacogned3a23c32015-05-28 16:39:47 +02007704#endif
7705
Willy Tarreau5db847a2019-05-09 14:13:35 +02007706#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +02007707 ERR_remove_state(0);
7708 ERR_free_strings();
7709
7710 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -08007711#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +02007712
Willy Tarreau5db847a2019-05-09 14:13:35 +02007713#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +02007714 CRYPTO_cleanup_all_ex_data();
7715#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +02007716 BIO_meth_free(ha_meth);
Remi Tricot-Le Breton6477bbd2022-12-20 11:11:13 +01007717
7718#if !defined OPENSSL_NO_OCSP
7719 ssl_destroy_ocsp_update_task();
7720#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +02007721}
Willy Tarreau79367f92022-04-25 19:18:24 +02007722REGISTER_POST_DEINIT(__ssl_sock_deinit);
Remi Gacogned3a23c32015-05-28 16:39:47 +02007723
William Dauchyf6370442020-11-14 19:25:33 +01007724
Emeric Brun46591952012-05-18 15:47:34 +02007725/*
7726 * Local variables:
7727 * c-indent-level: 8
7728 * c-basic-offset: 8
7729 * End:
7730 */