blob: 0da24c48cf1d0aa38d09fb4033d812b081f82a17 [file] [log] [blame]
yanbzhu08ce6ab2015-12-02 13:01:29 -05001
Emeric Brun46591952012-05-18 15:47:34 +02002/*
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003 * SSL/TLS transport layer over SOCK_STREAM sockets
Emeric Brun46591952012-05-18 15:47:34 +02004 *
5 * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Willy Tarreau69845df2012-09-10 09:43:09 +020012 * Acknowledgement:
13 * We'd like to specially thank the Stud project authors for a very clean
14 * and well documented code which helped us understand how the OpenSSL API
15 * ought to be used in non-blocking mode. This is one difficult part which
16 * is not easy to get from the OpenSSL doc, and reading the Stud code made
17 * it much more obvious than the examples in the OpenSSL package. Keep up
18 * the good works, guys !
19 *
20 * Stud is an extremely efficient and scalable SSL/TLS proxy which combines
21 * particularly well with haproxy. For more info about this project, visit :
22 * https://github.com/bumptech/stud
23 *
Emeric Brun46591952012-05-18 15:47:34 +020024 */
25
Willy Tarreau8d164dc2019-05-10 09:35:00 +020026/* Note: do NOT include openssl/xxx.h here, do it in openssl-compat.h */
Emeric Brun46591952012-05-18 15:47:34 +020027#define _GNU_SOURCE
Emeric Brunfc0421f2012-09-07 17:30:07 +020028#include <ctype.h>
29#include <dirent.h>
Emeric Brun46591952012-05-18 15:47:34 +020030#include <errno.h>
31#include <fcntl.h>
32#include <stdio.h>
33#include <stdlib.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020034#include <string.h>
35#include <unistd.h>
Emeric Brun46591952012-05-18 15:47:34 +020036
37#include <sys/socket.h>
38#include <sys/stat.h>
39#include <sys/types.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020040#include <netdb.h>
Emeric Brun46591952012-05-18 15:47:34 +020041#include <netinet/tcp.h>
42
Christopher Faulet31af49d2015-06-09 17:29:50 +020043#include <import/lru.h>
44#include <import/xxhash.h>
45
Emeric Brun46591952012-05-18 15:47:34 +020046#include <common/buffer.h>
Willy Tarreau843b7cb2018-07-13 10:54:26 +020047#include <common/chunk.h>
Emeric Brun46591952012-05-18 15:47:34 +020048#include <common/compat.h>
49#include <common/config.h>
50#include <common/debug.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020051#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010052#include <common/initcall.h>
Willy Tarreau55994562019-05-09 14:52:44 +020053#include <common/openssl-compat.h>
Emeric Brun46591952012-05-18 15:47:34 +020054#include <common/standard.h>
55#include <common/ticks.h>
56#include <common/time.h>
Emeric Brun2c86cbf2014-10-30 15:56:50 +010057#include <common/cfgparse.h>
Nenad Merdanovic05552d42015-02-27 19:56:49 +010058#include <common/base64.h>
Emeric Brun46591952012-05-18 15:47:34 +020059
William Lallemand2954c472020-03-06 21:54:13 +010060#include <ebpttree.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020061#include <ebsttree.h>
62
William Lallemand32af2032016-10-29 18:09:35 +020063#include <types/applet.h>
64#include <types/cli.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020065#include <types/global.h>
66#include <types/ssl_sock.h>
William Lallemand32af2032016-10-29 18:09:35 +020067#include <types/stats.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020068
Willy Tarreau7875d092012-09-10 08:20:03 +020069#include <proto/acl.h>
70#include <proto/arg.h>
William Lallemand32af2032016-10-29 18:09:35 +020071#include <proto/channel.h>
Emeric Brun46591952012-05-18 15:47:34 +020072#include <proto/connection.h>
William Lallemand32af2032016-10-29 18:09:35 +020073#include <proto/cli.h>
Emeric Brun46591952012-05-18 15:47:34 +020074#include <proto/fd.h>
75#include <proto/freq_ctr.h>
76#include <proto/frontend.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020077#include <proto/http_rules.h>
Willy Tarreau79eeafa2012-09-14 07:53:05 +020078#include <proto/listener.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010079#include <proto/pattern.h>
Christopher Faulet31af49d2015-06-09 17:29:50 +020080#include <proto/proto_tcp.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020081#include <proto/http_ana.h>
Willy Tarreau92faadf2012-10-10 23:04:25 +020082#include <proto/server.h>
William Lallemand32af2032016-10-29 18:09:35 +020083#include <proto/stream_interface.h>
Emeric Brun46591952012-05-18 15:47:34 +020084#include <proto/log.h>
Emeric Brun94324a42012-10-11 14:00:19 +020085#include <proto/proxy.h>
Emeric Brunfc0421f2012-09-07 17:30:07 +020086#include <proto/shctx.h>
William Lallemandc69973f2020-05-12 17:42:42 +020087#include <proto/ssl_ckch.h>
Emeric Brun46591952012-05-18 15:47:34 +020088#include <proto/ssl_sock.h>
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020089#include <proto/stream.h>
Emeric Brun46591952012-05-18 15:47:34 +020090#include <proto/task.h>
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010091#include <proto/vars.h>
Emeric Brun46591952012-05-18 15:47:34 +020092
Willy Tarreau9356dac2019-05-10 09:22:53 +020093/* ***** READ THIS before adding code here! *****
94 *
95 * Due to API incompatibilities between multiple OpenSSL versions and their
96 * derivatives, it's often tempting to add macros to (re-)define certain
97 * symbols. Please do not do this here, and do it in common/openssl-compat.h
98 * exclusively so that the whole code consistently uses the same macros.
99 *
100 * Whenever possible if a macro is missing in certain versions, it's better
101 * to conditionally define it in openssl-compat.h than using lots of ifdefs.
102 */
103
Willy Tarreau71b734c2014-01-28 15:19:44 +0100104int sslconns = 0;
105int totalsslconns = 0;
Willy Tarreaud9f5cca2016-12-22 21:08:52 +0100106static struct xprt_ops ssl_sock;
Emeric Brunece0c332017-12-06 13:51:49 +0100107int nb_engines = 0;
Emeric Brune1f38db2012-09-03 20:36:47 +0200108
William Lallemande0f3fd52020-02-25 14:53:06 +0100109static struct eb_root cert_issuer_tree = EB_ROOT; /* issuers tree from "issuers-chain-path" */
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +0100110static struct issuer_chain* ssl_get0_issuer_chain(X509 *cert);
William Lallemande0f3fd52020-02-25 14:53:06 +0100111
William Lallemand7fd8b452020-05-07 15:20:43 +0200112struct global_ssl global_ssl = {
Willy Tarreauef934602016-12-22 23:12:01 +0100113#ifdef LISTEN_DEFAULT_CIPHERS
114 .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS,
115#endif
116#ifdef CONNECT_DEFAULT_CIPHERS
117 .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS,
118#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200119#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200120#ifdef LISTEN_DEFAULT_CIPHERSUITES
121 .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES,
122#endif
123#ifdef CONNECT_DEFAULT_CIPHERSUITES
124 .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES,
125#endif
126#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100127 .listen_default_ssloptions = BC_SSL_O_NONE,
128 .connect_default_ssloptions = SRV_SSL_O_NONE,
129
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200130 .listen_default_sslmethods.flags = MC_SSL_O_ALL,
131 .listen_default_sslmethods.min = CONF_TLSV_NONE,
132 .listen_default_sslmethods.max = CONF_TLSV_NONE,
133 .connect_default_sslmethods.flags = MC_SSL_O_ALL,
134 .connect_default_sslmethods.min = CONF_TLSV_NONE,
135 .connect_default_sslmethods.max = CONF_TLSV_NONE,
136
Willy Tarreauef934602016-12-22 23:12:01 +0100137#ifdef DEFAULT_SSL_MAX_RECORD
138 .max_record = DEFAULT_SSL_MAX_RECORD,
139#endif
140 .default_dh_param = SSL_DEFAULT_DH_PARAM,
141 .ctx_cache = DEFAULT_SSL_CTX_CACHE,
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100142 .capture_cipherlist = 0,
William Lallemand3af48e72020-02-03 17:15:52 +0100143 .extra_files = SSL_GF_ALL,
Willy Tarreauef934602016-12-22 23:12:01 +0100144};
145
Olivier Houcharda8955d52019-04-07 22:00:38 +0200146static BIO_METHOD *ha_meth;
147
Olivier Houchard66ab4982019-02-26 18:37:15 +0100148struct ssl_sock_ctx {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200149 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100150 SSL *ssl;
Olivier Houcharda8955d52019-04-07 22:00:38 +0200151 BIO *bio;
Olivier Houchard5149b592019-05-23 17:47:36 +0200152 const struct xprt_ops *xprt;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100153 void *xprt_ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +0200154 struct wait_event wait_event;
Willy Tarreau113d52b2020-01-10 09:20:26 +0100155 struct wait_event *subs;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100156 int xprt_st; /* transport layer state, initialized to zero */
Olivier Houchard54907bb2019-12-19 15:02:39 +0100157 struct buffer early_buf; /* buffer to store the early data received */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100158 int sent_early_data; /* Amount of early data we sent so far */
159
Olivier Houchard66ab4982019-02-26 18:37:15 +0100160};
161
162DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx));
163
Olivier Houchardea8dd942019-05-20 14:02:16 +0200164static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short);
Olivier Houchard000694c2019-05-23 14:45:12 +0200165static int ssl_sock_handshake(struct connection *conn, unsigned int flag);
Olivier Houchardea8dd942019-05-20 14:02:16 +0200166
Olivier Houcharda8955d52019-04-07 22:00:38 +0200167/* Methods to implement OpenSSL BIO */
168static int ha_ssl_write(BIO *h, const char *buf, int num)
169{
170 struct buffer tmpbuf;
171 struct ssl_sock_ctx *ctx;
172 int ret;
173
174 ctx = BIO_get_data(h);
175 tmpbuf.size = num;
176 tmpbuf.area = (void *)(uintptr_t)buf;
177 tmpbuf.data = num;
178 tmpbuf.head = 0;
179 ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200180 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200181 BIO_set_retry_write(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200182 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200183 } else if (ret == 0)
184 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200185 return ret;
186}
187
188static int ha_ssl_gets(BIO *h, char *buf, int size)
189{
190
191 return 0;
192}
193
194static int ha_ssl_puts(BIO *h, const char *str)
195{
196
197 return ha_ssl_write(h, str, strlen(str));
198}
199
200static int ha_ssl_read(BIO *h, char *buf, int size)
201{
202 struct buffer tmpbuf;
203 struct ssl_sock_ctx *ctx;
204 int ret;
205
206 ctx = BIO_get_data(h);
207 tmpbuf.size = size;
208 tmpbuf.area = buf;
209 tmpbuf.data = 0;
210 tmpbuf.head = 0;
211 ret = ctx->xprt->rcv_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, size, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200212 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200213 BIO_set_retry_read(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200214 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200215 } else if (ret == 0)
216 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200217
218 return ret;
219}
220
221static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2)
222{
223 int ret = 0;
224 switch (cmd) {
225 case BIO_CTRL_DUP:
226 case BIO_CTRL_FLUSH:
227 ret = 1;
228 break;
229 }
230 return ret;
231}
232
233static int ha_ssl_new(BIO *h)
234{
235 BIO_set_init(h, 1);
236 BIO_set_data(h, NULL);
237 BIO_clear_flags(h, ~0);
238 return 1;
239}
240
241static int ha_ssl_free(BIO *data)
242{
243
244 return 1;
245}
246
247
Willy Tarreau5db847a2019-05-09 14:13:35 +0200248#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100249
Emeric Brun821bb9b2017-06-15 16:37:39 +0200250static HA_RWLOCK_T *ssl_rwlocks;
251
252
253unsigned long ssl_id_function(void)
254{
255 return (unsigned long)tid;
256}
257
258void ssl_locking_function(int mode, int n, const char * file, int line)
259{
260 if (mode & CRYPTO_LOCK) {
261 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100262 HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200263 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100264 HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200265 }
266 else {
267 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100268 HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200269 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100270 HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200271 }
272}
273
274static int ssl_locking_init(void)
275{
276 int i;
277
278 ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks());
279 if (!ssl_rwlocks)
280 return -1;
281
282 for (i = 0 ; i < CRYPTO_num_locks() ; i++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100283 HA_RWLOCK_INIT(&ssl_rwlocks[i]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200284
285 CRYPTO_set_id_callback(ssl_id_function);
286 CRYPTO_set_locking_callback(ssl_locking_function);
287
288 return 0;
289}
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100290
Emeric Brun821bb9b2017-06-15 16:37:39 +0200291#endif
292
William Lallemand150bfa82019-09-19 17:12:49 +0200293__decl_hathreads(HA_SPINLOCK_T ckch_lock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200294
William Lallemandbc6ca7c2019-10-29 23:48:19 +0100295/* Uncommitted CKCH transaction */
296
297static struct {
298 struct ckch_store *new_ckchs;
299 struct ckch_store *old_ckchs;
300 char *path;
301} ckchs_transaction;
302
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200303/*
Emmanuel Hocdetb270e812019-11-21 19:09:31 +0100304 * deduplicate cafile (and crlfile)
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200305 */
306struct cafile_entry {
307 X509_STORE *ca_store;
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200308 STACK_OF(X509_NAME) *ca_list;
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200309 struct ebmb_node node;
310 char path[0];
311};
312
313static struct eb_root cafile_tree = EB_ROOT_UNIQUE;
314
315static X509_STORE* ssl_store_get0_locations_file(char *path)
316{
317 struct ebmb_node *eb;
318
319 eb = ebst_lookup(&cafile_tree, path);
320 if (eb) {
321 struct cafile_entry *ca_e;
322 ca_e = ebmb_entry(eb, struct cafile_entry, node);
323 return ca_e->ca_store;
324 }
325 return NULL;
326}
327
328static int ssl_store_load_locations_file(char *path)
329{
330 if (ssl_store_get0_locations_file(path) == NULL) {
331 struct cafile_entry *ca_e;
332 X509_STORE *store = X509_STORE_new();
333 if (X509_STORE_load_locations(store, path, NULL)) {
334 int pathlen;
335 pathlen = strlen(path);
336 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
337 if (ca_e) {
338 memcpy(ca_e->path, path, pathlen + 1);
339 ca_e->ca_store = store;
340 ebst_insert(&cafile_tree, &ca_e->node);
341 return 1;
342 }
343 }
344 X509_STORE_free(store);
345 return 0;
346 }
347 return 1;
348}
349
350/* mimic what X509_STORE_load_locations do with store_ctx */
351static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path)
352{
353 X509_STORE *store;
354 store = ssl_store_get0_locations_file(path);
355 if (store_ctx && store) {
356 int i;
357 X509_OBJECT *obj;
358 STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store);
359 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
360 obj = sk_X509_OBJECT_value(objs, i);
361 switch (X509_OBJECT_get_type(obj)) {
362 case X509_LU_X509:
363 X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj));
364 break;
365 case X509_LU_CRL:
366 X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj));
367 break;
368 default:
369 break;
370 }
371 }
372 return 1;
373 }
374 return 0;
375}
376
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +0500377/* SSL_CTX_load_verify_locations substitute, internally call X509_STORE_load_locations */
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200378static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path)
379{
380 X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx);
381 return ssl_set_cert_crl_file(store_ctx, path);
382}
383
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200384/*
385 Extract CA_list from CA_file already in tree.
386 Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility.
387 Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX.
388*/
389static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path)
390{
391 struct ebmb_node *eb;
392 struct cafile_entry *ca_e;
393
394 eb = ebst_lookup(&cafile_tree, path);
395 if (!eb)
396 return NULL;
397 ca_e = ebmb_entry(eb, struct cafile_entry, node);
398
399 if (ca_e->ca_list == NULL) {
400 int i;
401 unsigned long key;
402 struct eb_root ca_name_tree = EB_ROOT;
403 struct eb64_node *node, *back;
404 struct {
405 struct eb64_node node;
406 X509_NAME *xname;
407 } *ca_name;
408 STACK_OF(X509_OBJECT) *objs;
409 STACK_OF(X509_NAME) *skn;
410 X509 *x;
411 X509_NAME *xn;
412
413 skn = sk_X509_NAME_new_null();
414 /* take x509 from cafile_tree */
415 objs = X509_STORE_get0_objects(ca_e->ca_store);
416 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
417 x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
418 if (!x)
419 continue;
420 xn = X509_get_subject_name(x);
421 if (!xn)
422 continue;
423 /* Check for duplicates. */
424 key = X509_NAME_hash(xn);
425 for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL;
426 node && ca_name == NULL;
427 node = eb64_next(node)) {
428 ca_name = container_of(node, typeof(*ca_name), node);
429 if (X509_NAME_cmp(xn, ca_name->xname) != 0)
430 ca_name = NULL;
431 }
432 /* find a duplicate */
433 if (ca_name)
434 continue;
435 ca_name = calloc(1, sizeof *ca_name);
436 xn = X509_NAME_dup(xn);
437 if (!ca_name ||
438 !xn ||
439 !sk_X509_NAME_push(skn, xn)) {
440 free(ca_name);
441 X509_NAME_free(xn);
442 sk_X509_NAME_pop_free(skn, X509_NAME_free);
443 sk_X509_NAME_free(skn);
444 skn = NULL;
445 break;
446 }
447 ca_name->node.key = key;
448 ca_name->xname = xn;
449 eb64_insert(&ca_name_tree, &ca_name->node);
450 }
451 ca_e->ca_list = skn;
452 /* remove temporary ca_name tree */
453 node = eb64_first(&ca_name_tree);
454 while (node) {
455 ca_name = container_of(node, typeof(*ca_name), node);
456 back = eb64_next(node);
457 eb64_delete(node);
458 free(ca_name);
459 node = back;
460 }
461 }
462 return ca_e->ca_list;
463}
464
Willy Tarreaubafbe012017-11-24 17:34:44 +0100465struct pool_head *pool_head_ssl_capture = NULL;
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100466static int ssl_capture_ptr_index = -1;
Thierry FOURNIER28962c92018-06-17 21:37:05 +0200467static int ssl_app_data_index = -1;
Willy Tarreauef934602016-12-22 23:12:01 +0100468
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200469#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
470struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference);
471#endif
472
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200473#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000474static unsigned int openssl_engines_initialized;
475struct list openssl_engines = LIST_HEAD_INIT(openssl_engines);
476struct ssl_engine_list {
477 struct list list;
478 ENGINE *e;
479};
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200480#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000481
Remi Gacogne8de54152014-07-15 11:36:40 +0200482#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +0200483static int ssl_dh_ptr_index = -1;
Remi Gacogne47783ef2015-05-29 15:53:22 +0200484static DH *global_dh = NULL;
Remi Gacogne8de54152014-07-15 11:36:40 +0200485static DH *local_dh_1024 = NULL;
486static DH *local_dh_2048 = NULL;
487static DH *local_dh_4096 = NULL;
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +0100488static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen);
Remi Gacogne8de54152014-07-15 11:36:40 +0200489#endif /* OPENSSL_NO_DH */
490
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100491#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +0200492/* X509V3 Extensions that will be added on generated certificates */
493#define X509V3_EXT_SIZE 5
494static char *x509v3_ext_names[X509V3_EXT_SIZE] = {
495 "basicConstraints",
496 "nsComment",
497 "subjectKeyIdentifier",
498 "authorityKeyIdentifier",
499 "keyUsage",
500};
501static char *x509v3_ext_values[X509V3_EXT_SIZE] = {
502 "CA:FALSE",
503 "\"OpenSSL Generated Certificate\"",
504 "hash",
505 "keyid,issuer:always",
506 "nonRepudiation,digitalSignature,keyEncipherment"
507};
Christopher Faulet31af49d2015-06-09 17:29:50 +0200508/* LRU cache to store generated certificate */
509static struct lru64_head *ssl_ctx_lru_tree = NULL;
510static unsigned int ssl_ctx_lru_seed = 0;
Emeric Brun821bb9b2017-06-15 16:37:39 +0200511static unsigned int ssl_ctx_serial;
Willy Tarreau86abe442018-11-25 20:12:18 +0100512__decl_rwlock(ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200513
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200514#endif // SSL_CTRL_SET_TLSEXT_HOSTNAME
515
Willy Tarreau9a1ab082019-05-09 13:26:41 +0200516#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhube2774d2015-12-10 15:07:30 -0500517/* The order here matters for picking a default context,
518 * keep the most common keytype at the bottom of the list
519 */
520const char *SSL_SOCK_KEYTYPE_NAMES[] = {
521 "dsa",
522 "ecdsa",
523 "rsa"
524};
yanbzhube2774d2015-12-10 15:07:30 -0500525#endif
526
William Lallemandc3cd35f2017-11-28 11:04:43 +0100527static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */
William Lallemand4f45bb92017-10-30 20:08:51 +0100528static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */
529
Dragan Dosen9ac98092020-05-11 15:51:45 +0200530/* Dedicated callback functions for heartbeat and clienthello.
531 */
532#ifdef TLS1_RT_HEARTBEAT
533static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version,
534 int content_type, const void *buf, size_t len,
535 SSL *ssl);
536#endif
537static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version,
538 int content_type, const void *buf, size_t len,
539 SSL *ssl);
540
Dragan Dosen1e7ed042020-05-08 18:30:00 +0200541/* List head of all registered SSL/TLS protocol message callbacks. */
542struct list ssl_sock_msg_callbacks = LIST_HEAD_INIT(ssl_sock_msg_callbacks);
543
544/* Registers the function <func> in order to be called on SSL/TLS protocol
545 * message processing. It will return 0 if the function <func> is not set
546 * or if it fails to allocate memory.
547 */
548int ssl_sock_register_msg_callback(ssl_sock_msg_callback_func func)
549{
550 struct ssl_sock_msg_callback *cbk;
551
552 if (!func)
553 return 0;
554
555 cbk = calloc(1, sizeof(*cbk));
556 if (!cbk) {
557 ha_alert("out of memory in ssl_sock_register_msg_callback().\n");
558 return 0;
559 }
560
561 cbk->func = func;
562
563 LIST_ADDQ(&ssl_sock_msg_callbacks, &cbk->list);
564
565 return 1;
566}
567
Dragan Dosen9ac98092020-05-11 15:51:45 +0200568/* Used to register dedicated SSL/TLS protocol message callbacks.
569 */
570static int ssl_sock_register_msg_callbacks(void)
571{
572#ifdef TLS1_RT_HEARTBEAT
573 if (!ssl_sock_register_msg_callback(ssl_sock_parse_heartbeat))
574 return ERR_ABORT;
575#endif
576 if (global_ssl.capture_cipherlist > 0) {
577 if (!ssl_sock_register_msg_callback(ssl_sock_parse_clienthello))
578 return ERR_ABORT;
579 }
580 return 0;
581}
582
Dragan Dosen1e7ed042020-05-08 18:30:00 +0200583/* Used to free all SSL/TLS protocol message callbacks that were
584 * registered by using ssl_sock_register_msg_callback().
585 */
586static void ssl_sock_unregister_msg_callbacks(void)
587{
588 struct ssl_sock_msg_callback *cbk, *cbkback;
589
590 list_for_each_entry_safe(cbk, cbkback, &ssl_sock_msg_callbacks, list) {
591 LIST_DEL(&cbk->list);
592 free(cbk);
593 }
594}
595
Dragan Doseneb607fe2020-05-11 17:17:06 +0200596SSL *ssl_sock_get_ssl_object(struct connection *conn)
597{
598 if (!ssl_sock_is_ssl(conn))
599 return NULL;
600
601 return ((struct ssl_sock_ctx *)(conn->xprt_ctx))->ssl;
602}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100603/*
604 * This function gives the detail of the SSL error. It is used only
605 * if the debug mode and the verbose mode are activated. It dump all
606 * the SSL error until the stack was empty.
607 */
608static forceinline void ssl_sock_dump_errors(struct connection *conn)
609{
610 unsigned long ret;
611
612 if (unlikely(global.mode & MODE_DEBUG)) {
613 while(1) {
614 ret = ERR_get_error();
615 if (ret == 0)
616 return;
617 fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n",
Willy Tarreau585744b2017-08-24 14:31:19 +0200618 (unsigned short)conn->handle.fd, ret,
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100619 ERR_func_error_string(ret), ERR_reason_error_string(ret));
620 }
621 }
622}
623
yanbzhube2774d2015-12-10 15:07:30 -0500624
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200625#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000626static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms)
627{
628 int err_code = ERR_ABORT;
629 ENGINE *engine;
630 struct ssl_engine_list *el;
631
632 /* grab the structural reference to the engine */
633 engine = ENGINE_by_id(engine_id);
634 if (engine == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100635 ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000636 goto fail_get;
637 }
638
639 if (!ENGINE_init(engine)) {
640 /* the engine couldn't initialise, release it */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100641 ha_alert("ssl-engine %s: failed to initialize\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000642 goto fail_init;
643 }
644
645 if (ENGINE_set_default_string(engine, def_algorithms) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100646 ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000647 goto fail_set_method;
648 }
649
650 el = calloc(1, sizeof(*el));
651 el->e = engine;
652 LIST_ADD(&openssl_engines, &el->list);
Emeric Brunece0c332017-12-06 13:51:49 +0100653 nb_engines++;
654 if (global_ssl.async)
655 global.ssl_used_async_engines = nb_engines;
Grant Zhang872f9c22017-01-21 01:10:18 +0000656 return 0;
657
658fail_set_method:
659 /* release the functional reference from ENGINE_init() */
660 ENGINE_finish(engine);
661
662fail_init:
663 /* release the structural reference from ENGINE_by_id() */
664 ENGINE_free(engine);
665
666fail_get:
667 return err_code;
668}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200669#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000670
Willy Tarreau5db847a2019-05-09 14:13:35 +0200671#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +0200672/*
673 * openssl async fd handler
674 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200675void ssl_async_fd_handler(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000676{
Olivier Houchardea8dd942019-05-20 14:02:16 +0200677 struct ssl_sock_ctx *ctx = fdtab[fd].owner;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000678
Emeric Brun3854e012017-05-17 20:42:48 +0200679 /* fd is an async enfine fd, we must stop
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000680 * to poll this fd until it is requested
681 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000682 fd_stop_recv(fd);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000683 fd_cant_recv(fd);
684
685 /* crypto engine is available, let's notify the associated
686 * connection that it can pursue its processing.
687 */
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200688 ssl_sock_io_cb(NULL, ctx, 0);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000689}
690
Emeric Brun3854e012017-05-17 20:42:48 +0200691/*
692 * openssl async delayed SSL_free handler
693 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200694void ssl_async_fd_free(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000695{
696 SSL *ssl = fdtab[fd].owner;
Emeric Brun3854e012017-05-17 20:42:48 +0200697 OSSL_ASYNC_FD all_fd[32];
698 size_t num_all_fds = 0;
699 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000700
Emeric Brun3854e012017-05-17 20:42:48 +0200701 /* We suppose that the async job for a same SSL *
702 * are serialized. So if we are awake it is
703 * because the running job has just finished
704 * and we can remove all async fds safely
705 */
706 SSL_get_all_async_fds(ssl, NULL, &num_all_fds);
707 if (num_all_fds > 32) {
708 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
709 return;
710 }
711
712 SSL_get_all_async_fds(ssl, all_fd, &num_all_fds);
713 for (i=0 ; i < num_all_fds ; i++)
714 fd_remove(all_fd[i]);
715
716 /* Now we can safely call SSL_free, no more pending job in engines */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000717 SSL_free(ssl);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +0100718 _HA_ATOMIC_SUB(&sslconns, 1);
719 _HA_ATOMIC_SUB(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000720}
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000721/*
Emeric Brun3854e012017-05-17 20:42:48 +0200722 * function used to manage a returned SSL_ERROR_WANT_ASYNC
723 * and enable/disable polling for async fds
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000724 */
Olivier Houchardea8dd942019-05-20 14:02:16 +0200725static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000726{
Willy Tarreaua9786b62018-01-25 07:22:13 +0100727 OSSL_ASYNC_FD add_fd[32];
Emeric Brun3854e012017-05-17 20:42:48 +0200728 OSSL_ASYNC_FD del_fd[32];
Olivier Houchardea8dd942019-05-20 14:02:16 +0200729 SSL *ssl = ctx->ssl;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000730 size_t num_add_fds = 0;
731 size_t num_del_fds = 0;
Emeric Brun3854e012017-05-17 20:42:48 +0200732 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000733
734 SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL,
735 &num_del_fds);
Emeric Brun3854e012017-05-17 20:42:48 +0200736 if (num_add_fds > 32 || num_del_fds > 32) {
737 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 +0000738 return;
739 }
740
Emeric Brun3854e012017-05-17 20:42:48 +0200741 SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000742
Emeric Brun3854e012017-05-17 20:42:48 +0200743 /* We remove unused fds from the fdtab */
744 for (i=0 ; i < num_del_fds ; i++)
745 fd_remove(del_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000746
Emeric Brun3854e012017-05-17 20:42:48 +0200747 /* We add new fds to the fdtab */
748 for (i=0 ; i < num_add_fds ; i++) {
Olivier Houchardea8dd942019-05-20 14:02:16 +0200749 fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000750 }
751
Emeric Brun3854e012017-05-17 20:42:48 +0200752 num_add_fds = 0;
753 SSL_get_all_async_fds(ssl, NULL, &num_add_fds);
754 if (num_add_fds > 32) {
755 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
756 return;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000757 }
Emeric Brun3854e012017-05-17 20:42:48 +0200758
759 /* We activate the polling for all known async fds */
760 SSL_get_all_async_fds(ssl, add_fd, &num_add_fds);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000761 for (i=0 ; i < num_add_fds ; i++) {
Emeric Brun3854e012017-05-17 20:42:48 +0200762 fd_want_recv(add_fd[i]);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000763 /* To ensure that the fd cache won't be used
764 * We'll prefer to catch a real RD event
765 * because handling an EAGAIN on this fd will
766 * result in a context switch and also
767 * some engines uses a fd in blocking mode.
768 */
769 fd_cant_recv(add_fd[i]);
770 }
Emeric Brun3854e012017-05-17 20:42:48 +0200771
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000772}
773#endif
774
William Lallemand104a7a62019-10-14 14:14:59 +0200775#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200776/*
777 * This function returns the number of seconds elapsed
778 * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the
779 * date presented un ASN1_GENERALIZEDTIME.
780 *
781 * In parsing error case, it returns -1.
782 */
783static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d)
784{
785 long epoch;
786 char *p, *end;
787 const unsigned short month_offset[12] = {
788 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
789 };
790 int year, month;
791
792 if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1;
793
794 p = (char *)d->data;
795 end = p + d->length;
796
797 if (end - p < 4) return -1;
798 year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0';
799 p += 4;
800 if (end - p < 2) return -1;
801 month = 10 * (p[0] - '0') + p[1] - '0';
802 if (month < 1 || month > 12) return -1;
803 /* Compute the number of seconds since 1 jan 1970 and the beginning of current month
804 We consider leap years and the current month (<marsh or not) */
805 epoch = ( ((year - 1970) * 365)
806 + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400)
807 - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400)
808 + month_offset[month-1]
809 ) * 24 * 60 * 60;
810 p += 2;
811 if (end - p < 2) return -1;
812 /* Add the number of seconds of completed days of current month */
813 epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60;
814 p += 2;
815 if (end - p < 2) return -1;
816 /* Add the completed hours of the current day */
817 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60;
818 p += 2;
819 if (end - p < 2) return -1;
820 /* Add the completed minutes of the current hour */
821 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60;
822 p += 2;
823 if (p == end) return -1;
824 /* Test if there is available seconds */
825 if (p[0] < '0' || p[0] > '9')
826 goto nosec;
827 if (end - p < 2) return -1;
828 /* Add the seconds of the current minute */
829 epoch += 10 * (p[0] - '0') + p[1] - '0';
830 p += 2;
831 if (p == end) return -1;
832 /* Ignore seconds float part if present */
833 if (p[0] == '.') {
834 do {
835 if (++p == end) return -1;
836 } while (p[0] >= '0' && p[0] <= '9');
837 }
838
839nosec:
840 if (p[0] == 'Z') {
841 if (end - p != 1) return -1;
842 return epoch;
843 }
844 else if (p[0] == '+') {
845 if (end - p != 5) return -1;
846 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700847 return epoch - ((10 * (p[1] - '0') + p[2] - '0') * 60 * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60;
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200848 }
849 else if (p[0] == '-') {
850 if (end - p != 5) return -1;
851 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700852 return epoch + ((10 * (p[1] - '0') + p[2] - '0') * 60 * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60;
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200853 }
854
855 return -1;
856}
857
William Lallemand104a7a62019-10-14 14:14:59 +0200858/*
859 * struct alignment works here such that the key.key is the same as key_data
860 * Do not change the placement of key_data
861 */
862struct certificate_ocsp {
863 struct ebmb_node key;
864 unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH];
865 struct buffer response;
866 long expire;
867};
868
869struct ocsp_cbk_arg {
870 int is_single;
871 int single_kt;
872 union {
873 struct certificate_ocsp *s_ocsp;
874 /*
875 * m_ocsp will have multiple entries dependent on key type
876 * Entry 0 - DSA
877 * Entry 1 - ECDSA
878 * Entry 2 - RSA
879 */
880 struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES];
881 };
882};
883
Emeric Brun1d3865b2014-06-20 15:37:32 +0200884static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200885
886/* This function starts to check if the OCSP response (in DER format) contained
887 * in chunk 'ocsp_response' is valid (else exits on error).
888 * If 'cid' is not NULL, it will be compared to the OCSP certificate ID
889 * contained in the OCSP Response and exits on error if no match.
890 * If it's a valid OCSP Response:
891 * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container
892 * pointed by 'ocsp'.
893 * If 'ocsp' is NULL, the function looks up into the OCSP response's
894 * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted
895 * from the response) and exits on error if not found. Finally, If an OCSP response is
896 * already present in the container, it will be overwritten.
897 *
898 * Note: OCSP response containing more than one OCSP Single response is not
899 * considered valid.
900 *
901 * Returns 0 on success, 1 in error case.
902 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200903static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response,
904 struct certificate_ocsp *ocsp,
905 OCSP_CERTID *cid, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200906{
907 OCSP_RESPONSE *resp;
908 OCSP_BASICRESP *bs = NULL;
909 OCSP_SINGLERESP *sr;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200910 OCSP_CERTID *id;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200911 unsigned char *p = (unsigned char *) ocsp_response->area;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200912 int rc , count_sr;
Emeric Brun13a6b482014-06-20 15:44:34 +0200913 ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200914 int reason;
915 int ret = 1;
916
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200917 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
918 ocsp_response->data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200919 if (!resp) {
920 memprintf(err, "Unable to parse OCSP response");
921 goto out;
922 }
923
924 rc = OCSP_response_status(resp);
925 if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
926 memprintf(err, "OCSP response status not successful");
927 goto out;
928 }
929
930 bs = OCSP_response_get1_basic(resp);
931 if (!bs) {
932 memprintf(err, "Failed to get basic response from OCSP Response");
933 goto out;
934 }
935
936 count_sr = OCSP_resp_count(bs);
937 if (count_sr > 1) {
938 memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr);
939 goto out;
940 }
941
942 sr = OCSP_resp_get0(bs, 0);
943 if (!sr) {
944 memprintf(err, "Failed to get OCSP single response");
945 goto out;
946 }
947
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200948 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
949
Emeric Brun4147b2e2014-06-16 18:36:30 +0200950 rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd);
Emmanuel Hocdetef607052017-10-24 14:57:16 +0200951 if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) {
Emmanuel Hocdet872085c2017-10-10 15:18:52 +0200952 memprintf(err, "OCSP single response: certificate status is unknown");
Emeric Brun4147b2e2014-06-16 18:36:30 +0200953 goto out;
954 }
955
Emeric Brun13a6b482014-06-20 15:44:34 +0200956 if (!nextupd) {
957 memprintf(err, "OCSP single response: missing nextupdate");
958 goto out;
959 }
960
Emeric Brunc8b27b62014-06-19 14:16:17 +0200961 rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200962 if (!rc) {
963 memprintf(err, "OCSP single response: no longer valid.");
964 goto out;
965 }
966
967 if (cid) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200968 if (OCSP_id_cmp(id, cid)) {
Emeric Brun4147b2e2014-06-16 18:36:30 +0200969 memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer");
970 goto out;
971 }
972 }
973
974 if (!ocsp) {
975 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH];
976 unsigned char *p;
977
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200978 rc = i2d_OCSP_CERTID(id, NULL);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200979 if (!rc) {
980 memprintf(err, "OCSP single response: Unable to encode Certificate ID");
981 goto out;
982 }
983
984 if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) {
985 memprintf(err, "OCSP single response: Certificate ID too long");
986 goto out;
987 }
988
989 p = key;
990 memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200991 i2d_OCSP_CERTID(id, &p);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200992 ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH);
993 if (!ocsp) {
994 memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer");
995 goto out;
996 }
997 }
998
999 /* According to comments on "chunk_dup", the
1000 previous chunk buffer will be freed */
1001 if (!chunk_dup(&ocsp->response, ocsp_response)) {
1002 memprintf(err, "OCSP response: Memory allocation error");
1003 goto out;
1004 }
1005
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001006 ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW;
1007
Emeric Brun4147b2e2014-06-16 18:36:30 +02001008 ret = 0;
1009out:
Janusz Dziemidowicz8d710492017-03-08 16:59:41 +01001010 ERR_clear_error();
1011
Emeric Brun4147b2e2014-06-16 18:36:30 +02001012 if (bs)
1013 OCSP_BASICRESP_free(bs);
1014
1015 if (resp)
1016 OCSP_RESPONSE_free(resp);
1017
1018 return ret;
1019}
1020/*
1021 * External function use to update the OCSP response in the OCSP response's
1022 * containers tree. The chunk 'ocsp_response' must contain the OCSP response
1023 * to update in DER format.
1024 *
1025 * Returns 0 on success, 1 in error case.
1026 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001027int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001028{
1029 return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err);
1030}
1031
William Lallemand4a660132019-10-14 14:51:41 +02001032#endif
1033
1034#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001035/*
1036 * This function load the OCSP Resonse in DER format contained in file at
William Lallemand3b5f3602019-10-16 18:05:05 +02001037 * path 'ocsp_path' or base64 in a buffer <buf>
Emeric Brun4147b2e2014-06-16 18:36:30 +02001038 *
1039 * Returns 0 on success, 1 in error case.
1040 */
William Lallemand3b5f3602019-10-16 18:05:05 +02001041static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, char *buf, struct cert_key_and_chain *ckch, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001042{
1043 int fd = -1;
1044 int r = 0;
1045 int ret = 1;
William Lallemand3b5f3602019-10-16 18:05:05 +02001046 struct buffer *ocsp_response;
1047 struct buffer *src = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001048
William Lallemand3b5f3602019-10-16 18:05:05 +02001049 if (buf) {
1050 int i, j;
1051 /* if it's from a buffer it will be base64 */
Emeric Brun4147b2e2014-06-16 18:36:30 +02001052
William Lallemand3b5f3602019-10-16 18:05:05 +02001053 /* remove \r and \n from the payload */
1054 for (i = 0, j = 0; buf[i]; i++) {
1055 if (buf[i] == '\r' || buf[i] == '\n')
Emeric Brun4147b2e2014-06-16 18:36:30 +02001056 continue;
William Lallemand3b5f3602019-10-16 18:05:05 +02001057 buf[j++] = buf[i];
1058 }
1059 buf[j] = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001060
William Lallemand3b5f3602019-10-16 18:05:05 +02001061 ret = base64dec(buf, j, trash.area, trash.size);
1062 if (ret < 0) {
1063 memprintf(err, "Error reading OCSP response in base64 format");
Emeric Brun4147b2e2014-06-16 18:36:30 +02001064 goto end;
1065 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001066 trash.data = ret;
1067 src = &trash;
1068 } else {
1069 fd = open(ocsp_path, O_RDONLY);
1070 if (fd == -1) {
1071 memprintf(err, "Error opening OCSP response file");
1072 goto end;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001073 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001074
1075 trash.data = 0;
1076 while (trash.data < trash.size) {
1077 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1078 if (r < 0) {
1079 if (errno == EINTR)
1080 continue;
1081
1082 memprintf(err, "Error reading OCSP response from file");
1083 goto end;
1084 }
1085 else if (r == 0) {
1086 break;
1087 }
1088 trash.data += r;
1089 }
1090 close(fd);
1091 fd = -1;
1092 src = &trash;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001093 }
1094
William Lallemand3b5f3602019-10-16 18:05:05 +02001095 ocsp_response = calloc(1, sizeof(*ocsp_response));
1096 if (!chunk_dup(ocsp_response, src)) {
1097 free(ocsp_response);
1098 ocsp_response = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001099 goto end;
1100 }
Emmanuel Hocdet0667fae2020-01-16 14:41:36 +01001101 /* no error, fill ckch with new context, old context must be free */
1102 if (ckch->ocsp_response) {
1103 free(ckch->ocsp_response->area);
1104 ckch->ocsp_response->area = NULL;
1105 free(ckch->ocsp_response);
1106 }
William Lallemand3b5f3602019-10-16 18:05:05 +02001107 ckch->ocsp_response = ocsp_response;
William Lallemande0f48ae2019-10-15 13:44:57 +02001108 ret = 0;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001109end:
1110 if (fd != -1)
1111 close(fd);
1112
1113 return ret;
1114}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001115#endif
Emeric Brun4147b2e2014-06-16 18:36:30 +02001116
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001117#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1118static int ssl_tlsext_ticket_key_cb(SSL *s, unsigned char key_name[16], unsigned char *iv, EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc)
1119{
Christopher Faulet16f45c82018-02-16 11:23:49 +01001120 struct tls_keys_ref *ref;
Emeric Brun9e754772019-01-10 17:51:55 +01001121 union tls_sess_key *keys;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001122 struct connection *conn;
1123 int head;
1124 int i;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001125 int ret = -1; /* error by default */
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001126
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001127 conn = SSL_get_ex_data(s, ssl_app_data_index);
Willy Tarreau07d94e42018-09-20 10:57:52 +02001128 ref = __objt_listener(conn->target)->bind_conf->keys_ref;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001129 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1130
1131 keys = ref->tlskeys;
1132 head = ref->tls_ticket_enc_index;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001133
1134 if (enc) {
1135 memcpy(key_name, keys[head].name, 16);
1136
1137 if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH))
Christopher Faulet16f45c82018-02-16 11:23:49 +01001138 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001139
Emeric Brun9e754772019-01-10 17:51:55 +01001140 if (ref->key_size_bits == 128) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001141
Emeric Brun9e754772019-01-10 17:51:55 +01001142 if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv))
1143 goto end;
1144
Willy Tarreau9356dac2019-05-10 09:22:53 +02001145 HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001146 ret = 1;
1147 }
1148 else if (ref->key_size_bits == 256 ) {
1149
1150 if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv))
1151 goto end;
1152
Willy Tarreau9356dac2019-05-10 09:22:53 +02001153 HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001154 ret = 1;
1155 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001156 } else {
1157 for (i = 0; i < TLS_TICKETS_NO; i++) {
1158 if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16))
1159 goto found;
1160 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01001161 ret = 0;
1162 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001163
Christopher Faulet16f45c82018-02-16 11:23:49 +01001164 found:
Emeric Brun9e754772019-01-10 17:51:55 +01001165 if (ref->key_size_bits == 128) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001166 HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001167 if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv))
1168 goto end;
1169 /* 2 for key renewal, 1 if current key is still valid */
1170 ret = i ? 2 : 1;
1171 }
1172 else if (ref->key_size_bits == 256) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001173 HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001174 if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv))
1175 goto end;
1176 /* 2 for key renewal, 1 if current key is still valid */
1177 ret = i ? 2 : 1;
1178 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001179 }
Emeric Brun9e754772019-01-10 17:51:55 +01001180
Christopher Faulet16f45c82018-02-16 11:23:49 +01001181 end:
1182 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1183 return ret;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001184}
1185
1186struct tls_keys_ref *tlskeys_ref_lookup(const char *filename)
1187{
1188 struct tls_keys_ref *ref;
1189
1190 list_for_each_entry(ref, &tlskeys_reference, list)
1191 if (ref->filename && strcmp(filename, ref->filename) == 0)
1192 return ref;
1193 return NULL;
1194}
1195
1196struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id)
1197{
1198 struct tls_keys_ref *ref;
1199
1200 list_for_each_entry(ref, &tlskeys_reference, list)
1201 if (ref->unique_id == unique_id)
1202 return ref;
1203 return NULL;
1204}
1205
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001206/* Update the key into ref: if keysize doesn't
Emeric Brun9e754772019-01-10 17:51:55 +01001207 * match existing ones, this function returns -1
1208 * else it returns 0 on success.
1209 */
1210int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref,
Willy Tarreau83061a82018-07-13 11:56:34 +02001211 struct buffer *tlskey)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001212{
Emeric Brun9e754772019-01-10 17:51:55 +01001213 if (ref->key_size_bits == 128) {
1214 if (tlskey->data != sizeof(struct tls_sess_key_128))
1215 return -1;
1216 }
1217 else if (ref->key_size_bits == 256) {
1218 if (tlskey->data != sizeof(struct tls_sess_key_256))
1219 return -1;
1220 }
1221 else
1222 return -1;
1223
Christopher Faulet16f45c82018-02-16 11:23:49 +01001224 HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001225 memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)),
1226 tlskey->area, tlskey->data);
Christopher Faulet16f45c82018-02-16 11:23:49 +01001227 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
1228 HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Emeric Brun9e754772019-01-10 17:51:55 +01001229
1230 return 0;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001231}
1232
Willy Tarreau83061a82018-07-13 11:56:34 +02001233int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001234{
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001235 struct tls_keys_ref *ref = tlskeys_ref_lookup(filename);
1236
1237 if(!ref) {
1238 memprintf(err, "Unable to locate the referenced filename: %s", filename);
1239 return 1;
1240 }
Emeric Brun9e754772019-01-10 17:51:55 +01001241 if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) {
1242 memprintf(err, "Invalid key size");
1243 return 1;
1244 }
1245
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001246 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001247}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001248
1249/* This function finalize the configuration parsing. Its set all the
Willy Tarreaud1c57502016-12-22 22:46:15 +01001250 * automatic ids. It's called just after the basic checks. It returns
1251 * 0 on success otherwise ERR_*.
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001252 */
Willy Tarreaud1c57502016-12-22 22:46:15 +01001253static int tlskeys_finalize_config(void)
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001254{
1255 int i = 0;
1256 struct tls_keys_ref *ref, *ref2, *ref3;
1257 struct list tkr = LIST_HEAD_INIT(tkr);
1258
1259 list_for_each_entry(ref, &tlskeys_reference, list) {
1260 if (ref->unique_id == -1) {
1261 /* Look for the first free id. */
1262 while (1) {
1263 list_for_each_entry(ref2, &tlskeys_reference, list) {
1264 if (ref2->unique_id == i) {
1265 i++;
1266 break;
1267 }
1268 }
1269 if (&ref2->list == &tlskeys_reference)
1270 break;
1271 }
1272
1273 /* Uses the unique id and increment it for the next entry. */
1274 ref->unique_id = i;
1275 i++;
1276 }
1277 }
1278
1279 /* This sort the reference list by id. */
1280 list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) {
1281 LIST_DEL(&ref->list);
1282 list_for_each_entry(ref3, &tkr, list) {
1283 if (ref->unique_id < ref3->unique_id) {
1284 LIST_ADDQ(&ref3->list, &ref->list);
1285 break;
1286 }
1287 }
1288 if (&ref3->list == &tkr)
1289 LIST_ADDQ(&tkr, &ref->list);
1290 }
1291
1292 /* swap root */
1293 LIST_ADD(&tkr, &tlskeys_reference);
1294 LIST_DEL(&tkr);
Willy Tarreaud1c57502016-12-22 22:46:15 +01001295 return 0;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001296}
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001297#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
1298
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001299#ifndef OPENSSL_NO_OCSP
yanbzhube2774d2015-12-10 15:07:30 -05001300int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype)
1301{
1302 switch (evp_keytype) {
1303 case EVP_PKEY_RSA:
1304 return 2;
1305 case EVP_PKEY_DSA:
1306 return 0;
1307 case EVP_PKEY_EC:
1308 return 1;
1309 }
1310
1311 return -1;
1312}
1313
Emeric Brun4147b2e2014-06-16 18:36:30 +02001314/*
1315 * Callback used to set OCSP status extension content in server hello.
1316 */
1317int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg)
1318{
yanbzhube2774d2015-12-10 15:07:30 -05001319 struct certificate_ocsp *ocsp;
1320 struct ocsp_cbk_arg *ocsp_arg;
1321 char *ssl_buf;
1322 EVP_PKEY *ssl_pkey;
1323 int key_type;
1324 int index;
1325
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001326 ocsp_arg = arg;
yanbzhube2774d2015-12-10 15:07:30 -05001327
1328 ssl_pkey = SSL_get_privatekey(ssl);
1329 if (!ssl_pkey)
1330 return SSL_TLSEXT_ERR_NOACK;
1331
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001332 key_type = EVP_PKEY_base_id(ssl_pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001333
1334 if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type)
1335 ocsp = ocsp_arg->s_ocsp;
1336 else {
1337 /* For multiple certs per context, we have to find the correct OCSP response based on
1338 * the certificate type
1339 */
1340 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
1341
1342 if (index < 0)
1343 return SSL_TLSEXT_ERR_NOACK;
1344
1345 ocsp = ocsp_arg->m_ocsp[index];
1346
1347 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001348
1349 if (!ocsp ||
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001350 !ocsp->response.area ||
1351 !ocsp->response.data ||
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001352 (ocsp->expire < now.tv_sec))
Emeric Brun4147b2e2014-06-16 18:36:30 +02001353 return SSL_TLSEXT_ERR_NOACK;
1354
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001355 ssl_buf = OPENSSL_malloc(ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001356 if (!ssl_buf)
1357 return SSL_TLSEXT_ERR_NOACK;
1358
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001359 memcpy(ssl_buf, ocsp->response.area, ocsp->response.data);
1360 SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001361
1362 return SSL_TLSEXT_ERR_OK;
1363}
1364
William Lallemand4a660132019-10-14 14:51:41 +02001365#endif
1366
1367#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001368/*
1369 * This function enables the handling of OCSP status extension on 'ctx' if a
William Lallemand246c0242019-10-11 08:59:13 +02001370 * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP
1371 * status extension, the issuer's certificate is mandatory. It should be
1372 * present in ckch->ocsp_issuer.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001373 *
William Lallemand246c0242019-10-11 08:59:13 +02001374 * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an
1375 * OCSP response. If file is empty or content is not a valid OCSP response,
1376 * OCSP status extension is enabled but OCSP response is ignored (a warning is
1377 * displayed).
Emeric Brun4147b2e2014-06-16 18:36:30 +02001378 *
1379 * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is
Joseph Herlant017b3da2018-11-15 09:07:59 -08001380 * successfully enabled, or -1 in other error case.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001381 */
William Lallemand4a660132019-10-14 14:51:41 +02001382#ifndef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001383static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, STACK_OF(X509) *chain)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001384{
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001385 X509 *x, *issuer;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001386 OCSP_CERTID *cid = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001387 int i, ret = -1;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001388 struct certificate_ocsp *ocsp = NULL, *iocsp;
1389 char *warn = NULL;
1390 unsigned char *p;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001391 void (*callback) (void);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001392
Emeric Brun4147b2e2014-06-16 18:36:30 +02001393
William Lallemand246c0242019-10-11 08:59:13 +02001394 x = ckch->cert;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001395 if (!x)
1396 goto out;
1397
William Lallemand246c0242019-10-11 08:59:13 +02001398 issuer = ckch->ocsp_issuer;
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001399 /* take issuer from chain over ocsp_issuer, is what is done historicaly */
1400 if (chain) {
1401 /* check if one of the certificate of the chain is the issuer */
1402 for (i = 0; i < sk_X509_num(chain); i++) {
1403 X509 *ti = sk_X509_value(chain, i);
1404 if (X509_check_issued(ti, x) == X509_V_OK) {
1405 issuer = ti;
1406 break;
1407 }
1408 }
1409 }
William Lallemand246c0242019-10-11 08:59:13 +02001410 if (!issuer)
1411 goto out;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001412
1413 cid = OCSP_cert_to_id(0, x, issuer);
1414 if (!cid)
1415 goto out;
1416
1417 i = i2d_OCSP_CERTID(cid, NULL);
1418 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1419 goto out;
1420
Vincent Bernat02779b62016-04-03 13:48:43 +02001421 ocsp = calloc(1, sizeof(*ocsp));
Emeric Brun4147b2e2014-06-16 18:36:30 +02001422 if (!ocsp)
1423 goto out;
1424
1425 p = ocsp->key_data;
1426 i2d_OCSP_CERTID(cid, &p);
1427
1428 iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH);
1429 if (iocsp == ocsp)
1430 ocsp = NULL;
1431
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001432#ifndef SSL_CTX_get_tlsext_status_cb
1433# define SSL_CTX_get_tlsext_status_cb(ctx, cb) \
1434 *cb = (void (*) (void))ctx->tlsext_status_cb;
1435#endif
1436 SSL_CTX_get_tlsext_status_cb(ctx, &callback);
1437
1438 if (!callback) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001439 struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg));
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001440 EVP_PKEY *pkey;
yanbzhube2774d2015-12-10 15:07:30 -05001441
1442 cb_arg->is_single = 1;
1443 cb_arg->s_ocsp = iocsp;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001444
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001445 pkey = X509_get_pubkey(x);
1446 cb_arg->single_kt = EVP_PKEY_base_id(pkey);
1447 EVP_PKEY_free(pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001448
1449 SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk);
1450 SSL_CTX_set_tlsext_status_arg(ctx, cb_arg);
1451 } else {
1452 /*
1453 * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx
1454 * Update that cb_arg with the new cert's staple
1455 */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001456 struct ocsp_cbk_arg *cb_arg;
yanbzhube2774d2015-12-10 15:07:30 -05001457 struct certificate_ocsp *tmp_ocsp;
1458 int index;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001459 int key_type;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001460 EVP_PKEY *pkey;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001461
1462#ifdef SSL_CTX_get_tlsext_status_arg
1463 SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg);
1464#else
1465 cb_arg = ctx->tlsext_status_arg;
1466#endif
yanbzhube2774d2015-12-10 15:07:30 -05001467
1468 /*
1469 * The following few lines will convert cb_arg from a single ocsp to multi ocsp
1470 * the order of operations below matter, take care when changing it
1471 */
1472 tmp_ocsp = cb_arg->s_ocsp;
1473 index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt);
1474 cb_arg->s_ocsp = NULL;
1475 cb_arg->m_ocsp[index] = tmp_ocsp;
1476 cb_arg->is_single = 0;
1477 cb_arg->single_kt = 0;
1478
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001479 pkey = X509_get_pubkey(x);
1480 key_type = EVP_PKEY_base_id(pkey);
1481 EVP_PKEY_free(pkey);
1482
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001483 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
yanbzhube2774d2015-12-10 15:07:30 -05001484 if (index >= 0 && !cb_arg->m_ocsp[index])
1485 cb_arg->m_ocsp[index] = iocsp;
1486
1487 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001488
1489 ret = 0;
1490
1491 warn = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001492 if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) {
William Lallemand3b5f3602019-10-16 18:05:05 +02001493 memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure");
Christopher Faulet767a84b2017-11-24 16:50:31 +01001494 ha_warning("%s.\n", warn);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001495 }
1496
1497out:
Emeric Brun4147b2e2014-06-16 18:36:30 +02001498 if (cid)
1499 OCSP_CERTID_free(cid);
1500
1501 if (ocsp)
1502 free(ocsp);
1503
1504 if (warn)
1505 free(warn);
1506
Emeric Brun4147b2e2014-06-16 18:36:30 +02001507 return ret;
1508}
William Lallemand4a660132019-10-14 14:51:41 +02001509#else /* OPENSSL_IS_BORINGSSL */
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001510static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, STACK_OF(X509) *chain)
Emmanuel Hocdet2c32d8f2017-05-22 14:58:00 +02001511{
William Lallemand4a660132019-10-14 14:51:41 +02001512 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 +02001513}
1514#endif
1515
William Lallemand4a660132019-10-14 14:51:41 +02001516#endif
1517
1518
Willy Tarreau5db847a2019-05-09 14:13:35 +02001519#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001520
1521#define CT_EXTENSION_TYPE 18
1522
1523static int sctl_ex_index = -1;
1524
1525/*
1526 * Try to parse Signed Certificate Timestamp List structure. This function
1527 * makes only basic test if the data seems like SCTL. No signature validation
1528 * is performed.
1529 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001530static int ssl_sock_parse_sctl(struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001531{
1532 int ret = 1;
1533 int len, pos, sct_len;
1534 unsigned char *data;
1535
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001536 if (sctl->data < 2)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001537 goto out;
1538
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001539 data = (unsigned char *) sctl->area;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001540 len = (data[0] << 8) | data[1];
1541
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001542 if (len + 2 != sctl->data)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001543 goto out;
1544
1545 data = data + 2;
1546 pos = 0;
1547 while (pos < len) {
1548 if (len - pos < 2)
1549 goto out;
1550
1551 sct_len = (data[pos] << 8) | data[pos + 1];
1552 if (pos + sct_len + 2 > len)
1553 goto out;
1554
1555 pos += sct_len + 2;
1556 }
1557
1558 ret = 0;
1559
1560out:
1561 return ret;
1562}
1563
William Lallemand0dfae6c2019-10-16 18:06:58 +02001564/* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path>
1565 * It fills the ckch->sctl buffer
1566 * return 0 on success or != 0 on failure */
1567static int ssl_sock_load_sctl_from_file(const char *sctl_path, char *buf, struct cert_key_and_chain *ckch, char **err)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001568{
1569 int fd = -1;
1570 int r = 0;
1571 int ret = 1;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001572 struct buffer tmp;
1573 struct buffer *src;
1574 struct buffer *sctl;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001575
William Lallemand0dfae6c2019-10-16 18:06:58 +02001576 if (buf) {
1577 tmp.area = buf;
1578 tmp.data = strlen(buf);
1579 tmp.size = tmp.data + 1;
1580 src = &tmp;
1581 } else {
1582 fd = open(sctl_path, O_RDONLY);
1583 if (fd == -1)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001584 goto end;
William Lallemand0dfae6c2019-10-16 18:06:58 +02001585
1586 trash.data = 0;
1587 while (trash.data < trash.size) {
1588 r = read(fd, trash.area + trash.data, trash.size - trash.data);
1589 if (r < 0) {
1590 if (errno == EINTR)
1591 continue;
1592 goto end;
1593 }
1594 else if (r == 0) {
1595 break;
1596 }
1597 trash.data += r;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001598 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001599 src = &trash;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001600 }
1601
William Lallemand0dfae6c2019-10-16 18:06:58 +02001602 ret = ssl_sock_parse_sctl(src);
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001603 if (ret)
1604 goto end;
1605
William Lallemand0dfae6c2019-10-16 18:06:58 +02001606 sctl = calloc(1, sizeof(*sctl));
1607 if (!chunk_dup(sctl, src)) {
1608 free(sctl);
1609 sctl = NULL;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001610 goto end;
1611 }
Emmanuel Hocdet224a0872020-01-16 15:15:49 +01001612 /* no error, fill ckch with new context, old context must be free */
1613 if (ckch->sctl) {
1614 free(ckch->sctl->area);
1615 ckch->sctl->area = NULL;
1616 free(ckch->sctl);
1617 }
William Lallemand0dfae6c2019-10-16 18:06:58 +02001618 ckch->sctl = sctl;
Emmanuel Hocdet224a0872020-01-16 15:15:49 +01001619 ret = 0;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001620end:
1621 if (fd != -1)
1622 close(fd);
1623
1624 return ret;
1625}
1626
1627int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg)
1628{
Willy Tarreau83061a82018-07-13 11:56:34 +02001629 struct buffer *sctl = add_arg;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001630
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001631 *out = (unsigned char *) sctl->area;
1632 *outlen = sctl->data;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001633
1634 return 1;
1635}
1636
1637int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg)
1638{
1639 return 1;
1640}
1641
William Lallemanda17f4112019-10-10 15:16:44 +02001642static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001643{
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001644 int ret = -1;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001645
William Lallemanda17f4112019-10-10 15:16:44 +02001646 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 +01001647 goto out;
1648
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001649 SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl);
1650
1651 ret = 0;
1652
1653out:
1654 return ret;
1655}
1656
1657#endif
1658
Emeric Brune1f38db2012-09-03 20:36:47 +02001659void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
1660{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001661 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001662 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001663 BIO *write_bio;
Willy Tarreau622317d2015-02-27 16:36:16 +01001664 (void)ret; /* shut gcc stupid warning */
Emeric Brune1f38db2012-09-03 20:36:47 +02001665
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001666#ifndef SSL_OP_NO_RENEGOTIATION
1667 /* Please note that BoringSSL defines this macro to zero so don't
1668 * change this to #if and do not assign a default value to this macro!
1669 */
Emeric Brune1f38db2012-09-03 20:36:47 +02001670 if (where & SSL_CB_HANDSHAKE_START) {
1671 /* Disable renegotiation (CVE-2009-3555) */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001672 if ((conn->flags & (CO_FL_WAIT_L6_CONN | CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == 0) {
Emeric Brune1f38db2012-09-03 20:36:47 +02001673 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001674 conn->err_code = CO_ER_SSL_RENEG;
1675 }
Emeric Brune1f38db2012-09-03 20:36:47 +02001676 }
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001677#endif
Emeric Brund8b2bb52014-01-28 15:43:53 +01001678
1679 if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001680 if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) {
Emeric Brund8b2bb52014-01-28 15:43:53 +01001681 /* Long certificate chains optimz
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001682 If write and read bios are different, we
Emeric Brund8b2bb52014-01-28 15:43:53 +01001683 consider that the buffering was activated,
1684 so we rise the output buffer size from 4k
1685 to 16k */
1686 write_bio = SSL_get_wbio(ssl);
1687 if (write_bio != SSL_get_rbio(ssl)) {
1688 BIO_set_write_buffer_size(write_bio, 16384);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001689 ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001690 }
1691 }
1692 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001693}
1694
Emeric Brune64aef12012-09-21 13:15:06 +02001695/* Callback is called for each certificate of the chain during a verify
1696 ok is set to 1 if preverify detect no error on current certificate.
1697 Returns 0 to break the handshake, 1 otherwise. */
Evan Broderbe554312013-06-27 00:05:25 -07001698int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +02001699{
1700 SSL *ssl;
1701 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001702 struct ssl_sock_ctx *ctx;
Emeric Brun81c00f02012-09-21 14:31:21 +02001703 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +02001704
1705 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001706 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emeric Brune64aef12012-09-21 13:15:06 +02001707
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001708 ctx = conn->xprt_ctx;
1709
1710 ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +02001711
Emeric Brun81c00f02012-09-21 14:31:21 +02001712 if (ok) /* no errors */
1713 return ok;
1714
1715 depth = X509_STORE_CTX_get_error_depth(x_store);
1716 err = X509_STORE_CTX_get_error(x_store);
1717
1718 /* check if CA error needs to be ignored */
1719 if (depth > 0) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001720 if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) {
1721 ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
1722 ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +02001723 }
1724
Willy Tarreau731248f2020-02-04 14:02:02 +01001725 if (err < 64 && __objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001726 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001727 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001728 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001729 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001730
Willy Tarreau20879a02012-12-03 16:32:10 +01001731 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001732 return 0;
1733 }
1734
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001735 if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st))
1736 ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +02001737
Emeric Brun81c00f02012-09-21 14:31:21 +02001738 /* check if certificate error needs to be ignored */
Willy Tarreau731248f2020-02-04 14:02:02 +01001739 if (err < 64 && __objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001740 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001741 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001742 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001743 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001744
Willy Tarreau20879a02012-12-03 16:32:10 +01001745 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001746 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001747}
1748
Dragan Dosen9ac98092020-05-11 15:51:45 +02001749#ifdef TLS1_RT_HEARTBEAT
1750static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version,
1751 int content_type, const void *buf, size_t len,
1752 SSL *ssl)
1753{
1754 /* test heartbeat received (write_p is set to 0
1755 for a received record) */
1756 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
1757 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
1758 const unsigned char *p = buf;
1759 unsigned int payload;
1760
1761 ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
1762
1763 /* Check if this is a CVE-2014-0160 exploitation attempt. */
1764 if (*p != TLS1_HB_REQUEST)
1765 return;
1766
1767 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
1768 goto kill_it;
1769
1770 payload = (p[1] * 256) + p[2];
1771 if (3 + payload + 16 <= len)
1772 return; /* OK no problem */
1773 kill_it:
1774 /* We have a clear heartbleed attack (CVE-2014-0160), the
1775 * advertised payload is larger than the advertised packet
1776 * length, so we have garbage in the buffer between the
1777 * payload and the end of the buffer (p+len). We can't know
1778 * if the SSL stack is patched, and we don't know if we can
1779 * safely wipe out the area between p+3+len and payload.
1780 * So instead, we prevent the response from being sent by
1781 * setting the max_send_fragment to 0 and we report an SSL
1782 * error, which will kill this connection. It will be reported
1783 * above as SSL_ERROR_SSL while an other handshake failure with
1784 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
1785 */
1786 ssl->max_send_fragment = 0;
1787 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
1788 }
1789}
1790#endif
1791
1792static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version,
1793 int content_type, const void *buf, size_t len,
1794 SSL *ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001795{
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001796 struct ssl_capture *capture;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001797 unsigned char *msg;
1798 unsigned char *end;
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001799 size_t rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001800
1801 /* This function is called for "from client" and "to server"
1802 * connections. The combination of write_p == 0 and content_type == 22
Joseph Herlant017b3da2018-11-15 09:07:59 -08001803 * is only available during "from client" connection.
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001804 */
1805
1806 /* "write_p" is set to 0 is the bytes are received messages,
1807 * otherwise it is set to 1.
1808 */
1809 if (write_p != 0)
1810 return;
1811
1812 /* content_type contains the type of message received or sent
1813 * according with the SSL/TLS protocol spec. This message is
1814 * encoded with one byte. The value 256 (two bytes) is used
1815 * for designing the SSL/TLS record layer. According with the
1816 * rfc6101, the expected message (other than 256) are:
1817 * - change_cipher_spec(20)
1818 * - alert(21)
1819 * - handshake(22)
1820 * - application_data(23)
1821 * - (255)
1822 * We are interessed by the handshake and specially the client
1823 * hello.
1824 */
1825 if (content_type != 22)
1826 return;
1827
1828 /* The message length is at least 4 bytes, containing the
1829 * message type and the message length.
1830 */
1831 if (len < 4)
1832 return;
1833
1834 /* First byte of the handshake message id the type of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001835 * message. The known types are:
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001836 * - hello_request(0)
1837 * - client_hello(1)
1838 * - server_hello(2)
1839 * - certificate(11)
1840 * - server_key_exchange (12)
1841 * - certificate_request(13)
1842 * - server_hello_done(14)
1843 * We are interested by the client hello.
1844 */
1845 msg = (unsigned char *)buf;
1846 if (msg[0] != 1)
1847 return;
1848
1849 /* Next three bytes are the length of the message. The total length
1850 * must be this decoded length + 4. If the length given as argument
1851 * is not the same, we abort the protocol dissector.
1852 */
1853 rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3];
1854 if (len < rec_len + 4)
1855 return;
1856 msg += 4;
1857 end = msg + rec_len;
1858 if (end < msg)
1859 return;
1860
1861 /* Expect 2 bytes for protocol version (1 byte for major and 1 byte
1862 * for minor, the random, composed by 4 bytes for the unix time and
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001863 * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28.
1864 */
1865 msg += 1 + 1 + 4 + 28;
1866 if (msg > end)
1867 return;
1868
1869 /* Next, is session id:
1870 * if present, we have to jump by length + 1 for the size information
1871 * if not present, we have to jump by 1 only
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001872 */
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001873 if (msg[0] > 0)
1874 msg += msg[0];
1875 msg += 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001876 if (msg > end)
1877 return;
1878
1879 /* Next two bytes are the ciphersuite length. */
1880 if (msg + 2 > end)
1881 return;
1882 rec_len = (msg[0] << 8) + msg[1];
1883 msg += 2;
1884 if (msg + rec_len > end || msg + rec_len < msg)
1885 return;
1886
Willy Tarreaubafbe012017-11-24 17:34:44 +01001887 capture = pool_alloc_dirty(pool_head_ssl_capture);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001888 if (!capture)
1889 return;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001890 /* Compute the xxh64 of the ciphersuite. */
1891 capture->xxh64 = XXH64(msg, rec_len, 0);
1892
1893 /* Capture the ciphersuite. */
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001894 capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ?
1895 global_ssl.capture_cipherlist : rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001896 memcpy(capture->ciphersuite, msg, capture->ciphersuite_len);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001897
1898 SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001899}
1900
Emeric Brun29f037d2014-04-25 19:05:36 +02001901/* Callback is called for ssl protocol analyse */
1902void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
1903{
Dragan Dosen1e7ed042020-05-08 18:30:00 +02001904 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
1905 struct ssl_sock_msg_callback *cbk;
1906
Dragan Dosen1e7ed042020-05-08 18:30:00 +02001907 /* Try to call all callback functions that were registered by using
1908 * ssl_sock_register_msg_callback().
1909 */
1910 list_for_each_entry(cbk, &ssl_sock_msg_callbacks, list) {
1911 cbk->func(conn, write_p, version, content_type, buf, len, ssl);
1912 }
Emeric Brun29f037d2014-04-25 19:05:36 +02001913}
1914
Bernard Spil13c53f82018-02-15 13:34:58 +01001915#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchardc7566002018-11-20 23:33:50 +01001916static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen,
1917 const unsigned char *in, unsigned int inlen,
1918 void *arg)
1919{
1920 struct server *srv = arg;
1921
1922 if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str,
1923 srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED)
1924 return SSL_TLSEXT_ERR_OK;
1925 return SSL_TLSEXT_ERR_NOACK;
1926}
1927#endif
1928
1929#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001930/* This callback is used so that the server advertises the list of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001931 * negotiable protocols for NPN.
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001932 */
1933static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
1934 unsigned int *len, void *arg)
1935{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001936 struct ssl_bind_conf *conf = arg;
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001937
1938 *data = (const unsigned char *)conf->npn_str;
1939 *len = conf->npn_len;
1940 return SSL_TLSEXT_ERR_OK;
1941}
1942#endif
1943
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001944#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001945/* This callback is used so that the server advertises the list of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001946 * negotiable protocols for ALPN.
Willy Tarreauab861d32013-04-02 02:30:41 +02001947 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001948static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out,
1949 unsigned char *outlen,
1950 const unsigned char *server,
1951 unsigned int server_len, void *arg)
Willy Tarreauab861d32013-04-02 02:30:41 +02001952{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001953 struct ssl_bind_conf *conf = arg;
Willy Tarreauab861d32013-04-02 02:30:41 +02001954
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001955 if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str,
1956 conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) {
1957 return SSL_TLSEXT_ERR_NOACK;
1958 }
Willy Tarreauab861d32013-04-02 02:30:41 +02001959 return SSL_TLSEXT_ERR_OK;
1960}
1961#endif
1962
Willy Tarreauc8ad3be2015-06-17 15:48:26 +02001963#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001964#ifndef SSL_NO_GENERATE_CERTIFICATES
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001965
Christopher Faulet30548802015-06-11 13:39:32 +02001966/* Create a X509 certificate with the specified servername and serial. This
1967 * function returns a SSL_CTX object or NULL if an error occurs. */
Christopher Faulet7969a332015-10-09 11:15:03 +02001968static SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001969ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001970{
Christopher Faulet7969a332015-10-09 11:15:03 +02001971 X509 *cacert = bind_conf->ca_sign_cert;
1972 EVP_PKEY *capkey = bind_conf->ca_sign_pkey;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001973 SSL_CTX *ssl_ctx = NULL;
1974 X509 *newcrt = NULL;
1975 EVP_PKEY *pkey = NULL;
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001976 SSL *tmp_ssl = NULL;
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001977 CONF *ctmp = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001978 X509_NAME *name;
1979 const EVP_MD *digest;
1980 X509V3_CTX ctx;
1981 unsigned int i;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001982 int key_type;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001983
Christopher Faulet48a83322017-07-28 16:56:09 +02001984 /* Get the private key of the default certificate and use it */
Willy Tarreau5db847a2019-05-09 14:13:35 +02001985#if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L)
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001986 pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx);
1987#else
1988 tmp_ssl = SSL_new(bind_conf->default_ctx);
1989 if (tmp_ssl)
1990 pkey = SSL_get_privatekey(tmp_ssl);
1991#endif
1992 if (!pkey)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001993 goto mkcert_error;
1994
1995 /* Create the certificate */
1996 if (!(newcrt = X509_new()))
1997 goto mkcert_error;
1998
1999 /* Set version number for the certificate (X509v3) and the serial
2000 * number */
2001 if (X509_set_version(newcrt, 2L) != 1)
2002 goto mkcert_error;
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01002003 ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002004
2005 /* Set duration for the certificate */
Rosen Penev68185952018-12-14 08:47:02 -08002006 if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) ||
2007 !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365))
Christopher Faulet31af49d2015-06-09 17:29:50 +02002008 goto mkcert_error;
2009
2010 /* set public key in the certificate */
2011 if (X509_set_pubkey(newcrt, pkey) != 1)
2012 goto mkcert_error;
2013
2014 /* Set issuer name from the CA */
2015 if (!(name = X509_get_subject_name(cacert)))
2016 goto mkcert_error;
2017 if (X509_set_issuer_name(newcrt, name) != 1)
2018 goto mkcert_error;
2019
2020 /* Set the subject name using the same, but the CN */
2021 name = X509_NAME_dup(name);
2022 if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
2023 (const unsigned char *)servername,
2024 -1, -1, 0) != 1) {
2025 X509_NAME_free(name);
2026 goto mkcert_error;
2027 }
2028 if (X509_set_subject_name(newcrt, name) != 1) {
2029 X509_NAME_free(name);
2030 goto mkcert_error;
2031 }
2032 X509_NAME_free(name);
2033
2034 /* Add x509v3 extensions as specified */
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002035 ctmp = NCONF_new(NULL);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002036 X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0);
2037 for (i = 0; i < X509V3_EXT_SIZE; i++) {
2038 X509_EXTENSION *ext;
2039
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002040 if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i])))
Christopher Faulet31af49d2015-06-09 17:29:50 +02002041 goto mkcert_error;
2042 if (!X509_add_ext(newcrt, ext, -1)) {
2043 X509_EXTENSION_free(ext);
2044 goto mkcert_error;
2045 }
2046 X509_EXTENSION_free(ext);
2047 }
2048
2049 /* Sign the certificate with the CA private key */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002050
2051 key_type = EVP_PKEY_base_id(capkey);
2052
2053 if (key_type == EVP_PKEY_DSA)
2054 digest = EVP_sha1();
2055 else if (key_type == EVP_PKEY_RSA)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002056 digest = EVP_sha256();
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002057 else if (key_type == EVP_PKEY_EC)
Christopher Faulet7969a332015-10-09 11:15:03 +02002058 digest = EVP_sha256();
2059 else {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002060#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet7969a332015-10-09 11:15:03 +02002061 int nid;
2062
2063 if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0)
2064 goto mkcert_error;
2065 if (!(digest = EVP_get_digestbynid(nid)))
2066 goto mkcert_error;
Christopher Faulete7db2162015-10-19 13:59:24 +02002067#else
2068 goto mkcert_error;
2069#endif
Christopher Faulet7969a332015-10-09 11:15:03 +02002070 }
2071
Christopher Faulet31af49d2015-06-09 17:29:50 +02002072 if (!(X509_sign(newcrt, capkey, digest)))
2073 goto mkcert_error;
2074
2075 /* Create and set the new SSL_CTX */
2076 if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method())))
2077 goto mkcert_error;
2078 if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
2079 goto mkcert_error;
2080 if (!SSL_CTX_use_certificate(ssl_ctx, newcrt))
2081 goto mkcert_error;
2082 if (!SSL_CTX_check_private_key(ssl_ctx))
2083 goto mkcert_error;
2084
2085 if (newcrt) X509_free(newcrt);
Christopher Faulet7969a332015-10-09 11:15:03 +02002086
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002087#ifndef OPENSSL_NO_DH
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002088 SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh);
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01002089#endif
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002090#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
2091 {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002092 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02002093 EC_KEY *ecc;
2094 int nid;
2095
2096 if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef)
2097 goto end;
2098 if (!(ecc = EC_KEY_new_by_curve_name(nid)))
2099 goto end;
2100 SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc);
2101 EC_KEY_free(ecc);
2102 }
2103#endif
2104 end:
Christopher Faulet31af49d2015-06-09 17:29:50 +02002105 return ssl_ctx;
2106
2107 mkcert_error:
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02002108 if (ctmp) NCONF_free(ctmp);
Emmanuel Hocdet15969292017-08-11 10:56:00 +02002109 if (tmp_ssl) SSL_free(tmp_ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002110 if (ssl_ctx) SSL_CTX_free(ssl_ctx);
2111 if (newcrt) X509_free(newcrt);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002112 return NULL;
2113}
2114
Christopher Faulet7969a332015-10-09 11:15:03 +02002115SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002116ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key)
Christopher Faulet7969a332015-10-09 11:15:03 +02002117{
Willy Tarreau07d94e42018-09-20 10:57:52 +02002118 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
Olivier Houchard66ab4982019-02-26 18:37:15 +01002119 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002120
Olivier Houchard66ab4982019-02-26 18:37:15 +01002121 return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl);
Christopher Faulet7969a332015-10-09 11:15:03 +02002122}
2123
Christopher Faulet30548802015-06-11 13:39:32 +02002124/* Do a lookup for a certificate in the LRU cache used to store generated
Emeric Brun821bb9b2017-06-15 16:37:39 +02002125 * certificates and immediately assign it to the SSL session if not null. */
Christopher Faulet30548802015-06-11 13:39:32 +02002126SSL_CTX *
Emeric Brun821bb9b2017-06-15 16:37:39 +02002127ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet30548802015-06-11 13:39:32 +02002128{
2129 struct lru64 *lru = NULL;
2130
2131 if (ssl_ctx_lru_tree) {
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002132 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002133 lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002134 if (lru && lru->domain) {
2135 if (ssl)
2136 SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data);
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002137 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002138 return (SSL_CTX *)lru->data;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002139 }
Willy Tarreau03f4ec42018-05-17 10:56:47 +02002140 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02002141 }
2142 return NULL;
2143}
2144
Emeric Brun821bb9b2017-06-15 16:37:39 +02002145/* Same as <ssl_sock_assign_generated_cert> but without SSL session. This
2146 * function is not thread-safe, it should only be used to check if a certificate
2147 * exists in the lru cache (with no warranty it will not be removed by another
2148 * thread). It is kept for backward compatibility. */
2149SSL_CTX *
2150ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf)
2151{
2152 return ssl_sock_assign_generated_cert(key, bind_conf, NULL);
2153}
2154
Christopher Fauletd2cab922015-07-28 16:03:47 +02002155/* Set a certificate int the LRU cache used to store generated
2156 * certificate. Return 0 on success, otherwise -1 */
2157int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002158ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02002159{
2160 struct lru64 *lru = NULL;
2161
2162 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002163 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002164 lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02002165 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002166 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002167 return -1;
Emeric Brun821bb9b2017-06-15 16:37:39 +02002168 }
Christopher Faulet30548802015-06-11 13:39:32 +02002169 if (lru->domain && lru->data)
2170 lru->free((SSL_CTX *)lru->data);
Christopher Faulet7969a332015-10-09 11:15:03 +02002171 lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002172 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002173 return 0;
Christopher Faulet30548802015-06-11 13:39:32 +02002174 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02002175 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02002176}
2177
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002178/* Compute the key of the certificate. */
Christopher Faulet30548802015-06-11 13:39:32 +02002179unsigned int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002180ssl_sock_generated_cert_key(const void *data, size_t len)
Christopher Faulet30548802015-06-11 13:39:32 +02002181{
2182 return XXH32(data, len, ssl_ctx_lru_seed);
2183}
2184
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002185/* Generate a cert and immediately assign it to the SSL session so that the cert's
2186 * refcount is maintained regardless of the cert's presence in the LRU cache.
2187 */
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002188static int
Christopher Faulet7969a332015-10-09 11:15:03 +02002189ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002190{
2191 X509 *cacert = bind_conf->ca_sign_cert;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002192 SSL_CTX *ssl_ctx = NULL;
2193 struct lru64 *lru = NULL;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002194 unsigned int key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002195
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002196 key = ssl_sock_generated_cert_key(servername, strlen(servername));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002197 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002198 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002199 lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002200 if (lru && lru->domain)
2201 ssl_ctx = (SSL_CTX *)lru->data;
Christopher Fauletd2cab922015-07-28 16:03:47 +02002202 if (!ssl_ctx && lru) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002203 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002204 lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002205 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002206 SSL_set_SSL_CTX(ssl, ssl_ctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002207 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002208 return 1;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002209 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002210 else {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002211 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002212 SSL_set_SSL_CTX(ssl, ssl_ctx);
2213 /* No LRU cache, this CTX will be released as soon as the session dies */
2214 SSL_CTX_free(ssl_ctx);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002215 return 1;
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002216 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002217 return 0;
2218}
2219static int
2220ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl)
2221{
2222 unsigned int key;
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002223 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002224
Willy Tarreauf5bdb642019-07-17 11:29:32 +02002225 if (conn_get_dst(conn)) {
Willy Tarreau085a1512019-07-17 14:47:35 +02002226 key = ssl_sock_generated_cert_key(conn->dst, get_addr_len(conn->dst));
Emeric Brun821bb9b2017-06-15 16:37:39 +02002227 if (ssl_sock_assign_generated_cert(key, bind_conf, ssl))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002228 return 1;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002229 }
2230 return 0;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002231}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002232#endif /* !defined SSL_NO_GENERATE_CERTIFICATES */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002233
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002234#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002235
2236static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002237{
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02002238#if SSL_OP_NO_SSLv3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002239 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002240 : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method());
2241#endif
2242}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002243static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2244 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002245 : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method());
2246}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002247static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002248#if SSL_OP_NO_TLSv1_1
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002249 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002250 : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method());
2251#endif
2252}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002253static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002254#if SSL_OP_NO_TLSv1_2
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002255 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002256 : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method());
2257#endif
2258}
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002259/* TLSv1.2 is the last supported version in this context. */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002260static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {}
2261/* Unusable in this context. */
2262static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {}
2263static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {}
2264static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {}
2265static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {}
2266static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {}
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002267#else /* openssl >= 1.1.0 */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002268
2269static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) {
2270 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002271 : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION);
2272}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002273static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {
2274 c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION)
2275 : SSL_set_min_proto_version(ssl, SSL3_VERSION);
2276}
2277static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2278 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002279 : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2280}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002281static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {
2282 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION)
2283 : SSL_set_min_proto_version(ssl, TLS1_VERSION);
2284}
2285static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
2286 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002287 : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
2288}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002289static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {
2290 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION)
2291 : SSL_set_min_proto_version(ssl, TLS1_1_VERSION);
2292}
2293static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
2294 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002295 : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
2296}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002297static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {
2298 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION)
2299 : SSL_set_min_proto_version(ssl, TLS1_2_VERSION);
2300}
2301static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002302#if SSL_OP_NO_TLSv1_3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002303 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002304 : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
2305#endif
2306}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002307static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {
2308#if SSL_OP_NO_TLSv1_3
2309 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION)
2310 : SSL_set_min_proto_version(ssl, TLS1_3_VERSION);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002311#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002312}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002313#endif
2314static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { }
2315static void ssl_set_None_func(SSL *ssl, set_context_func c) { }
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002316
William Lallemand7fd8b452020-05-07 15:20:43 +02002317struct methodVersions methodVersions[] = {
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002318 {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */
2319 {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */
2320 {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */
2321 {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */
2322 {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */
2323 {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 +02002324};
2325
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002326static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx)
2327{
2328 SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk);
2329 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx)));
2330 SSL_set_SSL_CTX(ssl, ctx);
2331}
2332
Willy Tarreau5db847a2019-05-09 14:13:35 +02002333#if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL))
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002334
2335static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv)
2336{
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002337 struct bind_conf *s = priv;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002338 (void)al; /* shut gcc stupid warning */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002339
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002340 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs)
2341 return SSL_TLSEXT_ERR_OK;
2342 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002343}
2344
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002345#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002346static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx)
2347{
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002348 SSL *ssl = ctx->ssl;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002349#else
2350static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg)
2351{
2352#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002353 struct connection *conn;
2354 struct bind_conf *s;
2355 const uint8_t *extension_data;
2356 size_t extension_len;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002357 int has_rsa_sig = 0, has_ecdsa_sig = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002358
2359 char *wildp = NULL;
2360 const uint8_t *servername;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002361 size_t servername_len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002362 struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002363 int allow_early = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002364 int i;
2365
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002366 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Willy Tarreaua8825522018-10-15 13:20:07 +02002367 s = __objt_listener(conn->target)->bind_conf;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002368
Olivier Houchard9679ac92017-10-27 14:58:08 +02002369 if (s->ssl_conf.early_data)
Olivier Houchardc2aae742017-09-22 18:26:28 +02002370 allow_early = 1;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002371#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002372 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
2373 &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002374#else
2375 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) {
2376#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002377 /*
2378 * The server_name extension was given too much extensibility when it
2379 * was written, so parsing the normal case is a bit complex.
2380 */
2381 size_t len;
2382 if (extension_len <= 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002383 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002384 /* Extract the length of the supplied list of names. */
2385 len = (*extension_data++) << 8;
2386 len |= *extension_data++;
2387 if (len + 2 != extension_len)
2388 goto abort;
2389 /*
2390 * The list in practice only has a single element, so we only consider
2391 * the first one.
2392 */
2393 if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name)
2394 goto abort;
2395 extension_len = len - 1;
2396 /* Now we can finally pull out the byte array with the actual hostname. */
2397 if (extension_len <= 2)
2398 goto abort;
2399 len = (*extension_data++) << 8;
2400 len |= *extension_data++;
2401 if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name
2402 || memchr(extension_data, 0, len) != NULL)
2403 goto abort;
2404 servername = extension_data;
2405 servername_len = len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002406 } else {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002407#if (!defined SSL_NO_GENERATE_CERTIFICATES)
2408 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002409 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002410 }
2411#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002412 /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002413 if (!s->strict_sni) {
William Lallemand21724f02019-11-04 17:56:13 +01002414 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002415 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002416 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchardc2aae742017-09-22 18:26:28 +02002417 goto allow_early;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002418 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002419 goto abort;
2420 }
2421
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05002422 /* extract/check clientHello information */
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002423#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002424 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002425#else
2426 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
2427#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002428 uint8_t sign;
2429 size_t len;
2430 if (extension_len < 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002431 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002432 len = (*extension_data++) << 8;
2433 len |= *extension_data++;
2434 if (len + 2 != extension_len)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002435 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002436 if (len % 2 != 0)
2437 goto abort;
2438 for (; len > 0; len -= 2) {
2439 extension_data++; /* hash */
2440 sign = *extension_data++;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002441 switch (sign) {
2442 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002443 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002444 break;
2445 case TLSEXT_signature_ecdsa:
2446 has_ecdsa_sig = 1;
2447 break;
2448 default:
2449 continue;
2450 }
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002451 if (has_ecdsa_sig && has_rsa_sig)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002452 break;
2453 }
2454 } else {
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002455 /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002456 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002457 }
2458 if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002459 const SSL_CIPHER *cipher;
2460 size_t len;
2461 const uint8_t *cipher_suites;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002462 has_ecdsa_sig = 0;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002463#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002464 len = ctx->cipher_suites_len;
2465 cipher_suites = ctx->cipher_suites;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002466#else
2467 len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites);
2468#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002469 if (len % 2 != 0)
2470 goto abort;
2471 for (; len != 0; len -= 2, cipher_suites += 2) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002472#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002473 uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002474 cipher = SSL_get_cipher_by_value(cipher_suite);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002475#else
2476 cipher = SSL_CIPHER_find(ssl, cipher_suites);
2477#endif
Emmanuel Hocdet019f9b12017-10-02 17:12:06 +02002478 if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) {
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002479 has_ecdsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002480 break;
2481 }
2482 }
2483 }
2484
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002485 for (i = 0; i < trash.size && i < servername_len; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002486 trash.area[i] = tolower(servername[i]);
2487 if (!wildp && (trash.area[i] == '.'))
2488 wildp = &trash.area[i];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002489 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002490 trash.area[i] = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002491
William Lallemand150bfa82019-09-19 17:12:49 +02002492 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002493
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002494 for (i = 0; i < 2; i++) {
2495 if (i == 0) /* lookup in full qualified names */
2496 node = ebst_lookup(&s->sni_ctx, trash.area);
2497 else if (i == 1 && wildp) /* lookup in wildcards names */
2498 node = ebst_lookup(&s->sni_w_ctx, wildp);
2499 else
2500 break;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002501 for (n = node; n; n = ebmb_next_dup(n)) {
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002502 /* lookup a not neg filter */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002503 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002504 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002505 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002506 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002507 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002508 break;
2509 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002510 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002511 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002512 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002513 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002514 if (!node_anonymous)
2515 node_anonymous = n;
2516 break;
2517 }
2518 }
2519 }
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002520 /* select by key_signature priority order */
2521 node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa
2522 : ((has_rsa_sig && node_rsa) ? node_rsa
2523 : (node_anonymous ? node_anonymous
2524 : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */
2525 : node_rsa /* no rsa signature case (far far away) */
2526 )));
2527 if (node) {
2528 /* switch ctx */
2529 struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf;
2530 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002531 if (conf) {
2532 methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN);
2533 methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX);
2534 if (conf->early_data)
2535 allow_early = 1;
2536 }
William Lallemand02010472019-10-18 11:02:19 +02002537 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002538 goto allow_early;
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002539 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002540 }
William Lallemand150bfa82019-09-19 17:12:49 +02002541
William Lallemand02010472019-10-18 11:02:19 +02002542 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002543#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002544 if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002545 /* switch ctx done in ssl_sock_generate_certificate */
Olivier Houchardc2aae742017-09-22 18:26:28 +02002546 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002547 }
2548#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002549 if (!s->strict_sni) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002550 /* no certificate match, is the default_ctx */
William Lallemand21724f02019-11-04 17:56:13 +01002551 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002552 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002553 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002554 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02002555allow_early:
2556#ifdef OPENSSL_IS_BORINGSSL
2557 if (allow_early)
2558 SSL_set_early_data_enabled(ssl, 1);
2559#else
2560 if (!allow_early)
2561 SSL_set_max_early_data(ssl, 0);
2562#endif
2563 return 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002564 abort:
2565 /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */
2566 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002567#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002568 return ssl_select_cert_error;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002569#else
2570 *al = SSL_AD_UNRECOGNIZED_NAME;
2571 return 0;
2572#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002573}
2574
2575#else /* OPENSSL_IS_BORINGSSL */
2576
Emeric Brunfc0421f2012-09-07 17:30:07 +02002577/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
2578 * warning when no match is found, which implies the default (first) cert
2579 * will keep being used.
2580 */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002581static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv)
Emeric Brunfc0421f2012-09-07 17:30:07 +02002582{
2583 const char *servername;
2584 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002585 struct ebmb_node *node, *n;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002586 struct bind_conf *s = priv;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002587 int i;
2588 (void)al; /* shut gcc stupid warning */
2589
2590 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002591 if (!servername) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002592#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002593 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl))
2594 return SSL_TLSEXT_ERR_OK;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002595#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002596 if (s->strict_sni)
2597 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002598 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002599 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002600 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002601 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002602 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02002603
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002604 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02002605 if (!servername[i])
2606 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002607 trash.area[i] = tolower(servername[i]);
2608 if (!wildp && (trash.area[i] == '.'))
2609 wildp = &trash.area[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +02002610 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002611 trash.area[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002612
William Lallemand150bfa82019-09-19 17:12:49 +02002613 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002614 node = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002615 /* lookup in full qualified names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002616 for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) {
2617 /* lookup a not neg filter */
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002618 if (!container_of(n, struct sni_ctx, name)->neg) {
2619 node = n;
2620 break;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002621 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002622 }
2623 if (!node && wildp) {
2624 /* lookup in wildcards names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002625 for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) {
2626 /* lookup a not neg filter */
2627 if (!container_of(n, struct sni_ctx, name)->neg) {
2628 node = n;
2629 break;
2630 }
2631 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002632 }
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002633 if (!node) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002634#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002635 if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) {
2636 /* switch ctx done in ssl_sock_generate_certificate */
William Lallemand150bfa82019-09-19 17:12:49 +02002637 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002638 return SSL_TLSEXT_ERR_OK;
2639 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002640#endif
William Lallemand21724f02019-11-04 17:56:13 +01002641 if (s->strict_sni) {
2642 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002643 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002644 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002645 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002646 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002647 return SSL_TLSEXT_ERR_OK;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002648 }
2649
2650 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002651 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
William Lallemand150bfa82019-09-19 17:12:49 +02002652 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002653 return SSL_TLSEXT_ERR_OK;
2654}
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002655#endif /* (!) OPENSSL_IS_BORINGSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002656#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2657
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002658#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002659
2660static DH * ssl_get_dh_1024(void)
2661{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002662 static unsigned char dh1024_p[]={
2663 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7,
2664 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3,
2665 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9,
2666 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96,
2667 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D,
2668 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17,
2669 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B,
2670 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94,
2671 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC,
2672 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E,
2673 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B,
2674 };
2675 static unsigned char dh1024_g[]={
2676 0x02,
2677 };
2678
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002679 BIGNUM *p;
2680 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002681 DH *dh = DH_new();
2682 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002683 p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL);
2684 g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002685
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002686 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002687 DH_free(dh);
2688 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002689 } else {
2690 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002691 }
2692 }
2693 return dh;
2694}
2695
2696static DH *ssl_get_dh_2048(void)
2697{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002698 static unsigned char dh2048_p[]={
2699 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59,
2700 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24,
2701 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66,
2702 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10,
2703 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B,
2704 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23,
2705 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC,
2706 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E,
2707 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77,
2708 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A,
2709 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66,
2710 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74,
2711 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E,
2712 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40,
2713 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93,
2714 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43,
2715 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88,
2716 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1,
2717 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17,
2718 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB,
2719 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41,
2720 0xB7,0x1F,0x77,0xF3,
2721 };
2722 static unsigned char dh2048_g[]={
2723 0x02,
2724 };
2725
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002726 BIGNUM *p;
2727 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002728 DH *dh = DH_new();
2729 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002730 p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL);
2731 g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002732
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002733 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002734 DH_free(dh);
2735 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002736 } else {
2737 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002738 }
2739 }
2740 return dh;
2741}
2742
2743static DH *ssl_get_dh_4096(void)
2744{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002745 static unsigned char dh4096_p[]={
2746 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11,
2747 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68,
2748 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD,
2749 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B,
2750 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B,
2751 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47,
2752 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15,
2753 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35,
2754 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79,
2755 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3,
2756 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC,
2757 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52,
2758 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C,
2759 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A,
2760 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41,
2761 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55,
2762 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52,
2763 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66,
2764 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E,
2765 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47,
2766 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2,
2767 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73,
2768 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13,
2769 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10,
2770 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14,
2771 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD,
2772 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E,
2773 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48,
2774 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01,
2775 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60,
2776 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC,
2777 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41,
2778 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8,
2779 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B,
2780 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23,
2781 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE,
2782 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD,
2783 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03,
2784 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08,
2785 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5,
2786 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F,
2787 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67,
2788 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B,
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002789 };
Remi Gacogned3a341a2015-05-29 16:26:17 +02002790 static unsigned char dh4096_g[]={
2791 0x02,
2792 };
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002793
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002794 BIGNUM *p;
2795 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002796 DH *dh = DH_new();
2797 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002798 p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL);
2799 g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002800
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002801 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002802 DH_free(dh);
2803 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002804 } else {
2805 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002806 }
2807 }
2808 return dh;
2809}
2810
2811/* Returns Diffie-Hellman parameters matching the private key length
Willy Tarreauef934602016-12-22 23:12:01 +01002812 but not exceeding global_ssl.default_dh_param */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002813static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen)
2814{
2815 DH *dh = NULL;
2816 EVP_PKEY *pkey = SSL_get_privatekey(ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002817 int type;
2818
2819 type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002820
2821 /* The keylen supplied by OpenSSL can only be 512 or 1024.
2822 See ssl3_send_server_key_exchange() in ssl/s3_srvr.c
2823 */
2824 if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) {
2825 keylen = EVP_PKEY_bits(pkey);
2826 }
2827
Willy Tarreauef934602016-12-22 23:12:01 +01002828 if (keylen > global_ssl.default_dh_param) {
2829 keylen = global_ssl.default_dh_param;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002830 }
2831
Remi Gacogned3a341a2015-05-29 16:26:17 +02002832 if (keylen >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002833 dh = local_dh_4096;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002834 }
2835 else if (keylen >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002836 dh = local_dh_2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002837 }
2838 else {
Remi Gacogne8de54152014-07-15 11:36:40 +02002839 dh = local_dh_1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002840 }
2841
2842 return dh;
2843}
2844
Remi Gacogne47783ef2015-05-29 15:53:22 +02002845static DH * ssl_sock_get_dh_from_file(const char *filename)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002846{
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002847 DH *dh = NULL;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002848 BIO *in = BIO_new(BIO_s_file());
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002849
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002850 if (in == NULL)
2851 goto end;
2852
Remi Gacogne47783ef2015-05-29 15:53:22 +02002853 if (BIO_read_filename(in, filename) <= 0)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002854 goto end;
2855
Remi Gacogne47783ef2015-05-29 15:53:22 +02002856 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
2857
2858end:
2859 if (in)
2860 BIO_free(in);
2861
Emeric Brune1b4ed42018-08-16 15:14:12 +02002862 ERR_clear_error();
2863
Remi Gacogne47783ef2015-05-29 15:53:22 +02002864 return dh;
2865}
2866
2867int ssl_sock_load_global_dh_param_from_file(const char *filename)
2868{
2869 global_dh = ssl_sock_get_dh_from_file(filename);
2870
2871 if (global_dh) {
2872 return 0;
2873 }
2874
2875 return -1;
2876}
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002877#endif
2878
William Lallemand82b21bb2020-04-10 10:26:27 +02002879/* release ssl bind conf */
2880void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf)
2881{
2882 if (conf) {
2883#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
2884 free(conf->npn_str);
2885 conf->npn_str = NULL;
2886#endif
2887#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
2888 free(conf->alpn_str);
2889 conf->alpn_str = NULL;
2890#endif
2891 free(conf->ca_file);
2892 conf->ca_file = NULL;
2893 free(conf->ca_verify_file);
2894 conf->ca_verify_file = NULL;
2895 free(conf->crl_file);
2896 conf->crl_file = NULL;
2897 free(conf->ciphers);
2898 conf->ciphers = NULL;
2899#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
2900 free(conf->ciphersuites);
2901 conf->ciphersuites = NULL;
2902#endif
2903 free(conf->curves);
2904 conf->curves = NULL;
2905 free(conf->ecdhe);
2906 conf->ecdhe = NULL;
2907 }
2908}
2909
William Lallemandd9d5d1b2020-04-09 16:31:05 +02002910/* unlink a ckch_inst, free all SNIs, free the ckch_inst */
2911/* The caller must use the lock of the bind_conf if used with inserted SNIs */
William Lallemandc69973f2020-05-12 17:42:42 +02002912void ckch_inst_free(struct ckch_inst *inst)
William Lallemandd9d5d1b2020-04-09 16:31:05 +02002913{
2914 struct sni_ctx *sni, *sni_s;
2915
2916 if (inst == NULL)
2917 return;
2918
2919 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
2920 SSL_CTX_free(sni->ctx);
2921 LIST_DEL(&sni->by_ckch_inst);
2922 ebmb_delete(&sni->name);
2923 free(sni);
2924 }
2925 LIST_DEL(&inst->by_ckchs);
2926 LIST_DEL(&inst->by_crtlist_entry);
2927 free(inst);
2928}
2929
William Lallemand9117de92019-10-04 00:29:42 +02002930/* Alloc and init a ckch_inst */
William Lallemandc69973f2020-05-12 17:42:42 +02002931struct ckch_inst *ckch_inst_new()
William Lallemand9117de92019-10-04 00:29:42 +02002932{
2933 struct ckch_inst *ckch_inst;
2934
2935 ckch_inst = calloc(1, sizeof *ckch_inst);
William Lallemand9cef2e22020-04-09 16:25:10 +02002936 if (!ckch_inst)
2937 return NULL;
2938
2939 LIST_INIT(&ckch_inst->sni_ctx);
2940 LIST_INIT(&ckch_inst->by_ckchs);
2941 LIST_INIT(&ckch_inst->by_crtlist_entry);
William Lallemand9117de92019-10-04 00:29:42 +02002942
2943 return ckch_inst;
2944}
2945
William Lallemand82b21bb2020-04-10 10:26:27 +02002946/* free sni filters */
2947static void crtlist_free_filters(char **args)
2948{
2949 int i;
2950
2951 if (!args)
2952 return;
2953
2954 for (i = 0; args[i]; i++)
2955 free(args[i]);
2956
2957 free(args);
2958}
2959
2960/* Alloc and duplicate a char ** array */
2961static char **crtlist_dup_filters(char **args, int fcount)
2962{
2963 char **dst;
2964 int i;
2965
2966 if (fcount == 0)
2967 return NULL;
2968
2969 dst = calloc(fcount + 1, sizeof(*dst));
2970 if (!dst)
2971 return NULL;
2972
2973 for (i = 0; i < fcount; i++) {
2974 dst[i] = strdup(args[i]);
2975 if (!dst[i])
2976 goto error;
2977 }
2978 return dst;
2979
2980error:
2981 crtlist_free_filters(dst);
2982 return NULL;
2983}
2984
William Lallemande718dfb2020-04-10 11:09:25 +02002985/*
2986 * Detach and free a crtlist_entry.
2987 * Free the filters, the ssl_conf and call ckch_inst_free() for each ckch_inst
2988 */
2989static void crtlist_entry_free(struct crtlist_entry *entry)
2990{
2991 struct ckch_inst *inst, *inst_s;
2992
2993 if (entry == NULL)
2994 return;
2995
2996 ebpt_delete(&entry->node);
2997 LIST_DEL(&entry->by_crtlist);
2998 LIST_DEL(&entry->by_ckch_store);
2999 crtlist_free_filters(entry->filters);
3000 ssl_sock_free_ssl_conf(entry->ssl_conf);
3001 free(entry->ssl_conf);
3002 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) {
3003 ckch_inst_free(inst);
3004 }
3005 free(entry);
3006}
3007
3008/*
3009 * Allocate and initialize a crtlist_entry
3010 */
3011static struct crtlist_entry *crtlist_entry_new()
3012{
3013 struct crtlist_entry *entry;
3014
3015 entry = calloc(1, sizeof(*entry));
3016 if (entry == NULL)
3017 return NULL;
3018
3019 LIST_INIT(&entry->ckch_inst);
3020
3021 /* initialize the nodes so we can LIST_DEL in any cases */
3022 LIST_INIT(&entry->by_crtlist);
3023 LIST_INIT(&entry->by_ckch_store);
3024
3025 return entry;
3026}
3027
William Lallemand82b21bb2020-04-10 10:26:27 +02003028/* Free a crtlist, from the crt_entry to the content of the ssl_conf */
3029static void crtlist_free(struct crtlist *crtlist)
3030{
3031 struct crtlist_entry *entry, *s_entry;
3032
3033 if (crtlist == NULL)
3034 return;
3035
3036 list_for_each_entry_safe(entry, s_entry, &crtlist->ord_entries, by_crtlist) {
3037 crtlist_entry_free(entry);
3038 }
3039 ebmb_delete(&crtlist->node);
3040 free(crtlist);
3041}
3042
3043/* Alloc and initialize a struct crtlist
3044 * <filename> is the key of the ebmb_node
3045 * <unique> initialize the list of entries to be unique (1) or not (0)
3046 */
3047static struct crtlist *crtlist_new(const char *filename, int unique)
3048{
3049 struct crtlist *newlist;
3050
3051 newlist = calloc(1, sizeof(*newlist) + strlen(filename) + 1);
3052 if (newlist == NULL)
3053 return NULL;
3054
3055 memcpy(newlist->node.key, filename, strlen(filename) + 1);
3056 if (unique)
3057 newlist->entries = EB_ROOT_UNIQUE;
3058 else
3059 newlist->entries = EB_ROOT;
3060
3061 LIST_INIT(&newlist->ord_entries);
3062
3063 return newlist;
3064}
William Lallemand9117de92019-10-04 00:29:42 +02003065
3066/* This function allocates a sni_ctx and adds it to the ckch_inst */
William Lallemand1d29c742019-10-04 00:53:29 +02003067static int ckch_inst_add_cert_sni(SSL_CTX *ctx, struct ckch_inst *ckch_inst,
William Lallemand9117de92019-10-04 00:29:42 +02003068 struct bind_conf *s, struct ssl_bind_conf *conf,
3069 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003070{
3071 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003072 int wild = 0, neg = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003073
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003074 if (*name == '!') {
3075 neg = 1;
3076 name++;
3077 }
3078 if (*name == '*') {
3079 wild = 1;
3080 name++;
3081 }
3082 /* !* filter is a nop */
3083 if (neg && wild)
3084 return order;
3085 if (*name) {
3086 int j, len;
3087 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02003088 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003089 trash.area[j] = tolower(name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02003090 if (j >= trash.size)
William Lallemandfe49bb32019-10-03 23:46:33 +02003091 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003092 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02003093
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003094 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02003095 if (!sc)
William Lallemandfe49bb32019-10-03 23:46:33 +02003096 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003097 memcpy(sc->name.key, trash.area, len + 1);
William Lallemand02e19a52020-04-08 16:11:26 +02003098 SSL_CTX_up_ref(ctx);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003099 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003100 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003101 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02003102 sc->order = order++;
3103 sc->neg = neg;
William Lallemand1d29c742019-10-04 00:53:29 +02003104 sc->wild = wild;
3105 sc->name.node.leaf_p = NULL;
William Lallemandcfca1422020-03-05 10:17:47 +01003106 sc->ckch_inst = ckch_inst;
William Lallemand1d29c742019-10-04 00:53:29 +02003107 LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003108 }
3109 return order;
3110}
3111
William Lallemand6af03992019-07-23 15:00:54 +02003112/*
William Lallemand1d29c742019-10-04 00:53:29 +02003113 * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree
3114 * This function can't return an error.
3115 *
3116 * *CAUTION*: The caller must lock the sni tree if called in multithreading mode
3117 */
3118static void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf)
3119{
3120
3121 struct sni_ctx *sc0, *sc0b, *sc1;
3122 struct ebmb_node *node;
William Lallemand21724f02019-11-04 17:56:13 +01003123 int def = 0;
William Lallemand1d29c742019-10-04 00:53:29 +02003124
3125 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
3126
3127 /* ignore if sc0 was already inserted in a tree */
3128 if (sc0->name.node.leaf_p)
3129 continue;
3130
3131 /* Check for duplicates. */
3132 if (sc0->wild)
3133 node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key);
3134 else
3135 node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key);
3136
3137 for (; node; node = ebmb_next_dup(node)) {
3138 sc1 = ebmb_entry(node, struct sni_ctx, name);
3139 if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf
3140 && sc1->neg == sc0->neg && sc1->wild == sc0->wild) {
3141 /* it's a duplicate, we should remove and free it */
3142 LIST_DEL(&sc0->by_ckch_inst);
William Lallemand02e19a52020-04-08 16:11:26 +02003143 SSL_CTX_free(sc0->ctx);
William Lallemand1d29c742019-10-04 00:53:29 +02003144 free(sc0);
3145 sc0 = NULL;
William Lallemande15029b2019-10-14 10:46:58 +02003146 break;
William Lallemand1d29c742019-10-04 00:53:29 +02003147 }
3148 }
3149
3150 /* if duplicate, ignore the insertion */
3151 if (!sc0)
3152 continue;
3153
3154 if (sc0->wild)
3155 ebst_insert(&bind_conf->sni_w_ctx, &sc0->name);
3156 else
3157 ebst_insert(&bind_conf->sni_ctx, &sc0->name);
William Lallemand21724f02019-11-04 17:56:13 +01003158
3159 /* replace the default_ctx if required with the first ctx */
3160 if (ckch_inst->is_default && !def) {
William Lallemand02e19a52020-04-08 16:11:26 +02003161 SSL_CTX_free(bind_conf->default_ctx);
3162 SSL_CTX_up_ref(sc0->ctx);
William Lallemand21724f02019-11-04 17:56:13 +01003163 bind_conf->default_ctx = sc0->ctx;
3164 def = 1;
3165 }
William Lallemand1d29c742019-10-04 00:53:29 +02003166 }
3167}
3168
3169/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003170 * tree used to store the ckchs ordered by filename/bundle name
William Lallemand6af03992019-07-23 15:00:54 +02003171 */
William Lallemande3af8fb2019-10-08 11:36:53 +02003172struct eb_root ckchs_tree = EB_ROOT_UNIQUE;
William Lallemand6af03992019-07-23 15:00:54 +02003173
William Lallemand2954c472020-03-06 21:54:13 +01003174/* tree of crtlist (crt-list/directory) */
3175static struct eb_root crtlists_tree = EB_ROOT_UNIQUE;
William Lallemandfa892222019-07-23 16:06:08 +02003176
Emeric Brun7a883362019-10-17 13:27:40 +02003177/* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX.
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05003178 * If there is no DH parameter available in the ckchs, the global
Emeric Brun7a883362019-10-17 13:27:40 +02003179 * DH parameter is loaded into the SSL_CTX and if there is no
3180 * DH parameter available in ckchs nor in global, the default
3181 * DH parameters are applied on the SSL_CTX.
3182 * Returns a bitfield containing the flags:
3183 * ERR_FATAL in any fatal error case
3184 * ERR_ALERT if a reason of the error is availabine in err
3185 * ERR_WARN if a warning is available into err
3186 * The value 0 means there is no error nor warning and
3187 * the operation succeed.
3188 */
William Lallemandfa892222019-07-23 16:06:08 +02003189#ifndef OPENSSL_NO_DH
Emeric Brun7a883362019-10-17 13:27:40 +02003190static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch,
3191 const char *path, char **err)
William Lallemandfa892222019-07-23 16:06:08 +02003192{
Emeric Brun7a883362019-10-17 13:27:40 +02003193 int ret = 0;
William Lallemandfa892222019-07-23 16:06:08 +02003194 DH *dh = NULL;
3195
William Lallemanda8c73742019-07-31 18:31:34 +02003196 if (ckch && ckch->dh) {
William Lallemandfa892222019-07-23 16:06:08 +02003197 dh = ckch->dh;
Emeric Bruna9363eb2019-10-17 14:53:03 +02003198 if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
3199 memprintf(err, "%sunable to load the DH parameter specified in '%s'",
3200 err && *err ? *err : "", path);
3201#if defined(SSL_CTX_set_dh_auto)
3202 SSL_CTX_set_dh_auto(ctx, 1);
3203 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3204 err && *err ? *err : "");
3205#else
3206 memprintf(err, "%s, DH ciphers won't be available.\n",
3207 err && *err ? *err : "");
3208#endif
3209 ret |= ERR_WARN;
3210 goto end;
3211 }
William Lallemandfa892222019-07-23 16:06:08 +02003212
3213 if (ssl_dh_ptr_index >= 0) {
3214 /* store a pointer to the DH params to avoid complaining about
3215 ssl-default-dh-param not being set for this SSL_CTX */
3216 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
3217 }
3218 }
3219 else if (global_dh) {
Emeric Bruna9363eb2019-10-17 14:53:03 +02003220 if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) {
3221 memprintf(err, "%sunable to use the global DH parameter for certificate '%s'",
3222 err && *err ? *err : "", path);
3223#if defined(SSL_CTX_set_dh_auto)
3224 SSL_CTX_set_dh_auto(ctx, 1);
3225 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3226 err && *err ? *err : "");
3227#else
3228 memprintf(err, "%s, DH ciphers won't be available.\n",
3229 err && *err ? *err : "");
3230#endif
3231 ret |= ERR_WARN;
3232 goto end;
3233 }
William Lallemandfa892222019-07-23 16:06:08 +02003234 }
3235 else {
3236 /* Clear openssl global errors stack */
3237 ERR_clear_error();
3238
3239 if (global_ssl.default_dh_param <= 1024) {
3240 /* we are limited to DH parameter of 1024 bits anyway */
3241 if (local_dh_1024 == NULL)
3242 local_dh_1024 = ssl_get_dh_1024();
3243
Emeric Brun7a883362019-10-17 13:27:40 +02003244 if (local_dh_1024 == NULL) {
3245 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3246 err && *err ? *err : "", path);
3247 ret |= ERR_ALERT | ERR_FATAL;
William Lallemandfa892222019-07-23 16:06:08 +02003248 goto end;
Emeric Brun7a883362019-10-17 13:27:40 +02003249 }
William Lallemandfa892222019-07-23 16:06:08 +02003250
Emeric Bruna9363eb2019-10-17 14:53:03 +02003251 if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) {
3252 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
3253 err && *err ? *err : "", path);
3254#if defined(SSL_CTX_set_dh_auto)
3255 SSL_CTX_set_dh_auto(ctx, 1);
3256 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
3257 err && *err ? *err : "");
3258#else
3259 memprintf(err, "%s, DH ciphers won't be available.\n",
3260 err && *err ? *err : "");
3261#endif
3262 ret |= ERR_WARN;
3263 goto end;
3264 }
William Lallemandfa892222019-07-23 16:06:08 +02003265 }
3266 else {
3267 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
3268 }
William Lallemandfa892222019-07-23 16:06:08 +02003269 }
3270
3271end:
William Lallemand4dd145a2020-02-05 11:46:33 +01003272 ERR_clear_error();
William Lallemandfa892222019-07-23 16:06:08 +02003273 return ret;
3274}
3275#endif
yanbzhu08ce6ab2015-12-02 13:01:29 -05003276
yanbzhu488a4d22015-12-01 15:16:07 -05003277/* Frees the contents of a cert_key_and_chain
3278 */
3279static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch)
3280{
yanbzhu488a4d22015-12-01 15:16:07 -05003281 if (!ckch)
3282 return;
3283
3284 /* Free the certificate and set pointer to NULL */
3285 if (ckch->cert)
3286 X509_free(ckch->cert);
3287 ckch->cert = NULL;
3288
3289 /* Free the key and set pointer to NULL */
3290 if (ckch->key)
3291 EVP_PKEY_free(ckch->key);
3292 ckch->key = NULL;
3293
3294 /* Free each certificate in the chain */
Emmanuel Hocdet9246f8b2018-11-30 16:00:21 +01003295 if (ckch->chain)
3296 sk_X509_pop_free(ckch->chain, X509_free);
3297 ckch->chain = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003298
William Lallemand455af502019-10-17 18:04:45 +02003299 if (ckch->dh)
3300 DH_free(ckch->dh);
3301 ckch->dh = NULL;
3302
3303 if (ckch->sctl) {
3304 free(ckch->sctl->area);
3305 ckch->sctl->area = NULL;
3306 free(ckch->sctl);
3307 ckch->sctl = NULL;
3308 }
3309
3310 if (ckch->ocsp_response) {
3311 free(ckch->ocsp_response->area);
3312 ckch->ocsp_response->area = NULL;
3313 free(ckch->ocsp_response);
3314 ckch->ocsp_response = NULL;
3315 }
William Lallemand5c3c96f2020-01-23 11:53:13 +01003316
3317 if (ckch->ocsp_issuer)
William Lallemanddad239d2020-01-23 11:59:02 +01003318 X509_free(ckch->ocsp_issuer);
William Lallemand5c3c96f2020-01-23 11:53:13 +01003319 ckch->ocsp_issuer = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003320}
3321
William Lallemand8d0f8932019-10-17 18:03:58 +02003322/*
3323 *
3324 * This function copy a cert_key_and_chain in memory
3325 *
3326 * It's used to try to apply changes on a ckch before committing them, because
3327 * most of the time it's not possible to revert those changes
3328 *
3329 * Return a the dst or NULL
3330 */
3331static struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src,
3332 struct cert_key_and_chain *dst)
3333{
3334 if (src->cert) {
3335 dst->cert = src->cert;
3336 X509_up_ref(src->cert);
3337 }
3338
3339 if (src->key) {
3340 dst->key = src->key;
3341 EVP_PKEY_up_ref(src->key);
3342 }
3343
3344 if (src->chain) {
3345 dst->chain = X509_chain_up_ref(src->chain);
3346 }
3347
3348 if (src->dh) {
3349 DH_up_ref(src->dh);
3350 dst->dh = src->dh;
3351 }
3352
3353 if (src->sctl) {
3354 struct buffer *sctl;
3355
3356 sctl = calloc(1, sizeof(*sctl));
3357 if (!chunk_dup(sctl, src->sctl)) {
3358 free(sctl);
3359 sctl = NULL;
3360 goto error;
3361 }
3362 dst->sctl = sctl;
3363 }
3364
3365 if (src->ocsp_response) {
3366 struct buffer *ocsp_response;
3367
3368 ocsp_response = calloc(1, sizeof(*ocsp_response));
3369 if (!chunk_dup(ocsp_response, src->ocsp_response)) {
3370 free(ocsp_response);
3371 ocsp_response = NULL;
3372 goto error;
3373 }
3374 dst->ocsp_response = ocsp_response;
3375 }
3376
3377 if (src->ocsp_issuer) {
3378 X509_up_ref(src->ocsp_issuer);
3379 dst->ocsp_issuer = src->ocsp_issuer;
3380 }
3381
3382 return dst;
3383
3384error:
3385
3386 /* free everything */
3387 ssl_sock_free_cert_key_and_chain_contents(dst);
3388
3389 return NULL;
3390}
3391
3392
yanbzhu488a4d22015-12-01 15:16:07 -05003393/* checks if a key and cert exists in the ckch
3394 */
William Lallemand1633e392019-09-30 12:58:13 +02003395#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu488a4d22015-12-01 15:16:07 -05003396static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch)
3397{
3398 return (ckch->cert != NULL && ckch->key != NULL);
3399}
William Lallemand1633e392019-09-30 12:58:13 +02003400#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003401
William Lallemandf9568fc2019-10-16 18:27:58 +02003402/*
3403 * return 0 on success or != 0 on failure
3404 */
3405static int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err)
3406{
3407 int ret = 1;
3408 BIO *in = NULL;
3409 X509 *issuer;
3410
3411 if (buf) {
3412 /* reading from a buffer */
3413 in = BIO_new_mem_buf(buf, -1);
3414 if (in == NULL) {
3415 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3416 goto end;
3417 }
3418
3419 } else {
3420 /* reading from a file */
3421 in = BIO_new(BIO_s_file());
3422 if (in == NULL)
3423 goto end;
3424
3425 if (BIO_read_filename(in, path) <= 0)
3426 goto end;
3427 }
3428
3429 issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3430 if (!issuer) {
3431 memprintf(err, "%s'%s' cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003432 err && *err ? *err : "", path);
William Lallemandf9568fc2019-10-16 18:27:58 +02003433 goto end;
3434 }
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003435 /* no error, fill ckch with new context, old context must be free */
3436 if (ckch->ocsp_issuer)
3437 X509_free(ckch->ocsp_issuer);
William Lallemandf9568fc2019-10-16 18:27:58 +02003438 ckch->ocsp_issuer = issuer;
Emmanuel Hocdeteb73dc32020-01-16 14:45:00 +01003439 ret = 0;
William Lallemandf9568fc2019-10-16 18:27:58 +02003440
3441end:
3442
3443 ERR_clear_error();
3444 if (in)
3445 BIO_free(in);
3446
3447 return ret;
3448}
3449
William Lallemand96a9c972019-10-17 11:56:17 +02003450
3451/*
3452 * Try to load a PEM file from a <path> or a buffer <buf>
William Lallemand4c5adbf2020-02-24 14:23:22 +01003453 * The PEM must contain at least a Certificate,
3454 * It could contain a DH, a certificate chain and a PrivateKey.
yanbzhu488a4d22015-12-01 15:16:07 -05003455 *
William Lallemand96a9c972019-10-17 11:56:17 +02003456 * If it failed you should not attempt to use the ckch but free it.
3457 *
3458 * Return 0 on success or != 0 on failure
yanbzhu488a4d22015-12-01 15:16:07 -05003459 */
William Lallemand96a9c972019-10-17 11:56:17 +02003460static int ssl_sock_load_pem_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
yanbzhu488a4d22015-12-01 15:16:07 -05003461{
William Lallemandf11365b2019-09-19 14:25:58 +02003462 BIO *in = NULL;
yanbzhu488a4d22015-12-01 15:16:07 -05003463 int ret = 1;
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003464 X509 *ca;
William Lallemand96a9c972019-10-17 11:56:17 +02003465 X509 *cert = NULL;
3466 EVP_PKEY *key = NULL;
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003467 DH *dh = NULL;
3468 STACK_OF(X509) *chain = NULL;
William Lallemand96a9c972019-10-17 11:56:17 +02003469
3470 if (buf) {
3471 /* reading from a buffer */
3472 in = BIO_new_mem_buf(buf, -1);
3473 if (in == NULL) {
3474 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3475 goto end;
3476 }
yanbzhu488a4d22015-12-01 15:16:07 -05003477
William Lallemand96a9c972019-10-17 11:56:17 +02003478 } else {
3479 /* reading from a file */
William Lallemandf11365b2019-09-19 14:25:58 +02003480 in = BIO_new(BIO_s_file());
William Lallemand7fd01b32020-04-07 14:16:32 +02003481 if (in == NULL) {
3482 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
William Lallemandf11365b2019-09-19 14:25:58 +02003483 goto end;
William Lallemand7fd01b32020-04-07 14:16:32 +02003484 }
yanbzhu488a4d22015-12-01 15:16:07 -05003485
William Lallemand7fd01b32020-04-07 14:16:32 +02003486 if (BIO_read_filename(in, path) <= 0) {
3487 memprintf(err, "%scannot open the file '%s'.\n",
3488 err && *err ? *err : "", path);
William Lallemandf11365b2019-09-19 14:25:58 +02003489 goto end;
William Lallemand7fd01b32020-04-07 14:16:32 +02003490 }
William Lallemandf11365b2019-09-19 14:25:58 +02003491 }
yanbzhu488a4d22015-12-01 15:16:07 -05003492
yanbzhu488a4d22015-12-01 15:16:07 -05003493 /* Read Private Key */
William Lallemand96a9c972019-10-17 11:56:17 +02003494 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
William Lallemand4c5adbf2020-02-24 14:23:22 +01003495 /* no need to check for errors here, because the private key could be loaded later */
yanbzhu488a4d22015-12-01 15:16:07 -05003496
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003497#ifndef OPENSSL_NO_DH
William Lallemandfa892222019-07-23 16:06:08 +02003498 /* Seek back to beginning of file */
3499 if (BIO_reset(in) == -1) {
3500 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3501 err && *err ? *err : "", path);
3502 goto end;
3503 }
3504
William Lallemand96a9c972019-10-17 11:56:17 +02003505 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
3506 /* no need to return an error there, dh is not mandatory */
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02003507#endif
William Lallemandfa892222019-07-23 16:06:08 +02003508
Willy Tarreaubb137a82016-04-06 19:02:38 +02003509 /* Seek back to beginning of file */
Thierry FOURNIER / OZON.IOd44ea3f2016-10-14 00:49:21 +02003510 if (BIO_reset(in) == -1) {
3511 memprintf(err, "%san error occurred while reading the file '%s'.\n",
3512 err && *err ? *err : "", path);
3513 goto end;
3514 }
Willy Tarreaubb137a82016-04-06 19:02:38 +02003515
3516 /* Read Certificate */
William Lallemand96a9c972019-10-17 11:56:17 +02003517 cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL);
3518 if (cert == NULL) {
Willy Tarreaubb137a82016-04-06 19:02:38 +02003519 memprintf(err, "%sunable to load certificate from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003520 err && *err ? *err : "", path);
Willy Tarreaubb137a82016-04-06 19:02:38 +02003521 goto end;
3522 }
3523
William Lallemand96a9c972019-10-17 11:56:17 +02003524 /* Look for a Certificate Chain */
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003525 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
3526 if (chain == NULL)
3527 chain = sk_X509_new_null();
3528 if (!sk_X509_push(chain, ca)) {
William Lallemand96a9c972019-10-17 11:56:17 +02003529 X509_free(ca);
3530 goto end;
3531 }
3532 }
Emmanuel Hocdeted17f472019-10-24 18:28:33 +02003533
yanbzhu488a4d22015-12-01 15:16:07 -05003534 ret = ERR_get_error();
3535 if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) {
3536 memprintf(err, "%sunable to load certificate chain from file '%s'.\n",
William Lallemand96a9c972019-10-17 11:56:17 +02003537 err && *err ? *err : "", path);
yanbzhu488a4d22015-12-01 15:16:07 -05003538 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003539 }
3540
William Lallemand75b15f72020-01-23 10:56:05 +01003541 /* once it loaded the PEM, it should remove everything else in the ckch */
3542 if (ckch->ocsp_response) {
3543 free(ckch->ocsp_response->area);
3544 ckch->ocsp_response->area = NULL;
3545 free(ckch->ocsp_response);
3546 ckch->ocsp_response = NULL;
3547 }
3548
3549 if (ckch->sctl) {
3550 free(ckch->sctl->area);
3551 ckch->sctl->area = NULL;
3552 free(ckch->sctl);
3553 ckch->sctl = NULL;
3554 }
3555
3556 if (ckch->ocsp_issuer) {
3557 X509_free(ckch->ocsp_issuer);
3558 ckch->ocsp_issuer = NULL;
3559 }
3560
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003561 /* no error, fill ckch with new context, old context will be free at end: */
3562 SWAP(ckch->key, key);
3563 SWAP(ckch->dh, dh);
3564 SWAP(ckch->cert, cert);
3565 SWAP(ckch->chain, chain);
3566
William Lallemand246c0242019-10-11 08:59:13 +02003567 ret = 0;
3568
William Lallemand96a9c972019-10-17 11:56:17 +02003569end:
William Lallemand246c0242019-10-11 08:59:13 +02003570
3571 ERR_clear_error();
William Lallemand96a9c972019-10-17 11:56:17 +02003572 if (in)
William Lallemand246c0242019-10-11 08:59:13 +02003573 BIO_free(in);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003574 if (key)
3575 EVP_PKEY_free(key);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003576 if (dh)
3577 DH_free(dh);
Emmanuel Hocdet83cbd3c2019-10-25 11:55:03 +02003578 if (cert)
3579 X509_free(cert);
Emmanuel Hocdet6b5b44e2019-12-20 17:47:12 +01003580 if (chain)
3581 sk_X509_pop_free(chain, X509_free);
William Lallemanda17f4112019-10-10 15:16:44 +02003582
William Lallemand96a9c972019-10-17 11:56:17 +02003583 return ret;
3584}
3585
3586/*
William Lallemand4c5adbf2020-02-24 14:23:22 +01003587 * Try to load a private key file from a <path> or a buffer <buf>
3588 *
3589 * If it failed you should not attempt to use the ckch but free it.
3590 *
3591 * Return 0 on success or != 0 on failure
3592 */
3593static int ssl_sock_load_key_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err)
3594{
3595 BIO *in = NULL;
3596 int ret = 1;
3597 EVP_PKEY *key = NULL;
3598
3599 if (buf) {
3600 /* reading from a buffer */
3601 in = BIO_new_mem_buf(buf, -1);
3602 if (in == NULL) {
3603 memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : "");
3604 goto end;
3605 }
3606
3607 } else {
3608 /* reading from a file */
3609 in = BIO_new(BIO_s_file());
3610 if (in == NULL)
3611 goto end;
3612
3613 if (BIO_read_filename(in, path) <= 0)
3614 goto end;
3615 }
3616
3617 /* Read Private Key */
3618 key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
3619 if (key == NULL) {
3620 memprintf(err, "%sunable to load private key from file '%s'.\n",
3621 err && *err ? *err : "", path);
3622 goto end;
3623 }
3624
3625 ret = 0;
3626
3627 SWAP(ckch->key, key);
3628
3629end:
3630
3631 ERR_clear_error();
3632 if (in)
3633 BIO_free(in);
3634 if (key)
3635 EVP_PKEY_free(key);
3636
3637 return ret;
3638}
3639
3640/*
William Lallemand96a9c972019-10-17 11:56:17 +02003641 * Try to load in a ckch every files related to a ckch.
3642 * (PEM, sctl, ocsp, issuer etc.)
3643 *
3644 * This function is only used to load files during the configuration parsing,
3645 * it is not used with the CLI.
3646 *
3647 * This allows us to carry the contents of the file without having to read the
3648 * file multiple times. The caller must call
3649 * ssl_sock_free_cert_key_and_chain_contents.
3650 *
3651 * returns:
3652 * 0 on Success
3653 * 1 on SSL Failure
3654 */
3655static int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err)
3656{
3657 int ret = 1;
3658
3659 /* try to load the PEM */
3660 if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) {
3661 goto end;
3662 }
3663
William Lallemand4c5adbf2020-02-24 14:23:22 +01003664 /* try to load an external private key if it wasn't in the PEM */
3665 if ((ckch->key == NULL) && (global_ssl.extra_files & SSL_GF_KEY)) {
3666 char fp[MAXPATHLEN+1];
3667 struct stat st;
3668
3669 snprintf(fp, MAXPATHLEN+1, "%s.key", path);
3670 if (stat(fp, &st) == 0) {
3671 if (ssl_sock_load_key_into_ckch(fp, NULL, ckch, err)) {
3672 memprintf(err, "%s '%s' is present but cannot be read or parsed'.\n",
3673 err && *err ? *err : "", fp);
3674 goto end;
3675 }
3676 }
3677 }
3678
3679 if (ckch->key == NULL) {
3680 memprintf(err, "%sNo Private Key found in '%s' or '%s.key'.\n", err && *err ? *err : "", path, path);
3681 goto end;
3682 }
3683
3684 if (!X509_check_private_key(ckch->cert, ckch->key)) {
3685 memprintf(err, "%sinconsistencies between private key and certificate loaded '%s'.\n",
3686 err && *err ? *err : "", path);
3687 goto end;
3688 }
3689
William Lallemanda17f4112019-10-10 15:16:44 +02003690#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3691 /* try to load the sctl file */
William Lallemand3af48e72020-02-03 17:15:52 +01003692 if (global_ssl.extra_files & SSL_GF_SCTL) {
William Lallemanda17f4112019-10-10 15:16:44 +02003693 char fp[MAXPATHLEN+1];
3694 struct stat st;
3695
3696 snprintf(fp, MAXPATHLEN+1, "%s.sctl", path);
3697 if (stat(fp, &st) == 0) {
William Lallemand0dfae6c2019-10-16 18:06:58 +02003698 if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) {
William Lallemanda17f4112019-10-10 15:16:44 +02003699 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003700 err && *err ? *err : "", fp);
William Lallemanda17f4112019-10-10 15:16:44 +02003701 ret = 1;
3702 goto end;
3703 }
3704 }
3705 }
3706#endif
yanbzhu488a4d22015-12-01 15:16:07 -05003707
William Lallemand246c0242019-10-11 08:59:13 +02003708 /* try to load an ocsp response file */
William Lallemand3af48e72020-02-03 17:15:52 +01003709 if (global_ssl.extra_files & SSL_GF_OCSP) {
William Lallemand246c0242019-10-11 08:59:13 +02003710 char fp[MAXPATHLEN+1];
3711 struct stat st;
3712
3713 snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path);
3714 if (stat(fp, &st) == 0) {
William Lallemand3b5f3602019-10-16 18:05:05 +02003715 if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003716 ret = 1;
3717 goto end;
3718 }
3719 }
3720 }
3721
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003722#ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */
William Lallemand3af48e72020-02-03 17:15:52 +01003723 if (ckch->ocsp_response && (global_ssl.extra_files & SSL_GF_OCSP_ISSUER)) {
William Lallemand246c0242019-10-11 08:59:13 +02003724 /* if no issuer was found, try to load an issuer from the .issuer */
Emmanuel Hocdet078156d2020-01-22 17:02:53 +01003725 if (!ckch->ocsp_issuer) {
William Lallemand246c0242019-10-11 08:59:13 +02003726 struct stat st;
3727 char fp[MAXPATHLEN+1];
3728
3729 snprintf(fp, MAXPATHLEN+1, "%s.issuer", path);
3730 if (stat(fp, &st) == 0) {
William Lallemandf9568fc2019-10-16 18:27:58 +02003731 if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) {
William Lallemand246c0242019-10-11 08:59:13 +02003732 ret = 1;
3733 goto end;
3734 }
3735
3736 if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) {
William Lallemand786188f2019-10-15 10:05:37 +02003737 memprintf(err, "%s '%s' is not an issuer'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003738 err && *err ? *err : "", fp);
William Lallemand246c0242019-10-11 08:59:13 +02003739 ret = 1;
3740 goto end;
3741 }
William Lallemand246c0242019-10-11 08:59:13 +02003742 }
3743 }
3744 }
Emmanuel Hocdeteaad5cc2019-10-25 12:19:00 +02003745#endif
William Lallemand246c0242019-10-11 08:59:13 +02003746
yanbzhu488a4d22015-12-01 15:16:07 -05003747 ret = 0;
3748
3749end:
3750
3751 ERR_clear_error();
yanbzhu488a4d22015-12-01 15:16:07 -05003752
3753 /* Something went wrong in one of the reads */
3754 if (ret != 0)
3755 ssl_sock_free_cert_key_and_chain_contents(ckch);
3756
3757 return ret;
3758}
3759
3760/* Loads the info in ckch into ctx
Emeric Bruna96b5822019-10-17 13:25:14 +02003761 * Returns a bitfield containing the flags:
3762 * ERR_FATAL in any fatal error case
3763 * ERR_ALERT if the reason of the error is available in err
3764 * ERR_WARN if a warning is available into err
3765 * The value 0 means there is no error nor warning and
3766 * the operation succeed.
yanbzhu488a4d22015-12-01 15:16:07 -05003767 */
3768static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
3769{
Emeric Bruna96b5822019-10-17 13:25:14 +02003770 int errcode = 0;
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003771 STACK_OF(X509) *find_chain = NULL;
Emeric Bruna96b5822019-10-17 13:25:14 +02003772
yanbzhu488a4d22015-12-01 15:16:07 -05003773 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
3774 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
3775 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003776 errcode |= ERR_ALERT | ERR_FATAL;
3777 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003778 }
3779
3780 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
3781 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
3782 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003783 errcode |= ERR_ALERT | ERR_FATAL;
3784 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05003785 }
3786
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003787 if (ckch->chain) {
3788 find_chain = ckch->chain;
3789 } else {
3790 /* Find Certificate Chain in global */
3791 struct issuer_chain *issuer;
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +01003792 issuer = ssl_get0_issuer_chain(ckch->cert);
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01003793 if (issuer)
3794 find_chain = issuer->chain;
3795 }
William Lallemand85888572020-02-27 14:48:35 +01003796
Emmanuel Hocdet16739772020-02-28 16:00:34 +01003797 /* Load all certs from chain, except Root, in the ssl_ctx */
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003798 if (find_chain) {
3799 int i;
3800 X509 *ca;
3801 for (i = 0; i < sk_X509_num(find_chain); i++) {
3802 ca = sk_X509_value(find_chain, i);
Emmanuel Hocdet16739772020-02-28 16:00:34 +01003803 /* skip self issued (Root CA) */
Emmanuel Hocdetc3b7e742020-04-22 11:06:19 +02003804 if (global_ssl.skip_self_issued_ca && !X509_NAME_cmp(X509_get_subject_name(ca), X509_get_issuer_name(ca)))
Emmanuel Hocdet16739772020-02-28 16:00:34 +01003805 continue;
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003806 /*
3807 SSL_CTX_add1_chain_cert could be used with openssl >= 1.0.2
3808 Used SSL_CTX_add_extra_chain_cert for compat (aka SSL_CTX_add0_chain_cert)
3809 */
3810 X509_up_ref(ca);
3811 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
3812 X509_free(ca);
3813 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
3814 err && *err ? *err : "", path);
3815 errcode |= ERR_ALERT | ERR_FATAL;
3816 goto end;
3817 }
Emmanuel Hocdetf4f14ea2020-03-23 10:31:47 +01003818 }
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01003819 }
yanbzhu488a4d22015-12-01 15:16:07 -05003820
William Lallemandfa892222019-07-23 16:06:08 +02003821#ifndef OPENSSL_NO_DH
3822 /* store a NULL pointer to indicate we have not yet loaded
3823 a custom DH param file */
3824 if (ssl_dh_ptr_index >= 0) {
3825 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
3826 }
3827
Emeric Brun7a883362019-10-17 13:27:40 +02003828 errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err);
3829 if (errcode & ERR_CODE) {
William Lallemandfa892222019-07-23 16:06:08 +02003830 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
3831 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003832 goto end;
William Lallemandfa892222019-07-23 16:06:08 +02003833 }
3834#endif
3835
William Lallemanda17f4112019-10-10 15:16:44 +02003836#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
3837 if (sctl_ex_index >= 0 && ckch->sctl) {
3838 if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) {
3839 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01003840 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003841 errcode |= ERR_ALERT | ERR_FATAL;
3842 goto end;
William Lallemanda17f4112019-10-10 15:16:44 +02003843 }
3844 }
3845#endif
3846
William Lallemand4a660132019-10-14 14:51:41 +02003847#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand246c0242019-10-11 08:59:13 +02003848 /* Load OCSP Info into context */
3849 if (ckch->ocsp_response) {
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01003850 if (ssl_sock_load_ocsp(ctx, ckch, find_chain) < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01003851 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",
3852 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02003853 errcode |= ERR_ALERT | ERR_FATAL;
3854 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003855 }
3856 }
William Lallemand246c0242019-10-11 08:59:13 +02003857#endif
3858
Emeric Bruna96b5822019-10-17 13:25:14 +02003859 end:
3860 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003861}
3862
William Lallemandc4ecddf2019-07-31 16:50:08 +02003863#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu08ce6ab2015-12-02 13:01:29 -05003864
William Lallemand28a8fce2019-10-04 17:36:55 +02003865static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003866{
3867 struct sni_keytype *s_kt = NULL;
3868 struct ebmb_node *node;
3869 int i;
3870
3871 for (i = 0; i < trash.size; i++) {
3872 if (!str[i])
3873 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003874 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003875 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003876 trash.area[i] = 0;
3877 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003878 if (!node) {
3879 /* CN not found in tree */
3880 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3881 /* Using memcpy here instead of strncpy.
3882 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3883 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3884 */
William Lallemand28a8fce2019-10-04 17:36:55 +02003885 if (!s_kt)
3886 return -1;
3887
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003888 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003889 s_kt->keytypes = 0;
3890 ebst_insert(sni_keytypes, &s_kt->name);
3891 } else {
3892 /* CN found in tree */
3893 s_kt = container_of(node, struct sni_keytype, name);
3894 }
3895
3896 /* Mark that this CN has the keytype of key_index via keytypes mask */
3897 s_kt->keytypes |= 1<<key_index;
3898
William Lallemand28a8fce2019-10-04 17:36:55 +02003899 return 0;
3900
yanbzhu08ce6ab2015-12-02 13:01:29 -05003901}
3902
William Lallemandc4ecddf2019-07-31 16:50:08 +02003903#endif
William Lallemand8c1cdde2019-10-18 10:58:14 +02003904/*
William Lallemandba1c33f2020-04-08 17:55:45 +02003905 * Free a ckch_store, its ckch, its instances and remove it from the ebtree
William Lallemand8c1cdde2019-10-18 10:58:14 +02003906 */
William Lallemandba1c33f2020-04-08 17:55:45 +02003907static void ckch_store_free(struct ckch_store *store)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003908{
William Lallemandba1c33f2020-04-08 17:55:45 +02003909 struct ckch_inst *inst, *inst_s;
3910
3911 if (!store)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003912 return;
3913
William Lallemandba1c33f2020-04-08 17:55:45 +02003914#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200L
3915 if (store->multi) {
William Lallemand8c1cdde2019-10-18 10:58:14 +02003916 int n;
3917
William Lallemandba1c33f2020-04-08 17:55:45 +02003918 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++)
3919 ssl_sock_free_cert_key_and_chain_contents(&store->ckch[n]);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003920 } else
3921#endif
3922 {
William Lallemandba1c33f2020-04-08 17:55:45 +02003923 ssl_sock_free_cert_key_and_chain_contents(store->ckch);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003924 }
3925
William Lallemandba1c33f2020-04-08 17:55:45 +02003926 free(store->ckch);
3927 store->ckch = NULL;
3928
3929 list_for_each_entry_safe(inst, inst_s, &store->ckch_inst, by_ckchs) {
3930 ckch_inst_free(inst);
3931 }
3932 ebmb_delete(&store->node);
3933 free(store);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003934}
3935
William Lallemand8a874e42020-04-09 10:32:53 +02003936/*
3937 * create and initialize a ckch_store
3938 * <path> is the key name
3939 * <nmemb> is the number of store->ckch objects to allocate
3940 *
3941 * Return a ckch_store or NULL upon failure.
3942 */
3943static struct ckch_store *ckch_store_new(const char *filename, int nmemb)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003944{
William Lallemand8a874e42020-04-09 10:32:53 +02003945 struct ckch_store *store;
William Lallemand8c1cdde2019-10-18 10:58:14 +02003946 int pathlen;
3947
William Lallemand8a874e42020-04-09 10:32:53 +02003948 pathlen = strlen(filename);
3949 store = calloc(1, sizeof(*store) + pathlen + 1);
3950 if (!store)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003951 return NULL;
William Lallemand8a874e42020-04-09 10:32:53 +02003952
3953 if (nmemb > 1)
3954 store->multi = 1;
3955 else
3956 store->multi = 0;
3957
3958 memcpy(store->path, filename, pathlen + 1);
3959
3960 LIST_INIT(&store->ckch_inst);
3961 LIST_INIT(&store->crtlist_entry);
William Lallemand8c1cdde2019-10-18 10:58:14 +02003962
William Lallemand8a874e42020-04-09 10:32:53 +02003963 store->ckch = calloc(nmemb, sizeof(*store->ckch));
3964 if (!store->ckch)
William Lallemand8c1cdde2019-10-18 10:58:14 +02003965 goto error;
3966
William Lallemand8a874e42020-04-09 10:32:53 +02003967 return store;
3968error:
3969 ckch_store_free(store);
3970 return NULL;
3971}
3972
3973/* allocate and duplicate a ckch_store
3974 * Return a new ckch_store or NULL */
3975static struct ckch_store *ckchs_dup(const struct ckch_store *src)
3976{
3977 struct ckch_store *dst;
3978
3979 dst = ckch_store_new(src->path, src->multi ? SSL_SOCK_NUM_KEYTYPES : 1);
3980
William Lallemand8c1cdde2019-10-18 10:58:14 +02003981#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3982 if (src->multi) {
3983 int n;
3984
3985 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3986 if (&src->ckch[n]) {
3987 if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n]))
3988 goto error;
3989 }
3990 }
3991 } else
3992#endif
3993 {
3994 if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch))
3995 goto error;
3996 }
3997
3998 return dst;
3999
4000error:
William Lallemandba1c33f2020-04-08 17:55:45 +02004001 ckch_store_free(dst);
William Lallemand8c1cdde2019-10-18 10:58:14 +02004002
4003 return NULL;
4004}
William Lallemandc4ecddf2019-07-31 16:50:08 +02004005
William Lallemand36b84632019-07-18 19:28:17 +02004006/*
William Lallemande3af8fb2019-10-08 11:36:53 +02004007 * lookup a path into the ckchs tree.
William Lallemand6af03992019-07-23 15:00:54 +02004008 */
William Lallemandc69973f2020-05-12 17:42:42 +02004009struct ckch_store *ckchs_lookup(char *path)
William Lallemand6af03992019-07-23 15:00:54 +02004010{
4011 struct ebmb_node *eb;
4012
William Lallemande3af8fb2019-10-08 11:36:53 +02004013 eb = ebst_lookup(&ckchs_tree, path);
William Lallemand6af03992019-07-23 15:00:54 +02004014 if (!eb)
4015 return NULL;
4016
William Lallemande3af8fb2019-10-08 11:36:53 +02004017 return ebmb_entry(eb, struct ckch_store, node);
William Lallemand6af03992019-07-23 15:00:54 +02004018}
4019
4020/*
William Lallemande3af8fb2019-10-08 11:36:53 +02004021 * This function allocate a ckch_store and populate it with certificates from files.
William Lallemand36b84632019-07-18 19:28:17 +02004022 */
William Lallemandc69973f2020-05-12 17:42:42 +02004023struct ckch_store *ckchs_load_cert_file(char *path, int multi, char **err)
William Lallemand36b84632019-07-18 19:28:17 +02004024{
William Lallemande3af8fb2019-10-08 11:36:53 +02004025 struct ckch_store *ckchs;
William Lallemand36b84632019-07-18 19:28:17 +02004026
William Lallemand8a874e42020-04-09 10:32:53 +02004027 ckchs = ckch_store_new(path, multi ? SSL_SOCK_NUM_KEYTYPES : 1);
William Lallemande3af8fb2019-10-08 11:36:53 +02004028 if (!ckchs) {
William Lallemand36b84632019-07-18 19:28:17 +02004029 memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : "");
4030 goto end;
4031 }
William Lallemand36b84632019-07-18 19:28:17 +02004032 if (!multi) {
4033
William Lallemand96a9c972019-10-17 11:56:17 +02004034 if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02004035 goto end;
4036
William Lallemande3af8fb2019-10-08 11:36:53 +02004037 /* insert into the ckchs tree */
4038 memcpy(ckchs->path, path, strlen(path) + 1);
4039 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02004040 } else {
4041 int found = 0;
William Lallemandc4ecddf2019-07-31 16:50:08 +02004042#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
4043 char fp[MAXPATHLEN+1] = {0};
4044 int n = 0;
William Lallemand36b84632019-07-18 19:28:17 +02004045
4046 /* Load all possible certs and keys */
4047 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
4048 struct stat buf;
4049 snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
4050 if (stat(fp, &buf) == 0) {
William Lallemand96a9c972019-10-17 11:56:17 +02004051 if (ssl_sock_load_files_into_ckch(fp, &ckchs->ckch[n], err) == 1)
William Lallemand36b84632019-07-18 19:28:17 +02004052 goto end;
4053 found = 1;
William Lallemande3af8fb2019-10-08 11:36:53 +02004054 ckchs->multi = 1;
William Lallemand36b84632019-07-18 19:28:17 +02004055 }
4056 }
William Lallemandc4ecddf2019-07-31 16:50:08 +02004057#endif
William Lallemand36b84632019-07-18 19:28:17 +02004058
4059 if (!found) {
William Lallemand6e5f2ce2019-08-01 14:43:20 +02004060 memprintf(err, "%sDidn't find any certificate for bundle '%s'.\n", err && *err ? *err : "", path);
William Lallemand36b84632019-07-18 19:28:17 +02004061 goto end;
4062 }
William Lallemande3af8fb2019-10-08 11:36:53 +02004063 /* insert into the ckchs tree */
4064 memcpy(ckchs->path, path, strlen(path) + 1);
4065 ebst_insert(&ckchs_tree, &ckchs->node);
William Lallemand36b84632019-07-18 19:28:17 +02004066 }
William Lallemande3af8fb2019-10-08 11:36:53 +02004067 return ckchs;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004068
William Lallemand36b84632019-07-18 19:28:17 +02004069end:
William Lallemandba1c33f2020-04-08 17:55:45 +02004070 ckch_store_free(ckchs);
William Lallemand36b84632019-07-18 19:28:17 +02004071
4072 return NULL;
4073}
4074
William Lallemandc4ecddf2019-07-31 16:50:08 +02004075#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
4076
William Lallemand36b84632019-07-18 19:28:17 +02004077/*
William Lallemande3af8fb2019-10-08 11:36:53 +02004078 * Take a ckch_store which contains a multi-certificate bundle.
William Lallemand36b84632019-07-18 19:28:17 +02004079 * Group these certificates into a set of SSL_CTX*
yanbzhu08ce6ab2015-12-02 13:01:29 -05004080 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
4081 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08004082 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05004083 *
Emeric Brun054563d2019-10-17 13:16:58 +02004084 * Returns a bitfield containing the flags:
4085 * ERR_FATAL in any fatal error case
4086 * ERR_ALERT if the reason of the error is available in err
4087 * ERR_WARN if a warning is available into err
William Lallemand36b84632019-07-18 19:28:17 +02004088 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05004089 */
Emeric Brun054563d2019-10-17 13:16:58 +02004090static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
4091 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4092 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004093{
William Lallemand36b84632019-07-18 19:28:17 +02004094 int i = 0, n = 0;
4095 struct cert_key_and_chain *certs_and_keys;
William Lallemand4b989f22019-10-04 18:36:55 +02004096 struct eb_root sni_keytypes_map = EB_ROOT;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004097 struct ebmb_node *node;
4098 struct ebmb_node *next;
4099 /* Array of SSL_CTX pointers corresponding to each possible combo
4100 * of keytypes
4101 */
4102 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Emeric Brun054563d2019-10-17 13:16:58 +02004103 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004104 X509_NAME *xname = NULL;
4105 char *str = NULL;
4106#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4107 STACK_OF(GENERAL_NAME) *names = NULL;
4108#endif
William Lallemand614ca0d2019-10-07 13:52:11 +02004109 struct ckch_inst *ckch_inst;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004110
Emeric Brun054563d2019-10-17 13:16:58 +02004111 *ckchi = NULL;
4112
William Lallemande3af8fb2019-10-08 11:36:53 +02004113 if (!ckchs || !ckchs->ckch || !ckchs->multi) {
William Lallemand36b84632019-07-18 19:28:17 +02004114 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
4115 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004116 return ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02004117 }
4118
4119 ckch_inst = ckch_inst_new();
4120 if (!ckch_inst) {
4121 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4122 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004123 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02004124 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004125 }
4126
William Lallemande3af8fb2019-10-08 11:36:53 +02004127 certs_and_keys = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004128
yanbzhu08ce6ab2015-12-02 13:01:29 -05004129 /* Process each ckch and update keytypes for each CN/SAN
4130 * for example, if CN/SAN www.a.com is associated with
4131 * certs with keytype 0 and 2, then at the end of the loop,
4132 * www.a.com will have:
4133 * keyindex = 0 | 1 | 4 = 5
4134 */
4135 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand28a8fce2019-10-04 17:36:55 +02004136 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004137
4138 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
4139 continue;
4140
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004141 if (fcount) {
William Lallemand28a8fce2019-10-04 17:36:55 +02004142 for (i = 0; i < fcount; i++) {
4143 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
4144 if (ret < 0) {
4145 memprintf(err, "%sunable to allocate SSL context.\n",
4146 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004147 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004148 goto end;
4149 }
4150 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004151 } else {
4152 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
4153 * so the line that contains logic is marked via comments
4154 */
4155 xname = X509_get_subject_name(certs_and_keys[n].cert);
4156 i = -1;
4157 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4158 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004159 ASN1_STRING *value;
4160 value = X509_NAME_ENTRY_get_data(entry);
4161 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004162 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02004163 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004164
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004165 OPENSSL_free(str);
4166 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004167 if (ret < 0) {
4168 memprintf(err, "%sunable to allocate SSL context.\n",
4169 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004170 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004171 goto end;
4172 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004173 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004174 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004175
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004176 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05004177#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004178 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
4179 if (names) {
4180 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4181 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004182
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004183 if (name->type == GEN_DNS) {
4184 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
4185 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02004186 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004187
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004188 OPENSSL_free(str);
4189 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004190 if (ret < 0) {
4191 memprintf(err, "%sunable to allocate SSL context.\n",
4192 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004193 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02004194 goto end;
4195 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02004196 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004197 }
4198 }
4199 }
4200 }
4201#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
4202 }
4203
4204 /* If no files found, return error */
4205 if (eb_is_empty(&sni_keytypes_map)) {
4206 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
4207 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004208 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004209 goto end;
4210 }
4211
4212 /* We now have a map of CN/SAN to keytypes that are loaded in
4213 * Iterate through the map to create the SSL_CTX's (if needed)
4214 * and add each CTX to the SNI tree
4215 *
4216 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08004217 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05004218 * combination is denoted by the key in the map. Each key
4219 * has a value between 1 and 2^n - 1. Conveniently, the array
4220 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
4221 * entry in the array to correspond to the unique combo (key)
4222 * associated with i. This unique key combo (i) will be associated
4223 * with combos[i-1]
4224 */
4225
4226 node = ebmb_first(&sni_keytypes_map);
4227 while (node) {
4228 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00004229 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004230 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05004231
4232 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
4233 i = container_of(node, struct sni_keytype, name)->keytypes;
4234 cur_ctx = key_combos[i-1].ctx;
4235
4236 if (cur_ctx == NULL) {
4237 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004238 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05004239 if (cur_ctx == NULL) {
4240 memprintf(err, "%sunable to allocate SSL context.\n",
4241 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004242 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004243 goto end;
4244 }
4245
yanbzhube2774d2015-12-10 15:07:30 -05004246 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05004247 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
4248 if (i & (1<<n)) {
4249 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00004250 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Bruna96b5822019-10-17 13:25:14 +02004251 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
4252 if (errcode & ERR_CODE)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004253 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004254 }
4255 }
4256
yanbzhu08ce6ab2015-12-02 13:01:29 -05004257 /* Update key_combos */
4258 key_combos[i-1].ctx = cur_ctx;
4259 }
4260
4261 /* Update SNI Tree */
William Lallemand9117de92019-10-04 00:29:42 +02004262
William Lallemand1d29c742019-10-04 00:53:29 +02004263 key_combos[i-1].order = ckch_inst_add_cert_sni(cur_ctx, ckch_inst, bind_conf, ssl_conf,
William Lallemandfe49bb32019-10-03 23:46:33 +02004264 kinfo, str, key_combos[i-1].order);
4265 if (key_combos[i-1].order < 0) {
4266 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004267 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandfe49bb32019-10-03 23:46:33 +02004268 goto end;
4269 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05004270 node = ebmb_next(node);
4271 }
4272
4273
4274 /* Mark a default context if none exists, using the ctx that has the most shared keys */
4275 if (!bind_conf->default_ctx) {
4276 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
4277 if (key_combos[i].ctx) {
4278 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004279 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004280 ckch_inst->is_default = 1;
William Lallemand02e19a52020-04-08 16:11:26 +02004281 SSL_CTX_up_ref(bind_conf->default_ctx);
yanbzhu08ce6ab2015-12-02 13:01:29 -05004282 break;
4283 }
4284 }
4285 }
4286
William Lallemand614ca0d2019-10-07 13:52:11 +02004287 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004288 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01004289 ckch_inst->ckch_store = ckchs;
William Lallemand02e19a52020-04-08 16:11:26 +02004290
yanbzhu08ce6ab2015-12-02 13:01:29 -05004291end:
4292
4293 if (names)
4294 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
4295
yanbzhu08ce6ab2015-12-02 13:01:29 -05004296 node = ebmb_first(&sni_keytypes_map);
4297 while (node) {
4298 next = ebmb_next(node);
4299 ebmb_delete(node);
William Lallemand8ed5b962019-10-04 17:24:39 +02004300 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05004301 node = next;
4302 }
4303
William Lallemand02e19a52020-04-08 16:11:26 +02004304 /* we need to free the ctx since we incremented the refcount where it's used */
4305 for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) {
4306 if (key_combos[i].ctx)
4307 SSL_CTX_free(key_combos[i].ctx);
4308 }
4309
Emeric Brun054563d2019-10-17 13:16:58 +02004310 if (errcode & ERR_CODE && ckch_inst) {
William Lallemand02e19a52020-04-08 16:11:26 +02004311 if (ckch_inst->is_default) {
4312 SSL_CTX_free(bind_conf->default_ctx);
4313 bind_conf->default_ctx = NULL;
4314 }
4315
William Lallemandd9d5d1b2020-04-09 16:31:05 +02004316 ckch_inst_free(ckch_inst);
William Lallemand614ca0d2019-10-07 13:52:11 +02004317 ckch_inst = NULL;
William Lallemand0c6d12f2019-10-04 18:38:51 +02004318 }
4319
Emeric Brun054563d2019-10-17 13:16:58 +02004320 *ckchi = ckch_inst;
4321 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004322}
4323#else
4324/* This is a dummy, that just logs an error and returns error */
Emeric Brun054563d2019-10-17 13:16:58 +02004325static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
4326 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
4327 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05004328{
4329 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4330 err && *err ? *err : "", path, strerror(errno));
Emeric Brun054563d2019-10-17 13:16:58 +02004331 return ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05004332}
4333
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004334#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05004335
William Lallemand614ca0d2019-10-07 13:52:11 +02004336/*
4337 * This function allocate a ckch_inst and create its snis
Emeric Brun054563d2019-10-17 13:16:58 +02004338 *
4339 * Returns a bitfield containing the flags:
4340 * ERR_FATAL in any fatal error case
4341 * ERR_ALERT if the reason of the error is available in err
4342 * ERR_WARN if a warning is available into err
William Lallemand614ca0d2019-10-07 13:52:11 +02004343 */
Emeric Brun054563d2019-10-17 13:16:58 +02004344static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf,
4345 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004346{
William Lallemandc9402072019-05-15 15:33:54 +02004347 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02004348 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004349 int order = 0;
4350 X509_NAME *xname;
4351 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004352 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004353 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02004354#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
4355 STACK_OF(GENERAL_NAME) *names;
4356#endif
William Lallemand36b84632019-07-18 19:28:17 +02004357 struct cert_key_and_chain *ckch;
William Lallemand614ca0d2019-10-07 13:52:11 +02004358 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02004359 int errcode = 0;
4360
4361 *ckchi = NULL;
William Lallemanda59191b2019-05-15 16:08:56 +02004362
William Lallemande3af8fb2019-10-08 11:36:53 +02004363 if (!ckchs || !ckchs->ckch)
Emeric Brun054563d2019-10-17 13:16:58 +02004364 return ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004365
William Lallemande3af8fb2019-10-08 11:36:53 +02004366 ckch = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02004367
William Lallemandc9402072019-05-15 15:33:54 +02004368 ctx = SSL_CTX_new(SSLv23_server_method());
4369 if (!ctx) {
4370 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4371 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004372 errcode |= ERR_ALERT | ERR_FATAL;
4373 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004374 }
4375
Emeric Bruna96b5822019-10-17 13:25:14 +02004376 errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err);
4377 if (errcode & ERR_CODE)
William Lallemand614ca0d2019-10-07 13:52:11 +02004378 goto error;
William Lallemand614ca0d2019-10-07 13:52:11 +02004379
4380 ckch_inst = ckch_inst_new();
4381 if (!ckch_inst) {
4382 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
4383 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02004384 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004385 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02004386 }
4387
William Lallemand36b84632019-07-18 19:28:17 +02004388 pkey = X509_get_pubkey(ckch->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004389 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004390 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004391 switch(EVP_PKEY_base_id(pkey)) {
4392 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004393 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004394 break;
4395 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02004396 kinfo.sig = TLSEXT_signature_ecdsa;
4397 break;
4398 case EVP_PKEY_DSA:
4399 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01004400 break;
4401 }
4402 EVP_PKEY_free(pkey);
4403 }
4404
Emeric Brun50bcecc2013-04-22 13:05:23 +02004405 if (fcount) {
William Lallemandfe49bb32019-10-03 23:46:33 +02004406 while (fcount--) {
William Lallemand1d29c742019-10-04 00:53:29 +02004407 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 +02004408 if (order < 0) {
4409 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004410 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004411 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004412 }
4413 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004414 }
4415 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02004416#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand36b84632019-07-18 19:28:17 +02004417 names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004418 if (names) {
4419 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
4420 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
4421 if (name->type == GEN_DNS) {
4422 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004423 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004424 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004425 if (order < 0) {
4426 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004427 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004428 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004429 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004430 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004431 }
4432 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004433 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004434 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004435#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand36b84632019-07-18 19:28:17 +02004436 xname = X509_get_subject_name(ckch->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004437 i = -1;
4438 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
4439 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004440 ASN1_STRING *value;
4441
4442 value = X509_NAME_ENTRY_get_data(entry);
4443 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02004444 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004445 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02004446 if (order < 0) {
4447 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004448 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004449 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02004450 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004451 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004452 }
4453 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004454 /* we must not free the SSL_CTX anymore below, since it's already in
4455 * the tree, so it will be discovered and cleaned in time.
4456 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02004457
Emeric Brunfc0421f2012-09-07 17:30:07 +02004458#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004459 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02004460 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
4461 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02004462 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02004463 goto error;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004464 }
4465#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004466 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004467 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004468 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01004469 ckch_inst->is_default = 1;
William Lallemand02e19a52020-04-08 16:11:26 +02004470 SSL_CTX_up_ref(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004471 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004472
William Lallemand9117de92019-10-04 00:29:42 +02004473 /* everything succeed, the ckch instance can be used */
4474 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02004475 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01004476 ckch_inst->ckch_store = ckchs;
William Lallemand9117de92019-10-04 00:29:42 +02004477
William Lallemand02e19a52020-04-08 16:11:26 +02004478 SSL_CTX_free(ctx); /* we need to free the ctx since we incremented the refcount where it's used */
4479
Emeric Brun054563d2019-10-17 13:16:58 +02004480 *ckchi = ckch_inst;
4481 return errcode;
William Lallemandd9199372019-10-04 15:37:05 +02004482
4483error:
4484 /* free the allocated sni_ctxs */
William Lallemand614ca0d2019-10-07 13:52:11 +02004485 if (ckch_inst) {
William Lallemand02e19a52020-04-08 16:11:26 +02004486 if (ckch_inst->is_default)
4487 SSL_CTX_free(ctx);
4488
William Lallemandd9d5d1b2020-04-09 16:31:05 +02004489 ckch_inst_free(ckch_inst);
William Lallemand614ca0d2019-10-07 13:52:11 +02004490 ckch_inst = NULL;
William Lallemandd9199372019-10-04 15:37:05 +02004491 }
William Lallemandd9199372019-10-04 15:37:05 +02004492 SSL_CTX_free(ctx);
4493
Emeric Brun054563d2019-10-17 13:16:58 +02004494 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004495}
4496
Willy Tarreau8c5414a2019-10-16 17:06:25 +02004497/* Returns a set of ERR_* flags possibly with an error in <err>. */
William Lallemand614ca0d2019-10-07 13:52:11 +02004498static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs,
4499 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
William Lallemand24bde432020-03-09 16:48:43 +01004500 char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err)
William Lallemand614ca0d2019-10-07 13:52:11 +02004501{
Emeric Brun054563d2019-10-17 13:16:58 +02004502 int errcode = 0;
William Lallemand614ca0d2019-10-07 13:52:11 +02004503
4504 /* we found the ckchs in the tree, we can use it directly */
4505 if (ckchs->multi)
William Lallemand24bde432020-03-09 16:48:43 +01004506 errcode |= ckch_inst_new_load_multi_store(path, ckchs, bind_conf, ssl_conf, sni_filter, fcount, ckch_inst, err);
William Lallemand614ca0d2019-10-07 13:52:11 +02004507 else
William Lallemand24bde432020-03-09 16:48:43 +01004508 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 +02004509
Emeric Brun054563d2019-10-17 13:16:58 +02004510 if (errcode & ERR_CODE)
4511 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004512
William Lallemand24bde432020-03-09 16:48:43 +01004513 ssl_sock_load_cert_sni(*ckch_inst, bind_conf);
William Lallemand614ca0d2019-10-07 13:52:11 +02004514
4515 /* succeed, add the instance to the ckch_store's list of instance */
William Lallemand24bde432020-03-09 16:48:43 +01004516 LIST_ADDQ(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs));
Emeric Brun054563d2019-10-17 13:16:58 +02004517 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02004518}
4519
William Lallemand6be66ec2020-03-06 22:26:32 +01004520
William Lallemand4c68bba2020-03-30 18:45:10 +02004521
4522
4523/* Make sure openssl opens /dev/urandom before the chroot. The work is only
4524 * done once. Zero is returned if the operation fails. No error is returned
4525 * if the random is said as not implemented, because we expect that openssl
4526 * will use another method once needed.
4527 */
4528static int ssl_initialize_random()
4529{
4530 unsigned char random;
4531 static int random_initialized = 0;
4532
4533 if (!random_initialized && RAND_bytes(&random, 1) != 0)
4534 random_initialized = 1;
4535
4536 return random_initialized;
4537}
4538
William Lallemandec2d4932020-04-09 13:44:21 +02004539
William Lallemand6be66ec2020-03-06 22:26:32 +01004540/* This function reads a directory and stores it in a struct crtlist, each file is a crtlist_entry structure
4541 * Fill the <crtlist> argument with a pointer to a new crtlist struct
4542 *
4543 * This function tries to open and store certificate files.
4544 */
4545static int crtlist_load_cert_dir(char *path, struct bind_conf *bind_conf, struct crtlist **crtlist, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004546{
William Lallemand6be66ec2020-03-06 22:26:32 +01004547 struct crtlist *dir;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004548 struct dirent **de_list;
4549 int i, n;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004550 struct stat buf;
Willy Tarreauee2663b2012-12-06 11:36:59 +01004551 char *end;
4552 char fp[MAXPATHLEN+1];
Emeric Brunfc0421f2012-09-07 17:30:07 +02004553 int cfgerr = 0;
William Lallemande3af8fb2019-10-08 11:36:53 +02004554 struct ckch_store *ckchs;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004555#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu63ea8462015-12-09 13:35:14 -05004556 int is_bundle;
4557 int j;
4558#endif
Willy Tarreaubbc91962019-10-16 16:42:19 +02004559
William Lallemandec2d4932020-04-09 13:44:21 +02004560 dir = crtlist_new(path, 1);
William Lallemand6be66ec2020-03-06 22:26:32 +01004561 if (dir == NULL) {
4562 memprintf(err, "not enough memory");
4563 return ERR_ALERT | ERR_FATAL;
4564 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004565
William Lallemand2d232c22020-03-06 22:12:35 +01004566 n = scandir(path, &de_list, 0, alphasort);
4567 if (n < 0) {
4568 memprintf(err, "%sunable to scan directory '%s' : %s.\n",
4569 err && *err ? *err : "", path, strerror(errno));
4570 cfgerr |= ERR_ALERT | ERR_FATAL;
4571 }
4572 else {
4573 for (i = 0; i < n; i++) {
William Lallemand6be66ec2020-03-06 22:26:32 +01004574 struct crtlist_entry *entry;
William Lallemand2d232c22020-03-06 22:12:35 +01004575 struct dirent *de = de_list[i];
Emeric Brun2aab7222014-06-18 18:15:09 +02004576
William Lallemand2d232c22020-03-06 22:12:35 +01004577 end = strrchr(de->d_name, '.');
4578 if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl") || !strcmp(end, ".key")))
4579 goto ignore_entry;
Cyril Bonté3180f7b2015-01-25 00:16:08 +01004580
William Lallemanda64593c2020-03-17 20:11:41 +01004581 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
4582 if (stat(fp, &buf) != 0) {
4583 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
4584 err && *err ? *err : "", fp, strerror(errno));
4585 cfgerr |= ERR_ALERT | ERR_FATAL;
4586 goto ignore_entry;
4587 }
4588 if (!S_ISREG(buf.st_mode))
4589 goto ignore_entry;
4590
William Lallemande718dfb2020-04-10 11:09:25 +02004591 entry = crtlist_entry_new();
William Lallemand6be66ec2020-03-06 22:26:32 +01004592 if (entry == NULL) {
4593 memprintf(err, "not enough memory '%s'", fp);
4594 cfgerr |= ERR_ALERT | ERR_FATAL;
4595 goto ignore_entry;
4596 }
4597
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004598#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemand2d232c22020-03-06 22:12:35 +01004599 is_bundle = 0;
4600 /* Check if current entry in directory is part of a multi-cert bundle */
yanbzhu63ea8462015-12-09 13:35:14 -05004601
William Lallemand2d232c22020-03-06 22:12:35 +01004602 if ((global_ssl.extra_files & SSL_GF_BUNDLE) && end) {
4603 for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) {
4604 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
4605 is_bundle = 1;
4606 break;
yanbzhu63ea8462015-12-09 13:35:14 -05004607 }
William Lallemand2d232c22020-03-06 22:12:35 +01004608 }
yanbzhu63ea8462015-12-09 13:35:14 -05004609
William Lallemand2d232c22020-03-06 22:12:35 +01004610 if (is_bundle) {
4611 int dp_len;
yanbzhu63ea8462015-12-09 13:35:14 -05004612
William Lallemand2d232c22020-03-06 22:12:35 +01004613 dp_len = end - de->d_name;
yanbzhu63ea8462015-12-09 13:35:14 -05004614
William Lallemand2d232c22020-03-06 22:12:35 +01004615 /* increment i and free de until we get to a non-bundle cert
4616 * Note here that we look at de_list[i + 1] before freeing de
4617 * this is important since ignore_entry will free de. This also
4618 * guarantees that de->d_name continues to hold the same prefix.
4619 */
4620 while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) {
4621 free(de);
4622 i++;
4623 de = de_list[i];
yanbzhu63ea8462015-12-09 13:35:14 -05004624 }
William Lallemand2d232c22020-03-06 22:12:35 +01004625
4626 snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name);
William Lallemand6be66ec2020-03-06 22:26:32 +01004627 ckchs = ckchs_lookup(fp);
4628 if (ckchs == NULL)
4629 ckchs = ckchs_load_cert_file(fp, 1, err);
4630 if (ckchs == NULL) {
4631 free(de);
4632 free(entry);
William Lallemand2d232c22020-03-06 22:12:35 +01004633 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand6be66ec2020-03-06 22:26:32 +01004634 goto end;
4635 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004636 entry->node.key = ckchs;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004637 entry->crtlist = dir;
William Lallemand23d61c02020-03-30 18:27:58 +02004638 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand83918e22020-03-16 17:21:51 +01004639 LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist);
William Lallemand6be66ec2020-03-06 22:26:32 +01004640 ebpt_insert(&dir->entries, &entry->node);
4641
William Lallemand2d232c22020-03-06 22:12:35 +01004642 /* Successfully processed the bundle */
4643 goto ignore_entry;
yanbzhu63ea8462015-12-09 13:35:14 -05004644 }
William Lallemand2d232c22020-03-06 22:12:35 +01004645 }
yanbzhu63ea8462015-12-09 13:35:14 -05004646
4647#endif
William Lallemand6be66ec2020-03-06 22:26:32 +01004648 ckchs = ckchs_lookup(fp);
4649 if (ckchs == NULL)
4650 ckchs = ckchs_load_cert_file(fp, 0, err);
4651 if (ckchs == NULL) {
4652 free(de);
4653 free(entry);
William Lallemand2d232c22020-03-06 22:12:35 +01004654 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand6be66ec2020-03-06 22:26:32 +01004655 goto end;
4656 }
4657 entry->node.key = ckchs;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004658 entry->crtlist = dir;
William Lallemand23d61c02020-03-30 18:27:58 +02004659 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand83918e22020-03-16 17:21:51 +01004660 LIST_ADDQ(&dir->ord_entries, &entry->by_crtlist);
William Lallemand6be66ec2020-03-06 22:26:32 +01004661 ebpt_insert(&dir->entries, &entry->node);
William Lallemand36b84632019-07-18 19:28:17 +02004662
yanbzhu08ce6ab2015-12-02 13:01:29 -05004663ignore_entry:
William Lallemand2d232c22020-03-06 22:12:35 +01004664 free(de);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004665 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004666end:
William Lallemand2d232c22020-03-06 22:12:35 +01004667 free(de_list);
4668 }
William Lallemand6be66ec2020-03-06 22:26:32 +01004669
4670 if (cfgerr & ERR_CODE) {
4671 /* free the dir and entries on error */
William Lallemand09bd5a02020-03-30 18:19:43 +02004672 crtlist_free(dir);
William Lallemand83918e22020-03-16 17:21:51 +01004673 } else {
4674 *crtlist = dir;
William Lallemand6be66ec2020-03-06 22:26:32 +01004675 }
William Lallemand2d232c22020-03-06 22:12:35 +01004676 return cfgerr;
William Lallemand6be66ec2020-03-06 22:26:32 +01004677
William Lallemand2d232c22020-03-06 22:12:35 +01004678}
yanbzhu08ce6ab2015-12-02 13:01:29 -05004679
William Lallemand97b08102020-04-01 10:14:00 +02004680/*
4681 * Read a single crt-list line. /!\ alter the <line> string.
4682 * Fill <crt_path> and <crtlist_entry>
4683 * <crtlist_entry> must be alloc and free by the caller
4684 * <crtlist_entry->ssl_conf> is alloc by the function
4685 * <crtlist_entry->filters> is alloc by the function
4686 * <crt_path> is a ptr in <line>
4687 * Return an error code
4688 */
4689static int crtlist_parse_line(char *line, char **crt_path, struct crtlist_entry *entry, const char *file, int linenum, char **err)
4690{
4691 int cfgerr = 0;
4692 int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0;
4693 char *end;
4694 char *args[MAX_CRT_ARGS + 1];
4695 struct ssl_bind_conf *ssl_conf = NULL;
4696
4697 if (!line || !crt_path || !entry)
4698 return ERR_ALERT | ERR_FATAL;
4699
4700 end = line + strlen(line);
4701 if (end-line >= CRT_LINESIZE-1 && *(end-1) != '\n') {
4702 /* Check if we reached the limit and the last char is not \n.
4703 * Watch out for the last line without the terminating '\n'!
4704 */
4705 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4706 linenum, file, CRT_LINESIZE-1);
4707 cfgerr |= ERR_ALERT | ERR_FATAL;
4708 goto error;
4709 }
4710 arg = 0;
4711 newarg = 1;
4712 while (*line) {
4713 if (isspace((unsigned char)*line)) {
4714 newarg = 1;
4715 *line = 0;
4716 } else if (*line == '[') {
4717 if (ssl_b) {
4718 memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file);
4719 cfgerr |= ERR_ALERT | ERR_FATAL;
4720 goto error;
4721 }
4722 if (!arg) {
4723 memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file);
4724 cfgerr |= ERR_ALERT | ERR_FATAL;
4725 goto error;
4726 }
4727 ssl_b = arg;
4728 newarg = 1;
4729 *line = 0;
4730 } else if (*line == ']') {
4731 if (ssl_e) {
4732 memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file);
4733 cfgerr |= ERR_ALERT | ERR_FATAL;
4734 goto error;
4735 }
4736 if (!ssl_b) {
4737 memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file);
4738 cfgerr |= ERR_ALERT | ERR_FATAL;
4739 goto error;
4740 }
4741 ssl_e = arg;
4742 newarg = 1;
4743 *line = 0;
4744 } else if (newarg) {
4745 if (arg == MAX_CRT_ARGS) {
4746 memprintf(err, "too many args on line %d in file '%s'.", linenum, file);
4747 cfgerr |= ERR_ALERT | ERR_FATAL;
4748 goto error;
4749 }
4750 newarg = 0;
4751 args[arg++] = line;
4752 }
4753 line++;
4754 }
4755 args[arg++] = line;
4756
4757 /* empty line */
4758 if (!*args[0]) {
4759 cfgerr |= ERR_NONE;
4760 goto error;
4761 }
4762
4763 *crt_path = args[0];
4764
William Lallemand1b2988b2020-04-10 17:20:45 +02004765 if (ssl_b) {
4766 ssl_conf = calloc(1, sizeof *ssl_conf);
4767 if (!ssl_conf) {
4768 memprintf(err, "not enough memory!");
4769 cfgerr |= ERR_ALERT | ERR_FATAL;
4770 goto error;
4771 }
William Lallemand97b08102020-04-01 10:14:00 +02004772 }
4773 cur_arg = ssl_b ? ssl_b : 1;
4774 while (cur_arg < ssl_e) {
4775 newarg = 0;
4776 for (i = 0; ssl_bind_kws[i].kw != NULL; i++) {
4777 if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) {
4778 newarg = 1;
4779 cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, NULL, ssl_conf, err);
4780 if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) {
4781 memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'",
4782 args[cur_arg], linenum, file);
4783 cfgerr |= ERR_ALERT | ERR_FATAL;
4784 goto error;
4785 }
4786 cur_arg += 1 + ssl_bind_kws[i].skip;
4787 break;
4788 }
4789 }
4790 if (!cfgerr && !newarg) {
4791 memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.",
4792 args[cur_arg], linenum, file);
4793 cfgerr |= ERR_ALERT | ERR_FATAL;
4794 goto error;
4795 }
4796 }
William Lallemandc69f02d2020-04-06 19:07:03 +02004797 entry->linenum = linenum;
William Lallemand97b08102020-04-01 10:14:00 +02004798 entry->ssl_conf = ssl_conf;
4799 entry->filters = crtlist_dup_filters(&args[cur_arg], arg - cur_arg - 1);
4800 entry->fcount = arg - cur_arg - 1;
4801
4802 return cfgerr;
4803
4804error:
4805 crtlist_free_filters(entry->filters);
4806 entry->filters = NULL;
4807 ssl_sock_free_ssl_conf(entry->ssl_conf);
4808 free(entry->ssl_conf);
4809 entry->ssl_conf = NULL;
4810 return cfgerr;
4811}
4812
William Lallemand2954c472020-03-06 21:54:13 +01004813/* This function parse a crt-list file and store it in a struct crtlist, each line is a crtlist_entry structure
4814 * Fill the <crtlist> argument with a pointer to a new crtlist struct
4815 *
4816 * This function tries to open and store certificate files.
4817 */
4818static int crtlist_parse_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, struct crtlist **crtlist, char **err)
4819{
4820 struct crtlist *newlist;
William Lallemand97b08102020-04-01 10:14:00 +02004821 struct crtlist_entry *entry = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004822 char thisline[CRT_LINESIZE];
4823 char path[MAXPATHLEN+1];
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004824 FILE *f;
yanbzhu1b04e5b2015-12-02 13:54:14 -05004825 struct stat buf;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004826 int linenum = 0;
4827 int cfgerr = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004828
Willy Tarreauad1731d2013-04-02 17:35:58 +02004829 if ((f = fopen(file, "r")) == NULL) {
4830 memprintf(err, "cannot open file '%s' : %s", file, strerror(errno));
Willy Tarreaubbc91962019-10-16 16:42:19 +02004831 return ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004832 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004833
William Lallemandec2d4932020-04-09 13:44:21 +02004834 newlist = crtlist_new(file, 0);
William Lallemand2954c472020-03-06 21:54:13 +01004835 if (newlist == NULL) {
4836 memprintf(err, "Not enough memory!");
4837 cfgerr |= ERR_ALERT | ERR_FATAL;
4838 goto error;
4839 }
William Lallemand2954c472020-03-06 21:54:13 +01004840
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004841 while (fgets(thisline, sizeof(thisline), f) != NULL) {
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004842 char *end;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004843 char *line = thisline;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004844 char *crt_path;
William Lallemand2954c472020-03-06 21:54:13 +01004845 struct ckch_store *ckchs;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004846
4847 linenum++;
4848 end = line + strlen(line);
4849 if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
4850 /* Check if we reached the limit and the last char is not \n.
4851 * Watch out for the last line without the terminating '\n'!
4852 */
Willy Tarreauad1731d2013-04-02 17:35:58 +02004853 memprintf(err, "line %d too long in file '%s', limit is %d characters",
4854 linenum, file, (int)sizeof(thisline)-1);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004855 cfgerr |= ERR_ALERT | ERR_FATAL;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004856 break;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004857 }
William Lallemand97b08102020-04-01 10:14:00 +02004858
4859 if (*line == '#' || *line == '\n' || *line == '\r')
4860 continue;
4861
William Lallemande718dfb2020-04-10 11:09:25 +02004862 entry = crtlist_entry_new();
William Lallemand97b08102020-04-01 10:14:00 +02004863 if (entry == NULL) {
4864 memprintf(err, "Not enough memory!");
4865 cfgerr |= ERR_ALERT | ERR_FATAL;
4866 goto error;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004867 }
William Lallemand97b08102020-04-01 10:14:00 +02004868
4869 *(end - 1) = '\0'; /* line parser mustn't receive any \n */
4870 cfgerr |= crtlist_parse_line(thisline, &crt_path, entry, file, linenum, err);
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02004871 if (cfgerr)
William Lallemand97b08102020-04-01 10:14:00 +02004872 goto error;
Willy Tarreauad1731d2013-04-02 17:35:58 +02004873
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004874 /* empty line */
William Lallemand97b08102020-04-01 10:14:00 +02004875 if (!crt_path || !*crt_path) {
William Lallemande718dfb2020-04-10 11:09:25 +02004876 crtlist_entry_free(entry);
William Lallemand97b08102020-04-01 10:14:00 +02004877 entry = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004878 continue;
William Lallemand97b08102020-04-01 10:14:00 +02004879 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004880
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004881 if (*crt_path != '/' && global_ssl.crt_base) {
4882 if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) {
4883 memprintf(err, "'%s' : path too long on line %d in file '%s'",
4884 crt_path, linenum, file);
Willy Tarreaubbc91962019-10-16 16:42:19 +02004885 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemand97b08102020-04-01 10:14:00 +02004886 goto error;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004887 }
4888 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path);
4889 crt_path = path;
4890 }
4891
William Lallemand2954c472020-03-06 21:54:13 +01004892 /* Look for a ckch_store or create one */
4893 ckchs = ckchs_lookup(crt_path);
4894 if (ckchs == NULL) {
William Lallemandeed4bf22019-10-10 11:38:13 +02004895 if (stat(crt_path, &buf) == 0)
William Lallemande3af8fb2019-10-08 11:36:53 +02004896 ckchs = ckchs_load_cert_file(crt_path, 0, err);
Emmanuel Hocdet1503e052019-07-31 18:30:33 +02004897 else
William Lallemande3af8fb2019-10-08 11:36:53 +02004898 ckchs = ckchs_load_cert_file(crt_path, 1, err);
yanbzhu1b04e5b2015-12-02 13:54:14 -05004899 }
William Lallemand909086e2020-03-17 16:53:27 +01004900 if (ckchs == NULL)
William Lallemand2954c472020-03-06 21:54:13 +01004901 cfgerr |= ERR_ALERT | ERR_FATAL;
William Lallemandeed4bf22019-10-10 11:38:13 +02004902
William Lallemand97b08102020-04-01 10:14:00 +02004903 if (cfgerr & ERR_CODE)
William Lallemand2954c472020-03-06 21:54:13 +01004904 goto error;
William Lallemand97b08102020-04-01 10:14:00 +02004905
William Lallemand2954c472020-03-06 21:54:13 +01004906 entry->node.key = ckchs;
William Lallemandfa8cf0c2020-03-30 19:59:57 +02004907 entry->crtlist = newlist;
William Lallemand2954c472020-03-06 21:54:13 +01004908 ebpt_insert(&newlist->entries, &entry->node);
4909 LIST_ADDQ(&newlist->ord_entries, &entry->by_crtlist);
William Lallemand23d61c02020-03-30 18:27:58 +02004910 LIST_ADDQ(&ckchs->crtlist_entry, &entry->by_ckch_store);
William Lallemand97b08102020-04-01 10:14:00 +02004911
4912 entry = NULL;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004913 }
William Lallemand2954c472020-03-06 21:54:13 +01004914 if (cfgerr & ERR_CODE)
4915 goto error;
4916
William Lallemandc69f02d2020-04-06 19:07:03 +02004917 newlist->linecount = linenum;
4918
William Lallemand2954c472020-03-06 21:54:13 +01004919 fclose(f);
4920 *crtlist = newlist;
4921
4922 return cfgerr;
4923error:
William Lallemande718dfb2020-04-10 11:09:25 +02004924 crtlist_entry_free(entry);
William Lallemand97b08102020-04-01 10:14:00 +02004925
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01004926 fclose(f);
William Lallemand2954c472020-03-06 21:54:13 +01004927 crtlist_free(newlist);
4928 return cfgerr;
4929}
4930
4931/* Load a crt-list file, this is done in 2 parts:
4932 * - store the content of the file in a crtlist structure with crtlist_entry structures
4933 * - generate the instances by iterating on entries in the crtlist struct
4934 *
4935 * Nothing is locked there, this function is used in the configuration parser.
4936 *
4937 * Returns a set of ERR_* flags possibly with an error in <err>.
4938 */
William Lallemand6be66ec2020-03-06 22:26:32 +01004939int 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 +01004940{
4941 struct crtlist *crtlist = NULL;
4942 struct ebmb_node *eb;
William Lallemand49398312020-03-30 17:01:33 +02004943 struct crtlist_entry *entry = NULL;
William Lallemand79d31ec2020-03-25 15:10:49 +01004944 struct bind_conf_list *bind_conf_node = NULL;
William Lallemand2954c472020-03-06 21:54:13 +01004945 int cfgerr = 0;
William Lallemand41ca9302020-04-08 13:15:18 +02004946 char *end;
William Lallemand2954c472020-03-06 21:54:13 +01004947
William Lallemand79d31ec2020-03-25 15:10:49 +01004948 bind_conf_node = malloc(sizeof(*bind_conf_node));
4949 if (!bind_conf_node) {
4950 memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : "");
4951 cfgerr |= ERR_FATAL | ERR_ALERT;
4952 goto error;
4953 }
4954 bind_conf_node->next = NULL;
4955 bind_conf_node->bind_conf = bind_conf;
4956
William Lallemand41ca9302020-04-08 13:15:18 +02004957 /* strip trailing slashes, including first one */
4958 for (end = file + strlen(file) - 1; end >= file && *end == '/'; end--)
4959 *end = 0;
4960
William Lallemand2954c472020-03-06 21:54:13 +01004961 /* look for an existing crtlist or create one */
4962 eb = ebst_lookup(&crtlists_tree, file);
4963 if (eb) {
4964 crtlist = ebmb_entry(eb, struct crtlist, node);
4965 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01004966 /* load a crt-list OR a directory */
4967 if (dir)
4968 cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err);
4969 else
4970 cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err);
4971
William Lallemand2954c472020-03-06 21:54:13 +01004972 if (!(cfgerr & ERR_CODE))
4973 ebst_insert(&crtlists_tree, &crtlist->node);
4974 }
4975
4976 if (cfgerr & ERR_CODE) {
4977 cfgerr |= ERR_FATAL | ERR_ALERT;
4978 goto error;
4979 }
4980
4981 /* generates ckch instance from the crtlist_entry */
4982 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
4983 struct ckch_store *store;
4984 struct ckch_inst *ckch_inst = NULL;
4985
4986 store = entry->node.key;
4987 cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err);
4988 if (cfgerr & ERR_CODE) {
4989 memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err);
4990 goto error;
4991 }
William Lallemand49398312020-03-30 17:01:33 +02004992 LIST_ADDQ(&entry->ckch_inst, &ckch_inst->by_crtlist_entry);
William Lallemandcaa16192020-04-08 16:29:15 +02004993 ckch_inst->crtlist_entry = entry;
William Lallemand2954c472020-03-06 21:54:13 +01004994 }
William Lallemand2954c472020-03-06 21:54:13 +01004995
William Lallemand79d31ec2020-03-25 15:10:49 +01004996 /* add the bind_conf to the list */
4997 bind_conf_node->next = crtlist->bind_conf;
4998 crtlist->bind_conf = bind_conf_node;
4999
William Lallemand2954c472020-03-06 21:54:13 +01005000 return cfgerr;
5001error:
5002 {
William Lallemand49398312020-03-30 17:01:33 +02005003 struct crtlist_entry *lastentry;
William Lallemand2954c472020-03-06 21:54:13 +01005004 struct ckch_inst *inst, *s_inst;
5005
William Lallemand49398312020-03-30 17:01:33 +02005006 lastentry = entry; /* which entry we tried to generate last */
5007 if (lastentry) {
5008 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
5009 if (entry == lastentry) /* last entry we tried to generate, no need to go further */
5010 break;
5011
5012 list_for_each_entry_safe(inst, s_inst, &entry->ckch_inst, by_crtlist_entry) {
William Lallemand2954c472020-03-06 21:54:13 +01005013
William Lallemand49398312020-03-30 17:01:33 +02005014 /* this was not generated for this bind_conf, skip */
5015 if (inst->bind_conf != bind_conf)
5016 continue;
5017
William Lallemandd9d5d1b2020-04-09 16:31:05 +02005018 /* free the sni_ctx and instance */
5019 ckch_inst_free(inst);
William Lallemand49398312020-03-30 17:01:33 +02005020 }
William Lallemand2954c472020-03-06 21:54:13 +01005021 }
William Lallemand2954c472020-03-06 21:54:13 +01005022 }
William Lallemand79d31ec2020-03-25 15:10:49 +01005023 free(bind_conf_node);
William Lallemand2954c472020-03-06 21:54:13 +01005024 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01005025 return cfgerr;
5026}
5027
William Lallemand06b22a82020-03-16 14:45:55 +01005028/* Returns a set of ERR_* flags possibly with an error in <err>. */
5029int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
5030{
5031 struct stat buf;
5032 char fp[MAXPATHLEN+1];
5033 int cfgerr = 0;
5034 struct ckch_store *ckchs;
William Lallemand24bde432020-03-09 16:48:43 +01005035 struct ckch_inst *ckch_inst = NULL;
William Lallemand06b22a82020-03-16 14:45:55 +01005036
5037 if ((ckchs = ckchs_lookup(path))) {
5038 /* we found the ckchs in the tree, we can use it directly */
William Lallemand24bde432020-03-09 16:48:43 +01005039 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005040 }
5041 if (stat(path, &buf) == 0) {
5042 if (S_ISDIR(buf.st_mode) == 0) {
5043 ckchs = ckchs_load_cert_file(path, 0, err);
5044 if (!ckchs)
5045 return ERR_ALERT | ERR_FATAL;
5046
William Lallemand24bde432020-03-09 16:48:43 +01005047 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005048 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01005049 return ssl_sock_load_cert_list_file(path, 1, bind_conf, bind_conf->frontend, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005050 }
5051 } else {
5052 /* stat failed, could be a bundle */
5053 if (global_ssl.extra_files & SSL_GF_BUNDLE) {
5054 /* try to load a bundle if it is permitted */
5055 ckchs = ckchs_load_cert_file(path, 1, err);
5056 if (!ckchs)
5057 return ERR_ALERT | ERR_FATAL;
William Lallemand24bde432020-03-09 16:48:43 +01005058 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01005059 } else {
5060 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
5061 err && *err ? *err : "", fp, strerror(errno));
5062 cfgerr |= ERR_ALERT | ERR_FATAL;
5063 }
5064 }
5065
5066 return cfgerr;
5067}
5068
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005069/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005070static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005071ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02005072{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005073 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005074 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02005075 SSL_OP_ALL | /* all known workarounds for bugs */
5076 SSL_OP_NO_SSLv2 |
5077 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02005078 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02005079 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02005080 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02005081 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02005082 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005083 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02005084 SSL_MODE_ENABLE_PARTIAL_WRITE |
5085 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005086 SSL_MODE_RELEASE_BUFFERS |
5087 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02005088 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005089 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005090 int flags = MC_SSL_O_ALL;
5091 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005092
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005093 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005094 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005095
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005096 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005097 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
5098 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5099 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005100 else
5101 flags = conf_ssl_methods->flags;
5102
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02005103 min = conf_ssl_methods->min;
5104 max = conf_ssl_methods->max;
5105 /* start with TLSv10 to remove SSLv3 per default */
5106 if (!min && (!max || max >= CONF_TLSV10))
5107 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005108 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02005109 if (min)
5110 flags |= (methodVersions[min].flag - 1);
5111 if (max)
5112 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005113 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005114 min = max = CONF_TLSV_NONE;
5115 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005116 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005117 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005118 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005119 if (min) {
5120 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005121 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
5122 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5123 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
5124 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005125 hole = 0;
5126 }
5127 max = i;
5128 }
5129 else {
5130 min = max = i;
5131 }
5132 }
5133 else {
5134 if (min)
5135 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005136 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005137 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005138 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5139 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005140 cfgerr += 1;
5141 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005142 /* save real min/max in bind_conf */
5143 conf_ssl_methods->min = min;
5144 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005145
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005146#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005147 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005148 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005149 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005150 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005151 else
5152 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5153 if (flags & methodVersions[i].flag)
5154 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005155#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005156 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005157 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5158 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02005159#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005160
5161 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
5162 options |= SSL_OP_NO_TICKET;
5163 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
5164 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08005165
5166#ifdef SSL_OP_NO_RENEGOTIATION
5167 options |= SSL_OP_NO_RENEGOTIATION;
5168#endif
5169
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005170 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005171
Willy Tarreau5db847a2019-05-09 14:13:35 +02005172#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005173 if (global_ssl.async)
5174 mode |= SSL_MODE_ASYNC;
5175#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005176 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005177 if (global_ssl.life_time)
5178 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005179
5180#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
5181#ifdef OPENSSL_IS_BORINGSSL
5182 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
5183 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Ilya Shipitsine9ff8992020-01-19 12:20:14 +05005184#elif defined(SSL_OP_NO_ANTI_REPLAY)
Olivier Houchard545989f2019-12-17 15:39:54 +01005185 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01005186 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02005187 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
5188 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005189#else
5190 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005191#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02005192 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01005193#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005194 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005195}
5196
William Lallemand4f45bb92017-10-30 20:08:51 +01005197
5198static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
5199{
5200 if (first == block) {
5201 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
5202 if (first->len > 0)
5203 sh_ssl_sess_tree_delete(sh_ssl_sess);
5204 }
5205}
5206
5207/* return first block from sh_ssl_sess */
5208static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
5209{
5210 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
5211
5212}
5213
5214/* store a session into the cache
5215 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
5216 * data: asn1 encoded session
5217 * data_len: asn1 encoded session length
5218 * Returns 1 id session was stored (else 0)
5219 */
5220static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
5221{
5222 struct shared_block *first;
5223 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
5224
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02005225 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01005226 if (!first) {
5227 /* Could not retrieve enough free blocks to store that session */
5228 return 0;
5229 }
5230
5231 /* STORE the key in the first elem */
5232 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
5233 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
5234 first->len = sizeof(struct sh_ssl_sess_hdr);
5235
5236 /* it returns the already existing node
5237 or current node if none, never returns null */
5238 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
5239 if (oldsh_ssl_sess != sh_ssl_sess) {
5240 /* NOTE: Row couldn't be in use because we lock read & write function */
5241 /* release the reserved row */
5242 shctx_row_dec_hot(ssl_shctx, first);
5243 /* replace the previous session already in the tree */
5244 sh_ssl_sess = oldsh_ssl_sess;
5245 /* ignore the previous session data, only use the header */
5246 first = sh_ssl_sess_first_block(sh_ssl_sess);
5247 shctx_row_inc_hot(ssl_shctx, first);
5248 first->len = sizeof(struct sh_ssl_sess_hdr);
5249 }
5250
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02005251 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01005252 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01005253 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01005254 }
5255
5256 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01005257
5258 return 1;
5259}
William Lallemanded0b5ad2017-10-30 19:36:36 +01005260
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005261/* SSL callback used when a new session is created while connecting to a server */
5262static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
5263{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005264 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01005265 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005266
Willy Tarreau07d94e42018-09-20 10:57:52 +02005267 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005268
Olivier Houcharde6060c52017-11-16 17:42:52 +01005269 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
5270 int len;
5271 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005272
Olivier Houcharde6060c52017-11-16 17:42:52 +01005273 len = i2d_SSL_SESSION(sess, NULL);
5274 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
5275 ptr = s->ssl_ctx.reused_sess[tid].ptr;
5276 } else {
5277 free(s->ssl_ctx.reused_sess[tid].ptr);
5278 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
5279 s->ssl_ctx.reused_sess[tid].allocated_size = len;
5280 }
5281 if (s->ssl_ctx.reused_sess[tid].ptr) {
5282 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
5283 &ptr);
5284 }
5285 } else {
5286 free(s->ssl_ctx.reused_sess[tid].ptr);
5287 s->ssl_ctx.reused_sess[tid].ptr = NULL;
5288 }
5289
5290 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01005291}
5292
Olivier Houcharde6060c52017-11-16 17:42:52 +01005293
William Lallemanded0b5ad2017-10-30 19:36:36 +01005294/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01005295int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005296{
5297 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
5298 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
5299 unsigned char *p;
5300 int data_len;
Emeric Bruneb469652019-10-08 18:27:37 +02005301 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005302 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005303
5304 /* Session id is already stored in to key and session id is known
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05005305 * so we don't store it to keep size.
Emeric Bruneb469652019-10-08 18:27:37 +02005306 * note: SSL_SESSION_set1_id is using
5307 * a memcpy so we need to use a different pointer
5308 * than sid_data or sid_ctx_data to avoid valgrind
5309 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01005310 */
5311
5312 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Bruneb469652019-10-08 18:27:37 +02005313
5314 /* copy value in an other buffer */
5315 memcpy(encid, sid_data, sid_length);
5316
5317 /* pad with 0 */
5318 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
5319 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
5320
5321 /* force length to zero to avoid ASN1 encoding */
5322 SSL_SESSION_set1_id(sess, encid, 0);
5323
5324 /* force length to zero to avoid ASN1 encoding */
5325 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005326
5327 /* check if buffer is large enough for the ASN1 encoded session */
5328 data_len = i2d_SSL_SESSION(sess, NULL);
5329 if (data_len > SHSESS_MAX_DATA_LEN)
5330 goto err;
5331
5332 p = encsess;
5333
5334 /* process ASN1 session encoding before the lock */
5335 i2d_SSL_SESSION(sess, &p);
5336
William Lallemanded0b5ad2017-10-30 19:36:36 +01005337
William Lallemanda3c77cf2017-10-30 23:44:40 +01005338 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005339 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005340 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01005341 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005342err:
5343 /* reset original length values */
Emeric Bruneb469652019-10-08 18:27:37 +02005344 SSL_SESSION_set1_id(sess, encid, sid_length);
5345 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01005346
5347 return 0; /* do not increment session reference count */
5348}
5349
5350/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005351SSL_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 +01005352{
William Lallemand4f45bb92017-10-30 20:08:51 +01005353 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005354 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
5355 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01005356 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01005357 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005358
5359 global.shctx_lookups++;
5360
5361 /* allow the session to be freed automatically by openssl */
5362 *do_copy = 0;
5363
5364 /* tree key is zeros padded sessionid */
5365 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
5366 memcpy(tmpkey, key, key_len);
5367 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
5368 key = tmpkey;
5369 }
5370
5371 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005372 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005373
5374 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005375 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
5376 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01005377 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005378 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005379 global.shctx_misses++;
5380 return NULL;
5381 }
5382
William Lallemand4f45bb92017-10-30 20:08:51 +01005383 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
5384 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005385
William Lallemand4f45bb92017-10-30 20:08:51 +01005386 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 +01005387
William Lallemanda3c77cf2017-10-30 23:44:40 +01005388 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005389
5390 /* decode ASN1 session */
5391 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01005392 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01005393 /* Reset session id and session id contenxt */
5394 if (sess) {
5395 SSL_SESSION_set1_id(sess, key, key_len);
5396 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
5397 }
5398
5399 return sess;
5400}
5401
William Lallemand4f45bb92017-10-30 20:08:51 +01005402
William Lallemanded0b5ad2017-10-30 19:36:36 +01005403/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01005404void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005405{
William Lallemand4f45bb92017-10-30 20:08:51 +01005406 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01005407 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
5408 unsigned int sid_length;
5409 const unsigned char *sid_data;
5410 (void)ctx;
5411
5412 sid_data = SSL_SESSION_get_id(sess, &sid_length);
5413 /* tree key is zeros padded sessionid */
5414 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
5415 memcpy(tmpkey, sid_data, sid_length);
5416 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
5417 sid_data = tmpkey;
5418 }
5419
William Lallemanda3c77cf2017-10-30 23:44:40 +01005420 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005421
5422 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005423 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
5424 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01005425 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01005426 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005427 }
5428
5429 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01005430 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005431}
5432
5433/* Set session cache mode to server and disable openssl internal cache.
5434 * Set shared cache callbacks on an ssl context.
5435 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01005436void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01005437{
5438 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
5439
5440 if (!ssl_shctx) {
5441 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
5442 return;
5443 }
5444
5445 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
5446 SSL_SESS_CACHE_NO_INTERNAL |
5447 SSL_SESS_CACHE_NO_AUTO_CLEAR);
5448
5449 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01005450 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
5451 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
5452 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01005453}
5454
William Lallemand8b453912019-11-21 15:48:10 +01005455/*
5456 * This function applies the SSL configuration on a SSL_CTX
5457 * It returns an error code and fills the <err> buffer
5458 */
5459int 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 +01005460{
5461 struct proxy *curproxy = bind_conf->frontend;
5462 int cfgerr = 0;
5463 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01005464 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005465 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005466#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005467 const char *conf_ciphersuites;
5468#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005469 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005470
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005471 if (ssl_conf) {
5472 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
5473 int i, min, max;
5474 int flags = MC_SSL_O_ALL;
5475
5476 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02005477 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
5478 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005479 if (min)
5480 flags |= (methodVersions[min].flag - 1);
5481 if (max)
5482 flags |= ~((methodVersions[max].flag << 1) - 1);
5483 min = max = CONF_TLSV_NONE;
5484 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5485 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
5486 if (min)
5487 max = i;
5488 else
5489 min = max = i;
5490 }
5491 /* save real min/max */
5492 conf_ssl_methods->min = min;
5493 conf_ssl_methods->max = max;
5494 if (!min) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005495 memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
5496 err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005497 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02005498 }
5499 }
5500
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005501 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01005502 case SSL_SOCK_VERIFY_NONE:
5503 verify = SSL_VERIFY_NONE;
5504 break;
5505 case SSL_SOCK_VERIFY_OPTIONAL:
5506 verify = SSL_VERIFY_PEER;
5507 break;
5508 case SSL_SOCK_VERIFY_REQUIRED:
5509 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
5510 break;
5511 }
5512 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
5513 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005514 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 +01005515 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 +01005516 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 +01005517 if (ca_file || ca_verify_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005518 /* set CAfile to verify */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01005519 if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005520 memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01005521 err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005522 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005523 }
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01005524 if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) {
5525 memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n",
5526 err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line);
5527 cfgerr |= ERR_ALERT | ERR_FATAL;
5528 }
5529 if (ca_file && !((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02005530 /* set CA names for client cert request, function returns void */
Emmanuel Hocdet129d3282019-10-24 18:08:51 +02005531 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 +02005532 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005533 }
Emeric Brun850efd52014-01-29 12:24:34 +01005534 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01005535 memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
5536 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005537 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun850efd52014-01-29 12:24:34 +01005538 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005539#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005540 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02005541 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
5542
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01005543 if (!ssl_set_cert_crl_file(store, crl_file)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005544 memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
5545 err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005546 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02005547 }
Emeric Brun561e5742012-10-02 15:20:55 +02005548 else {
5549 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
5550 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02005551 }
Emeric Brun051cdab2012-10-02 19:25:50 +02005552#endif
Emeric Brun644cde02012-12-14 11:21:13 +01005553 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02005554 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005555#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02005556 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005557 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005558 memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
5559 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005560 cfgerr |= ERR_ALERT | ERR_FATAL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01005561 }
5562 }
5563#endif
5564
William Lallemand4f45bb92017-10-30 20:08:51 +01005565 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005566 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
5567 if (conf_ciphers &&
5568 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005569 memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
5570 err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005571 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02005572 }
5573
Emmanuel Hocdet839af572019-05-14 16:27:35 +02005574#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005575 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
5576 if (conf_ciphersuites &&
5577 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005578 memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
5579 err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005580 cfgerr |= ERR_ALERT | ERR_FATAL;
Dirkjan Bussink415150f2018-09-14 11:14:21 +02005581 }
5582#endif
5583
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01005584#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02005585 /* If tune.ssl.default-dh-param has not been set,
5586 neither has ssl-default-dh-file and no static DH
5587 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01005588 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02005589 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02005590 (ssl_dh_ptr_index == -1 ||
5591 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Willy Tarreau3ba77d22020-05-08 09:31:18 +02005592 /* default to dh-param 2048 */
5593 global_ssl.default_dh_param = 2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005594 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005595
Willy Tarreauef934602016-12-22 23:12:01 +01005596 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005597 if (local_dh_1024 == NULL) {
5598 local_dh_1024 = ssl_get_dh_1024();
5599 }
Willy Tarreauef934602016-12-22 23:12:01 +01005600 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005601 if (local_dh_2048 == NULL) {
5602 local_dh_2048 = ssl_get_dh_2048();
5603 }
Willy Tarreauef934602016-12-22 23:12:01 +01005604 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02005605 if (local_dh_4096 == NULL) {
5606 local_dh_4096 = ssl_get_dh_4096();
5607 }
Remi Gacogne8de54152014-07-15 11:36:40 +02005608 }
5609 }
5610 }
5611#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02005612
Emeric Brunfc0421f2012-09-07 17:30:07 +02005613 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005614#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02005615 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02005616#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02005617
Bernard Spil13c53f82018-02-15 13:34:58 +01005618#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005619 ssl_conf_cur = NULL;
5620 if (ssl_conf && ssl_conf->npn_str)
5621 ssl_conf_cur = ssl_conf;
5622 else if (bind_conf->ssl_conf.npn_str)
5623 ssl_conf_cur = &bind_conf->ssl_conf;
5624 if (ssl_conf_cur)
5625 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02005626#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01005627#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005628 ssl_conf_cur = NULL;
5629 if (ssl_conf && ssl_conf->alpn_str)
5630 ssl_conf_cur = ssl_conf;
5631 else if (bind_conf->ssl_conf.alpn_str)
5632 ssl_conf_cur = &bind_conf->ssl_conf;
5633 if (ssl_conf_cur)
5634 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02005635#endif
Lukas Tribusd14b49c2019-11-24 18:20:40 +01005636#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005637 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
5638 if (conf_curves) {
5639 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005640 memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
5641 err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005642 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005643 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01005644 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005645 }
5646#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005647#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01005648 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02005649 int i;
5650 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005651#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005652 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02005653 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5654 NULL);
5655
5656 if (ecdhe == NULL) {
Eric Salama3c8bde82019-11-20 11:33:40 +01005657 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005658 return cfgerr;
5659 }
5660#else
5661 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
5662 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
5663 ECDHE_DEFAULT_CURVE);
5664#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02005665
Emmanuel Hocdet98263292016-12-29 18:26:15 +01005666 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02005667 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01005668 memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
5669 err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01005670 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun2b58d042012-09-20 17:10:03 +02005671 }
5672 else {
5673 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
5674 EC_KEY_free(ecdh);
5675 }
5676 }
5677#endif
5678
Emeric Brunfc0421f2012-09-07 17:30:07 +02005679 return cfgerr;
5680}
5681
Evan Broderbe554312013-06-27 00:05:25 -07005682static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
5683{
5684 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
5685 size_t prefixlen, suffixlen;
5686
5687 /* Trivial case */
5688 if (strcmp(pattern, hostname) == 0)
5689 return 1;
5690
Evan Broderbe554312013-06-27 00:05:25 -07005691 /* The rest of this logic is based on RFC 6125, section 6.4.3
5692 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
5693
Emeric Bruna848dae2013-10-08 11:27:28 +02005694 pattern_wildcard = NULL;
5695 pattern_left_label_end = pattern;
5696 while (*pattern_left_label_end != '.') {
5697 switch (*pattern_left_label_end) {
5698 case 0:
5699 /* End of label not found */
5700 return 0;
5701 case '*':
5702 /* If there is more than one wildcards */
5703 if (pattern_wildcard)
5704 return 0;
5705 pattern_wildcard = pattern_left_label_end;
5706 break;
5707 }
5708 pattern_left_label_end++;
5709 }
5710
5711 /* If it's not trivial and there is no wildcard, it can't
5712 * match */
5713 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07005714 return 0;
5715
5716 /* Make sure all labels match except the leftmost */
5717 hostname_left_label_end = strchr(hostname, '.');
5718 if (!hostname_left_label_end
5719 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
5720 return 0;
5721
5722 /* Make sure the leftmost label of the hostname is long enough
5723 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02005724 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07005725 return 0;
5726
5727 /* Finally compare the string on either side of the
5728 * wildcard */
5729 prefixlen = pattern_wildcard - pattern;
5730 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02005731 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
5732 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07005733 return 0;
5734
5735 return 1;
5736}
5737
5738static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
5739{
5740 SSL *ssl;
5741 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005742 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005743 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02005744 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07005745
5746 int depth;
5747 X509 *cert;
5748 STACK_OF(GENERAL_NAME) *alt_names;
5749 int i;
5750 X509_NAME *cert_subject;
5751 char *str;
5752
5753 if (ok == 0)
5754 return ok;
5755
5756 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02005757 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005758 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07005759
Willy Tarreauad92a9a2017-07-28 11:38:41 +02005760 /* We're checking if the provided hostnames match the desired one. The
5761 * desired hostname comes from the SNI we presented if any, or if not
5762 * provided then it may have been explicitly stated using a "verifyhost"
5763 * directive. If neither is set, we don't care about the name so the
5764 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02005765 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005766 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02005767 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005768 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02005769 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02005770 if (!servername)
5771 return ok;
5772 }
Evan Broderbe554312013-06-27 00:05:25 -07005773
5774 /* We only need to verify the CN on the actual server cert,
5775 * not the indirect CAs */
5776 depth = X509_STORE_CTX_get_error_depth(ctx);
5777 if (depth != 0)
5778 return ok;
5779
5780 /* At this point, the cert is *not* OK unless we can find a
5781 * hostname match */
5782 ok = 0;
5783
5784 cert = X509_STORE_CTX_get_current_cert(ctx);
5785 /* It seems like this might happen if verify peer isn't set */
5786 if (!cert)
5787 return ok;
5788
5789 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
5790 if (alt_names) {
5791 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
5792 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
5793 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005794#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02005795 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
5796#else
Evan Broderbe554312013-06-27 00:05:25 -07005797 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02005798#endif
Evan Broderbe554312013-06-27 00:05:25 -07005799 ok = ssl_sock_srv_hostcheck(str, servername);
5800 OPENSSL_free(str);
5801 }
5802 }
5803 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02005804 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07005805 }
5806
5807 cert_subject = X509_get_subject_name(cert);
5808 i = -1;
5809 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
5810 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005811 ASN1_STRING *value;
5812 value = X509_NAME_ENTRY_get_data(entry);
5813 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07005814 ok = ssl_sock_srv_hostcheck(str, servername);
5815 OPENSSL_free(str);
5816 }
5817 }
5818
Willy Tarreau71d058c2017-07-26 20:09:56 +02005819 /* report the mismatch and indicate if SNI was used or not */
5820 if (!ok && !conn->err_code)
5821 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07005822 return ok;
5823}
5824
Emeric Brun94324a42012-10-11 14:00:19 +02005825/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01005826int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02005827{
Willy Tarreau03209342016-12-22 17:08:28 +01005828 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02005829 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005830 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02005831 SSL_OP_ALL | /* all known workarounds for bugs */
5832 SSL_OP_NO_SSLv2 |
5833 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02005834 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02005835 SSL_MODE_ENABLE_PARTIAL_WRITE |
5836 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01005837 SSL_MODE_RELEASE_BUFFERS |
5838 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01005839 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005840 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005841 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005842 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005843 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02005844
Thierry Fournier383085f2013-01-24 14:15:43 +01005845 /* Make sure openssl opens /dev/urandom before the chroot */
5846 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005847 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01005848 cfgerr++;
5849 }
5850
Willy Tarreaufce03112015-01-15 21:32:40 +01005851 /* Automatic memory computations need to know we use SSL there */
5852 global.ssl_used_backend = 1;
5853
5854 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02005855 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005856 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005857 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
5858 curproxy->id, srv->id,
5859 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02005860 cfgerr++;
5861 return cfgerr;
5862 }
5863 }
Christopher Fauletf61f33a2020-03-27 18:55:49 +01005864 if (srv->use_ssl == 1)
Emeric Brun94324a42012-10-11 14:00:19 +02005865 srv->xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02005866
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005867 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005868 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005869 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
5870 proxy_type_str(curproxy), curproxy->id,
5871 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02005872 cfgerr++;
5873 return cfgerr;
5874 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005875
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005876 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01005877 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
5878 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5879 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005880 else
5881 flags = conf_ssl_methods->flags;
5882
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005883 /* Real min and max should be determinate with configuration and openssl's capabilities */
5884 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005885 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005886 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005887 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005888
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005889 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005890 min = max = CONF_TLSV_NONE;
5891 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005892 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005893 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005894 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005895 if (min) {
5896 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005897 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
5898 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
5899 proxy_type_str(curproxy), curproxy->id, srv->id,
5900 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005901 hole = 0;
5902 }
5903 max = i;
5904 }
5905 else {
5906 min = max = i;
5907 }
5908 }
5909 else {
5910 if (min)
5911 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005912 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005913 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005914 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
5915 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005916 cfgerr += 1;
5917 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005918
Willy Tarreau9a1ab082019-05-09 13:26:41 +02005919#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005920 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08005921 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005922 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005923 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02005924 else
5925 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
5926 if (flags & methodVersions[i].flag)
5927 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005928#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02005929 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02005930 methodVersions[min].ctx_set_version(ctx, SET_MIN);
5931 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02005932#endif
5933
5934 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
5935 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005936 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005937
Willy Tarreau5db847a2019-05-09 14:13:35 +02005938#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005939 if (global_ssl.async)
5940 mode |= SSL_MODE_ASYNC;
5941#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01005942 SSL_CTX_set_mode(ctx, mode);
5943 srv->ssl_ctx.ctx = ctx;
5944
Emeric Bruna7aa3092012-10-26 12:58:00 +02005945 if (srv->ssl_ctx.client_crt) {
5946 if (SSL_CTX_use_PrivateKey_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt, SSL_FILETYPE_PEM) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005947 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
5948 proxy_type_str(curproxy), curproxy->id,
5949 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005950 cfgerr++;
5951 }
5952 else if (SSL_CTX_use_certificate_chain_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005953 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
5954 proxy_type_str(curproxy), curproxy->id,
5955 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005956 cfgerr++;
5957 }
5958 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01005959 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
5960 proxy_type_str(curproxy), curproxy->id,
5961 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02005962 cfgerr++;
5963 }
5964 }
Emeric Brun94324a42012-10-11 14:00:19 +02005965
Emeric Brun850efd52014-01-29 12:24:34 +01005966 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
5967 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01005968 switch (srv->ssl_ctx.verify) {
5969 case SSL_SOCK_VERIFY_NONE:
5970 verify = SSL_VERIFY_NONE;
5971 break;
5972 case SSL_SOCK_VERIFY_REQUIRED:
5973 verify = SSL_VERIFY_PEER;
5974 break;
5975 }
Evan Broderbe554312013-06-27 00:05:25 -07005976 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01005977 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02005978 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01005979 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02005980 if (srv->ssl_ctx.ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02005981 /* set CAfile to verify */
5982 if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) {
5983 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to set CA file '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01005984 curproxy->id, srv->id,
5985 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02005986 cfgerr++;
5987 }
5988 }
Emeric Brun850efd52014-01-29 12:24:34 +01005989 else {
5990 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01005991 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled by default but no CA file specified. If you're running on a LAN where you're certain to trust the server's certificate, please set an explicit 'verify none' statement on the 'server' line, or use 'ssl-server-verify none' in the global section to disable server-side verifications by default.\n",
5992 curproxy->id, srv->id,
5993 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01005994 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01005995 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
5996 curproxy->id, srv->id,
5997 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01005998 cfgerr++;
5999 }
Emeric Brunef42d922012-10-11 16:11:36 +02006000#ifdef X509_V_FLAG_CRL_CHECK
6001 if (srv->ssl_ctx.crl_file) {
6002 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
6003
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01006004 if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006005 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
6006 curproxy->id, srv->id,
6007 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02006008 cfgerr++;
6009 }
6010 else {
6011 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
6012 }
6013 }
6014#endif
6015 }
6016
Olivier Houchardbd84ac82017-11-03 13:43:35 +01006017 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
6018 SSL_SESS_CACHE_NO_INTERNAL_STORE);
6019 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02006020 if (srv->ssl_ctx.ciphers &&
6021 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006022 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
6023 curproxy->id, srv->id,
6024 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02006025 cfgerr++;
6026 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006027
Emmanuel Hocdet839af572019-05-14 16:27:35 +02006028#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006029 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00006030 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02006031 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
6032 curproxy->id, srv->id,
6033 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
6034 cfgerr++;
6035 }
6036#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01006037#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
6038 if (srv->ssl_ctx.npn_str)
6039 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
6040#endif
6041#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
6042 if (srv->ssl_ctx.alpn_str)
6043 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
6044#endif
6045
Emeric Brun94324a42012-10-11 14:00:19 +02006046
6047 return cfgerr;
6048}
6049
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006050/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02006051 * be NULL, in which case nothing is done. Returns the number of errors
6052 * encountered.
6053 */
Willy Tarreau03209342016-12-22 17:08:28 +01006054int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02006055{
6056 struct ebmb_node *node;
6057 struct sni_ctx *sni;
6058 int err = 0;
William Lallemand8b453912019-11-21 15:48:10 +01006059 int errcode = 0;
6060 char *errmsg = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02006061
Willy Tarreaufce03112015-01-15 21:32:40 +01006062 /* Automatic memory computations need to know we use SSL there */
6063 global.ssl_used_frontend = 1;
6064
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006065 /* Make sure openssl opens /dev/urandom before the chroot */
6066 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006067 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006068 err++;
6069 }
6070 /* Create initial_ctx used to start the ssl connection before do switchctx */
6071 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02006072 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006073 /* It should not be necessary to call this function, but it's
6074 necessary first to check and move all initialisation related
6075 to initial_ctx in ssl_sock_initial_ctx. */
William Lallemand8b453912019-11-21 15:48:10 +01006076 errcode |= ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx, &errmsg);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006077 }
Emeric Brun0bed9942014-10-30 19:25:24 +01006078 if (bind_conf->default_ctx)
William Lallemand8b453912019-11-21 15:48:10 +01006079 errcode |= ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, &errmsg);
Emeric Brun0bed9942014-10-30 19:25:24 +01006080
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006081 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006082 while (node) {
6083 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01006084 if (!sni->order && sni->ctx != bind_conf->default_ctx)
6085 /* only initialize the CTX on its first occurrence and
6086 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01006087 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006088 node = ebmb_next(node);
6089 }
6090
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006091 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006092 while (node) {
6093 sni = ebmb_entry(node, struct sni_ctx, name);
William Lallemand8b453912019-11-21 15:48:10 +01006094 if (!sni->order && sni->ctx != bind_conf->default_ctx) {
Emeric Brun0bed9942014-10-30 19:25:24 +01006095 /* only initialize the CTX on its first occurrence and
6096 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01006097 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
6098 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006099 node = ebmb_next(node);
6100 }
William Lallemand8b453912019-11-21 15:48:10 +01006101
6102 if (errcode & ERR_WARN) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01006103 ha_warning("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01006104 } else if (errcode & ERR_CODE) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01006105 ha_alert("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01006106 err++;
6107 }
6108
6109 free(errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006110 return err;
6111}
6112
Willy Tarreau55d37912016-12-21 23:38:39 +01006113/* Prepares all the contexts for a bind_conf and allocates the shared SSL
6114 * context if needed. Returns < 0 on error, 0 on success. The warnings and
6115 * alerts are directly emitted since the rest of the stack does it below.
6116 */
6117int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
6118{
6119 struct proxy *px = bind_conf->frontend;
6120 int alloc_ctx;
6121 int err;
6122
6123 if (!bind_conf->is_ssl) {
6124 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006125 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
6126 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01006127 }
6128 return 0;
6129 }
6130 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006131 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006132 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
6133 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006134 }
6135 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006136 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
6137 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02006138 return -1;
6139 }
Willy Tarreau55d37912016-12-21 23:38:39 +01006140 }
William Lallemandc61c0b32017-12-04 18:46:39 +01006141 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01006142 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02006143 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01006144 sizeof(*sh_ssl_sess_tree),
6145 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02006146 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01006147 if (alloc_ctx == SHCTX_E_INIT_LOCK)
6148 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");
6149 else
6150 ha_alert("Unable to allocate SSL session cache.\n");
6151 return -1;
6152 }
6153 /* free block callback */
6154 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
6155 /* init the root tree within the extra space */
6156 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
6157 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01006158 }
Willy Tarreau55d37912016-12-21 23:38:39 +01006159 err = 0;
6160 /* initialize all certificate contexts */
6161 err += ssl_sock_prepare_all_ctx(bind_conf);
6162
6163 /* initialize CA variables if the certificates generation is enabled */
6164 err += ssl_sock_load_ca(bind_conf);
6165
6166 return -err;
6167}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02006168
6169/* release ssl context allocated for servers. */
6170void ssl_sock_free_srv_ctx(struct server *srv)
6171{
Olivier Houchardc7566002018-11-20 23:33:50 +01006172#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
6173 if (srv->ssl_ctx.alpn_str)
6174 free(srv->ssl_ctx.alpn_str);
6175#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01006176#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01006177 if (srv->ssl_ctx.npn_str)
6178 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01006179#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02006180 if (srv->ssl_ctx.ctx)
6181 SSL_CTX_free(srv->ssl_ctx.ctx);
6182}
6183
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006184/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02006185 * be NULL, in which case nothing is done. The default_ctx is nullified too.
6186 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006187void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02006188{
6189 struct ebmb_node *node, *back;
6190 struct sni_ctx *sni;
6191
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006192 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006193 while (node) {
6194 sni = ebmb_entry(node, struct sni_ctx, name);
6195 back = ebmb_next(node);
6196 ebmb_delete(node);
William Lallemand02e19a52020-04-08 16:11:26 +02006197 SSL_CTX_free(sni->ctx);
6198 if (!sni->order) { /* only free the CTX conf on its first occurrence */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006199 ssl_sock_free_ssl_conf(sni->conf);
6200 free(sni->conf);
6201 sni->conf = NULL;
6202 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006203 free(sni);
6204 node = back;
6205 }
6206
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006207 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02006208 while (node) {
6209 sni = ebmb_entry(node, struct sni_ctx, name);
6210 back = ebmb_next(node);
6211 ebmb_delete(node);
William Lallemand02e19a52020-04-08 16:11:26 +02006212 SSL_CTX_free(sni->ctx);
6213 if (!sni->order) { /* only free the SSL conf its first occurrence */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006214 ssl_sock_free_ssl_conf(sni->conf);
6215 free(sni->conf);
6216 sni->conf = NULL;
6217 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02006218 free(sni);
6219 node = back;
6220 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01006221 SSL_CTX_free(bind_conf->initial_ctx);
6222 bind_conf->initial_ctx = NULL;
William Lallemand02e19a52020-04-08 16:11:26 +02006223 SSL_CTX_free(bind_conf->default_ctx);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02006224 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006225 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02006226}
6227
Willy Tarreau795cdab2016-12-22 17:30:54 +01006228/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
6229void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
6230{
6231 ssl_sock_free_ca(bind_conf);
6232 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01006233 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01006234 free(bind_conf->ca_sign_file);
6235 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02006236 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01006237 free(bind_conf->keys_ref->filename);
6238 free(bind_conf->keys_ref->tlskeys);
6239 LIST_DEL(&bind_conf->keys_ref->list);
6240 free(bind_conf->keys_ref);
6241 }
6242 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01006243 bind_conf->ca_sign_pass = NULL;
6244 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01006245}
6246
Christopher Faulet31af49d2015-06-09 17:29:50 +02006247/* Load CA cert file and private key used to generate certificates */
6248int
Willy Tarreau03209342016-12-22 17:08:28 +01006249ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02006250{
Willy Tarreau03209342016-12-22 17:08:28 +01006251 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006252 FILE *fp;
6253 X509 *cacert = NULL;
6254 EVP_PKEY *capkey = NULL;
6255 int err = 0;
6256
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02006257 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02006258 return err;
6259
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006260#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02006261 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01006262 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02006263 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02006264 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02006265 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02006266#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02006267
Christopher Faulet31af49d2015-06-09 17:29:50 +02006268 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006269 ha_alert("Proxy '%s': cannot enable certificate generation, "
6270 "no CA certificate File configured at [%s:%d].\n",
6271 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006272 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006273 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02006274
6275 /* read in the CA certificate */
6276 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006277 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
6278 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006279 goto load_error;
6280 }
6281 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006282 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
6283 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006284 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006285 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006286 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006287 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01006288 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
6289 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006290 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006291 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02006292
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006293 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006294 bind_conf->ca_sign_cert = cacert;
6295 bind_conf->ca_sign_pkey = capkey;
6296 return err;
6297
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006298 read_error:
6299 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02006300 if (capkey) EVP_PKEY_free(capkey);
6301 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02006302 load_error:
6303 bind_conf->generate_certs = 0;
6304 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006305 return err;
6306}
6307
6308/* Release CA cert and private key used to generate certificated */
6309void
6310ssl_sock_free_ca(struct bind_conf *bind_conf)
6311{
Christopher Faulet31af49d2015-06-09 17:29:50 +02006312 if (bind_conf->ca_sign_pkey)
6313 EVP_PKEY_free(bind_conf->ca_sign_pkey);
6314 if (bind_conf->ca_sign_cert)
6315 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01006316 bind_conf->ca_sign_pkey = NULL;
6317 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02006318}
6319
Emeric Brun46591952012-05-18 15:47:34 +02006320/*
6321 * This function is called if SSL * context is not yet allocated. The function
6322 * is designed to be called before any other data-layer operation and sets the
6323 * handshake flag on the connection. It is safe to call it multiple times.
6324 * It returns 0 on success and -1 in error case.
6325 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006326static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006327{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006328 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006329 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006330 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006331 return 0;
6332
Willy Tarreau3c728722014-01-23 13:50:42 +01006333 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006334 return 0;
6335
Olivier Houchard66ab4982019-02-26 18:37:15 +01006336 ctx = pool_alloc(ssl_sock_ctx_pool);
6337 if (!ctx) {
6338 conn->err_code = CO_ER_SSL_NO_MEM;
6339 return -1;
6340 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006341 ctx->wait_event.tasklet = tasklet_new();
6342 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006343 conn->err_code = CO_ER_SSL_NO_MEM;
6344 pool_free(ssl_sock_ctx_pool, ctx);
6345 return -1;
6346 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006347 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
6348 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006349 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006350 ctx->sent_early_data = 0;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006351 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02006352 ctx->conn = conn;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006353 ctx->subs = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02006354 ctx->xprt_st = 0;
6355 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02006356
6357 /* Only work with sockets for now, this should be adapted when we'll
6358 * add QUIC support.
6359 */
6360 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02006361 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006362 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
6363 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02006364 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006365
Willy Tarreau20879a02012-12-03 16:32:10 +01006366 if (global.maxsslconn && sslconns >= global.maxsslconn) {
6367 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006368 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006369 }
Willy Tarreau403edff2012-09-06 11:58:37 +02006370
Emeric Brun46591952012-05-18 15:47:34 +02006371 /* If it is in client mode initiate SSL session
6372 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006373 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006374 int may_retry = 1;
6375
6376 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02006377 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006378 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
6379 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006380 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006381 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006382 goto retry_connect;
6383 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006384 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006385 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006386 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006387 ctx->bio = BIO_new(ha_meth);
6388 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006389 SSL_free(ctx->ssl);
6390 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006391 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006392 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006393 goto retry_connect;
6394 }
Emeric Brun55476152014-11-12 17:35:37 +01006395 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006396 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006397 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006398 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006399 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006400
Evan Broderbe554312013-06-27 00:05:25 -07006401 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006402 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6403 SSL_free(ctx->ssl);
6404 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01006405 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006406 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006407 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006408 goto retry_connect;
6409 }
Emeric Brun55476152014-11-12 17:35:37 +01006410 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006411 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006412 }
6413
Olivier Houchard66ab4982019-02-26 18:37:15 +01006414 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02006415 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6416 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
6417 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 +01006418 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01006419 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02006420 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6421 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01006422 } else if (sess) {
6423 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01006424 }
6425 }
Evan Broderbe554312013-06-27 00:05:25 -07006426
Emeric Brun46591952012-05-18 15:47:34 +02006427 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006428 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02006429
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006430 _HA_ATOMIC_ADD(&sslconns, 1);
6431 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006432 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006433 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006434 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006435 return 0;
6436 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006437 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006438 int may_retry = 1;
6439
6440 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02006441 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006442 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
6443 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006444 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006445 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006446 goto retry_accept;
6447 }
Willy Tarreau20879a02012-12-03 16:32:10 +01006448 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006449 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01006450 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006451 ctx->bio = BIO_new(ha_meth);
6452 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01006453 SSL_free(ctx->ssl);
6454 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006455 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006456 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006457 goto retry_accept;
6458 }
Emeric Brun55476152014-11-12 17:35:37 +01006459 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006460 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006461 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02006462 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02006463 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02006464
Emeric Brune1f38db2012-09-03 20:36:47 +02006465 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006466 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
6467 SSL_free(ctx->ssl);
6468 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006469 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006470 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01006471 goto retry_accept;
6472 }
Emeric Brun55476152014-11-12 17:35:37 +01006473 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006474 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01006475 }
6476
Frédéric Lécaille3139c1b2020-01-24 14:56:18 +01006477#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6478 if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) {
6479 b_alloc(&ctx->early_buf);
6480 SSL_set_max_early_data(ctx->ssl,
6481 /* Only allow early data if we managed to allocate
6482 * a buffer.
6483 */
6484 (!b_is_null(&ctx->early_buf)) ?
6485 global.tune.bufsize - global.tune.maxrewrite : 0);
6486 }
6487#endif
6488
Olivier Houchard66ab4982019-02-26 18:37:15 +01006489 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02006490
Emeric Brun46591952012-05-18 15:47:34 +02006491 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02006492 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006493#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02006494 conn->flags |= CO_FL_EARLY_SSL_HS;
6495#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02006496
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01006497 _HA_ATOMIC_ADD(&sslconns, 1);
6498 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006499 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006500 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006501 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02006502 return 0;
6503 }
6504 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01006505 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006506err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02006507 if (ctx && ctx->wait_event.tasklet)
6508 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01006509 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02006510 return -1;
6511}
6512
6513
6514/* This is the callback which is used when an SSL handshake is pending. It
6515 * updates the FD status if it wants some polling before being called again.
6516 * It returns 0 if it fails in a fatal way or needs to poll to go further,
6517 * otherwise it returns non-zero and removes itself from the connection's
6518 * flags (the bit is provided in <flag> by the caller).
6519 */
Olivier Houchard000694c2019-05-23 14:45:12 +02006520static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02006521{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006522 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02006523 int ret;
6524
Willy Tarreau3c728722014-01-23 13:50:42 +01006525 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02006526 return 0;
6527
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02006528 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006529 goto out_error;
6530
Willy Tarreau5db847a2019-05-09 14:13:35 +02006531#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02006532 /*
6533 * Check if we have early data. If we do, we have to read them
6534 * before SSL_do_handshake() is called, And there's no way to
6535 * detect early data, except to try to read them
6536 */
6537 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchard54907bb2019-12-19 15:02:39 +01006538 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02006539
Olivier Houchard54907bb2019-12-19 15:02:39 +01006540 while (1) {
6541 ret = SSL_read_early_data(ctx->ssl,
6542 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
6543 &read_data);
6544 if (ret == SSL_READ_EARLY_DATA_ERROR)
6545 goto check_error;
6546 if (read_data > 0) {
6547 conn->flags |= CO_FL_EARLY_DATA;
6548 b_add(&ctx->early_buf, read_data);
6549 }
6550 if (ret == SSL_READ_EARLY_DATA_FINISH) {
6551 conn->flags &= ~CO_FL_EARLY_SSL_HS;
6552 if (!b_data(&ctx->early_buf))
6553 b_free(&ctx->early_buf);
6554 break;
6555 }
6556 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02006557 }
6558#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006559 /* If we use SSL_do_handshake to process a reneg initiated by
6560 * the remote peer, it sometimes returns SSL_ERROR_SSL.
6561 * Usually SSL_write and SSL_read are used and process implicitly
6562 * the reneg handshake.
6563 * Here we use SSL_peek as a workaround for reneg.
6564 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01006565 if (!(conn->flags & CO_FL_WAIT_L6_CONN) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006566 char c;
6567
Olivier Houchard66ab4982019-02-26 18:37:15 +01006568 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01006569 if (ret <= 0) {
6570 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006571 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006572
Emeric Brun674b7432012-11-08 19:21:55 +01006573 if (ret == SSL_ERROR_WANT_WRITE) {
6574 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006575 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006576 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006577 return 0;
6578 }
6579 else if (ret == SSL_ERROR_WANT_READ) {
6580 /* handshake may have been completed but we have
6581 * no more data to read.
6582 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006583 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01006584 ret = 1;
6585 goto reneg_ok;
6586 }
6587 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006588 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006589 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01006590 return 0;
6591 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006592#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006593 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006594 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006595 return 0;
6596 }
6597#endif
Emeric Brun674b7432012-11-08 19:21:55 +01006598 else if (ret == SSL_ERROR_SYSCALL) {
6599 /* if errno is null, then connection was successfully established */
6600 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6601 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01006602 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006603#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6604 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006605 conn->err_code = CO_ER_SSL_HANDSHAKE;
6606#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006607 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006608#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006609 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006610 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006611 empty_handshake = state == TLS_ST_BEFORE;
6612#else
Lukas Tribus49799162019-07-08 14:29:15 +02006613 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6614 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006615#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006616 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02006617 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006618 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006619 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6620 else
6621 conn->err_code = CO_ER_SSL_EMPTY;
6622 }
6623 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006624 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006625 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6626 else
6627 conn->err_code = CO_ER_SSL_ABORT;
6628 }
6629 }
6630 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006631 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006632 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01006633 else
Emeric Brun29f037d2014-04-25 19:05:36 +02006634 conn->err_code = CO_ER_SSL_HANDSHAKE;
6635 }
Lukas Tribus49799162019-07-08 14:29:15 +02006636#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01006637 }
Emeric Brun674b7432012-11-08 19:21:55 +01006638 goto out_error;
6639 }
6640 else {
6641 /* Fail on all other handshake errors */
6642 /* Note: OpenSSL may leave unread bytes in the socket's
6643 * buffer, causing an RST to be emitted upon close() on
6644 * TCP sockets. We first try to drain possibly pending
6645 * data to avoid this as much as possible.
6646 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006647 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006648 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006649 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006650 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01006651 goto out_error;
6652 }
6653 }
6654 /* read some data: consider handshake completed */
6655 goto reneg_ok;
6656 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01006657 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02006658check_error:
Emeric Brun46591952012-05-18 15:47:34 +02006659 if (ret != 1) {
6660 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006661 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006662
6663 if (ret == SSL_ERROR_WANT_WRITE) {
6664 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02006665 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006666 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006667 return 0;
6668 }
6669 else if (ret == SSL_ERROR_WANT_READ) {
6670 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02006671 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02006672 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
6673 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02006674 return 0;
6675 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006676#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006677 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02006678 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00006679 return 0;
6680 }
6681#endif
Willy Tarreau89230192012-09-28 20:22:13 +02006682 else if (ret == SSL_ERROR_SYSCALL) {
6683 /* if errno is null, then connection was successfully established */
6684 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
6685 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006686 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02006687#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
6688 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006689 conn->err_code = CO_ER_SSL_HANDSHAKE;
6690#else
6691 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02006692#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02006693 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006694 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006695 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006696#else
Lukas Tribus49799162019-07-08 14:29:15 +02006697 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
6698 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006699#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006700 if (empty_handshake) {
6701 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006702 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006703 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6704 else
6705 conn->err_code = CO_ER_SSL_EMPTY;
6706 }
6707 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006708 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006709 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6710 else
6711 conn->err_code = CO_ER_SSL_ABORT;
6712 }
Emeric Brun29f037d2014-04-25 19:05:36 +02006713 }
6714 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006715 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02006716 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
6717 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01006718 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02006719 }
Lukas Tribus49799162019-07-08 14:29:15 +02006720#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02006721 }
Willy Tarreau89230192012-09-28 20:22:13 +02006722 goto out_error;
6723 }
Emeric Brun46591952012-05-18 15:47:34 +02006724 else {
6725 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02006726 /* Note: OpenSSL may leave unread bytes in the socket's
6727 * buffer, causing an RST to be emitted upon close() on
6728 * TCP sockets. We first try to drain possibly pending
6729 * data to avoid this as much as possible.
6730 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01006731 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01006732 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006733 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02006734 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006735 goto out_error;
6736 }
6737 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02006738#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01006739 else {
6740 /*
6741 * If the server refused the early data, we have to send a
6742 * 425 to the client, as we no longer have the data to sent
6743 * them again.
6744 */
6745 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006746 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01006747 conn->err_code = CO_ER_SSL_EARLY_FAILED;
6748 goto out_error;
6749 }
6750 }
6751 }
6752#endif
6753
Emeric Brun46591952012-05-18 15:47:34 +02006754
Emeric Brun674b7432012-11-08 19:21:55 +01006755reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00006756
Willy Tarreau5db847a2019-05-09 14:13:35 +02006757#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00006758 /* ASYNC engine API doesn't support moving read/write
6759 * buffers. So we disable ASYNC mode right after
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05006760 * the handshake to avoid buffer overflow.
Emeric Brunb5e42a82017-06-06 12:35:14 +00006761 */
6762 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006763 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00006764#endif
Emeric Brun46591952012-05-18 15:47:34 +02006765 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006766 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006767 if (objt_server(conn->target)) {
6768 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
6769 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
6770 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02006771 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02006772 else {
6773 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
6774 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
6775 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
6776 }
Emeric Brun46591952012-05-18 15:47:34 +02006777 }
6778
6779 /* The connection is now established at both layers, it's time to leave */
6780 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
6781 return 1;
6782
6783 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01006784 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02006785 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01006786 ERR_clear_error();
6787
Emeric Brun9fa89732012-10-04 17:09:56 +02006788 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02006789 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
6790 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
6791 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02006792 }
6793
Emeric Brun46591952012-05-18 15:47:34 +02006794 /* Fail on all other handshake errors */
6795 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01006796 if (!conn->err_code)
6797 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02006798 return 0;
6799}
6800
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006801/* Called from the upper layer, to subscribe <es> to events <event_type>. The
6802 * event subscriber <es> is not allowed to change from a previous call as long
6803 * as at least one event is still subscribed. The <event_type> must only be a
6804 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0,
6805 * unless the transport layer was already released.
6806 */
6807static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006808{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006809 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006810
Olivier Houchard0ff28652019-06-24 18:57:39 +02006811 if (!ctx)
6812 return -1;
6813
Willy Tarreau113d52b2020-01-10 09:20:26 +01006814 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006815 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006816
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006817 ctx->subs = es;
6818 es->events |= event_type;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006819
6820 /* we may have to subscribe to lower layers for new events */
6821 event_type &= ~ctx->wait_event.events;
6822 if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS))
6823 ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006824 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006825}
6826
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006827/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
6828 * The <es> pointer is not allowed to differ from the one passed to the
6829 * subscribe() call. It always returns zero.
6830 */
6831static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01006832{
Olivier Houchardea8dd942019-05-20 14:02:16 +02006833 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006834
Willy Tarreau113d52b2020-01-10 09:20:26 +01006835 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006836 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006837
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01006838 es->events &= ~event_type;
6839 if (!es->events)
Willy Tarreau113d52b2020-01-10 09:20:26 +01006840 ctx->subs = NULL;
6841
6842 /* If we subscribed, and we're not doing the handshake,
6843 * then we subscribed because the upper layer asked for it,
6844 * as the upper layer is no longer interested, we can
6845 * unsubscribe too.
6846 */
6847 event_type &= ctx->wait_event.events;
6848 if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS))
6849 conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006850
6851 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01006852}
6853
Olivier Houchard2e055482019-05-27 19:50:12 +02006854/* Use the provided XPRT as an underlying XPRT, and provide the old one.
6855 * Returns 0 on success, and non-zero on failure.
6856 */
6857static 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)
6858{
6859 struct ssl_sock_ctx *ctx = xprt_ctx;
6860
6861 if (oldxprt_ops != NULL)
6862 *oldxprt_ops = ctx->xprt;
6863 if (oldxprt_ctx != NULL)
6864 *oldxprt_ctx = ctx->xprt_ctx;
6865 ctx->xprt = toadd_ops;
6866 ctx->xprt_ctx = toadd_ctx;
6867 return 0;
6868}
6869
Olivier Houchard5149b592019-05-23 17:47:36 +02006870/* Remove the specified xprt. If if it our underlying XPRT, remove it and
6871 * return 0, otherwise just call the remove_xprt method from the underlying
6872 * XPRT.
6873 */
6874static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
6875{
6876 struct ssl_sock_ctx *ctx = xprt_ctx;
6877
6878 if (ctx->xprt_ctx == toremove_ctx) {
6879 ctx->xprt_ctx = newctx;
6880 ctx->xprt = newops;
6881 return 0;
6882 }
6883 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
6884}
6885
Olivier Houchardea8dd942019-05-20 14:02:16 +02006886static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
6887{
6888 struct ssl_sock_ctx *ctx = context;
6889
6890 /* First if we're doing an handshake, try that */
6891 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
6892 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
6893 /* If we had an error, or the handshake is done and I/O is available,
6894 * let the upper layer know.
Olivier Houchard477902b2020-01-22 18:08:48 +01006895 * If no mux was set up yet, then call conn_create_mux()
Olivier Houchardea8dd942019-05-20 14:02:16 +02006896 * we can't be sure conn_fd_handler() will be called again.
6897 */
6898 if ((ctx->conn->flags & CO_FL_ERROR) ||
6899 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
6900 int ret = 0;
6901 int woke = 0;
6902
6903 /* On error, wake any waiter */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006904 if (ctx->subs) {
6905 tasklet_wakeup(ctx->subs->tasklet);
6906 ctx->subs->events = 0;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006907 woke = 1;
Willy Tarreau113d52b2020-01-10 09:20:26 +01006908 ctx->subs = NULL;
Olivier Houchardea8dd942019-05-20 14:02:16 +02006909 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01006910
Olivier Houchardea8dd942019-05-20 14:02:16 +02006911 /* If we're the first xprt for the connection, let the
Olivier Houchard477902b2020-01-22 18:08:48 +01006912 * upper layers know. If we have no mux, create it,
6913 * and once we have a mux, call its wake method if we didn't
6914 * woke a tasklet already.
Olivier Houchardea8dd942019-05-20 14:02:16 +02006915 */
6916 if (ctx->conn->xprt_ctx == ctx) {
Olivier Houchard477902b2020-01-22 18:08:48 +01006917 if (!ctx->conn->mux)
6918 ret = conn_create_mux(ctx->conn);
Olivier Houchardea8dd942019-05-20 14:02:16 +02006919 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
6920 ctx->conn->mux->wake(ctx->conn);
6921 return NULL;
6922 }
6923 }
Olivier Houchard54907bb2019-12-19 15:02:39 +01006924#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6925 /* If we have early data and somebody wants to receive, let them */
Willy Tarreau113d52b2020-01-10 09:20:26 +01006926 else if (b_data(&ctx->early_buf) && ctx->subs &&
6927 ctx->subs->events & SUB_RETRY_RECV) {
6928 tasklet_wakeup(ctx->subs->tasklet);
6929 ctx->subs->events &= ~SUB_RETRY_RECV;
6930 if (!ctx->subs->events)
6931 ctx->subs = NULL;
Olivier Houchard54907bb2019-12-19 15:02:39 +01006932 }
6933#endif
Olivier Houchardea8dd942019-05-20 14:02:16 +02006934 return NULL;
6935}
6936
Emeric Brun46591952012-05-18 15:47:34 +02006937/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01006938 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02006939 * buffer wraps, in which case a second call may be performed. The connection's
6940 * flags are updated with whatever special event is detected (error, read0,
6941 * empty). The caller is responsible for taking care of those events and
6942 * avoiding the call if inappropriate. The function does not call the
6943 * connection's polling update function, so the caller is responsible for this.
6944 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006945static 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 +02006946{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006947 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02006948 ssize_t ret;
6949 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02006950
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006951 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02006952 goto out_error;
6953
Olivier Houchard54907bb2019-12-19 15:02:39 +01006954#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
6955 if (b_data(&ctx->early_buf)) {
6956 try = b_contig_space(buf);
6957 if (try > b_data(&ctx->early_buf))
6958 try = b_data(&ctx->early_buf);
6959 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
6960 b_add(buf, try);
6961 b_del(&ctx->early_buf, try);
6962 if (b_data(&ctx->early_buf) == 0)
6963 b_free(&ctx->early_buf);
6964 return try;
6965 }
6966#endif
6967
Willy Tarreau911db9b2020-01-23 16:27:54 +01006968 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02006969 /* a handshake was requested */
6970 return 0;
6971
Emeric Brun46591952012-05-18 15:47:34 +02006972 /* read the largest possible block. For this, we perform only one call
6973 * to recv() unless the buffer wraps and we exactly fill the first hunk,
6974 * in which case we accept to do it once again. A new attempt is made on
6975 * EINTR too.
6976 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01006977 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02006978
Willy Tarreau591d4452018-06-15 17:21:00 +02006979 try = b_contig_space(buf);
6980 if (!try)
6981 break;
6982
Willy Tarreauabf08d92014-01-14 11:31:27 +01006983 if (try > count)
6984 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02006985
Olivier Houchard66ab4982019-02-26 18:37:15 +01006986 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02006987
Emeric Brune1f38db2012-09-03 20:36:47 +02006988 if (conn->flags & CO_FL_ERROR) {
6989 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01006990 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02006991 }
Emeric Brun46591952012-05-18 15:47:34 +02006992 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02006993 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006994 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02006995 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02006996 }
Emeric Brun46591952012-05-18 15:47:34 +02006997 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01006998 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02006999 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01007000 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02007001 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007002 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007003#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007004 /* Async mode can be re-enabled, because we're leaving data state.*/
7005 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007006 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007007#endif
Emeric Brun46591952012-05-18 15:47:34 +02007008 break;
7009 }
7010 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007011 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02007012 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
7013 SUB_RETRY_RECV,
7014 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01007015 /* handshake is running, and it may need to re-enable read */
7016 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02007017#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007018 /* Async mode can be re-enabled, because we're leaving data state.*/
7019 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007020 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007021#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01007022 break;
7023 }
Emeric Brun46591952012-05-18 15:47:34 +02007024 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02007025 } else if (ret == SSL_ERROR_ZERO_RETURN)
7026 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007027 /* For SSL_ERROR_SYSCALL, make sure to clear the error
7028 * stack before shutting down the connection for
7029 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01007030 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
7031 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02007032 /* otherwise it's a real error */
7033 goto out_error;
7034 }
7035 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007036 leave:
Emeric Brun46591952012-05-18 15:47:34 +02007037 return done;
7038
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007039 clear_ssl_error:
7040 /* Clear openssl global errors stack */
7041 ssl_sock_dump_errors(conn);
7042 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02007043 read0:
7044 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007045 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01007046
Emeric Brun46591952012-05-18 15:47:34 +02007047 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01007048 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01007049 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007050 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007051 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007052 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02007053}
7054
7055
Willy Tarreau787db9a2018-06-14 18:31:46 +02007056/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
7057 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
7058 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02007059 * Only one call to send() is performed, unless the buffer wraps, in which case
7060 * a second call may be performed. The connection's flags are updated with
7061 * whatever special event is detected (error, empty). The caller is responsible
7062 * for taking care of those events and avoiding the call if inappropriate. The
7063 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02007064 * is responsible for this. The buffer's output is not adjusted, it's up to the
7065 * caller to take care of this. It's up to the caller to update the buffer's
7066 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02007067 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007068static 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 +02007069{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007070 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02007071 ssize_t ret;
7072 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02007073
7074 done = 0;
7075
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007076 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02007077 goto out_error;
7078
Willy Tarreau911db9b2020-01-23 16:27:54 +01007079 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007080 /* a handshake was requested */
7081 return 0;
7082
7083 /* send the largest possible block. For this we perform only one call
7084 * to send() unless the buffer wraps and we exactly fill the first hunk,
7085 * in which case we accept to do it once again.
7086 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02007087 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02007088#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02007089 size_t written_data;
7090#endif
7091
Willy Tarreau787db9a2018-06-14 18:31:46 +02007092 try = b_contig_data(buf, done);
7093 if (try > count)
7094 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01007095
Willy Tarreau7bed9452014-02-02 02:00:24 +01007096 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007097 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01007098 global_ssl.max_record && try > global_ssl.max_record) {
7099 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01007100 }
7101 else {
7102 /* we need to keep the information about the fact that
7103 * we're not limiting the upcoming send(), because if it
7104 * fails, we'll have to retry with at least as many data.
7105 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007106 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01007107 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01007108
Willy Tarreau5db847a2019-05-09 14:13:35 +02007109#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02007110 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02007111 unsigned int max_early;
7112
Olivier Houchard522eea72017-11-03 16:27:47 +01007113 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007114 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01007115 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007116 if (SSL_get0_session(ctx->ssl))
7117 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01007118 else
7119 max_early = 0;
7120 }
7121
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007122 if (try + ctx->sent_early_data > max_early) {
7123 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01007124 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02007125 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007126 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02007127 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01007128 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02007129 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01007130 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02007131 if (ret == 1) {
7132 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007133 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007134 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01007135 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02007136 /* Initiate the handshake, now */
7137 tasklet_wakeup(ctx->wait_event.tasklet);
7138 }
Olivier Houchard522eea72017-11-03 16:27:47 +01007139
Olivier Houchardc2aae742017-09-22 18:26:28 +02007140 }
7141
7142 } else
7143#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01007144 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01007145
Emeric Brune1f38db2012-09-03 20:36:47 +02007146 if (conn->flags & CO_FL_ERROR) {
7147 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01007148 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02007149 }
Emeric Brun46591952012-05-18 15:47:34 +02007150 if (ret > 0) {
Willy Tarreauc192b0a2020-01-23 09:11:58 +01007151 /* A send succeeded, so we can consider ourself connected */
7152 conn->flags &= ~CO_FL_WAIT_L4L6;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007153 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02007154 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02007155 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02007156 }
7157 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007158 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007159
Emeric Brun46591952012-05-18 15:47:34 +02007160 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01007161 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01007162 /* handshake is running, and it may need to re-enable write */
7163 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007164 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007165#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007166 /* Async mode can be re-enabled, because we're leaving data state.*/
7167 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007168 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007169#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01007170 break;
7171 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02007172
Emeric Brun46591952012-05-18 15:47:34 +02007173 break;
7174 }
7175 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01007176 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02007177 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02007178 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
7179 SUB_RETRY_RECV,
7180 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007181#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00007182 /* Async mode can be re-enabled, because we're leaving data state.*/
7183 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007184 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00007185#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007186 break;
7187 }
Emeric Brun46591952012-05-18 15:47:34 +02007188 goto out_error;
7189 }
7190 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007191 leave:
Emeric Brun46591952012-05-18 15:47:34 +02007192 return done;
7193
7194 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01007195 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007196 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007197 ERR_clear_error();
7198
Emeric Brun46591952012-05-18 15:47:34 +02007199 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02007200 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02007201}
7202
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007203static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02007204
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007205 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007206
Olivier Houchardea8dd942019-05-20 14:02:16 +02007207
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007208 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02007209 if (ctx->wait_event.events != 0)
7210 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
7211 ctx->wait_event.events,
7212 &ctx->wait_event);
Willy Tarreau113d52b2020-01-10 09:20:26 +01007213 if (ctx->subs) {
7214 ctx->subs->events = 0;
7215 tasklet_wakeup(ctx->subs->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02007216 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01007217
Olivier Houchard692c1d02019-05-23 18:41:47 +02007218 if (ctx->xprt->close)
7219 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02007220#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02007221 if (global_ssl.async) {
7222 OSSL_ASYNC_FD all_fd[32], afd;
7223 size_t num_all_fds = 0;
7224 int i;
7225
Olivier Houchard66ab4982019-02-26 18:37:15 +01007226 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02007227 if (num_all_fds > 32) {
7228 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
7229 return;
7230 }
7231
Olivier Houchard66ab4982019-02-26 18:37:15 +01007232 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02007233
7234 /* If an async job is pending, we must try to
7235 to catch the end using polling before calling
7236 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007237 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02007238 for (i=0 ; i < num_all_fds ; i++) {
7239 /* switch on an handler designed to
7240 * handle the SSL_free
7241 */
7242 afd = all_fd[i];
7243 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007244 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02007245 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00007246 /* To ensure that the fd cache won't be used
7247 * and we'll catch a real RD event.
7248 */
7249 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02007250 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02007251 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007252 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01007253 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007254 return;
7255 }
Emeric Brun3854e012017-05-17 20:42:48 +02007256 /* Else we can remove the fds from the fdtab
7257 * and call SSL_free.
7258 * note: we do a fd_remove and not a delete
7259 * because the fd is owned by the engine.
7260 * the engine is responsible to close
7261 */
7262 for (i=0 ; i < num_all_fds ; i++)
7263 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00007264 }
7265#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01007266 SSL_free(ctx->ssl);
Olivier Houchard54907bb2019-12-19 15:02:39 +01007267 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02007268 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01007269 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01007270 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02007271 }
Emeric Brun46591952012-05-18 15:47:34 +02007272}
7273
7274/* This function tries to perform a clean shutdown on an SSL connection, and in
7275 * any case, flags the connection as reusable if no handshake was in progress.
7276 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007277static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02007278{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007279 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007280
Willy Tarreau911db9b2020-01-23 16:27:54 +01007281 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02007282 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01007283 if (!clean)
7284 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007285 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02007286 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007287 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01007288 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02007289 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01007290 ERR_clear_error();
7291 }
Emeric Brun46591952012-05-18 15:47:34 +02007292}
7293
William Lallemandd4f946c2019-12-05 10:26:40 +01007294/* fill a buffer with the algorithm and size of a public key */
7295static int cert_get_pkey_algo(X509 *crt, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007296{
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007297 int bits = 0;
7298 int sig = TLSEXT_signature_anonymous;
7299 int len = -1;
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007300 EVP_PKEY *pkey;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007301
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007302 pkey = X509_get_pubkey(crt);
7303 if (pkey) {
7304 bits = EVP_PKEY_bits(pkey);
7305 switch(EVP_PKEY_base_id(pkey)) {
7306 case EVP_PKEY_RSA:
7307 sig = TLSEXT_signature_rsa;
7308 break;
7309 case EVP_PKEY_EC:
7310 sig = TLSEXT_signature_ecdsa;
7311 break;
7312 case EVP_PKEY_DSA:
7313 sig = TLSEXT_signature_dsa;
7314 break;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007315 }
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01007316 EVP_PKEY_free(pkey);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01007317 }
7318
7319 switch(sig) {
7320 case TLSEXT_signature_rsa:
7321 len = chunk_printf(out, "RSA%d", bits);
7322 break;
7323 case TLSEXT_signature_ecdsa:
7324 len = chunk_printf(out, "EC%d", bits);
7325 break;
7326 case TLSEXT_signature_dsa:
7327 len = chunk_printf(out, "DSA%d", bits);
7328 break;
7329 default:
7330 return 0;
7331 }
7332 if (len < 0)
7333 return 0;
7334 return 1;
7335}
7336
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05007337/* used for ppv2 pkey algo (can be used for logging) */
William Lallemandd4f946c2019-12-05 10:26:40 +01007338int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
7339{
7340 struct ssl_sock_ctx *ctx;
7341 X509 *crt;
7342
7343 if (!ssl_sock_is_ssl(conn))
7344 return 0;
7345
7346 ctx = conn->xprt_ctx;
7347
7348 crt = SSL_get_certificate(ctx->ssl);
7349 if (!crt)
7350 return 0;
7351
7352 return cert_get_pkey_algo(crt, out);
7353}
7354
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007355/* used for ppv2 cert signature (can be used for logging) */
7356const char *ssl_sock_get_cert_sig(struct connection *conn)
7357{
Christopher Faulet82004142019-09-10 10:12:03 +02007358 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007359
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007360 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
7361 X509 *crt;
7362
7363 if (!ssl_sock_is_ssl(conn))
7364 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007365 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007366 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01007367 if (!crt)
7368 return NULL;
7369 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
7370 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
7371}
7372
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007373/* used for ppv2 authority */
7374const char *ssl_sock_get_sni(struct connection *conn)
7375{
7376#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007377 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007378
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007379 if (!ssl_sock_is_ssl(conn))
7380 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007381 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007382 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007383#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01007384 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01007385#endif
7386}
7387
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007388/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007389const char *ssl_sock_get_cipher_name(struct connection *conn)
7390{
Christopher Faulet82004142019-09-10 10:12:03 +02007391 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007392
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007393 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007394 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007395 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007396 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007397}
7398
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007399/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007400const char *ssl_sock_get_proto_version(struct connection *conn)
7401{
Christopher Faulet82004142019-09-10 10:12:03 +02007402 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007403
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02007404 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007405 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02007406 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007407 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02007408}
7409
Willy Tarreau8d598402012-10-22 17:58:39 +02007410/* Extract a serial from a cert, and copy it to a chunk.
7411 * Returns 1 if serial is found and copied, 0 if no serial found and
7412 * -1 if output is not large enough.
7413 */
7414static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007415ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02007416{
7417 ASN1_INTEGER *serial;
7418
7419 serial = X509_get_serialNumber(crt);
7420 if (!serial)
7421 return 0;
7422
7423 if (out->size < serial->length)
7424 return -1;
7425
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007426 memcpy(out->area, serial->data, serial->length);
7427 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02007428 return 1;
7429}
7430
Emeric Brun43e79582014-10-29 19:03:26 +01007431/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08007432 * Returns 1 if the cert is found and copied, 0 on der conversion failure
7433 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01007434 */
7435static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007436ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01007437{
7438 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007439 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01007440
7441 len =i2d_X509(crt, NULL);
7442 if (len <= 0)
7443 return 1;
7444
7445 if (out->size < len)
7446 return -1;
7447
7448 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007449 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01007450 return 1;
7451}
7452
Emeric Brunce5ad802012-10-22 14:11:22 +02007453
Willy Tarreau83061a82018-07-13 11:56:34 +02007454/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02007455 * Returns 1 if serial is found and copied, 0 if no valid time found
7456 * and -1 if output is not large enough.
7457 */
7458static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007459ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02007460{
7461 if (tm->type == V_ASN1_GENERALIZEDTIME) {
7462 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
7463
7464 if (gentm->length < 12)
7465 return 0;
7466 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
7467 return 0;
7468 if (out->size < gentm->length-2)
7469 return -1;
7470
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007471 memcpy(out->area, gentm->data+2, gentm->length-2);
7472 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02007473 return 1;
7474 }
7475 else if (tm->type == V_ASN1_UTCTIME) {
7476 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
7477
7478 if (utctm->length < 10)
7479 return 0;
7480 if (utctm->data[0] >= 0x35)
7481 return 0;
7482 if (out->size < utctm->length)
7483 return -1;
7484
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007485 memcpy(out->area, utctm->data, utctm->length);
7486 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02007487 return 1;
7488 }
7489
7490 return 0;
7491}
7492
Emeric Brun87855892012-10-17 17:39:35 +02007493/* Extract an entry from a X509_NAME and copy its value to an output chunk.
7494 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
7495 */
7496static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007497ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
7498 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007499{
7500 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007501 ASN1_OBJECT *obj;
7502 ASN1_STRING *data;
7503 const unsigned char *data_ptr;
7504 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007505 int i, j, n;
7506 int cur = 0;
7507 const char *s;
7508 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007509 int name_count;
7510
7511 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007512
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007513 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007514 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02007515 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007516 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02007517 else
7518 j = i;
7519
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007520 ne = X509_NAME_get_entry(a, j);
7521 obj = X509_NAME_ENTRY_get_object(ne);
7522 data = X509_NAME_ENTRY_get_data(ne);
7523 data_ptr = ASN1_STRING_get0_data(data);
7524 data_len = ASN1_STRING_length(data);
7525 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007526 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007527 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007528 s = tmp;
7529 }
7530
7531 if (chunk_strcasecmp(entry, s) != 0)
7532 continue;
7533
7534 if (pos < 0)
7535 cur--;
7536 else
7537 cur++;
7538
7539 if (cur != pos)
7540 continue;
7541
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007542 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02007543 return -1;
7544
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007545 memcpy(out->area, data_ptr, data_len);
7546 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007547 return 1;
7548 }
7549
7550 return 0;
7551
William Lallemandd4f946c2019-12-05 10:26:40 +01007552}
7553
7554/*
7555 * Extract and format the DNS SAN extensions and copy result into a chuink
7556 * Return 0;
7557 */
7558#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
7559static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
7560{
7561 int i;
7562 char *str;
7563 STACK_OF(GENERAL_NAME) *names = NULL;
7564
7565 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
7566 if (names) {
7567 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
7568 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
7569 if (i > 0)
7570 chunk_appendf(out, ", ");
7571 if (name->type == GEN_DNS) {
7572 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
7573 chunk_appendf(out, "DNS:%s", str);
7574 OPENSSL_free(str);
7575 }
7576 }
7577 }
7578 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
7579 }
7580 return 0;
Emeric Brun87855892012-10-17 17:39:35 +02007581}
William Lallemandd4f946c2019-12-05 10:26:40 +01007582#endif
Emeric Brun87855892012-10-17 17:39:35 +02007583
Elliot Otchet71f82972020-01-15 08:12:14 -05007584/*
7585 * Extract the DN in the specified format from the X509_NAME and copy result to a chunk.
7586 * Currently supports rfc2253 for returning LDAP V3 DNs.
7587 * Returns 1 if dn entries exist, 0 if no dn entry was found.
7588 */
7589static int
7590ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out)
7591{
7592 BIO *bio = NULL;
7593 int ret = 0;
7594 int data_len = 0;
7595
7596 if (chunk_strcmp(format, "rfc2253") == 0) {
7597 bio = BIO_new(BIO_s_mem());
7598 if (bio == NULL)
7599 goto out;
7600
7601 if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0)
7602 goto out;
7603
7604 if ((data_len = BIO_read(bio, out->area, out->size)) <= 0)
7605 goto out;
7606
7607 out->data = data_len;
7608
7609 ret = 1;
7610 }
7611out:
7612 if (bio)
7613 BIO_free(bio);
7614 return ret;
7615}
7616
Emeric Brun87855892012-10-17 17:39:35 +02007617/* Extract and format full DN from a X509_NAME and copy result into a chunk
7618 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
7619 */
7620static int
Willy Tarreau83061a82018-07-13 11:56:34 +02007621ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02007622{
7623 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007624 ASN1_OBJECT *obj;
7625 ASN1_STRING *data;
7626 const unsigned char *data_ptr;
7627 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007628 int i, n, ln;
7629 int l = 0;
7630 const char *s;
7631 char *p;
7632 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007633 int name_count;
7634
7635
7636 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02007637
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007638 out->data = 0;
7639 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007640 for (i = 0; i < name_count; i++) {
7641 ne = X509_NAME_get_entry(a, i);
7642 obj = X509_NAME_ENTRY_get_object(ne);
7643 data = X509_NAME_ENTRY_get_data(ne);
7644 data_ptr = ASN1_STRING_get0_data(data);
7645 data_len = ASN1_STRING_length(data);
7646 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02007647 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007648 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02007649 s = tmp;
7650 }
7651 ln = strlen(s);
7652
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007653 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007654 if (l > out->size)
7655 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007656 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02007657
7658 *(p++)='/';
7659 memcpy(p, s, ln);
7660 p += ln;
7661 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02007662 memcpy(p, data_ptr, data_len);
7663 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02007664 }
7665
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007666 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02007667 return 0;
7668
7669 return 1;
7670}
7671
Olivier Houchardab28a322018-12-21 19:45:40 +01007672void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
7673{
7674#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet82004142019-09-10 10:12:03 +02007675 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007676
Olivier Houcharde488ea82019-06-28 14:10:33 +02007677 if (!ssl_sock_is_ssl(conn))
7678 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007679 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007680 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01007681#endif
7682}
7683
Willy Tarreau119a4082016-12-22 21:58:38 +01007684/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
7685 * to disable SNI.
7686 */
Willy Tarreau63076412015-07-10 11:33:32 +02007687void ssl_sock_set_servername(struct connection *conn, const char *hostname)
7688{
7689#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02007690 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007691
Willy Tarreau119a4082016-12-22 21:58:38 +01007692 char *prev_name;
7693
Willy Tarreau63076412015-07-10 11:33:32 +02007694 if (!ssl_sock_is_ssl(conn))
7695 return;
Christopher Faulet82004142019-09-10 10:12:03 +02007696 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02007697
Willy Tarreau119a4082016-12-22 21:58:38 +01007698 /* if the SNI changes, we must destroy the reusable context so that a
7699 * new connection will present a new SNI. As an optimization we could
7700 * later imagine having a small cache of ssl_ctx to hold a few SNI per
7701 * server.
7702 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007703 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01007704 if ((!prev_name && hostname) ||
7705 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01007706 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01007707
Olivier Houchard66ab4982019-02-26 18:37:15 +01007708 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02007709#endif
7710}
7711
Emeric Brun0abf8362014-06-24 18:26:41 +02007712/* Extract peer certificate's common name into the chunk dest
7713 * Returns
7714 * the len of the extracted common name
7715 * or 0 if no CN found in DN
7716 * or -1 on error case (i.e. no peer certificate)
7717 */
Willy Tarreau83061a82018-07-13 11:56:34 +02007718int ssl_sock_get_remote_common_name(struct connection *conn,
7719 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04007720{
Christopher Faulet82004142019-09-10 10:12:03 +02007721 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04007722 X509 *crt = NULL;
7723 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04007724 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02007725 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007726 .area = (char *)&find_cn,
7727 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04007728 };
Emeric Brun0abf8362014-06-24 18:26:41 +02007729 int result = -1;
David Safb76832014-05-08 23:42:08 -04007730
7731 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02007732 goto out;
Christopher Faulet82004142019-09-10 10:12:03 +02007733 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04007734
7735 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007736 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007737 if (!crt)
7738 goto out;
7739
7740 name = X509_get_subject_name(crt);
7741 if (!name)
7742 goto out;
David Safb76832014-05-08 23:42:08 -04007743
Emeric Brun0abf8362014-06-24 18:26:41 +02007744 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
7745out:
David Safb76832014-05-08 23:42:08 -04007746 if (crt)
7747 X509_free(crt);
7748
7749 return result;
7750}
7751
Dave McCowan328fb582014-07-30 10:39:13 -04007752/* returns 1 if client passed a certificate for this session, 0 if not */
7753int ssl_sock_get_cert_used_sess(struct connection *conn)
7754{
Christopher Faulet82004142019-09-10 10:12:03 +02007755 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007756 X509 *crt = NULL;
7757
7758 if (!ssl_sock_is_ssl(conn))
7759 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007760 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04007761
7762 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01007763 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04007764 if (!crt)
7765 return 0;
7766
7767 X509_free(crt);
7768 return 1;
7769}
7770
7771/* returns 1 if client passed a certificate for this connection, 0 if not */
7772int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04007773{
Christopher Faulet82004142019-09-10 10:12:03 +02007774 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007775
David Safb76832014-05-08 23:42:08 -04007776 if (!ssl_sock_is_ssl(conn))
7777 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02007778 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007779 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04007780}
7781
7782/* returns result from SSL verify */
7783unsigned int ssl_sock_get_verify_result(struct connection *conn)
7784{
Christopher Faulet82004142019-09-10 10:12:03 +02007785 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007786
David Safb76832014-05-08 23:42:08 -04007787 if (!ssl_sock_is_ssl(conn))
7788 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet82004142019-09-10 10:12:03 +02007789 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01007790 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04007791}
7792
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007793/* Returns the application layer protocol name in <str> and <len> when known.
7794 * Zero is returned if the protocol name was not found, otherwise non-zero is
7795 * returned. The string is allocated in the SSL context and doesn't have to be
7796 * freed by the caller. NPN is also checked if available since older versions
7797 * of openssl (1.0.1) which are more common in field only support this one.
7798 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007799static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007800{
Olivier Houchard66ab4982019-02-26 18:37:15 +01007801#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
7802 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007803 struct ssl_sock_ctx *ctx = xprt_ctx;
7804 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007805 return 0;
7806
7807 *str = NULL;
7808
7809#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01007810 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007811 if (*str)
7812 return 1;
7813#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01007814#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01007815 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007816 if (*str)
7817 return 1;
7818#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01007819#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01007820 return 0;
7821}
7822
Willy Tarreau7875d092012-09-10 08:20:03 +02007823/***** Below are some sample fetching functions for ACL/patterns *****/
7824
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007825static int
7826smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
7827{
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007828 SSL *ssl;
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007829 struct connection *conn;
7830
7831 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007832 ssl = ssl_sock_get_ssl_object(conn);
7833 if (!ssl)
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007834 return 0;
7835
7836 smp->flags = 0;
7837 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007838#ifdef OPENSSL_IS_BORINGSSL
7839 {
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007840 smp->data.u.sint = (SSL_in_early_data(ssl) &&
7841 SSL_early_data_accepted(ssl));
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007842 }
7843#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01007844 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +01007845 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) ? 1 : 0;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02007846#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02007847 return 1;
7848}
7849
Emeric Brune64aef12012-09-21 13:15:06 +02007850/* boolean, returns true if client cert was present */
7851static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007852smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02007853{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007854 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007855 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007856
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007857 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007858 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02007859 return 0;
7860
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007861 ctx = conn->xprt_ctx;
7862
Willy Tarreau911db9b2020-01-23 16:27:54 +01007863 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brune64aef12012-09-21 13:15:06 +02007864 smp->flags |= SMP_F_MAY_CHANGE;
7865 return 0;
7866 }
7867
7868 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007869 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007870 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02007871
7872 return 1;
7873}
7874
Emeric Brun43e79582014-10-29 19:03:26 +01007875/* binary, returns a certificate in a binary chunk (der/raw).
7876 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7877 * should be use.
7878 */
7879static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007880smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01007881{
7882 int cert_peer = (kw[4] == 'c') ? 1 : 0;
7883 X509 *crt = NULL;
7884 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007885 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01007886 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007887 SSL *ssl;
Emeric Brun43e79582014-10-29 19:03:26 +01007888
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007889 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007890 ssl = ssl_sock_get_ssl_object(conn);
7891 if (!ssl)
Emeric Brun43e79582014-10-29 19:03:26 +01007892 return 0;
7893
Willy Tarreau911db9b2020-01-23 16:27:54 +01007894 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun43e79582014-10-29 19:03:26 +01007895 smp->flags |= SMP_F_MAY_CHANGE;
7896 return 0;
7897 }
7898
7899 if (cert_peer)
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007900 crt = SSL_get_peer_certificate(ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007901 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007902 crt = SSL_get_certificate(ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01007903
7904 if (!crt)
7905 goto out;
7906
7907 smp_trash = get_trash_chunk();
7908 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
7909 goto out;
7910
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007911 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007912 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01007913 ret = 1;
7914out:
7915 /* SSL_get_peer_certificate, it increase X509 * ref count */
7916 if (cert_peer && crt)
7917 X509_free(crt);
7918 return ret;
7919}
7920
Emeric Brunba841a12014-04-30 17:05:08 +02007921/* binary, returns serial of certificate in a binary chunk.
7922 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7923 * should be use.
7924 */
Willy Tarreau8d598402012-10-22 17:58:39 +02007925static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007926smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02007927{
Emeric Brunba841a12014-04-30 17:05:08 +02007928 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02007929 X509 *crt = NULL;
7930 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007931 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007932 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007933 SSL *ssl;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007934
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007935 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007936 ssl = ssl_sock_get_ssl_object(conn);
7937 if (!ssl)
Willy Tarreau8d598402012-10-22 17:58:39 +02007938 return 0;
7939
Willy Tarreau911db9b2020-01-23 16:27:54 +01007940 if (conn->flags & CO_FL_WAIT_XPRT) {
Willy Tarreau8d598402012-10-22 17:58:39 +02007941 smp->flags |= SMP_F_MAY_CHANGE;
7942 return 0;
7943 }
7944
Emeric Brunba841a12014-04-30 17:05:08 +02007945 if (cert_peer)
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007946 crt = SSL_get_peer_certificate(ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007947 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007948 crt = SSL_get_certificate(ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007949
Willy Tarreau8d598402012-10-22 17:58:39 +02007950 if (!crt)
7951 goto out;
7952
Willy Tarreau47ca5452012-12-23 20:22:19 +01007953 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02007954 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
7955 goto out;
7956
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007957 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007958 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02007959 ret = 1;
7960out:
Emeric Brunba841a12014-04-30 17:05:08 +02007961 /* SSL_get_peer_certificate, it increase X509 * ref count */
7962 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02007963 X509_free(crt);
7964 return ret;
7965}
Emeric Brune64aef12012-09-21 13:15:06 +02007966
Emeric Brunba841a12014-04-30 17:05:08 +02007967/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
7968 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
7969 * should be use.
7970 */
James Votha051b4a2013-05-14 20:37:59 +02007971static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007972smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02007973{
Emeric Brunba841a12014-04-30 17:05:08 +02007974 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02007975 X509 *crt = NULL;
7976 const EVP_MD *digest;
7977 int ret = 0;
Willy Tarreau105599c2020-02-25 08:59:23 +01007978 unsigned int len = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02007979 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007980 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007981 SSL *ssl;
James Votha051b4a2013-05-14 20:37:59 +02007982
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007983 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007984 ssl = ssl_sock_get_ssl_object(conn);
7985 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007986 return 0;
7987
Willy Tarreau911db9b2020-01-23 16:27:54 +01007988 if (conn->flags & CO_FL_WAIT_XPRT) {
James Votha051b4a2013-05-14 20:37:59 +02007989 smp->flags |= SMP_F_MAY_CHANGE;
7990 return 0;
7991 }
7992
Emeric Brunba841a12014-04-30 17:05:08 +02007993 if (cert_peer)
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007994 crt = SSL_get_peer_certificate(ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02007995 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007996 crt = SSL_get_certificate(ssl);
James Votha051b4a2013-05-14 20:37:59 +02007997 if (!crt)
7998 goto out;
7999
8000 smp_trash = get_trash_chunk();
8001 digest = EVP_sha1();
Willy Tarreau105599c2020-02-25 08:59:23 +01008002 X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len);
8003 smp_trash->data = len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008004 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008005 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02008006 ret = 1;
8007out:
Emeric Brunba841a12014-04-30 17:05:08 +02008008 /* SSL_get_peer_certificate, it increase X509 * ref count */
8009 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02008010 X509_free(crt);
8011 return ret;
8012}
8013
Emeric Brunba841a12014-04-30 17:05:08 +02008014/* string, returns certificate's notafter date in ASN1_UTCTIME format.
8015 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8016 * should be use.
8017 */
Emeric Brunce5ad802012-10-22 14:11:22 +02008018static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008019smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02008020{
Emeric Brunba841a12014-04-30 17:05:08 +02008021 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02008022 X509 *crt = NULL;
8023 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008024 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008025 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008026 SSL *ssl;
Emeric Brunce5ad802012-10-22 14:11:22 +02008027
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008028 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008029 ssl = ssl_sock_get_ssl_object(conn);
8030 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008031 return 0;
8032
Willy Tarreau911db9b2020-01-23 16:27:54 +01008033 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02008034 smp->flags |= SMP_F_MAY_CHANGE;
8035 return 0;
8036 }
8037
Emeric Brunba841a12014-04-30 17:05:08 +02008038 if (cert_peer)
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008039 crt = SSL_get_peer_certificate(ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008040 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008041 crt = SSL_get_certificate(ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02008042 if (!crt)
8043 goto out;
8044
Willy Tarreau47ca5452012-12-23 20:22:19 +01008045 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08008046 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02008047 goto out;
8048
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008049 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008050 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02008051 ret = 1;
8052out:
Emeric Brunba841a12014-04-30 17:05:08 +02008053 /* SSL_get_peer_certificate, it increase X509 * ref count */
8054 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02008055 X509_free(crt);
8056 return ret;
8057}
8058
Emeric Brunba841a12014-04-30 17:05:08 +02008059/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
8060 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8061 * should be use.
8062 */
Emeric Brun87855892012-10-17 17:39:35 +02008063static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008064smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02008065{
Emeric Brunba841a12014-04-30 17:05:08 +02008066 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02008067 X509 *crt = NULL;
8068 X509_NAME *name;
8069 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008070 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008071 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008072 SSL *ssl;
Emeric Brun87855892012-10-17 17:39:35 +02008073
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008074 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008075 ssl = ssl_sock_get_ssl_object(conn);
8076 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008077 return 0;
8078
Willy Tarreau911db9b2020-01-23 16:27:54 +01008079 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02008080 smp->flags |= SMP_F_MAY_CHANGE;
8081 return 0;
8082 }
8083
Emeric Brunba841a12014-04-30 17:05:08 +02008084 if (cert_peer)
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008085 crt = SSL_get_peer_certificate(ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008086 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008087 crt = SSL_get_certificate(ssl);
Emeric Brun87855892012-10-17 17:39:35 +02008088 if (!crt)
8089 goto out;
8090
8091 name = X509_get_issuer_name(crt);
8092 if (!name)
8093 goto out;
8094
Willy Tarreau47ca5452012-12-23 20:22:19 +01008095 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05008096 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02008097 int pos = 1;
8098
8099 if (args[1].type == ARGT_SINT)
8100 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02008101
8102 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
8103 goto out;
8104 }
Elliot Otchet71f82972020-01-15 08:12:14 -05008105 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
8106 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
8107 goto out;
8108 }
Emeric Brun87855892012-10-17 17:39:35 +02008109 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
8110 goto out;
8111
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008112 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008113 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02008114 ret = 1;
8115out:
Emeric Brunba841a12014-04-30 17:05:08 +02008116 /* SSL_get_peer_certificate, it increase X509 * ref count */
8117 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02008118 X509_free(crt);
8119 return ret;
8120}
8121
Emeric Brunba841a12014-04-30 17:05:08 +02008122/* string, returns notbefore date in ASN1_UTCTIME format.
8123 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8124 * should be use.
8125 */
Emeric Brunce5ad802012-10-22 14:11:22 +02008126static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008127smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02008128{
Emeric Brunba841a12014-04-30 17:05:08 +02008129 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02008130 X509 *crt = NULL;
8131 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008132 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008133 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008134 SSL *ssl;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008135
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008136 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008137 ssl = ssl_sock_get_ssl_object(conn);
8138 if (!ssl)
Emeric Brunce5ad802012-10-22 14:11:22 +02008139 return 0;
8140
Willy Tarreau911db9b2020-01-23 16:27:54 +01008141 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02008142 smp->flags |= SMP_F_MAY_CHANGE;
8143 return 0;
8144 }
8145
Emeric Brunba841a12014-04-30 17:05:08 +02008146 if (cert_peer)
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008147 crt = SSL_get_peer_certificate(ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008148 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008149 crt = SSL_get_certificate(ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02008150 if (!crt)
8151 goto out;
8152
Willy Tarreau47ca5452012-12-23 20:22:19 +01008153 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08008154 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02008155 goto out;
8156
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008157 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008158 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02008159 ret = 1;
8160out:
Emeric Brunba841a12014-04-30 17:05:08 +02008161 /* SSL_get_peer_certificate, it increase X509 * ref count */
8162 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02008163 X509_free(crt);
8164 return ret;
8165}
8166
Emeric Brunba841a12014-04-30 17:05:08 +02008167/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
8168 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8169 * should be use.
8170 */
Emeric Brun87855892012-10-17 17:39:35 +02008171static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008172smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02008173{
Emeric Brunba841a12014-04-30 17:05:08 +02008174 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02008175 X509 *crt = NULL;
8176 X509_NAME *name;
8177 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02008178 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008179 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008180 SSL *ssl;
Emeric Brun87855892012-10-17 17:39:35 +02008181
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008182 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008183 ssl = ssl_sock_get_ssl_object(conn);
8184 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008185 return 0;
8186
Willy Tarreau911db9b2020-01-23 16:27:54 +01008187 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02008188 smp->flags |= SMP_F_MAY_CHANGE;
8189 return 0;
8190 }
8191
Emeric Brunba841a12014-04-30 17:05:08 +02008192 if (cert_peer)
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008193 crt = SSL_get_peer_certificate(ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008194 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008195 crt = SSL_get_certificate(ssl);
Emeric Brun87855892012-10-17 17:39:35 +02008196 if (!crt)
8197 goto out;
8198
8199 name = X509_get_subject_name(crt);
8200 if (!name)
8201 goto out;
8202
Willy Tarreau47ca5452012-12-23 20:22:19 +01008203 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05008204 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02008205 int pos = 1;
8206
8207 if (args[1].type == ARGT_SINT)
8208 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02008209
8210 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
8211 goto out;
8212 }
Elliot Otchet71f82972020-01-15 08:12:14 -05008213 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
8214 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
8215 goto out;
8216 }
Emeric Brun87855892012-10-17 17:39:35 +02008217 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
8218 goto out;
8219
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008220 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008221 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02008222 ret = 1;
8223out:
Emeric Brunba841a12014-04-30 17:05:08 +02008224 /* SSL_get_peer_certificate, it increase X509 * ref count */
8225 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02008226 X509_free(crt);
8227 return ret;
8228}
Emeric Brun9143d372012-12-20 15:44:16 +01008229
8230/* integer, returns true if current session use a client certificate */
8231static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008232smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01008233{
8234 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008235 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008236 SSL *ssl;
Emeric Brun9143d372012-12-20 15:44:16 +01008237
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008238 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008239 ssl = ssl_sock_get_ssl_object(conn);
8240 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008241 return 0;
8242
Willy Tarreau911db9b2020-01-23 16:27:54 +01008243 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun9143d372012-12-20 15:44:16 +01008244 smp->flags |= SMP_F_MAY_CHANGE;
8245 return 0;
8246 }
8247
8248 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008249 crt = SSL_get_peer_certificate(ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01008250 if (crt) {
8251 X509_free(crt);
8252 }
8253
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008254 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008255 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01008256 return 1;
8257}
8258
Emeric Brunba841a12014-04-30 17:05:08 +02008259/* integer, returns the certificate version
8260 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8261 * should be use.
8262 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02008263static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008264smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02008265{
Emeric Brunba841a12014-04-30 17:05:08 +02008266 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008267 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008268 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008269 SSL *ssl;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008270
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008271 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008272 ssl = ssl_sock_get_ssl_object(conn);
8273 if (!ssl)
Emeric Bruna7359fd2012-10-17 15:03:11 +02008274 return 0;
8275
Willy Tarreau911db9b2020-01-23 16:27:54 +01008276 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02008277 smp->flags |= SMP_F_MAY_CHANGE;
8278 return 0;
8279 }
8280
Emeric Brunba841a12014-04-30 17:05:08 +02008281 if (cert_peer)
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008282 crt = SSL_get_peer_certificate(ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008283 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008284 crt = SSL_get_certificate(ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02008285 if (!crt)
8286 return 0;
8287
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008288 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02008289 /* SSL_get_peer_certificate increase X509 * ref count */
8290 if (cert_peer)
8291 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008292 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02008293
8294 return 1;
8295}
8296
Emeric Brunba841a12014-04-30 17:05:08 +02008297/* string, returns the certificate's signature algorithm.
8298 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8299 * should be use.
8300 */
Emeric Brun7f56e742012-10-19 18:15:40 +02008301static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008302smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02008303{
Emeric Brunba841a12014-04-30 17:05:08 +02008304 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02008305 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008306 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02008307 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008308 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008309 SSL *ssl;
Emeric Brun7f56e742012-10-19 18:15:40 +02008310
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008311 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008312 ssl = ssl_sock_get_ssl_object(conn);
8313 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008314 return 0;
8315
Willy Tarreau911db9b2020-01-23 16:27:54 +01008316 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun7f56e742012-10-19 18:15:40 +02008317 smp->flags |= SMP_F_MAY_CHANGE;
8318 return 0;
8319 }
8320
Emeric Brunba841a12014-04-30 17:05:08 +02008321 if (cert_peer)
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008322 crt = SSL_get_peer_certificate(ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008323 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008324 crt = SSL_get_certificate(ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02008325 if (!crt)
8326 return 0;
8327
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008328 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
8329 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02008330
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008331 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
8332 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02008333 /* SSL_get_peer_certificate increase X509 * ref count */
8334 if (cert_peer)
8335 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02008336 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02008337 }
Emeric Brun7f56e742012-10-19 18:15:40 +02008338
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008339 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008340 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008341 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02008342 /* SSL_get_peer_certificate increase X509 * ref count */
8343 if (cert_peer)
8344 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02008345
8346 return 1;
8347}
8348
Emeric Brunba841a12014-04-30 17:05:08 +02008349/* string, returns the certificate's key algorithm.
8350 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
8351 * should be use.
8352 */
Emeric Brun521a0112012-10-22 12:22:55 +02008353static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008354smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02008355{
Emeric Brunba841a12014-04-30 17:05:08 +02008356 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02008357 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008358 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02008359 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008360 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008361 SSL *ssl;
Emeric Brun521a0112012-10-22 12:22:55 +02008362
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008363 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008364 ssl = ssl_sock_get_ssl_object(conn);
8365 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008366 return 0;
8367
Willy Tarreau911db9b2020-01-23 16:27:54 +01008368 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun521a0112012-10-22 12:22:55 +02008369 smp->flags |= SMP_F_MAY_CHANGE;
8370 return 0;
8371 }
8372
Emeric Brunba841a12014-04-30 17:05:08 +02008373 if (cert_peer)
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008374 crt = SSL_get_peer_certificate(ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02008375 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008376 crt = SSL_get_certificate(ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02008377 if (!crt)
8378 return 0;
8379
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02008380 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
8381 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02008382
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008383 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
8384 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02008385 /* SSL_get_peer_certificate increase X509 * ref count */
8386 if (cert_peer)
8387 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02008388 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02008389 }
Emeric Brun521a0112012-10-22 12:22:55 +02008390
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008391 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008392 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008393 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02008394 if (cert_peer)
8395 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02008396
8397 return 1;
8398}
8399
Emeric Brun645ae792014-04-30 14:21:06 +02008400/* boolean, returns true if front conn. transport layer is SSL.
8401 * This function is also usable on backend conn if the fetch keyword 5th
8402 * char is 'b'.
8403 */
Willy Tarreau7875d092012-09-10 08:20:03 +02008404static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008405smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008406{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008407 struct connection *conn;
8408
Christopher Fauletf98e6262020-05-06 09:42:04 +02008409 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008410 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8411 else
8412 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8413 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008414
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008415 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008416 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02008417 return 1;
8418}
8419
Emeric Brun2525b6b2012-10-18 15:59:43 +02008420/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02008421static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008422smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008423{
8424#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008425 struct connection *conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008426 SSL *ssl = ssl_sock_get_ssl_object(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008427
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008428 smp->data.type = SMP_T_BOOL;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008429 smp->data.u.sint = ssl && SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02008430 return 1;
8431#else
8432 return 0;
8433#endif
8434}
8435
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008436/* boolean, returns true if client session has been resumed.
8437 * This function is also usable on backend conn if the fetch keyword 5th
8438 * char is 'b'.
8439 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008440static int
8441smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
8442{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008443 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008444 SSL *ssl;
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008445
Christopher Fauletf98e6262020-05-06 09:42:04 +02008446 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008447 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8448 else
8449 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8450 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01008451
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008452 ssl = ssl_sock_get_ssl_object(conn);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008453
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008454 smp->data.type = SMP_T_BOOL;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008455 smp->data.u.sint = ssl && SSL_session_reused(ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02008456 return 1;
8457}
8458
Emeric Brun645ae792014-04-30 14:21:06 +02008459/* string, returns the used cipher if front conn. transport layer is SSL.
8460 * This function is also usable on backend conn if the fetch keyword 5th
8461 * char is 'b'.
8462 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008463static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008464smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008465{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008466 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008467 SSL *ssl;
Emeric Brun589fcad2012-10-16 14:13:26 +02008468
Christopher Fauletf98e6262020-05-06 09:42:04 +02008469 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008470 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8471 else
8472 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8473 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8474
Willy Tarreaube508f12016-03-10 11:47:01 +01008475 smp->flags = 0;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008476 ssl = ssl_sock_get_ssl_object(conn);
8477 if (!ssl)
Emeric Brun589fcad2012-10-16 14:13:26 +02008478 return 0;
8479
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008480 smp->data.u.str.area = (char *)SSL_get_cipher_name(ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008481 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008482 return 0;
8483
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008484 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008485 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008486 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008487
8488 return 1;
8489}
8490
Emeric Brun645ae792014-04-30 14:21:06 +02008491/* integer, returns the algoritm's keysize if front conn. transport layer
8492 * is SSL.
8493 * This function is also usable on backend conn if the fetch keyword 5th
8494 * char is 'b'.
8495 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008496static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008497smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008498{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008499 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008500 SSL *ssl;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008501 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01008502
Christopher Fauletf98e6262020-05-06 09:42:04 +02008503 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008504 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8505 else
8506 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8507 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8508
Emeric Brun589fcad2012-10-16 14:13:26 +02008509 smp->flags = 0;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008510 ssl = ssl_sock_get_ssl_object(conn);
8511 if (!ssl)
Emeric Brun589fcad2012-10-16 14:13:26 +02008512 return 0;
8513
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008514 if (!SSL_get_cipher_bits(ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008515 return 0;
8516
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008517 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008518 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008519
8520 return 1;
8521}
8522
Emeric Brun645ae792014-04-30 14:21:06 +02008523/* integer, returns the used keysize if front conn. transport layer is SSL.
8524 * This function is also usable on backend conn if the fetch keyword 5th
8525 * char is 'b'.
8526 */
Emeric Brun589fcad2012-10-16 14:13:26 +02008527static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008528smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008529{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008530 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008531 SSL *ssl;
Willy Tarreaube508f12016-03-10 11:47:01 +01008532
Christopher Fauletf98e6262020-05-06 09:42:04 +02008533 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008534 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8535 else
8536 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8537 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8538
Emeric Brun589fcad2012-10-16 14:13:26 +02008539 smp->flags = 0;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008540 ssl = ssl_sock_get_ssl_object(conn);
8541 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008542 return 0;
8543
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008544 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008545 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02008546 return 0;
8547
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008548 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02008549
8550 return 1;
8551}
8552
Bernard Spil13c53f82018-02-15 13:34:58 +01008553#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02008554static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008555smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008556{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008557 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008558 SSL *ssl;
Willy Tarreau105599c2020-02-25 08:59:23 +01008559 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008560
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008561 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008562 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008563
Christopher Fauletf98e6262020-05-06 09:42:04 +02008564 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008565 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8566 else
8567 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8568 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8569
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008570 ssl = ssl_sock_get_ssl_object(conn);
8571 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008572 return 0;
8573
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008574 smp->data.u.str.area = NULL;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008575 SSL_get0_next_proto_negotiated(ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01008576 (const unsigned char **)&smp->data.u.str.area,
8577 &len);
Willy Tarreaua33c6542012-10-15 13:19:06 +02008578
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008579 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02008580 return 0;
8581
Willy Tarreau105599c2020-02-25 08:59:23 +01008582 smp->data.u.str.data = len;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008583 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02008584}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008585#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02008586
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01008587#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02008588static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008589smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02008590{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008591 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008592 SSL *ssl;
Willy Tarreau105599c2020-02-25 08:59:23 +01008593 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008594
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008595 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008596 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02008597
Christopher Fauletf98e6262020-05-06 09:42:04 +02008598 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008599 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8600 else
8601 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8602 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard6b77f492018-11-22 18:18:29 +01008603
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008604 ssl = ssl_sock_get_ssl_object(conn);
8605 if (!ssl)
Willy Tarreauab861d32013-04-02 02:30:41 +02008606 return 0;
8607
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008608 smp->data.u.str.area = NULL;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008609 SSL_get0_alpn_selected(ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01008610 (const unsigned char **)&smp->data.u.str.area,
8611 &len);
Willy Tarreauab861d32013-04-02 02:30:41 +02008612
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008613 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02008614 return 0;
8615
Willy Tarreau105599c2020-02-25 08:59:23 +01008616 smp->data.u.str.data = len;
Willy Tarreauab861d32013-04-02 02:30:41 +02008617 return 1;
8618}
8619#endif
8620
Emeric Brun645ae792014-04-30 14:21:06 +02008621/* string, returns the used protocol if front conn. transport layer is SSL.
8622 * This function is also usable on backend conn if the fetch keyword 5th
8623 * char is 'b'.
8624 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02008625static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008626smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02008627{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008628 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008629 SSL *ssl;
Willy Tarreaube508f12016-03-10 11:47:01 +01008630
Christopher Fauletf98e6262020-05-06 09:42:04 +02008631 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008632 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8633 else
8634 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8635 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8636
Emeric Brun589fcad2012-10-16 14:13:26 +02008637 smp->flags = 0;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008638 ssl = ssl_sock_get_ssl_object(conn);
8639 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008640 return 0;
8641
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008642 smp->data.u.str.area = (char *)SSL_get_version(ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008643 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02008644 return 0;
8645
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008646 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008647 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008648 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02008649
8650 return 1;
8651}
8652
Willy Tarreau87b09662015-04-03 00:22:06 +02008653/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02008654 * This function is also usable on backend conn if the fetch keyword 5th
8655 * char is 'b'.
8656 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008657#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02008658static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008659smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02008660{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008661 struct connection *conn;
Willy Tarreaue237fe12016-03-10 17:05:28 +01008662 SSL_SESSION *ssl_sess;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008663 SSL *ssl;
Willy Tarreau105599c2020-02-25 08:59:23 +01008664 unsigned int len = 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01008665
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008666 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008667 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02008668
Christopher Fauletf98e6262020-05-06 09:42:04 +02008669 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008670 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8671 else
8672 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8673 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8674
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008675 ssl = ssl_sock_get_ssl_object(conn);
8676 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008677 return 0;
8678
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008679 ssl_sess = SSL_get_session(ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02008680 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02008681 return 0;
8682
Willy Tarreau105599c2020-02-25 08:59:23 +01008683 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, &len);
Dragan Dosenf35d69e2020-05-04 09:07:28 +02008684 if (!smp->data.u.str.area || !len)
Emeric Brunfe68f682012-10-16 14:59:28 +02008685 return 0;
8686
Willy Tarreau105599c2020-02-25 08:59:23 +01008687 smp->data.u.str.data = len;
Emeric Brunfe68f682012-10-16 14:59:28 +02008688 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02008689}
Patrick Hemmer41966772018-04-28 19:15:48 -04008690#endif
8691
Emeric Brunfe68f682012-10-16 14:59:28 +02008692
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008693#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04008694static int
Patrick Hemmer65674662019-06-04 08:13:03 -04008695smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
8696{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008697 struct connection *conn;
Patrick Hemmer65674662019-06-04 08:13:03 -04008698 struct buffer *data;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008699 SSL *ssl;
Patrick Hemmer65674662019-06-04 08:13:03 -04008700
Christopher Fauletf98e6262020-05-06 09:42:04 +02008701 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008702 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8703 else
8704 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8705 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8706
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008707 ssl = ssl_sock_get_ssl_object(conn);
8708 if (!ssl)
Patrick Hemmer65674662019-06-04 08:13:03 -04008709 return 0;
Patrick Hemmer65674662019-06-04 08:13:03 -04008710
8711 data = get_trash_chunk();
8712 if (kw[7] == 'c')
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008713 data->data = SSL_get_client_random(ssl,
Patrick Hemmer65674662019-06-04 08:13:03 -04008714 (unsigned char *) data->area,
8715 data->size);
8716 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008717 data->data = SSL_get_server_random(ssl,
Patrick Hemmer65674662019-06-04 08:13:03 -04008718 (unsigned char *) data->area,
8719 data->size);
8720 if (!data->data)
8721 return 0;
8722
8723 smp->flags = 0;
8724 smp->data.type = SMP_T_BIN;
8725 smp->data.u.str = *data;
8726
8727 return 1;
8728}
8729
8730static int
Patrick Hemmere0275472018-04-28 19:15:51 -04008731smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
8732{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008733 struct connection *conn;
Patrick Hemmere0275472018-04-28 19:15:51 -04008734 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02008735 struct buffer *data;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008736 SSL *ssl;
Patrick Hemmere0275472018-04-28 19:15:51 -04008737
Christopher Fauletf98e6262020-05-06 09:42:04 +02008738 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008739 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8740 else
8741 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8742 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8743
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008744 ssl = ssl_sock_get_ssl_object(conn);
8745 if (!ssl)
Patrick Hemmere0275472018-04-28 19:15:51 -04008746 return 0;
8747
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008748 ssl_sess = SSL_get_session(ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04008749 if (!ssl_sess)
8750 return 0;
8751
8752 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008753 data->data = SSL_SESSION_get_master_key(ssl_sess,
8754 (unsigned char *) data->area,
8755 data->size);
8756 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04008757 return 0;
8758
8759 smp->flags = 0;
8760 smp->data.type = SMP_T_BIN;
8761 smp->data.u.str = *data;
8762
8763 return 1;
8764}
8765#endif
8766
Patrick Hemmer41966772018-04-28 19:15:48 -04008767#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02008768static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008769smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02008770{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008771 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008772 SSL *ssl;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008773
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01008774 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008775 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02008776
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008777 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008778 ssl = ssl_sock_get_ssl_object(conn);
8779 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008780 return 0;
8781
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008782 smp->data.u.str.area = (char *)SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008783 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02008784 return 0;
8785
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008786 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02008787 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02008788}
Patrick Hemmer41966772018-04-28 19:15:48 -04008789#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02008790
David Sc1ad52e2014-04-08 18:48:47 -04008791static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008792smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
8793{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008794 struct connection *conn;
8795 struct ssl_capture *capture;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008796 SSL *ssl;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008797
8798 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008799 ssl = ssl_sock_get_ssl_object(conn);
8800 if (!ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008801 return 0;
8802
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008803 capture = SSL_get_ex_data(ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008804 if (!capture)
8805 return 0;
8806
8807 smp->flags = SMP_F_CONST;
8808 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008809 smp->data.u.str.area = capture->ciphersuite;
8810 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008811 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008812}
8813
8814static int
8815smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
8816{
Willy Tarreau83061a82018-07-13 11:56:34 +02008817 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008818
8819 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8820 return 0;
8821
8822 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008823 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008824 smp->data.type = SMP_T_BIN;
8825 smp->data.u.str = *data;
8826 return 1;
8827}
8828
8829static int
8830smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
8831{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008832 struct connection *conn;
8833 struct ssl_capture *capture;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008834 SSL *ssl;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008835
8836 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008837 ssl = ssl_sock_get_ssl_object(conn);
8838 if (!ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008839 return 0;
8840
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008841 capture = SSL_get_ex_data(ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008842 if (!capture)
8843 return 0;
8844
8845 smp->data.type = SMP_T_SINT;
8846 smp->data.u.sint = capture->xxh64;
8847 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008848}
8849
8850static int
8851smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
8852{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008853#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02008854 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008855 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008856
8857 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
8858 return 0;
8859
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008860 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008861 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008862 const char *str;
8863 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008864 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008865 uint16_t id = (bin[0] << 8) | bin[1];
8866#if defined(OPENSSL_IS_BORINGSSL)
8867 cipher = SSL_get_cipher_by_value(id);
8868#else
Willy Tarreaub7290772018-10-15 11:01:59 +02008869 struct connection *conn = __objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008870 SSL *ssl = ssl_sock_get_ssl_object(conn);
8871 cipher = SSL_CIPHER_find(ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02008872#endif
8873 str = SSL_CIPHER_get_name(cipher);
8874 if (!str || strcmp(str, "(NONE)") == 0)
8875 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008876 else
8877 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
8878 }
8879 smp->data.type = SMP_T_STR;
8880 smp->data.u.str = *data;
8881 return 1;
8882#else
8883 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
8884#endif
8885}
8886
Willy Tarreau9a1ab082019-05-09 13:26:41 +02008887#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01008888static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008889smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04008890{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008891 struct connection *conn;
David Sc1ad52e2014-04-08 18:48:47 -04008892 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02008893 struct buffer *finished_trash;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008894 SSL *ssl;
David Sc1ad52e2014-04-08 18:48:47 -04008895
Christopher Fauletf98e6262020-05-06 09:42:04 +02008896 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02008897 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
8898 else
8899 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
8900 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
8901
David Sc1ad52e2014-04-08 18:48:47 -04008902 smp->flags = 0;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008903 ssl = ssl_sock_get_ssl_object(conn);
8904 if (!ssl)
David Sc1ad52e2014-04-08 18:48:47 -04008905 return 0;
8906
Willy Tarreau911db9b2020-01-23 16:27:54 +01008907 if (conn->flags & CO_FL_WAIT_XPRT) {
David Sc1ad52e2014-04-08 18:48:47 -04008908 smp->flags |= SMP_F_MAY_CHANGE;
8909 return 0;
8910 }
8911
8912 finished_trash = get_trash_chunk();
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008913 if (!SSL_session_reused(ssl))
8914 finished_len = SSL_get_peer_finished(ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008915 finished_trash->area,
8916 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008917 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02008918 finished_len = SSL_get_finished(ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008919 finished_trash->area,
8920 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04008921
8922 if (!finished_len)
8923 return 0;
8924
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008925 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02008926 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008927 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04008928
8929 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04008930}
Patrick Hemmer41966772018-04-28 19:15:48 -04008931#endif
David Sc1ad52e2014-04-08 18:48:47 -04008932
Emeric Brun2525b6b2012-10-18 15:59:43 +02008933/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008934static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008935smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008936{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008937 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008938 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008939
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008940 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008941 if (!conn || conn->xprt != &ssl_sock)
8942 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008943 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008944
Willy Tarreau911db9b2020-01-23 16:27:54 +01008945 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008946 smp->flags = SMP_F_MAY_CHANGE;
8947 return 0;
8948 }
8949
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008950 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008951 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008952 smp->flags = 0;
8953
8954 return 1;
8955}
8956
Emeric Brun2525b6b2012-10-18 15:59:43 +02008957/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02008958static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008959smp_fetch_ssl_c_ca_err_depth(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008960{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008961 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008962 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008963
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008964 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008965 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02008966 return 0;
8967
Willy Tarreau911db9b2020-01-23 16:27:54 +01008968 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008969 smp->flags = SMP_F_MAY_CHANGE;
8970 return 0;
8971 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008972 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02008973
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008974 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008975 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02008976 smp->flags = 0;
8977
8978 return 1;
8979}
8980
Emeric Brun2525b6b2012-10-18 15:59:43 +02008981/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02008982static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02008983smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02008984{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008985 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008986 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008987
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02008988 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008989 if (!conn || conn->xprt != &ssl_sock)
8990 return 0;
8991
Willy Tarreau911db9b2020-01-23 16:27:54 +01008992 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02008993 smp->flags = SMP_F_MAY_CHANGE;
8994 return 0;
8995 }
8996
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01008997 ctx = conn->xprt_ctx;
8998
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02008999 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01009000 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02009001 smp->flags = 0;
9002
9003 return 1;
9004}
9005
Emeric Brun2525b6b2012-10-18 15:59:43 +02009006/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009007static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02009008smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009009{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009010 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02009011 SSL *ssl;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009012
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02009013 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02009014 ssl = ssl_sock_get_ssl_object(conn);
9015 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009016 return 0;
9017
Willy Tarreau911db9b2020-01-23 16:27:54 +01009018 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009019 smp->flags = SMP_F_MAY_CHANGE;
9020 return 0;
9021 }
9022
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02009023 smp->data.type = SMP_T_SINT;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02009024 smp->data.u.sint = (long long int)SSL_get_verify_result(ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02009025 smp->flags = 0;
9026
9027 return 1;
9028}
9029
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009030/* for ca-file and ca-verify-file */
9031static int ssl_bind_parse_ca_file_common(char **args, int cur_arg, char **ca_file_p, char **err)
Emeric Brund94b3fe2012-09-20 18:23:56 +02009032{
9033 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009034 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009035 return ERR_ALERT | ERR_FATAL;
9036 }
9037
Willy Tarreauef934602016-12-22 23:12:01 +01009038 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009039 memprintf(ca_file_p, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009040 else
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009041 memprintf(ca_file_p, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009042
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009043 if (!ssl_store_load_locations_file(*ca_file_p)) {
9044 memprintf(err, "'%s' : unable to load %s", args[cur_arg], *ca_file_p);
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009045 return ERR_ALERT | ERR_FATAL;
9046 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02009047 return 0;
9048}
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009049
9050/* parse the "ca-file" bind keyword */
9051static int ssl_bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9052{
9053 return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_file, err);
9054}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009055static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9056{
9057 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
9058}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009059
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01009060/* parse the "ca-verify-file" bind keyword */
9061static int ssl_bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9062{
9063 return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_verify_file, err);
9064}
9065static int bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9066{
9067 return ssl_bind_parse_ca_verify_file(args, cur_arg, px, &conf->ssl_conf, err);
9068}
9069
Christopher Faulet31af49d2015-06-09 17:29:50 +02009070/* parse the "ca-sign-file" bind keyword */
9071static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9072{
9073 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009074 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009075 return ERR_ALERT | ERR_FATAL;
9076 }
9077
Willy Tarreauef934602016-12-22 23:12:01 +01009078 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
9079 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009080 else
9081 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
9082
9083 return 0;
9084}
9085
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009086/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02009087static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9088{
9089 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009090 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02009091 return ERR_ALERT | ERR_FATAL;
9092 }
9093 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
9094 return 0;
9095}
9096
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009097/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009098static int ssl_bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009099{
9100 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009101 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009102 return ERR_ALERT | ERR_FATAL;
9103 }
9104
Emeric Brun76d88952012-10-05 15:47:31 +02009105 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02009106 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009107 return 0;
9108}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009109static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9110{
9111 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
9112}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009113
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009114#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009115/* parse the "ciphersuites" bind keyword */
9116static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9117{
9118 if (!*args[cur_arg + 1]) {
9119 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
9120 return ERR_ALERT | ERR_FATAL;
9121 }
9122
9123 free(conf->ciphersuites);
9124 conf->ciphersuites = strdup(args[cur_arg + 1]);
9125 return 0;
9126}
9127static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9128{
9129 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
9130}
9131#endif
9132
Willy Tarreaubbc91962019-10-16 16:42:19 +02009133/* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau4348fad2012-09-20 16:48:07 +02009134static int bind_parse_crt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009135{
Willy Tarreau38011032013-08-13 16:59:39 +02009136 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02009137
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009138 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009139 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009140 return ERR_ALERT | ERR_FATAL;
9141 }
9142
Willy Tarreauef934602016-12-22 23:12:01 +01009143 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
9144 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02009145 memprintf(err, "'%s' : path too long", args[cur_arg]);
9146 return ERR_ALERT | ERR_FATAL;
9147 }
Willy Tarreauef934602016-12-22 23:12:01 +01009148 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreaubbc91962019-10-16 16:42:19 +02009149 return ssl_sock_load_cert(path, conf, err);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009150 }
9151
Willy Tarreaubbc91962019-10-16 16:42:19 +02009152 return ssl_sock_load_cert(args[cur_arg + 1], conf, err);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009153}
9154
Willy Tarreaubbc91962019-10-16 16:42:19 +02009155/* parse the "crt-list" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009156static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9157{
Willy Tarreaubbc91962019-10-16 16:42:19 +02009158 int err_code;
9159
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009160 if (!*args[cur_arg + 1]) {
9161 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
9162 return ERR_ALERT | ERR_FATAL;
9163 }
9164
William Lallemand6be66ec2020-03-06 22:26:32 +01009165 err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], 0, conf, px, err);
Willy Tarreaubbc91962019-10-16 16:42:19 +02009166 if (err_code)
Willy Tarreauad1731d2013-04-02 17:35:58 +02009167 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009168
Willy Tarreaubbc91962019-10-16 16:42:19 +02009169 return err_code;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01009170}
9171
Emeric Brunfb510ea2012-10-05 12:00:26 +02009172/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009173static int ssl_bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
Emeric Brund94b3fe2012-09-20 18:23:56 +02009174{
Emeric Brun051cdab2012-10-02 19:25:50 +02009175#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009176 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
Emeric Brun051cdab2012-10-02 19:25:50 +02009177 return ERR_ALERT | ERR_FATAL;
9178#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02009179 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009180 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009181 return ERR_ALERT | ERR_FATAL;
9182 }
Emeric Brun2b58d042012-09-20 17:10:03 +02009183
Willy Tarreauef934602016-12-22 23:12:01 +01009184 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
9185 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009186 else
9187 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02009188
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009189 if (!ssl_store_load_locations_file(conf->crl_file)) {
9190 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file);
9191 return ERR_ALERT | ERR_FATAL;
9192 }
Emeric Brun2b58d042012-09-20 17:10:03 +02009193 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02009194#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02009195}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009196static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9197{
9198 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
9199}
Emeric Brun2b58d042012-09-20 17:10:03 +02009200
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009201/* parse the "curves" bind keyword keyword */
9202static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9203{
Lukas Tribusd14b49c2019-11-24 18:20:40 +01009204#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009205 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009206 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009207 return ERR_ALERT | ERR_FATAL;
9208 }
9209 conf->curves = strdup(args[cur_arg + 1]);
9210 return 0;
9211#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009212 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01009213 return ERR_ALERT | ERR_FATAL;
9214#endif
9215}
9216static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9217{
9218 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
9219}
9220
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009221/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009222static int ssl_bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
Emeric Brun2b58d042012-09-20 17:10:03 +02009223{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009224#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Tim Duesterhus93128532019-11-23 23:45:10 +01009225 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02009226 return ERR_ALERT | ERR_FATAL;
9227#elif defined(OPENSSL_NO_ECDH)
Tim Duesterhus93128532019-11-23 23:45:10 +01009228 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02009229 return ERR_ALERT | ERR_FATAL;
9230#else
9231 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009232 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02009233 return ERR_ALERT | ERR_FATAL;
9234 }
9235
9236 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009237
9238 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02009239#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009240}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009241static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9242{
9243 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
9244}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009245
Bertrand Jacquinff13c062016-11-13 16:37:11 +00009246/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02009247static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9248{
9249 int code;
9250 char *p = args[cur_arg + 1];
9251 unsigned long long *ignerr = &conf->crt_ignerr;
9252
9253 if (!*p) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009254 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
Emeric Brun81c00f02012-09-21 14:31:21 +02009255 return ERR_ALERT | ERR_FATAL;
9256 }
9257
9258 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
9259 ignerr = &conf->ca_ignerr;
9260
9261 if (strcmp(p, "all") == 0) {
9262 *ignerr = ~0ULL;
9263 return 0;
9264 }
9265
9266 while (p) {
9267 code = atoi(p);
9268 if ((code <= 0) || (code > 63)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009269 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
9270 args[cur_arg], code, args[cur_arg + 1]);
Emeric Brun81c00f02012-09-21 14:31:21 +02009271 return ERR_ALERT | ERR_FATAL;
9272 }
9273 *ignerr |= 1ULL << code;
9274 p = strchr(p, ',');
9275 if (p)
9276 p++;
9277 }
9278
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009279 return 0;
9280}
9281
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009282/* parse tls_method_options "no-xxx" and "force-xxx" */
9283static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009284{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009285 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009286 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009287 p = strchr(arg, '-');
9288 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009289 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009290 p++;
9291 if (!strcmp(p, "sslv3"))
9292 v = CONF_SSLV3;
9293 else if (!strcmp(p, "tlsv10"))
9294 v = CONF_TLSV10;
9295 else if (!strcmp(p, "tlsv11"))
9296 v = CONF_TLSV11;
9297 else if (!strcmp(p, "tlsv12"))
9298 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02009299 else if (!strcmp(p, "tlsv13"))
9300 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009301 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009302 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009303 if (!strncmp(arg, "no-", 3))
9304 methods->flags |= methodVersions[v].flag;
9305 else if (!strncmp(arg, "force-", 6))
9306 methods->min = methods->max = v;
9307 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009308 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02009309 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009310 fail:
Tim Duesterhus93128532019-11-23 23:45:10 +01009311 memprintf(err, "'%s' : option not implemented", arg);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009312 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02009313}
9314
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009315static int bind_parse_tls_method_options(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009316{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009317 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009318}
9319
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009320static int srv_parse_tls_method_options(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009321{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009322 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
9323}
9324
9325/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
9326static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
9327{
9328 uint16_t i, v = 0;
9329 char *argv = args[cur_arg + 1];
9330 if (!*argv) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009331 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009332 return ERR_ALERT | ERR_FATAL;
9333 }
9334 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
9335 if (!strcmp(argv, methodVersions[i].name))
9336 v = i;
9337 if (!v) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009338 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009339 return ERR_ALERT | ERR_FATAL;
9340 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009341 if (!strcmp("ssl-min-ver", args[cur_arg]))
9342 methods->min = v;
9343 else if (!strcmp("ssl-max-ver", args[cur_arg]))
9344 methods->max = v;
9345 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009346 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009347 return ERR_ALERT | ERR_FATAL;
9348 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009349 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009350}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02009351
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02009352static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9353{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02009354#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01009355 ha_warning("crt-list: ssl-min-ver and ssl-max-ver are not supported with this Openssl version (skipped).\n");
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02009356#endif
9357 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
9358}
9359
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009360static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9361{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009362 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02009363}
9364
9365static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9366{
9367 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
9368}
9369
Emeric Brun2d0c4822012-10-02 13:45:20 +02009370/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01009371static int bind_parse_no_tls_tickets(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Emeric Brun2d0c4822012-10-02 13:45:20 +02009372{
Emeric Brun89675492012-10-05 13:48:26 +02009373 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02009374 return 0;
9375}
Emeric Brun2d0c4822012-10-02 13:45:20 +02009376
Olivier Houchardc2aae742017-09-22 18:26:28 +02009377/* parse the "allow-0rtt" bind keyword */
9378static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9379{
9380 conf->early_data = 1;
9381 return 0;
9382}
9383
9384static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9385{
Olivier Houchard9679ac92017-10-27 14:58:08 +02009386 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02009387 return 0;
9388}
9389
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009390/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009391static int ssl_bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009392{
Bernard Spil13c53f82018-02-15 13:34:58 +01009393#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009394 char *p1, *p2;
9395
9396 if (!*args[cur_arg + 1]) {
9397 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
9398 return ERR_ALERT | ERR_FATAL;
9399 }
9400
9401 free(conf->npn_str);
9402
Willy Tarreau3724da12016-02-12 17:11:12 +01009403 /* the NPN string is built as a suite of (<len> <name>)*,
9404 * so we reuse each comma to store the next <len> and need
9405 * one more for the end of the string.
9406 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009407 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01009408 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009409 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
9410
9411 /* replace commas with the name length */
9412 p1 = conf->npn_str;
9413 p2 = p1 + 1;
9414 while (1) {
9415 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
9416 if (!p2)
9417 p2 = p1 + 1 + strlen(p1 + 1);
9418
9419 if (p2 - (p1 + 1) > 255) {
9420 *p2 = '\0';
9421 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
9422 return ERR_ALERT | ERR_FATAL;
9423 }
9424
9425 *p1 = p2 - (p1 + 1);
9426 p1 = p2;
9427
9428 if (!*p2)
9429 break;
9430
9431 *(p2++) = '\0';
9432 }
9433 return 0;
9434#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009435 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02009436 return ERR_ALERT | ERR_FATAL;
9437#endif
9438}
9439
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009440static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9441{
9442 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
9443}
9444
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009445
9446/* Parses a alpn string and converts it to the right format for the SSL api */
9447int ssl_sock_parse_alpn(char *arg, char **alpn_str, int *alpn_len, char **err)
Willy Tarreauab861d32013-04-02 02:30:41 +02009448{
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009449 char *p1, *p2, *alpn = NULL;
9450 int len, ret = 0;
Willy Tarreauab861d32013-04-02 02:30:41 +02009451
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009452 *alpn_str = NULL;
9453 *alpn_len = 0;
Willy Tarreauab861d32013-04-02 02:30:41 +02009454
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009455 if (!*arg) {
9456 memprintf(err, "missing the comma-delimited ALPN protocol suite");
9457 goto error;
9458 }
Willy Tarreauab861d32013-04-02 02:30:41 +02009459
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01009460 /* the ALPN string is built as a suite of (<len> <name>)*,
9461 * so we reuse each comma to store the next <len> and need
9462 * one more for the end of the string.
9463 */
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009464 len = strlen(arg) + 1;
9465 alpn = calloc(1, len+1);
9466 if (!alpn) {
9467 memprintf(err, "'%s' : out of memory", arg);
9468 goto error;
9469 }
9470 memcpy(alpn+1, arg, len);
Willy Tarreauab861d32013-04-02 02:30:41 +02009471
9472 /* replace commas with the name length */
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009473 p1 = alpn;
Willy Tarreauab861d32013-04-02 02:30:41 +02009474 p2 = p1 + 1;
9475 while (1) {
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009476 p2 = memchr(p1 + 1, ',', alpn + len - (p1 + 1));
Willy Tarreauab861d32013-04-02 02:30:41 +02009477 if (!p2)
9478 p2 = p1 + 1 + strlen(p1 + 1);
9479
9480 if (p2 - (p1 + 1) > 255) {
9481 *p2 = '\0';
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009482 memprintf(err, "ALPN protocol name too long : '%s'", p1 + 1);
9483 goto error;
Willy Tarreauab861d32013-04-02 02:30:41 +02009484 }
9485
9486 *p1 = p2 - (p1 + 1);
9487 p1 = p2;
9488
9489 if (!*p2)
9490 break;
9491
9492 *(p2++) = '\0';
9493 }
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009494
9495 *alpn_str = alpn;
9496 *alpn_len = len;
9497
9498 out:
9499 return ret;
9500
9501 error:
9502 free(alpn);
9503 ret = ERR_ALERT | ERR_FATAL;
9504 goto out;
9505}
9506
9507/* parse the "alpn" bind keyword */
9508static int ssl_bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9509{
9510#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
9511 int ret;
9512
9513 free(conf->alpn_str);
9514
9515 ret = ssl_sock_parse_alpn(args[cur_arg + 1], &conf->alpn_str, &conf->alpn_len, err);
9516 if (ret)
9517 memprintf(err, "'%s' : %s", args[cur_arg], *err);
9518 return ret;
Willy Tarreauab861d32013-04-02 02:30:41 +02009519#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009520 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
Willy Tarreauab861d32013-04-02 02:30:41 +02009521 return ERR_ALERT | ERR_FATAL;
9522#endif
9523}
9524
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009525static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9526{
9527 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
9528}
9529
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009530/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02009531static int bind_parse_ssl(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009532{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01009533 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02009534 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02009535
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009536 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
9537 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Jerome Magninb203ff62020-04-03 15:28:22 +02009538#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
9539 if (global_ssl.listen_default_curves && !conf->ssl_conf.curves)
9540 conf->ssl_conf.curves = strdup(global_ssl.listen_default_curves);
9541#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009542#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009543 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
9544 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
9545#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01009546 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02009547 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
9548 if (!conf->ssl_conf.ssl_methods.min)
9549 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
9550 if (!conf->ssl_conf.ssl_methods.max)
9551 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02009552
Willy Tarreau79eeafa2012-09-14 07:53:05 +02009553 return 0;
9554}
9555
Lukas Tribus53ae85c2017-05-04 15:45:40 +00009556/* parse the "prefer-client-ciphers" bind keyword */
9557static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9558{
9559 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
9560 return 0;
9561}
9562
Christopher Faulet31af49d2015-06-09 17:29:50 +02009563/* parse the "generate-certificates" bind keyword */
9564static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9565{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009566#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02009567 conf->generate_certs = 1;
9568#else
9569 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
9570 err && *err ? *err : "");
9571#endif
9572 return 0;
9573}
9574
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009575/* parse the "strict-sni" bind keyword */
9576static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9577{
9578 conf->strict_sni = 1;
9579 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009580}
9581
9582/* parse the "tls-ticket-keys" bind keyword */
9583static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9584{
9585#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Christopher Faulete566f3d2019-10-21 09:55:49 +02009586 FILE *f = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009587 int i = 0;
9588 char thisline[LINESIZE];
Christopher Faulete566f3d2019-10-21 09:55:49 +02009589 struct tls_keys_ref *keys_ref = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009590
9591 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009592 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009593 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009594 }
9595
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009596 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009597 if (keys_ref) {
9598 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009599 conf->keys_ref = keys_ref;
9600 return 0;
9601 }
9602
Christopher Faulete566f3d2019-10-21 09:55:49 +02009603 keys_ref = calloc(1, sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01009604 if (!keys_ref) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009605 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009606 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009607 }
9608
Emeric Brun9e754772019-01-10 17:51:55 +01009609 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01009610 if (!keys_ref->tlskeys) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009611 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009612 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009613 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009614
9615 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009616 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009617 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009618 }
9619
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009620 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01009621 if (!keys_ref->filename) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009622 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009623 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01009624 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009625
Emeric Brun9e754772019-01-10 17:51:55 +01009626 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009627 while (fgets(thisline, sizeof(thisline), f) != NULL) {
9628 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01009629 int dec_size;
9630
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009631 /* Strip newline characters from the end */
9632 if(thisline[len - 1] == '\n')
9633 thisline[--len] = 0;
9634
9635 if(thisline[len - 1] == '\r')
9636 thisline[--len] = 0;
9637
Emeric Brun9e754772019-01-10 17:51:55 +01009638 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
9639 if (dec_size < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009640 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009641 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009642 }
Emeric Brun9e754772019-01-10 17:51:55 +01009643 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
9644 keys_ref->key_size_bits = 128;
9645 }
9646 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
9647 keys_ref->key_size_bits = 256;
9648 }
9649 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
9650 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
9651 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009652 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009653 goto fail;
Emeric Brun9e754772019-01-10 17:51:55 +01009654 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009655 i++;
9656 }
9657
9658 if (i < TLS_TICKETS_NO) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009659 memprintf(err, "'%s' : please supply at least %d keys in the tls-tickets-file", args[cur_arg+1], TLS_TICKETS_NO);
Christopher Faulete566f3d2019-10-21 09:55:49 +02009660 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009661 }
9662
9663 fclose(f);
9664
9665 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01009666 i -= 2;
9667 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009668 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02009669 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01009670 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02009671 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009672
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02009673 LIST_ADD(&tlskeys_reference, &keys_ref->list);
9674
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009675 return 0;
Christopher Faulete566f3d2019-10-21 09:55:49 +02009676
9677 fail:
9678 if (f)
9679 fclose(f);
9680 if (keys_ref) {
9681 free(keys_ref->filename);
9682 free(keys_ref->tlskeys);
9683 free(keys_ref);
9684 }
9685 return ERR_ALERT | ERR_FATAL;
9686
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009687#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009688 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01009689 return ERR_ALERT | ERR_FATAL;
9690#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01009691}
9692
Emeric Brund94b3fe2012-09-20 18:23:56 +02009693/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009694static int ssl_bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
Emeric Brund94b3fe2012-09-20 18:23:56 +02009695{
9696 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009697 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009698 return ERR_ALERT | ERR_FATAL;
9699 }
9700
9701 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009702 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009703 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009704 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009705 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01009706 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02009707 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01009708 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
9709 args[cur_arg], args[cur_arg + 1]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02009710 return ERR_ALERT | ERR_FATAL;
9711 }
9712
9713 return 0;
9714}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01009715static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9716{
9717 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
9718}
Emeric Brund94b3fe2012-09-20 18:23:56 +02009719
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02009720/* parse the "no-ca-names" bind keyword */
9721static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
9722{
9723 conf->no_ca_names = 1;
9724 return 0;
9725}
9726static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
9727{
9728 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
9729}
9730
Willy Tarreau92faadf2012-10-10 23:04:25 +02009731/************** "server" keywords ****************/
9732
Olivier Houchardc7566002018-11-20 23:33:50 +01009733/* parse the "npn" bind keyword */
9734static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9735{
9736#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
9737 char *p1, *p2;
9738
9739 if (!*args[*cur_arg + 1]) {
9740 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
9741 return ERR_ALERT | ERR_FATAL;
9742 }
9743
9744 free(newsrv->ssl_ctx.npn_str);
9745
9746 /* the NPN string is built as a suite of (<len> <name>)*,
9747 * so we reuse each comma to store the next <len> and need
9748 * one more for the end of the string.
9749 */
9750 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
9751 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
9752 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
9753 newsrv->ssl_ctx.npn_len);
9754
9755 /* replace commas with the name length */
9756 p1 = newsrv->ssl_ctx.npn_str;
9757 p2 = p1 + 1;
9758 while (1) {
9759 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
9760 newsrv->ssl_ctx.npn_len - (p1 + 1));
9761 if (!p2)
9762 p2 = p1 + 1 + strlen(p1 + 1);
9763
9764 if (p2 - (p1 + 1) > 255) {
9765 *p2 = '\0';
9766 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
9767 return ERR_ALERT | ERR_FATAL;
9768 }
9769
9770 *p1 = p2 - (p1 + 1);
9771 p1 = p2;
9772
9773 if (!*p2)
9774 break;
9775
9776 *(p2++) = '\0';
9777 }
9778 return 0;
9779#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009780 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009781 return ERR_ALERT | ERR_FATAL;
9782#endif
9783}
9784
Olivier Houchard92150142018-12-21 19:47:01 +01009785/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01009786static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9787{
9788#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard92150142018-12-21 19:47:01 +01009789 char **alpn_str;
9790 int *alpn_len;
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009791 int ret;
Olivier Houchardc7566002018-11-20 23:33:50 +01009792
Olivier Houchard92150142018-12-21 19:47:01 +01009793 if (*args[*cur_arg] == 'c') {
9794 alpn_str = &newsrv->check.alpn_str;
9795 alpn_len = &newsrv->check.alpn_len;
9796 } else {
9797 alpn_str = &newsrv->ssl_ctx.alpn_str;
9798 alpn_len = &newsrv->ssl_ctx.alpn_len;
9799
9800 }
Olivier Houchardc7566002018-11-20 23:33:50 +01009801
Olivier Houchard92150142018-12-21 19:47:01 +01009802 free(*alpn_str);
Christopher Fauletd75f57e2020-04-20 18:32:29 +02009803 ret = ssl_sock_parse_alpn(args[*cur_arg + 1], alpn_str, alpn_len, err);
9804 if (ret)
9805 memprintf(err, "'%s' : %s", args[*cur_arg], *err);
9806 return ret;
Olivier Houchardc7566002018-11-20 23:33:50 +01009807#else
Tim Duesterhus93128532019-11-23 23:45:10 +01009808 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01009809 return ERR_ALERT | ERR_FATAL;
9810#endif
9811}
9812
Emeric Brunef42d922012-10-11 16:11:36 +02009813/* parse the "ca-file" server keyword */
9814static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9815{
9816 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009817 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009818 return ERR_ALERT | ERR_FATAL;
9819 }
9820
Willy Tarreauef934602016-12-22 23:12:01 +01009821 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9822 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009823 else
9824 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
9825
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02009826 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) {
9827 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file);
9828 return ERR_ALERT | ERR_FATAL;
9829 }
Emeric Brunef42d922012-10-11 16:11:36 +02009830 return 0;
9831}
9832
Olivier Houchard9130a962017-10-17 17:33:43 +02009833/* parse the "check-sni" server keyword */
9834static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9835{
9836 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009837 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
Olivier Houchard9130a962017-10-17 17:33:43 +02009838 return ERR_ALERT | ERR_FATAL;
9839 }
9840
9841 newsrv->check.sni = strdup(args[*cur_arg + 1]);
9842 if (!newsrv->check.sni) {
9843 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
9844 return ERR_ALERT | ERR_FATAL;
9845 }
9846 return 0;
9847
9848}
9849
Willy Tarreau92faadf2012-10-10 23:04:25 +02009850/* parse the "check-ssl" server keyword */
9851static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9852{
9853 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01009854 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
9855 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009856#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009857 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
9858 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
9859#endif
Willy Tarreauef934602016-12-22 23:12:01 +01009860 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02009861 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
9862 if (!newsrv->ssl_ctx.methods.min)
9863 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
9864 if (!newsrv->ssl_ctx.methods.max)
9865 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
9866
Willy Tarreau92faadf2012-10-10 23:04:25 +02009867 return 0;
9868}
9869
9870/* parse the "ciphers" server keyword */
9871static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9872{
9873 if (!*args[*cur_arg + 1]) {
9874 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9875 return ERR_ALERT | ERR_FATAL;
9876 }
9877
9878 free(newsrv->ssl_ctx.ciphers);
9879 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
9880 return 0;
9881}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009882
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009883#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009884/* parse the "ciphersuites" server keyword */
9885static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9886{
9887 if (!*args[*cur_arg + 1]) {
9888 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
9889 return ERR_ALERT | ERR_FATAL;
9890 }
9891
9892 free(newsrv->ssl_ctx.ciphersuites);
9893 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
9894 return 0;
9895}
9896#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02009897
Emeric Brunef42d922012-10-11 16:11:36 +02009898/* parse the "crl-file" server keyword */
9899static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9900{
9901#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01009902 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009903 return ERR_ALERT | ERR_FATAL;
9904#else
9905 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009906 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02009907 return ERR_ALERT | ERR_FATAL;
9908 }
9909
Willy Tarreauef934602016-12-22 23:12:01 +01009910 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
9911 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02009912 else
9913 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
9914
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01009915 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) {
9916 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file);
9917 return ERR_ALERT | ERR_FATAL;
9918 }
Emeric Brunef42d922012-10-11 16:11:36 +02009919 return 0;
9920#endif
9921}
9922
Emeric Bruna7aa3092012-10-26 12:58:00 +02009923/* parse the "crt" server keyword */
9924static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9925{
9926 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01009927 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009928 return ERR_ALERT | ERR_FATAL;
9929 }
9930
Willy Tarreauef934602016-12-22 23:12:01 +01009931 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01009932 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02009933 else
9934 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
9935
9936 return 0;
9937}
Emeric Brunef42d922012-10-11 16:11:36 +02009938
Frédéric Lécaille340ae602017-03-13 10:38:04 +01009939/* parse the "no-check-ssl" server keyword */
9940static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9941{
Christopher Fauletf61f33a2020-03-27 18:55:49 +01009942 newsrv->check.use_ssl = -1;
Frédéric Lécaille340ae602017-03-13 10:38:04 +01009943 free(newsrv->ssl_ctx.ciphers);
9944 newsrv->ssl_ctx.ciphers = NULL;
9945 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
9946 return 0;
9947}
9948
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01009949/* parse the "no-send-proxy-v2-ssl" server keyword */
9950static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9951{
9952 newsrv->pp_opts &= ~SRV_PP_V2;
9953 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9954 return 0;
9955}
9956
9957/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
9958static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9959{
9960 newsrv->pp_opts &= ~SRV_PP_V2;
9961 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
9962 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
9963 return 0;
9964}
9965
Frédéric Lécaillee381d762017-03-13 11:54:17 +01009966/* parse the "no-ssl" server keyword */
9967static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9968{
Christopher Fauletf61f33a2020-03-27 18:55:49 +01009969 newsrv->use_ssl = -1;
Frédéric Lécaillee381d762017-03-13 11:54:17 +01009970 free(newsrv->ssl_ctx.ciphers);
9971 newsrv->ssl_ctx.ciphers = NULL;
9972 return 0;
9973}
9974
Olivier Houchard522eea72017-11-03 16:27:47 +01009975/* parse the "allow-0rtt" server keyword */
9976static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9977{
9978 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
9979 return 0;
9980}
9981
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01009982/* parse the "no-ssl-reuse" server keyword */
9983static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9984{
9985 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
9986 return 0;
9987}
9988
Emeric Brunf9c5c472012-10-11 15:28:34 +02009989/* parse the "no-tls-tickets" server keyword */
9990static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9991{
9992 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
9993 return 0;
9994}
David Safb76832014-05-08 23:42:08 -04009995/* parse the "send-proxy-v2-ssl" server keyword */
9996static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
9997{
9998 newsrv->pp_opts |= SRV_PP_V2;
9999 newsrv->pp_opts |= SRV_PP_V2_SSL;
10000 return 0;
10001}
10002
10003/* parse the "send-proxy-v2-ssl-cn" server keyword */
10004static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10005{
10006 newsrv->pp_opts |= SRV_PP_V2;
10007 newsrv->pp_opts |= SRV_PP_V2_SSL;
10008 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
10009 return 0;
10010}
Emeric Brunf9c5c472012-10-11 15:28:34 +020010011
Willy Tarreau732eac42015-07-09 11:40:25 +020010012/* parse the "sni" server keyword */
10013static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10014{
10015#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
10016 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
10017 return ERR_ALERT | ERR_FATAL;
10018#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010010019 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +020010020
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010010021 arg = args[*cur_arg + 1];
10022 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +020010023 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
10024 return ERR_ALERT | ERR_FATAL;
10025 }
10026
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010010027 free(newsrv->sni_expr);
10028 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +020010029
Willy Tarreau732eac42015-07-09 11:40:25 +020010030 return 0;
10031#endif
10032}
10033
Willy Tarreau92faadf2012-10-10 23:04:25 +020010034/* parse the "ssl" server keyword */
10035static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10036{
10037 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +010010038 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
10039 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010040#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010041 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
10042 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
10043#endif
Jerome Magnin2e8d52f2020-04-22 11:40:18 +020010044 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
10045 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
10046
10047 if (!newsrv->ssl_ctx.methods.min)
10048 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
10049
10050 if (!newsrv->ssl_ctx.methods.max)
10051 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
10052
10053
Willy Tarreau92faadf2012-10-10 23:04:25 +020010054 return 0;
10055}
10056
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +010010057/* parse the "ssl-reuse" server keyword */
10058static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10059{
10060 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
10061 return 0;
10062}
10063
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +010010064/* parse the "tls-tickets" server keyword */
10065static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10066{
10067 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
10068 return 0;
10069}
10070
Emeric Brunef42d922012-10-11 16:11:36 +020010071/* parse the "verify" server keyword */
10072static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10073{
10074 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +010010075 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +020010076 return ERR_ALERT | ERR_FATAL;
10077 }
10078
10079 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +010010080 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +020010081 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +010010082 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +020010083 else {
Tim Duesterhus93128532019-11-23 23:45:10 +010010084 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
10085 args[*cur_arg], args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +020010086 return ERR_ALERT | ERR_FATAL;
10087 }
10088
Evan Broderbe554312013-06-27 00:05:25 -070010089 return 0;
10090}
10091
10092/* parse the "verifyhost" server keyword */
10093static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
10094{
10095 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +010010096 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
Evan Broderbe554312013-06-27 00:05:25 -070010097 return ERR_ALERT | ERR_FATAL;
10098 }
10099
Frédéric Lécaille273f3212017-03-13 15:52:01 +010010100 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -070010101 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
10102
Emeric Brunef42d922012-10-11 16:11:36 +020010103 return 0;
10104}
10105
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010106/* parse the "ssl-default-bind-options" keyword in global section */
10107static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
10108 struct proxy *defpx, const char *file, int line,
10109 char **err) {
10110 int i = 1;
10111
10112 if (*(args[i]) == 0) {
10113 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
10114 return -1;
10115 }
10116 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010117 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +010010118 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +000010119 else if (!strcmp(args[i], "prefer-client-ciphers"))
10120 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010121 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
10122 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
10123 i++;
10124 else {
10125 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
10126 return -1;
10127 }
10128 }
10129 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010130 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
10131 return -1;
10132 }
10133 i++;
10134 }
10135 return 0;
10136}
10137
10138/* parse the "ssl-default-server-options" keyword in global section */
10139static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
10140 struct proxy *defpx, const char *file, int line,
10141 char **err) {
10142 int i = 1;
10143
10144 if (*(args[i]) == 0) {
10145 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
10146 return -1;
10147 }
10148 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020010149 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +010010150 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020010151 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
10152 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
10153 i++;
10154 else {
10155 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
10156 return -1;
10157 }
10158 }
10159 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +010010160 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
10161 return -1;
10162 }
10163 i++;
10164 }
10165 return 0;
10166}
10167
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010168/* parse the "ca-base" / "crt-base" keywords in global section.
10169 * Returns <0 on alert, >0 on warning, 0 on success.
10170 */
10171static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
10172 struct proxy *defpx, const char *file, int line,
10173 char **err)
10174{
10175 char **target;
10176
Willy Tarreauef934602016-12-22 23:12:01 +010010177 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010178
10179 if (too_many_args(1, args, err, NULL))
10180 return -1;
10181
10182 if (*target) {
10183 memprintf(err, "'%s' already specified.", args[0]);
10184 return -1;
10185 }
10186
10187 if (*(args[1]) == 0) {
10188 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
10189 return -1;
10190 }
10191 *target = strdup(args[1]);
10192 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010193}
10194
Emmanuel Hocdetc3b7e742020-04-22 11:06:19 +020010195/* parse the "ssl-skip-self-issued-ca" keyword in global section. */
10196static int ssl_parse_skip_self_issued_ca(char **args, int section_type, struct proxy *curpx,
10197 struct proxy *defpx, const char *file, int line,
10198 char **err)
10199{
10200 global_ssl.skip_self_issued_ca = 1;
10201 return 0;
10202}
10203
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010204/* "issuers-chain-path" load chain certificate in global */
10205static int ssl_load_global_issuer_from_BIO(BIO *in, char *fp, char **err)
10206{
10207 X509 *ca;
10208 X509_NAME *name = NULL;
10209 ASN1_OCTET_STRING *skid = NULL;
10210 STACK_OF(X509) *chain = NULL;
10211 struct issuer_chain *issuer;
10212 struct eb64_node *node;
10213 char *path;
10214 u64 key;
10215 int ret = 0;
10216
10217 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
10218 if (chain == NULL) {
10219 chain = sk_X509_new_null();
10220 skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL);
10221 name = X509_get_subject_name(ca);
10222 }
10223 if (!sk_X509_push(chain, ca)) {
10224 X509_free(ca);
10225 goto end;
10226 }
10227 }
10228 if (!chain) {
10229 memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp);
10230 goto end;
10231 }
10232 if (!skid) {
10233 memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp);
10234 goto end;
10235 }
10236 if (!name) {
10237 memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp);
10238 goto end;
10239 }
10240 key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0);
William Lallemande0f3fd52020-02-25 14:53:06 +010010241 for (node = eb64_lookup(&cert_issuer_tree, key); node; node = eb64_next(node)) {
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010242 issuer = container_of(node, typeof(*issuer), node);
10243 if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) {
10244 memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path);
10245 goto end;
10246 }
10247 }
10248 issuer = calloc(1, sizeof *issuer);
10249 path = strdup(fp);
10250 if (!issuer || !path) {
10251 free(issuer);
10252 free(path);
10253 goto end;
10254 }
10255 issuer->node.key = key;
10256 issuer->path = path;
10257 issuer->chain = chain;
10258 chain = NULL;
William Lallemande0f3fd52020-02-25 14:53:06 +010010259 eb64_insert(&cert_issuer_tree, &issuer->node);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010260 ret = 1;
10261 end:
10262 if (skid)
10263 ASN1_OCTET_STRING_free(skid);
10264 if (chain)
10265 sk_X509_pop_free(chain, X509_free);
10266 return ret;
10267}
10268
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +010010269static struct issuer_chain* ssl_get0_issuer_chain(X509 *cert)
Emmanuel Hocdet75a7aa12020-02-18 15:19:24 +010010270{
10271 AUTHORITY_KEYID *akid;
10272 struct issuer_chain *issuer = NULL;
10273
10274 akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL);
10275 if (akid) {
10276 struct eb64_node *node;
10277 u64 hk;
10278 hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0);
10279 for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) {
10280 struct issuer_chain *ti = container_of(node, typeof(*issuer), node);
10281 if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) {
10282 issuer = ti;
10283 break;
10284 }
10285 }
10286 AUTHORITY_KEYID_free(akid);
10287 }
10288 return issuer;
10289}
10290
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010291static void ssl_free_global_issuers(void)
10292{
10293 struct eb64_node *node, *back;
10294 struct issuer_chain *issuer;
10295
William Lallemande0f3fd52020-02-25 14:53:06 +010010296 node = eb64_first(&cert_issuer_tree);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010297 while (node) {
10298 issuer = container_of(node, typeof(*issuer), node);
10299 back = eb64_next(node);
10300 eb64_delete(node);
10301 free(issuer->path);
10302 sk_X509_pop_free(issuer->chain, X509_free);
10303 free(issuer);
10304 node = back;
10305 }
10306}
10307
10308static int ssl_load_global_issuers_from_path(char **args, int section_type, struct proxy *curpx,
10309 struct proxy *defpx, const char *file, int line,
10310 char **err)
10311{
10312 char *path;
10313 struct dirent **de_list;
10314 int i, n;
10315 struct stat buf;
10316 char *end;
10317 char fp[MAXPATHLEN+1];
10318
10319 if (too_many_args(1, args, err, NULL))
10320 return -1;
10321
10322 path = args[1];
10323 if (*path == 0 || stat(path, &buf)) {
10324 memprintf(err, "%sglobal statement '%s' expects a directory path as an argument.\n",
10325 err && *err ? *err : "", args[0]);
10326 return -1;
10327 }
10328 if (S_ISDIR(buf.st_mode) == 0) {
10329 memprintf(err, "%sglobal statement '%s': %s is not a directory.\n",
10330 err && *err ? *err : "", args[0], path);
10331 return -1;
10332 }
10333
10334 /* strip trailing slashes, including first one */
10335 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
10336 *end = 0;
10337 /* path already parsed? */
10338 if (global_ssl.issuers_chain_path && strcmp(global_ssl.issuers_chain_path, path) == 0)
10339 return 0;
10340 /* overwrite old issuers_chain_path */
10341 free(global_ssl.issuers_chain_path);
10342 global_ssl.issuers_chain_path = strdup(path);
10343 ssl_free_global_issuers();
10344
10345 n = scandir(path, &de_list, 0, alphasort);
10346 if (n < 0) {
10347 memprintf(err, "%sglobal statement '%s': unable to scan directory '%s' : %s.\n",
10348 err && *err ? *err : "", args[0], path, strerror(errno));
10349 return -1;
10350 }
10351 for (i = 0; i < n; i++) {
10352 struct dirent *de = de_list[i];
10353 BIO *in = NULL;
10354 char *warn = NULL;
10355
10356 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
10357 free(de);
10358 if (stat(fp, &buf) != 0) {
10359 ha_warning("unable to stat certificate from file '%s' : %s.\n", fp, strerror(errno));
10360 goto next;
10361 }
10362 if (!S_ISREG(buf.st_mode))
10363 goto next;
10364
10365 in = BIO_new(BIO_s_file());
10366 if (in == NULL)
10367 goto next;
10368 if (BIO_read_filename(in, fp) <= 0)
10369 goto next;
10370 ssl_load_global_issuer_from_BIO(in, fp, &warn);
10371 if (warn) {
Tim Duesterhuse8aa5f22020-02-19 11:41:13 +010010372 ha_warning("%s", warn);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010010373 free(warn);
10374 warn = NULL;
10375 }
10376 next:
10377 if (in)
10378 BIO_free(in);
10379 }
10380 free(de_list);
10381
10382 return 0;
10383}
10384
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010385/* parse the "ssl-mode-async" keyword in global section.
10386 * Returns <0 on alert, >0 on warning, 0 on success.
10387 */
10388static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
10389 struct proxy *defpx, const char *file, int line,
10390 char **err)
10391{
Willy Tarreau5db847a2019-05-09 14:13:35 +020010392#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010393 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +010010394 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010395 return 0;
10396#else
10397 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
10398 return -1;
10399#endif
10400}
10401
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010402#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010403static int ssl_check_async_engine_count(void) {
10404 int err_code = 0;
10405
Emeric Brun3854e012017-05-17 20:42:48 +020010406 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +010010407 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000010408 err_code = ERR_ABORT;
10409 }
10410 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010010411}
10412
Grant Zhang872f9c22017-01-21 01:10:18 +000010413/* parse the "ssl-engine" keyword in global section.
10414 * Returns <0 on alert, >0 on warning, 0 on success.
10415 */
10416static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
10417 struct proxy *defpx, const char *file, int line,
10418 char **err)
10419{
10420 char *algo;
10421 int ret = -1;
10422
10423 if (*(args[1]) == 0) {
10424 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
10425 return ret;
10426 }
10427
10428 if (*(args[2]) == 0) {
10429 /* if no list of algorithms is given, it defaults to ALL */
10430 algo = strdup("ALL");
10431 goto add_engine;
10432 }
10433
10434 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
10435 if (strcmp(args[2], "algo") != 0) {
10436 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
10437 return ret;
10438 }
10439
10440 if (*(args[3]) == 0) {
10441 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
10442 return ret;
10443 }
10444 algo = strdup(args[3]);
10445
10446add_engine:
10447 if (ssl_init_single_engine(args[1], algo)==0) {
10448 openssl_engines_initialized++;
10449 ret = 0;
10450 }
10451 free(algo);
10452 return ret;
10453}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020010454#endif
Grant Zhang872f9c22017-01-21 01:10:18 +000010455
Willy Tarreauf22e9682016-12-21 23:23:19 +010010456/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
10457 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
10458 */
10459static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
10460 struct proxy *defpx, const char *file, int line,
10461 char **err)
10462{
10463 char **target;
10464
Willy Tarreauef934602016-12-22 23:12:01 +010010465 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +010010466
10467 if (too_many_args(1, args, err, NULL))
10468 return -1;
10469
10470 if (*(args[1]) == 0) {
10471 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
10472 return -1;
10473 }
10474
10475 free(*target);
10476 *target = strdup(args[1]);
10477 return 0;
10478}
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010479
Emmanuel Hocdet839af572019-05-14 16:27:35 +020010480#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020010481/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
10482 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
10483 */
10484static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
10485 struct proxy *defpx, const char *file, int line,
10486 char **err)
10487{
10488 char **target;
10489
10490 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
10491
10492 if (too_many_args(1, args, err, NULL))
10493 return -1;
10494
10495 if (*(args[1]) == 0) {
10496 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
10497 return -1;
10498 }
10499
10500 free(*target);
10501 *target = strdup(args[1]);
10502 return 0;
10503}
10504#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +010010505
Jerome Magninb203ff62020-04-03 15:28:22 +020010506#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
10507/*
10508 * parse the "ssl-default-bind-curves" keyword in a global section.
10509 * Returns <0 on alert, >0 on warning, 0 on success.
10510 */
10511static int ssl_parse_global_curves(char **args, int section_type, struct proxy *curpx,
10512 struct proxy *defpx, const char *file, int line,
10513 char **err)
10514{
10515 char **target;
10516 target = &global_ssl.listen_default_curves;
10517
10518 if (too_many_args(1, args, err, NULL))
10519 return -1;
10520
10521 if (*(args[1]) == 0) {
10522 memprintf(err, "global statement '%s' expects a curves suite as an arguments.", args[0]);
10523 return -1;
10524 }
10525
10526 free(*target);
10527 *target = strdup(args[1]);
10528 return 0;
10529}
10530#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +010010531/* parse various global tune.ssl settings consisting in positive integers.
10532 * Returns <0 on alert, >0 on warning, 0 on success.
10533 */
10534static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
10535 struct proxy *defpx, const char *file, int line,
10536 char **err)
10537{
10538 int *target;
10539
10540 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
10541 target = &global.tune.sslcachesize;
10542 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +010010543 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010544 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +010010545 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +010010546 else if (strcmp(args[0], "maxsslconn") == 0)
10547 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010548 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
10549 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010550 else {
10551 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
10552 return -1;
10553 }
10554
10555 if (too_many_args(1, args, err, NULL))
10556 return -1;
10557
10558 if (*(args[1]) == 0) {
10559 memprintf(err, "'%s' expects an integer argument.", args[0]);
10560 return -1;
10561 }
10562
10563 *target = atoi(args[1]);
10564 if (*target < 0) {
10565 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
10566 return -1;
10567 }
10568 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010569}
10570
10571static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
10572 struct proxy *defpx, const char *file, int line,
10573 char **err)
10574{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010575 int ret;
10576
10577 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
10578 if (ret != 0)
10579 return ret;
10580
Willy Tarreaubafbe012017-11-24 17:34:44 +010010581 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010582 memprintf(err, "'%s' is already configured.", args[0]);
10583 return -1;
10584 }
10585
Willy Tarreaubafbe012017-11-24 17:34:44 +010010586 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
10587 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010010588 memprintf(err, "Out of memory error.");
10589 return -1;
10590 }
10591 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010592}
10593
10594/* parse "ssl.force-private-cache".
10595 * Returns <0 on alert, >0 on warning, 0 on success.
10596 */
10597static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
10598 struct proxy *defpx, const char *file, int line,
10599 char **err)
10600{
10601 if (too_many_args(0, args, err, NULL))
10602 return -1;
10603
Willy Tarreauef934602016-12-22 23:12:01 +010010604 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +010010605 return 0;
10606}
10607
10608/* parse "ssl.lifetime".
10609 * Returns <0 on alert, >0 on warning, 0 on success.
10610 */
10611static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
10612 struct proxy *defpx, const char *file, int line,
10613 char **err)
10614{
10615 const char *res;
10616
10617 if (too_many_args(1, args, err, NULL))
10618 return -1;
10619
10620 if (*(args[1]) == 0) {
10621 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
10622 return -1;
10623 }
10624
Willy Tarreauef934602016-12-22 23:12:01 +010010625 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +020010626 if (res == PARSE_TIME_OVER) {
10627 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
10628 args[1], args[0]);
10629 return -1;
10630 }
10631 else if (res == PARSE_TIME_UNDER) {
10632 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
10633 args[1], args[0]);
10634 return -1;
10635 }
10636 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +010010637 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
10638 return -1;
10639 }
10640 return 0;
10641}
10642
10643#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +010010644/* parse "ssl-dh-param-file".
10645 * Returns <0 on alert, >0 on warning, 0 on success.
10646 */
10647static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
10648 struct proxy *defpx, const char *file, int line,
10649 char **err)
10650{
10651 if (too_many_args(1, args, err, NULL))
10652 return -1;
10653
10654 if (*(args[1]) == 0) {
10655 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
10656 return -1;
10657 }
10658
10659 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
10660 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
10661 return -1;
10662 }
10663 return 0;
10664}
10665
Willy Tarreau9ceda382016-12-21 23:13:03 +010010666/* parse "ssl.default-dh-param".
10667 * Returns <0 on alert, >0 on warning, 0 on success.
10668 */
10669static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
10670 struct proxy *defpx, const char *file, int line,
10671 char **err)
10672{
10673 if (too_many_args(1, args, err, NULL))
10674 return -1;
10675
10676 if (*(args[1]) == 0) {
10677 memprintf(err, "'%s' expects an integer argument.", args[0]);
10678 return -1;
10679 }
10680
Willy Tarreauef934602016-12-22 23:12:01 +010010681 global_ssl.default_dh_param = atoi(args[1]);
10682 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +010010683 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
10684 return -1;
10685 }
10686 return 0;
10687}
10688#endif
10689
William Lallemand3af48e72020-02-03 17:15:52 +010010690
10691/*
10692 * parse "ssl-load-extra-files".
10693 * multiple arguments are allowed: "bundle", "sctl", "ocsp", "issuer", "all", "none"
10694 */
10695static int ssl_parse_global_extra_files(char **args, int section_type, struct proxy *curpx,
10696 struct proxy *defpx, const char *file, int line,
10697 char **err)
10698{
10699 int i;
10700 int gf = SSL_GF_NONE;
10701
10702 if (*(args[1]) == 0)
10703 goto err_arg;
10704
10705 for (i = 1; *args[i]; i++) {
10706
10707 if (!strcmp("bundle", args[i])) {
10708 gf |= SSL_GF_BUNDLE;
10709
10710 } else if (!strcmp("sctl", args[i])) {
10711 gf |= SSL_GF_SCTL;
10712
10713 } else if (!strcmp("ocsp", args[i])){
10714 gf |= SSL_GF_OCSP;
10715
10716 } else if (!strcmp("issuer", args[i])){
10717 gf |= SSL_GF_OCSP_ISSUER;
10718
William Lallemand4c5adbf2020-02-24 14:23:22 +010010719 } else if (!strcmp("key", args[i])) {
10720 gf |= SSL_GF_KEY;
10721
William Lallemand3af48e72020-02-03 17:15:52 +010010722 } else if (!strcmp("none", args[i])) {
10723 if (gf != SSL_GF_NONE)
10724 goto err_alone;
10725 gf = SSL_GF_NONE;
10726 i++;
10727 break;
10728
10729 } else if (!strcmp("all", args[i])) {
10730 if (gf != SSL_GF_NONE)
10731 goto err_alone;
10732 gf = SSL_GF_ALL;
10733 i++;
10734 break;
10735 } else {
10736 goto err_arg;
10737 }
10738 }
10739 /* break from loop but there are still arguments */
10740 if (*args[i])
10741 goto err_alone;
10742
10743 global_ssl.extra_files = gf;
10744
10745 return 0;
10746
10747err_alone:
10748 memprintf(err, "'%s' 'none' and 'all' can be only used alone", args[0]);
10749 return -1;
10750
10751err_arg:
10752 memprintf(err, "'%s' expects one or multiple arguments (none, all, bundle, sctl, ocsp, issuer).", args[0]);
10753 return -1;
10754}
10755
Willy Tarreau9ceda382016-12-21 23:13:03 +010010756
William Lallemand32af2032016-10-29 18:09:35 +020010757/* This function is used with TLS ticket keys management. It permits to browse
10758 * each reference. The variable <getnext> must contain the current node,
10759 * <end> point to the root node.
10760 */
10761#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10762static inline
10763struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
10764{
10765 struct tls_keys_ref *ref = getnext;
10766
10767 while (1) {
10768
10769 /* Get next list entry. */
10770 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
10771
10772 /* If the entry is the last of the list, return NULL. */
10773 if (&ref->list == end)
10774 return NULL;
10775
10776 return ref;
10777 }
10778}
10779
10780static inline
10781struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
10782{
10783 int id;
10784 char *error;
10785
10786 /* If the reference starts by a '#', this is numeric id. */
10787 if (reference[0] == '#') {
10788 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
10789 id = strtol(reference + 1, &error, 10);
10790 if (*error != '\0')
10791 return NULL;
10792
10793 /* Perform the unique id lookup. */
10794 return tlskeys_ref_lookupid(id);
10795 }
10796
10797 /* Perform the string lookup. */
10798 return tlskeys_ref_lookup(reference);
10799}
10800#endif
10801
10802
10803#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
10804
10805static int cli_io_handler_tlskeys_files(struct appctx *appctx);
10806
10807static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
10808 return cli_io_handler_tlskeys_files(appctx);
10809}
10810
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010811/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
10812 * (next index to be dumped), and cli.p0 (next key reference).
10813 */
William Lallemand32af2032016-10-29 18:09:35 +020010814static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
10815
10816 struct stream_interface *si = appctx->owner;
10817
10818 switch (appctx->st2) {
10819 case STAT_ST_INIT:
10820 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -080010821 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +020010822 * later and restart at the state "STAT_ST_INIT".
10823 */
10824 chunk_reset(&trash);
10825
10826 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
10827 chunk_appendf(&trash, "# id secret\n");
10828 else
10829 chunk_appendf(&trash, "# id (file)\n");
10830
Willy Tarreau06d80a92017-10-19 14:32:15 +020010831 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +010010832 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010833 return 0;
10834 }
10835
William Lallemand32af2032016-10-29 18:09:35 +020010836 /* Now, we start the browsing of the references lists.
10837 * Note that the following call to LIST_ELEM return bad pointer. The only
10838 * available field of this pointer is <list>. It is used with the function
10839 * tlskeys_list_get_next() for retruning the first available entry
10840 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010841 if (appctx->ctx.cli.p0 == NULL) {
10842 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
10843 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010844 }
10845
10846 appctx->st2 = STAT_ST_LIST;
10847 /* fall through */
10848
10849 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010850 while (appctx->ctx.cli.p0) {
10851 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +020010852
10853 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010854 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +020010855 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010856
10857 if (appctx->ctx.cli.i1 == 0)
10858 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
10859
William Lallemand32af2032016-10-29 18:09:35 +020010860 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +010010861 int head;
10862
10863 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
10864 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010865 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +020010866 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +020010867
10868 chunk_reset(t2);
10869 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +010010870 if (ref->key_size_bits == 128) {
10871 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10872 sizeof(struct tls_sess_key_128),
10873 t2->area, t2->size);
10874 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10875 t2->area);
10876 }
10877 else if (ref->key_size_bits == 256) {
10878 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
10879 sizeof(struct tls_sess_key_256),
10880 t2->area, t2->size);
10881 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
10882 t2->area);
10883 }
10884 else {
10885 /* This case should never happen */
10886 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
10887 }
William Lallemand32af2032016-10-29 18:09:35 +020010888
Willy Tarreau06d80a92017-10-19 14:32:15 +020010889 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010890 /* let's try again later from this stream. We add ourselves into
10891 * this stream's users so that it can remove us upon termination.
10892 */
Christopher Faulet16f45c82018-02-16 11:23:49 +010010893 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +010010894 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010895 return 0;
10896 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010897 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +020010898 }
Christopher Faulet16f45c82018-02-16 11:23:49 +010010899 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010900 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +020010901 }
Willy Tarreau06d80a92017-10-19 14:32:15 +020010902 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +020010903 /* let's try again later from this stream. We add ourselves into
10904 * this stream's users so that it can remove us upon termination.
10905 */
Willy Tarreaudb398432018-11-15 11:08:52 +010010906 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +020010907 return 0;
10908 }
10909
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010910 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +020010911 break;
10912
10913 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010914 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +020010915 }
10916
10917 appctx->st2 = STAT_ST_FIN;
10918 /* fall through */
10919
10920 default:
10921 appctx->st2 = STAT_ST_FIN;
10922 return 1;
10923 }
10924 return 0;
10925}
10926
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010927/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010928static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010929{
William Lallemand32af2032016-10-29 18:09:35 +020010930 /* no parameter, shows only file list */
10931 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010932 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010933 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010934 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010935 }
10936
10937 if (args[2][0] == '*') {
10938 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010939 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +020010940 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010941 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010942 if (!appctx->ctx.cli.p0)
10943 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010944 }
William Lallemand32af2032016-10-29 18:09:35 +020010945 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +010010946 return 0;
William Lallemand32af2032016-10-29 18:09:35 +020010947}
10948
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020010949static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020010950{
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010951 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +020010952 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010953
William Lallemand32af2032016-10-29 18:09:35 +020010954 /* Expect two parameters: the filename and the new new TLS key in encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020010955 if (!*args[3] || !*args[4])
10956 return cli_err(appctx, "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010957
Willy Tarreauf5f26e82016-12-16 18:47:27 +010010958 ref = tlskeys_ref_lookup_ref(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +020010959 if (!ref)
10960 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +020010961
Willy Tarreau1c913e42018-08-22 05:26:57 +020010962 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020010963 if (ret < 0)
10964 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Emeric Brun9e754772019-01-10 17:51:55 +010010965
Willy Tarreau1c913e42018-08-22 05:26:57 +020010966 trash.data = ret;
Willy Tarreau9d008692019-08-09 11:21:01 +020010967 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
10968 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
William Lallemand32af2032016-10-29 18:09:35 +020010969
Willy Tarreau9d008692019-08-09 11:21:01 +020010970 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020010971}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010010972#endif
William Lallemand32af2032016-10-29 18:09:35 +020010973
William Lallemanda6ffd5b2020-03-09 13:35:19 +010010974/*
10975 * Take an ssl_bind_conf structure and append the configuration line used to
10976 * create it in the buffer
10977 */
10978static void dump_crtlist_sslconf(struct buffer *buf, const struct ssl_bind_conf *conf)
10979{
10980 int space = 0;
10981
10982 if (conf == NULL)
10983 return;
10984
10985 chunk_appendf(buf, " [");
10986#ifdef OPENSSL_NPN_NEGOTIATED
10987 if (conf->npn_str) {
10988 int len = conf->npn_len;
10989 char *ptr = conf->npn_str;
10990 int comma = 0;
10991
10992 if (space) chunk_appendf(buf, " ");
10993 chunk_appendf(buf, "npn ");
10994 while (len) {
10995 unsigned short size;
10996
10997 size = *ptr;
10998 ptr++;
10999 if (comma)
11000 chunk_memcat(buf, ",", 1);
11001 chunk_memcat(buf, ptr, size);
11002 ptr += size;
11003 len -= size + 1;
11004 comma = 1;
11005 }
11006 chunk_memcat(buf, "", 1); /* finish with a \0 */
11007 space++;
11008 }
11009#endif
11010#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
11011 if (conf->alpn_str) {
11012 int len = conf->alpn_len;
11013 char *ptr = conf->alpn_str;
11014 int comma = 0;
11015
11016 if (space) chunk_appendf(buf, " ");
11017 chunk_appendf(buf, "alpn ");
11018 while (len) {
11019 unsigned short size;
11020
11021 size = *ptr;
11022 ptr++;
11023 if (comma)
11024 chunk_memcat(buf, ",", 1);
11025 chunk_memcat(buf, ptr, size);
11026 ptr += size;
11027 len -= size + 1;
11028 comma = 1;
11029 }
11030 chunk_memcat(buf, "", 1); /* finish with a \0 */
11031 space++;
11032 }
11033#endif
11034 /* verify */
11035 {
11036 if (conf->verify == SSL_SOCK_VERIFY_NONE) {
11037 if (space) chunk_appendf(buf, " ");
11038 chunk_appendf(buf, "verify none");
11039 space++;
11040 } else if (conf->verify == SSL_SOCK_VERIFY_OPTIONAL) {
11041 if (space) chunk_appendf(buf, " ");
11042 chunk_appendf(buf, "verify optional");
11043 space++;
11044 } else if (conf->verify == SSL_SOCK_VERIFY_REQUIRED) {
11045 if (space) chunk_appendf(buf, " ");
11046 chunk_appendf(buf, "verify required");
11047 space++;
11048 }
11049 }
11050
11051 if (conf->no_ca_names) {
11052 if (space) chunk_appendf(buf, " ");
11053 chunk_appendf(buf, "no-ca-names");
11054 space++;
11055 }
11056
11057 if (conf->early_data) {
11058 if (space) chunk_appendf(buf, " ");
11059 chunk_appendf(buf, "allow-0rtt");
11060 space++;
11061 }
11062 if (conf->ca_file) {
11063 if (space) chunk_appendf(buf, " ");
11064 chunk_appendf(buf, "ca-file %s", conf->ca_file);
11065 space++;
11066 }
11067 if (conf->crl_file) {
11068 if (space) chunk_appendf(buf, " ");
11069 chunk_appendf(buf, "crl-file %s", conf->crl_file);
11070 space++;
11071 }
11072 if (conf->ciphers) {
11073 if (space) chunk_appendf(buf, " ");
11074 chunk_appendf(buf, "ciphers %s", conf->ciphers);
11075 space++;
11076 }
11077#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
11078 if (conf->ciphersuites) {
11079 if (space) chunk_appendf(buf, " ");
11080 chunk_appendf(buf, "ciphersuites %s", conf->ciphersuites);
11081 space++;
11082 }
11083#endif
11084 if (conf->curves) {
11085 if (space) chunk_appendf(buf, " ");
11086 chunk_appendf(buf, "curves %s", conf->curves);
11087 space++;
11088 }
11089 if (conf->ecdhe) {
11090 if (space) chunk_appendf(buf, " ");
11091 chunk_appendf(buf, "ecdhe %s", conf->ecdhe);
11092 space++;
11093 }
11094
11095 /* the crt-lists only support ssl-min-ver and ssl-max-ver */
11096 /* XXX: this part need to be revamp so we don't dump the default settings */
11097 if (conf->ssl_methods.min) {
11098 if (space) chunk_appendf(buf, " ");
11099 chunk_appendf(buf, "ssl-min-ver %s", methodVersions[conf->ssl_methods.min].name);
11100 space++;
11101 }
11102
11103 if (conf->ssl_methods.max) {
11104 if (space) chunk_appendf(buf, " ");
11105 chunk_appendf(buf, "ssl-max-ver %s", methodVersions[conf->ssl_methods.max].name);
11106 space++;
11107 }
11108
William Lallemand58a52222020-04-02 18:11:47 +020011109 chunk_appendf(buf, "]");
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011110
11111 return;
11112}
11113
11114/* dump a list of filters */
11115static void dump_crtlist_filters(struct buffer *buf, struct crtlist_entry *entry)
11116{
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011117 int i;
11118
11119 if (!entry->fcount)
11120 return;
11121
11122 for (i = 0; i < entry->fcount; i++) {
William Lallemand58a52222020-04-02 18:11:47 +020011123 chunk_appendf(buf, " %s", entry->filters[i]);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011124 }
11125 return;
11126}
11127
11128/* CLI IO handler for '(show|dump) ssl crt-list' */
11129static int cli_io_handler_dump_crtlist(struct appctx *appctx)
11130{
11131 struct buffer *trash = alloc_trash_chunk();
11132 struct stream_interface *si = appctx->owner;
11133 struct ebmb_node *lnode;
11134
11135 if (trash == NULL)
11136 return 1;
11137
11138 /* dump the list of crt-lists */
11139 lnode = appctx->ctx.cli.p1;
11140 if (lnode == NULL)
11141 lnode = ebmb_first(&crtlists_tree);
11142 while (lnode) {
11143 chunk_appendf(trash, "%s\n", lnode->key);
11144 if (ci_putchk(si_ic(si), trash) == -1) {
11145 si_rx_room_blk(si);
11146 goto yield;
11147 }
11148 lnode = ebmb_next(lnode);
11149 }
William Lallemand2ea1b492020-03-17 15:13:11 +010011150 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011151 return 1;
11152yield:
11153 appctx->ctx.cli.p1 = lnode;
William Lallemand2ea1b492020-03-17 15:13:11 +010011154 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011155 return 0;
11156}
11157
11158/* CLI IO handler for '(show|dump) ssl crt-list <filename>' */
11159static int cli_io_handler_dump_crtlist_entries(struct appctx *appctx)
11160{
11161 struct buffer *trash = alloc_trash_chunk();
11162 struct crtlist *crtlist;
11163 struct stream_interface *si = appctx->owner;
11164 struct crtlist_entry *entry;
11165
11166 if (trash == NULL)
11167 return 1;
11168
11169 crtlist = ebmb_entry(appctx->ctx.cli.p0, struct crtlist, node);
11170
11171 entry = appctx->ctx.cli.p1;
11172 if (entry == NULL) {
11173 entry = LIST_ELEM((crtlist->ord_entries).n, typeof(entry), by_crtlist);
11174 chunk_appendf(trash, "# %s\n", crtlist->node.key);
11175 if (ci_putchk(si_ic(si), trash) == -1) {
11176 si_rx_room_blk(si);
11177 goto yield;
11178 }
11179 }
11180
11181 list_for_each_entry_from(entry, &crtlist->ord_entries, by_crtlist) {
11182 struct ckch_store *store;
11183 const char *filename;
11184
11185 store = entry->node.key;
11186 filename = store->path;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011187 chunk_appendf(trash, "%s", filename);
William Lallemandc69f02d2020-04-06 19:07:03 +020011188 if (appctx->ctx.cli.i0 == 's') /* show */
11189 chunk_appendf(trash, ":%d", entry->linenum);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011190 dump_crtlist_sslconf(trash, entry->ssl_conf);
11191 dump_crtlist_filters(trash, entry);
11192 chunk_appendf(trash, "\n");
11193
11194 if (ci_putchk(si_ic(si), trash) == -1) {
11195 si_rx_room_blk(si);
11196 goto yield;
11197 }
11198 }
William Lallemand2ea1b492020-03-17 15:13:11 +010011199 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011200 return 1;
11201yield:
11202 appctx->ctx.cli.p1 = entry;
William Lallemand2ea1b492020-03-17 15:13:11 +010011203 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011204 return 0;
11205}
11206
11207/* CLI argument parser for '(show|dump) ssl crt-list' */
11208static int cli_parse_dump_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11209{
11210 struct ebmb_node *lnode;
William Lallemandc69f02d2020-04-06 19:07:03 +020011211 char *filename = NULL;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011212 int mode;
11213
11214 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11215 return 1;
11216
11217 appctx->ctx.cli.p0 = NULL;
11218 appctx->ctx.cli.p1 = NULL;
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011219
William Lallemandc69f02d2020-04-06 19:07:03 +020011220 if (*args[3] && !strcmp(args[3], "-n")) {
11221 mode = 's';
11222 filename = args[4];
11223 } else {
11224 mode = 'd';
11225 filename = args[3];
11226 }
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011227
William Lallemandc69f02d2020-04-06 19:07:03 +020011228 if (mode == 's' && !*args[4])
11229 return cli_err(appctx, "'show ssl crt-list -n' expects a filename or a directory\n");
11230
11231 if (filename && *filename) {
11232 lnode = ebst_lookup(&crtlists_tree, filename);
William Lallemanda6ffd5b2020-03-09 13:35:19 +010011233 if (lnode == NULL)
11234 return cli_err(appctx, "didn't find the specified filename\n");
11235
11236 appctx->ctx.cli.p0 = lnode;
11237 appctx->io_handler = cli_io_handler_dump_crtlist_entries;
11238 }
11239 appctx->ctx.cli.i0 = mode;
11240
11241 return 0;
11242}
William Lallemand44b35322019-10-17 16:28:40 +020011243
William Lallemande67c80b2020-03-25 14:42:37 +010011244/* release function of the "add ssl crt-list' command, free things and unlock
11245 the spinlock */
11246static void cli_release_add_crtlist(struct appctx *appctx)
11247{
11248 struct crtlist_entry *entry = appctx->ctx.cli.p1;
11249
11250 if (appctx->st2 != SETCERT_ST_FIN) {
11251 struct ckch_inst *inst, *inst_s;
11252 /* upon error free the ckch_inst and everything inside */
11253 ebpt_delete(&entry->node);
11254 LIST_DEL(&entry->by_crtlist);
11255 LIST_DEL(&entry->by_ckch_store);
11256
11257 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_ckchs) {
William Lallemandd9d5d1b2020-04-09 16:31:05 +020011258 ckch_inst_free(inst);
William Lallemande67c80b2020-03-25 14:42:37 +010011259 }
11260 crtlist_free_filters(entry->filters);
11261 ssl_sock_free_ssl_conf(entry->ssl_conf);
11262 free(entry->ssl_conf);
11263 free(entry);
11264 }
11265
11266 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11267}
11268
11269
11270/* IO Handler for the "add ssl crt-list" command It adds a new entry in the
11271 * crt-list and generates the ckch_insts for each bind_conf that uses this crt-list
11272 *
11273 * The logic is the same as the "commit ssl cert" command but without the
11274 * freeing of the old structures, because there are none.
11275 */
11276static int cli_io_handler_add_crtlist(struct appctx *appctx)
11277{
11278 struct bind_conf_list *bind_conf_node;
11279 struct stream_interface *si = appctx->owner;
11280 struct crtlist *crtlist = appctx->ctx.cli.p0;
11281 struct crtlist_entry *entry = appctx->ctx.cli.p1;
11282 struct ckch_store *store = entry->node.key;
11283 struct buffer *trash = alloc_trash_chunk();
11284 struct ckch_inst *new_inst;
11285 char *err = NULL;
11286 int i = 0;
11287 int errcode = 0;
11288
11289 if (trash == NULL)
11290 goto error;
11291
11292 /* for each bind_conf which use the crt-list, a new ckch_inst must be
11293 * created.
11294 */
11295 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
11296 goto error;
11297
11298 while (1) {
11299 switch (appctx->st2) {
11300 case SETCERT_ST_INIT:
11301 /* This state just print the update message */
11302 chunk_printf(trash, "Inserting certificate '%s' in crt-list '%s'", store->path, crtlist->node.key);
11303 if (ci_putchk(si_ic(si), trash) == -1) {
11304 si_rx_room_blk(si);
11305 goto yield;
11306 }
11307 appctx->st2 = SETCERT_ST_GEN;
11308 /* fallthrough */
11309 case SETCERT_ST_GEN:
11310 bind_conf_node = appctx->ctx.cli.p2; /* get the previous ptr from the yield */
11311 if (bind_conf_node == NULL)
11312 bind_conf_node = crtlist->bind_conf;
11313 for (; bind_conf_node; bind_conf_node = bind_conf_node->next) {
11314 struct bind_conf *bind_conf = bind_conf_node->bind_conf;
11315 struct sni_ctx *sni;
11316
11317 /* yield every 10 generations */
11318 if (i > 10) {
11319 appctx->ctx.cli.p2 = bind_conf_node;
11320 goto yield;
11321 }
11322
11323 /* we don't support multi-cert bundles, only simple ones */
11324 errcode |= ckch_inst_new_load_store(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &new_inst, &err);
11325 if (errcode & ERR_CODE)
11326 goto error;
11327
11328 /* we need to initialize the SSL_CTX generated */
11329 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
11330 list_for_each_entry(sni, &new_inst->sni_ctx, by_ckch_inst) {
11331 if (!sni->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
11332 errcode |= ssl_sock_prepare_ctx(bind_conf, new_inst->ssl_conf, sni->ctx, &err);
11333 if (errcode & ERR_CODE)
11334 goto error;
11335 }
11336 }
11337 /* display one dot for each new instance */
11338 chunk_appendf(trash, ".");
11339 i++;
11340 LIST_ADDQ(&store->ckch_inst, &new_inst->by_ckchs);
11341 }
11342 appctx->st2 = SETCERT_ST_INSERT;
11343 /* fallthrough */
11344 case SETCERT_ST_INSERT:
11345 /* insert SNIs in bind_conf */
11346 list_for_each_entry(new_inst, &store->ckch_inst, by_ckchs) {
11347 HA_RWLOCK_WRLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock);
11348 ssl_sock_load_cert_sni(new_inst, new_inst->bind_conf);
11349 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock);
11350 }
William Lallemandc69f02d2020-04-06 19:07:03 +020011351 entry->linenum = ++crtlist->linecount;
William Lallemande67c80b2020-03-25 14:42:37 +010011352 appctx->st2 = SETCERT_ST_FIN;
11353 goto end;
11354 }
11355 }
11356
11357end:
11358 chunk_appendf(trash, "\n");
11359 if (errcode & ERR_WARN)
11360 chunk_appendf(trash, "%s", err);
11361 chunk_appendf(trash, "Success!\n");
11362 if (ci_putchk(si_ic(si), trash) == -1)
11363 si_rx_room_blk(si);
11364 free_trash_chunk(trash);
11365 /* success: call the release function and don't come back */
11366 return 1;
11367yield:
11368 /* store the state */
11369 if (ci_putchk(si_ic(si), trash) == -1)
11370 si_rx_room_blk(si);
11371 free_trash_chunk(trash);
11372 si_rx_endp_more(si); /* let's come back later */
11373 return 0; /* should come back */
11374
11375error:
11376 /* spin unlock and free are done in the release function */
11377 if (trash) {
11378 chunk_appendf(trash, "\n%sFailed!\n", err);
11379 if (ci_putchk(si_ic(si), trash) == -1)
11380 si_rx_room_blk(si);
11381 free_trash_chunk(trash);
11382 }
11383 /* error: call the release function and don't come back */
11384 return 1;
11385}
11386
11387
11388/*
11389 * Parse a "add ssl crt-list <crt-list> <certfile>" line.
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011390 * Filters and option must be passed through payload:
William Lallemande67c80b2020-03-25 14:42:37 +010011391 */
11392static int cli_parse_add_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11393{
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011394 int cfgerr = 0;
William Lallemande67c80b2020-03-25 14:42:37 +010011395 struct ckch_store *store;
11396 char *err = NULL;
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011397 char path[MAXPATHLEN+1];
11398 char *crtlist_path;
11399 char *cert_path = NULL;
William Lallemande67c80b2020-03-25 14:42:37 +010011400 struct ebmb_node *eb;
11401 struct ebpt_node *inserted;
11402 struct crtlist *crtlist;
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011403 struct crtlist_entry *entry = NULL;
William Lallemande67c80b2020-03-25 14:42:37 +010011404
11405 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11406 return 1;
11407
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011408 if (!*args[3] || (!payload && !*args[4]))
William Lallemande67c80b2020-03-25 14:42:37 +010011409 return cli_err(appctx, "'add ssl crtlist' expects a filename and a certificate name\n");
11410
11411 crtlist_path = args[3];
William Lallemande67c80b2020-03-25 14:42:37 +010011412
11413 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11414 return cli_err(appctx, "Operations on certificates are currently locked!\n");
11415
11416 eb = ebst_lookup(&crtlists_tree, crtlist_path);
11417 if (!eb) {
11418 memprintf(&err, "crt-list '%s' does not exist!", crtlist_path);
11419 goto error;
11420 }
11421 crtlist = ebmb_entry(eb, struct crtlist, node);
11422
William Lallemande718dfb2020-04-10 11:09:25 +020011423 entry = crtlist_entry_new();
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011424 if (entry == NULL) {
11425 memprintf(&err, "Not enough memory!");
11426 goto error;
11427 }
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011428
11429 if (payload) {
11430 char *lf;
11431
11432 lf = strrchr(payload, '\n');
11433 if (lf) {
11434 memprintf(&err, "only one line of payload is supported!");
11435 goto error;
11436 }
11437 /* cert_path is filled here */
11438 cfgerr |= crtlist_parse_line(payload, &cert_path, entry, "CLI", 1, &err);
11439 if (cfgerr & ERR_CODE)
11440 goto error;
11441 } else {
11442 cert_path = args[4];
11443 }
11444
11445 if (!cert_path) {
11446 memprintf(&err, "'add ssl crtlist' should contain the certificate name in the payload");
11447 cfgerr |= ERR_ALERT | ERR_FATAL;
11448 goto error;
11449 }
11450
William Lallemand916d0b52020-04-21 18:29:12 +020011451 if (eb_gettag(crtlist->entries.b[EB_RGHT])) {
11452 char *slash;
11453
11454 slash = strrchr(cert_path, '/');
11455 if (!slash) {
11456 memprintf(&err, "'%s' is a directory, certificate path '%s' must contain the directory path", (char *)crtlist->node.key, cert_path);
11457 goto error;
11458 }
11459 /* temporary replace / by 0 to do an strcmp */
11460 *slash = '\0';
11461 if (strcmp(cert_path, (char*)crtlist->node.key) != 0) {
11462 *slash = '/';
11463 memprintf(&err, "'%s' is a directory, certificate path '%s' must contain the directory path", (char *)crtlist->node.key, cert_path);
11464 goto error;
11465 }
11466 *slash = '/';
11467 }
11468
William Lallemandc7c7a6b2020-04-01 17:32:46 +020011469 if (*cert_path != '/' && global_ssl.crt_base) {
11470 if ((strlen(global_ssl.crt_base) + 1 + strlen(cert_path)) > MAXPATHLEN) {
11471 memprintf(&err, "'%s' : path too long", cert_path);
11472 cfgerr |= ERR_ALERT | ERR_FATAL;
11473 goto error;
11474 }
11475 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, cert_path);
11476 cert_path = path;
11477 }
11478
William Lallemande67c80b2020-03-25 14:42:37 +010011479 store = ckchs_lookup(cert_path);
11480 if (store == NULL) {
11481 memprintf(&err, "certificate '%s' does not exist!", cert_path);
11482 goto error;
11483 }
William Lallemand36ccc392020-04-08 10:57:24 +020011484 if (store->multi) {
11485 memprintf(&err, "certificate '%s' is a bundle. You can disable the bundle merging with the directive 'ssl-load-extra-files' in the global section.", cert_path);
11486 goto error;
11487 }
William Lallemande67c80b2020-03-25 14:42:37 +010011488 if (store->ckch == NULL || store->ckch->cert == NULL) {
11489 memprintf(&err, "certificate '%s' is empty!", cert_path);
11490 goto error;
11491 }
11492
William Lallemande67c80b2020-03-25 14:42:37 +010011493 /* check if it's possible to insert this new crtlist_entry */
11494 entry->node.key = store;
11495 inserted = ebpt_insert(&crtlist->entries, &entry->node);
11496 if (inserted != &entry->node) {
11497 memprintf(&err, "file already exists in this directory!");
William Lallemande67c80b2020-03-25 14:42:37 +010011498 goto error;
11499 }
11500
William Lallemandb74d5642020-04-21 16:54:19 +020011501 /* this is supposed to be a directory (EB_ROOT_UNIQUE), so no ssl_conf are allowed */
11502 if ((entry->ssl_conf || entry->filters) && eb_gettag(crtlist->entries.b[EB_RGHT])) {
11503 memprintf(&err, "this is a directory, SSL configuration and filters are not allowed");
11504 goto error;
11505 }
11506
William Lallemande67c80b2020-03-25 14:42:37 +010011507 LIST_ADDQ(&crtlist->ord_entries, &entry->by_crtlist);
William Lallemande67c80b2020-03-25 14:42:37 +010011508 entry->crtlist = crtlist;
11509 LIST_ADDQ(&store->crtlist_entry, &entry->by_ckch_store);
11510
11511 appctx->st2 = SETCERT_ST_INIT;
11512 appctx->ctx.cli.p0 = crtlist;
11513 appctx->ctx.cli.p1 = entry;
11514
11515 /* unlock is done in the release handler */
11516 return 0;
11517
11518error:
William Lallemande718dfb2020-04-10 11:09:25 +020011519 crtlist_entry_free(entry);
William Lallemande67c80b2020-03-25 14:42:37 +010011520 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11521 err = memprintf(&err, "Can't edit the crt-list: %s\n", err ? err : "");
11522 return cli_dynerr(appctx, err);
11523}
11524
William Lallemand0a9b9412020-04-06 17:43:05 +020011525/* Parse a "del ssl crt-list <crt-list> <certfile>" line. */
11526static int cli_parse_del_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
11527{
11528 struct ckch_store *store;
11529 char *err = NULL;
11530 char *crtlist_path, *cert_path;
11531 struct ebmb_node *ebmb;
11532 struct ebpt_node *ebpt;
11533 struct crtlist *crtlist;
11534 struct crtlist_entry *entry = NULL;
11535 struct ckch_inst *inst, *inst_s;
11536 int linenum = 0;
11537 char *colons;
11538
11539 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11540 return 1;
11541
11542 if (!*args[3] || !*args[4])
11543 return cli_err(appctx, "'del ssl crtlist' expects a filename and a certificate name\n");
11544
William Lallemand463b5242020-04-08 10:30:44 +020011545 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11546 return cli_err(appctx, "Can't delete!\nOperations on certificates are currently locked!\n");
11547
William Lallemand0a9b9412020-04-06 17:43:05 +020011548 crtlist_path = args[3];
11549 cert_path = args[4];
11550
11551 colons = strchr(cert_path, ':');
11552 if (colons) {
11553 char *endptr;
11554
11555 linenum = strtol(colons + 1, &endptr, 10);
11556 if (colons + 1 == endptr || *endptr != '\0') {
11557 memprintf(&err, "wrong line number after colons in '%s'!", cert_path);
11558 goto error;
11559 }
11560 *colons = '\0';
11561 }
11562 /* look for crtlist */
11563 ebmb = ebst_lookup(&crtlists_tree, crtlist_path);
11564 if (!ebmb) {
11565 memprintf(&err, "crt-list '%s' does not exist!", crtlist_path);
11566 goto error;
11567 }
11568 crtlist = ebmb_entry(ebmb, struct crtlist, node);
11569
11570 /* look for store */
11571 store = ckchs_lookup(cert_path);
11572 if (store == NULL) {
11573 memprintf(&err, "certificate '%s' does not exist!", cert_path);
11574 goto error;
11575 }
William Lallemand36ccc392020-04-08 10:57:24 +020011576 if (store->multi) {
11577 memprintf(&err, "certificate '%s' is a bundle. You can disable the bundle merging with the directive 'ssl-load-extra-files' in the global section.", cert_path);
11578 goto error;
11579 }
William Lallemand0a9b9412020-04-06 17:43:05 +020011580 if (store->ckch == NULL || store->ckch->cert == NULL) {
11581 memprintf(&err, "certificate '%s' is empty!", cert_path);
11582 goto error;
11583 }
11584
11585 ebpt = ebpt_lookup(&crtlist->entries, store);
11586 if (!ebpt) {
11587 memprintf(&err, "certificate '%s' can't be found in crt-list '%s'!", cert_path, crtlist_path);
11588 goto error;
11589 }
11590
11591 /* list the line number of entries for errors in err, and select the right ebpt */
11592 for (; ebpt; ebpt = ebpt_next_dup(ebpt)) {
11593 struct crtlist_entry *tmp;
11594
11595 tmp = ebpt_entry(ebpt, struct crtlist_entry, node);
11596 memprintf(&err, "%s%s%d", err ? err : "", err ? ", " : "", tmp->linenum);
11597
11598 /* select the entry we wanted */
11599 if (linenum == 0 || tmp->linenum == linenum) {
11600 if (!entry)
11601 entry = tmp;
11602 }
11603 }
11604
11605 /* we didn't found the specified entry */
11606 if (!entry) {
11607 memprintf(&err, "found a certificate '%s' but the line number is incorrect, please specify a correct line number preceded by colons (%s)!", cert_path, err ? err : NULL);
11608 goto error;
11609 }
11610
11611 /* we didn't specified a line number but there were several entries */
11612 if (linenum == 0 && ebpt_next_dup(&entry->node)) {
11613 memprintf(&err, "found the certificate '%s' in several entries, please specify a line number preceded by colons (%s)!", cert_path, err ? err : NULL);
11614 goto error;
11615 }
11616
11617 /* upon error free the ckch_inst and everything inside */
11618
11619 ebpt_delete(&entry->node);
11620 LIST_DEL(&entry->by_crtlist);
11621 LIST_DEL(&entry->by_ckch_store);
11622
11623 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) {
11624 struct sni_ctx *sni, *sni_s;
11625
11626 HA_RWLOCK_WRLOCK(SNI_LOCK, &inst->bind_conf->sni_lock);
11627 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
11628 ebmb_delete(&sni->name);
11629 LIST_DEL(&sni->by_ckch_inst);
William Lallemand02e19a52020-04-08 16:11:26 +020011630 SSL_CTX_free(sni->ctx);
William Lallemand0a9b9412020-04-06 17:43:05 +020011631 free(sni);
11632 }
11633 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &inst->bind_conf->sni_lock);
11634 LIST_DEL(&inst->by_ckchs);
11635 free(inst);
11636 }
11637
11638 crtlist_free_filters(entry->filters);
11639 ssl_sock_free_ssl_conf(entry->ssl_conf);
11640 free(entry->ssl_conf);
11641 free(entry);
11642
11643 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11644 err = memprintf(&err, "Entry '%s' deleted in crtlist '%s'!\n", cert_path, crtlist_path);
11645 return cli_dynmsg(appctx, LOG_NOTICE, err);
11646
11647error:
11648 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11649 err = memprintf(&err, "Can't delete the entry: %s\n", err ? err : "");
11650 return cli_dynerr(appctx, err);
11651}
William Lallemande67c80b2020-03-25 14:42:37 +010011652
William Lallemand44b35322019-10-17 16:28:40 +020011653/* Type of SSL payloads that can be updated over the CLI */
11654
11655enum {
11656 CERT_TYPE_PEM = 0,
William Lallemand4c5adbf2020-02-24 14:23:22 +010011657 CERT_TYPE_KEY,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011658#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011659 CERT_TYPE_OCSP,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011660#endif
William Lallemand44b35322019-10-17 16:28:40 +020011661 CERT_TYPE_ISSUER,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011662#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011663 CERT_TYPE_SCTL,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010011664#endif
William Lallemand44b35322019-10-17 16:28:40 +020011665 CERT_TYPE_MAX,
11666};
11667
11668struct {
11669 const char *ext;
11670 int type;
11671 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
11672 /* add a parsing callback */
William Lallemandf29cdef2019-10-23 15:00:52 +020011673} cert_exts[CERT_TYPE_MAX+1] = {
William Lallemand44b35322019-10-17 16:28:40 +020011674 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand4c5adbf2020-02-24 14:23:22 +010011675 [CERT_TYPE_KEY] = { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
William Lallemand541a5342019-10-23 14:11:54 +020011676#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011677 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020011678#endif
11679#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020011680 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020011681#endif
William Lallemand44b35322019-10-17 16:28:40 +020011682 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
William Lallemandf29cdef2019-10-23 15:00:52 +020011683 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
William Lallemand44b35322019-10-17 16:28:40 +020011684};
11685
William Lallemand8f840d72019-10-23 10:53:05 +020011686
William Lallemandd4f946c2019-12-05 10:26:40 +010011687/* release function of the `show ssl cert' command */
11688static void cli_release_show_cert(struct appctx *appctx)
11689{
11690 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11691}
11692
11693/* IO handler of "show ssl cert <filename>" */
11694static int cli_io_handler_show_cert(struct appctx *appctx)
11695{
11696 struct buffer *trash = alloc_trash_chunk();
11697 struct ebmb_node *node;
11698 struct stream_interface *si = appctx->owner;
11699 struct ckch_store *ckchs;
William Lallemandd4f946c2019-12-05 10:26:40 +010011700
11701 if (trash == NULL)
11702 return 1;
11703
11704 if (!appctx->ctx.ssl.old_ckchs) {
11705 if (ckchs_transaction.old_ckchs) {
11706 ckchs = ckchs_transaction.old_ckchs;
11707 chunk_appendf(trash, "# transaction\n");
11708 if (!ckchs->multi) {
11709 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010011710#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010011711 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010011712 int n;
11713
William Lallemandd4f946c2019-12-05 10:26:40 +010011714 chunk_appendf(trash, "*%s:", ckchs->path);
11715 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
11716 if (ckchs->ckch[n].cert)
11717 chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
11718 }
11719 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010011720#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010011721 }
11722 }
11723 }
11724
11725 if (!appctx->ctx.cli.p0) {
11726 chunk_appendf(trash, "# filename\n");
11727 node = ebmb_first(&ckchs_tree);
11728 } else {
11729 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
11730 }
11731 while (node) {
11732 ckchs = ebmb_entry(node, struct ckch_store, node);
11733 if (!ckchs->multi) {
11734 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010011735#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010011736 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010011737 int n;
11738
William Lallemandd4f946c2019-12-05 10:26:40 +010011739 chunk_appendf(trash, "%s:", ckchs->path);
11740 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
11741 if (ckchs->ckch[n].cert)
11742 chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
11743 }
11744 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010011745#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010011746 }
11747
11748 node = ebmb_next(node);
11749 if (ci_putchk(si_ic(si), trash) == -1) {
11750 si_rx_room_blk(si);
11751 goto yield;
11752 }
11753 }
11754
11755 appctx->ctx.cli.p0 = NULL;
11756 free_trash_chunk(trash);
11757 return 1;
11758yield:
11759
11760 free_trash_chunk(trash);
11761 appctx->ctx.cli.p0 = ckchs;
11762 return 0; /* should come back */
11763}
11764
11765/* IO handler of the details "show ssl cert <filename>" */
11766static int cli_io_handler_show_cert_detail(struct appctx *appctx)
11767{
11768 struct stream_interface *si = appctx->owner;
11769 struct ckch_store *ckchs = appctx->ctx.cli.p0;
11770 struct buffer *out = alloc_trash_chunk();
11771 struct buffer *tmp = alloc_trash_chunk();
11772 X509_NAME *name = NULL;
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011773 STACK_OF(X509) *chain;
Willy Tarreau105599c2020-02-25 08:59:23 +010011774 unsigned int len = 0;
William Lallemandd4f946c2019-12-05 10:26:40 +010011775 int write = -1;
11776 BIO *bio = NULL;
William Lallemand35f4a9d2020-02-25 11:56:32 +010011777 int i;
William Lallemandd4f946c2019-12-05 10:26:40 +010011778
11779 if (!tmp || !out)
William Lallemand18eeb8e2020-03-20 14:42:36 +010011780 goto end_no_putchk;
William Lallemandd4f946c2019-12-05 10:26:40 +010011781
11782 if (!ckchs->multi) {
11783 chunk_appendf(out, "Filename: ");
11784 if (ckchs == ckchs_transaction.new_ckchs)
11785 chunk_appendf(out, "*");
11786 chunk_appendf(out, "%s\n", ckchs->path);
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011787
William Lallemand59c16fc2020-03-19 20:26:02 +010011788 chunk_appendf(out, "Status: ");
11789 if (ckchs->ckch->cert == NULL)
11790 chunk_appendf(out, "Empty\n");
11791 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
11792 chunk_appendf(out, "Unused\n");
11793 else
11794 chunk_appendf(out, "Used\n");
11795
William Lallemandea987ed2020-03-19 16:48:33 +010011796 if (ckchs->ckch->cert == NULL)
11797 goto end;
11798
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011799 chain = ckchs->ckch->chain;
11800 if (chain == NULL) {
11801 struct issuer_chain *issuer;
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +010011802 issuer = ssl_get0_issuer_chain(ckchs->ckch->cert);
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011803 if (issuer) {
11804 chain = issuer->chain;
11805 chunk_appendf(out, "Chain Filename: ");
11806 chunk_appendf(out, "%s\n", issuer->path);
11807 }
11808 }
William Lallemandd4f946c2019-12-05 10:26:40 +010011809 chunk_appendf(out, "Serial: ");
11810 if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1)
11811 goto end;
11812 dump_binary(out, tmp->area, tmp->data);
11813 chunk_appendf(out, "\n");
11814
11815 chunk_appendf(out, "notBefore: ");
11816 chunk_reset(tmp);
11817 if ((bio = BIO_new(BIO_s_mem())) == NULL)
11818 goto end;
11819 if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0)
11820 goto end;
11821 write = BIO_read(bio, tmp->area, tmp->size-1);
11822 tmp->area[write] = '\0';
11823 BIO_free(bio);
William Lallemand67b991d2020-03-20 14:10:17 +010011824 bio = NULL;
William Lallemandd4f946c2019-12-05 10:26:40 +010011825 chunk_appendf(out, "%s\n", tmp->area);
11826
11827 chunk_appendf(out, "notAfter: ");
11828 chunk_reset(tmp);
11829 if ((bio = BIO_new(BIO_s_mem())) == NULL)
11830 goto end;
11831 if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0)
11832 goto end;
11833 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
11834 goto end;
11835 tmp->area[write] = '\0';
11836 BIO_free(bio);
William Lallemand67b991d2020-03-20 14:10:17 +010011837 bio = NULL;
William Lallemandd4f946c2019-12-05 10:26:40 +010011838 chunk_appendf(out, "%s\n", tmp->area);
11839
William Lallemandd4f946c2019-12-05 10:26:40 +010011840#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
11841 chunk_appendf(out, "Subject Alternative Name: ");
11842 if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1)
11843 goto end;
11844 *(out->area + out->data) = '\0';
11845 chunk_appendf(out, "\n");
11846#endif
11847 chunk_reset(tmp);
11848 chunk_appendf(out, "Algorithm: ");
11849 if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0)
11850 goto end;
11851 chunk_appendf(out, "%s\n", tmp->area);
11852
11853 chunk_reset(tmp);
11854 chunk_appendf(out, "SHA1 FingerPrint: ");
Willy Tarreau105599c2020-02-25 08:59:23 +010011855 if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemandd4f946c2019-12-05 10:26:40 +010011856 goto end;
Willy Tarreau105599c2020-02-25 08:59:23 +010011857 tmp->data = len;
William Lallemandd4f946c2019-12-05 10:26:40 +010011858 dump_binary(out, tmp->area, tmp->data);
11859 chunk_appendf(out, "\n");
William Lallemand35f4a9d2020-02-25 11:56:32 +010011860
William Lallemanda90e5932020-02-25 14:07:58 +010011861 chunk_appendf(out, "Subject: ");
11862 if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL)
11863 goto end;
11864 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11865 goto end;
11866 *(tmp->area + tmp->data) = '\0';
11867 chunk_appendf(out, "%s\n", tmp->area);
11868
11869 chunk_appendf(out, "Issuer: ");
11870 if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL)
11871 goto end;
11872 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11873 goto end;
11874 *(tmp->area + tmp->data) = '\0';
11875 chunk_appendf(out, "%s\n", tmp->area);
11876
William Lallemand35f4a9d2020-02-25 11:56:32 +010011877 /* Displays subject of each certificate in the chain */
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010011878 for (i = 0; i < sk_X509_num(chain); i++) {
11879 X509 *ca = sk_X509_value(chain, i);
William Lallemand35f4a9d2020-02-25 11:56:32 +010011880
William Lallemandbb7288a2020-02-25 14:04:33 +010011881 chunk_appendf(out, "Chain Subject: ");
William Lallemand35f4a9d2020-02-25 11:56:32 +010011882 if ((name = X509_get_subject_name(ca)) == NULL)
11883 goto end;
11884 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11885 goto end;
11886 *(tmp->area + tmp->data) = '\0';
11887 chunk_appendf(out, "%s\n", tmp->area);
11888
William Lallemandbb7288a2020-02-25 14:04:33 +010011889 chunk_appendf(out, "Chain Issuer: ");
11890 if ((name = X509_get_issuer_name(ca)) == NULL)
11891 goto end;
11892 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
11893 goto end;
11894 *(tmp->area + tmp->data) = '\0';
11895 chunk_appendf(out, "%s\n", tmp->area);
William Lallemand35f4a9d2020-02-25 11:56:32 +010011896 }
William Lallemandd4f946c2019-12-05 10:26:40 +010011897 }
11898
William Lallemandea987ed2020-03-19 16:48:33 +010011899end:
William Lallemandd4f946c2019-12-05 10:26:40 +010011900 if (ci_putchk(si_ic(si), out) == -1) {
11901 si_rx_room_blk(si);
11902 goto yield;
11903 }
11904
William Lallemand18eeb8e2020-03-20 14:42:36 +010011905end_no_putchk:
William Lallemand67b991d2020-03-20 14:10:17 +010011906 if (bio)
11907 BIO_free(bio);
William Lallemandd4f946c2019-12-05 10:26:40 +010011908 free_trash_chunk(tmp);
11909 free_trash_chunk(out);
11910 return 1;
11911yield:
11912 free_trash_chunk(tmp);
11913 free_trash_chunk(out);
11914 return 0; /* should come back */
11915}
11916
11917/* parsing function for 'show ssl cert [certfile]' */
11918static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
11919{
11920 struct ckch_store *ckchs;
11921
11922 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
11923 return cli_err(appctx, "Can't allocate memory!\n");
11924
11925 /* The operations on the CKCH architecture are locked so we can
11926 * manipulate ckch_store and ckch_inst */
11927 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11928 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
11929
11930 /* check if there is a certificate to lookup */
11931 if (*args[3]) {
11932 if (*args[3] == '*') {
11933 if (!ckchs_transaction.new_ckchs)
11934 goto error;
11935
11936 ckchs = ckchs_transaction.new_ckchs;
11937
11938 if (strcmp(args[3] + 1, ckchs->path))
11939 goto error;
11940
11941 } else {
11942 if ((ckchs = ckchs_lookup(args[3])) == NULL)
11943 goto error;
11944
11945 }
11946
11947 if (ckchs->multi)
11948 goto error;
11949
11950 appctx->ctx.cli.p0 = ckchs;
11951 /* use the IO handler that shows details */
11952 appctx->io_handler = cli_io_handler_show_cert_detail;
11953 }
11954
11955 return 0;
11956
11957error:
11958 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11959 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
11960}
11961
William Lallemand430413e2019-10-28 14:30:47 +010011962/* release function of the `set ssl cert' command, free things and unlock the spinlock */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011963static void cli_release_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020011964{
11965 struct ckch_store *new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011966
William Lallemand430413e2019-10-28 14:30:47 +010011967 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand8f840d72019-10-23 10:53:05 +020011968
William Lallemand430413e2019-10-28 14:30:47 +010011969 if (appctx->st2 != SETCERT_ST_FIN) {
William Lallemand8f840d72019-10-23 10:53:05 +020011970 /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */
William Lallemandbeea2a42019-10-30 17:45:33 +010011971 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020011972
William Lallemandbeea2a42019-10-30 17:45:33 +010011973 /* if the allocation failed, we need to free everything from the temporary list */
William Lallemandba1c33f2020-04-08 17:55:45 +020011974 ckch_store_free(new_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020011975 }
11976}
11977
11978
11979/*
11980 * This function tries to create the new ckch_inst and their SNIs
11981 */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011982static int cli_io_handler_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020011983{
11984 struct stream_interface *si = appctx->owner;
11985 int y = 0;
11986 char *err = NULL;
11987 int errcode = 0;
11988 struct ckch_store *old_ckchs, *new_ckchs = NULL;
11989 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020011990 struct buffer *trash = alloc_trash_chunk();
William Lallemand8ef0c2a2019-11-21 16:30:34 +010011991 struct sni_ctx *sc0, *sc0s;
William Lallemand90afe902020-03-30 19:29:45 +020011992 struct crtlist_entry *entry;
William Lallemand8f840d72019-10-23 10:53:05 +020011993
William Lallemand33cc76f2019-10-31 11:43:45 +010011994 if (trash == NULL)
11995 goto error;
11996
William Lallemand8f840d72019-10-23 10:53:05 +020011997 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
11998 goto error;
11999
William Lallemand430413e2019-10-28 14:30:47 +010012000 while (1) {
12001 switch (appctx->st2) {
12002 case SETCERT_ST_INIT:
12003 /* This state just print the update message */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012004 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
William Lallemand430413e2019-10-28 14:30:47 +010012005 if (ci_putchk(si_ic(si), trash) == -1) {
12006 si_rx_room_blk(si);
William Lallemand8f840d72019-10-23 10:53:05 +020012007 goto yield;
William Lallemand430413e2019-10-28 14:30:47 +010012008 }
12009 appctx->st2 = SETCERT_ST_GEN;
12010 /* fallthrough */
12011 case SETCERT_ST_GEN:
12012 /*
12013 * This state generates the ckch instances with their
12014 * sni_ctxs and SSL_CTX.
12015 *
William Lallemand430413e2019-10-28 14:30:47 +010012016 * Since the SSL_CTX generation can be CPU consumer, we
12017 * yield every 10 instances.
12018 */
William Lallemand8f840d72019-10-23 10:53:05 +020012019
William Lallemandbeea2a42019-10-30 17:45:33 +010012020 old_ckchs = appctx->ctx.ssl.old_ckchs;
12021 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020012022
William Lallemandbeea2a42019-10-30 17:45:33 +010012023 if (!new_ckchs)
12024 continue;
William Lallemand8f840d72019-10-23 10:53:05 +020012025
William Lallemandbeea2a42019-10-30 17:45:33 +010012026 /* get the next ckchi to regenerate */
12027 ckchi = appctx->ctx.ssl.next_ckchi;
12028 /* we didn't start yet, set it to the first elem */
12029 if (ckchi == NULL)
12030 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020012031
William Lallemandbeea2a42019-10-30 17:45:33 +010012032 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
12033 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
12034 struct ckch_inst *new_inst;
William Lallemand38df1c82019-12-04 15:39:35 +010012035 char **sni_filter = NULL;
12036 int fcount = 0;
William Lallemand8f840d72019-10-23 10:53:05 +020012037
William Lallemandbeea2a42019-10-30 17:45:33 +010012038 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
12039 if (y >= 10) {
12040 /* save the next ckchi to compute */
12041 appctx->ctx.ssl.next_ckchi = ckchi;
12042 goto yield;
12043 }
William Lallemandcaa16192020-04-08 16:29:15 +020012044
12045 if (ckchi->crtlist_entry) {
12046 sni_filter = ckchi->crtlist_entry->filters;
12047 fcount = ckchi->crtlist_entry->fcount;
William Lallemand67630162020-03-09 16:56:39 +010012048 }
William Lallemand38df1c82019-12-04 15:39:35 +010012049
William Lallemandbeea2a42019-10-30 17:45:33 +010012050 if (new_ckchs->multi)
William Lallemand38df1c82019-12-04 15:39:35 +010012051 errcode |= ckch_inst_new_load_multi_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, &new_inst, &err);
William Lallemandbeea2a42019-10-30 17:45:33 +010012052 else
William Lallemand38df1c82019-12-04 15:39:35 +010012053 errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, &new_inst, &err);
12054
William Lallemandbeea2a42019-10-30 17:45:33 +010012055 if (errcode & ERR_CODE)
12056 goto error;
William Lallemand8f840d72019-10-23 10:53:05 +020012057
William Lallemand21724f02019-11-04 17:56:13 +010012058 /* if the previous ckchi was used as the default */
12059 if (ckchi->is_default)
12060 new_inst->is_default = 1;
12061
William Lallemand8ef0c2a2019-11-21 16:30:34 +010012062 /* we need to initialize the SSL_CTX generated */
William Lallemand696f3172020-02-07 20:45:24 +010012063 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
12064 list_for_each_entry_safe(sc0, sc0s, &new_inst->sni_ctx, by_ckch_inst) {
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012065 if (!sc0->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
William Lallemand8ef0c2a2019-11-21 16:30:34 +010012066 errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err);
12067 if (errcode & ERR_CODE)
12068 goto error;
12069 }
12070 }
12071
12072
William Lallemandbeea2a42019-10-30 17:45:33 +010012073 /* display one dot per new instance */
12074 chunk_appendf(trash, ".");
12075 /* link the new ckch_inst to the duplicate */
12076 LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
12077 y++;
12078 }
William Lallemand430413e2019-10-28 14:30:47 +010012079 appctx->st2 = SETCERT_ST_INSERT;
12080 /* fallthrough */
12081 case SETCERT_ST_INSERT:
12082 /* The generation is finished, we can insert everything */
William Lallemand8f840d72019-10-23 10:53:05 +020012083
William Lallemandbeea2a42019-10-30 17:45:33 +010012084 old_ckchs = appctx->ctx.ssl.old_ckchs;
12085 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020012086
William Lallemandbeea2a42019-10-30 17:45:33 +010012087 if (!new_ckchs)
12088 continue;
William Lallemand430413e2019-10-28 14:30:47 +010012089
William Lallemand90afe902020-03-30 19:29:45 +020012090 /* get the list of crtlist_entry in the old store, and update the pointers to the store */
12091 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
12092 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
12093 ebpt_delete(&entry->node);
12094 /* change the ptr and reinsert the node */
12095 entry->node.key = new_ckchs;
12096 ebpt_insert(&entry->crtlist->entries, &entry->node);
12097 }
12098
William Lallemand21724f02019-11-04 17:56:13 +010012099 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
William Lallemandbeea2a42019-10-30 17:45:33 +010012100 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
12101 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
12102 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
12103 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
12104 }
William Lallemand8f840d72019-10-23 10:53:05 +020012105
William Lallemandbeea2a42019-10-30 17:45:33 +010012106 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
12107 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
Willy Tarreaua6cd0782020-05-01 11:38:39 +020012108 struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf;
William Lallemandd5e93772020-04-09 17:12:16 +020012109
12110 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
William Lallemandd9d5d1b2020-04-09 16:31:05 +020012111 ckch_inst_free(ckchi);
William Lallemandd5e93772020-04-09 17:12:16 +020012112 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
William Lallemandbeea2a42019-10-30 17:45:33 +010012113 }
William Lallemand8f840d72019-10-23 10:53:05 +020012114
William Lallemandbeea2a42019-10-30 17:45:33 +010012115 /* Replace the old ckchs by the new one */
William Lallemandba1c33f2020-04-08 17:55:45 +020012116 ckch_store_free(old_ckchs);
William Lallemandbeea2a42019-10-30 17:45:33 +010012117 ebst_insert(&ckchs_tree, &new_ckchs->node);
William Lallemand430413e2019-10-28 14:30:47 +010012118 appctx->st2 = SETCERT_ST_FIN;
12119 /* fallthrough */
12120 case SETCERT_ST_FIN:
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012121 /* we achieved the transaction, we can set everything to NULL */
12122 free(ckchs_transaction.path);
12123 ckchs_transaction.path = NULL;
12124 ckchs_transaction.new_ckchs = NULL;
12125 ckchs_transaction.old_ckchs = NULL;
William Lallemand430413e2019-10-28 14:30:47 +010012126 goto end;
12127 }
William Lallemand8f840d72019-10-23 10:53:05 +020012128 }
William Lallemand430413e2019-10-28 14:30:47 +010012129end:
William Lallemand8f840d72019-10-23 10:53:05 +020012130
William Lallemanded442432019-11-21 16:41:07 +010012131 chunk_appendf(trash, "\n");
12132 if (errcode & ERR_WARN)
Tim Duesterhusc0e820c2019-11-23 23:52:30 +010012133 chunk_appendf(trash, "%s", err);
William Lallemanded442432019-11-21 16:41:07 +010012134 chunk_appendf(trash, "Success!\n");
William Lallemand430413e2019-10-28 14:30:47 +010012135 if (ci_putchk(si_ic(si), trash) == -1)
12136 si_rx_room_blk(si);
12137 free_trash_chunk(trash);
12138 /* success: call the release function and don't come back */
12139 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020012140yield:
12141 /* store the state */
12142 if (ci_putchk(si_ic(si), trash) == -1)
12143 si_rx_room_blk(si);
12144 free_trash_chunk(trash);
12145 si_rx_endp_more(si); /* let's come back later */
William Lallemand8f840d72019-10-23 10:53:05 +020012146 return 0; /* should come back */
12147
12148error:
12149 /* spin unlock and free are done in the release function */
William Lallemand33cc76f2019-10-31 11:43:45 +010012150 if (trash) {
12151 chunk_appendf(trash, "\n%sFailed!\n", err);
12152 if (ci_putchk(si_ic(si), trash) == -1)
12153 si_rx_room_blk(si);
12154 free_trash_chunk(trash);
12155 }
William Lallemand430413e2019-10-28 14:30:47 +010012156 /* error: call the release function and don't come back */
12157 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020012158}
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012159
12160/*
12161 * Parsing function of 'commit ssl cert'
12162 */
12163static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
12164{
12165 char *err = NULL;
12166
William Lallemand230662a2019-12-03 13:32:54 +010012167 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12168 return 1;
12169
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012170 if (!*args[3])
12171 return cli_err(appctx, "'commit ssl cert expects a filename\n");
12172
12173 /* The operations on the CKCH architecture are locked so we can
12174 * manipulate ckch_store and ckch_inst */
12175 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12176 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
12177
12178 if (!ckchs_transaction.path) {
12179 memprintf(&err, "No ongoing transaction! !\n");
12180 goto error;
12181 }
12182
12183 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
12184 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
12185 goto error;
12186 }
12187
William Lallemand4c5adbf2020-02-24 14:23:22 +010012188#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
12189 if (ckchs_transaction.new_ckchs->multi) {
12190 int n;
12191
12192 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
12193 if (ckchs_transaction.new_ckchs->ckch[n].cert && !X509_check_private_key(ckchs_transaction.new_ckchs->ckch[n].cert, ckchs_transaction.new_ckchs->ckch[n].key)) {
12194 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
12195 goto error;
12196 }
12197 }
12198 } else
12199#endif
12200 {
12201 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
12202 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
12203 goto error;
12204 }
12205 }
12206
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012207 /* init the appctx structure */
12208 appctx->st2 = SETCERT_ST_INIT;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012209 appctx->ctx.ssl.next_ckchi = NULL;
12210 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
12211 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
12212
12213 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
12214 return 0;
12215
12216error:
12217
12218 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12219 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
12220
12221 return cli_dynerr(appctx, err);
12222}
12223
William Lallemand8f840d72019-10-23 10:53:05 +020012224/*
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012225 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
William Lallemand8f840d72019-10-23 10:53:05 +020012226 */
William Lallemand150bfa82019-09-19 17:12:49 +020012227static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
12228{
William Lallemand0c3b7d92019-10-18 11:27:07 +020012229 struct ckch_store *new_ckchs = NULL;
William Lallemand8f840d72019-10-23 10:53:05 +020012230 struct ckch_store *old_ckchs = NULL;
William Lallemand150bfa82019-09-19 17:12:49 +020012231 char *err = NULL;
William Lallemand963b2e72019-10-14 11:38:36 +020012232 int i;
William Lallemand849eed62019-10-17 16:23:50 +020012233 int bundle = -1; /* TRUE if >= 0 (ckch index) */
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012234 int errcode = 0;
William Lallemand44b35322019-10-17 16:28:40 +020012235 char *end;
12236 int type = CERT_TYPE_PEM;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012237 struct cert_key_and_chain *ckch;
12238 struct buffer *buf;
William Lallemand8f840d72019-10-23 10:53:05 +020012239
William Lallemand230662a2019-12-03 13:32:54 +010012240 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12241 return 1;
12242
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012243 if ((buf = alloc_trash_chunk()) == NULL)
12244 return cli_err(appctx, "Can't allocate memory\n");
William Lallemand150bfa82019-09-19 17:12:49 +020012245
12246 if (!*args[3] || !payload)
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012247 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
William Lallemand150bfa82019-09-19 17:12:49 +020012248
12249 /* The operations on the CKCH architecture are locked so we can
12250 * manipulate ckch_store and ckch_inst */
12251 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12252 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
12253
William Lallemand8f840d72019-10-23 10:53:05 +020012254 if (!chunk_strcpy(buf, args[3])) {
12255 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
12256 errcode |= ERR_ALERT | ERR_FATAL;
12257 goto end;
12258 }
12259
William Lallemand44b35322019-10-17 16:28:40 +020012260 /* check which type of file we want to update */
William Lallemandf29cdef2019-10-23 15:00:52 +020012261 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
William Lallemand8f840d72019-10-23 10:53:05 +020012262 end = strrchr(buf->area, '.');
William Lallemand44b35322019-10-17 16:28:40 +020012263 if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) {
12264 *end = '\0';
12265 type = cert_exts[i].type;
12266 break;
12267 }
12268 }
12269
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012270 appctx->ctx.ssl.old_ckchs = NULL;
12271 appctx->ctx.ssl.new_ckchs = NULL;
William Lallemand849eed62019-10-17 16:23:50 +020012272
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012273 /* if there is an ongoing transaction */
12274 if (ckchs_transaction.path) {
12275 /* if the ongoing transaction is a bundle, we need to find which part of the bundle need to be updated */
William Lallemand963b2e72019-10-14 11:38:36 +020012276#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012277 if (ckchs_transaction.new_ckchs->multi) {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012278 char *end;
William Lallemand963b2e72019-10-14 11:38:36 +020012279 int j;
William Lallemand150bfa82019-09-19 17:12:49 +020012280
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012281 /* check if it was used in a bundle by removing the
William Lallemand963b2e72019-10-14 11:38:36 +020012282 * .dsa/.rsa/.ecdsa at the end of the filename */
William Lallemand8f840d72019-10-23 10:53:05 +020012283 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012284 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemand963b2e72019-10-14 11:38:36 +020012285 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
12286 bundle = j; /* keep the type of certificate so we insert it at the right place */
12287 *end = '\0'; /* it's a bundle let's end the string*/
12288 break;
12289 }
William Lallemand150bfa82019-09-19 17:12:49 +020012290 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012291 if (bundle < 0) {
12292 memprintf(&err, "The ongoing transaction is the '%s' bundle. You need to specify which part of the bundle you want to update ('%s.{rsa,ecdsa,dsa}')\n", ckchs_transaction.path, buf->area);
12293 errcode |= ERR_ALERT | ERR_FATAL;
12294 goto end;
12295 }
12296 }
12297#endif
12298
12299 /* if there is an ongoing transaction, check if this is the same file */
12300 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
12301 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
12302 errcode |= ERR_ALERT | ERR_FATAL;
12303 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020012304 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012305
12306 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
12307
12308 } else {
12309 struct ckch_store *find_ckchs[2] = { NULL, NULL };
12310
12311 /* lookup for the certificate in the tree:
12312 * check if this is used as a bundle AND as a unique certificate */
12313 for (i = 0; i < 2; i++) {
12314
12315 if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) {
12316 /* only the bundle name is in the tree and you should
12317 * never update a bundle name, only a filename */
12318 if (bundle < 0 && find_ckchs[i]->multi) {
12319 /* we tried to look for a non-bundle and we found a bundle */
12320 memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n",
12321 err ? err : "", args[3], args[3]);
12322 errcode |= ERR_ALERT | ERR_FATAL;
12323 goto end;
12324 }
William Lallemand3246d942019-11-04 14:02:11 +010012325 /* If we want a bundle but this is not a bundle
12326 * example: When you try to update <file>.rsa, but
12327 * <file> is a regular file */
12328 if (bundle >= 0 && find_ckchs[i]->multi == 0) {
12329 find_ckchs[i] = NULL;
12330 break;
12331 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012332 }
12333#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
12334 {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012335 char *end;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012336 int j;
12337
12338 /* check if it was used in a bundle by removing the
12339 * .dsa/.rsa/.ecdsa at the end of the filename */
12340 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010012341 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012342 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
12343 bundle = j; /* keep the type of certificate so we insert it at the right place */
12344 *end = '\0'; /* it's a bundle let's end the string*/
12345 break;
12346 }
12347 }
William Lallemand37031b82019-11-04 13:38:53 +010012348 if (bundle < 0) /* we didn't find a bundle extension */
12349 break;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012350 }
William Lallemand963b2e72019-10-14 11:38:36 +020012351#else
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012352 /* bundles are not supported here, so we don't need to lookup again */
12353 break;
William Lallemand963b2e72019-10-14 11:38:36 +020012354#endif
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012355 }
12356
12357 if (find_ckchs[0] && find_ckchs[1]) {
12358 memprintf(&err, "%sUpdating a certificate which is used in the HAProxy configuration as a bundle and as a unique certificate is not supported. ('%s' and '%s')\n",
12359 err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path);
12360 errcode |= ERR_ALERT | ERR_FATAL;
12361 goto end;
12362 }
12363
12364 appctx->ctx.ssl.old_ckchs = find_ckchs[0] ? find_ckchs[0] : find_ckchs[1];
William Lallemand150bfa82019-09-19 17:12:49 +020012365 }
12366
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012367 if (!appctx->ctx.ssl.old_ckchs) {
12368 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
William Lallemand150bfa82019-09-19 17:12:49 +020012369 err ? err : "");
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012370 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand8f840d72019-10-23 10:53:05 +020012371 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020012372 }
12373
William Lallemand8a7fdf02019-11-04 10:59:32 +010012374 if (!appctx->ctx.ssl.path) {
12375 /* this is a new transaction, set the path of the transaction */
12376 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
12377 if (!appctx->ctx.ssl.path) {
12378 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
12379 errcode |= ERR_ALERT | ERR_FATAL;
12380 goto end;
12381 }
12382 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012383
12384 old_ckchs = appctx->ctx.ssl.old_ckchs;
12385
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012386 /* duplicate the ckch store */
12387 new_ckchs = ckchs_dup(old_ckchs);
12388 if (!new_ckchs) {
12389 memprintf(&err, "%sCannot allocate memory!\n",
12390 err ? err : "");
12391 errcode |= ERR_ALERT | ERR_FATAL;
12392 goto end;
12393 }
12394
12395 if (!new_ckchs->multi)
12396 ckch = new_ckchs->ckch;
12397 else
12398 ckch = &new_ckchs->ckch[bundle];
12399
12400 /* appply the change on the duplicate */
12401 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
12402 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
12403 errcode |= ERR_ALERT | ERR_FATAL;
12404 goto end;
12405 }
12406
12407 appctx->ctx.ssl.new_ckchs = new_ckchs;
12408
12409 /* we succeed, we can save the ckchs in the transaction */
12410
12411 /* if there wasn't a transaction, update the old ckchs */
William Dauchyc8bb1532019-11-24 15:04:20 +010012412 if (!ckchs_transaction.old_ckchs) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012413 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
12414 ckchs_transaction.path = appctx->ctx.ssl.path;
12415 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
12416 } else {
12417 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
12418
12419 }
12420
12421 /* free the previous ckchs if there was a transaction */
William Lallemandba1c33f2020-04-08 17:55:45 +020012422 ckch_store_free(ckchs_transaction.new_ckchs);
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012423
12424 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
12425
12426
William Lallemand8f840d72019-10-23 10:53:05 +020012427 /* creates the SNI ctxs later in the IO handler */
William Lallemand150bfa82019-09-19 17:12:49 +020012428
William Lallemand8f840d72019-10-23 10:53:05 +020012429end:
12430 free_trash_chunk(buf);
William Lallemand150bfa82019-09-19 17:12:49 +020012431
Emeric Brunf69ed1d2019-10-17 11:56:56 +020012432 if (errcode & ERR_CODE) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012433
William Lallemandba1c33f2020-04-08 17:55:45 +020012434 ckch_store_free(appctx->ctx.ssl.new_ckchs);
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012435 appctx->ctx.ssl.new_ckchs = NULL;
12436
12437 appctx->ctx.ssl.old_ckchs = NULL;
12438
12439 free(appctx->ctx.ssl.path);
12440 appctx->ctx.ssl.path = NULL;
12441
12442 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand44b35322019-10-17 16:28:40 +020012443 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
William Lallemand430413e2019-10-28 14:30:47 +010012444 } else {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012445
12446 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12447 return cli_dynmsg(appctx, LOG_NOTICE, err);
William Lallemand430413e2019-10-28 14:30:47 +010012448 }
William Lallemand8f840d72019-10-23 10:53:05 +020012449 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
William Lallemand150bfa82019-09-19 17:12:49 +020012450}
12451
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012452/* parsing function of 'abort ssl cert' */
12453static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
12454{
12455 char *err = NULL;
12456
William Lallemand230662a2019-12-03 13:32:54 +010012457 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12458 return 1;
12459
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012460 if (!*args[3])
12461 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
12462
12463 /* The operations on the CKCH architecture are locked so we can
12464 * manipulate ckch_store and ckch_inst */
12465 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12466 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
12467
12468 if (!ckchs_transaction.path) {
12469 memprintf(&err, "No ongoing transaction!\n");
12470 goto error;
12471 }
12472
12473 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
12474 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
12475 goto error;
12476 }
12477
12478 /* Only free the ckchs there, because the SNI and instances were not generated yet */
William Lallemandba1c33f2020-04-08 17:55:45 +020012479 ckch_store_free(ckchs_transaction.new_ckchs);
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012480 ckchs_transaction.new_ckchs = NULL;
William Lallemandba1c33f2020-04-08 17:55:45 +020012481 ckch_store_free(ckchs_transaction.old_ckchs);
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012482 ckchs_transaction.old_ckchs = NULL;
12483 free(ckchs_transaction.path);
12484 ckchs_transaction.path = NULL;
12485
12486 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12487
12488 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
12489 return cli_dynmsg(appctx, LOG_NOTICE, err);
12490
12491error:
12492 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12493
12494 return cli_dynerr(appctx, err);
12495}
12496
William Lallemandea987ed2020-03-19 16:48:33 +010012497/* parsing function of 'new ssl cert' */
12498static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
12499{
12500 struct ckch_store *store;
12501 char *err = NULL;
12502 char *path;
12503
12504 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12505 return 1;
12506
12507 if (!*args[3])
12508 return cli_err(appctx, "'new ssl cert' expects a filename\n");
12509
12510 path = args[3];
12511
12512 /* The operations on the CKCH architecture are locked so we can
12513 * manipulate ckch_store and ckch_inst */
12514 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12515 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
12516
12517 store = ckchs_lookup(path);
12518 if (store != NULL) {
12519 memprintf(&err, "Certificate '%s' already exists!\n", path);
12520 store = NULL; /* we don't want to free it */
12521 goto error;
12522 }
William Lallemand8a874e42020-04-09 10:32:53 +020012523 /* we won't support multi-certificate bundle here */
12524 store = ckch_store_new(path, 1);
William Lallemandea987ed2020-03-19 16:48:33 +010012525 if (!store) {
12526 memprintf(&err, "unable to allocate memory.\n");
12527 goto error;
12528 }
William Lallemandea987ed2020-03-19 16:48:33 +010012529
12530 /* insert into the ckchs tree */
William Lallemandea987ed2020-03-19 16:48:33 +010012531 ebst_insert(&ckchs_tree, &store->node);
12532 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
12533
12534 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12535 return cli_dynmsg(appctx, LOG_NOTICE, err);
12536error:
12537 free(store);
12538 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12539 return cli_dynerr(appctx, err);
12540}
12541
William Lallemand419e6342020-04-08 12:05:39 +020012542/* parsing function of 'del ssl cert' */
12543static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
12544{
12545 struct ckch_store *store;
12546 char *err = NULL;
12547 char *filename;
12548
12549 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
12550 return 1;
12551
12552 if (!*args[3])
12553 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
12554
12555 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
12556 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
12557
12558 filename = args[3];
12559
12560 store = ckchs_lookup(filename);
12561 if (store == NULL) {
12562 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
12563 goto error;
12564 }
12565 if (!LIST_ISEMPTY(&store->ckch_inst)) {
12566 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
12567 goto error;
12568 }
12569
12570 ebmb_delete(&store->node);
William Lallemandba1c33f2020-04-08 17:55:45 +020012571 ckch_store_free(store);
William Lallemand419e6342020-04-08 12:05:39 +020012572
12573 memprintf(&err, "Certificate '%s' deleted!\n", filename);
12574
12575 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12576 return cli_dynmsg(appctx, LOG_NOTICE, err);
12577
12578error:
12579 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
12580 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
12581 return cli_dynerr(appctx, err);
12582}
12583
12584
12585
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020012586static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020012587{
12588#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
12589 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +020012590 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020012591
12592 if (!payload)
12593 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +020012594
12595 /* Expect one parameter: the new response in base64 encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020012596 if (!*payload)
12597 return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n");
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020012598
12599 /* remove \r and \n from the payload */
12600 for (i = 0, j = 0; payload[i]; i++) {
12601 if (payload[i] == '\r' || payload[i] == '\n')
12602 continue;
12603 payload[j++] = payload[i];
12604 }
12605 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +020012606
Willy Tarreau1c913e42018-08-22 05:26:57 +020012607 ret = base64dec(payload, j, trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020012608 if (ret < 0)
12609 return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020012610
Willy Tarreau1c913e42018-08-22 05:26:57 +020012611 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +020012612 if (ssl_sock_update_ocsp_response(&trash, &err)) {
Willy Tarreau9d008692019-08-09 11:21:01 +020012613 if (err)
12614 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
12615 else
12616 return cli_err(appctx, "Failed to update OCSP response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020012617 }
Willy Tarreau9d008692019-08-09 11:21:01 +020012618
12619 return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020012620#else
Willy Tarreau9d008692019-08-09 11:21:01 +020012621 return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n");
William Lallemand32af2032016-10-29 18:09:35 +020012622#endif
12623
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010012624}
12625
Elliot Otchet71f82972020-01-15 08:12:14 -050012626/* Argument validation functions */
12627
12628/* This function is used to validate the arguments passed to any "x_dn" ssl
12629 * keywords. These keywords support specifying a third parameter that must be
12630 * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK.
12631 */
12632int val_dnfmt(struct arg *arg, char **err_msg)
12633{
12634 if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) {
12635 memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument.");
12636 return 0;
12637 }
12638 return 1;
12639}
12640
William Lallemand32af2032016-10-29 18:09:35 +020012641/* register cli keywords */
12642static struct cli_kw_list cli_kws = {{ },{
12643#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
12644 { { "show", "tls-keys", NULL }, "show tls-keys [id|*]: show tls keys references or dump tls ticket keys when id specified", cli_parse_show_tlskeys, NULL },
Lukas Tribusf4bbc432017-10-24 12:26:31 +020012645 { { "set", "ssl", "tls-key", NULL }, "set ssl tls-key [id|keyfile] <tlskey>: set the next TLS key for the <id> or <keyfile> listener to <tlskey>", cli_parse_set_tlskeys, NULL },
William Lallemand32af2032016-10-29 18:09:35 +020012646#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010012647 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemandea987ed2020-03-19 16:48:33 +010012648 { { "new", "ssl", "cert", NULL }, "new ssl cert <certfile> : create a new certificate file to be used in a crt-list or a directory", cli_parse_new_cert, NULL, NULL },
William Lallemandbc6ca7c2019-10-29 23:48:19 +010012649 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
12650 { { "commit", "ssl", "cert", NULL }, "commit ssl cert <certfile> : commit a certificate file", cli_parse_commit_cert, cli_io_handler_commit_cert, cli_release_commit_cert },
William Lallemand0bc9c8a2019-11-19 15:51:51 +010012651 { { "abort", "ssl", "cert", NULL }, "abort ssl cert <certfile> : abort a transaction for a certificate file", cli_parse_abort_cert, NULL, NULL },
William Lallemand419e6342020-04-08 12:05:39 +020012652 { { "del", "ssl", "cert", NULL }, "del ssl cert <certfile> : delete an unused certificate file", cli_parse_del_cert, NULL, NULL },
William Lallemandd4f946c2019-12-05 10:26:40 +010012653 { { "show", "ssl", "cert", NULL }, "show ssl cert [<certfile>] : display the SSL certificates used in memory, or the details of a <certfile>", cli_parse_show_cert, cli_io_handler_show_cert, cli_release_show_cert },
William Lallemande67c80b2020-03-25 14:42:37 +010012654 { { "add", "ssl", "crt-list", NULL }, "add ssl crt-list <filename> <certfile> [options] : add a line <certfile> to a crt-list <filename>", cli_parse_add_crtlist, cli_io_handler_add_crtlist, cli_release_add_crtlist },
William Lallemand0a9b9412020-04-06 17:43:05 +020012655 { { "del", "ssl", "crt-list", NULL }, "del ssl crt-list <filename> <certfile[:line]> : delete a line <certfile> in a crt-list <filename>", cli_parse_del_crtlist, NULL, NULL },
William Lallemandc69f02d2020-04-06 19:07:03 +020012656 { { "show", "ssl", "crt-list", NULL }, "show ssl crt-list [-n] [<filename>] : show the list of crt-lists or the content of a crt-list <filename>", cli_parse_dump_crtlist, cli_io_handler_dump_crtlist, NULL },
William Lallemand32af2032016-10-29 18:09:35 +020012657 { { NULL }, NULL, NULL, NULL }
12658}};
12659
Willy Tarreau0108d902018-11-25 19:14:37 +010012660INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +020012661
Willy Tarreau7875d092012-09-10 08:20:03 +020012662/* Note: must not be declared <const> as its list will be overwritten.
12663 * Please take care of keeping this list alphabetically sorted.
12664 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012665static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +020012666 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012667 { "ssl_bc_alg_keysize", smp_fetch_ssl_fc_alg_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010012668#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +010012669 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010012670#endif
Emeric Brun645ae792014-04-30 14:21:06 +020012671 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010012672#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
12673 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
12674#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +010012675 { "ssl_bc_is_resumed", smp_fetch_ssl_fc_is_resumed, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Emeric Brun645ae792014-04-30 14:21:06 +020012676 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +020012677 { "ssl_bc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012678 { "ssl_bc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV },
Willy Tarreau9a1ab082019-05-09 13:26:41 +020012679#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +020012680 { "ssl_bc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmer41966772018-04-28 19:15:48 -040012681#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012682#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040012683 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
12684 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -040012685 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
12686#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012687 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
12688 { "ssl_c_ca_err_depth", smp_fetch_ssl_c_ca_err_depth, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +010012689 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012690 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050012691 { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +020012692 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12693 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12694 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12695 { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050012696 { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +020012697 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12698 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012699 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012700 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
12701 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +010012702 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050012703 { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +020012704 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12705 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12706 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12707 { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050012708 { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG3(0,STR,SINT,STR),val_dnfmt, SMP_T_STR, SMP_USE_L5CLI },
Emeric Brunba841a12014-04-30 17:05:08 +020012709 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +020012710 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012711 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012712 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012713 { "ssl_fc_alg_keysize", smp_fetch_ssl_fc_alg_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012714 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012715 { "ssl_fc_has_crt", smp_fetch_ssl_fc_has_crt, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Olivier Houchardccaa7de2017-10-02 11:51:03 +020012716 { "ssl_fc_has_early", smp_fetch_ssl_fc_has_early, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010012717 { "ssl_fc_has_sni", smp_fetch_ssl_fc_has_sni, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Nenad Merdanovic26ea8222015-05-18 02:28:57 +020012718 { "ssl_fc_is_resumed", smp_fetch_ssl_fc_is_resumed, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Bernard Spil13c53f82018-02-15 13:34:58 +010012719#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012720 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +020012721#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +010012722#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012723 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +020012724#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012725 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +020012726#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +020012727 { "ssl_fc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -040012728#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020012729 { "ssl_fc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +020012730#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012731 { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -040012732#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012733#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040012734 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12735 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -040012736 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12737#endif
Patrick Hemmer41966772018-04-28 19:15:48 -040012738#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010012739 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -040012740#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010012741 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12742 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
12743 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
12744 { "ssl_fc_cipherlist_xxh", smp_fetch_ssl_fc_cl_xxh64, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau7875d092012-09-10 08:20:03 +020012745 { NULL, NULL, 0, 0, 0 },
12746}};
12747
Willy Tarreau0108d902018-11-25 19:14:37 +010012748INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
12749
Willy Tarreau7875d092012-09-10 08:20:03 +020012750/* Note: must not be declared <const> as its list will be overwritten.
12751 * Please take care of keeping this list alphabetically sorted.
12752 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020012753static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010012754 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
12755 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +010012756 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +020012757}};
12758
Willy Tarreau0108d902018-11-25 19:14:37 +010012759INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
12760
Willy Tarreau79eeafa2012-09-14 07:53:05 +020012761/* Note: must not be declared <const> as its list will be overwritten.
12762 * Please take care of keeping this list alphabetically sorted, doing so helps
12763 * all code contributors.
12764 * Optional keywords are also declared with a NULL ->parse() function so that
12765 * the config parser can report an appropriate error when a known keyword was
12766 * not enabled.
12767 */
William Lallemand557823f2020-04-01 17:42:47 +020012768
12769/* the <ssl_bind_kws> keywords are used for crt-list parsing, they *MUST* be safe
12770 * with their proxy argument NULL and must only fill the ssl_bind_conf */
William Lallemandd4632b22020-05-12 14:46:24 +020012771struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +020012772 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012773 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +010012774 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */
12775 { "ca-verify-file", ssl_bind_parse_ca_verify_file, 1 }, /* set CAverify file to process verify on client cert */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012776 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012777#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012778 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
12779#endif
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012780 { "crl-file", ssl_bind_parse_crl_file, 1 }, /* set certificate revocation list file use on client cert verify */
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +010012781 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012782 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020012783 { "no-ca-names", ssl_bind_parse_no_ca_names, 0 }, /* do not send ca names to clients (ca_file related) */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012784 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +020012785 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
12786 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010012787 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
12788 { NULL, NULL, 0 },
12789};
12790
Willy Tarreau0108d902018-11-25 19:14:37 +010012791/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
12792
Willy Tarreau51fb7652012-09-18 18:24:39 +020012793static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +020012794 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012795 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +010012796 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */
12797 { "ca-verify-file", bind_parse_ca_verify_file, 1 }, /* set CAverify file to process verify on client cert */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012798 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
12799 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
12800 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
12801 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012802#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012803 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
12804#endif
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012805 { "crl-file", bind_parse_crl_file, 1 }, /* set certificate revocation list file use on client cert verify */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012806 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050012807 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth == 0 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012808 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
12809 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
12810 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
12811 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
12812 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
12813 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
12814 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020012815 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012816 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020012817 { "no-ca-names", bind_parse_no_ca_names, 0 }, /* do not send ca names to clients (ca_file related) */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012818 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
12819 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
12820 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
12821 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020012822 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012823 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
12824 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012825 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
12826 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020012827 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
12828 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
12829 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
12830 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
12831 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +020012832 { NULL, NULL, 0 },
12833}};
Emeric Brun46591952012-05-18 15:47:34 +020012834
Willy Tarreau0108d902018-11-25 19:14:37 +010012835INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
12836
Willy Tarreau92faadf2012-10-10 23:04:25 +020012837/* Note: must not be declared <const> as its list will be overwritten.
12838 * Please take care of keeping this list alphabetically sorted, doing so helps
12839 * all code contributors.
12840 * Optional keywords are also declared with a NULL ->parse() function so that
12841 * the config parser can report an appropriate error when a known keyword was
12842 * not enabled.
12843 */
12844static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +010012845 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +010012846 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012847 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +010012848 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +020012849 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012850 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
12851 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012852#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012853 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
12854#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012855 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
12856 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
12857 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
12858 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
12859 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
12860 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
12861 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
12862 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
12863 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
12864 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
12865 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
12866 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
12867 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
12868 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
12869 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
12870 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
12871 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
12872 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +010012873 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020012874 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
12875 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
12876 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
12877 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
12878 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
12879 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
12880 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
12881 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
12882 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
12883 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +020012884 { NULL, NULL, 0, 0 },
12885}};
12886
Willy Tarreau0108d902018-11-25 19:14:37 +010012887INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
12888
Emeric Brun2c86cbf2014-10-30 15:56:50 +010012889static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010012890 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
12891 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010012892 { CFG_GLOBAL, "issuers-chain-path", ssl_load_global_issuers_from_path },
Willy Tarreau0bea58d2016-12-21 23:17:25 +010012893 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010012894 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
12895 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +010012896#ifndef OPENSSL_NO_DH
12897 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
12898#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000012899 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020012900#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000012901 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020012902#endif
Emmanuel Hocdetc3b7e742020-04-22 11:06:19 +020012903 { CFG_GLOBAL, "ssl-skip-self-issued-ca", ssl_parse_skip_self_issued_ca },
Willy Tarreau9ceda382016-12-21 23:13:03 +010012904 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
12905#ifndef OPENSSL_NO_DH
12906 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
12907#endif
12908 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
12909 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
12910 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
12911 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010012912 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +010012913 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
12914 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Jerome Magninb203ff62020-04-03 15:28:22 +020012915#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
12916 { CFG_GLOBAL, "ssl-default-bind-curves", ssl_parse_global_curves },
12917#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020012918#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020012919 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
12920 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
12921#endif
William Lallemand3af48e72020-02-03 17:15:52 +010012922 { CFG_GLOBAL, "ssl-load-extra-files", ssl_parse_global_extra_files },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010012923 { 0, NULL, NULL },
12924}};
12925
Willy Tarreau0108d902018-11-25 19:14:37 +010012926INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
12927
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020012928/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +010012929static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +020012930 .snd_buf = ssl_sock_from_buf,
12931 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +010012932 .subscribe = ssl_subscribe,
12933 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +020012934 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +020012935 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +020012936 .rcv_pipe = NULL,
12937 .snd_pipe = NULL,
12938 .shutr = NULL,
12939 .shutw = ssl_sock_shutw,
12940 .close = ssl_sock_close,
12941 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +010012942 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +010012943 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +010012944 .prepare_srv = ssl_sock_prepare_srv_ctx,
12945 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +010012946 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +010012947 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +020012948};
12949
Olivier Houchardccaa7de2017-10-02 11:51:03 +020012950enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
12951 struct session *sess, struct stream *s, int flags)
12952{
12953 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +010012954 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020012955
12956 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +010012957 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +020012958
Olivier Houchard6fa63d92017-11-27 18:41:32 +010012959 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +020012960 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +010012961 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020012962 s->req.flags |= CF_READ_NULL;
12963 return ACT_RET_YIELD;
12964 }
12965 }
12966 return (ACT_RET_CONT);
12967}
12968
12969static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
12970{
12971 rule->action_ptr = ssl_action_wait_for_hs;
12972
12973 return ACT_RET_PRS_OK;
12974}
12975
12976static struct action_kw_list http_req_actions = {ILH, {
12977 { "wait-for-handshake", ssl_parse_wait_for_hs },
12978 { /* END */ }
12979}};
12980
Willy Tarreau0108d902018-11-25 19:14:37 +010012981INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
12982
Willy Tarreau5db847a2019-05-09 14:13:35 +020012983#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010012984
12985static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
12986{
12987 if (ptr) {
12988 chunk_destroy(ptr);
12989 free(ptr);
12990 }
12991}
12992
12993#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010012994static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
12995{
Willy Tarreaubafbe012017-11-24 17:34:44 +010012996 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010012997}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010012998
Emeric Brun46591952012-05-18 15:47:34 +020012999__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +020013000static void __ssl_sock_init(void)
13001{
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013002#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020013003 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013004 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013005#endif
Emeric Brun46591952012-05-18 15:47:34 +020013006
Willy Tarreauef934602016-12-22 23:12:01 +010013007 if (global_ssl.listen_default_ciphers)
13008 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
13009 if (global_ssl.connect_default_ciphers)
13010 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020013011#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020013012 if (global_ssl.listen_default_ciphersuites)
13013 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
13014 if (global_ssl.connect_default_ciphersuites)
13015 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
13016#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +010013017
Willy Tarreau13e14102016-12-22 20:25:26 +010013018 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +020013019#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +020013020 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -080013021#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013022#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020013023 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013024 n = sk_SSL_COMP_num(cm);
13025 while (n--) {
13026 (void) sk_SSL_COMP_pop(cm);
13027 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +050013028#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050013029
Willy Tarreau5db847a2019-05-09 14:13:35 +020013030#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +020013031 ssl_locking_init();
13032#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +020013033#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010013034 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
13035#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +020013036 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +020013037 ssl_capture_ptr_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_capture_free_func);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013038#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000013039 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000013040 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013041#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +010013042#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
13043 hap_register_post_check(tlskeys_finalize_config);
13044#endif
Willy Tarreau80713382018-11-26 10:19:54 +010013045
13046 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
13047 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
13048
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010013049 hap_register_post_deinit(ssl_free_global_issuers);
13050
Willy Tarreau80713382018-11-26 10:19:54 +010013051#ifndef OPENSSL_NO_DH
13052 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
13053 hap_register_post_deinit(ssl_free_dh);
13054#endif
13055#ifndef OPENSSL_NO_ENGINE
13056 hap_register_post_deinit(ssl_free_engines);
13057#endif
13058 /* Load SSL string for the verbose & debug mode. */
13059 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +020013060 ha_meth = BIO_meth_new(0x666, "ha methods");
13061 BIO_meth_set_write(ha_meth, ha_ssl_write);
13062 BIO_meth_set_read(ha_meth, ha_ssl_read);
13063 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
13064 BIO_meth_set_create(ha_meth, ha_ssl_new);
13065 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
13066 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
13067 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
William Lallemand150bfa82019-09-19 17:12:49 +020013068
13069 HA_SPIN_INIT(&ckch_lock);
Dragan Dosen1e7ed042020-05-08 18:30:00 +020013070
Dragan Dosen9ac98092020-05-11 15:51:45 +020013071 /* Try to register dedicated SSL/TLS protocol message callbacks for
13072 * heartbleed attack (CVE-2014-0160) and clienthello.
13073 */
13074 hap_register_post_check(ssl_sock_register_msg_callbacks);
13075
Dragan Dosen1e7ed042020-05-08 18:30:00 +020013076 /* Try to free all callbacks that were registered by using
13077 * ssl_sock_register_msg_callback().
13078 */
13079 hap_register_post_deinit(ssl_sock_unregister_msg_callbacks);
Willy Tarreau80713382018-11-26 10:19:54 +010013080}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010013081
Willy Tarreau80713382018-11-26 10:19:54 +010013082/* Compute and register the version string */
13083static void ssl_register_build_options()
13084{
13085 char *ptr = NULL;
13086 int i;
13087
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013088 memprintf(&ptr, "Built with OpenSSL version : "
13089#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010013090 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013091#else /* OPENSSL_IS_BORINGSSL */
13092 OPENSSL_VERSION_TEXT
13093 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080013094 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020013095 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013096#endif
13097 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020013098#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013099 "no (library version too old)"
13100#elif defined(OPENSSL_NO_TLSEXT)
13101 "no (disabled via OPENSSL_NO_TLSEXT)"
13102#else
13103 "yes"
13104#endif
13105 "", ptr);
13106
13107 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
13108#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
13109 "yes"
13110#else
13111#ifdef OPENSSL_NO_TLSEXT
13112 "no (because of OPENSSL_NO_TLSEXT)"
13113#else
13114 "no (version might be too old, 0.9.8f min needed)"
13115#endif
13116#endif
13117 "", ptr);
13118
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020013119 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
13120 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
13121 if (methodVersions[i].option)
13122 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010013123
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013124 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010013125}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010013126
Willy Tarreau80713382018-11-26 10:19:54 +010013127INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020013128
Emeric Brun46591952012-05-18 15:47:34 +020013129
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013130#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000013131void ssl_free_engines(void) {
13132 struct ssl_engine_list *wl, *wlb;
13133 /* free up engine list */
13134 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
13135 ENGINE_finish(wl->e);
13136 ENGINE_free(wl->e);
13137 LIST_DEL(&wl->list);
13138 free(wl);
13139 }
13140}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020013141#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020013142
Remi Gacogned3a23c32015-05-28 16:39:47 +020013143#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000013144void ssl_free_dh(void) {
13145 if (local_dh_1024) {
13146 DH_free(local_dh_1024);
13147 local_dh_1024 = NULL;
13148 }
13149 if (local_dh_2048) {
13150 DH_free(local_dh_2048);
13151 local_dh_2048 = NULL;
13152 }
13153 if (local_dh_4096) {
13154 DH_free(local_dh_4096);
13155 local_dh_4096 = NULL;
13156 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020013157 if (global_dh) {
13158 DH_free(global_dh);
13159 global_dh = NULL;
13160 }
Grant Zhang872f9c22017-01-21 01:10:18 +000013161}
13162#endif
13163
13164__attribute__((destructor))
13165static void __ssl_sock_deinit(void)
13166{
13167#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020013168 if (ssl_ctx_lru_tree) {
13169 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010013170 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020013171 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020013172#endif
13173
Willy Tarreau5db847a2019-05-09 14:13:35 +020013174#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020013175 ERR_remove_state(0);
13176 ERR_free_strings();
13177
13178 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080013179#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020013180
Willy Tarreau5db847a2019-05-09 14:13:35 +020013181#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020013182 CRYPTO_cleanup_all_ex_data();
13183#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020013184 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020013185}
13186
13187
Emeric Brun46591952012-05-18 15:47:34 +020013188/*
13189 * Local variables:
13190 * c-indent-level: 8
13191 * c-basic-offset: 8
13192 * End:
13193 */