blob: 43ba750aef73e7ba0a3e5001ddb3fc617d7c3edb [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>
William Lallemand6e9556b2020-05-12 17:52:44 +020088#include <proto/ssl_crtlist.h>
Emeric Brun46591952012-05-18 15:47:34 +020089#include <proto/ssl_sock.h>
Willy Tarreau9ad7bd42015-04-03 19:19:59 +020090#include <proto/stream.h>
Emeric Brun46591952012-05-18 15:47:34 +020091#include <proto/task.h>
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010092#include <proto/vars.h>
Emeric Brun46591952012-05-18 15:47:34 +020093
Willy Tarreau9356dac2019-05-10 09:22:53 +020094/* ***** READ THIS before adding code here! *****
95 *
96 * Due to API incompatibilities between multiple OpenSSL versions and their
97 * derivatives, it's often tempting to add macros to (re-)define certain
98 * symbols. Please do not do this here, and do it in common/openssl-compat.h
99 * exclusively so that the whole code consistently uses the same macros.
100 *
101 * Whenever possible if a macro is missing in certain versions, it's better
102 * to conditionally define it in openssl-compat.h than using lots of ifdefs.
103 */
104
Willy Tarreau71b734c2014-01-28 15:19:44 +0100105int sslconns = 0;
106int totalsslconns = 0;
Willy Tarreaud9f5cca2016-12-22 21:08:52 +0100107static struct xprt_ops ssl_sock;
Emeric Brunece0c332017-12-06 13:51:49 +0100108int nb_engines = 0;
Emeric Brune1f38db2012-09-03 20:36:47 +0200109
William Lallemande0f3fd52020-02-25 14:53:06 +0100110static struct eb_root cert_issuer_tree = EB_ROOT; /* issuers tree from "issuers-chain-path" */
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +0100111static struct issuer_chain* ssl_get0_issuer_chain(X509 *cert);
William Lallemande0f3fd52020-02-25 14:53:06 +0100112
William Lallemand7fd8b452020-05-07 15:20:43 +0200113struct global_ssl global_ssl = {
Willy Tarreauef934602016-12-22 23:12:01 +0100114#ifdef LISTEN_DEFAULT_CIPHERS
115 .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS,
116#endif
117#ifdef CONNECT_DEFAULT_CIPHERS
118 .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS,
119#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +0200120#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +0200121#ifdef LISTEN_DEFAULT_CIPHERSUITES
122 .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES,
123#endif
124#ifdef CONNECT_DEFAULT_CIPHERSUITES
125 .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES,
126#endif
127#endif
Willy Tarreauef934602016-12-22 23:12:01 +0100128 .listen_default_ssloptions = BC_SSL_O_NONE,
129 .connect_default_ssloptions = SRV_SSL_O_NONE,
130
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +0200131 .listen_default_sslmethods.flags = MC_SSL_O_ALL,
132 .listen_default_sslmethods.min = CONF_TLSV_NONE,
133 .listen_default_sslmethods.max = CONF_TLSV_NONE,
134 .connect_default_sslmethods.flags = MC_SSL_O_ALL,
135 .connect_default_sslmethods.min = CONF_TLSV_NONE,
136 .connect_default_sslmethods.max = CONF_TLSV_NONE,
137
Willy Tarreauef934602016-12-22 23:12:01 +0100138#ifdef DEFAULT_SSL_MAX_RECORD
139 .max_record = DEFAULT_SSL_MAX_RECORD,
140#endif
141 .default_dh_param = SSL_DEFAULT_DH_PARAM,
142 .ctx_cache = DEFAULT_SSL_CTX_CACHE,
Thierry FOURNIER5bf77322017-02-25 12:45:22 +0100143 .capture_cipherlist = 0,
William Lallemand3af48e72020-02-03 17:15:52 +0100144 .extra_files = SSL_GF_ALL,
Willy Tarreauef934602016-12-22 23:12:01 +0100145};
146
Olivier Houcharda8955d52019-04-07 22:00:38 +0200147static BIO_METHOD *ha_meth;
148
Olivier Houchard66ab4982019-02-26 18:37:15 +0100149struct ssl_sock_ctx {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200150 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100151 SSL *ssl;
Olivier Houcharda8955d52019-04-07 22:00:38 +0200152 BIO *bio;
Olivier Houchard5149b592019-05-23 17:47:36 +0200153 const struct xprt_ops *xprt;
Olivier Houchard66ab4982019-02-26 18:37:15 +0100154 void *xprt_ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +0200155 struct wait_event wait_event;
Willy Tarreau113d52b2020-01-10 09:20:26 +0100156 struct wait_event *subs;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100157 int xprt_st; /* transport layer state, initialized to zero */
Olivier Houchard54907bb2019-12-19 15:02:39 +0100158 struct buffer early_buf; /* buffer to store the early data received */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +0100159 int sent_early_data; /* Amount of early data we sent so far */
160
Olivier Houchard66ab4982019-02-26 18:37:15 +0100161};
162
163DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx));
164
Olivier Houchardea8dd942019-05-20 14:02:16 +0200165static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short);
Olivier Houchard000694c2019-05-23 14:45:12 +0200166static int ssl_sock_handshake(struct connection *conn, unsigned int flag);
Olivier Houchardea8dd942019-05-20 14:02:16 +0200167
Olivier Houcharda8955d52019-04-07 22:00:38 +0200168/* Methods to implement OpenSSL BIO */
169static int ha_ssl_write(BIO *h, const char *buf, int num)
170{
171 struct buffer tmpbuf;
172 struct ssl_sock_ctx *ctx;
173 int ret;
174
175 ctx = BIO_get_data(h);
176 tmpbuf.size = num;
177 tmpbuf.area = (void *)(uintptr_t)buf;
178 tmpbuf.data = num;
179 tmpbuf.head = 0;
180 ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200181 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200182 BIO_set_retry_write(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200183 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200184 } else if (ret == 0)
185 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200186 return ret;
187}
188
189static int ha_ssl_gets(BIO *h, char *buf, int size)
190{
191
192 return 0;
193}
194
195static int ha_ssl_puts(BIO *h, const char *str)
196{
197
198 return ha_ssl_write(h, str, strlen(str));
199}
200
201static int ha_ssl_read(BIO *h, char *buf, int size)
202{
203 struct buffer tmpbuf;
204 struct ssl_sock_ctx *ctx;
205 int ret;
206
207 ctx = BIO_get_data(h);
208 tmpbuf.size = size;
209 tmpbuf.area = buf;
210 tmpbuf.data = 0;
211 tmpbuf.head = 0;
212 ret = ctx->xprt->rcv_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, size, 0);
Olivier Houchardb51937e2019-05-01 17:24:36 +0200213 if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))) {
Olivier Houcharda8955d52019-04-07 22:00:38 +0200214 BIO_set_retry_read(h);
Olivier Houcharda28454e2019-04-24 12:04:36 +0200215 ret = -1;
Olivier Houchardb51937e2019-05-01 17:24:36 +0200216 } else if (ret == 0)
217 BIO_clear_retry_flags(h);
Olivier Houcharda8955d52019-04-07 22:00:38 +0200218
219 return ret;
220}
221
222static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2)
223{
224 int ret = 0;
225 switch (cmd) {
226 case BIO_CTRL_DUP:
227 case BIO_CTRL_FLUSH:
228 ret = 1;
229 break;
230 }
231 return ret;
232}
233
234static int ha_ssl_new(BIO *h)
235{
236 BIO_set_init(h, 1);
237 BIO_set_data(h, NULL);
238 BIO_clear_flags(h, ~0);
239 return 1;
240}
241
242static int ha_ssl_free(BIO *data)
243{
244
245 return 1;
246}
247
248
Willy Tarreau5db847a2019-05-09 14:13:35 +0200249#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100250
Emeric Brun821bb9b2017-06-15 16:37:39 +0200251static HA_RWLOCK_T *ssl_rwlocks;
252
253
254unsigned long ssl_id_function(void)
255{
256 return (unsigned long)tid;
257}
258
259void ssl_locking_function(int mode, int n, const char * file, int line)
260{
261 if (mode & CRYPTO_LOCK) {
262 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100263 HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200264 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100265 HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200266 }
267 else {
268 if (mode & CRYPTO_READ)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100269 HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200270 else
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100271 HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200272 }
273}
274
275static int ssl_locking_init(void)
276{
277 int i;
278
279 ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks());
280 if (!ssl_rwlocks)
281 return -1;
282
283 for (i = 0 ; i < CRYPTO_num_locks() ; i++)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100284 HA_RWLOCK_INIT(&ssl_rwlocks[i]);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200285
286 CRYPTO_set_id_callback(ssl_id_function);
287 CRYPTO_set_locking_callback(ssl_locking_function);
288
289 return 0;
290}
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100291
Emeric Brun821bb9b2017-06-15 16:37:39 +0200292#endif
293
William Lallemand150bfa82019-09-19 17:12:49 +0200294__decl_hathreads(HA_SPINLOCK_T ckch_lock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200295
William Lallemandbc6ca7c2019-10-29 23:48:19 +0100296/* Uncommitted CKCH transaction */
297
298static struct {
299 struct ckch_store *new_ckchs;
300 struct ckch_store *old_ckchs;
301 char *path;
302} ckchs_transaction;
303
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200304/*
Emmanuel Hocdetb270e812019-11-21 19:09:31 +0100305 * deduplicate cafile (and crlfile)
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200306 */
307struct cafile_entry {
308 X509_STORE *ca_store;
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200309 STACK_OF(X509_NAME) *ca_list;
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200310 struct ebmb_node node;
311 char path[0];
312};
313
314static struct eb_root cafile_tree = EB_ROOT_UNIQUE;
315
316static X509_STORE* ssl_store_get0_locations_file(char *path)
317{
318 struct ebmb_node *eb;
319
320 eb = ebst_lookup(&cafile_tree, path);
321 if (eb) {
322 struct cafile_entry *ca_e;
323 ca_e = ebmb_entry(eb, struct cafile_entry, node);
324 return ca_e->ca_store;
325 }
326 return NULL;
327}
328
329static int ssl_store_load_locations_file(char *path)
330{
331 if (ssl_store_get0_locations_file(path) == NULL) {
332 struct cafile_entry *ca_e;
333 X509_STORE *store = X509_STORE_new();
334 if (X509_STORE_load_locations(store, path, NULL)) {
335 int pathlen;
336 pathlen = strlen(path);
337 ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1);
338 if (ca_e) {
339 memcpy(ca_e->path, path, pathlen + 1);
340 ca_e->ca_store = store;
341 ebst_insert(&cafile_tree, &ca_e->node);
342 return 1;
343 }
344 }
345 X509_STORE_free(store);
346 return 0;
347 }
348 return 1;
349}
350
351/* mimic what X509_STORE_load_locations do with store_ctx */
352static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path)
353{
354 X509_STORE *store;
355 store = ssl_store_get0_locations_file(path);
356 if (store_ctx && store) {
357 int i;
358 X509_OBJECT *obj;
359 STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store);
360 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
361 obj = sk_X509_OBJECT_value(objs, i);
362 switch (X509_OBJECT_get_type(obj)) {
363 case X509_LU_X509:
364 X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj));
365 break;
366 case X509_LU_CRL:
367 X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj));
368 break;
369 default:
370 break;
371 }
372 }
373 return 1;
374 }
375 return 0;
376}
377
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +0500378/* SSL_CTX_load_verify_locations substitute, internally call X509_STORE_load_locations */
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +0200379static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path)
380{
381 X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx);
382 return ssl_set_cert_crl_file(store_ctx, path);
383}
384
Emmanuel Hocdet129d3282019-10-24 18:08:51 +0200385/*
386 Extract CA_list from CA_file already in tree.
387 Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility.
388 Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX.
389*/
390static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path)
391{
392 struct ebmb_node *eb;
393 struct cafile_entry *ca_e;
394
395 eb = ebst_lookup(&cafile_tree, path);
396 if (!eb)
397 return NULL;
398 ca_e = ebmb_entry(eb, struct cafile_entry, node);
399
400 if (ca_e->ca_list == NULL) {
401 int i;
402 unsigned long key;
403 struct eb_root ca_name_tree = EB_ROOT;
404 struct eb64_node *node, *back;
405 struct {
406 struct eb64_node node;
407 X509_NAME *xname;
408 } *ca_name;
409 STACK_OF(X509_OBJECT) *objs;
410 STACK_OF(X509_NAME) *skn;
411 X509 *x;
412 X509_NAME *xn;
413
414 skn = sk_X509_NAME_new_null();
415 /* take x509 from cafile_tree */
416 objs = X509_STORE_get0_objects(ca_e->ca_store);
417 for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
418 x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i));
419 if (!x)
420 continue;
421 xn = X509_get_subject_name(x);
422 if (!xn)
423 continue;
424 /* Check for duplicates. */
425 key = X509_NAME_hash(xn);
426 for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL;
427 node && ca_name == NULL;
428 node = eb64_next(node)) {
429 ca_name = container_of(node, typeof(*ca_name), node);
430 if (X509_NAME_cmp(xn, ca_name->xname) != 0)
431 ca_name = NULL;
432 }
433 /* find a duplicate */
434 if (ca_name)
435 continue;
436 ca_name = calloc(1, sizeof *ca_name);
437 xn = X509_NAME_dup(xn);
438 if (!ca_name ||
439 !xn ||
440 !sk_X509_NAME_push(skn, xn)) {
441 free(ca_name);
442 X509_NAME_free(xn);
443 sk_X509_NAME_pop_free(skn, X509_NAME_free);
444 sk_X509_NAME_free(skn);
445 skn = NULL;
446 break;
447 }
448 ca_name->node.key = key;
449 ca_name->xname = xn;
450 eb64_insert(&ca_name_tree, &ca_name->node);
451 }
452 ca_e->ca_list = skn;
453 /* remove temporary ca_name tree */
454 node = eb64_first(&ca_name_tree);
455 while (node) {
456 ca_name = container_of(node, typeof(*ca_name), node);
457 back = eb64_next(node);
458 eb64_delete(node);
459 free(ca_name);
460 node = back;
461 }
462 }
463 return ca_e->ca_list;
464}
465
Willy Tarreaubafbe012017-11-24 17:34:44 +0100466struct pool_head *pool_head_ssl_capture = NULL;
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +0100467static int ssl_capture_ptr_index = -1;
Thierry FOURNIER28962c92018-06-17 21:37:05 +0200468static int ssl_app_data_index = -1;
Willy Tarreauef934602016-12-22 23:12:01 +0100469
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +0200470#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
471struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference);
472#endif
473
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200474#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000475static unsigned int openssl_engines_initialized;
476struct list openssl_engines = LIST_HEAD_INIT(openssl_engines);
477struct ssl_engine_list {
478 struct list list;
479 ENGINE *e;
480};
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200481#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000482
Remi Gacogne8de54152014-07-15 11:36:40 +0200483#ifndef OPENSSL_NO_DH
Remi Gacogne4f902b82015-05-28 16:23:00 +0200484static int ssl_dh_ptr_index = -1;
Remi Gacogne47783ef2015-05-29 15:53:22 +0200485static DH *global_dh = NULL;
Remi Gacogne8de54152014-07-15 11:36:40 +0200486static DH *local_dh_1024 = NULL;
487static DH *local_dh_2048 = NULL;
488static DH *local_dh_4096 = NULL;
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +0100489static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen);
Remi Gacogne8de54152014-07-15 11:36:40 +0200490#endif /* OPENSSL_NO_DH */
491
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100492#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +0200493/* X509V3 Extensions that will be added on generated certificates */
494#define X509V3_EXT_SIZE 5
495static char *x509v3_ext_names[X509V3_EXT_SIZE] = {
496 "basicConstraints",
497 "nsComment",
498 "subjectKeyIdentifier",
499 "authorityKeyIdentifier",
500 "keyUsage",
501};
502static char *x509v3_ext_values[X509V3_EXT_SIZE] = {
503 "CA:FALSE",
504 "\"OpenSSL Generated Certificate\"",
505 "hash",
506 "keyid,issuer:always",
507 "nonRepudiation,digitalSignature,keyEncipherment"
508};
Christopher Faulet31af49d2015-06-09 17:29:50 +0200509/* LRU cache to store generated certificate */
510static struct lru64_head *ssl_ctx_lru_tree = NULL;
511static unsigned int ssl_ctx_lru_seed = 0;
Emeric Brun821bb9b2017-06-15 16:37:39 +0200512static unsigned int ssl_ctx_serial;
Willy Tarreau86abe442018-11-25 20:12:18 +0100513__decl_rwlock(ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +0200514
Willy Tarreauc8ad3be2015-06-17 15:48:26 +0200515#endif // SSL_CTRL_SET_TLSEXT_HOSTNAME
516
Willy Tarreau9a1ab082019-05-09 13:26:41 +0200517#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhube2774d2015-12-10 15:07:30 -0500518/* The order here matters for picking a default context,
519 * keep the most common keytype at the bottom of the list
520 */
521const char *SSL_SOCK_KEYTYPE_NAMES[] = {
522 "dsa",
523 "ecdsa",
524 "rsa"
525};
yanbzhube2774d2015-12-10 15:07:30 -0500526#endif
527
William Lallemandc3cd35f2017-11-28 11:04:43 +0100528static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */
William Lallemand4f45bb92017-10-30 20:08:51 +0100529static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */
530
Dragan Dosen9ac98092020-05-11 15:51:45 +0200531/* Dedicated callback functions for heartbeat and clienthello.
532 */
533#ifdef TLS1_RT_HEARTBEAT
534static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version,
535 int content_type, const void *buf, size_t len,
536 SSL *ssl);
537#endif
538static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version,
539 int content_type, const void *buf, size_t len,
540 SSL *ssl);
541
Dragan Dosen1e7ed042020-05-08 18:30:00 +0200542/* List head of all registered SSL/TLS protocol message callbacks. */
543struct list ssl_sock_msg_callbacks = LIST_HEAD_INIT(ssl_sock_msg_callbacks);
544
545/* Registers the function <func> in order to be called on SSL/TLS protocol
546 * message processing. It will return 0 if the function <func> is not set
547 * or if it fails to allocate memory.
548 */
549int ssl_sock_register_msg_callback(ssl_sock_msg_callback_func func)
550{
551 struct ssl_sock_msg_callback *cbk;
552
553 if (!func)
554 return 0;
555
556 cbk = calloc(1, sizeof(*cbk));
557 if (!cbk) {
558 ha_alert("out of memory in ssl_sock_register_msg_callback().\n");
559 return 0;
560 }
561
562 cbk->func = func;
563
564 LIST_ADDQ(&ssl_sock_msg_callbacks, &cbk->list);
565
566 return 1;
567}
568
Dragan Dosen9ac98092020-05-11 15:51:45 +0200569/* Used to register dedicated SSL/TLS protocol message callbacks.
570 */
571static int ssl_sock_register_msg_callbacks(void)
572{
573#ifdef TLS1_RT_HEARTBEAT
574 if (!ssl_sock_register_msg_callback(ssl_sock_parse_heartbeat))
575 return ERR_ABORT;
576#endif
577 if (global_ssl.capture_cipherlist > 0) {
578 if (!ssl_sock_register_msg_callback(ssl_sock_parse_clienthello))
579 return ERR_ABORT;
580 }
581 return 0;
582}
583
Dragan Dosen1e7ed042020-05-08 18:30:00 +0200584/* Used to free all SSL/TLS protocol message callbacks that were
585 * registered by using ssl_sock_register_msg_callback().
586 */
587static void ssl_sock_unregister_msg_callbacks(void)
588{
589 struct ssl_sock_msg_callback *cbk, *cbkback;
590
591 list_for_each_entry_safe(cbk, cbkback, &ssl_sock_msg_callbacks, list) {
592 LIST_DEL(&cbk->list);
593 free(cbk);
594 }
595}
596
Dragan Doseneb607fe2020-05-11 17:17:06 +0200597SSL *ssl_sock_get_ssl_object(struct connection *conn)
598{
599 if (!ssl_sock_is_ssl(conn))
600 return NULL;
601
602 return ((struct ssl_sock_ctx *)(conn->xprt_ctx))->ssl;
603}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100604/*
605 * This function gives the detail of the SSL error. It is used only
606 * if the debug mode and the verbose mode are activated. It dump all
607 * the SSL error until the stack was empty.
608 */
609static forceinline void ssl_sock_dump_errors(struct connection *conn)
610{
611 unsigned long ret;
612
613 if (unlikely(global.mode & MODE_DEBUG)) {
614 while(1) {
615 ret = ERR_get_error();
616 if (ret == 0)
617 return;
618 fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n",
Willy Tarreau585744b2017-08-24 14:31:19 +0200619 (unsigned short)conn->handle.fd, ret,
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +0100620 ERR_func_error_string(ret), ERR_reason_error_string(ret));
621 }
622 }
623}
624
yanbzhube2774d2015-12-10 15:07:30 -0500625
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200626#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +0000627static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms)
628{
629 int err_code = ERR_ABORT;
630 ENGINE *engine;
631 struct ssl_engine_list *el;
632
633 /* grab the structural reference to the engine */
634 engine = ENGINE_by_id(engine_id);
635 if (engine == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100636 ha_alert("ssl-engine %s: failed to get structural reference\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000637 goto fail_get;
638 }
639
640 if (!ENGINE_init(engine)) {
641 /* the engine couldn't initialise, release it */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100642 ha_alert("ssl-engine %s: failed to initialize\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000643 goto fail_init;
644 }
645
646 if (ENGINE_set_default_string(engine, def_algorithms) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100647 ha_alert("ssl-engine %s: failed on ENGINE_set_default_string\n", engine_id);
Grant Zhang872f9c22017-01-21 01:10:18 +0000648 goto fail_set_method;
649 }
650
651 el = calloc(1, sizeof(*el));
652 el->e = engine;
653 LIST_ADD(&openssl_engines, &el->list);
Emeric Brunece0c332017-12-06 13:51:49 +0100654 nb_engines++;
655 if (global_ssl.async)
656 global.ssl_used_async_engines = nb_engines;
Grant Zhang872f9c22017-01-21 01:10:18 +0000657 return 0;
658
659fail_set_method:
660 /* release the functional reference from ENGINE_init() */
661 ENGINE_finish(engine);
662
663fail_init:
664 /* release the structural reference from ENGINE_by_id() */
665 ENGINE_free(engine);
666
667fail_get:
668 return err_code;
669}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +0200670#endif
Grant Zhang872f9c22017-01-21 01:10:18 +0000671
Willy Tarreau5db847a2019-05-09 14:13:35 +0200672#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +0200673/*
674 * openssl async fd handler
675 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200676void ssl_async_fd_handler(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000677{
Olivier Houchardea8dd942019-05-20 14:02:16 +0200678 struct ssl_sock_ctx *ctx = fdtab[fd].owner;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000679
Emeric Brun3854e012017-05-17 20:42:48 +0200680 /* fd is an async enfine fd, we must stop
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000681 * to poll this fd until it is requested
682 */
Emeric Brunbbc16542017-06-02 15:54:06 +0000683 fd_stop_recv(fd);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000684 fd_cant_recv(fd);
685
686 /* crypto engine is available, let's notify the associated
687 * connection that it can pursue its processing.
688 */
Olivier Houchard03abf2d2019-05-28 10:12:02 +0200689 ssl_sock_io_cb(NULL, ctx, 0);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000690}
691
Emeric Brun3854e012017-05-17 20:42:48 +0200692/*
693 * openssl async delayed SSL_free handler
694 */
Emeric Brund0e095c2019-04-19 17:15:28 +0200695void ssl_async_fd_free(int fd)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000696{
697 SSL *ssl = fdtab[fd].owner;
Emeric Brun3854e012017-05-17 20:42:48 +0200698 OSSL_ASYNC_FD all_fd[32];
699 size_t num_all_fds = 0;
700 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000701
Emeric Brun3854e012017-05-17 20:42:48 +0200702 /* We suppose that the async job for a same SSL *
703 * are serialized. So if we are awake it is
704 * because the running job has just finished
705 * and we can remove all async fds safely
706 */
707 SSL_get_all_async_fds(ssl, NULL, &num_all_fds);
708 if (num_all_fds > 32) {
709 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
710 return;
711 }
712
713 SSL_get_all_async_fds(ssl, all_fd, &num_all_fds);
714 for (i=0 ; i < num_all_fds ; i++)
715 fd_remove(all_fd[i]);
716
717 /* Now we can safely call SSL_free, no more pending job in engines */
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000718 SSL_free(ssl);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +0100719 _HA_ATOMIC_SUB(&sslconns, 1);
720 _HA_ATOMIC_SUB(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000721}
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000722/*
Emeric Brun3854e012017-05-17 20:42:48 +0200723 * function used to manage a returned SSL_ERROR_WANT_ASYNC
724 * and enable/disable polling for async fds
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000725 */
Olivier Houchardea8dd942019-05-20 14:02:16 +0200726static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000727{
Willy Tarreaua9786b62018-01-25 07:22:13 +0100728 OSSL_ASYNC_FD add_fd[32];
Emeric Brun3854e012017-05-17 20:42:48 +0200729 OSSL_ASYNC_FD del_fd[32];
Olivier Houchardea8dd942019-05-20 14:02:16 +0200730 SSL *ssl = ctx->ssl;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000731 size_t num_add_fds = 0;
732 size_t num_del_fds = 0;
Emeric Brun3854e012017-05-17 20:42:48 +0200733 int i;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000734
735 SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL,
736 &num_del_fds);
Emeric Brun3854e012017-05-17 20:42:48 +0200737 if (num_add_fds > 32 || num_del_fds > 32) {
738 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 +0000739 return;
740 }
741
Emeric Brun3854e012017-05-17 20:42:48 +0200742 SSL_get_changed_async_fds(ssl, add_fd, &num_add_fds, del_fd, &num_del_fds);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000743
Emeric Brun3854e012017-05-17 20:42:48 +0200744 /* We remove unused fds from the fdtab */
745 for (i=0 ; i < num_del_fds ; i++)
746 fd_remove(del_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000747
Emeric Brun3854e012017-05-17 20:42:48 +0200748 /* We add new fds to the fdtab */
749 for (i=0 ; i < num_add_fds ; i++) {
Olivier Houchardea8dd942019-05-20 14:02:16 +0200750 fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000751 }
752
Emeric Brun3854e012017-05-17 20:42:48 +0200753 num_add_fds = 0;
754 SSL_get_all_async_fds(ssl, NULL, &num_add_fds);
755 if (num_add_fds > 32) {
756 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
757 return;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000758 }
Emeric Brun3854e012017-05-17 20:42:48 +0200759
760 /* We activate the polling for all known async fds */
761 SSL_get_all_async_fds(ssl, add_fd, &num_add_fds);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000762 for (i=0 ; i < num_add_fds ; i++) {
Emeric Brun3854e012017-05-17 20:42:48 +0200763 fd_want_recv(add_fd[i]);
Emeric Brunce9e01c2017-05-31 10:02:53 +0000764 /* To ensure that the fd cache won't be used
765 * We'll prefer to catch a real RD event
766 * because handling an EAGAIN on this fd will
767 * result in a context switch and also
768 * some engines uses a fd in blocking mode.
769 */
770 fd_cant_recv(add_fd[i]);
771 }
Emeric Brun3854e012017-05-17 20:42:48 +0200772
Grant Zhangfa6c7ee2017-01-14 01:42:15 +0000773}
774#endif
775
William Lallemand104a7a62019-10-14 14:14:59 +0200776#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
Emeric Brun4f3c87a2014-06-20 15:46:13 +0200777/*
778 * This function returns the number of seconds elapsed
779 * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the
780 * date presented un ASN1_GENERALIZEDTIME.
781 *
782 * In parsing error case, it returns -1.
783 */
784static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d)
785{
786 long epoch;
787 char *p, *end;
788 const unsigned short month_offset[12] = {
789 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
790 };
791 int year, month;
792
793 if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1;
794
795 p = (char *)d->data;
796 end = p + d->length;
797
798 if (end - p < 4) return -1;
799 year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0';
800 p += 4;
801 if (end - p < 2) return -1;
802 month = 10 * (p[0] - '0') + p[1] - '0';
803 if (month < 1 || month > 12) return -1;
804 /* Compute the number of seconds since 1 jan 1970 and the beginning of current month
805 We consider leap years and the current month (<marsh or not) */
806 epoch = ( ((year - 1970) * 365)
807 + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400)
808 - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400)
809 + month_offset[month-1]
810 ) * 24 * 60 * 60;
811 p += 2;
812 if (end - p < 2) return -1;
813 /* Add the number of seconds of completed days of current month */
814 epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60;
815 p += 2;
816 if (end - p < 2) return -1;
817 /* Add the completed hours of the current day */
818 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60;
819 p += 2;
820 if (end - p < 2) return -1;
821 /* Add the completed minutes of the current hour */
822 epoch += (10 * (p[0] - '0') + p[1] - '0') * 60;
823 p += 2;
824 if (p == end) return -1;
825 /* Test if there is available seconds */
826 if (p[0] < '0' || p[0] > '9')
827 goto nosec;
828 if (end - p < 2) return -1;
829 /* Add the seconds of the current minute */
830 epoch += 10 * (p[0] - '0') + p[1] - '0';
831 p += 2;
832 if (p == end) return -1;
833 /* Ignore seconds float part if present */
834 if (p[0] == '.') {
835 do {
836 if (++p == end) return -1;
837 } while (p[0] >= '0' && p[0] <= '9');
838 }
839
840nosec:
841 if (p[0] == 'Z') {
842 if (end - p != 1) return -1;
843 return epoch;
844 }
845 else if (p[0] == '+') {
846 if (end - p != 5) return -1;
847 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700848 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 +0200849 }
850 else if (p[0] == '-') {
851 if (end - p != 5) return -1;
852 /* Apply timezone offset */
Frederik Deweerdt953917a2017-10-16 07:37:31 -0700853 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 +0200854 }
855
856 return -1;
857}
858
William Lallemand104a7a62019-10-14 14:14:59 +0200859/*
860 * struct alignment works here such that the key.key is the same as key_data
861 * Do not change the placement of key_data
862 */
863struct certificate_ocsp {
864 struct ebmb_node key;
865 unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH];
866 struct buffer response;
867 long expire;
868};
869
870struct ocsp_cbk_arg {
871 int is_single;
872 int single_kt;
873 union {
874 struct certificate_ocsp *s_ocsp;
875 /*
876 * m_ocsp will have multiple entries dependent on key type
877 * Entry 0 - DSA
878 * Entry 1 - ECDSA
879 * Entry 2 - RSA
880 */
881 struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES];
882 };
883};
884
Emeric Brun1d3865b2014-06-20 15:37:32 +0200885static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200886
887/* This function starts to check if the OCSP response (in DER format) contained
888 * in chunk 'ocsp_response' is valid (else exits on error).
889 * If 'cid' is not NULL, it will be compared to the OCSP certificate ID
890 * contained in the OCSP Response and exits on error if no match.
891 * If it's a valid OCSP Response:
892 * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container
893 * pointed by 'ocsp'.
894 * If 'ocsp' is NULL, the function looks up into the OCSP response's
895 * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted
896 * from the response) and exits on error if not found. Finally, If an OCSP response is
897 * already present in the container, it will be overwritten.
898 *
899 * Note: OCSP response containing more than one OCSP Single response is not
900 * considered valid.
901 *
902 * Returns 0 on success, 1 in error case.
903 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200904static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response,
905 struct certificate_ocsp *ocsp,
906 OCSP_CERTID *cid, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +0200907{
908 OCSP_RESPONSE *resp;
909 OCSP_BASICRESP *bs = NULL;
910 OCSP_SINGLERESP *sr;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200911 OCSP_CERTID *id;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200912 unsigned char *p = (unsigned char *) ocsp_response->area;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200913 int rc , count_sr;
Emeric Brun13a6b482014-06-20 15:44:34 +0200914 ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +0200915 int reason;
916 int ret = 1;
917
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200918 resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p,
919 ocsp_response->data);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200920 if (!resp) {
921 memprintf(err, "Unable to parse OCSP response");
922 goto out;
923 }
924
925 rc = OCSP_response_status(resp);
926 if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) {
927 memprintf(err, "OCSP response status not successful");
928 goto out;
929 }
930
931 bs = OCSP_response_get1_basic(resp);
932 if (!bs) {
933 memprintf(err, "Failed to get basic response from OCSP Response");
934 goto out;
935 }
936
937 count_sr = OCSP_resp_count(bs);
938 if (count_sr > 1) {
939 memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr);
940 goto out;
941 }
942
943 sr = OCSP_resp_get0(bs, 0);
944 if (!sr) {
945 memprintf(err, "Failed to get OCSP single response");
946 goto out;
947 }
948
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200949 id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr);
950
Emeric Brun4147b2e2014-06-16 18:36:30 +0200951 rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd);
Emmanuel Hocdetef607052017-10-24 14:57:16 +0200952 if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) {
Emmanuel Hocdet872085c2017-10-10 15:18:52 +0200953 memprintf(err, "OCSP single response: certificate status is unknown");
Emeric Brun4147b2e2014-06-16 18:36:30 +0200954 goto out;
955 }
956
Emeric Brun13a6b482014-06-20 15:44:34 +0200957 if (!nextupd) {
958 memprintf(err, "OCSP single response: missing nextupdate");
959 goto out;
960 }
961
Emeric Brunc8b27b62014-06-19 14:16:17 +0200962 rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200963 if (!rc) {
964 memprintf(err, "OCSP single response: no longer valid.");
965 goto out;
966 }
967
968 if (cid) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200969 if (OCSP_id_cmp(id, cid)) {
Emeric Brun4147b2e2014-06-16 18:36:30 +0200970 memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer");
971 goto out;
972 }
973 }
974
975 if (!ocsp) {
976 unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH];
977 unsigned char *p;
978
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200979 rc = i2d_OCSP_CERTID(id, NULL);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200980 if (!rc) {
981 memprintf(err, "OCSP single response: Unable to encode Certificate ID");
982 goto out;
983 }
984
985 if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) {
986 memprintf(err, "OCSP single response: Certificate ID too long");
987 goto out;
988 }
989
990 p = key;
991 memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +0200992 i2d_OCSP_CERTID(id, &p);
Emeric Brun4147b2e2014-06-16 18:36:30 +0200993 ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH);
994 if (!ocsp) {
995 memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer");
996 goto out;
997 }
998 }
999
1000 /* According to comments on "chunk_dup", the
1001 previous chunk buffer will be freed */
1002 if (!chunk_dup(&ocsp->response, ocsp_response)) {
1003 memprintf(err, "OCSP response: Memory allocation error");
1004 goto out;
1005 }
1006
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001007 ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW;
1008
Emeric Brun4147b2e2014-06-16 18:36:30 +02001009 ret = 0;
1010out:
Janusz Dziemidowicz8d710492017-03-08 16:59:41 +01001011 ERR_clear_error();
1012
Emeric Brun4147b2e2014-06-16 18:36:30 +02001013 if (bs)
1014 OCSP_BASICRESP_free(bs);
1015
1016 if (resp)
1017 OCSP_RESPONSE_free(resp);
1018
1019 return ret;
1020}
1021/*
1022 * External function use to update the OCSP response in the OCSP response's
1023 * containers tree. The chunk 'ocsp_response' must contain the OCSP response
1024 * to update in DER format.
1025 *
1026 * Returns 0 on success, 1 in error case.
1027 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001028int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001029{
1030 return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err);
1031}
1032
William Lallemand4a660132019-10-14 14:51:41 +02001033#endif
1034
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001035#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
1036static 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)
1037{
Christopher Faulet16f45c82018-02-16 11:23:49 +01001038 struct tls_keys_ref *ref;
Emeric Brun9e754772019-01-10 17:51:55 +01001039 union tls_sess_key *keys;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001040 struct connection *conn;
1041 int head;
1042 int i;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001043 int ret = -1; /* error by default */
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001044
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001045 conn = SSL_get_ex_data(s, ssl_app_data_index);
Willy Tarreau07d94e42018-09-20 10:57:52 +02001046 ref = __objt_listener(conn->target)->bind_conf->keys_ref;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001047 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1048
1049 keys = ref->tlskeys;
1050 head = ref->tls_ticket_enc_index;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001051
1052 if (enc) {
1053 memcpy(key_name, keys[head].name, 16);
1054
1055 if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH))
Christopher Faulet16f45c82018-02-16 11:23:49 +01001056 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001057
Emeric Brun9e754772019-01-10 17:51:55 +01001058 if (ref->key_size_bits == 128) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001059
Emeric Brun9e754772019-01-10 17:51:55 +01001060 if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv))
1061 goto end;
1062
Willy Tarreau9356dac2019-05-10 09:22:53 +02001063 HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001064 ret = 1;
1065 }
1066 else if (ref->key_size_bits == 256 ) {
1067
1068 if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv))
1069 goto end;
1070
Willy Tarreau9356dac2019-05-10 09:22:53 +02001071 HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL);
Emeric Brun9e754772019-01-10 17:51:55 +01001072 ret = 1;
1073 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001074 } else {
1075 for (i = 0; i < TLS_TICKETS_NO; i++) {
1076 if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16))
1077 goto found;
1078 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01001079 ret = 0;
1080 goto end;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001081
Christopher Faulet16f45c82018-02-16 11:23:49 +01001082 found:
Emeric Brun9e754772019-01-10 17:51:55 +01001083 if (ref->key_size_bits == 128) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001084 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 +01001085 if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv))
1086 goto end;
1087 /* 2 for key renewal, 1 if current key is still valid */
1088 ret = i ? 2 : 1;
1089 }
1090 else if (ref->key_size_bits == 256) {
Willy Tarreau9356dac2019-05-10 09:22:53 +02001091 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 +01001092 if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv))
1093 goto end;
1094 /* 2 for key renewal, 1 if current key is still valid */
1095 ret = i ? 2 : 1;
1096 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001097 }
Emeric Brun9e754772019-01-10 17:51:55 +01001098
Christopher Faulet16f45c82018-02-16 11:23:49 +01001099 end:
1100 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
1101 return ret;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001102}
1103
1104struct tls_keys_ref *tlskeys_ref_lookup(const char *filename)
1105{
1106 struct tls_keys_ref *ref;
1107
1108 list_for_each_entry(ref, &tlskeys_reference, list)
1109 if (ref->filename && strcmp(filename, ref->filename) == 0)
1110 return ref;
1111 return NULL;
1112}
1113
1114struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id)
1115{
1116 struct tls_keys_ref *ref;
1117
1118 list_for_each_entry(ref, &tlskeys_reference, list)
1119 if (ref->unique_id == unique_id)
1120 return ref;
1121 return NULL;
1122}
1123
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001124/* Update the key into ref: if keysize doesn't
Emeric Brun9e754772019-01-10 17:51:55 +01001125 * match existing ones, this function returns -1
1126 * else it returns 0 on success.
1127 */
1128int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref,
Willy Tarreau83061a82018-07-13 11:56:34 +02001129 struct buffer *tlskey)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001130{
Emeric Brun9e754772019-01-10 17:51:55 +01001131 if (ref->key_size_bits == 128) {
1132 if (tlskey->data != sizeof(struct tls_sess_key_128))
1133 return -1;
1134 }
1135 else if (ref->key_size_bits == 256) {
1136 if (tlskey->data != sizeof(struct tls_sess_key_256))
1137 return -1;
1138 }
1139 else
1140 return -1;
1141
Christopher Faulet16f45c82018-02-16 11:23:49 +01001142 HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001143 memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)),
1144 tlskey->area, tlskey->data);
Christopher Faulet16f45c82018-02-16 11:23:49 +01001145 ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO;
1146 HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Emeric Brun9e754772019-01-10 17:51:55 +01001147
1148 return 0;
Christopher Faulet16f45c82018-02-16 11:23:49 +01001149}
1150
Willy Tarreau83061a82018-07-13 11:56:34 +02001151int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err)
Christopher Faulet16f45c82018-02-16 11:23:49 +01001152{
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001153 struct tls_keys_ref *ref = tlskeys_ref_lookup(filename);
1154
1155 if(!ref) {
1156 memprintf(err, "Unable to locate the referenced filename: %s", filename);
1157 return 1;
1158 }
Emeric Brun9e754772019-01-10 17:51:55 +01001159 if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) {
1160 memprintf(err, "Invalid key size");
1161 return 1;
1162 }
1163
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001164 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001165}
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001166
1167/* This function finalize the configuration parsing. Its set all the
Willy Tarreaud1c57502016-12-22 22:46:15 +01001168 * automatic ids. It's called just after the basic checks. It returns
1169 * 0 on success otherwise ERR_*.
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001170 */
Willy Tarreaud1c57502016-12-22 22:46:15 +01001171static int tlskeys_finalize_config(void)
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001172{
1173 int i = 0;
1174 struct tls_keys_ref *ref, *ref2, *ref3;
1175 struct list tkr = LIST_HEAD_INIT(tkr);
1176
1177 list_for_each_entry(ref, &tlskeys_reference, list) {
1178 if (ref->unique_id == -1) {
1179 /* Look for the first free id. */
1180 while (1) {
1181 list_for_each_entry(ref2, &tlskeys_reference, list) {
1182 if (ref2->unique_id == i) {
1183 i++;
1184 break;
1185 }
1186 }
1187 if (&ref2->list == &tlskeys_reference)
1188 break;
1189 }
1190
1191 /* Uses the unique id and increment it for the next entry. */
1192 ref->unique_id = i;
1193 i++;
1194 }
1195 }
1196
1197 /* This sort the reference list by id. */
1198 list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) {
1199 LIST_DEL(&ref->list);
1200 list_for_each_entry(ref3, &tkr, list) {
1201 if (ref->unique_id < ref3->unique_id) {
1202 LIST_ADDQ(&ref3->list, &ref->list);
1203 break;
1204 }
1205 }
1206 if (&ref3->list == &tkr)
1207 LIST_ADDQ(&tkr, &ref->list);
1208 }
1209
1210 /* swap root */
1211 LIST_ADD(&tkr, &tlskeys_reference);
1212 LIST_DEL(&tkr);
Willy Tarreaud1c57502016-12-22 22:46:15 +01001213 return 0;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02001214}
Nenad Merdanovic05552d42015-02-27 19:56:49 +01001215#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
1216
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001217#ifndef OPENSSL_NO_OCSP
yanbzhube2774d2015-12-10 15:07:30 -05001218int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype)
1219{
1220 switch (evp_keytype) {
1221 case EVP_PKEY_RSA:
1222 return 2;
1223 case EVP_PKEY_DSA:
1224 return 0;
1225 case EVP_PKEY_EC:
1226 return 1;
1227 }
1228
1229 return -1;
1230}
1231
Emeric Brun4147b2e2014-06-16 18:36:30 +02001232/*
1233 * Callback used to set OCSP status extension content in server hello.
1234 */
1235int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg)
1236{
yanbzhube2774d2015-12-10 15:07:30 -05001237 struct certificate_ocsp *ocsp;
1238 struct ocsp_cbk_arg *ocsp_arg;
1239 char *ssl_buf;
1240 EVP_PKEY *ssl_pkey;
1241 int key_type;
1242 int index;
1243
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001244 ocsp_arg = arg;
yanbzhube2774d2015-12-10 15:07:30 -05001245
1246 ssl_pkey = SSL_get_privatekey(ssl);
1247 if (!ssl_pkey)
1248 return SSL_TLSEXT_ERR_NOACK;
1249
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001250 key_type = EVP_PKEY_base_id(ssl_pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001251
1252 if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type)
1253 ocsp = ocsp_arg->s_ocsp;
1254 else {
1255 /* For multiple certs per context, we have to find the correct OCSP response based on
1256 * the certificate type
1257 */
1258 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
1259
1260 if (index < 0)
1261 return SSL_TLSEXT_ERR_NOACK;
1262
1263 ocsp = ocsp_arg->m_ocsp[index];
1264
1265 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001266
1267 if (!ocsp ||
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001268 !ocsp->response.area ||
1269 !ocsp->response.data ||
Emeric Brun4f3c87a2014-06-20 15:46:13 +02001270 (ocsp->expire < now.tv_sec))
Emeric Brun4147b2e2014-06-16 18:36:30 +02001271 return SSL_TLSEXT_ERR_NOACK;
1272
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001273 ssl_buf = OPENSSL_malloc(ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001274 if (!ssl_buf)
1275 return SSL_TLSEXT_ERR_NOACK;
1276
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001277 memcpy(ssl_buf, ocsp->response.area, ocsp->response.data);
1278 SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001279
1280 return SSL_TLSEXT_ERR_OK;
1281}
1282
William Lallemand4a660132019-10-14 14:51:41 +02001283#endif
1284
1285#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
Emeric Brun4147b2e2014-06-16 18:36:30 +02001286/*
1287 * This function enables the handling of OCSP status extension on 'ctx' if a
William Lallemand246c0242019-10-11 08:59:13 +02001288 * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP
1289 * status extension, the issuer's certificate is mandatory. It should be
1290 * present in ckch->ocsp_issuer.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001291 *
William Lallemand246c0242019-10-11 08:59:13 +02001292 * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an
1293 * OCSP response. If file is empty or content is not a valid OCSP response,
1294 * OCSP status extension is enabled but OCSP response is ignored (a warning is
1295 * displayed).
Emeric Brun4147b2e2014-06-16 18:36:30 +02001296 *
1297 * Returns 1 if no ".ocsp" file found, 0 if OCSP status extension is
Joseph Herlant017b3da2018-11-15 09:07:59 -08001298 * successfully enabled, or -1 in other error case.
Emeric Brun4147b2e2014-06-16 18:36:30 +02001299 */
William Lallemand4a660132019-10-14 14:51:41 +02001300#ifndef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001301static 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 +02001302{
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001303 X509 *x, *issuer;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001304 OCSP_CERTID *cid = NULL;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001305 int i, ret = -1;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001306 struct certificate_ocsp *ocsp = NULL, *iocsp;
1307 char *warn = NULL;
1308 unsigned char *p;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001309 void (*callback) (void);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001310
Emeric Brun4147b2e2014-06-16 18:36:30 +02001311
William Lallemand246c0242019-10-11 08:59:13 +02001312 x = ckch->cert;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001313 if (!x)
1314 goto out;
1315
William Lallemand246c0242019-10-11 08:59:13 +02001316 issuer = ckch->ocsp_issuer;
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001317 /* take issuer from chain over ocsp_issuer, is what is done historicaly */
1318 if (chain) {
1319 /* check if one of the certificate of the chain is the issuer */
1320 for (i = 0; i < sk_X509_num(chain); i++) {
1321 X509 *ti = sk_X509_value(chain, i);
1322 if (X509_check_issued(ti, x) == X509_V_OK) {
1323 issuer = ti;
1324 break;
1325 }
1326 }
1327 }
William Lallemand246c0242019-10-11 08:59:13 +02001328 if (!issuer)
1329 goto out;
Emeric Brun4147b2e2014-06-16 18:36:30 +02001330
1331 cid = OCSP_cert_to_id(0, x, issuer);
1332 if (!cid)
1333 goto out;
1334
1335 i = i2d_OCSP_CERTID(cid, NULL);
1336 if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH))
1337 goto out;
1338
Vincent Bernat02779b62016-04-03 13:48:43 +02001339 ocsp = calloc(1, sizeof(*ocsp));
Emeric Brun4147b2e2014-06-16 18:36:30 +02001340 if (!ocsp)
1341 goto out;
1342
1343 p = ocsp->key_data;
1344 i2d_OCSP_CERTID(cid, &p);
1345
1346 iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH);
1347 if (iocsp == ocsp)
1348 ocsp = NULL;
1349
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001350#ifndef SSL_CTX_get_tlsext_status_cb
1351# define SSL_CTX_get_tlsext_status_cb(ctx, cb) \
1352 *cb = (void (*) (void))ctx->tlsext_status_cb;
1353#endif
1354 SSL_CTX_get_tlsext_status_cb(ctx, &callback);
1355
1356 if (!callback) {
Vincent Bernat02779b62016-04-03 13:48:43 +02001357 struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg));
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001358 EVP_PKEY *pkey;
yanbzhube2774d2015-12-10 15:07:30 -05001359
1360 cb_arg->is_single = 1;
1361 cb_arg->s_ocsp = iocsp;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001362
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001363 pkey = X509_get_pubkey(x);
1364 cb_arg->single_kt = EVP_PKEY_base_id(pkey);
1365 EVP_PKEY_free(pkey);
yanbzhube2774d2015-12-10 15:07:30 -05001366
1367 SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk);
1368 SSL_CTX_set_tlsext_status_arg(ctx, cb_arg);
1369 } else {
1370 /*
1371 * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx
1372 * Update that cb_arg with the new cert's staple
1373 */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001374 struct ocsp_cbk_arg *cb_arg;
yanbzhube2774d2015-12-10 15:07:30 -05001375 struct certificate_ocsp *tmp_ocsp;
1376 int index;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001377 int key_type;
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001378 EVP_PKEY *pkey;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001379
1380#ifdef SSL_CTX_get_tlsext_status_arg
1381 SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg);
1382#else
1383 cb_arg = ctx->tlsext_status_arg;
1384#endif
yanbzhube2774d2015-12-10 15:07:30 -05001385
1386 /*
1387 * The following few lines will convert cb_arg from a single ocsp to multi ocsp
1388 * the order of operations below matter, take care when changing it
1389 */
1390 tmp_ocsp = cb_arg->s_ocsp;
1391 index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt);
1392 cb_arg->s_ocsp = NULL;
1393 cb_arg->m_ocsp[index] = tmp_ocsp;
1394 cb_arg->is_single = 0;
1395 cb_arg->single_kt = 0;
1396
Emmanuel Hocdetb7a4c342017-01-06 12:57:46 +01001397 pkey = X509_get_pubkey(x);
1398 key_type = EVP_PKEY_base_id(pkey);
1399 EVP_PKEY_free(pkey);
1400
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001401 index = ssl_sock_get_ocsp_arg_kt_index(key_type);
yanbzhube2774d2015-12-10 15:07:30 -05001402 if (index >= 0 && !cb_arg->m_ocsp[index])
1403 cb_arg->m_ocsp[index] = iocsp;
1404
1405 }
Emeric Brun4147b2e2014-06-16 18:36:30 +02001406
1407 ret = 0;
1408
1409 warn = NULL;
William Lallemand246c0242019-10-11 08:59:13 +02001410 if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) {
William Lallemand3b5f3602019-10-16 18:05:05 +02001411 memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure");
Christopher Faulet767a84b2017-11-24 16:50:31 +01001412 ha_warning("%s.\n", warn);
Emeric Brun4147b2e2014-06-16 18:36:30 +02001413 }
1414
1415out:
Emeric Brun4147b2e2014-06-16 18:36:30 +02001416 if (cid)
1417 OCSP_CERTID_free(cid);
1418
1419 if (ocsp)
1420 free(ocsp);
1421
1422 if (warn)
1423 free(warn);
1424
Emeric Brun4147b2e2014-06-16 18:36:30 +02001425 return ret;
1426}
William Lallemand4a660132019-10-14 14:51:41 +02001427#else /* OPENSSL_IS_BORINGSSL */
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01001428static 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 +02001429{
William Lallemand4a660132019-10-14 14:51:41 +02001430 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 +02001431}
1432#endif
1433
William Lallemand4a660132019-10-14 14:51:41 +02001434#endif
1435
1436
Willy Tarreau5db847a2019-05-09 14:13:35 +02001437#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001438
1439#define CT_EXTENSION_TYPE 18
1440
William Lallemand03c331c2020-05-13 10:10:01 +02001441int sctl_ex_index = -1;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001442
1443int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg)
1444{
Willy Tarreau83061a82018-07-13 11:56:34 +02001445 struct buffer *sctl = add_arg;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001446
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001447 *out = (unsigned char *) sctl->area;
1448 *outlen = sctl->data;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001449
1450 return 1;
1451}
1452
1453int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg)
1454{
1455 return 1;
1456}
1457
William Lallemanda17f4112019-10-10 15:16:44 +02001458static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001459{
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001460 int ret = -1;
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001461
William Lallemanda17f4112019-10-10 15:16:44 +02001462 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 +01001463 goto out;
1464
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +01001465 SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl);
1466
1467 ret = 0;
1468
1469out:
1470 return ret;
1471}
1472
1473#endif
1474
Emeric Brune1f38db2012-09-03 20:36:47 +02001475void ssl_sock_infocbk(const SSL *ssl, int where, int ret)
1476{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001477 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001478 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001479 BIO *write_bio;
Willy Tarreau622317d2015-02-27 16:36:16 +01001480 (void)ret; /* shut gcc stupid warning */
Emeric Brune1f38db2012-09-03 20:36:47 +02001481
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001482#ifndef SSL_OP_NO_RENEGOTIATION
1483 /* Please note that BoringSSL defines this macro to zero so don't
1484 * change this to #if and do not assign a default value to this macro!
1485 */
Emeric Brune1f38db2012-09-03 20:36:47 +02001486 if (where & SSL_CB_HANDSHAKE_START) {
1487 /* Disable renegotiation (CVE-2009-3555) */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01001488 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 +02001489 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01001490 conn->err_code = CO_ER_SSL_RENEG;
1491 }
Emeric Brune1f38db2012-09-03 20:36:47 +02001492 }
Dirkjan Bussink526894f2019-01-21 09:35:03 -08001493#endif
Emeric Brund8b2bb52014-01-28 15:43:53 +01001494
1495 if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001496 if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) {
Emeric Brund8b2bb52014-01-28 15:43:53 +01001497 /* Long certificate chains optimz
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001498 If write and read bios are different, we
Emeric Brund8b2bb52014-01-28 15:43:53 +01001499 consider that the buffering was activated,
1500 so we rise the output buffer size from 4k
1501 to 16k */
1502 write_bio = SSL_get_wbio(ssl);
1503 if (write_bio != SSL_get_rbio(ssl)) {
1504 BIO_set_write_buffer_size(write_bio, 16384);
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001505 ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE;
Emeric Brund8b2bb52014-01-28 15:43:53 +01001506 }
1507 }
1508 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02001509}
1510
Emeric Brune64aef12012-09-21 13:15:06 +02001511/* Callback is called for each certificate of the chain during a verify
1512 ok is set to 1 if preverify detect no error on current certificate.
1513 Returns 0 to break the handshake, 1 otherwise. */
Evan Broderbe554312013-06-27 00:05:25 -07001514int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store)
Emeric Brune64aef12012-09-21 13:15:06 +02001515{
1516 SSL *ssl;
1517 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001518 struct ssl_sock_ctx *ctx;
Emeric Brun81c00f02012-09-21 14:31:21 +02001519 int err, depth;
Emeric Brune64aef12012-09-21 13:15:06 +02001520
1521 ssl = X509_STORE_CTX_get_ex_data(x_store, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02001522 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emeric Brune64aef12012-09-21 13:15:06 +02001523
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001524 ctx = conn->xprt_ctx;
1525
1526 ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE;
Emeric Brune64aef12012-09-21 13:15:06 +02001527
Emeric Brun81c00f02012-09-21 14:31:21 +02001528 if (ok) /* no errors */
1529 return ok;
1530
1531 depth = X509_STORE_CTX_get_error_depth(x_store);
1532 err = X509_STORE_CTX_get_error(x_store);
1533
1534 /* check if CA error needs to be ignored */
1535 if (depth > 0) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001536 if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) {
1537 ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err);
1538 ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth);
Emeric Brunf282a812012-09-21 15:27:54 +02001539 }
1540
Willy Tarreau731248f2020-02-04 14:02:02 +01001541 if (err < 64 && __objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001542 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001543 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001544 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001545 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001546
Willy Tarreau20879a02012-12-03 16:32:10 +01001547 conn->err_code = CO_ER_SSL_CA_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001548 return 0;
1549 }
1550
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01001551 if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st))
1552 ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err);
Emeric Brunf282a812012-09-21 15:27:54 +02001553
Emeric Brun81c00f02012-09-21 14:31:21 +02001554 /* check if certificate error needs to be ignored */
Willy Tarreau731248f2020-02-04 14:02:02 +01001555 if (err < 64 && __objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) {
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02001556 ssl_sock_dump_errors(conn);
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001557 ERR_clear_error();
Emeric Brun81c00f02012-09-21 14:31:21 +02001558 return 1;
Emeric Brun1eb20ef2012-12-03 13:24:29 +01001559 }
Emeric Brun81c00f02012-09-21 14:31:21 +02001560
Willy Tarreau20879a02012-12-03 16:32:10 +01001561 conn->err_code = CO_ER_SSL_CRT_FAIL;
Emeric Brun81c00f02012-09-21 14:31:21 +02001562 return 0;
Emeric Brune64aef12012-09-21 13:15:06 +02001563}
1564
Dragan Dosen9ac98092020-05-11 15:51:45 +02001565#ifdef TLS1_RT_HEARTBEAT
1566static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version,
1567 int content_type, const void *buf, size_t len,
1568 SSL *ssl)
1569{
1570 /* test heartbeat received (write_p is set to 0
1571 for a received record) */
1572 if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) {
1573 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
1574 const unsigned char *p = buf;
1575 unsigned int payload;
1576
1577 ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT;
1578
1579 /* Check if this is a CVE-2014-0160 exploitation attempt. */
1580 if (*p != TLS1_HB_REQUEST)
1581 return;
1582
1583 if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */
1584 goto kill_it;
1585
1586 payload = (p[1] * 256) + p[2];
1587 if (3 + payload + 16 <= len)
1588 return; /* OK no problem */
1589 kill_it:
1590 /* We have a clear heartbleed attack (CVE-2014-0160), the
1591 * advertised payload is larger than the advertised packet
1592 * length, so we have garbage in the buffer between the
1593 * payload and the end of the buffer (p+len). We can't know
1594 * if the SSL stack is patched, and we don't know if we can
1595 * safely wipe out the area between p+3+len and payload.
1596 * So instead, we prevent the response from being sent by
1597 * setting the max_send_fragment to 0 and we report an SSL
1598 * error, which will kill this connection. It will be reported
1599 * above as SSL_ERROR_SSL while an other handshake failure with
1600 * a heartbeat message will be reported as SSL_ERROR_SYSCALL.
1601 */
1602 ssl->max_send_fragment = 0;
1603 SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE);
1604 }
1605}
1606#endif
1607
1608static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version,
1609 int content_type, const void *buf, size_t len,
1610 SSL *ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001611{
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001612 struct ssl_capture *capture;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001613 unsigned char *msg;
1614 unsigned char *end;
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001615 size_t rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001616
1617 /* This function is called for "from client" and "to server"
1618 * connections. The combination of write_p == 0 and content_type == 22
Joseph Herlant017b3da2018-11-15 09:07:59 -08001619 * is only available during "from client" connection.
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001620 */
1621
1622 /* "write_p" is set to 0 is the bytes are received messages,
1623 * otherwise it is set to 1.
1624 */
1625 if (write_p != 0)
1626 return;
1627
1628 /* content_type contains the type of message received or sent
1629 * according with the SSL/TLS protocol spec. This message is
1630 * encoded with one byte. The value 256 (two bytes) is used
1631 * for designing the SSL/TLS record layer. According with the
1632 * rfc6101, the expected message (other than 256) are:
1633 * - change_cipher_spec(20)
1634 * - alert(21)
1635 * - handshake(22)
1636 * - application_data(23)
1637 * - (255)
1638 * We are interessed by the handshake and specially the client
1639 * hello.
1640 */
1641 if (content_type != 22)
1642 return;
1643
1644 /* The message length is at least 4 bytes, containing the
1645 * message type and the message length.
1646 */
1647 if (len < 4)
1648 return;
1649
1650 /* First byte of the handshake message id the type of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001651 * message. The known types are:
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001652 * - hello_request(0)
1653 * - client_hello(1)
1654 * - server_hello(2)
1655 * - certificate(11)
1656 * - server_key_exchange (12)
1657 * - certificate_request(13)
1658 * - server_hello_done(14)
1659 * We are interested by the client hello.
1660 */
1661 msg = (unsigned char *)buf;
1662 if (msg[0] != 1)
1663 return;
1664
1665 /* Next three bytes are the length of the message. The total length
1666 * must be this decoded length + 4. If the length given as argument
1667 * is not the same, we abort the protocol dissector.
1668 */
1669 rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3];
1670 if (len < rec_len + 4)
1671 return;
1672 msg += 4;
1673 end = msg + rec_len;
1674 if (end < msg)
1675 return;
1676
1677 /* Expect 2 bytes for protocol version (1 byte for major and 1 byte
1678 * for minor, the random, composed by 4 bytes for the unix time and
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001679 * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28.
1680 */
1681 msg += 1 + 1 + 4 + 28;
1682 if (msg > end)
1683 return;
1684
1685 /* Next, is session id:
1686 * if present, we have to jump by length + 1 for the size information
1687 * if not present, we have to jump by 1 only
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001688 */
Baptiste Assmann6be139f2018-11-28 15:20:25 +01001689 if (msg[0] > 0)
1690 msg += msg[0];
1691 msg += 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001692 if (msg > end)
1693 return;
1694
1695 /* Next two bytes are the ciphersuite length. */
1696 if (msg + 2 > end)
1697 return;
1698 rec_len = (msg[0] << 8) + msg[1];
1699 msg += 2;
1700 if (msg + rec_len > end || msg + rec_len < msg)
1701 return;
1702
Willy Tarreaubafbe012017-11-24 17:34:44 +01001703 capture = pool_alloc_dirty(pool_head_ssl_capture);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001704 if (!capture)
1705 return;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001706 /* Compute the xxh64 of the ciphersuite. */
1707 capture->xxh64 = XXH64(msg, rec_len, 0);
1708
1709 /* Capture the ciphersuite. */
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001710 capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ?
1711 global_ssl.capture_cipherlist : rec_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001712 memcpy(capture->ciphersuite, msg, capture->ciphersuite_len);
Emmanuel Hocdete3804742017-03-08 11:07:10 +01001713
1714 SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01001715}
1716
Emeric Brun29f037d2014-04-25 19:05:36 +02001717/* Callback is called for ssl protocol analyse */
1718void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg)
1719{
Dragan Dosen1e7ed042020-05-08 18:30:00 +02001720 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
1721 struct ssl_sock_msg_callback *cbk;
1722
Dragan Dosen1e7ed042020-05-08 18:30:00 +02001723 /* Try to call all callback functions that were registered by using
1724 * ssl_sock_register_msg_callback().
1725 */
1726 list_for_each_entry(cbk, &ssl_sock_msg_callbacks, list) {
1727 cbk->func(conn, write_p, version, content_type, buf, len, ssl);
1728 }
Emeric Brun29f037d2014-04-25 19:05:36 +02001729}
1730
Bernard Spil13c53f82018-02-15 13:34:58 +01001731#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchardc7566002018-11-20 23:33:50 +01001732static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen,
1733 const unsigned char *in, unsigned int inlen,
1734 void *arg)
1735{
1736 struct server *srv = arg;
1737
1738 if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str,
1739 srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED)
1740 return SSL_TLSEXT_ERR_OK;
1741 return SSL_TLSEXT_ERR_NOACK;
1742}
1743#endif
1744
1745#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001746/* This callback is used so that the server advertises the list of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001747 * negotiable protocols for NPN.
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001748 */
1749static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data,
1750 unsigned int *len, void *arg)
1751{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001752 struct ssl_bind_conf *conf = arg;
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02001753
1754 *data = (const unsigned char *)conf->npn_str;
1755 *len = conf->npn_len;
1756 return SSL_TLSEXT_ERR_OK;
1757}
1758#endif
1759
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001760#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02001761/* This callback is used so that the server advertises the list of
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05001762 * negotiable protocols for ALPN.
Willy Tarreauab861d32013-04-02 02:30:41 +02001763 */
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001764static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out,
1765 unsigned char *outlen,
1766 const unsigned char *server,
1767 unsigned int server_len, void *arg)
Willy Tarreauab861d32013-04-02 02:30:41 +02001768{
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001769 struct ssl_bind_conf *conf = arg;
Willy Tarreauab861d32013-04-02 02:30:41 +02001770
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01001771 if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str,
1772 conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) {
1773 return SSL_TLSEXT_ERR_NOACK;
1774 }
Willy Tarreauab861d32013-04-02 02:30:41 +02001775 return SSL_TLSEXT_ERR_OK;
1776}
1777#endif
1778
Willy Tarreauc8ad3be2015-06-17 15:48:26 +02001779#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01001780#ifndef SSL_NO_GENERATE_CERTIFICATES
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001781
Christopher Faulet30548802015-06-11 13:39:32 +02001782/* Create a X509 certificate with the specified servername and serial. This
1783 * function returns a SSL_CTX object or NULL if an error occurs. */
Christopher Faulet7969a332015-10-09 11:15:03 +02001784static SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001785ssl_sock_do_create_cert(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001786{
Christopher Faulet7969a332015-10-09 11:15:03 +02001787 X509 *cacert = bind_conf->ca_sign_cert;
1788 EVP_PKEY *capkey = bind_conf->ca_sign_pkey;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001789 SSL_CTX *ssl_ctx = NULL;
1790 X509 *newcrt = NULL;
1791 EVP_PKEY *pkey = NULL;
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001792 SSL *tmp_ssl = NULL;
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001793 CONF *ctmp = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001794 X509_NAME *name;
1795 const EVP_MD *digest;
1796 X509V3_CTX ctx;
1797 unsigned int i;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001798 int key_type;
Christopher Faulet31af49d2015-06-09 17:29:50 +02001799
Christopher Faulet48a83322017-07-28 16:56:09 +02001800 /* Get the private key of the default certificate and use it */
Willy Tarreau5db847a2019-05-09 14:13:35 +02001801#if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L)
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001802 pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx);
1803#else
1804 tmp_ssl = SSL_new(bind_conf->default_ctx);
1805 if (tmp_ssl)
1806 pkey = SSL_get_privatekey(tmp_ssl);
1807#endif
1808 if (!pkey)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001809 goto mkcert_error;
1810
1811 /* Create the certificate */
1812 if (!(newcrt = X509_new()))
1813 goto mkcert_error;
1814
1815 /* Set version number for the certificate (X509v3) and the serial
1816 * number */
1817 if (X509_set_version(newcrt, 2L) != 1)
1818 goto mkcert_error;
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01001819 ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1));
Christopher Faulet31af49d2015-06-09 17:29:50 +02001820
1821 /* Set duration for the certificate */
Rosen Penev68185952018-12-14 08:47:02 -08001822 if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) ||
1823 !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001824 goto mkcert_error;
1825
1826 /* set public key in the certificate */
1827 if (X509_set_pubkey(newcrt, pkey) != 1)
1828 goto mkcert_error;
1829
1830 /* Set issuer name from the CA */
1831 if (!(name = X509_get_subject_name(cacert)))
1832 goto mkcert_error;
1833 if (X509_set_issuer_name(newcrt, name) != 1)
1834 goto mkcert_error;
1835
1836 /* Set the subject name using the same, but the CN */
1837 name = X509_NAME_dup(name);
1838 if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
1839 (const unsigned char *)servername,
1840 -1, -1, 0) != 1) {
1841 X509_NAME_free(name);
1842 goto mkcert_error;
1843 }
1844 if (X509_set_subject_name(newcrt, name) != 1) {
1845 X509_NAME_free(name);
1846 goto mkcert_error;
1847 }
1848 X509_NAME_free(name);
1849
1850 /* Add x509v3 extensions as specified */
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001851 ctmp = NCONF_new(NULL);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001852 X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0);
1853 for (i = 0; i < X509V3_EXT_SIZE; i++) {
1854 X509_EXTENSION *ext;
1855
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001856 if (!(ext = X509V3_EXT_nconf(ctmp, &ctx, x509v3_ext_names[i], x509v3_ext_values[i])))
Christopher Faulet31af49d2015-06-09 17:29:50 +02001857 goto mkcert_error;
1858 if (!X509_add_ext(newcrt, ext, -1)) {
1859 X509_EXTENSION_free(ext);
1860 goto mkcert_error;
1861 }
1862 X509_EXTENSION_free(ext);
1863 }
1864
1865 /* Sign the certificate with the CA private key */
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001866
1867 key_type = EVP_PKEY_base_id(capkey);
1868
1869 if (key_type == EVP_PKEY_DSA)
1870 digest = EVP_sha1();
1871 else if (key_type == EVP_PKEY_RSA)
Christopher Faulet31af49d2015-06-09 17:29:50 +02001872 digest = EVP_sha256();
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02001873 else if (key_type == EVP_PKEY_EC)
Christopher Faulet7969a332015-10-09 11:15:03 +02001874 digest = EVP_sha256();
1875 else {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02001876#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet7969a332015-10-09 11:15:03 +02001877 int nid;
1878
1879 if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0)
1880 goto mkcert_error;
1881 if (!(digest = EVP_get_digestbynid(nid)))
1882 goto mkcert_error;
Christopher Faulete7db2162015-10-19 13:59:24 +02001883#else
1884 goto mkcert_error;
1885#endif
Christopher Faulet7969a332015-10-09 11:15:03 +02001886 }
1887
Christopher Faulet31af49d2015-06-09 17:29:50 +02001888 if (!(X509_sign(newcrt, capkey, digest)))
1889 goto mkcert_error;
1890
1891 /* Create and set the new SSL_CTX */
1892 if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method())))
1893 goto mkcert_error;
1894 if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey))
1895 goto mkcert_error;
1896 if (!SSL_CTX_use_certificate(ssl_ctx, newcrt))
1897 goto mkcert_error;
1898 if (!SSL_CTX_check_private_key(ssl_ctx))
1899 goto mkcert_error;
1900
1901 if (newcrt) X509_free(newcrt);
Christopher Faulet7969a332015-10-09 11:15:03 +02001902
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01001903#ifndef OPENSSL_NO_DH
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001904 SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh);
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01001905#endif
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001906#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
1907 {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01001908 const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE);
Christopher Faulet85b5a1a2015-10-09 11:46:32 +02001909 EC_KEY *ecc;
1910 int nid;
1911
1912 if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef)
1913 goto end;
1914 if (!(ecc = EC_KEY_new_by_curve_name(nid)))
1915 goto end;
1916 SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc);
1917 EC_KEY_free(ecc);
1918 }
1919#endif
1920 end:
Christopher Faulet31af49d2015-06-09 17:29:50 +02001921 return ssl_ctx;
1922
1923 mkcert_error:
Emmanuel Hocdeta9b84022018-10-01 18:41:36 +02001924 if (ctmp) NCONF_free(ctmp);
Emmanuel Hocdet15969292017-08-11 10:56:00 +02001925 if (tmp_ssl) SSL_free(tmp_ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001926 if (ssl_ctx) SSL_CTX_free(ssl_ctx);
1927 if (newcrt) X509_free(newcrt);
Christopher Faulet31af49d2015-06-09 17:29:50 +02001928 return NULL;
1929}
1930
Christopher Faulet7969a332015-10-09 11:15:03 +02001931SSL_CTX *
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001932ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key)
Christopher Faulet7969a332015-10-09 11:15:03 +02001933{
Willy Tarreau07d94e42018-09-20 10:57:52 +02001934 struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf;
Olivier Houchard66ab4982019-02-26 18:37:15 +01001935 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001936
Olivier Houchard66ab4982019-02-26 18:37:15 +01001937 return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl);
Christopher Faulet7969a332015-10-09 11:15:03 +02001938}
1939
Christopher Faulet30548802015-06-11 13:39:32 +02001940/* Do a lookup for a certificate in the LRU cache used to store generated
Emeric Brun821bb9b2017-06-15 16:37:39 +02001941 * certificates and immediately assign it to the SSL session if not null. */
Christopher Faulet30548802015-06-11 13:39:32 +02001942SSL_CTX *
Emeric Brun821bb9b2017-06-15 16:37:39 +02001943ssl_sock_assign_generated_cert(unsigned int key, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet30548802015-06-11 13:39:32 +02001944{
1945 struct lru64 *lru = NULL;
1946
1947 if (ssl_ctx_lru_tree) {
Willy Tarreau03f4ec42018-05-17 10:56:47 +02001948 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001949 lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02001950 if (lru && lru->domain) {
1951 if (ssl)
1952 SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data);
Willy Tarreau03f4ec42018-05-17 10:56:47 +02001953 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02001954 return (SSL_CTX *)lru->data;
Emeric Brun821bb9b2017-06-15 16:37:39 +02001955 }
Willy Tarreau03f4ec42018-05-17 10:56:47 +02001956 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet30548802015-06-11 13:39:32 +02001957 }
1958 return NULL;
1959}
1960
Emeric Brun821bb9b2017-06-15 16:37:39 +02001961/* Same as <ssl_sock_assign_generated_cert> but without SSL session. This
1962 * function is not thread-safe, it should only be used to check if a certificate
1963 * exists in the lru cache (with no warranty it will not be removed by another
1964 * thread). It is kept for backward compatibility. */
1965SSL_CTX *
1966ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf)
1967{
1968 return ssl_sock_assign_generated_cert(key, bind_conf, NULL);
1969}
1970
Christopher Fauletd2cab922015-07-28 16:03:47 +02001971/* Set a certificate int the LRU cache used to store generated
1972 * certificate. Return 0 on success, otherwise -1 */
1973int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001974ssl_sock_set_generated_cert(SSL_CTX *ssl_ctx, unsigned int key, struct bind_conf *bind_conf)
Christopher Faulet30548802015-06-11 13:39:32 +02001975{
1976 struct lru64 *lru = NULL;
1977
1978 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001979 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001980 lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0);
Emeric Brun821bb9b2017-06-15 16:37:39 +02001981 if (!lru) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001982 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02001983 return -1;
Emeric Brun821bb9b2017-06-15 16:37:39 +02001984 }
Christopher Faulet30548802015-06-11 13:39:32 +02001985 if (lru->domain && lru->data)
1986 lru->free((SSL_CTX *)lru->data);
Christopher Faulet7969a332015-10-09 11:15:03 +02001987 lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001988 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Fauletd2cab922015-07-28 16:03:47 +02001989 return 0;
Christopher Faulet30548802015-06-11 13:39:32 +02001990 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02001991 return -1;
Christopher Faulet30548802015-06-11 13:39:32 +02001992}
1993
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001994/* Compute the key of the certificate. */
Christopher Faulet30548802015-06-11 13:39:32 +02001995unsigned int
Christopher Faulet635c0ad2015-11-12 11:35:51 +01001996ssl_sock_generated_cert_key(const void *data, size_t len)
Christopher Faulet30548802015-06-11 13:39:32 +02001997{
1998 return XXH32(data, len, ssl_ctx_lru_seed);
1999}
2000
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002001/* Generate a cert and immediately assign it to the SSL session so that the cert's
2002 * refcount is maintained regardless of the cert's presence in the LRU cache.
2003 */
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002004static int
Christopher Faulet7969a332015-10-09 11:15:03 +02002005ssl_sock_generate_certificate(const char *servername, struct bind_conf *bind_conf, SSL *ssl)
Christopher Faulet31af49d2015-06-09 17:29:50 +02002006{
2007 X509 *cacert = bind_conf->ca_sign_cert;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002008 SSL_CTX *ssl_ctx = NULL;
2009 struct lru64 *lru = NULL;
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002010 unsigned int key;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002011
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002012 key = ssl_sock_generated_cert_key(servername, strlen(servername));
Christopher Faulet31af49d2015-06-09 17:29:50 +02002013 if (ssl_ctx_lru_tree) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002014 HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002015 lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002016 if (lru && lru->domain)
2017 ssl_ctx = (SSL_CTX *)lru->data;
Christopher Fauletd2cab922015-07-28 16:03:47 +02002018 if (!ssl_ctx && lru) {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002019 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002020 lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free);
Christopher Fauletd2cab922015-07-28 16:03:47 +02002021 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002022 SSL_set_SSL_CTX(ssl, ssl_ctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002023 HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002024 return 1;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002025 }
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002026 else {
Christopher Faulet635c0ad2015-11-12 11:35:51 +01002027 ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl);
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002028 SSL_set_SSL_CTX(ssl, ssl_ctx);
2029 /* No LRU cache, this CTX will be released as soon as the session dies */
2030 SSL_CTX_free(ssl_ctx);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002031 return 1;
Willy Tarreau2f63ef42015-10-20 15:16:01 +02002032 }
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002033 return 0;
2034}
2035static int
2036ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl)
2037{
2038 unsigned int key;
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002039 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002040
Willy Tarreauf5bdb642019-07-17 11:29:32 +02002041 if (conn_get_dst(conn)) {
Willy Tarreau085a1512019-07-17 14:47:35 +02002042 key = ssl_sock_generated_cert_key(conn->dst, get_addr_len(conn->dst));
Emeric Brun821bb9b2017-06-15 16:37:39 +02002043 if (ssl_sock_assign_generated_cert(key, bind_conf, ssl))
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002044 return 1;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002045 }
2046 return 0;
Christopher Faulet31af49d2015-06-09 17:29:50 +02002047}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002048#endif /* !defined SSL_NO_GENERATE_CERTIFICATES */
Christopher Faulet31af49d2015-06-09 17:29:50 +02002049
Willy Tarreau9a1ab082019-05-09 13:26:41 +02002050#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002051
2052static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002053{
Emmanuel Hocdet23877ab2017-07-12 12:53:02 +02002054#if SSL_OP_NO_SSLv3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002055 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002056 : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method());
2057#endif
2058}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002059static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2060 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002061 : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method());
2062}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002063static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002064#if SSL_OP_NO_TLSv1_1
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002065 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_1_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002066 : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method());
2067#endif
2068}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002069static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002070#if SSL_OP_NO_TLSv1_2
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002071 c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_2_server_method())
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002072 : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method());
2073#endif
2074}
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002075/* TLSv1.2 is the last supported version in this context. */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002076static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {}
2077/* Unusable in this context. */
2078static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {}
2079static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {}
2080static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {}
2081static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {}
2082static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {}
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002083#else /* openssl >= 1.1.0 */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002084
2085static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) {
2086 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002087 : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION);
2088}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002089static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {
2090 c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION)
2091 : SSL_set_min_proto_version(ssl, SSL3_VERSION);
2092}
2093static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) {
2094 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002095 : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
2096}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002097static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {
2098 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION)
2099 : SSL_set_min_proto_version(ssl, TLS1_VERSION);
2100}
2101static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) {
2102 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_1_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002103 : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
2104}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002105static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {
2106 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION)
2107 : SSL_set_min_proto_version(ssl, TLS1_1_VERSION);
2108}
2109static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) {
2110 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002111 : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
2112}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002113static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {
2114 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION)
2115 : SSL_set_min_proto_version(ssl, TLS1_2_VERSION);
2116}
2117static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002118#if SSL_OP_NO_TLSv1_3
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002119 c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION)
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002120 : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION);
2121#endif
2122}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002123static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {
2124#if SSL_OP_NO_TLSv1_3
2125 c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION)
2126 : SSL_set_min_proto_version(ssl, TLS1_3_VERSION);
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002127#endif
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002128}
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002129#endif
2130static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { }
2131static void ssl_set_None_func(SSL *ssl, set_context_func c) { }
Emmanuel Hocdetecb0e232017-05-18 11:56:58 +02002132
William Lallemand7fd8b452020-05-07 15:20:43 +02002133struct methodVersions methodVersions[] = {
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02002134 {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */
2135 {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */
2136 {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */
2137 {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */
2138 {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */
2139 {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 +02002140};
2141
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002142static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx)
2143{
2144 SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk);
2145 SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx)));
2146 SSL_set_SSL_CTX(ssl, ctx);
2147}
2148
Willy Tarreau5db847a2019-05-09 14:13:35 +02002149#if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL))
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002150
2151static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv)
2152{
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002153 struct bind_conf *s = priv;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002154 (void)al; /* shut gcc stupid warning */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002155
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002156 if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs)
2157 return SSL_TLSEXT_ERR_OK;
2158 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002159}
2160
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002161#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002162static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx)
2163{
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002164 SSL *ssl = ctx->ssl;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002165#else
2166static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg)
2167{
2168#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002169 struct connection *conn;
2170 struct bind_conf *s;
2171 const uint8_t *extension_data;
2172 size_t extension_len;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002173 int has_rsa_sig = 0, has_ecdsa_sig = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002174
2175 char *wildp = NULL;
2176 const uint8_t *servername;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002177 size_t servername_len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002178 struct ebmb_node *node, *n, *node_ecdsa = NULL, *node_rsa = NULL, *node_anonymous = NULL;
Olivier Houchardc2aae742017-09-22 18:26:28 +02002179 int allow_early = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002180 int i;
2181
Thierry FOURNIER28962c92018-06-17 21:37:05 +02002182 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Willy Tarreaua8825522018-10-15 13:20:07 +02002183 s = __objt_listener(conn->target)->bind_conf;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002184
Olivier Houchard9679ac92017-10-27 14:58:08 +02002185 if (s->ssl_conf.early_data)
Olivier Houchardc2aae742017-09-22 18:26:28 +02002186 allow_early = 1;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002187#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002188 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name,
2189 &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002190#else
2191 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) {
2192#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002193 /*
2194 * The server_name extension was given too much extensibility when it
2195 * was written, so parsing the normal case is a bit complex.
2196 */
2197 size_t len;
2198 if (extension_len <= 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002199 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002200 /* Extract the length of the supplied list of names. */
2201 len = (*extension_data++) << 8;
2202 len |= *extension_data++;
2203 if (len + 2 != extension_len)
2204 goto abort;
2205 /*
2206 * The list in practice only has a single element, so we only consider
2207 * the first one.
2208 */
2209 if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name)
2210 goto abort;
2211 extension_len = len - 1;
2212 /* Now we can finally pull out the byte array with the actual hostname. */
2213 if (extension_len <= 2)
2214 goto abort;
2215 len = (*extension_data++) << 8;
2216 len |= *extension_data++;
2217 if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name
2218 || memchr(extension_data, 0, len) != NULL)
2219 goto abort;
2220 servername = extension_data;
2221 servername_len = len;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002222 } else {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002223#if (!defined SSL_NO_GENERATE_CERTIFICATES)
2224 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02002225 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002226 }
2227#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002228 /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002229 if (!s->strict_sni) {
William Lallemand21724f02019-11-04 17:56:13 +01002230 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002231 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002232 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchardc2aae742017-09-22 18:26:28 +02002233 goto allow_early;
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002234 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002235 goto abort;
2236 }
2237
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05002238 /* extract/check clientHello information */
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002239#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002240 if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002241#else
2242 if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) {
2243#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002244 uint8_t sign;
2245 size_t len;
2246 if (extension_len < 2)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002247 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002248 len = (*extension_data++) << 8;
2249 len |= *extension_data++;
2250 if (len + 2 != extension_len)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002251 goto abort;
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002252 if (len % 2 != 0)
2253 goto abort;
2254 for (; len > 0; len -= 2) {
2255 extension_data++; /* hash */
2256 sign = *extension_data++;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002257 switch (sign) {
2258 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002259 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002260 break;
2261 case TLSEXT_signature_ecdsa:
2262 has_ecdsa_sig = 1;
2263 break;
2264 default:
2265 continue;
2266 }
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002267 if (has_ecdsa_sig && has_rsa_sig)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002268 break;
2269 }
2270 } else {
Bertrand Jacquina25282b2018-08-14 00:56:13 +01002271 /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002272 has_rsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002273 }
2274 if (has_ecdsa_sig) { /* in very rare case: has ecdsa sign but not a ECDSA cipher */
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002275 const SSL_CIPHER *cipher;
2276 size_t len;
2277 const uint8_t *cipher_suites;
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002278 has_ecdsa_sig = 0;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002279#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002280 len = ctx->cipher_suites_len;
2281 cipher_suites = ctx->cipher_suites;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002282#else
2283 len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites);
2284#endif
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002285 if (len % 2 != 0)
2286 goto abort;
2287 for (; len != 0; len -= 2, cipher_suites += 2) {
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002288#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002289 uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002290 cipher = SSL_get_cipher_by_value(cipher_suite);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002291#else
2292 cipher = SSL_CIPHER_find(ssl, cipher_suites);
2293#endif
Emmanuel Hocdet019f9b12017-10-02 17:12:06 +02002294 if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) {
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002295 has_ecdsa_sig = 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002296 break;
2297 }
2298 }
2299 }
2300
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002301 for (i = 0; i < trash.size && i < servername_len; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002302 trash.area[i] = tolower(servername[i]);
2303 if (!wildp && (trash.area[i] == '.'))
2304 wildp = &trash.area[i];
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002305 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002306 trash.area[i] = 0;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002307
William Lallemand150bfa82019-09-19 17:12:49 +02002308 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002309
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002310 for (i = 0; i < 2; i++) {
2311 if (i == 0) /* lookup in full qualified names */
2312 node = ebst_lookup(&s->sni_ctx, trash.area);
2313 else if (i == 1 && wildp) /* lookup in wildcards names */
2314 node = ebst_lookup(&s->sni_w_ctx, wildp);
2315 else
2316 break;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002317 for (n = node; n; n = ebmb_next_dup(n)) {
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002318 /* lookup a not neg filter */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002319 if (!container_of(n, struct sni_ctx, name)->neg) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002320 switch(container_of(n, struct sni_ctx, name)->kinfo.sig) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002321 case TLSEXT_signature_ecdsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002322 if (!node_ecdsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002323 node_ecdsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002324 break;
2325 case TLSEXT_signature_rsa:
Emmanuel Hocdet9f9b0c62018-09-03 16:29:16 +02002326 if (!node_rsa)
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002327 node_rsa = n;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002328 break;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002329 default: /* TLSEXT_signature_anonymous|dsa */
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002330 if (!node_anonymous)
2331 node_anonymous = n;
2332 break;
2333 }
2334 }
2335 }
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002336 /* select by key_signature priority order */
2337 node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa
2338 : ((has_rsa_sig && node_rsa) ? node_rsa
2339 : (node_anonymous ? node_anonymous
2340 : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */
2341 : node_rsa /* no rsa signature case (far far away) */
2342 )));
2343 if (node) {
2344 /* switch ctx */
2345 struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf;
2346 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002347 if (conf) {
2348 methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN);
2349 methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX);
2350 if (conf->early_data)
2351 allow_early = 1;
2352 }
William Lallemand02010472019-10-18 11:02:19 +02002353 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Olivier Houchard35a63cc2017-11-02 19:04:38 +01002354 goto allow_early;
Emmanuel Hocdet3777e3a2019-11-06 16:05:34 +01002355 }
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002356 }
William Lallemand150bfa82019-09-19 17:12:49 +02002357
William Lallemand02010472019-10-18 11:02:19 +02002358 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002359#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002360 if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) {
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002361 /* switch ctx done in ssl_sock_generate_certificate */
Olivier Houchardc2aae742017-09-22 18:26:28 +02002362 goto allow_early;
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002363 }
2364#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002365 if (!s->strict_sni) {
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002366 /* no certificate match, is the default_ctx */
William Lallemand21724f02019-11-04 17:56:13 +01002367 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002368 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002369 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002370 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02002371allow_early:
2372#ifdef OPENSSL_IS_BORINGSSL
2373 if (allow_early)
2374 SSL_set_early_data_enabled(ssl, 1);
2375#else
2376 if (!allow_early)
2377 SSL_set_max_early_data(ssl, 0);
2378#endif
2379 return 1;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002380 abort:
2381 /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */
2382 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002383#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet48e87552017-08-16 11:28:44 +02002384 return ssl_select_cert_error;
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02002385#else
2386 *al = SSL_AD_UNRECOGNIZED_NAME;
2387 return 0;
2388#endif
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002389}
2390
2391#else /* OPENSSL_IS_BORINGSSL */
2392
Emeric Brunfc0421f2012-09-07 17:30:07 +02002393/* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a
2394 * warning when no match is found, which implies the default (first) cert
2395 * will keep being used.
2396 */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002397static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv)
Emeric Brunfc0421f2012-09-07 17:30:07 +02002398{
2399 const char *servername;
2400 const char *wildp = NULL;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002401 struct ebmb_node *node, *n;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002402 struct bind_conf *s = priv;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002403 int i;
2404 (void)al; /* shut gcc stupid warning */
2405
2406 servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002407 if (!servername) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002408#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002409 if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl))
2410 return SSL_TLSEXT_ERR_OK;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002411#endif
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002412 if (s->strict_sni)
2413 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002414 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002415 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002416 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002417 return SSL_TLSEXT_ERR_NOACK;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002418 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02002419
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002420 for (i = 0; i < trash.size; i++) {
Emeric Brunfc0421f2012-09-07 17:30:07 +02002421 if (!servername[i])
2422 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002423 trash.area[i] = tolower(servername[i]);
2424 if (!wildp && (trash.area[i] == '.'))
2425 wildp = &trash.area[i];
Emeric Brunfc0421f2012-09-07 17:30:07 +02002426 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002427 trash.area[i] = 0;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002428
William Lallemand150bfa82019-09-19 17:12:49 +02002429 HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002430 node = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002431 /* lookup in full qualified names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002432 for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) {
2433 /* lookup a not neg filter */
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002434 if (!container_of(n, struct sni_ctx, name)->neg) {
2435 node = n;
2436 break;
Emmanuel Hocdet65623372013-01-24 17:17:15 +01002437 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002438 }
2439 if (!node && wildp) {
2440 /* lookup in wildcards names */
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002441 for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) {
2442 /* lookup a not neg filter */
2443 if (!container_of(n, struct sni_ctx, name)->neg) {
2444 node = n;
2445 break;
2446 }
2447 }
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002448 }
Emmanuel Hocdetc5fdf0f2019-11-04 15:49:46 +01002449 if (!node) {
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002450#if (!defined SSL_NO_GENERATE_CERTIFICATES)
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02002451 if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) {
2452 /* switch ctx done in ssl_sock_generate_certificate */
William Lallemand150bfa82019-09-19 17:12:49 +02002453 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Christopher Faulet31af49d2015-06-09 17:29:50 +02002454 return SSL_TLSEXT_ERR_OK;
2455 }
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01002456#endif
William Lallemand21724f02019-11-04 17:56:13 +01002457 if (s->strict_sni) {
2458 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002459 return SSL_TLSEXT_ERR_ALERT_FATAL;
William Lallemand21724f02019-11-04 17:56:13 +01002460 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002461 ssl_sock_switchctx_set(ssl, s->default_ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002462 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01002463 return SSL_TLSEXT_ERR_OK;
Emeric Brunfc0421f2012-09-07 17:30:07 +02002464 }
2465
2466 /* switch ctx */
Emmanuel Hocdet530141f2017-03-01 18:54:56 +01002467 ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx);
William Lallemand150bfa82019-09-19 17:12:49 +02002468 HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock);
Emeric Brunfc0421f2012-09-07 17:30:07 +02002469 return SSL_TLSEXT_ERR_OK;
2470}
Emmanuel Hocdet05942112017-02-20 16:11:50 +01002471#endif /* (!) OPENSSL_IS_BORINGSSL */
Emeric Brunfc0421f2012-09-07 17:30:07 +02002472#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
2473
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002474#ifndef OPENSSL_NO_DH
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002475
2476static DH * ssl_get_dh_1024(void)
2477{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002478 static unsigned char dh1024_p[]={
2479 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7,
2480 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3,
2481 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9,
2482 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96,
2483 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D,
2484 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17,
2485 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B,
2486 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94,
2487 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC,
2488 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E,
2489 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B,
2490 };
2491 static unsigned char dh1024_g[]={
2492 0x02,
2493 };
2494
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002495 BIGNUM *p;
2496 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002497 DH *dh = DH_new();
2498 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002499 p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL);
2500 g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002501
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002502 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002503 DH_free(dh);
2504 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002505 } else {
2506 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002507 }
2508 }
2509 return dh;
2510}
2511
2512static DH *ssl_get_dh_2048(void)
2513{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002514 static unsigned char dh2048_p[]={
2515 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59,
2516 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24,
2517 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66,
2518 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10,
2519 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B,
2520 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23,
2521 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC,
2522 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E,
2523 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77,
2524 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A,
2525 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66,
2526 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74,
2527 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E,
2528 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40,
2529 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93,
2530 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43,
2531 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88,
2532 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1,
2533 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17,
2534 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB,
2535 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41,
2536 0xB7,0x1F,0x77,0xF3,
2537 };
2538 static unsigned char dh2048_g[]={
2539 0x02,
2540 };
2541
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002542 BIGNUM *p;
2543 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002544 DH *dh = DH_new();
2545 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002546 p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL);
2547 g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002548
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002549 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002550 DH_free(dh);
2551 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002552 } else {
2553 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002554 }
2555 }
2556 return dh;
2557}
2558
2559static DH *ssl_get_dh_4096(void)
2560{
Remi Gacogned3a341a2015-05-29 16:26:17 +02002561 static unsigned char dh4096_p[]={
2562 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11,
2563 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68,
2564 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD,
2565 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B,
2566 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B,
2567 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47,
2568 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15,
2569 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35,
2570 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79,
2571 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3,
2572 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC,
2573 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52,
2574 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C,
2575 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A,
2576 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41,
2577 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55,
2578 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52,
2579 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66,
2580 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E,
2581 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47,
2582 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2,
2583 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73,
2584 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13,
2585 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10,
2586 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14,
2587 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD,
2588 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E,
2589 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48,
2590 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01,
2591 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60,
2592 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC,
2593 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41,
2594 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8,
2595 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B,
2596 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23,
2597 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE,
2598 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD,
2599 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03,
2600 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08,
2601 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5,
2602 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F,
2603 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67,
2604 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B,
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002605 };
Remi Gacogned3a341a2015-05-29 16:26:17 +02002606 static unsigned char dh4096_g[]={
2607 0x02,
2608 };
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002609
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002610 BIGNUM *p;
2611 BIGNUM *g;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002612 DH *dh = DH_new();
2613 if (dh) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002614 p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL);
2615 g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL);
Remi Gacogned3a341a2015-05-29 16:26:17 +02002616
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002617 if (!p || !g) {
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002618 DH_free(dh);
2619 dh = NULL;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002620 } else {
2621 DH_set0_pqg(dh, p, NULL, g);
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002622 }
2623 }
2624 return dh;
2625}
2626
2627/* Returns Diffie-Hellman parameters matching the private key length
Willy Tarreauef934602016-12-22 23:12:01 +01002628 but not exceeding global_ssl.default_dh_param */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002629static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen)
2630{
2631 DH *dh = NULL;
2632 EVP_PKEY *pkey = SSL_get_privatekey(ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02002633 int type;
2634
2635 type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002636
2637 /* The keylen supplied by OpenSSL can only be 512 or 1024.
2638 See ssl3_send_server_key_exchange() in ssl/s3_srvr.c
2639 */
2640 if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) {
2641 keylen = EVP_PKEY_bits(pkey);
2642 }
2643
Willy Tarreauef934602016-12-22 23:12:01 +01002644 if (keylen > global_ssl.default_dh_param) {
2645 keylen = global_ssl.default_dh_param;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002646 }
2647
Remi Gacogned3a341a2015-05-29 16:26:17 +02002648 if (keylen >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002649 dh = local_dh_4096;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002650 }
2651 else if (keylen >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02002652 dh = local_dh_2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002653 }
2654 else {
Remi Gacogne8de54152014-07-15 11:36:40 +02002655 dh = local_dh_1024;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02002656 }
2657
2658 return dh;
2659}
2660
Remi Gacogne47783ef2015-05-29 15:53:22 +02002661static DH * ssl_sock_get_dh_from_file(const char *filename)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002662{
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002663 DH *dh = NULL;
Remi Gacogne47783ef2015-05-29 15:53:22 +02002664 BIO *in = BIO_new(BIO_s_file());
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002665
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002666 if (in == NULL)
2667 goto end;
2668
Remi Gacogne47783ef2015-05-29 15:53:22 +02002669 if (BIO_read_filename(in, filename) <= 0)
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002670 goto end;
2671
Remi Gacogne47783ef2015-05-29 15:53:22 +02002672 dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
2673
2674end:
2675 if (in)
2676 BIO_free(in);
2677
Emeric Brune1b4ed42018-08-16 15:14:12 +02002678 ERR_clear_error();
2679
Remi Gacogne47783ef2015-05-29 15:53:22 +02002680 return dh;
2681}
2682
2683int ssl_sock_load_global_dh_param_from_file(const char *filename)
2684{
2685 global_dh = ssl_sock_get_dh_from_file(filename);
2686
2687 if (global_dh) {
2688 return 0;
2689 }
2690
2691 return -1;
2692}
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02002693#endif
2694
William Lallemand9117de92019-10-04 00:29:42 +02002695/* This function allocates a sni_ctx and adds it to the ckch_inst */
William Lallemand1d29c742019-10-04 00:53:29 +02002696static int ckch_inst_add_cert_sni(SSL_CTX *ctx, struct ckch_inst *ckch_inst,
William Lallemand9117de92019-10-04 00:29:42 +02002697 struct bind_conf *s, struct ssl_bind_conf *conf,
2698 struct pkey_info kinfo, char *name, int order)
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002699{
2700 struct sni_ctx *sc;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002701 int wild = 0, neg = 0;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002702
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002703 if (*name == '!') {
2704 neg = 1;
2705 name++;
2706 }
2707 if (*name == '*') {
2708 wild = 1;
2709 name++;
2710 }
2711 /* !* filter is a nop */
2712 if (neg && wild)
2713 return order;
2714 if (*name) {
2715 int j, len;
2716 len = strlen(name);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002717 for (j = 0; j < len && j < trash.size; j++)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002718 trash.area[j] = tolower(name[j]);
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002719 if (j >= trash.size)
William Lallemandfe49bb32019-10-03 23:46:33 +02002720 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002721 trash.area[j] = 0;
Thierry FOURNIER / OZON.IO07c3d782016-10-06 10:56:48 +02002722
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002723 sc = malloc(sizeof(struct sni_ctx) + len + 1);
Thierry FOURNIER / OZON.IO7a3bd3b2016-10-06 10:35:29 +02002724 if (!sc)
William Lallemandfe49bb32019-10-03 23:46:33 +02002725 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002726 memcpy(sc->name.key, trash.area, len + 1);
William Lallemand02e19a52020-04-08 16:11:26 +02002727 SSL_CTX_up_ref(ctx);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002728 sc->ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01002729 sc->conf = conf;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02002730 sc->kinfo = kinfo;
Emmanuel Hocdet7c41a1b2013-05-07 20:20:06 +02002731 sc->order = order++;
2732 sc->neg = neg;
William Lallemand1d29c742019-10-04 00:53:29 +02002733 sc->wild = wild;
2734 sc->name.node.leaf_p = NULL;
William Lallemandcfca1422020-03-05 10:17:47 +01002735 sc->ckch_inst = ckch_inst;
William Lallemand1d29c742019-10-04 00:53:29 +02002736 LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01002737 }
2738 return order;
2739}
2740
William Lallemand6af03992019-07-23 15:00:54 +02002741/*
William Lallemand1d29c742019-10-04 00:53:29 +02002742 * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree
2743 * This function can't return an error.
2744 *
2745 * *CAUTION*: The caller must lock the sni tree if called in multithreading mode
2746 */
2747static void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf)
2748{
2749
2750 struct sni_ctx *sc0, *sc0b, *sc1;
2751 struct ebmb_node *node;
William Lallemand21724f02019-11-04 17:56:13 +01002752 int def = 0;
William Lallemand1d29c742019-10-04 00:53:29 +02002753
2754 list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) {
2755
2756 /* ignore if sc0 was already inserted in a tree */
2757 if (sc0->name.node.leaf_p)
2758 continue;
2759
2760 /* Check for duplicates. */
2761 if (sc0->wild)
2762 node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key);
2763 else
2764 node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key);
2765
2766 for (; node; node = ebmb_next_dup(node)) {
2767 sc1 = ebmb_entry(node, struct sni_ctx, name);
2768 if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf
2769 && sc1->neg == sc0->neg && sc1->wild == sc0->wild) {
2770 /* it's a duplicate, we should remove and free it */
2771 LIST_DEL(&sc0->by_ckch_inst);
William Lallemand02e19a52020-04-08 16:11:26 +02002772 SSL_CTX_free(sc0->ctx);
William Lallemand1d29c742019-10-04 00:53:29 +02002773 free(sc0);
2774 sc0 = NULL;
William Lallemande15029b2019-10-14 10:46:58 +02002775 break;
William Lallemand1d29c742019-10-04 00:53:29 +02002776 }
2777 }
2778
2779 /* if duplicate, ignore the insertion */
2780 if (!sc0)
2781 continue;
2782
2783 if (sc0->wild)
2784 ebst_insert(&bind_conf->sni_w_ctx, &sc0->name);
2785 else
2786 ebst_insert(&bind_conf->sni_ctx, &sc0->name);
William Lallemand21724f02019-11-04 17:56:13 +01002787
2788 /* replace the default_ctx if required with the first ctx */
2789 if (ckch_inst->is_default && !def) {
William Lallemand02e19a52020-04-08 16:11:26 +02002790 SSL_CTX_free(bind_conf->default_ctx);
2791 SSL_CTX_up_ref(sc0->ctx);
William Lallemand21724f02019-11-04 17:56:13 +01002792 bind_conf->default_ctx = sc0->ctx;
2793 def = 1;
2794 }
William Lallemand1d29c742019-10-04 00:53:29 +02002795 }
2796}
2797
2798/*
William Lallemande3af8fb2019-10-08 11:36:53 +02002799 * tree used to store the ckchs ordered by filename/bundle name
William Lallemand6af03992019-07-23 15:00:54 +02002800 */
William Lallemande3af8fb2019-10-08 11:36:53 +02002801struct eb_root ckchs_tree = EB_ROOT_UNIQUE;
William Lallemand6af03992019-07-23 15:00:54 +02002802
William Lallemand2954c472020-03-06 21:54:13 +01002803/* tree of crtlist (crt-list/directory) */
2804static struct eb_root crtlists_tree = EB_ROOT_UNIQUE;
William Lallemandfa892222019-07-23 16:06:08 +02002805
Emeric Brun7a883362019-10-17 13:27:40 +02002806/* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX.
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05002807 * If there is no DH parameter available in the ckchs, the global
Emeric Brun7a883362019-10-17 13:27:40 +02002808 * DH parameter is loaded into the SSL_CTX and if there is no
2809 * DH parameter available in ckchs nor in global, the default
2810 * DH parameters are applied on the SSL_CTX.
2811 * Returns a bitfield containing the flags:
2812 * ERR_FATAL in any fatal error case
2813 * ERR_ALERT if a reason of the error is availabine in err
2814 * ERR_WARN if a warning is available into err
2815 * The value 0 means there is no error nor warning and
2816 * the operation succeed.
2817 */
William Lallemandfa892222019-07-23 16:06:08 +02002818#ifndef OPENSSL_NO_DH
Emeric Brun7a883362019-10-17 13:27:40 +02002819static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch,
2820 const char *path, char **err)
William Lallemandfa892222019-07-23 16:06:08 +02002821{
Emeric Brun7a883362019-10-17 13:27:40 +02002822 int ret = 0;
William Lallemandfa892222019-07-23 16:06:08 +02002823 DH *dh = NULL;
2824
William Lallemanda8c73742019-07-31 18:31:34 +02002825 if (ckch && ckch->dh) {
William Lallemandfa892222019-07-23 16:06:08 +02002826 dh = ckch->dh;
Emeric Bruna9363eb2019-10-17 14:53:03 +02002827 if (!SSL_CTX_set_tmp_dh(ctx, dh)) {
2828 memprintf(err, "%sunable to load the DH parameter specified in '%s'",
2829 err && *err ? *err : "", path);
2830#if defined(SSL_CTX_set_dh_auto)
2831 SSL_CTX_set_dh_auto(ctx, 1);
2832 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
2833 err && *err ? *err : "");
2834#else
2835 memprintf(err, "%s, DH ciphers won't be available.\n",
2836 err && *err ? *err : "");
2837#endif
2838 ret |= ERR_WARN;
2839 goto end;
2840 }
William Lallemandfa892222019-07-23 16:06:08 +02002841
2842 if (ssl_dh_ptr_index >= 0) {
2843 /* store a pointer to the DH params to avoid complaining about
2844 ssl-default-dh-param not being set for this SSL_CTX */
2845 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh);
2846 }
2847 }
2848 else if (global_dh) {
Emeric Bruna9363eb2019-10-17 14:53:03 +02002849 if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) {
2850 memprintf(err, "%sunable to use the global DH parameter for certificate '%s'",
2851 err && *err ? *err : "", path);
2852#if defined(SSL_CTX_set_dh_auto)
2853 SSL_CTX_set_dh_auto(ctx, 1);
2854 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
2855 err && *err ? *err : "");
2856#else
2857 memprintf(err, "%s, DH ciphers won't be available.\n",
2858 err && *err ? *err : "");
2859#endif
2860 ret |= ERR_WARN;
2861 goto end;
2862 }
William Lallemandfa892222019-07-23 16:06:08 +02002863 }
2864 else {
2865 /* Clear openssl global errors stack */
2866 ERR_clear_error();
2867
2868 if (global_ssl.default_dh_param <= 1024) {
2869 /* we are limited to DH parameter of 1024 bits anyway */
2870 if (local_dh_1024 == NULL)
2871 local_dh_1024 = ssl_get_dh_1024();
2872
Emeric Brun7a883362019-10-17 13:27:40 +02002873 if (local_dh_1024 == NULL) {
2874 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
2875 err && *err ? *err : "", path);
2876 ret |= ERR_ALERT | ERR_FATAL;
William Lallemandfa892222019-07-23 16:06:08 +02002877 goto end;
Emeric Brun7a883362019-10-17 13:27:40 +02002878 }
William Lallemandfa892222019-07-23 16:06:08 +02002879
Emeric Bruna9363eb2019-10-17 14:53:03 +02002880 if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) {
2881 memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n",
2882 err && *err ? *err : "", path);
2883#if defined(SSL_CTX_set_dh_auto)
2884 SSL_CTX_set_dh_auto(ctx, 1);
2885 memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n",
2886 err && *err ? *err : "");
2887#else
2888 memprintf(err, "%s, DH ciphers won't be available.\n",
2889 err && *err ? *err : "");
2890#endif
2891 ret |= ERR_WARN;
2892 goto end;
2893 }
William Lallemandfa892222019-07-23 16:06:08 +02002894 }
2895 else {
2896 SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh);
2897 }
William Lallemand8d0f8932019-10-17 18:03:58 +02002898 }
2899
William Lallemandf9568fc2019-10-16 18:27:58 +02002900end:
William Lallemandf9568fc2019-10-16 18:27:58 +02002901 ERR_clear_error();
William Lallemandf9568fc2019-10-16 18:27:58 +02002902 return ret;
2903}
Emmanuel Hocdet54227d82019-07-30 17:04:01 +02002904#endif
William Lallemandfa892222019-07-23 16:06:08 +02002905
yanbzhu488a4d22015-12-01 15:16:07 -05002906/* Loads the info in ckch into ctx
Emeric Bruna96b5822019-10-17 13:25:14 +02002907 * Returns a bitfield containing the flags:
2908 * ERR_FATAL in any fatal error case
2909 * ERR_ALERT if the reason of the error is available in err
2910 * ERR_WARN if a warning is available into err
2911 * The value 0 means there is no error nor warning and
2912 * the operation succeed.
yanbzhu488a4d22015-12-01 15:16:07 -05002913 */
2914static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err)
2915{
Emeric Bruna96b5822019-10-17 13:25:14 +02002916 int errcode = 0;
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01002917 STACK_OF(X509) *find_chain = NULL;
Emeric Bruna96b5822019-10-17 13:25:14 +02002918
yanbzhu488a4d22015-12-01 15:16:07 -05002919 if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) {
2920 memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n",
2921 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02002922 errcode |= ERR_ALERT | ERR_FATAL;
2923 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05002924 }
2925
2926 if (!SSL_CTX_use_certificate(ctx, ckch->cert)) {
2927 memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n",
2928 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02002929 errcode |= ERR_ALERT | ERR_FATAL;
2930 goto end;
yanbzhu488a4d22015-12-01 15:16:07 -05002931 }
2932
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01002933 if (ckch->chain) {
2934 find_chain = ckch->chain;
2935 } else {
2936 /* Find Certificate Chain in global */
2937 struct issuer_chain *issuer;
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +01002938 issuer = ssl_get0_issuer_chain(ckch->cert);
Emmanuel Hocdetb90d2cb2020-02-18 15:27:32 +01002939 if (issuer)
2940 find_chain = issuer->chain;
2941 }
William Lallemand85888572020-02-27 14:48:35 +01002942
Emmanuel Hocdet16739772020-02-28 16:00:34 +01002943 /* Load all certs from chain, except Root, in the ssl_ctx */
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01002944 if (find_chain) {
2945 int i;
2946 X509 *ca;
2947 for (i = 0; i < sk_X509_num(find_chain); i++) {
2948 ca = sk_X509_value(find_chain, i);
Emmanuel Hocdet16739772020-02-28 16:00:34 +01002949 /* skip self issued (Root CA) */
Emmanuel Hocdetc3b7e742020-04-22 11:06:19 +02002950 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 +01002951 continue;
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01002952 /*
2953 SSL_CTX_add1_chain_cert could be used with openssl >= 1.0.2
2954 Used SSL_CTX_add_extra_chain_cert for compat (aka SSL_CTX_add0_chain_cert)
2955 */
2956 X509_up_ref(ca);
2957 if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) {
2958 X509_free(ca);
2959 memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n",
2960 err && *err ? *err : "", path);
2961 errcode |= ERR_ALERT | ERR_FATAL;
2962 goto end;
2963 }
Emmanuel Hocdetf4f14ea2020-03-23 10:31:47 +01002964 }
Emmanuel Hocdet4fed93e2020-02-28 16:00:34 +01002965 }
yanbzhu488a4d22015-12-01 15:16:07 -05002966
William Lallemandfa892222019-07-23 16:06:08 +02002967#ifndef OPENSSL_NO_DH
2968 /* store a NULL pointer to indicate we have not yet loaded
2969 a custom DH param file */
2970 if (ssl_dh_ptr_index >= 0) {
2971 SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL);
2972 }
2973
Emeric Brun7a883362019-10-17 13:27:40 +02002974 errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err);
2975 if (errcode & ERR_CODE) {
William Lallemandfa892222019-07-23 16:06:08 +02002976 memprintf(err, "%sunable to load DH parameters from file '%s'.\n",
2977 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02002978 goto end;
William Lallemandfa892222019-07-23 16:06:08 +02002979 }
2980#endif
2981
William Lallemanda17f4112019-10-10 15:16:44 +02002982#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
2983 if (sctl_ex_index >= 0 && ckch->sctl) {
2984 if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) {
2985 memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01002986 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02002987 errcode |= ERR_ALERT | ERR_FATAL;
2988 goto end;
William Lallemanda17f4112019-10-10 15:16:44 +02002989 }
2990 }
2991#endif
2992
William Lallemand4a660132019-10-14 14:51:41 +02002993#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand246c0242019-10-11 08:59:13 +02002994 /* Load OCSP Info into context */
2995 if (ckch->ocsp_response) {
Emmanuel Hocdet6f507c72020-02-18 15:56:39 +01002996 if (ssl_sock_load_ocsp(ctx, ckch, find_chain) < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01002997 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",
2998 err && *err ? *err : "", path);
Emeric Bruna96b5822019-10-17 13:25:14 +02002999 errcode |= ERR_ALERT | ERR_FATAL;
3000 goto end;
William Lallemand246c0242019-10-11 08:59:13 +02003001 }
3002 }
William Lallemand246c0242019-10-11 08:59:13 +02003003#endif
3004
Emeric Bruna96b5822019-10-17 13:25:14 +02003005 end:
3006 return errcode;
yanbzhu488a4d22015-12-01 15:16:07 -05003007}
3008
William Lallemandc4ecddf2019-07-31 16:50:08 +02003009#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
yanbzhu08ce6ab2015-12-02 13:01:29 -05003010
William Lallemand28a8fce2019-10-04 17:36:55 +02003011static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003012{
3013 struct sni_keytype *s_kt = NULL;
3014 struct ebmb_node *node;
3015 int i;
3016
3017 for (i = 0; i < trash.size; i++) {
3018 if (!str[i])
3019 break;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003020 trash.area[i] = tolower(str[i]);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003021 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003022 trash.area[i] = 0;
3023 node = ebst_lookup(sni_keytypes, trash.area);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003024 if (!node) {
3025 /* CN not found in tree */
3026 s_kt = malloc(sizeof(struct sni_keytype) + i + 1);
3027 /* Using memcpy here instead of strncpy.
3028 * strncpy will cause sig_abrt errors under certain versions of gcc with -O2
3029 * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792
3030 */
William Lallemand28a8fce2019-10-04 17:36:55 +02003031 if (!s_kt)
3032 return -1;
3033
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003034 memcpy(s_kt->name.key, trash.area, i+1);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003035 s_kt->keytypes = 0;
3036 ebst_insert(sni_keytypes, &s_kt->name);
3037 } else {
3038 /* CN found in tree */
3039 s_kt = container_of(node, struct sni_keytype, name);
3040 }
3041
3042 /* Mark that this CN has the keytype of key_index via keytypes mask */
3043 s_kt->keytypes |= 1<<key_index;
3044
William Lallemand28a8fce2019-10-04 17:36:55 +02003045 return 0;
3046
William Lallemand6af03992019-07-23 15:00:54 +02003047}
3048
William Lallemandc4ecddf2019-07-31 16:50:08 +02003049#endif
William Lallemand36b84632019-07-18 19:28:17 +02003050
William Lallemandc4ecddf2019-07-31 16:50:08 +02003051#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
3052
William Lallemand36b84632019-07-18 19:28:17 +02003053/*
William Lallemande3af8fb2019-10-08 11:36:53 +02003054 * Take a ckch_store which contains a multi-certificate bundle.
William Lallemand36b84632019-07-18 19:28:17 +02003055 * Group these certificates into a set of SSL_CTX*
yanbzhu08ce6ab2015-12-02 13:01:29 -05003056 * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree.
3057 *
Joseph Herlant017b3da2018-11-15 09:07:59 -08003058 * This will allow the user to explicitly group multiple cert/keys for a single purpose
yanbzhu08ce6ab2015-12-02 13:01:29 -05003059 *
Emeric Brun054563d2019-10-17 13:16:58 +02003060 * Returns a bitfield containing the flags:
3061 * ERR_FATAL in any fatal error case
3062 * ERR_ALERT if the reason of the error is available in err
3063 * ERR_WARN if a warning is available into err
William Lallemand36b84632019-07-18 19:28:17 +02003064 *
yanbzhu08ce6ab2015-12-02 13:01:29 -05003065 */
Emeric Brun054563d2019-10-17 13:16:58 +02003066static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
3067 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3068 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003069{
William Lallemand36b84632019-07-18 19:28:17 +02003070 int i = 0, n = 0;
3071 struct cert_key_and_chain *certs_and_keys;
William Lallemand4b989f22019-10-04 18:36:55 +02003072 struct eb_root sni_keytypes_map = EB_ROOT;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003073 struct ebmb_node *node;
3074 struct ebmb_node *next;
3075 /* Array of SSL_CTX pointers corresponding to each possible combo
3076 * of keytypes
3077 */
3078 struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} };
Emeric Brun054563d2019-10-17 13:16:58 +02003079 int errcode = 0;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003080 X509_NAME *xname = NULL;
3081 char *str = NULL;
3082#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3083 STACK_OF(GENERAL_NAME) *names = NULL;
3084#endif
William Lallemand614ca0d2019-10-07 13:52:11 +02003085 struct ckch_inst *ckch_inst;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003086
Emeric Brun054563d2019-10-17 13:16:58 +02003087 *ckchi = NULL;
3088
William Lallemande3af8fb2019-10-08 11:36:53 +02003089 if (!ckchs || !ckchs->ckch || !ckchs->multi) {
William Lallemand36b84632019-07-18 19:28:17 +02003090 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3091 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003092 return ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003093 }
3094
3095 ckch_inst = ckch_inst_new();
3096 if (!ckch_inst) {
3097 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3098 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003099 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand614ca0d2019-10-07 13:52:11 +02003100 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003101 }
3102
William Lallemande3af8fb2019-10-08 11:36:53 +02003103 certs_and_keys = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02003104
yanbzhu08ce6ab2015-12-02 13:01:29 -05003105 /* Process each ckch and update keytypes for each CN/SAN
3106 * for example, if CN/SAN www.a.com is associated with
3107 * certs with keytype 0 and 2, then at the end of the loop,
3108 * www.a.com will have:
3109 * keyindex = 0 | 1 | 4 = 5
3110 */
3111 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003112 int ret;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003113
3114 if (!ssl_sock_is_ckch_valid(&certs_and_keys[n]))
3115 continue;
3116
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003117 if (fcount) {
William Lallemand28a8fce2019-10-04 17:36:55 +02003118 for (i = 0; i < fcount; i++) {
3119 ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n);
3120 if (ret < 0) {
3121 memprintf(err, "%sunable to allocate SSL context.\n",
3122 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003123 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003124 goto end;
3125 }
3126 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003127 } else {
3128 /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's,
3129 * so the line that contains logic is marked via comments
3130 */
3131 xname = X509_get_subject_name(certs_and_keys[n].cert);
3132 i = -1;
3133 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3134 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003135 ASN1_STRING *value;
3136 value = X509_NAME_ENTRY_get_data(entry);
3137 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003138 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02003139 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003140
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003141 OPENSSL_free(str);
3142 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003143 if (ret < 0) {
3144 memprintf(err, "%sunable to allocate SSL context.\n",
3145 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003146 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003147 goto end;
3148 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003149 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003150 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003151
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003152 /* Do the above logic for each SAN */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003153#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003154 names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL);
3155 if (names) {
3156 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3157 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003158
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003159 if (name->type == GEN_DNS) {
3160 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
3161 /* Important line is here */
William Lallemand28a8fce2019-10-04 17:36:55 +02003162 ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003163
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003164 OPENSSL_free(str);
3165 str = NULL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003166 if (ret < 0) {
3167 memprintf(err, "%sunable to allocate SSL context.\n",
3168 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003169 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand28a8fce2019-10-04 17:36:55 +02003170 goto end;
3171 }
Emmanuel Hocdetd294aea2016-05-13 11:14:06 +02003172 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003173 }
3174 }
3175 }
3176 }
3177#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
3178 }
3179
3180 /* If no files found, return error */
3181 if (eb_is_empty(&sni_keytypes_map)) {
3182 memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n",
3183 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003184 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003185 goto end;
3186 }
3187
3188 /* We now have a map of CN/SAN to keytypes that are loaded in
3189 * Iterate through the map to create the SSL_CTX's (if needed)
3190 * and add each CTX to the SNI tree
3191 *
3192 * Some math here:
Joseph Herlant017b3da2018-11-15 09:07:59 -08003193 * There are 2^n - 1 possible combinations, each unique
yanbzhu08ce6ab2015-12-02 13:01:29 -05003194 * combination is denoted by the key in the map. Each key
3195 * has a value between 1 and 2^n - 1. Conveniently, the array
3196 * of SSL_CTX* is sized 2^n. So, we can simply use the i'th
3197 * entry in the array to correspond to the unique combo (key)
3198 * associated with i. This unique key combo (i) will be associated
3199 * with combos[i-1]
3200 */
3201
3202 node = ebmb_first(&sni_keytypes_map);
3203 while (node) {
3204 SSL_CTX *cur_ctx;
Bertrand Jacquin33423092016-11-13 16:37:13 +00003205 char cur_file[MAXPATHLEN+1];
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003206 const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
yanbzhu08ce6ab2015-12-02 13:01:29 -05003207
3208 str = (char *)container_of(node, struct sni_keytype, name)->name.key;
3209 i = container_of(node, struct sni_keytype, name)->keytypes;
3210 cur_ctx = key_combos[i-1].ctx;
3211
3212 if (cur_ctx == NULL) {
3213 /* need to create SSL_CTX */
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003214 cur_ctx = SSL_CTX_new(SSLv23_server_method());
yanbzhu08ce6ab2015-12-02 13:01:29 -05003215 if (cur_ctx == NULL) {
3216 memprintf(err, "%sunable to allocate SSL context.\n",
3217 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003218 errcode |= ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003219 goto end;
3220 }
3221
yanbzhube2774d2015-12-10 15:07:30 -05003222 /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */
yanbzhu08ce6ab2015-12-02 13:01:29 -05003223 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
3224 if (i & (1<<n)) {
3225 /* Key combo contains ckch[n] */
Bertrand Jacquin33423092016-11-13 16:37:13 +00003226 snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]);
Emeric Bruna96b5822019-10-17 13:25:14 +02003227 errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err);
3228 if (errcode & ERR_CODE)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003229 goto end;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003230 }
3231 }
3232
yanbzhu08ce6ab2015-12-02 13:01:29 -05003233 /* Update key_combos */
3234 key_combos[i-1].ctx = cur_ctx;
3235 }
3236
3237 /* Update SNI Tree */
William Lallemand9117de92019-10-04 00:29:42 +02003238
William Lallemand1d29c742019-10-04 00:53:29 +02003239 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 +02003240 kinfo, str, key_combos[i-1].order);
3241 if (key_combos[i-1].order < 0) {
3242 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003243 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandfe49bb32019-10-03 23:46:33 +02003244 goto end;
3245 }
yanbzhu08ce6ab2015-12-02 13:01:29 -05003246 node = ebmb_next(node);
3247 }
3248
3249
3250 /* Mark a default context if none exists, using the ctx that has the most shared keys */
3251 if (!bind_conf->default_ctx) {
3252 for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) {
3253 if (key_combos[i].ctx) {
3254 bind_conf->default_ctx = key_combos[i].ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003255 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01003256 ckch_inst->is_default = 1;
William Lallemand02e19a52020-04-08 16:11:26 +02003257 SSL_CTX_up_ref(bind_conf->default_ctx);
yanbzhu08ce6ab2015-12-02 13:01:29 -05003258 break;
3259 }
3260 }
3261 }
3262
William Lallemand614ca0d2019-10-07 13:52:11 +02003263 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02003264 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01003265 ckch_inst->ckch_store = ckchs;
William Lallemand02e19a52020-04-08 16:11:26 +02003266
yanbzhu08ce6ab2015-12-02 13:01:29 -05003267end:
3268
3269 if (names)
3270 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
3271
yanbzhu08ce6ab2015-12-02 13:01:29 -05003272 node = ebmb_first(&sni_keytypes_map);
3273 while (node) {
3274 next = ebmb_next(node);
3275 ebmb_delete(node);
William Lallemand8ed5b962019-10-04 17:24:39 +02003276 free(ebmb_entry(node, struct sni_keytype, name));
yanbzhu08ce6ab2015-12-02 13:01:29 -05003277 node = next;
3278 }
3279
William Lallemand02e19a52020-04-08 16:11:26 +02003280 /* we need to free the ctx since we incremented the refcount where it's used */
3281 for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) {
3282 if (key_combos[i].ctx)
3283 SSL_CTX_free(key_combos[i].ctx);
3284 }
3285
Emeric Brun054563d2019-10-17 13:16:58 +02003286 if (errcode & ERR_CODE && ckch_inst) {
William Lallemand02e19a52020-04-08 16:11:26 +02003287 if (ckch_inst->is_default) {
3288 SSL_CTX_free(bind_conf->default_ctx);
3289 bind_conf->default_ctx = NULL;
3290 }
3291
William Lallemandd9d5d1b2020-04-09 16:31:05 +02003292 ckch_inst_free(ckch_inst);
William Lallemand614ca0d2019-10-07 13:52:11 +02003293 ckch_inst = NULL;
William Lallemand0c6d12f2019-10-04 18:38:51 +02003294 }
3295
Emeric Brun054563d2019-10-17 13:16:58 +02003296 *ckchi = ckch_inst;
3297 return errcode;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003298}
3299#else
3300/* This is a dummy, that just logs an error and returns error */
Emeric Brun054563d2019-10-17 13:16:58 +02003301static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs,
3302 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
3303 char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
yanbzhu08ce6ab2015-12-02 13:01:29 -05003304{
3305 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
3306 err && *err ? *err : "", path, strerror(errno));
Emeric Brun054563d2019-10-17 13:16:58 +02003307 return ERR_ALERT | ERR_FATAL;
yanbzhu08ce6ab2015-12-02 13:01:29 -05003308}
3309
Willy Tarreau9a1ab082019-05-09 13:26:41 +02003310#endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */
yanbzhu488a4d22015-12-01 15:16:07 -05003311
William Lallemand614ca0d2019-10-07 13:52:11 +02003312/*
3313 * This function allocate a ckch_inst and create its snis
Emeric Brun054563d2019-10-17 13:16:58 +02003314 *
3315 * Returns a bitfield containing the flags:
3316 * ERR_FATAL in any fatal error case
3317 * ERR_ALERT if the reason of the error is available in err
3318 * ERR_WARN if a warning is available into err
William Lallemand614ca0d2019-10-07 13:52:11 +02003319 */
Emeric Brun054563d2019-10-17 13:16:58 +02003320static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf,
3321 struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003322{
William Lallemandc9402072019-05-15 15:33:54 +02003323 SSL_CTX *ctx;
William Lallemandc9402072019-05-15 15:33:54 +02003324 int i;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003325 int order = 0;
3326 X509_NAME *xname;
3327 char *str;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003328 EVP_PKEY *pkey;
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003329 struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 };
Emeric Brunfc0421f2012-09-07 17:30:07 +02003330#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3331 STACK_OF(GENERAL_NAME) *names;
3332#endif
William Lallemand36b84632019-07-18 19:28:17 +02003333 struct cert_key_and_chain *ckch;
William Lallemand614ca0d2019-10-07 13:52:11 +02003334 struct ckch_inst *ckch_inst = NULL;
Emeric Brun054563d2019-10-17 13:16:58 +02003335 int errcode = 0;
3336
3337 *ckchi = NULL;
William Lallemanda59191b2019-05-15 16:08:56 +02003338
William Lallemande3af8fb2019-10-08 11:36:53 +02003339 if (!ckchs || !ckchs->ckch)
Emeric Brun054563d2019-10-17 13:16:58 +02003340 return ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003341
William Lallemande3af8fb2019-10-08 11:36:53 +02003342 ckch = ckchs->ckch;
William Lallemand36b84632019-07-18 19:28:17 +02003343
William Lallemandc9402072019-05-15 15:33:54 +02003344 ctx = SSL_CTX_new(SSLv23_server_method());
3345 if (!ctx) {
3346 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3347 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003348 errcode |= ERR_ALERT | ERR_FATAL;
3349 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02003350 }
3351
Emeric Bruna96b5822019-10-17 13:25:14 +02003352 errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err);
3353 if (errcode & ERR_CODE)
William Lallemand614ca0d2019-10-07 13:52:11 +02003354 goto error;
William Lallemand614ca0d2019-10-07 13:52:11 +02003355
3356 ckch_inst = ckch_inst_new();
3357 if (!ckch_inst) {
3358 memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n",
3359 err && *err ? *err : "", path);
Emeric Brun054563d2019-10-17 13:16:58 +02003360 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02003361 goto error;
William Lallemandc9402072019-05-15 15:33:54 +02003362 }
3363
William Lallemand36b84632019-07-18 19:28:17 +02003364 pkey = X509_get_pubkey(ckch->cert);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003365 if (pkey) {
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003366 kinfo.bits = EVP_PKEY_bits(pkey);
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003367 switch(EVP_PKEY_base_id(pkey)) {
3368 case EVP_PKEY_RSA:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003369 kinfo.sig = TLSEXT_signature_rsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003370 break;
3371 case EVP_PKEY_EC:
Emmanuel Hocdetddc090b2017-10-27 18:43:29 +02003372 kinfo.sig = TLSEXT_signature_ecdsa;
3373 break;
3374 case EVP_PKEY_DSA:
3375 kinfo.sig = TLSEXT_signature_dsa;
Emmanuel Hocdet05942112017-02-20 16:11:50 +01003376 break;
3377 }
3378 EVP_PKEY_free(pkey);
3379 }
3380
Emeric Brun50bcecc2013-04-22 13:05:23 +02003381 if (fcount) {
William Lallemandfe49bb32019-10-03 23:46:33 +02003382 while (fcount--) {
William Lallemand1d29c742019-10-04 00:53:29 +02003383 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 +02003384 if (order < 0) {
3385 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003386 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02003387 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02003388 }
3389 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003390 }
3391 else {
Emeric Brunfc0421f2012-09-07 17:30:07 +02003392#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
William Lallemand36b84632019-07-18 19:28:17 +02003393 names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003394 if (names) {
3395 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
3396 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
3397 if (name->type == GEN_DNS) {
3398 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02003399 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003400 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02003401 if (order < 0) {
3402 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003403 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02003404 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02003405 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003406 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003407 }
3408 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003409 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
Emeric Brunfc0421f2012-09-07 17:30:07 +02003410 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003411#endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */
William Lallemand36b84632019-07-18 19:28:17 +02003412 xname = X509_get_subject_name(ckch->cert);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003413 i = -1;
3414 while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) {
3415 X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02003416 ASN1_STRING *value;
3417
3418 value = X509_NAME_ENTRY_get_data(entry);
3419 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
William Lallemand1d29c742019-10-04 00:53:29 +02003420 order = ckch_inst_add_cert_sni(ctx, ckch_inst, bind_conf, ssl_conf, kinfo, str, order);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003421 OPENSSL_free(str);
William Lallemandfe49bb32019-10-03 23:46:33 +02003422 if (order < 0) {
3423 memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003424 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02003425 goto error;
William Lallemandfe49bb32019-10-03 23:46:33 +02003426 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003427 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003428 }
3429 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003430 /* we must not free the SSL_CTX anymore below, since it's already in
3431 * the tree, so it will be discovered and cleaned in time.
3432 */
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003433
Emeric Brunfc0421f2012-09-07 17:30:07 +02003434#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003435 if (bind_conf->default_ctx) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02003436 memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n",
3437 err && *err ? *err : "");
Emeric Brun054563d2019-10-17 13:16:58 +02003438 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemandd9199372019-10-04 15:37:05 +02003439 goto error;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003440 }
3441#endif
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003442 if (!bind_conf->default_ctx) {
Willy Tarreau2a65ff02012-09-13 17:54:29 +02003443 bind_conf->default_ctx = ctx;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003444 bind_conf->default_ssl_conf = ssl_conf;
William Lallemand21724f02019-11-04 17:56:13 +01003445 ckch_inst->is_default = 1;
William Lallemand02e19a52020-04-08 16:11:26 +02003446 SSL_CTX_up_ref(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01003447 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02003448
William Lallemand9117de92019-10-04 00:29:42 +02003449 /* everything succeed, the ckch instance can be used */
3450 ckch_inst->bind_conf = bind_conf;
William Lallemand150bfa82019-09-19 17:12:49 +02003451 ckch_inst->ssl_conf = ssl_conf;
William Lallemandcfca1422020-03-05 10:17:47 +01003452 ckch_inst->ckch_store = ckchs;
William Lallemand9117de92019-10-04 00:29:42 +02003453
William Lallemand02e19a52020-04-08 16:11:26 +02003454 SSL_CTX_free(ctx); /* we need to free the ctx since we incremented the refcount where it's used */
3455
Emeric Brun054563d2019-10-17 13:16:58 +02003456 *ckchi = ckch_inst;
3457 return errcode;
William Lallemandd9199372019-10-04 15:37:05 +02003458
3459error:
3460 /* free the allocated sni_ctxs */
William Lallemand614ca0d2019-10-07 13:52:11 +02003461 if (ckch_inst) {
William Lallemand02e19a52020-04-08 16:11:26 +02003462 if (ckch_inst->is_default)
3463 SSL_CTX_free(ctx);
3464
William Lallemandd9d5d1b2020-04-09 16:31:05 +02003465 ckch_inst_free(ckch_inst);
William Lallemand614ca0d2019-10-07 13:52:11 +02003466 ckch_inst = NULL;
William Lallemandd9199372019-10-04 15:37:05 +02003467 }
William Lallemandd9199372019-10-04 15:37:05 +02003468 SSL_CTX_free(ctx);
3469
Emeric Brun054563d2019-10-17 13:16:58 +02003470 return errcode;
Emeric Brunfc0421f2012-09-07 17:30:07 +02003471}
3472
Willy Tarreau8c5414a2019-10-16 17:06:25 +02003473/* Returns a set of ERR_* flags possibly with an error in <err>. */
William Lallemand614ca0d2019-10-07 13:52:11 +02003474static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs,
3475 struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf,
William Lallemand24bde432020-03-09 16:48:43 +01003476 char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err)
William Lallemand614ca0d2019-10-07 13:52:11 +02003477{
Emeric Brun054563d2019-10-17 13:16:58 +02003478 int errcode = 0;
William Lallemand614ca0d2019-10-07 13:52:11 +02003479
3480 /* we found the ckchs in the tree, we can use it directly */
3481 if (ckchs->multi)
William Lallemand24bde432020-03-09 16:48:43 +01003482 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 +02003483 else
William Lallemand24bde432020-03-09 16:48:43 +01003484 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 +02003485
Emeric Brun054563d2019-10-17 13:16:58 +02003486 if (errcode & ERR_CODE)
3487 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02003488
William Lallemand24bde432020-03-09 16:48:43 +01003489 ssl_sock_load_cert_sni(*ckch_inst, bind_conf);
William Lallemand614ca0d2019-10-07 13:52:11 +02003490
3491 /* succeed, add the instance to the ckch_store's list of instance */
William Lallemand24bde432020-03-09 16:48:43 +01003492 LIST_ADDQ(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs));
Emeric Brun054563d2019-10-17 13:16:58 +02003493 return errcode;
William Lallemand614ca0d2019-10-07 13:52:11 +02003494}
3495
William Lallemand6be66ec2020-03-06 22:26:32 +01003496
William Lallemand4c68bba2020-03-30 18:45:10 +02003497
3498
3499/* Make sure openssl opens /dev/urandom before the chroot. The work is only
3500 * done once. Zero is returned if the operation fails. No error is returned
3501 * if the random is said as not implemented, because we expect that openssl
3502 * will use another method once needed.
3503 */
3504static int ssl_initialize_random()
3505{
3506 unsigned char random;
3507 static int random_initialized = 0;
3508
3509 if (!random_initialized && RAND_bytes(&random, 1) != 0)
3510 random_initialized = 1;
3511
3512 return random_initialized;
3513}
3514
William Lallemand2954c472020-03-06 21:54:13 +01003515/* Load a crt-list file, this is done in 2 parts:
3516 * - store the content of the file in a crtlist structure with crtlist_entry structures
3517 * - generate the instances by iterating on entries in the crtlist struct
3518 *
3519 * Nothing is locked there, this function is used in the configuration parser.
3520 *
3521 * Returns a set of ERR_* flags possibly with an error in <err>.
3522 */
William Lallemand6be66ec2020-03-06 22:26:32 +01003523int 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 +01003524{
3525 struct crtlist *crtlist = NULL;
3526 struct ebmb_node *eb;
William Lallemand49398312020-03-30 17:01:33 +02003527 struct crtlist_entry *entry = NULL;
William Lallemand79d31ec2020-03-25 15:10:49 +01003528 struct bind_conf_list *bind_conf_node = NULL;
William Lallemand2954c472020-03-06 21:54:13 +01003529 int cfgerr = 0;
William Lallemand41ca9302020-04-08 13:15:18 +02003530 char *end;
William Lallemand2954c472020-03-06 21:54:13 +01003531
William Lallemand79d31ec2020-03-25 15:10:49 +01003532 bind_conf_node = malloc(sizeof(*bind_conf_node));
3533 if (!bind_conf_node) {
3534 memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : "");
3535 cfgerr |= ERR_FATAL | ERR_ALERT;
3536 goto error;
3537 }
3538 bind_conf_node->next = NULL;
3539 bind_conf_node->bind_conf = bind_conf;
3540
William Lallemand41ca9302020-04-08 13:15:18 +02003541 /* strip trailing slashes, including first one */
3542 for (end = file + strlen(file) - 1; end >= file && *end == '/'; end--)
3543 *end = 0;
3544
William Lallemand2954c472020-03-06 21:54:13 +01003545 /* look for an existing crtlist or create one */
3546 eb = ebst_lookup(&crtlists_tree, file);
3547 if (eb) {
3548 crtlist = ebmb_entry(eb, struct crtlist, node);
3549 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01003550 /* load a crt-list OR a directory */
3551 if (dir)
3552 cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err);
3553 else
3554 cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err);
3555
William Lallemand2954c472020-03-06 21:54:13 +01003556 if (!(cfgerr & ERR_CODE))
3557 ebst_insert(&crtlists_tree, &crtlist->node);
3558 }
3559
3560 if (cfgerr & ERR_CODE) {
3561 cfgerr |= ERR_FATAL | ERR_ALERT;
3562 goto error;
3563 }
3564
3565 /* generates ckch instance from the crtlist_entry */
3566 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
3567 struct ckch_store *store;
3568 struct ckch_inst *ckch_inst = NULL;
3569
3570 store = entry->node.key;
3571 cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err);
3572 if (cfgerr & ERR_CODE) {
3573 memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err);
3574 goto error;
3575 }
William Lallemand49398312020-03-30 17:01:33 +02003576 LIST_ADDQ(&entry->ckch_inst, &ckch_inst->by_crtlist_entry);
William Lallemandcaa16192020-04-08 16:29:15 +02003577 ckch_inst->crtlist_entry = entry;
William Lallemand2954c472020-03-06 21:54:13 +01003578 }
William Lallemand2954c472020-03-06 21:54:13 +01003579
William Lallemand79d31ec2020-03-25 15:10:49 +01003580 /* add the bind_conf to the list */
3581 bind_conf_node->next = crtlist->bind_conf;
3582 crtlist->bind_conf = bind_conf_node;
3583
William Lallemand2954c472020-03-06 21:54:13 +01003584 return cfgerr;
3585error:
3586 {
William Lallemand49398312020-03-30 17:01:33 +02003587 struct crtlist_entry *lastentry;
William Lallemand2954c472020-03-06 21:54:13 +01003588 struct ckch_inst *inst, *s_inst;
3589
William Lallemand49398312020-03-30 17:01:33 +02003590 lastentry = entry; /* which entry we tried to generate last */
3591 if (lastentry) {
3592 list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) {
3593 if (entry == lastentry) /* last entry we tried to generate, no need to go further */
3594 break;
3595
3596 list_for_each_entry_safe(inst, s_inst, &entry->ckch_inst, by_crtlist_entry) {
William Lallemand2954c472020-03-06 21:54:13 +01003597
William Lallemand49398312020-03-30 17:01:33 +02003598 /* this was not generated for this bind_conf, skip */
3599 if (inst->bind_conf != bind_conf)
3600 continue;
3601
William Lallemandd9d5d1b2020-04-09 16:31:05 +02003602 /* free the sni_ctx and instance */
3603 ckch_inst_free(inst);
William Lallemand49398312020-03-30 17:01:33 +02003604 }
William Lallemand2954c472020-03-06 21:54:13 +01003605 }
William Lallemand2954c472020-03-06 21:54:13 +01003606 }
William Lallemand79d31ec2020-03-25 15:10:49 +01003607 free(bind_conf_node);
William Lallemand2954c472020-03-06 21:54:13 +01003608 }
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01003609 return cfgerr;
3610}
3611
William Lallemand06b22a82020-03-16 14:45:55 +01003612/* Returns a set of ERR_* flags possibly with an error in <err>. */
3613int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err)
3614{
3615 struct stat buf;
3616 char fp[MAXPATHLEN+1];
3617 int cfgerr = 0;
3618 struct ckch_store *ckchs;
William Lallemand24bde432020-03-09 16:48:43 +01003619 struct ckch_inst *ckch_inst = NULL;
William Lallemand06b22a82020-03-16 14:45:55 +01003620
3621 if ((ckchs = ckchs_lookup(path))) {
3622 /* we found the ckchs in the tree, we can use it directly */
William Lallemand24bde432020-03-09 16:48:43 +01003623 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01003624 }
3625 if (stat(path, &buf) == 0) {
3626 if (S_ISDIR(buf.st_mode) == 0) {
3627 ckchs = ckchs_load_cert_file(path, 0, err);
3628 if (!ckchs)
3629 return ERR_ALERT | ERR_FATAL;
3630
William Lallemand24bde432020-03-09 16:48:43 +01003631 return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01003632 } else {
William Lallemand6be66ec2020-03-06 22:26:32 +01003633 return ssl_sock_load_cert_list_file(path, 1, bind_conf, bind_conf->frontend, err);
William Lallemand06b22a82020-03-16 14:45:55 +01003634 }
3635 } else {
3636 /* stat failed, could be a bundle */
3637 if (global_ssl.extra_files & SSL_GF_BUNDLE) {
3638 /* try to load a bundle if it is permitted */
3639 ckchs = ckchs_load_cert_file(path, 1, err);
3640 if (!ckchs)
3641 return ERR_ALERT | ERR_FATAL;
William Lallemand24bde432020-03-09 16:48:43 +01003642 cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err);
William Lallemand06b22a82020-03-16 14:45:55 +01003643 } else {
3644 memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n",
3645 err && *err ? *err : "", fp, strerror(errno));
3646 cfgerr |= ERR_ALERT | ERR_FATAL;
3647 }
3648 }
3649
3650 return cfgerr;
3651}
3652
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003653/* Create an initial CTX used to start the SSL connection before switchctx */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003654static int
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003655ssl_sock_initial_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02003656{
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003657 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003658 long options =
Emeric Brunfc0421f2012-09-07 17:30:07 +02003659 SSL_OP_ALL | /* all known workarounds for bugs */
3660 SSL_OP_NO_SSLv2 |
3661 SSL_OP_NO_COMPRESSION |
Emeric Bruna4bcd9a2012-09-20 16:19:02 +02003662 SSL_OP_SINGLE_DH_USE |
Emeric Brun2b58d042012-09-20 17:10:03 +02003663 SSL_OP_SINGLE_ECDH_USE |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02003664 SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
Lukas Tribus926594f2018-05-18 17:55:57 +02003665 SSL_OP_PRIORITIZE_CHACHA |
Emeric Brun3c4bc6e2012-10-04 18:44:19 +02003666 SSL_OP_CIPHER_SERVER_PREFERENCE;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003667 long mode =
Emeric Brunfc0421f2012-09-07 17:30:07 +02003668 SSL_MODE_ENABLE_PARTIAL_WRITE |
3669 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01003670 SSL_MODE_RELEASE_BUFFERS |
3671 SSL_MODE_SMALL_BUFFERS;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02003672 struct tls_version_filter *conf_ssl_methods = &bind_conf->ssl_conf.ssl_methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003673 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003674 int flags = MC_SSL_O_ALL;
3675 int cfgerr = 0;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003676
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003677 ctx = SSL_CTX_new(SSLv23_server_method());
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003678 bind_conf->initial_ctx = ctx;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003679
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003680 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01003681 ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. "
3682 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
3683 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003684 else
3685 flags = conf_ssl_methods->flags;
3686
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02003687 min = conf_ssl_methods->min;
3688 max = conf_ssl_methods->max;
3689 /* start with TLSv10 to remove SSLv3 per default */
3690 if (!min && (!max || max >= CONF_TLSV10))
3691 min = CONF_TLSV10;
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003692 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdetbd695fe2017-05-15 15:53:41 +02003693 if (min)
3694 flags |= (methodVersions[min].flag - 1);
3695 if (max)
3696 flags |= ~((methodVersions[max].flag << 1) - 1);
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003697 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003698 min = max = CONF_TLSV_NONE;
3699 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003700 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003701 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003702 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003703 if (min) {
3704 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003705 ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. "
3706 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
3707 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line,
3708 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003709 hole = 0;
3710 }
3711 max = i;
3712 }
3713 else {
3714 min = max = i;
3715 }
3716 }
3717 else {
3718 if (min)
3719 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003720 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003721 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003722 ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
3723 bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003724 cfgerr += 1;
3725 }
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02003726 /* save real min/max in bind_conf */
3727 conf_ssl_methods->min = min;
3728 conf_ssl_methods->max = max;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003729
Willy Tarreau9a1ab082019-05-09 13:26:41 +02003730#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003731 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08003732 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003733 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003734 methodVersions[min].ctx_set_version(ctx, SET_SERVER);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003735 else
3736 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
3737 if (flags & methodVersions[i].flag)
3738 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003739#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02003740 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02003741 methodVersions[min].ctx_set_version(ctx, SET_MIN);
3742 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emeric Brunfa5c5c82017-04-28 16:19:51 +02003743#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003744
3745 if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS)
3746 options |= SSL_OP_NO_TICKET;
3747 if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH)
3748 options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE;
Dirkjan Bussink526894f2019-01-21 09:35:03 -08003749
3750#ifdef SSL_OP_NO_RENEGOTIATION
3751 options |= SSL_OP_NO_RENEGOTIATION;
3752#endif
3753
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003754 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00003755
Willy Tarreau5db847a2019-05-09 14:13:35 +02003756#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00003757 if (global_ssl.async)
3758 mode |= SSL_MODE_ASYNC;
3759#endif
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02003760 SSL_CTX_set_mode(ctx, mode);
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003761 if (global_ssl.life_time)
3762 SSL_CTX_set_timeout(ctx, global_ssl.life_time);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003763
3764#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
3765#ifdef OPENSSL_IS_BORINGSSL
3766 SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk);
3767 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Ilya Shipitsine9ff8992020-01-19 12:20:14 +05003768#elif defined(SSL_OP_NO_ANTI_REPLAY)
Olivier Houchard545989f2019-12-17 15:39:54 +01003769 if (bind_conf->ssl_conf.early_data)
Olivier Houchard51088ce2019-01-02 18:46:41 +01003770 SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY);
Emmanuel Hocdet84e417d2017-08-16 11:33:17 +02003771 SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL);
3772 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003773#else
3774 SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003775#endif
Emmanuel Hocdet253c62b2017-08-14 11:01:25 +02003776 SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01003777#endif
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02003778 return cfgerr;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01003779}
3780
William Lallemand4f45bb92017-10-30 20:08:51 +01003781
3782static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block)
3783{
3784 if (first == block) {
3785 struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
3786 if (first->len > 0)
3787 sh_ssl_sess_tree_delete(sh_ssl_sess);
3788 }
3789}
3790
3791/* return first block from sh_ssl_sess */
3792static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess)
3793{
3794 return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data);
3795
3796}
3797
3798/* store a session into the cache
3799 * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH
3800 * data: asn1 encoded session
3801 * data_len: asn1 encoded session length
3802 * Returns 1 id session was stored (else 0)
3803 */
3804static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len)
3805{
3806 struct shared_block *first;
3807 struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess;
3808
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02003809 first = shctx_row_reserve_hot(ssl_shctx, NULL, data_len + sizeof(struct sh_ssl_sess_hdr));
William Lallemand4f45bb92017-10-30 20:08:51 +01003810 if (!first) {
3811 /* Could not retrieve enough free blocks to store that session */
3812 return 0;
3813 }
3814
3815 /* STORE the key in the first elem */
3816 sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data;
3817 memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH);
3818 first->len = sizeof(struct sh_ssl_sess_hdr);
3819
3820 /* it returns the already existing node
3821 or current node if none, never returns null */
3822 oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess);
3823 if (oldsh_ssl_sess != sh_ssl_sess) {
3824 /* NOTE: Row couldn't be in use because we lock read & write function */
3825 /* release the reserved row */
3826 shctx_row_dec_hot(ssl_shctx, first);
3827 /* replace the previous session already in the tree */
3828 sh_ssl_sess = oldsh_ssl_sess;
3829 /* ignore the previous session data, only use the header */
3830 first = sh_ssl_sess_first_block(sh_ssl_sess);
3831 shctx_row_inc_hot(ssl_shctx, first);
3832 first->len = sizeof(struct sh_ssl_sess_hdr);
3833 }
3834
Frédéric Lécaille0bec8072018-10-22 17:55:57 +02003835 if (shctx_row_data_append(ssl_shctx, first, NULL, data, data_len) < 0) {
William Lallemand99b90af2018-01-03 19:15:51 +01003836 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01003837 return 0;
William Lallemand99b90af2018-01-03 19:15:51 +01003838 }
3839
3840 shctx_row_dec_hot(ssl_shctx, first);
William Lallemand4f45bb92017-10-30 20:08:51 +01003841
3842 return 1;
3843}
William Lallemanded0b5ad2017-10-30 19:36:36 +01003844
Olivier Houchardbd84ac82017-11-03 13:43:35 +01003845/* SSL callback used when a new session is created while connecting to a server */
3846static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess)
3847{
Thierry FOURNIER28962c92018-06-17 21:37:05 +02003848 struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houcharde6060c52017-11-16 17:42:52 +01003849 struct server *s;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01003850
Willy Tarreau07d94e42018-09-20 10:57:52 +02003851 s = __objt_server(conn->target);
Olivier Houchardbd84ac82017-11-03 13:43:35 +01003852
Olivier Houcharde6060c52017-11-16 17:42:52 +01003853 if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) {
3854 int len;
3855 unsigned char *ptr;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01003856
Olivier Houcharde6060c52017-11-16 17:42:52 +01003857 len = i2d_SSL_SESSION(sess, NULL);
3858 if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) {
3859 ptr = s->ssl_ctx.reused_sess[tid].ptr;
3860 } else {
3861 free(s->ssl_ctx.reused_sess[tid].ptr);
3862 ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len);
3863 s->ssl_ctx.reused_sess[tid].allocated_size = len;
3864 }
3865 if (s->ssl_ctx.reused_sess[tid].ptr) {
3866 s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess,
3867 &ptr);
3868 }
3869 } else {
3870 free(s->ssl_ctx.reused_sess[tid].ptr);
3871 s->ssl_ctx.reused_sess[tid].ptr = NULL;
3872 }
3873
3874 return 0;
Olivier Houchardbd84ac82017-11-03 13:43:35 +01003875}
3876
Olivier Houcharde6060c52017-11-16 17:42:52 +01003877
William Lallemanded0b5ad2017-10-30 19:36:36 +01003878/* SSL callback used on new session creation */
William Lallemand4f45bb92017-10-30 20:08:51 +01003879int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01003880{
3881 unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */
3882 unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */
3883 unsigned char *p;
3884 int data_len;
Emeric Bruneb469652019-10-08 18:27:37 +02003885 unsigned int sid_length;
William Lallemanded0b5ad2017-10-30 19:36:36 +01003886 const unsigned char *sid_data;
William Lallemanded0b5ad2017-10-30 19:36:36 +01003887
3888 /* Session id is already stored in to key and session id is known
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05003889 * so we don't store it to keep size.
Emeric Bruneb469652019-10-08 18:27:37 +02003890 * note: SSL_SESSION_set1_id is using
3891 * a memcpy so we need to use a different pointer
3892 * than sid_data or sid_ctx_data to avoid valgrind
3893 * complaining.
William Lallemanded0b5ad2017-10-30 19:36:36 +01003894 */
3895
3896 sid_data = SSL_SESSION_get_id(sess, &sid_length);
Emeric Bruneb469652019-10-08 18:27:37 +02003897
3898 /* copy value in an other buffer */
3899 memcpy(encid, sid_data, sid_length);
3900
3901 /* pad with 0 */
3902 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH)
3903 memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length);
3904
3905 /* force length to zero to avoid ASN1 encoding */
3906 SSL_SESSION_set1_id(sess, encid, 0);
3907
3908 /* force length to zero to avoid ASN1 encoding */
3909 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0);
William Lallemanded0b5ad2017-10-30 19:36:36 +01003910
3911 /* check if buffer is large enough for the ASN1 encoded session */
3912 data_len = i2d_SSL_SESSION(sess, NULL);
3913 if (data_len > SHSESS_MAX_DATA_LEN)
3914 goto err;
3915
3916 p = encsess;
3917
3918 /* process ASN1 session encoding before the lock */
3919 i2d_SSL_SESSION(sess, &p);
3920
William Lallemanded0b5ad2017-10-30 19:36:36 +01003921
William Lallemanda3c77cf2017-10-30 23:44:40 +01003922 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01003923 /* store to cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01003924 sh_ssl_sess_store(encid, encsess, data_len);
William Lallemanda3c77cf2017-10-30 23:44:40 +01003925 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01003926err:
3927 /* reset original length values */
Emeric Bruneb469652019-10-08 18:27:37 +02003928 SSL_SESSION_set1_id(sess, encid, sid_length);
3929 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
William Lallemanded0b5ad2017-10-30 19:36:36 +01003930
3931 return 0; /* do not increment session reference count */
3932}
3933
3934/* SSL callback used on lookup an existing session cause none found in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01003935SSL_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 +01003936{
William Lallemand4f45bb92017-10-30 20:08:51 +01003937 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01003938 unsigned char data[SHSESS_MAX_DATA_LEN], *p;
3939 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
William Lallemanded0b5ad2017-10-30 19:36:36 +01003940 SSL_SESSION *sess;
William Lallemand4f45bb92017-10-30 20:08:51 +01003941 struct shared_block *first;
William Lallemanded0b5ad2017-10-30 19:36:36 +01003942
3943 global.shctx_lookups++;
3944
3945 /* allow the session to be freed automatically by openssl */
3946 *do_copy = 0;
3947
3948 /* tree key is zeros padded sessionid */
3949 if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) {
3950 memcpy(tmpkey, key, key_len);
3951 memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len);
3952 key = tmpkey;
3953 }
3954
3955 /* lock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01003956 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01003957
3958 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01003959 sh_ssl_sess = sh_ssl_sess_tree_lookup(key);
3960 if (!sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01003961 /* no session found: unlock cache and exit */
William Lallemanda3c77cf2017-10-30 23:44:40 +01003962 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01003963 global.shctx_misses++;
3964 return NULL;
3965 }
3966
William Lallemand4f45bb92017-10-30 20:08:51 +01003967 /* sh_ssl_sess (shared_block->data) is at the end of shared_block */
3968 first = sh_ssl_sess_first_block(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01003969
William Lallemand4f45bb92017-10-30 20:08:51 +01003970 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 +01003971
William Lallemanda3c77cf2017-10-30 23:44:40 +01003972 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01003973
3974 /* decode ASN1 session */
3975 p = data;
William Lallemand4f45bb92017-10-30 20:08:51 +01003976 sess = d2i_SSL_SESSION(NULL, (const unsigned char **)&p, first->len-sizeof(struct sh_ssl_sess_hdr));
William Lallemanded0b5ad2017-10-30 19:36:36 +01003977 /* Reset session id and session id contenxt */
3978 if (sess) {
3979 SSL_SESSION_set1_id(sess, key, key_len);
3980 SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
3981 }
3982
3983 return sess;
3984}
3985
William Lallemand4f45bb92017-10-30 20:08:51 +01003986
William Lallemanded0b5ad2017-10-30 19:36:36 +01003987/* SSL callback used to signal session is no more used in internal cache */
William Lallemand4f45bb92017-10-30 20:08:51 +01003988void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess)
William Lallemanded0b5ad2017-10-30 19:36:36 +01003989{
William Lallemand4f45bb92017-10-30 20:08:51 +01003990 struct sh_ssl_sess_hdr *sh_ssl_sess;
William Lallemanded0b5ad2017-10-30 19:36:36 +01003991 unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH];
3992 unsigned int sid_length;
3993 const unsigned char *sid_data;
3994 (void)ctx;
3995
3996 sid_data = SSL_SESSION_get_id(sess, &sid_length);
3997 /* tree key is zeros padded sessionid */
3998 if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) {
3999 memcpy(tmpkey, sid_data, sid_length);
4000 memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length);
4001 sid_data = tmpkey;
4002 }
4003
William Lallemanda3c77cf2017-10-30 23:44:40 +01004004 shctx_lock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004005
4006 /* lookup for session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004007 sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data);
4008 if (sh_ssl_sess) {
William Lallemanded0b5ad2017-10-30 19:36:36 +01004009 /* free session */
William Lallemand4f45bb92017-10-30 20:08:51 +01004010 sh_ssl_sess_tree_delete(sh_ssl_sess);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004011 }
4012
4013 /* unlock cache */
William Lallemanda3c77cf2017-10-30 23:44:40 +01004014 shctx_unlock(ssl_shctx);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004015}
4016
4017/* Set session cache mode to server and disable openssl internal cache.
4018 * Set shared cache callbacks on an ssl context.
4019 * Shared context MUST be firstly initialized */
William Lallemand4f45bb92017-10-30 20:08:51 +01004020void ssl_set_shctx(SSL_CTX *ctx)
William Lallemanded0b5ad2017-10-30 19:36:36 +01004021{
4022 SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME));
4023
4024 if (!ssl_shctx) {
4025 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
4026 return;
4027 }
4028
4029 SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER |
4030 SSL_SESS_CACHE_NO_INTERNAL |
4031 SSL_SESS_CACHE_NO_AUTO_CLEAR);
4032
4033 /* Set callbacks */
William Lallemand4f45bb92017-10-30 20:08:51 +01004034 SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb);
4035 SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb);
4036 SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb);
William Lallemanded0b5ad2017-10-30 19:36:36 +01004037}
4038
William Lallemand8b453912019-11-21 15:48:10 +01004039/*
4040 * This function applies the SSL configuration on a SSL_CTX
4041 * It returns an error code and fills the <err> buffer
4042 */
4043int 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 +01004044{
4045 struct proxy *curproxy = bind_conf->frontend;
4046 int cfgerr = 0;
4047 int verify = SSL_VERIFY_NONE;
Willy Tarreau5d4cafb2018-01-04 18:55:19 +01004048 struct ssl_bind_conf __maybe_unused *ssl_conf_cur;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004049 const char *conf_ciphers;
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004050#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004051 const char *conf_ciphersuites;
4052#endif
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004053 const char *conf_curves = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004054
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004055 if (ssl_conf) {
4056 struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods;
4057 int i, min, max;
4058 int flags = MC_SSL_O_ALL;
4059
4060 /* Real min and max should be determinate with configuration and openssl's capabilities */
Emmanuel Hocdet43664762017-08-09 18:26:20 +02004061 min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min;
4062 max = conf_ssl_methods->max ? conf_ssl_methods->max : bind_conf->ssl_conf.ssl_methods.max;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004063 if (min)
4064 flags |= (methodVersions[min].flag - 1);
4065 if (max)
4066 flags |= ~((methodVersions[max].flag << 1) - 1);
4067 min = max = CONF_TLSV_NONE;
4068 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4069 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
4070 if (min)
4071 max = i;
4072 else
4073 min = max = i;
4074 }
4075 /* save real min/max */
4076 conf_ssl_methods->min = min;
4077 conf_ssl_methods->max = max;
4078 if (!min) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004079 memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n",
4080 err && *err ? *err : "", bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004081 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02004082 }
4083 }
4084
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004085 switch ((ssl_conf && ssl_conf->verify) ? ssl_conf->verify : bind_conf->ssl_conf.verify) {
Emeric Brun850efd52014-01-29 12:24:34 +01004086 case SSL_SOCK_VERIFY_NONE:
4087 verify = SSL_VERIFY_NONE;
4088 break;
4089 case SSL_SOCK_VERIFY_OPTIONAL:
4090 verify = SSL_VERIFY_PEER;
4091 break;
4092 case SSL_SOCK_VERIFY_REQUIRED:
4093 verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
4094 break;
4095 }
4096 SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk);
4097 if (verify & SSL_VERIFY_PEER) {
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004098 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 +01004099 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 +01004100 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 +01004101 if (ca_file || ca_verify_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02004102 /* set CAfile to verify */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01004103 if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02004104 memprintf(err, "%sProxy '%s': unable to set CA file '%s' for bind '%s' at [%s:%d].\n",
Tim Duesterhus93128532019-11-23 23:45:10 +01004105 err && *err ? *err : "", curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004106 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02004107 }
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01004108 if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) {
4109 memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n",
4110 err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line);
4111 cfgerr |= ERR_ALERT | ERR_FATAL;
4112 }
4113 if (ca_file && !((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) {
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02004114 /* set CA names for client cert request, function returns void */
Emmanuel Hocdet129d3282019-10-24 18:08:51 +02004115 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 +02004116 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02004117 }
Emeric Brun850efd52014-01-29 12:24:34 +01004118 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01004119 memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n",
4120 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004121 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun850efd52014-01-29 12:24:34 +01004122 }
Emeric Brun051cdab2012-10-02 19:25:50 +02004123#ifdef X509_V_FLAG_CRL_CHECK
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004124 if (crl_file) {
Emeric Brund94b3fe2012-09-20 18:23:56 +02004125 X509_STORE *store = SSL_CTX_get_cert_store(ctx);
4126
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01004127 if (!ssl_set_cert_crl_file(store, crl_file)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004128 memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n",
4129 err && *err ? *err : "", curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004130 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02004131 }
Emeric Brun561e5742012-10-02 15:20:55 +02004132 else {
4133 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
4134 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02004135 }
Emeric Brun051cdab2012-10-02 19:25:50 +02004136#endif
Emeric Brun644cde02012-12-14 11:21:13 +01004137 ERR_clear_error();
Emeric Brund94b3fe2012-09-20 18:23:56 +02004138 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004139#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Nenad Merdanovic146defa2015-05-09 08:46:00 +02004140 if(bind_conf->keys_ref) {
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004141 if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004142 memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n",
4143 err && *err ? *err : "", curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004144 cfgerr |= ERR_ALERT | ERR_FATAL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01004145 }
4146 }
4147#endif
4148
William Lallemand4f45bb92017-10-30 20:08:51 +01004149 ssl_set_shctx(ctx);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004150 conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers;
4151 if (conf_ciphers &&
4152 !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004153 memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n",
4154 err && *err ? *err : "", curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004155 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004156 }
4157
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004158#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004159 conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites;
4160 if (conf_ciphersuites &&
4161 !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004162 memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n",
4163 err && *err ? *err : "", curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004164 cfgerr |= ERR_ALERT | ERR_FATAL;
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004165 }
4166#endif
4167
Emmanuel Hocdetcc6c2a22017-03-03 17:04:14 +01004168#ifndef OPENSSL_NO_DH
Remi Gacogne47783ef2015-05-29 15:53:22 +02004169 /* If tune.ssl.default-dh-param has not been set,
4170 neither has ssl-default-dh-file and no static DH
4171 params were in the certificate file. */
Willy Tarreauef934602016-12-22 23:12:01 +01004172 if (global_ssl.default_dh_param == 0 &&
Remi Gacogne47783ef2015-05-29 15:53:22 +02004173 global_dh == NULL &&
Remi Gacogne4f902b82015-05-28 16:23:00 +02004174 (ssl_dh_ptr_index == -1 ||
4175 SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) {
Willy Tarreau3ba77d22020-05-08 09:31:18 +02004176 /* default to dh-param 2048 */
4177 global_ssl.default_dh_param = 2048;
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004178 }
Remi Gacogne8de54152014-07-15 11:36:40 +02004179
Willy Tarreauef934602016-12-22 23:12:01 +01004180 if (global_ssl.default_dh_param >= 1024) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004181 if (local_dh_1024 == NULL) {
4182 local_dh_1024 = ssl_get_dh_1024();
4183 }
Willy Tarreauef934602016-12-22 23:12:01 +01004184 if (global_ssl.default_dh_param >= 2048) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004185 if (local_dh_2048 == NULL) {
4186 local_dh_2048 = ssl_get_dh_2048();
4187 }
Willy Tarreauef934602016-12-22 23:12:01 +01004188 if (global_ssl.default_dh_param >= 4096) {
Remi Gacogne8de54152014-07-15 11:36:40 +02004189 if (local_dh_4096 == NULL) {
4190 local_dh_4096 = ssl_get_dh_4096();
4191 }
Remi Gacogne8de54152014-07-15 11:36:40 +02004192 }
4193 }
4194 }
4195#endif /* OPENSSL_NO_DH */
Remi Gacognef46cd6e2014-06-12 14:58:40 +02004196
Emeric Brunfc0421f2012-09-07 17:30:07 +02004197 SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk);
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004198#if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L
Emeric Brun29f037d2014-04-25 19:05:36 +02004199 SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk);
Willy Tarreau5cbe4ef2014-05-08 22:45:11 +02004200#endif
Emeric Brun29f037d2014-04-25 19:05:36 +02004201
Bernard Spil13c53f82018-02-15 13:34:58 +01004202#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004203 ssl_conf_cur = NULL;
4204 if (ssl_conf && ssl_conf->npn_str)
4205 ssl_conf_cur = ssl_conf;
4206 else if (bind_conf->ssl_conf.npn_str)
4207 ssl_conf_cur = &bind_conf->ssl_conf;
4208 if (ssl_conf_cur)
4209 SSL_CTX_set_next_protos_advertised_cb(ctx, ssl_sock_advertise_npn_protos, ssl_conf_cur);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02004210#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01004211#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004212 ssl_conf_cur = NULL;
4213 if (ssl_conf && ssl_conf->alpn_str)
4214 ssl_conf_cur = ssl_conf;
4215 else if (bind_conf->ssl_conf.alpn_str)
4216 ssl_conf_cur = &bind_conf->ssl_conf;
4217 if (ssl_conf_cur)
4218 SSL_CTX_set_alpn_select_cb(ctx, ssl_sock_advertise_alpn_protos, ssl_conf_cur);
Willy Tarreauab861d32013-04-02 02:30:41 +02004219#endif
Lukas Tribusd14b49c2019-11-24 18:20:40 +01004220#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004221 conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves;
4222 if (conf_curves) {
4223 if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004224 memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n",
4225 err && *err ? *err : "", curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004226 cfgerr |= ERR_ALERT | ERR_FATAL;
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004227 }
Emmanuel Hocdeta52bb152017-03-20 11:11:49 +01004228 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004229 }
4230#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02004231#if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH)
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01004232 if (!conf_curves) {
Emeric Brun2b58d042012-09-20 17:10:03 +02004233 int i;
4234 EC_KEY *ecdh;
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004235#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004236 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
Olivier Houchardc2aae742017-09-22 18:26:28 +02004237 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
4238 NULL);
4239
4240 if (ecdhe == NULL) {
Eric Salama3c8bde82019-11-20 11:33:40 +01004241 (void)SSL_CTX_set_ecdh_auto(ctx, 1);
Olivier Houchardc2aae742017-09-22 18:26:28 +02004242 return cfgerr;
4243 }
4244#else
4245 const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe :
4246 (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe :
4247 ECDHE_DEFAULT_CURVE);
4248#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02004249
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004250 i = OBJ_sn2nid(ecdhe);
Emeric Brun2b58d042012-09-20 17:10:03 +02004251 if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01004252 memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n",
4253 err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line);
William Lallemand8b453912019-11-21 15:48:10 +01004254 cfgerr |= ERR_ALERT | ERR_FATAL;
Emeric Brun2b58d042012-09-20 17:10:03 +02004255 }
4256 else {
4257 SSL_CTX_set_tmp_ecdh(ctx, ecdh);
4258 EC_KEY_free(ecdh);
4259 }
4260 }
4261#endif
4262
Emeric Brunfc0421f2012-09-07 17:30:07 +02004263 return cfgerr;
4264}
4265
Evan Broderbe554312013-06-27 00:05:25 -07004266static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname)
4267{
4268 const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end;
4269 size_t prefixlen, suffixlen;
4270
4271 /* Trivial case */
4272 if (strcmp(pattern, hostname) == 0)
4273 return 1;
4274
Evan Broderbe554312013-06-27 00:05:25 -07004275 /* The rest of this logic is based on RFC 6125, section 6.4.3
4276 * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */
4277
Emeric Bruna848dae2013-10-08 11:27:28 +02004278 pattern_wildcard = NULL;
4279 pattern_left_label_end = pattern;
4280 while (*pattern_left_label_end != '.') {
4281 switch (*pattern_left_label_end) {
4282 case 0:
4283 /* End of label not found */
4284 return 0;
4285 case '*':
4286 /* If there is more than one wildcards */
4287 if (pattern_wildcard)
4288 return 0;
4289 pattern_wildcard = pattern_left_label_end;
4290 break;
4291 }
4292 pattern_left_label_end++;
4293 }
4294
4295 /* If it's not trivial and there is no wildcard, it can't
4296 * match */
4297 if (!pattern_wildcard)
Evan Broderbe554312013-06-27 00:05:25 -07004298 return 0;
4299
4300 /* Make sure all labels match except the leftmost */
4301 hostname_left_label_end = strchr(hostname, '.');
4302 if (!hostname_left_label_end
4303 || strcmp(pattern_left_label_end, hostname_left_label_end) != 0)
4304 return 0;
4305
4306 /* Make sure the leftmost label of the hostname is long enough
4307 * that the wildcard can match */
Emeric Brun369da852013-10-08 11:39:35 +02004308 if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1)
Evan Broderbe554312013-06-27 00:05:25 -07004309 return 0;
4310
4311 /* Finally compare the string on either side of the
4312 * wildcard */
4313 prefixlen = pattern_wildcard - pattern;
4314 suffixlen = pattern_left_label_end - (pattern_wildcard + 1);
Emeric Bruna848dae2013-10-08 11:27:28 +02004315 if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0))
4316 || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0)))
Evan Broderbe554312013-06-27 00:05:25 -07004317 return 0;
4318
4319 return 1;
4320}
4321
4322static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx)
4323{
4324 SSL *ssl;
4325 struct connection *conn;
Olivier Houchard66ab4982019-02-26 18:37:15 +01004326 struct ssl_sock_ctx *ssl_ctx;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004327 const char *servername;
Willy Tarreau71d058c2017-07-26 20:09:56 +02004328 const char *sni;
Evan Broderbe554312013-06-27 00:05:25 -07004329
4330 int depth;
4331 X509 *cert;
4332 STACK_OF(GENERAL_NAME) *alt_names;
4333 int i;
4334 X509_NAME *cert_subject;
4335 char *str;
4336
4337 if (ok == 0)
4338 return ok;
4339
4340 ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
Thierry FOURNIER28962c92018-06-17 21:37:05 +02004341 conn = SSL_get_ex_data(ssl, ssl_app_data_index);
Olivier Houchard66ab4982019-02-26 18:37:15 +01004342 ssl_ctx = conn->xprt_ctx;
Evan Broderbe554312013-06-27 00:05:25 -07004343
Willy Tarreauad92a9a2017-07-28 11:38:41 +02004344 /* We're checking if the provided hostnames match the desired one. The
4345 * desired hostname comes from the SNI we presented if any, or if not
4346 * provided then it may have been explicitly stated using a "verifyhost"
4347 * directive. If neither is set, we don't care about the name so the
4348 * verification is OK.
Willy Tarreau2ab88672017-07-05 18:23:03 +02004349 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01004350 servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau71d058c2017-07-26 20:09:56 +02004351 sni = servername;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004352 if (!servername) {
Willy Tarreau07d94e42018-09-20 10:57:52 +02004353 servername = __objt_server(conn->target)->ssl_ctx.verify_host;
Willy Tarreau2ab88672017-07-05 18:23:03 +02004354 if (!servername)
4355 return ok;
4356 }
Evan Broderbe554312013-06-27 00:05:25 -07004357
4358 /* We only need to verify the CN on the actual server cert,
4359 * not the indirect CAs */
4360 depth = X509_STORE_CTX_get_error_depth(ctx);
4361 if (depth != 0)
4362 return ok;
4363
4364 /* At this point, the cert is *not* OK unless we can find a
4365 * hostname match */
4366 ok = 0;
4367
4368 cert = X509_STORE_CTX_get_current_cert(ctx);
4369 /* It seems like this might happen if verify peer isn't set */
4370 if (!cert)
4371 return ok;
4372
4373 alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
4374 if (alt_names) {
4375 for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) {
4376 GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i);
4377 if (name->type == GEN_DNS) {
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004378#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Emeric Bruna33410c2013-09-17 15:47:48 +02004379 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) {
4380#else
Evan Broderbe554312013-06-27 00:05:25 -07004381 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
Emeric Bruna33410c2013-09-17 15:47:48 +02004382#endif
Evan Broderbe554312013-06-27 00:05:25 -07004383 ok = ssl_sock_srv_hostcheck(str, servername);
4384 OPENSSL_free(str);
4385 }
4386 }
4387 }
Emeric Brun4ad50a42013-09-17 15:19:54 +02004388 sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
Evan Broderbe554312013-06-27 00:05:25 -07004389 }
4390
4391 cert_subject = X509_get_subject_name(cert);
4392 i = -1;
4393 while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) {
4394 X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02004395 ASN1_STRING *value;
4396 value = X509_NAME_ENTRY_get_data(entry);
4397 if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) {
Evan Broderbe554312013-06-27 00:05:25 -07004398 ok = ssl_sock_srv_hostcheck(str, servername);
4399 OPENSSL_free(str);
4400 }
4401 }
4402
Willy Tarreau71d058c2017-07-26 20:09:56 +02004403 /* report the mismatch and indicate if SNI was used or not */
4404 if (!ok && !conn->err_code)
4405 conn->err_code = sni ? CO_ER_SSL_MISMATCH_SNI : CO_ER_SSL_MISMATCH;
Evan Broderbe554312013-06-27 00:05:25 -07004406 return ok;
4407}
4408
Emeric Brun94324a42012-10-11 14:00:19 +02004409/* prepare ssl context from servers options. Returns an error count */
Willy Tarreau03209342016-12-22 17:08:28 +01004410int ssl_sock_prepare_srv_ctx(struct server *srv)
Emeric Brun94324a42012-10-11 14:00:19 +02004411{
Willy Tarreau03209342016-12-22 17:08:28 +01004412 struct proxy *curproxy = srv->proxy;
Emeric Brun94324a42012-10-11 14:00:19 +02004413 int cfgerr = 0;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02004414 long options =
Emeric Brun94324a42012-10-11 14:00:19 +02004415 SSL_OP_ALL | /* all known workarounds for bugs */
4416 SSL_OP_NO_SSLv2 |
4417 SSL_OP_NO_COMPRESSION;
Remi Gacogneaf5c3da2014-05-19 10:29:58 +02004418 long mode =
Emeric Brun94324a42012-10-11 14:00:19 +02004419 SSL_MODE_ENABLE_PARTIAL_WRITE |
4420 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER |
Willy Tarreau396a1862014-11-13 14:06:52 +01004421 SSL_MODE_RELEASE_BUFFERS |
4422 SSL_MODE_SMALL_BUFFERS;
Emeric Brun850efd52014-01-29 12:24:34 +01004423 int verify = SSL_VERIFY_NONE;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004424 SSL_CTX *ctx = NULL;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004425 struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004426 int i, min, max, hole;
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004427 int flags = MC_SSL_O_ALL;
Emeric Brun94324a42012-10-11 14:00:19 +02004428
Thierry Fournier383085f2013-01-24 14:15:43 +01004429 /* Make sure openssl opens /dev/urandom before the chroot */
4430 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004431 ha_alert("OpenSSL random data generator initialization failed.\n");
Thierry Fournier383085f2013-01-24 14:15:43 +01004432 cfgerr++;
4433 }
4434
Willy Tarreaufce03112015-01-15 21:32:40 +01004435 /* Automatic memory computations need to know we use SSL there */
4436 global.ssl_used_backend = 1;
4437
4438 /* Initiate SSL context for current server */
Emeric Brun821bb9b2017-06-15 16:37:39 +02004439 if (!srv->ssl_ctx.reused_sess) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01004440 if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004441 ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n",
4442 curproxy->id, srv->id,
4443 srv->conf.file, srv->conf.line);
Emeric Brun821bb9b2017-06-15 16:37:39 +02004444 cfgerr++;
4445 return cfgerr;
4446 }
4447 }
Christopher Fauletf61f33a2020-03-27 18:55:49 +01004448 if (srv->use_ssl == 1)
Emeric Brun94324a42012-10-11 14:00:19 +02004449 srv->xprt = &ssl_sock;
Emeric Brun94324a42012-10-11 14:00:19 +02004450
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004451 ctx = SSL_CTX_new(SSLv23_client_method());
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004452 if (!ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004453 ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n",
4454 proxy_type_str(curproxy), curproxy->id,
4455 srv->id);
Emeric Brun94324a42012-10-11 14:00:19 +02004456 cfgerr++;
4457 return cfgerr;
4458 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004459
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004460 if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max))
Christopher Faulet767a84b2017-11-24 16:50:31 +01004461 ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. "
4462 "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4463 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004464 else
4465 flags = conf_ssl_methods->flags;
4466
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004467 /* Real min and max should be determinate with configuration and openssl's capabilities */
4468 if (conf_ssl_methods->min)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004469 flags |= (methodVersions[conf_ssl_methods->min].flag - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004470 if (conf_ssl_methods->max)
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004471 flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004472
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004473 /* find min, max and holes */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004474 min = max = CONF_TLSV_NONE;
4475 hole = 0;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004476 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004477 /* version is in openssl && version not disable in configuration */
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004478 if (methodVersions[i].option && !(flags & methodVersions[i].flag)) {
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004479 if (min) {
4480 if (hole) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004481 ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. "
4482 "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n",
4483 proxy_type_str(curproxy), curproxy->id, srv->id,
4484 methodVersions[hole].name);
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004485 hole = 0;
4486 }
4487 max = i;
4488 }
4489 else {
4490 min = max = i;
4491 }
4492 }
4493 else {
4494 if (min)
4495 hole = i;
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004496 }
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004497 if (!min) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004498 ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n",
4499 proxy_type_str(curproxy), curproxy->id, srv->id);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004500 cfgerr += 1;
4501 }
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004502
Willy Tarreau9a1ab082019-05-09 13:26:41 +02004503#if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL)
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004504 /* Keep force-xxx implementation as it is in older haproxy. It's a
Joseph Herlant017b3da2018-11-15 09:07:59 -08004505 precautionary measure to avoid any surprise with older openssl version. */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004506 if (min == max)
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004507 methodVersions[min].ctx_set_version(ctx, SET_CLIENT);
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004508 else
4509 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
4510 if (flags & methodVersions[i].flag)
4511 options |= methodVersions[i].option;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004512#else /* openssl >= 1.1.0 */
Emmanuel Hocdetb4e9ba42017-03-30 19:25:07 +02004513 /* set the max_version is required to cap TLS version or activate new TLS (v1.3) */
Emmanuel Hocdet4aa615f2017-05-18 12:33:19 +02004514 methodVersions[min].ctx_set_version(ctx, SET_MIN);
4515 methodVersions[max].ctx_set_version(ctx, SET_MAX);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02004516#endif
4517
4518 if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS)
4519 options |= SSL_OP_NO_TICKET;
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004520 SSL_CTX_set_options(ctx, options);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004521
Willy Tarreau5db847a2019-05-09 14:13:35 +02004522#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00004523 if (global_ssl.async)
4524 mode |= SSL_MODE_ASYNC;
4525#endif
Emmanuel Hocdet4de1ff12017-03-03 12:21:32 +01004526 SSL_CTX_set_mode(ctx, mode);
4527 srv->ssl_ctx.ctx = ctx;
4528
Emeric Bruna7aa3092012-10-26 12:58:00 +02004529 if (srv->ssl_ctx.client_crt) {
4530 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 +01004531 ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n",
4532 proxy_type_str(curproxy), curproxy->id,
4533 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02004534 cfgerr++;
4535 }
4536 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 +01004537 ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n",
4538 proxy_type_str(curproxy), curproxy->id,
4539 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02004540 cfgerr++;
4541 }
4542 else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004543 ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n",
4544 proxy_type_str(curproxy), curproxy->id,
4545 srv->id, srv->ssl_ctx.client_crt);
Emeric Bruna7aa3092012-10-26 12:58:00 +02004546 cfgerr++;
4547 }
4548 }
Emeric Brun94324a42012-10-11 14:00:19 +02004549
Emeric Brun850efd52014-01-29 12:24:34 +01004550 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
4551 verify = SSL_VERIFY_PEER;
Emeric Brun850efd52014-01-29 12:24:34 +01004552 switch (srv->ssl_ctx.verify) {
4553 case SSL_SOCK_VERIFY_NONE:
4554 verify = SSL_VERIFY_NONE;
4555 break;
4556 case SSL_SOCK_VERIFY_REQUIRED:
4557 verify = SSL_VERIFY_PEER;
4558 break;
4559 }
Evan Broderbe554312013-06-27 00:05:25 -07004560 SSL_CTX_set_verify(srv->ssl_ctx.ctx,
Emeric Brun850efd52014-01-29 12:24:34 +01004561 verify,
Willy Tarreau2ab88672017-07-05 18:23:03 +02004562 (srv->ssl_ctx.verify_host || (verify & SSL_VERIFY_PEER)) ? ssl_sock_srv_verifycbk : NULL);
Emeric Brun850efd52014-01-29 12:24:34 +01004563 if (verify & SSL_VERIFY_PEER) {
Emeric Brunef42d922012-10-11 16:11:36 +02004564 if (srv->ssl_ctx.ca_file) {
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02004565 /* set CAfile to verify */
4566 if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) {
4567 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to set CA file '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004568 curproxy->id, srv->id,
4569 srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file);
Emeric Brunef42d922012-10-11 16:11:36 +02004570 cfgerr++;
4571 }
4572 }
Emeric Brun850efd52014-01-29 12:24:34 +01004573 else {
4574 if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED)
Christopher Faulet767a84b2017-11-24 16:50:31 +01004575 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",
4576 curproxy->id, srv->id,
4577 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01004578 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01004579 ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n",
4580 curproxy->id, srv->id,
4581 srv->conf.file, srv->conf.line);
Emeric Brun850efd52014-01-29 12:24:34 +01004582 cfgerr++;
4583 }
Emeric Brunef42d922012-10-11 16:11:36 +02004584#ifdef X509_V_FLAG_CRL_CHECK
4585 if (srv->ssl_ctx.crl_file) {
4586 X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx);
4587
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01004588 if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004589 ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n",
4590 curproxy->id, srv->id,
4591 srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file);
Emeric Brunef42d922012-10-11 16:11:36 +02004592 cfgerr++;
4593 }
4594 else {
4595 X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
4596 }
4597 }
4598#endif
4599 }
4600
Olivier Houchardbd84ac82017-11-03 13:43:35 +01004601 SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT |
4602 SSL_SESS_CACHE_NO_INTERNAL_STORE);
4603 SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb);
Emeric Brun94324a42012-10-11 14:00:19 +02004604 if (srv->ssl_ctx.ciphers &&
4605 !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004606 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n",
4607 curproxy->id, srv->id,
4608 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers);
Emeric Brun94324a42012-10-11 14:00:19 +02004609 cfgerr++;
4610 }
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004611
Emmanuel Hocdet839af572019-05-14 16:27:35 +02004612#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004613 if (srv->ssl_ctx.ciphersuites &&
Pierre Cheynierbc34cd12019-03-21 16:15:47 +00004614 !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) {
Dirkjan Bussink415150f2018-09-14 11:14:21 +02004615 ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n",
4616 curproxy->id, srv->id,
4617 srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites);
4618 cfgerr++;
4619 }
4620#endif
Olivier Houchardc7566002018-11-20 23:33:50 +01004621#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
4622 if (srv->ssl_ctx.npn_str)
4623 SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv);
4624#endif
4625#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4626 if (srv->ssl_ctx.alpn_str)
4627 SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len);
4628#endif
4629
Emeric Brun94324a42012-10-11 14:00:19 +02004630
4631 return cfgerr;
4632}
4633
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004634/* Walks down the two trees in bind_conf and prepares all certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02004635 * be NULL, in which case nothing is done. Returns the number of errors
4636 * encountered.
4637 */
Willy Tarreau03209342016-12-22 17:08:28 +01004638int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004639{
4640 struct ebmb_node *node;
4641 struct sni_ctx *sni;
4642 int err = 0;
William Lallemand8b453912019-11-21 15:48:10 +01004643 int errcode = 0;
4644 char *errmsg = NULL;
Emeric Brunfc0421f2012-09-07 17:30:07 +02004645
Willy Tarreaufce03112015-01-15 21:32:40 +01004646 /* Automatic memory computations need to know we use SSL there */
4647 global.ssl_used_frontend = 1;
4648
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004649 /* Make sure openssl opens /dev/urandom before the chroot */
4650 if (!ssl_initialize_random()) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004651 ha_alert("OpenSSL random data generator initialization failed.\n");
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004652 err++;
4653 }
4654 /* Create initial_ctx used to start the ssl connection before do switchctx */
4655 if (!bind_conf->initial_ctx) {
Emmanuel Hocdetabd32332017-05-05 18:06:12 +02004656 err += ssl_sock_initial_ctx(bind_conf);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004657 /* It should not be necessary to call this function, but it's
4658 necessary first to check and move all initialisation related
4659 to initial_ctx in ssl_sock_initial_ctx. */
William Lallemand8b453912019-11-21 15:48:10 +01004660 errcode |= ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx, &errmsg);
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004661 }
Emeric Brun0bed9942014-10-30 19:25:24 +01004662 if (bind_conf->default_ctx)
William Lallemand8b453912019-11-21 15:48:10 +01004663 errcode |= ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, &errmsg);
Emeric Brun0bed9942014-10-30 19:25:24 +01004664
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004665 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004666 while (node) {
4667 sni = ebmb_entry(node, struct sni_ctx, name);
Emeric Brun0bed9942014-10-30 19:25:24 +01004668 if (!sni->order && sni->ctx != bind_conf->default_ctx)
4669 /* only initialize the CTX on its first occurrence and
4670 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01004671 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004672 node = ebmb_next(node);
4673 }
4674
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004675 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004676 while (node) {
4677 sni = ebmb_entry(node, struct sni_ctx, name);
William Lallemand8b453912019-11-21 15:48:10 +01004678 if (!sni->order && sni->ctx != bind_conf->default_ctx) {
Emeric Brun0bed9942014-10-30 19:25:24 +01004679 /* only initialize the CTX on its first occurrence and
4680 if it is not the default_ctx */
William Lallemand8b453912019-11-21 15:48:10 +01004681 errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg);
4682 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004683 node = ebmb_next(node);
4684 }
William Lallemand8b453912019-11-21 15:48:10 +01004685
4686 if (errcode & ERR_WARN) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01004687 ha_warning("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01004688 } else if (errcode & ERR_CODE) {
Tim Duesterhusc0e820c2019-11-23 23:52:30 +01004689 ha_alert("%s", errmsg);
William Lallemand8b453912019-11-21 15:48:10 +01004690 err++;
4691 }
4692
4693 free(errmsg);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004694 return err;
4695}
4696
Willy Tarreau55d37912016-12-21 23:38:39 +01004697/* Prepares all the contexts for a bind_conf and allocates the shared SSL
4698 * context if needed. Returns < 0 on error, 0 on success. The warnings and
4699 * alerts are directly emitted since the rest of the stack does it below.
4700 */
4701int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf)
4702{
4703 struct proxy *px = bind_conf->frontend;
4704 int alloc_ctx;
4705 int err;
4706
4707 if (!bind_conf->is_ssl) {
4708 if (bind_conf->default_ctx) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004709 ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n",
4710 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Willy Tarreau55d37912016-12-21 23:38:39 +01004711 }
4712 return 0;
4713 }
4714 if (!bind_conf->default_ctx) {
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02004715 if (bind_conf->strict_sni && !bind_conf->generate_certs) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004716 ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n",
4717 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02004718 }
4719 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004720 ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n",
4721 px->id, bind_conf->arg, bind_conf->file, bind_conf->line);
Emmanuel Hocdetaa0d6372017-08-09 11:24:25 +02004722 return -1;
4723 }
Willy Tarreau55d37912016-12-21 23:38:39 +01004724 }
William Lallemandc61c0b32017-12-04 18:46:39 +01004725 if (!ssl_shctx && global.tune.sslcachesize) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01004726 alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize,
Frédéric Lécailleb7838af2018-10-22 16:21:39 +02004727 sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1,
William Lallemandc3cd35f2017-11-28 11:04:43 +01004728 sizeof(*sh_ssl_sess_tree),
4729 ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0);
Frédéric Lécaille4c8aa112018-10-25 20:22:46 +02004730 if (alloc_ctx <= 0) {
William Lallemandc3cd35f2017-11-28 11:04:43 +01004731 if (alloc_ctx == SHCTX_E_INIT_LOCK)
4732 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");
4733 else
4734 ha_alert("Unable to allocate SSL session cache.\n");
4735 return -1;
4736 }
4737 /* free block callback */
4738 ssl_shctx->free_block = sh_ssl_sess_free_blocks;
4739 /* init the root tree within the extra space */
4740 sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context);
4741 *sh_ssl_sess_tree = EB_ROOT_UNIQUE;
Willy Tarreau55d37912016-12-21 23:38:39 +01004742 }
Willy Tarreau55d37912016-12-21 23:38:39 +01004743 err = 0;
4744 /* initialize all certificate contexts */
4745 err += ssl_sock_prepare_all_ctx(bind_conf);
4746
4747 /* initialize CA variables if the certificates generation is enabled */
4748 err += ssl_sock_load_ca(bind_conf);
4749
4750 return -err;
4751}
Christopher Faulet77fe80c2015-07-29 13:02:40 +02004752
4753/* release ssl context allocated for servers. */
4754void ssl_sock_free_srv_ctx(struct server *srv)
4755{
Olivier Houchardc7566002018-11-20 23:33:50 +01004756#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
4757 if (srv->ssl_ctx.alpn_str)
4758 free(srv->ssl_ctx.alpn_str);
4759#endif
Lukas Tribusda95fd92018-11-25 13:21:27 +01004760#ifdef OPENSSL_NPN_NEGOTIATED
Olivier Houchardc7566002018-11-20 23:33:50 +01004761 if (srv->ssl_ctx.npn_str)
4762 free(srv->ssl_ctx.npn_str);
Lukas Tribus7706b852018-11-26 22:57:17 +01004763#endif
Christopher Faulet77fe80c2015-07-29 13:02:40 +02004764 if (srv->ssl_ctx.ctx)
4765 SSL_CTX_free(srv->ssl_ctx.ctx);
4766}
4767
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004768/* Walks down the two trees in bind_conf and frees all the certs. The pointer may
Emeric Brunfc0421f2012-09-07 17:30:07 +02004769 * be NULL, in which case nothing is done. The default_ctx is nullified too.
4770 */
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004771void ssl_sock_free_all_ctx(struct bind_conf *bind_conf)
Emeric Brunfc0421f2012-09-07 17:30:07 +02004772{
4773 struct ebmb_node *node, *back;
4774 struct sni_ctx *sni;
4775
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004776 node = ebmb_first(&bind_conf->sni_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004777 while (node) {
4778 sni = ebmb_entry(node, struct sni_ctx, name);
4779 back = ebmb_next(node);
4780 ebmb_delete(node);
William Lallemand02e19a52020-04-08 16:11:26 +02004781 SSL_CTX_free(sni->ctx);
4782 if (!sni->order) { /* only free the CTX conf on its first occurrence */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004783 ssl_sock_free_ssl_conf(sni->conf);
4784 free(sni->conf);
4785 sni->conf = NULL;
4786 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004787 free(sni);
4788 node = back;
4789 }
4790
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004791 node = ebmb_first(&bind_conf->sni_w_ctx);
Emeric Brunfc0421f2012-09-07 17:30:07 +02004792 while (node) {
4793 sni = ebmb_entry(node, struct sni_ctx, name);
4794 back = ebmb_next(node);
4795 ebmb_delete(node);
William Lallemand02e19a52020-04-08 16:11:26 +02004796 SSL_CTX_free(sni->ctx);
4797 if (!sni->order) { /* only free the SSL conf its first occurrence */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004798 ssl_sock_free_ssl_conf(sni->conf);
4799 free(sni->conf);
4800 sni->conf = NULL;
4801 }
Emeric Brunfc0421f2012-09-07 17:30:07 +02004802 free(sni);
4803 node = back;
4804 }
Emmanuel Hocdetf6b37c62017-03-06 15:34:44 +01004805 SSL_CTX_free(bind_conf->initial_ctx);
4806 bind_conf->initial_ctx = NULL;
William Lallemand02e19a52020-04-08 16:11:26 +02004807 SSL_CTX_free(bind_conf->default_ctx);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02004808 bind_conf->default_ctx = NULL;
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004809 bind_conf->default_ssl_conf = NULL;
Emeric Brune1f38db2012-09-03 20:36:47 +02004810}
4811
Willy Tarreau795cdab2016-12-22 17:30:54 +01004812/* Destroys all the contexts for a bind_conf. This is used during deinit(). */
4813void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf)
4814{
4815 ssl_sock_free_ca(bind_conf);
4816 ssl_sock_free_all_ctx(bind_conf);
Emmanuel Hocdet98263292016-12-29 18:26:15 +01004817 ssl_sock_free_ssl_conf(&bind_conf->ssl_conf);
Willy Tarreau795cdab2016-12-22 17:30:54 +01004818 free(bind_conf->ca_sign_file);
4819 free(bind_conf->ca_sign_pass);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02004820 if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) {
Willy Tarreau795cdab2016-12-22 17:30:54 +01004821 free(bind_conf->keys_ref->filename);
4822 free(bind_conf->keys_ref->tlskeys);
4823 LIST_DEL(&bind_conf->keys_ref->list);
4824 free(bind_conf->keys_ref);
4825 }
4826 bind_conf->keys_ref = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01004827 bind_conf->ca_sign_pass = NULL;
4828 bind_conf->ca_sign_file = NULL;
Willy Tarreau795cdab2016-12-22 17:30:54 +01004829}
4830
Christopher Faulet31af49d2015-06-09 17:29:50 +02004831/* Load CA cert file and private key used to generate certificates */
4832int
Willy Tarreau03209342016-12-22 17:08:28 +01004833ssl_sock_load_ca(struct bind_conf *bind_conf)
Christopher Faulet31af49d2015-06-09 17:29:50 +02004834{
Willy Tarreau03209342016-12-22 17:08:28 +01004835 struct proxy *px = bind_conf->frontend;
Christopher Faulet31af49d2015-06-09 17:29:50 +02004836 FILE *fp;
4837 X509 *cacert = NULL;
4838 EVP_PKEY *capkey = NULL;
4839 int err = 0;
4840
Christopher Fauletf8bb0ce2017-09-15 09:52:49 +02004841 if (!bind_conf->generate_certs)
Christopher Faulet31af49d2015-06-09 17:29:50 +02004842 return err;
4843
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01004844#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +02004845 if (global_ssl.ctx_cache) {
Willy Tarreauef934602016-12-22 23:12:01 +01004846 ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache);
Emeric Brun821bb9b2017-06-15 16:37:39 +02004847 }
Christopher Fauletd2cab922015-07-28 16:03:47 +02004848 ssl_ctx_lru_seed = (unsigned int)time(NULL);
Emeric Brun821bb9b2017-06-15 16:37:39 +02004849 ssl_ctx_serial = now_ms;
Willy Tarreaua84c2672015-10-09 12:10:13 +02004850#endif
Christopher Fauletd2cab922015-07-28 16:03:47 +02004851
Christopher Faulet31af49d2015-06-09 17:29:50 +02004852 if (!bind_conf->ca_sign_file) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004853 ha_alert("Proxy '%s': cannot enable certificate generation, "
4854 "no CA certificate File configured at [%s:%d].\n",
4855 px->id, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02004856 goto load_error;
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004857 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02004858
4859 /* read in the CA certificate */
4860 if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004861 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
4862 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Faulet31af49d2015-06-09 17:29:50 +02004863 goto load_error;
4864 }
4865 if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004866 ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n",
4867 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004868 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02004869 }
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004870 rewind(fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02004871 if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004872 ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n",
4873 px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004874 goto read_error;
Christopher Faulet31af49d2015-06-09 17:29:50 +02004875 }
Christopher Faulet31af49d2015-06-09 17:29:50 +02004876
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004877 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02004878 bind_conf->ca_sign_cert = cacert;
4879 bind_conf->ca_sign_pkey = capkey;
4880 return err;
4881
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004882 read_error:
4883 fclose (fp);
Christopher Faulet31af49d2015-06-09 17:29:50 +02004884 if (capkey) EVP_PKEY_free(capkey);
4885 if (cacert) X509_free(cacert);
Christopher Fauletc6f02fb2015-10-09 10:53:31 +02004886 load_error:
4887 bind_conf->generate_certs = 0;
4888 err++;
Christopher Faulet31af49d2015-06-09 17:29:50 +02004889 return err;
4890}
4891
4892/* Release CA cert and private key used to generate certificated */
4893void
4894ssl_sock_free_ca(struct bind_conf *bind_conf)
4895{
Christopher Faulet31af49d2015-06-09 17:29:50 +02004896 if (bind_conf->ca_sign_pkey)
4897 EVP_PKEY_free(bind_conf->ca_sign_pkey);
4898 if (bind_conf->ca_sign_cert)
4899 X509_free(bind_conf->ca_sign_cert);
Willy Tarreau94ff03a2016-12-22 17:57:46 +01004900 bind_conf->ca_sign_pkey = NULL;
4901 bind_conf->ca_sign_cert = NULL;
Christopher Faulet31af49d2015-06-09 17:29:50 +02004902}
4903
Emeric Brun46591952012-05-18 15:47:34 +02004904/*
4905 * This function is called if SSL * context is not yet allocated. The function
4906 * is designed to be called before any other data-layer operation and sets the
4907 * handshake flag on the connection. It is safe to call it multiple times.
4908 * It returns 0 on success and -1 in error case.
4909 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01004910static int ssl_sock_init(struct connection *conn, void **xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02004911{
Olivier Houchard66ab4982019-02-26 18:37:15 +01004912 struct ssl_sock_ctx *ctx;
Emeric Brun46591952012-05-18 15:47:34 +02004913 /* already initialized */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01004914 if (*xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02004915 return 0;
4916
Willy Tarreau3c728722014-01-23 13:50:42 +01004917 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02004918 return 0;
4919
Olivier Houchard66ab4982019-02-26 18:37:15 +01004920 ctx = pool_alloc(ssl_sock_ctx_pool);
4921 if (!ctx) {
4922 conn->err_code = CO_ER_SSL_NO_MEM;
4923 return -1;
4924 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004925 ctx->wait_event.tasklet = tasklet_new();
4926 if (!ctx->wait_event.tasklet) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02004927 conn->err_code = CO_ER_SSL_NO_MEM;
4928 pool_free(ssl_sock_ctx_pool, ctx);
4929 return -1;
4930 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02004931 ctx->wait_event.tasklet->process = ssl_sock_io_cb;
4932 ctx->wait_event.tasklet->context = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02004933 ctx->wait_event.events = 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01004934 ctx->sent_early_data = 0;
Olivier Houchard54907bb2019-12-19 15:02:39 +01004935 ctx->early_buf = BUF_NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02004936 ctx->conn = conn;
Willy Tarreau113d52b2020-01-10 09:20:26 +01004937 ctx->subs = NULL;
Emeric Brun5762a0d2019-09-06 15:36:02 +02004938 ctx->xprt_st = 0;
4939 ctx->xprt_ctx = NULL;
Olivier Houcharda8955d52019-04-07 22:00:38 +02004940
4941 /* Only work with sockets for now, this should be adapted when we'll
4942 * add QUIC support.
4943 */
4944 ctx->xprt = xprt_get(XPRT_RAW);
Olivier Houchard19afb272019-05-23 18:24:07 +02004945 if (ctx->xprt->init) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02004946 if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0)
4947 goto err;
Olivier Houchard19afb272019-05-23 18:24:07 +02004948 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01004949
Willy Tarreau20879a02012-12-03 16:32:10 +01004950 if (global.maxsslconn && sslconns >= global.maxsslconn) {
4951 conn->err_code = CO_ER_SSL_TOO_MANY;
Olivier Houchardea8dd942019-05-20 14:02:16 +02004952 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01004953 }
Willy Tarreau403edff2012-09-06 11:58:37 +02004954
Emeric Brun46591952012-05-18 15:47:34 +02004955 /* If it is in client mode initiate SSL session
4956 in connect state otherwise accept state */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004957 if (objt_server(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004958 int may_retry = 1;
4959
4960 retry_connect:
Emeric Brun46591952012-05-18 15:47:34 +02004961 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01004962 ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx);
4963 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004964 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01004965 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004966 goto retry_connect;
4967 }
Willy Tarreau20879a02012-12-03 16:32:10 +01004968 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01004969 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01004970 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02004971 ctx->bio = BIO_new(ha_meth);
4972 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01004973 SSL_free(ctx->ssl);
4974 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004975 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01004976 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004977 goto retry_connect;
4978 }
Emeric Brun55476152014-11-12 17:35:37 +01004979 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01004980 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01004981 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02004982 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02004983 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02004984
Evan Broderbe554312013-06-27 00:05:25 -07004985 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01004986 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
4987 SSL_free(ctx->ssl);
4988 ctx->ssl = NULL;
Emeric Brun55476152014-11-12 17:35:37 +01004989 conn->xprt_ctx = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004990 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01004991 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01004992 goto retry_connect;
4993 }
Emeric Brun55476152014-11-12 17:35:37 +01004994 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01004995 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01004996 }
4997
Olivier Houchard66ab4982019-02-26 18:37:15 +01004998 SSL_set_connect_state(ctx->ssl);
Willy Tarreau07d94e42018-09-20 10:57:52 +02004999 if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
5000 const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr;
5001 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 +01005002 if (sess && !SSL_set_session(ctx->ssl, sess)) {
Olivier Houcharde6060c52017-11-16 17:42:52 +01005003 SSL_SESSION_free(sess);
Willy Tarreau07d94e42018-09-20 10:57:52 +02005004 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
5005 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Olivier Houcharde6060c52017-11-16 17:42:52 +01005006 } else if (sess) {
5007 SSL_SESSION_free(sess);
Emeric Brun55476152014-11-12 17:35:37 +01005008 }
5009 }
Evan Broderbe554312013-06-27 00:05:25 -07005010
Emeric Brun46591952012-05-18 15:47:34 +02005011 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005012 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Willy Tarreau403edff2012-09-06 11:58:37 +02005013
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005014 _HA_ATOMIC_ADD(&sslconns, 1);
5015 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005016 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005017 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005018 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02005019 return 0;
5020 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005021 else if (objt_listener(conn->target)) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005022 int may_retry = 1;
5023
5024 retry_accept:
Emeric Brun46591952012-05-18 15:47:34 +02005025 /* Alloc a new SSL session ctx */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005026 ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx);
5027 if (!ctx->ssl) {
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005028 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005029 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005030 goto retry_accept;
5031 }
Willy Tarreau20879a02012-12-03 16:32:10 +01005032 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005033 goto err;
Willy Tarreau20879a02012-12-03 16:32:10 +01005034 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005035 ctx->bio = BIO_new(ha_meth);
5036 if (!ctx->bio) {
Olivier Houchardefe5e8e2020-01-24 15:17:38 +01005037 SSL_free(ctx->ssl);
5038 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005039 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005040 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005041 goto retry_accept;
5042 }
Emeric Brun55476152014-11-12 17:35:37 +01005043 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005044 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005045 }
Olivier Houcharda8955d52019-04-07 22:00:38 +02005046 BIO_set_data(ctx->bio, ctx);
Olivier Houcharda8955d52019-04-07 22:00:38 +02005047 SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio);
Emeric Brun46591952012-05-18 15:47:34 +02005048
Emeric Brune1f38db2012-09-03 20:36:47 +02005049 /* set connection pointer */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005050 if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) {
5051 SSL_free(ctx->ssl);
5052 ctx->ssl = NULL;
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005053 if (may_retry--) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01005054 pool_gc(NULL);
Willy Tarreaufba03cd2014-11-13 13:48:58 +01005055 goto retry_accept;
5056 }
Emeric Brun55476152014-11-12 17:35:37 +01005057 conn->err_code = CO_ER_SSL_NO_MEM;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005058 goto err;
Emeric Brun55476152014-11-12 17:35:37 +01005059 }
5060
Frédéric Lécaille3139c1b2020-01-24 14:56:18 +01005061#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
5062 if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) {
5063 b_alloc(&ctx->early_buf);
5064 SSL_set_max_early_data(ctx->ssl,
5065 /* Only allow early data if we managed to allocate
5066 * a buffer.
5067 */
5068 (!b_is_null(&ctx->early_buf)) ?
5069 global.tune.bufsize - global.tune.maxrewrite : 0);
5070 }
5071#endif
5072
Olivier Houchard66ab4982019-02-26 18:37:15 +01005073 SSL_set_accept_state(ctx->ssl);
Emeric Brune1f38db2012-09-03 20:36:47 +02005074
Emeric Brun46591952012-05-18 15:47:34 +02005075 /* leave init state and start handshake */
Willy Tarreau05737472012-09-04 08:03:39 +02005076 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02005077#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02005078 conn->flags |= CO_FL_EARLY_SSL_HS;
5079#endif
Willy Tarreau403edff2012-09-06 11:58:37 +02005080
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005081 _HA_ATOMIC_ADD(&sslconns, 1);
5082 _HA_ATOMIC_ADD(&totalsslconns, 1);
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005083 *xprt_ctx = ctx;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005084 /* Start the handshake */
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005085 tasklet_wakeup(ctx->wait_event.tasklet);
Emeric Brun46591952012-05-18 15:47:34 +02005086 return 0;
5087 }
5088 /* don't know how to handle such a target */
Willy Tarreau20879a02012-12-03 16:32:10 +01005089 conn->err_code = CO_ER_SSL_NO_TARGET;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005090err:
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005091 if (ctx && ctx->wait_event.tasklet)
5092 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005093 pool_free(ssl_sock_ctx_pool, ctx);
Emeric Brun46591952012-05-18 15:47:34 +02005094 return -1;
5095}
5096
5097
5098/* This is the callback which is used when an SSL handshake is pending. It
5099 * updates the FD status if it wants some polling before being called again.
5100 * It returns 0 if it fails in a fatal way or needs to poll to go further,
5101 * otherwise it returns non-zero and removes itself from the connection's
5102 * flags (the bit is provided in <flag> by the caller).
5103 */
Olivier Houchard000694c2019-05-23 14:45:12 +02005104static int ssl_sock_handshake(struct connection *conn, unsigned int flag)
Emeric Brun46591952012-05-18 15:47:34 +02005105{
Olivier Houchard66ab4982019-02-26 18:37:15 +01005106 struct ssl_sock_ctx *ctx = conn->xprt_ctx;
Emeric Brun46591952012-05-18 15:47:34 +02005107 int ret;
5108
Willy Tarreau3c728722014-01-23 13:50:42 +01005109 if (!conn_ctrl_ready(conn))
Willy Tarreauf79c8172013-10-21 16:30:56 +02005110 return 0;
5111
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02005112 if (!conn->xprt_ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005113 goto out_error;
5114
Willy Tarreau5db847a2019-05-09 14:13:35 +02005115#if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L
Olivier Houchardc2aae742017-09-22 18:26:28 +02005116 /*
5117 * Check if we have early data. If we do, we have to read them
5118 * before SSL_do_handshake() is called, And there's no way to
5119 * detect early data, except to try to read them
5120 */
5121 if (conn->flags & CO_FL_EARLY_SSL_HS) {
Olivier Houchard54907bb2019-12-19 15:02:39 +01005122 size_t read_data = 0;
Olivier Houchardc2aae742017-09-22 18:26:28 +02005123
Olivier Houchard54907bb2019-12-19 15:02:39 +01005124 while (1) {
5125 ret = SSL_read_early_data(ctx->ssl,
5126 b_tail(&ctx->early_buf), b_room(&ctx->early_buf),
5127 &read_data);
5128 if (ret == SSL_READ_EARLY_DATA_ERROR)
5129 goto check_error;
5130 if (read_data > 0) {
5131 conn->flags |= CO_FL_EARLY_DATA;
5132 b_add(&ctx->early_buf, read_data);
5133 }
5134 if (ret == SSL_READ_EARLY_DATA_FINISH) {
5135 conn->flags &= ~CO_FL_EARLY_SSL_HS;
5136 if (!b_data(&ctx->early_buf))
5137 b_free(&ctx->early_buf);
5138 break;
5139 }
5140 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02005141 }
5142#endif
Emeric Brun674b7432012-11-08 19:21:55 +01005143 /* If we use SSL_do_handshake to process a reneg initiated by
5144 * the remote peer, it sometimes returns SSL_ERROR_SSL.
5145 * Usually SSL_write and SSL_read are used and process implicitly
5146 * the reneg handshake.
5147 * Here we use SSL_peek as a workaround for reneg.
5148 */
Willy Tarreauc192b0a2020-01-23 09:11:58 +01005149 if (!(conn->flags & CO_FL_WAIT_L6_CONN) && SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01005150 char c;
5151
Olivier Houchard66ab4982019-02-26 18:37:15 +01005152 ret = SSL_peek(ctx->ssl, &c, 1);
Emeric Brun674b7432012-11-08 19:21:55 +01005153 if (ret <= 0) {
5154 /* handshake may have not been completed, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005155 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005156
Emeric Brun674b7432012-11-08 19:21:55 +01005157 if (ret == SSL_ERROR_WANT_WRITE) {
5158 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02005159 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005160 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01005161 return 0;
5162 }
5163 else if (ret == SSL_ERROR_WANT_READ) {
5164 /* handshake may have been completed but we have
5165 * no more data to read.
5166 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005167 if (!SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun674b7432012-11-08 19:21:55 +01005168 ret = 1;
5169 goto reneg_ok;
5170 }
5171 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02005172 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005173 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun674b7432012-11-08 19:21:55 +01005174 return 0;
5175 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02005176#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005177 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005178 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005179 return 0;
5180 }
5181#endif
Emeric Brun674b7432012-11-08 19:21:55 +01005182 else if (ret == SSL_ERROR_SYSCALL) {
5183 /* if errno is null, then connection was successfully established */
5184 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
5185 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Willy Tarreau20879a02012-12-03 16:32:10 +01005186 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02005187#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
5188 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005189 conn->err_code = CO_ER_SSL_HANDSHAKE;
5190#else
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005191 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005192#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02005193 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005194 OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl);
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005195 empty_handshake = state == TLS_ST_BEFORE;
5196#else
Lukas Tribus49799162019-07-08 14:29:15 +02005197 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
5198 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005199#endif
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005200 if (empty_handshake) {
Emeric Brun29f037d2014-04-25 19:05:36 +02005201 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005202 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005203 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5204 else
5205 conn->err_code = CO_ER_SSL_EMPTY;
5206 }
5207 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005208 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005209 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5210 else
5211 conn->err_code = CO_ER_SSL_ABORT;
5212 }
5213 }
5214 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005215 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005216 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
Willy Tarreau20879a02012-12-03 16:32:10 +01005217 else
Emeric Brun29f037d2014-04-25 19:05:36 +02005218 conn->err_code = CO_ER_SSL_HANDSHAKE;
5219 }
Lukas Tribus49799162019-07-08 14:29:15 +02005220#endif /* BoringSSL or LibreSSL */
Willy Tarreau20879a02012-12-03 16:32:10 +01005221 }
Emeric Brun674b7432012-11-08 19:21:55 +01005222 goto out_error;
5223 }
5224 else {
5225 /* Fail on all other handshake errors */
5226 /* Note: OpenSSL may leave unread bytes in the socket's
5227 * buffer, causing an RST to be emitted upon close() on
5228 * TCP sockets. We first try to drain possibly pending
5229 * data to avoid this as much as possible.
5230 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01005231 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01005232 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005233 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02005234 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun674b7432012-11-08 19:21:55 +01005235 goto out_error;
5236 }
5237 }
5238 /* read some data: consider handshake completed */
5239 goto reneg_ok;
5240 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005241 ret = SSL_do_handshake(ctx->ssl);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005242check_error:
Emeric Brun46591952012-05-18 15:47:34 +02005243 if (ret != 1) {
5244 /* handshake did not complete, let's find why */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005245 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02005246
5247 if (ret == SSL_ERROR_WANT_WRITE) {
5248 /* SSL handshake needs to write, L4 connection may not be ready */
Olivier Houchard03abf2d2019-05-28 10:12:02 +02005249 if (!(ctx->wait_event.events & SUB_RETRY_SEND))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005250 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02005251 return 0;
5252 }
5253 else if (ret == SSL_ERROR_WANT_READ) {
5254 /* SSL handshake needs to read, L4 connection is ready */
Olivier Houchardea8dd942019-05-20 14:02:16 +02005255 if (!(ctx->wait_event.events & SUB_RETRY_RECV))
Olivier Houchardea8dd942019-05-20 14:02:16 +02005256 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
5257 SUB_RETRY_RECV, &ctx->wait_event);
Emeric Brun46591952012-05-18 15:47:34 +02005258 return 0;
5259 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02005260#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005261 else if (ret == SSL_ERROR_WANT_ASYNC) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005262 ssl_async_process_fds(ctx);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005263 return 0;
5264 }
5265#endif
Willy Tarreau89230192012-09-28 20:22:13 +02005266 else if (ret == SSL_ERROR_SYSCALL) {
5267 /* if errno is null, then connection was successfully established */
5268 if (!errno && conn->flags & CO_FL_WAIT_L4_CONN)
5269 conn->flags &= ~CO_FL_WAIT_L4_CONN;
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005270 if (!conn->err_code) {
Lukas Tribus49799162019-07-08 14:29:15 +02005271#if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER)
5272 /* do not handle empty handshakes in BoringSSL or LibreSSL */
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005273 conn->err_code = CO_ER_SSL_HANDSHAKE;
5274#else
5275 int empty_handshake;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005276#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL)
Lukas Tribus49799162019-07-08 14:29:15 +02005277 /* use SSL_get_state() in OpenSSL >= 1.1.0; SSL_state() is broken */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005278 OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl);
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005279 empty_handshake = state == TLS_ST_BEFORE;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005280#else
Lukas Tribus49799162019-07-08 14:29:15 +02005281 /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */
5282 empty_handshake = !ctx->ssl->packet_length;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02005283#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005284 if (empty_handshake) {
5285 if (!errno) {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005286 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005287 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5288 else
5289 conn->err_code = CO_ER_SSL_EMPTY;
5290 }
5291 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005292 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005293 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5294 else
5295 conn->err_code = CO_ER_SSL_ABORT;
5296 }
Emeric Brun29f037d2014-04-25 19:05:36 +02005297 }
5298 else {
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005299 if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT)
Emeric Brun29f037d2014-04-25 19:05:36 +02005300 conn->err_code = CO_ER_SSL_HANDSHAKE_HB;
5301 else
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01005302 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun29f037d2014-04-25 19:05:36 +02005303 }
Lukas Tribus49799162019-07-08 14:29:15 +02005304#endif /* BoringSSL or LibreSSL */
Emeric Brun29f037d2014-04-25 19:05:36 +02005305 }
Willy Tarreau89230192012-09-28 20:22:13 +02005306 goto out_error;
5307 }
Emeric Brun46591952012-05-18 15:47:34 +02005308 else {
5309 /* Fail on all other handshake errors */
Willy Tarreau566dc552012-10-19 20:52:18 +02005310 /* Note: OpenSSL may leave unread bytes in the socket's
5311 * buffer, causing an RST to be emitted upon close() on
5312 * TCP sockets. We first try to drain possibly pending
5313 * data to avoid this as much as possible.
5314 */
Willy Tarreaud85c4852015-03-13 00:40:28 +01005315 conn_sock_drain(conn);
Willy Tarreau20879a02012-12-03 16:32:10 +01005316 if (!conn->err_code)
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005317 conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ?
Willy Tarreauf51c6982014-04-25 20:02:39 +02005318 CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02005319 goto out_error;
5320 }
5321 }
Willy Tarreau5db847a2019-05-09 14:13:35 +02005322#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard522eea72017-11-03 16:27:47 +01005323 else {
5324 /*
5325 * If the server refused the early data, we have to send a
5326 * 425 to the client, as we no longer have the data to sent
5327 * them again.
5328 */
5329 if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005330 if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) {
Olivier Houchard522eea72017-11-03 16:27:47 +01005331 conn->err_code = CO_ER_SSL_EARLY_FAILED;
5332 goto out_error;
5333 }
5334 }
5335 }
5336#endif
5337
Emeric Brun46591952012-05-18 15:47:34 +02005338
Emeric Brun674b7432012-11-08 19:21:55 +01005339reneg_ok:
Emeric Brunb5e42a82017-06-06 12:35:14 +00005340
Willy Tarreau5db847a2019-05-09 14:13:35 +02005341#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005342 /* ASYNC engine API doesn't support moving read/write
5343 * buffers. So we disable ASYNC mode right after
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05005344 * the handshake to avoid buffer overflow.
Emeric Brunb5e42a82017-06-06 12:35:14 +00005345 */
5346 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005347 SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005348#endif
Emeric Brun46591952012-05-18 15:47:34 +02005349 /* Handshake succeeded */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005350 if (!SSL_session_reused(ctx->ssl)) {
Willy Tarreau0c9c2722014-05-28 12:28:58 +02005351 if (objt_server(conn->target)) {
5352 update_freq_ctr(&global.ssl_be_keys_per_sec, 1);
5353 if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max)
5354 global.ssl_be_keys_max = global.ssl_be_keys_per_sec.curr_ctr;
Emeric Brun46591952012-05-18 15:47:34 +02005355 }
Willy Tarreau0c9c2722014-05-28 12:28:58 +02005356 else {
5357 update_freq_ctr(&global.ssl_fe_keys_per_sec, 1);
5358 if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max)
5359 global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr;
5360 }
Emeric Brun46591952012-05-18 15:47:34 +02005361 }
5362
5363 /* The connection is now established at both layers, it's time to leave */
5364 conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN);
5365 return 1;
5366
5367 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01005368 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02005369 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01005370 ERR_clear_error();
5371
Emeric Brun9fa89732012-10-04 17:09:56 +02005372 /* free resumed session if exists */
Willy Tarreau07d94e42018-09-20 10:57:52 +02005373 if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) {
5374 free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr);
5375 __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL;
Emeric Brun9fa89732012-10-04 17:09:56 +02005376 }
5377
Emeric Brun46591952012-05-18 15:47:34 +02005378 /* Fail on all other handshake errors */
5379 conn->flags |= CO_FL_ERROR;
Willy Tarreau20879a02012-12-03 16:32:10 +01005380 if (!conn->err_code)
5381 conn->err_code = CO_ER_SSL_HANDSHAKE;
Emeric Brun46591952012-05-18 15:47:34 +02005382 return 0;
5383}
5384
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01005385/* Called from the upper layer, to subscribe <es> to events <event_type>. The
5386 * event subscriber <es> is not allowed to change from a previous call as long
5387 * as at least one event is still subscribed. The <event_type> must only be a
5388 * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0,
5389 * unless the transport layer was already released.
5390 */
5391static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01005392{
Olivier Houchardea8dd942019-05-20 14:02:16 +02005393 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005394
Olivier Houchard0ff28652019-06-24 18:57:39 +02005395 if (!ctx)
5396 return -1;
5397
Willy Tarreau113d52b2020-01-10 09:20:26 +01005398 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01005399 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005400
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01005401 ctx->subs = es;
5402 es->events |= event_type;
Willy Tarreau113d52b2020-01-10 09:20:26 +01005403
5404 /* we may have to subscribe to lower layers for new events */
5405 event_type &= ~ctx->wait_event.events;
5406 if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS))
5407 ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005408 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01005409}
5410
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01005411/* Called from the upper layer, to unsubscribe <es> from events <event_type>.
5412 * The <es> pointer is not allowed to differ from the one passed to the
5413 * subscribe() call. It always returns zero.
5414 */
5415static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es)
Olivier Houcharddf357842019-03-21 16:30:07 +01005416{
Olivier Houchardea8dd942019-05-20 14:02:16 +02005417 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005418
Willy Tarreau113d52b2020-01-10 09:20:26 +01005419 BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV));
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01005420 BUG_ON(ctx->subs && ctx->subs != es);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005421
Willy Tarreauee1a6fc2020-01-17 07:52:13 +01005422 es->events &= ~event_type;
5423 if (!es->events)
Willy Tarreau113d52b2020-01-10 09:20:26 +01005424 ctx->subs = NULL;
5425
5426 /* If we subscribed, and we're not doing the handshake,
5427 * then we subscribed because the upper layer asked for it,
5428 * as the upper layer is no longer interested, we can
5429 * unsubscribe too.
5430 */
5431 event_type &= ctx->wait_event.events;
5432 if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS))
5433 conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005434
5435 return 0;
Olivier Houcharddf357842019-03-21 16:30:07 +01005436}
5437
Olivier Houchard2e055482019-05-27 19:50:12 +02005438/* Use the provided XPRT as an underlying XPRT, and provide the old one.
5439 * Returns 0 on success, and non-zero on failure.
5440 */
5441static 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)
5442{
5443 struct ssl_sock_ctx *ctx = xprt_ctx;
5444
5445 if (oldxprt_ops != NULL)
5446 *oldxprt_ops = ctx->xprt;
5447 if (oldxprt_ctx != NULL)
5448 *oldxprt_ctx = ctx->xprt_ctx;
5449 ctx->xprt = toadd_ops;
5450 ctx->xprt_ctx = toadd_ctx;
5451 return 0;
5452}
5453
Olivier Houchard5149b592019-05-23 17:47:36 +02005454/* Remove the specified xprt. If if it our underlying XPRT, remove it and
5455 * return 0, otherwise just call the remove_xprt method from the underlying
5456 * XPRT.
5457 */
5458static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx)
5459{
5460 struct ssl_sock_ctx *ctx = xprt_ctx;
5461
5462 if (ctx->xprt_ctx == toremove_ctx) {
5463 ctx->xprt_ctx = newctx;
5464 ctx->xprt = newops;
5465 return 0;
5466 }
5467 return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx));
5468}
5469
Olivier Houchardea8dd942019-05-20 14:02:16 +02005470static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state)
5471{
5472 struct ssl_sock_ctx *ctx = context;
5473
5474 /* First if we're doing an handshake, try that */
5475 if (ctx->conn->flags & CO_FL_SSL_WAIT_HS)
5476 ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS);
5477 /* If we had an error, or the handshake is done and I/O is available,
5478 * let the upper layer know.
Olivier Houchard477902b2020-01-22 18:08:48 +01005479 * If no mux was set up yet, then call conn_create_mux()
Olivier Houchardea8dd942019-05-20 14:02:16 +02005480 * we can't be sure conn_fd_handler() will be called again.
5481 */
5482 if ((ctx->conn->flags & CO_FL_ERROR) ||
5483 !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) {
5484 int ret = 0;
5485 int woke = 0;
5486
5487 /* On error, wake any waiter */
Willy Tarreau113d52b2020-01-10 09:20:26 +01005488 if (ctx->subs) {
5489 tasklet_wakeup(ctx->subs->tasklet);
5490 ctx->subs->events = 0;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005491 woke = 1;
Willy Tarreau113d52b2020-01-10 09:20:26 +01005492 ctx->subs = NULL;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005493 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01005494
Olivier Houchardea8dd942019-05-20 14:02:16 +02005495 /* If we're the first xprt for the connection, let the
Olivier Houchard477902b2020-01-22 18:08:48 +01005496 * upper layers know. If we have no mux, create it,
5497 * and once we have a mux, call its wake method if we didn't
5498 * woke a tasklet already.
Olivier Houchardea8dd942019-05-20 14:02:16 +02005499 */
5500 if (ctx->conn->xprt_ctx == ctx) {
Olivier Houchard477902b2020-01-22 18:08:48 +01005501 if (!ctx->conn->mux)
5502 ret = conn_create_mux(ctx->conn);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005503 if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake)
5504 ctx->conn->mux->wake(ctx->conn);
5505 return NULL;
5506 }
5507 }
Olivier Houchard54907bb2019-12-19 15:02:39 +01005508#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
5509 /* If we have early data and somebody wants to receive, let them */
Willy Tarreau113d52b2020-01-10 09:20:26 +01005510 else if (b_data(&ctx->early_buf) && ctx->subs &&
5511 ctx->subs->events & SUB_RETRY_RECV) {
5512 tasklet_wakeup(ctx->subs->tasklet);
5513 ctx->subs->events &= ~SUB_RETRY_RECV;
5514 if (!ctx->subs->events)
5515 ctx->subs = NULL;
Olivier Houchard54907bb2019-12-19 15:02:39 +01005516 }
5517#endif
Olivier Houchardea8dd942019-05-20 14:02:16 +02005518 return NULL;
5519}
5520
Emeric Brun46591952012-05-18 15:47:34 +02005521/* Receive up to <count> bytes from connection <conn>'s socket and store them
Willy Tarreauabf08d92014-01-14 11:31:27 +01005522 * into buffer <buf>. Only one call to recv() is performed, unless the
Emeric Brun46591952012-05-18 15:47:34 +02005523 * buffer wraps, in which case a second call may be performed. The connection's
5524 * flags are updated with whatever special event is detected (error, read0,
5525 * empty). The caller is responsible for taking care of those events and
5526 * avoiding the call if inappropriate. The function does not call the
5527 * connection's polling update function, so the caller is responsible for this.
5528 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005529static 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 +02005530{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005531 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreaubfc4d772018-07-18 11:22:03 +02005532 ssize_t ret;
5533 size_t try, done = 0;
Emeric Brun46591952012-05-18 15:47:34 +02005534
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005535 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005536 goto out_error;
5537
Olivier Houchard54907bb2019-12-19 15:02:39 +01005538#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
5539 if (b_data(&ctx->early_buf)) {
5540 try = b_contig_space(buf);
5541 if (try > b_data(&ctx->early_buf))
5542 try = b_data(&ctx->early_buf);
5543 memcpy(b_tail(buf), b_head(&ctx->early_buf), try);
5544 b_add(buf, try);
5545 b_del(&ctx->early_buf, try);
5546 if (b_data(&ctx->early_buf) == 0)
5547 b_free(&ctx->early_buf);
5548 return try;
5549 }
5550#endif
5551
Willy Tarreau911db9b2020-01-23 16:27:54 +01005552 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02005553 /* a handshake was requested */
5554 return 0;
5555
Emeric Brun46591952012-05-18 15:47:34 +02005556 /* read the largest possible block. For this, we perform only one call
5557 * to recv() unless the buffer wraps and we exactly fill the first hunk,
5558 * in which case we accept to do it once again. A new attempt is made on
5559 * EINTR too.
5560 */
Willy Tarreau00b0fb92014-01-17 11:09:40 +01005561 while (count > 0) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02005562
Willy Tarreau591d4452018-06-15 17:21:00 +02005563 try = b_contig_space(buf);
5564 if (!try)
5565 break;
5566
Willy Tarreauabf08d92014-01-14 11:31:27 +01005567 if (try > count)
5568 try = count;
Willy Tarreau591d4452018-06-15 17:21:00 +02005569
Olivier Houchard66ab4982019-02-26 18:37:15 +01005570 ret = SSL_read(ctx->ssl, b_tail(buf), try);
Emmanuel Hocdetf967c312019-08-05 18:04:16 +02005571
Emeric Brune1f38db2012-09-03 20:36:47 +02005572 if (conn->flags & CO_FL_ERROR) {
5573 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01005574 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02005575 }
Emeric Brun46591952012-05-18 15:47:34 +02005576 if (ret > 0) {
Olivier Houchardacd14032018-06-28 18:17:23 +02005577 b_add(buf, ret);
Emeric Brun46591952012-05-18 15:47:34 +02005578 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02005579 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02005580 }
Emeric Brun46591952012-05-18 15:47:34 +02005581 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005582 ret = SSL_get_error(ctx->ssl, ret);
Emeric Brun46591952012-05-18 15:47:34 +02005583 if (ret == SSL_ERROR_WANT_WRITE) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01005584 /* handshake is running, and it needs to enable write */
Emeric Brun46591952012-05-18 15:47:34 +02005585 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005586 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02005587#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005588 /* Async mode can be re-enabled, because we're leaving data state.*/
5589 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005590 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005591#endif
Emeric Brun46591952012-05-18 15:47:34 +02005592 break;
5593 }
5594 else if (ret == SSL_ERROR_WANT_READ) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005595 if (SSL_renegotiate_pending(ctx->ssl)) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005596 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
5597 SUB_RETRY_RECV,
5598 &ctx->wait_event);
Emeric Brun282a76a2012-11-08 18:02:56 +01005599 /* handshake is running, and it may need to re-enable read */
5600 conn->flags |= CO_FL_SSL_WAIT_HS;
Willy Tarreau5db847a2019-05-09 14:13:35 +02005601#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005602 /* Async mode can be re-enabled, because we're leaving data state.*/
5603 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005604 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005605#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01005606 break;
5607 }
Emeric Brun46591952012-05-18 15:47:34 +02005608 break;
Olivier Houchardc2aae742017-09-22 18:26:28 +02005609 } else if (ret == SSL_ERROR_ZERO_RETURN)
5610 goto read0;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01005611 /* For SSL_ERROR_SYSCALL, make sure to clear the error
5612 * stack before shutting down the connection for
5613 * reading. */
Olivier Houchard7e2e5052018-02-13 15:17:23 +01005614 if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN))
5615 goto clear_ssl_error;
Emeric Brun46591952012-05-18 15:47:34 +02005616 /* otherwise it's a real error */
5617 goto out_error;
5618 }
5619 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005620 leave:
Emeric Brun46591952012-05-18 15:47:34 +02005621 return done;
5622
Christopher Faulet4ac77a92018-02-19 14:25:15 +01005623 clear_ssl_error:
5624 /* Clear openssl global errors stack */
5625 ssl_sock_dump_errors(conn);
5626 ERR_clear_error();
Emeric Brun46591952012-05-18 15:47:34 +02005627 read0:
5628 conn_sock_read0(conn);
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005629 goto leave;
Christopher Faulet4ac77a92018-02-19 14:25:15 +01005630
Emeric Brun46591952012-05-18 15:47:34 +02005631 out_error:
Olivier Houchard7e2e5052018-02-13 15:17:23 +01005632 conn->flags |= CO_FL_ERROR;
Emeric Brun644cde02012-12-14 11:21:13 +01005633 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02005634 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01005635 ERR_clear_error();
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005636 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02005637}
5638
5639
Willy Tarreau787db9a2018-06-14 18:31:46 +02005640/* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s
5641 * socket. <flags> may contain some CO_SFL_* flags to hint the system about
5642 * other pending data for example, but this flag is ignored at the moment.
Emeric Brun46591952012-05-18 15:47:34 +02005643 * Only one call to send() is performed, unless the buffer wraps, in which case
5644 * a second call may be performed. The connection's flags are updated with
5645 * whatever special event is detected (error, empty). The caller is responsible
5646 * for taking care of those events and avoiding the call if inappropriate. The
5647 * function does not call the connection's polling update function, so the caller
Willy Tarreau787db9a2018-06-14 18:31:46 +02005648 * is responsible for this. The buffer's output is not adjusted, it's up to the
5649 * caller to take care of this. It's up to the caller to update the buffer's
5650 * contents based on the return value.
Emeric Brun46591952012-05-18 15:47:34 +02005651 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005652static 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 +02005653{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005654 struct ssl_sock_ctx *ctx = xprt_ctx;
Willy Tarreau787db9a2018-06-14 18:31:46 +02005655 ssize_t ret;
5656 size_t try, done;
Emeric Brun46591952012-05-18 15:47:34 +02005657
5658 done = 0;
5659
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005660 if (!ctx)
Emeric Brun46591952012-05-18 15:47:34 +02005661 goto out_error;
5662
Willy Tarreau911db9b2020-01-23 16:27:54 +01005663 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS | CO_FL_EARLY_SSL_HS))
Emeric Brun46591952012-05-18 15:47:34 +02005664 /* a handshake was requested */
5665 return 0;
5666
5667 /* send the largest possible block. For this we perform only one call
5668 * to send() unless the buffer wraps and we exactly fill the first hunk,
5669 * in which case we accept to do it once again.
5670 */
Willy Tarreau787db9a2018-06-14 18:31:46 +02005671 while (count) {
Willy Tarreau5db847a2019-05-09 14:13:35 +02005672#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchardc2aae742017-09-22 18:26:28 +02005673 size_t written_data;
5674#endif
5675
Willy Tarreau787db9a2018-06-14 18:31:46 +02005676 try = b_contig_data(buf, done);
5677 if (try > count)
5678 try = count;
Willy Tarreaubfd59462013-02-21 07:46:09 +01005679
Willy Tarreau7bed9452014-02-02 02:00:24 +01005680 if (!(flags & CO_SFL_STREAMER) &&
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005681 !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) &&
Willy Tarreauef934602016-12-22 23:12:01 +01005682 global_ssl.max_record && try > global_ssl.max_record) {
5683 try = global_ssl.max_record;
Willy Tarreau518cedd2014-02-17 15:43:01 +01005684 }
5685 else {
5686 /* we need to keep the information about the fact that
5687 * we're not limiting the upcoming send(), because if it
5688 * fails, we'll have to retry with at least as many data.
5689 */
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005690 ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau518cedd2014-02-17 15:43:01 +01005691 }
Willy Tarreaubfd59462013-02-21 07:46:09 +01005692
Willy Tarreau5db847a2019-05-09 14:13:35 +02005693#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Olivier Houchard010941f2019-05-03 20:56:19 +02005694 if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) {
Olivier Houchardc2aae742017-09-22 18:26:28 +02005695 unsigned int max_early;
5696
Olivier Houchard522eea72017-11-03 16:27:47 +01005697 if (objt_listener(conn->target))
Olivier Houchard66ab4982019-02-26 18:37:15 +01005698 max_early = SSL_get_max_early_data(ctx->ssl);
Olivier Houchard522eea72017-11-03 16:27:47 +01005699 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005700 if (SSL_get0_session(ctx->ssl))
5701 max_early = SSL_SESSION_get_max_early_data(SSL_get0_session(ctx->ssl));
Olivier Houchard522eea72017-11-03 16:27:47 +01005702 else
5703 max_early = 0;
5704 }
5705
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005706 if (try + ctx->sent_early_data > max_early) {
5707 try -= (try + ctx->sent_early_data) - max_early;
Olivier Houchard522eea72017-11-03 16:27:47 +01005708 if (try <= 0) {
Olivier Houchard010941f2019-05-03 20:56:19 +02005709 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN;
Olivier Houchard965e84e2019-06-15 20:59:30 +02005710 tasklet_wakeup(ctx->wait_event.tasklet);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005711 break;
Olivier Houchard522eea72017-11-03 16:27:47 +01005712 }
Olivier Houchardc2aae742017-09-22 18:26:28 +02005713 }
Olivier Houchard66ab4982019-02-26 18:37:15 +01005714 ret = SSL_write_early_data(ctx->ssl, b_peek(buf, done), try, &written_data);
Olivier Houchardc2aae742017-09-22 18:26:28 +02005715 if (ret == 1) {
5716 ret = written_data;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005717 ctx->sent_early_data += ret;
Olivier Houchard965e84e2019-06-15 20:59:30 +02005718 if (objt_server(conn->target)) {
Olivier Houchard522eea72017-11-03 16:27:47 +01005719 conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA;
Olivier Houchard965e84e2019-06-15 20:59:30 +02005720 /* Initiate the handshake, now */
5721 tasklet_wakeup(ctx->wait_event.tasklet);
5722 }
Olivier Houchard522eea72017-11-03 16:27:47 +01005723
Olivier Houchardc2aae742017-09-22 18:26:28 +02005724 }
5725
5726 } else
5727#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01005728 ret = SSL_write(ctx->ssl, b_peek(buf, done), try);
Willy Tarreau518cedd2014-02-17 15:43:01 +01005729
Emeric Brune1f38db2012-09-03 20:36:47 +02005730 if (conn->flags & CO_FL_ERROR) {
5731 /* CO_FL_ERROR may be set by ssl_sock_infocbk */
Emeric Brun644cde02012-12-14 11:21:13 +01005732 goto out_error;
Emeric Brune1f38db2012-09-03 20:36:47 +02005733 }
Emeric Brun46591952012-05-18 15:47:34 +02005734 if (ret > 0) {
Willy Tarreauc192b0a2020-01-23 09:11:58 +01005735 /* A send succeeded, so we can consider ourself connected */
5736 conn->flags &= ~CO_FL_WAIT_L4L6;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01005737 ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED;
Willy Tarreau787db9a2018-06-14 18:31:46 +02005738 count -= ret;
Emeric Brun46591952012-05-18 15:47:34 +02005739 done += ret;
Emeric Brun46591952012-05-18 15:47:34 +02005740 }
5741 else {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005742 ret = SSL_get_error(ctx->ssl, ret);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005743
Emeric Brun46591952012-05-18 15:47:34 +02005744 if (ret == SSL_ERROR_WANT_WRITE) {
Olivier Houchard66ab4982019-02-26 18:37:15 +01005745 if (SSL_renegotiate_pending(ctx->ssl)) {
Emeric Brun282a76a2012-11-08 18:02:56 +01005746 /* handshake is running, and it may need to re-enable write */
5747 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005748 ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02005749#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005750 /* Async mode can be re-enabled, because we're leaving data state.*/
5751 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005752 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005753#endif
Emeric Brun282a76a2012-11-08 18:02:56 +01005754 break;
5755 }
Olivier Houchardea8dd942019-05-20 14:02:16 +02005756
Emeric Brun46591952012-05-18 15:47:34 +02005757 break;
5758 }
5759 else if (ret == SSL_ERROR_WANT_READ) {
Emeric Brun8af8dd12012-11-08 17:56:20 +01005760 /* handshake is running, and it needs to enable read */
Emeric Brun46591952012-05-18 15:47:34 +02005761 conn->flags |= CO_FL_SSL_WAIT_HS;
Olivier Houchardea8dd942019-05-20 14:02:16 +02005762 ctx->xprt->subscribe(conn, ctx->xprt_ctx,
5763 SUB_RETRY_RECV,
5764 &ctx->wait_event);
Willy Tarreau5db847a2019-05-09 14:13:35 +02005765#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brunb5e42a82017-06-06 12:35:14 +00005766 /* Async mode can be re-enabled, because we're leaving data state.*/
5767 if (global_ssl.async)
Olivier Houchard66ab4982019-02-26 18:37:15 +01005768 SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC);
Emeric Brunb5e42a82017-06-06 12:35:14 +00005769#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005770 break;
5771 }
Emeric Brun46591952012-05-18 15:47:34 +02005772 goto out_error;
5773 }
5774 }
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005775 leave:
Emeric Brun46591952012-05-18 15:47:34 +02005776 return done;
5777
5778 out_error:
Emeric Brun644cde02012-12-14 11:21:13 +01005779 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02005780 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01005781 ERR_clear_error();
5782
Emeric Brun46591952012-05-18 15:47:34 +02005783 conn->flags |= CO_FL_ERROR;
Willy Tarreau31d4dbe2017-10-25 09:32:15 +02005784 goto leave;
Emeric Brun46591952012-05-18 15:47:34 +02005785}
5786
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005787static void ssl_sock_close(struct connection *conn, void *xprt_ctx) {
Emeric Brun46591952012-05-18 15:47:34 +02005788
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005789 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005790
Olivier Houchardea8dd942019-05-20 14:02:16 +02005791
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005792 if (ctx) {
Olivier Houchardea8dd942019-05-20 14:02:16 +02005793 if (ctx->wait_event.events != 0)
5794 ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx,
5795 ctx->wait_event.events,
5796 &ctx->wait_event);
Willy Tarreau113d52b2020-01-10 09:20:26 +01005797 if (ctx->subs) {
5798 ctx->subs->events = 0;
5799 tasklet_wakeup(ctx->subs->tasklet);
Olivier Houchardea8dd942019-05-20 14:02:16 +02005800 }
Willy Tarreau113d52b2020-01-10 09:20:26 +01005801
Olivier Houchard692c1d02019-05-23 18:41:47 +02005802 if (ctx->xprt->close)
5803 ctx->xprt->close(conn, ctx->xprt_ctx);
Willy Tarreau5db847a2019-05-09 14:13:35 +02005804#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Emeric Brun3854e012017-05-17 20:42:48 +02005805 if (global_ssl.async) {
5806 OSSL_ASYNC_FD all_fd[32], afd;
5807 size_t num_all_fds = 0;
5808 int i;
5809
Olivier Houchard66ab4982019-02-26 18:37:15 +01005810 SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02005811 if (num_all_fds > 32) {
5812 send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n");
5813 return;
5814 }
5815
Olivier Houchard66ab4982019-02-26 18:37:15 +01005816 SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds);
Emeric Brun3854e012017-05-17 20:42:48 +02005817
5818 /* If an async job is pending, we must try to
5819 to catch the end using polling before calling
5820 SSL_free */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005821 if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) {
Emeric Brun3854e012017-05-17 20:42:48 +02005822 for (i=0 ; i < num_all_fds ; i++) {
5823 /* switch on an handler designed to
5824 * handle the SSL_free
5825 */
5826 afd = all_fd[i];
5827 fdtab[afd].iocb = ssl_async_fd_free;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005828 fdtab[afd].owner = ctx->ssl;
Emeric Brun3854e012017-05-17 20:42:48 +02005829 fd_want_recv(afd);
Emeric Brunce9e01c2017-05-31 10:02:53 +00005830 /* To ensure that the fd cache won't be used
5831 * and we'll catch a real RD event.
5832 */
5833 fd_cant_recv(afd);
Emeric Brun3854e012017-05-17 20:42:48 +02005834 }
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005835 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005836 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005837 _HA_ATOMIC_ADD(&jobs, 1);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005838 return;
5839 }
Emeric Brun3854e012017-05-17 20:42:48 +02005840 /* Else we can remove the fds from the fdtab
5841 * and call SSL_free.
5842 * note: we do a fd_remove and not a delete
5843 * because the fd is owned by the engine.
5844 * the engine is responsible to close
5845 */
5846 for (i=0 ; i < num_all_fds ; i++)
5847 fd_remove(all_fd[i]);
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00005848 }
5849#endif
Olivier Houchard66ab4982019-02-26 18:37:15 +01005850 SSL_free(ctx->ssl);
Olivier Houchard54907bb2019-12-19 15:02:39 +01005851 b_free(&ctx->early_buf);
Willy Tarreau3c39a7d2019-06-14 14:42:29 +02005852 tasklet_free(ctx->wait_event.tasklet);
Olivier Houchard66ab4982019-02-26 18:37:15 +01005853 pool_free(ssl_sock_ctx_pool, ctx);
Olivier Houchard2be5a4c2019-03-08 18:54:43 +01005854 _HA_ATOMIC_SUB(&sslconns, 1);
Emeric Brun46591952012-05-18 15:47:34 +02005855 }
Emeric Brun46591952012-05-18 15:47:34 +02005856}
5857
5858/* This function tries to perform a clean shutdown on an SSL connection, and in
5859 * any case, flags the connection as reusable if no handshake was in progress.
5860 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005861static void ssl_sock_shutw(struct connection *conn, void *xprt_ctx, int clean)
Emeric Brun46591952012-05-18 15:47:34 +02005862{
Olivier Houcharde179d0e2019-03-21 18:27:17 +01005863 struct ssl_sock_ctx *ctx = xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005864
Willy Tarreau911db9b2020-01-23 16:27:54 +01005865 if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS))
Emeric Brun46591952012-05-18 15:47:34 +02005866 return;
Emmanuel Hocdet405ff312017-01-08 14:07:39 +01005867 if (!clean)
5868 /* don't sent notify on SSL_shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005869 SSL_set_quiet_shutdown(ctx->ssl, 1);
Emeric Brun46591952012-05-18 15:47:34 +02005870 /* no handshake was in progress, try a clean ssl shutdown */
Olivier Houchard66ab4982019-02-26 18:37:15 +01005871 if (SSL_shutdown(ctx->ssl) <= 0) {
Emeric Brun644cde02012-12-14 11:21:13 +01005872 /* Clear openssl global errors stack */
Thierry FOURNIER / OZON.IO8b068c22016-10-10 11:59:50 +02005873 ssl_sock_dump_errors(conn);
Emeric Brun644cde02012-12-14 11:21:13 +01005874 ERR_clear_error();
5875 }
Emeric Brun46591952012-05-18 15:47:34 +02005876}
5877
William Lallemandd4f946c2019-12-05 10:26:40 +01005878/* fill a buffer with the algorithm and size of a public key */
5879static int cert_get_pkey_algo(X509 *crt, struct buffer *out)
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01005880{
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01005881 int bits = 0;
5882 int sig = TLSEXT_signature_anonymous;
5883 int len = -1;
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01005884 EVP_PKEY *pkey;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01005885
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01005886 pkey = X509_get_pubkey(crt);
5887 if (pkey) {
5888 bits = EVP_PKEY_bits(pkey);
5889 switch(EVP_PKEY_base_id(pkey)) {
5890 case EVP_PKEY_RSA:
5891 sig = TLSEXT_signature_rsa;
5892 break;
5893 case EVP_PKEY_EC:
5894 sig = TLSEXT_signature_ecdsa;
5895 break;
5896 case EVP_PKEY_DSA:
5897 sig = TLSEXT_signature_dsa;
5898 break;
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01005899 }
Emmanuel Hocdetc3775d22019-11-04 18:19:32 +01005900 EVP_PKEY_free(pkey);
Emmanuel Hocdet96b78342017-10-31 15:46:07 +01005901 }
5902
5903 switch(sig) {
5904 case TLSEXT_signature_rsa:
5905 len = chunk_printf(out, "RSA%d", bits);
5906 break;
5907 case TLSEXT_signature_ecdsa:
5908 len = chunk_printf(out, "EC%d", bits);
5909 break;
5910 case TLSEXT_signature_dsa:
5911 len = chunk_printf(out, "DSA%d", bits);
5912 break;
5913 default:
5914 return 0;
5915 }
5916 if (len < 0)
5917 return 0;
5918 return 1;
5919}
5920
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +05005921/* used for ppv2 pkey algo (can be used for logging) */
William Lallemandd4f946c2019-12-05 10:26:40 +01005922int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out)
5923{
5924 struct ssl_sock_ctx *ctx;
5925 X509 *crt;
5926
5927 if (!ssl_sock_is_ssl(conn))
5928 return 0;
5929
5930 ctx = conn->xprt_ctx;
5931
5932 crt = SSL_get_certificate(ctx->ssl);
5933 if (!crt)
5934 return 0;
5935
5936 return cert_get_pkey_algo(crt, out);
5937}
5938
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01005939/* used for ppv2 cert signature (can be used for logging) */
5940const char *ssl_sock_get_cert_sig(struct connection *conn)
5941{
Christopher Faulet82004142019-09-10 10:12:03 +02005942 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005943
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01005944 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
5945 X509 *crt;
5946
5947 if (!ssl_sock_is_ssl(conn))
5948 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02005949 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005950 crt = SSL_get_certificate(ctx->ssl);
Emmanuel Hocdet283e0042017-11-02 14:05:23 +01005951 if (!crt)
5952 return NULL;
5953 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
5954 return OBJ_nid2sn(OBJ_obj2nid(algorithm));
5955}
5956
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01005957/* used for ppv2 authority */
5958const char *ssl_sock_get_sni(struct connection *conn)
5959{
5960#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02005961 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005962
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01005963 if (!ssl_sock_is_ssl(conn))
5964 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02005965 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005966 return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01005967#else
Olivier Houchard66ab4982019-02-26 18:37:15 +01005968 return NULL;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01005969#endif
5970}
5971
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02005972/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02005973const char *ssl_sock_get_cipher_name(struct connection *conn)
5974{
Christopher Faulet82004142019-09-10 10:12:03 +02005975 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005976
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02005977 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02005978 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02005979 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005980 return SSL_get_cipher_name(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02005981}
5982
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02005983/* used for logging/ppv2, may be changed for a sample fetch later */
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02005984const char *ssl_sock_get_proto_version(struct connection *conn)
5985{
Christopher Faulet82004142019-09-10 10:12:03 +02005986 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005987
Emmanuel Hocdet01da5712017-10-13 16:59:49 +02005988 if (!ssl_sock_is_ssl(conn))
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02005989 return NULL;
Christopher Faulet82004142019-09-10 10:12:03 +02005990 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01005991 return SSL_get_version(ctx->ssl);
Willy Tarreauffc3fcd2012-10-12 20:17:54 +02005992}
5993
Willy Tarreau8d598402012-10-22 17:58:39 +02005994/* Extract a serial from a cert, and copy it to a chunk.
5995 * Returns 1 if serial is found and copied, 0 if no serial found and
5996 * -1 if output is not large enough.
5997 */
5998static int
Willy Tarreau83061a82018-07-13 11:56:34 +02005999ssl_sock_get_serial(X509 *crt, struct buffer *out)
Willy Tarreau8d598402012-10-22 17:58:39 +02006000{
6001 ASN1_INTEGER *serial;
6002
6003 serial = X509_get_serialNumber(crt);
6004 if (!serial)
6005 return 0;
6006
6007 if (out->size < serial->length)
6008 return -1;
6009
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006010 memcpy(out->area, serial->data, serial->length);
6011 out->data = serial->length;
Willy Tarreau8d598402012-10-22 17:58:39 +02006012 return 1;
6013}
6014
Emeric Brun43e79582014-10-29 19:03:26 +01006015/* Extract a cert to der, and copy it to a chunk.
Joseph Herlant017b3da2018-11-15 09:07:59 -08006016 * Returns 1 if the cert is found and copied, 0 on der conversion failure
6017 * and -1 if the output is not large enough.
Emeric Brun43e79582014-10-29 19:03:26 +01006018 */
6019static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006020ssl_sock_crt2der(X509 *crt, struct buffer *out)
Emeric Brun43e79582014-10-29 19:03:26 +01006021{
6022 int len;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006023 unsigned char *p = (unsigned char *) out->area;;
Emeric Brun43e79582014-10-29 19:03:26 +01006024
6025 len =i2d_X509(crt, NULL);
6026 if (len <= 0)
6027 return 1;
6028
6029 if (out->size < len)
6030 return -1;
6031
6032 i2d_X509(crt,&p);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006033 out->data = len;
Emeric Brun43e79582014-10-29 19:03:26 +01006034 return 1;
6035}
6036
Emeric Brunce5ad802012-10-22 14:11:22 +02006037
Willy Tarreau83061a82018-07-13 11:56:34 +02006038/* Copy Date in ASN1_UTCTIME format in struct buffer out.
Emeric Brunce5ad802012-10-22 14:11:22 +02006039 * Returns 1 if serial is found and copied, 0 if no valid time found
6040 * and -1 if output is not large enough.
6041 */
6042static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006043ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out)
Emeric Brunce5ad802012-10-22 14:11:22 +02006044{
6045 if (tm->type == V_ASN1_GENERALIZEDTIME) {
6046 ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm;
6047
6048 if (gentm->length < 12)
6049 return 0;
6050 if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30)
6051 return 0;
6052 if (out->size < gentm->length-2)
6053 return -1;
6054
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006055 memcpy(out->area, gentm->data+2, gentm->length-2);
6056 out->data = gentm->length-2;
Emeric Brunce5ad802012-10-22 14:11:22 +02006057 return 1;
6058 }
6059 else if (tm->type == V_ASN1_UTCTIME) {
6060 ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm;
6061
6062 if (utctm->length < 10)
6063 return 0;
6064 if (utctm->data[0] >= 0x35)
6065 return 0;
6066 if (out->size < utctm->length)
6067 return -1;
6068
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006069 memcpy(out->area, utctm->data, utctm->length);
6070 out->data = utctm->length;
Emeric Brunce5ad802012-10-22 14:11:22 +02006071 return 1;
6072 }
6073
6074 return 0;
6075}
6076
Emeric Brun87855892012-10-17 17:39:35 +02006077/* Extract an entry from a X509_NAME and copy its value to an output chunk.
6078 * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough.
6079 */
6080static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006081ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos,
6082 struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02006083{
6084 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006085 ASN1_OBJECT *obj;
6086 ASN1_STRING *data;
6087 const unsigned char *data_ptr;
6088 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006089 int i, j, n;
6090 int cur = 0;
6091 const char *s;
6092 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006093 int name_count;
6094
6095 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02006096
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006097 out->data = 0;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006098 for (i = 0; i < name_count; i++) {
Emeric Brun87855892012-10-17 17:39:35 +02006099 if (pos < 0)
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006100 j = (name_count-1) - i;
Emeric Brun87855892012-10-17 17:39:35 +02006101 else
6102 j = i;
6103
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006104 ne = X509_NAME_get_entry(a, j);
6105 obj = X509_NAME_ENTRY_get_object(ne);
6106 data = X509_NAME_ENTRY_get_data(ne);
6107 data_ptr = ASN1_STRING_get0_data(data);
6108 data_len = ASN1_STRING_length(data);
6109 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02006110 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006111 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02006112 s = tmp;
6113 }
6114
6115 if (chunk_strcasecmp(entry, s) != 0)
6116 continue;
6117
6118 if (pos < 0)
6119 cur--;
6120 else
6121 cur++;
6122
6123 if (cur != pos)
6124 continue;
6125
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006126 if (data_len > out->size)
Emeric Brun87855892012-10-17 17:39:35 +02006127 return -1;
6128
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006129 memcpy(out->area, data_ptr, data_len);
6130 out->data = data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006131 return 1;
6132 }
6133
6134 return 0;
6135
William Lallemandd4f946c2019-12-05 10:26:40 +01006136}
6137
6138/*
6139 * Extract and format the DNS SAN extensions and copy result into a chuink
6140 * Return 0;
6141 */
6142#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
6143static int ssl_sock_get_san_oneline(X509 *cert, struct buffer *out)
6144{
6145 int i;
6146 char *str;
6147 STACK_OF(GENERAL_NAME) *names = NULL;
6148
6149 names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
6150 if (names) {
6151 for (i = 0; i < sk_GENERAL_NAME_num(names); i++) {
6152 GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i);
6153 if (i > 0)
6154 chunk_appendf(out, ", ");
6155 if (name->type == GEN_DNS) {
6156 if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) {
6157 chunk_appendf(out, "DNS:%s", str);
6158 OPENSSL_free(str);
6159 }
6160 }
6161 }
6162 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
6163 }
6164 return 0;
Emeric Brun87855892012-10-17 17:39:35 +02006165}
William Lallemandd4f946c2019-12-05 10:26:40 +01006166#endif
Emeric Brun87855892012-10-17 17:39:35 +02006167
Elliot Otchet71f82972020-01-15 08:12:14 -05006168/*
6169 * Extract the DN in the specified format from the X509_NAME and copy result to a chunk.
6170 * Currently supports rfc2253 for returning LDAP V3 DNs.
6171 * Returns 1 if dn entries exist, 0 if no dn entry was found.
6172 */
6173static int
6174ssl_sock_get_dn_formatted(X509_NAME *a, const struct buffer *format, struct buffer *out)
6175{
6176 BIO *bio = NULL;
6177 int ret = 0;
6178 int data_len = 0;
6179
6180 if (chunk_strcmp(format, "rfc2253") == 0) {
6181 bio = BIO_new(BIO_s_mem());
6182 if (bio == NULL)
6183 goto out;
6184
6185 if (X509_NAME_print_ex(bio, a, 0, XN_FLAG_RFC2253) < 0)
6186 goto out;
6187
6188 if ((data_len = BIO_read(bio, out->area, out->size)) <= 0)
6189 goto out;
6190
6191 out->data = data_len;
6192
6193 ret = 1;
6194 }
6195out:
6196 if (bio)
6197 BIO_free(bio);
6198 return ret;
6199}
6200
Emeric Brun87855892012-10-17 17:39:35 +02006201/* Extract and format full DN from a X509_NAME and copy result into a chunk
6202 * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough.
6203 */
6204static int
Willy Tarreau83061a82018-07-13 11:56:34 +02006205ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out)
Emeric Brun87855892012-10-17 17:39:35 +02006206{
6207 X509_NAME_ENTRY *ne;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006208 ASN1_OBJECT *obj;
6209 ASN1_STRING *data;
6210 const unsigned char *data_ptr;
6211 int data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006212 int i, n, ln;
6213 int l = 0;
6214 const char *s;
6215 char *p;
6216 char tmp[128];
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006217 int name_count;
6218
6219
6220 name_count = X509_NAME_entry_count(a);
Emeric Brun87855892012-10-17 17:39:35 +02006221
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006222 out->data = 0;
6223 p = out->area;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006224 for (i = 0; i < name_count; i++) {
6225 ne = X509_NAME_get_entry(a, i);
6226 obj = X509_NAME_ENTRY_get_object(ne);
6227 data = X509_NAME_ENTRY_get_data(ne);
6228 data_ptr = ASN1_STRING_get0_data(data);
6229 data_len = ASN1_STRING_length(data);
6230 n = OBJ_obj2nid(obj);
Emeric Brun87855892012-10-17 17:39:35 +02006231 if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006232 i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj);
Emeric Brun87855892012-10-17 17:39:35 +02006233 s = tmp;
6234 }
6235 ln = strlen(s);
6236
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006237 l += 1 + ln + 1 + data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006238 if (l > out->size)
6239 return -1;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006240 out->data = l;
Emeric Brun87855892012-10-17 17:39:35 +02006241
6242 *(p++)='/';
6243 memcpy(p, s, ln);
6244 p += ln;
6245 *(p++)='=';
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006246 memcpy(p, data_ptr, data_len);
6247 p += data_len;
Emeric Brun87855892012-10-17 17:39:35 +02006248 }
6249
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006250 if (!out->data)
Emeric Brun87855892012-10-17 17:39:35 +02006251 return 0;
6252
6253 return 1;
6254}
6255
Olivier Houchardab28a322018-12-21 19:45:40 +01006256void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len)
6257{
6258#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Christopher Faulet82004142019-09-10 10:12:03 +02006259 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006260
Olivier Houcharde488ea82019-06-28 14:10:33 +02006261 if (!ssl_sock_is_ssl(conn))
6262 return;
Christopher Faulet82004142019-09-10 10:12:03 +02006263 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006264 SSL_set_alpn_protos(ctx->ssl, alpn, len);
Olivier Houchardab28a322018-12-21 19:45:40 +01006265#endif
6266}
6267
Willy Tarreau119a4082016-12-22 21:58:38 +01006268/* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL
6269 * to disable SNI.
6270 */
Willy Tarreau63076412015-07-10 11:33:32 +02006271void ssl_sock_set_servername(struct connection *conn, const char *hostname)
6272{
6273#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Christopher Faulet82004142019-09-10 10:12:03 +02006274 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006275
Willy Tarreau119a4082016-12-22 21:58:38 +01006276 char *prev_name;
6277
Willy Tarreau63076412015-07-10 11:33:32 +02006278 if (!ssl_sock_is_ssl(conn))
6279 return;
Christopher Faulet82004142019-09-10 10:12:03 +02006280 ctx = conn->xprt_ctx;
Willy Tarreau63076412015-07-10 11:33:32 +02006281
Willy Tarreau119a4082016-12-22 21:58:38 +01006282 /* if the SNI changes, we must destroy the reusable context so that a
6283 * new connection will present a new SNI. As an optimization we could
6284 * later imagine having a small cache of ssl_ctx to hold a few SNI per
6285 * server.
6286 */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006287 prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau119a4082016-12-22 21:58:38 +01006288 if ((!prev_name && hostname) ||
6289 (prev_name && (!hostname || strcmp(hostname, prev_name) != 0)))
Olivier Houchard66ab4982019-02-26 18:37:15 +01006290 SSL_set_session(ctx->ssl, NULL);
Willy Tarreau119a4082016-12-22 21:58:38 +01006291
Olivier Houchard66ab4982019-02-26 18:37:15 +01006292 SSL_set_tlsext_host_name(ctx->ssl, hostname);
Willy Tarreau63076412015-07-10 11:33:32 +02006293#endif
6294}
6295
Emeric Brun0abf8362014-06-24 18:26:41 +02006296/* Extract peer certificate's common name into the chunk dest
6297 * Returns
6298 * the len of the extracted common name
6299 * or 0 if no CN found in DN
6300 * or -1 on error case (i.e. no peer certificate)
6301 */
Willy Tarreau83061a82018-07-13 11:56:34 +02006302int ssl_sock_get_remote_common_name(struct connection *conn,
6303 struct buffer *dest)
David Safb76832014-05-08 23:42:08 -04006304{
Christopher Faulet82004142019-09-10 10:12:03 +02006305 struct ssl_sock_ctx *ctx;
David Safb76832014-05-08 23:42:08 -04006306 X509 *crt = NULL;
6307 X509_NAME *name;
David Safb76832014-05-08 23:42:08 -04006308 const char find_cn[] = "CN";
Willy Tarreau83061a82018-07-13 11:56:34 +02006309 const struct buffer find_cn_chunk = {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006310 .area = (char *)&find_cn,
6311 .data = sizeof(find_cn)-1
David Safb76832014-05-08 23:42:08 -04006312 };
Emeric Brun0abf8362014-06-24 18:26:41 +02006313 int result = -1;
David Safb76832014-05-08 23:42:08 -04006314
6315 if (!ssl_sock_is_ssl(conn))
Emeric Brun0abf8362014-06-24 18:26:41 +02006316 goto out;
Christopher Faulet82004142019-09-10 10:12:03 +02006317 ctx = conn->xprt_ctx;
David Safb76832014-05-08 23:42:08 -04006318
6319 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006320 crt = SSL_get_peer_certificate(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04006321 if (!crt)
6322 goto out;
6323
6324 name = X509_get_subject_name(crt);
6325 if (!name)
6326 goto out;
David Safb76832014-05-08 23:42:08 -04006327
Emeric Brun0abf8362014-06-24 18:26:41 +02006328 result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest);
6329out:
David Safb76832014-05-08 23:42:08 -04006330 if (crt)
6331 X509_free(crt);
6332
6333 return result;
6334}
6335
Dave McCowan328fb582014-07-30 10:39:13 -04006336/* returns 1 if client passed a certificate for this session, 0 if not */
6337int ssl_sock_get_cert_used_sess(struct connection *conn)
6338{
Christopher Faulet82004142019-09-10 10:12:03 +02006339 struct ssl_sock_ctx *ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04006340 X509 *crt = NULL;
6341
6342 if (!ssl_sock_is_ssl(conn))
6343 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02006344 ctx = conn->xprt_ctx;
Dave McCowan328fb582014-07-30 10:39:13 -04006345
6346 /* SSL_get_peer_certificate, it increase X509 * ref count */
Olivier Houchard66ab4982019-02-26 18:37:15 +01006347 crt = SSL_get_peer_certificate(ctx->ssl);
Dave McCowan328fb582014-07-30 10:39:13 -04006348 if (!crt)
6349 return 0;
6350
6351 X509_free(crt);
6352 return 1;
6353}
6354
6355/* returns 1 if client passed a certificate for this connection, 0 if not */
6356int ssl_sock_get_cert_used_conn(struct connection *conn)
David Safb76832014-05-08 23:42:08 -04006357{
Christopher Faulet82004142019-09-10 10:12:03 +02006358 struct ssl_sock_ctx *ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006359
David Safb76832014-05-08 23:42:08 -04006360 if (!ssl_sock_is_ssl(conn))
6361 return 0;
Christopher Faulet82004142019-09-10 10:12:03 +02006362 ctx = conn->xprt_ctx;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006363 return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
David Safb76832014-05-08 23:42:08 -04006364}
6365
6366/* returns result from SSL verify */
6367unsigned int ssl_sock_get_verify_result(struct connection *conn)
6368{
Christopher Faulet82004142019-09-10 10:12:03 +02006369 struct ssl_sock_ctx *ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006370
David Safb76832014-05-08 23:42:08 -04006371 if (!ssl_sock_is_ssl(conn))
6372 return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION;
Christopher Faulet82004142019-09-10 10:12:03 +02006373 ctx = conn->xprt_ctx;
Olivier Houchard66ab4982019-02-26 18:37:15 +01006374 return (unsigned int)SSL_get_verify_result(ctx->ssl);
David Safb76832014-05-08 23:42:08 -04006375}
6376
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006377/* Returns the application layer protocol name in <str> and <len> when known.
6378 * Zero is returned if the protocol name was not found, otherwise non-zero is
6379 * returned. The string is allocated in the SSL context and doesn't have to be
6380 * freed by the caller. NPN is also checked if available since older versions
6381 * of openssl (1.0.1) which are more common in field only support this one.
6382 */
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006383static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006384{
Olivier Houchard66ab4982019-02-26 18:37:15 +01006385#if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \
6386 defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006387 struct ssl_sock_ctx *ctx = xprt_ctx;
6388 if (!ctx)
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006389 return 0;
6390
6391 *str = NULL;
6392
6393#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard66ab4982019-02-26 18:37:15 +01006394 SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006395 if (*str)
6396 return 1;
6397#endif
Bernard Spil13c53f82018-02-15 13:34:58 +01006398#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Olivier Houchard66ab4982019-02-26 18:37:15 +01006399 SSL_get0_next_proto_negotiated(ctx->ssl, (const unsigned char **)str, (unsigned *)len);
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006400 if (*str)
6401 return 1;
6402#endif
Olivier Houcharde179d0e2019-03-21 18:27:17 +01006403#endif
Willy Tarreau8743f7e2016-12-04 18:44:29 +01006404 return 0;
6405}
6406
Willy Tarreau7875d092012-09-10 08:20:03 +02006407/***** Below are some sample fetching functions for ACL/patterns *****/
6408
Olivier Houchardccaa7de2017-10-02 11:51:03 +02006409static int
6410smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private)
6411{
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006412 SSL *ssl;
Olivier Houchardccaa7de2017-10-02 11:51:03 +02006413 struct connection *conn;
6414
6415 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006416 ssl = ssl_sock_get_ssl_object(conn);
6417 if (!ssl)
Olivier Houchardccaa7de2017-10-02 11:51:03 +02006418 return 0;
6419
6420 smp->flags = 0;
6421 smp->data.type = SMP_T_BOOL;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02006422#ifdef OPENSSL_IS_BORINGSSL
6423 {
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006424 smp->data.u.sint = (SSL_in_early_data(ssl) &&
6425 SSL_early_data_accepted(ssl));
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02006426 }
6427#else
Olivier Houchard25ae45a2017-11-29 19:51:19 +01006428 smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) &&
Olivier Houchard220a26c2020-01-23 14:57:36 +01006429 (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS))) ? 1 : 0;
Emmanuel Hocdetc9858012019-08-07 14:44:49 +02006430#endif
Olivier Houchardccaa7de2017-10-02 11:51:03 +02006431 return 1;
6432}
6433
Emeric Brune64aef12012-09-21 13:15:06 +02006434/* boolean, returns true if client cert was present */
6435static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006436smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brune64aef12012-09-21 13:15:06 +02006437{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006438 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006439 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006440
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006441 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006442 if (!conn || conn->xprt != &ssl_sock)
Emeric Brune64aef12012-09-21 13:15:06 +02006443 return 0;
6444
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006445 ctx = conn->xprt_ctx;
6446
Willy Tarreau911db9b2020-01-23 16:27:54 +01006447 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brune64aef12012-09-21 13:15:06 +02006448 smp->flags |= SMP_F_MAY_CHANGE;
6449 return 0;
6450 }
6451
6452 smp->flags = 0;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006453 smp->data.type = SMP_T_BOOL;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01006454 smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0;
Emeric Brune64aef12012-09-21 13:15:06 +02006455
6456 return 1;
6457}
6458
Emeric Brun43e79582014-10-29 19:03:26 +01006459/* binary, returns a certificate in a binary chunk (der/raw).
6460 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6461 * should be use.
6462 */
6463static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006464smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun43e79582014-10-29 19:03:26 +01006465{
6466 int cert_peer = (kw[4] == 'c') ? 1 : 0;
6467 X509 *crt = NULL;
6468 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006469 struct buffer *smp_trash;
Emeric Brun43e79582014-10-29 19:03:26 +01006470 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006471 SSL *ssl;
Emeric Brun43e79582014-10-29 19:03:26 +01006472
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006473 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006474 ssl = ssl_sock_get_ssl_object(conn);
6475 if (!ssl)
Emeric Brun43e79582014-10-29 19:03:26 +01006476 return 0;
6477
Willy Tarreau911db9b2020-01-23 16:27:54 +01006478 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun43e79582014-10-29 19:03:26 +01006479 smp->flags |= SMP_F_MAY_CHANGE;
6480 return 0;
6481 }
6482
6483 if (cert_peer)
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006484 crt = SSL_get_peer_certificate(ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01006485 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006486 crt = SSL_get_certificate(ssl);
Emeric Brun43e79582014-10-29 19:03:26 +01006487
6488 if (!crt)
6489 goto out;
6490
6491 smp_trash = get_trash_chunk();
6492 if (ssl_sock_crt2der(crt, smp_trash) <= 0)
6493 goto out;
6494
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006495 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006496 smp->data.type = SMP_T_BIN;
Emeric Brun43e79582014-10-29 19:03:26 +01006497 ret = 1;
6498out:
6499 /* SSL_get_peer_certificate, it increase X509 * ref count */
6500 if (cert_peer && crt)
6501 X509_free(crt);
6502 return ret;
6503}
6504
Emeric Brunba841a12014-04-30 17:05:08 +02006505/* binary, returns serial of certificate in a binary chunk.
6506 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6507 * should be use.
6508 */
Willy Tarreau8d598402012-10-22 17:58:39 +02006509static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006510smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8d598402012-10-22 17:58:39 +02006511{
Emeric Brunba841a12014-04-30 17:05:08 +02006512 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Willy Tarreau8d598402012-10-22 17:58:39 +02006513 X509 *crt = NULL;
6514 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006515 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006516 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006517 SSL *ssl;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006518
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006519 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006520 ssl = ssl_sock_get_ssl_object(conn);
6521 if (!ssl)
Willy Tarreau8d598402012-10-22 17:58:39 +02006522 return 0;
6523
Willy Tarreau911db9b2020-01-23 16:27:54 +01006524 if (conn->flags & CO_FL_WAIT_XPRT) {
Willy Tarreau8d598402012-10-22 17:58:39 +02006525 smp->flags |= SMP_F_MAY_CHANGE;
6526 return 0;
6527 }
6528
Emeric Brunba841a12014-04-30 17:05:08 +02006529 if (cert_peer)
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006530 crt = SSL_get_peer_certificate(ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006531 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006532 crt = SSL_get_certificate(ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006533
Willy Tarreau8d598402012-10-22 17:58:39 +02006534 if (!crt)
6535 goto out;
6536
Willy Tarreau47ca5452012-12-23 20:22:19 +01006537 smp_trash = get_trash_chunk();
Willy Tarreau8d598402012-10-22 17:58:39 +02006538 if (ssl_sock_get_serial(crt, smp_trash) <= 0)
6539 goto out;
6540
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006541 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006542 smp->data.type = SMP_T_BIN;
Willy Tarreau8d598402012-10-22 17:58:39 +02006543 ret = 1;
6544out:
Emeric Brunba841a12014-04-30 17:05:08 +02006545 /* SSL_get_peer_certificate, it increase X509 * ref count */
6546 if (cert_peer && crt)
Willy Tarreau8d598402012-10-22 17:58:39 +02006547 X509_free(crt);
6548 return ret;
6549}
Emeric Brune64aef12012-09-21 13:15:06 +02006550
Emeric Brunba841a12014-04-30 17:05:08 +02006551/* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk.
6552 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6553 * should be use.
6554 */
James Votha051b4a2013-05-14 20:37:59 +02006555static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006556smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private)
James Votha051b4a2013-05-14 20:37:59 +02006557{
Emeric Brunba841a12014-04-30 17:05:08 +02006558 int cert_peer = (kw[4] == 'c') ? 1 : 0;
James Votha051b4a2013-05-14 20:37:59 +02006559 X509 *crt = NULL;
6560 const EVP_MD *digest;
6561 int ret = 0;
Willy Tarreau105599c2020-02-25 08:59:23 +01006562 unsigned int len = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006563 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006564 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006565 SSL *ssl;
James Votha051b4a2013-05-14 20:37:59 +02006566
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006567 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006568 ssl = ssl_sock_get_ssl_object(conn);
6569 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006570 return 0;
6571
Willy Tarreau911db9b2020-01-23 16:27:54 +01006572 if (conn->flags & CO_FL_WAIT_XPRT) {
James Votha051b4a2013-05-14 20:37:59 +02006573 smp->flags |= SMP_F_MAY_CHANGE;
6574 return 0;
6575 }
6576
Emeric Brunba841a12014-04-30 17:05:08 +02006577 if (cert_peer)
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006578 crt = SSL_get_peer_certificate(ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006579 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006580 crt = SSL_get_certificate(ssl);
James Votha051b4a2013-05-14 20:37:59 +02006581 if (!crt)
6582 goto out;
6583
6584 smp_trash = get_trash_chunk();
6585 digest = EVP_sha1();
Willy Tarreau105599c2020-02-25 08:59:23 +01006586 X509_digest(crt, digest, (unsigned char *) smp_trash->area, &len);
6587 smp_trash->data = len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006588 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006589 smp->data.type = SMP_T_BIN;
James Votha051b4a2013-05-14 20:37:59 +02006590 ret = 1;
6591out:
Emeric Brunba841a12014-04-30 17:05:08 +02006592 /* SSL_get_peer_certificate, it increase X509 * ref count */
6593 if (cert_peer && crt)
James Votha051b4a2013-05-14 20:37:59 +02006594 X509_free(crt);
6595 return ret;
6596}
6597
Emeric Brunba841a12014-04-30 17:05:08 +02006598/* string, returns certificate's notafter date in ASN1_UTCTIME format.
6599 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6600 * should be use.
6601 */
Emeric Brunce5ad802012-10-22 14:11:22 +02006602static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006603smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02006604{
Emeric Brunba841a12014-04-30 17:05:08 +02006605 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02006606 X509 *crt = NULL;
6607 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006608 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006609 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006610 SSL *ssl;
Emeric Brunce5ad802012-10-22 14:11:22 +02006611
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006612 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006613 ssl = ssl_sock_get_ssl_object(conn);
6614 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006615 return 0;
6616
Willy Tarreau911db9b2020-01-23 16:27:54 +01006617 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02006618 smp->flags |= SMP_F_MAY_CHANGE;
6619 return 0;
6620 }
6621
Emeric Brunba841a12014-04-30 17:05:08 +02006622 if (cert_peer)
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006623 crt = SSL_get_peer_certificate(ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006624 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006625 crt = SSL_get_certificate(ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02006626 if (!crt)
6627 goto out;
6628
Willy Tarreau47ca5452012-12-23 20:22:19 +01006629 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08006630 if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02006631 goto out;
6632
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006633 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006634 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02006635 ret = 1;
6636out:
Emeric Brunba841a12014-04-30 17:05:08 +02006637 /* SSL_get_peer_certificate, it increase X509 * ref count */
6638 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02006639 X509_free(crt);
6640 return ret;
6641}
6642
Emeric Brunba841a12014-04-30 17:05:08 +02006643/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer
6644 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6645 * should be use.
6646 */
Emeric Brun87855892012-10-17 17:39:35 +02006647static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006648smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02006649{
Emeric Brunba841a12014-04-30 17:05:08 +02006650 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02006651 X509 *crt = NULL;
6652 X509_NAME *name;
6653 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006654 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006655 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006656 SSL *ssl;
Emeric Brun87855892012-10-17 17:39:35 +02006657
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006658 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006659 ssl = ssl_sock_get_ssl_object(conn);
6660 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006661 return 0;
6662
Willy Tarreau911db9b2020-01-23 16:27:54 +01006663 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02006664 smp->flags |= SMP_F_MAY_CHANGE;
6665 return 0;
6666 }
6667
Emeric Brunba841a12014-04-30 17:05:08 +02006668 if (cert_peer)
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006669 crt = SSL_get_peer_certificate(ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006670 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006671 crt = SSL_get_certificate(ssl);
Emeric Brun87855892012-10-17 17:39:35 +02006672 if (!crt)
6673 goto out;
6674
6675 name = X509_get_issuer_name(crt);
6676 if (!name)
6677 goto out;
6678
Willy Tarreau47ca5452012-12-23 20:22:19 +01006679 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05006680 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02006681 int pos = 1;
6682
6683 if (args[1].type == ARGT_SINT)
6684 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02006685
6686 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
6687 goto out;
6688 }
Elliot Otchet71f82972020-01-15 08:12:14 -05006689 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
6690 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
6691 goto out;
6692 }
Emeric Brun87855892012-10-17 17:39:35 +02006693 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
6694 goto out;
6695
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006696 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006697 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02006698 ret = 1;
6699out:
Emeric Brunba841a12014-04-30 17:05:08 +02006700 /* SSL_get_peer_certificate, it increase X509 * ref count */
6701 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02006702 X509_free(crt);
6703 return ret;
6704}
6705
Emeric Brunba841a12014-04-30 17:05:08 +02006706/* string, returns notbefore date in ASN1_UTCTIME format.
6707 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6708 * should be use.
6709 */
Emeric Brunce5ad802012-10-22 14:11:22 +02006710static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006711smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunce5ad802012-10-22 14:11:22 +02006712{
Emeric Brunba841a12014-04-30 17:05:08 +02006713 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brunce5ad802012-10-22 14:11:22 +02006714 X509 *crt = NULL;
6715 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006716 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006717 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006718 SSL *ssl;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006719
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006720 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006721 ssl = ssl_sock_get_ssl_object(conn);
6722 if (!ssl)
Emeric Brunce5ad802012-10-22 14:11:22 +02006723 return 0;
6724
Willy Tarreau911db9b2020-01-23 16:27:54 +01006725 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunce5ad802012-10-22 14:11:22 +02006726 smp->flags |= SMP_F_MAY_CHANGE;
6727 return 0;
6728 }
6729
Emeric Brunba841a12014-04-30 17:05:08 +02006730 if (cert_peer)
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006731 crt = SSL_get_peer_certificate(ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006732 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006733 crt = SSL_get_certificate(ssl);
Emeric Brunce5ad802012-10-22 14:11:22 +02006734 if (!crt)
6735 goto out;
6736
Willy Tarreau47ca5452012-12-23 20:22:19 +01006737 smp_trash = get_trash_chunk();
Rosen Penev68185952018-12-14 08:47:02 -08006738 if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0)
Emeric Brunce5ad802012-10-22 14:11:22 +02006739 goto out;
6740
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006741 smp->data.u.str = *smp_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006742 smp->data.type = SMP_T_STR;
Emeric Brunce5ad802012-10-22 14:11:22 +02006743 ret = 1;
6744out:
Emeric Brunba841a12014-04-30 17:05:08 +02006745 /* SSL_get_peer_certificate, it increase X509 * ref count */
6746 if (cert_peer && crt)
Emeric Brunce5ad802012-10-22 14:11:22 +02006747 X509_free(crt);
6748 return ret;
6749}
6750
Emeric Brunba841a12014-04-30 17:05:08 +02006751/* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject
6752 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6753 * should be use.
6754 */
Emeric Brun87855892012-10-17 17:39:35 +02006755static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006756smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun87855892012-10-17 17:39:35 +02006757{
Emeric Brunba841a12014-04-30 17:05:08 +02006758 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun87855892012-10-17 17:39:35 +02006759 X509 *crt = NULL;
6760 X509_NAME *name;
6761 int ret = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +02006762 struct buffer *smp_trash;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006763 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006764 SSL *ssl;
Emeric Brun87855892012-10-17 17:39:35 +02006765
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006766 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006767 ssl = ssl_sock_get_ssl_object(conn);
6768 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006769 return 0;
6770
Willy Tarreau911db9b2020-01-23 16:27:54 +01006771 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun87855892012-10-17 17:39:35 +02006772 smp->flags |= SMP_F_MAY_CHANGE;
6773 return 0;
6774 }
6775
Emeric Brunba841a12014-04-30 17:05:08 +02006776 if (cert_peer)
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006777 crt = SSL_get_peer_certificate(ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006778 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006779 crt = SSL_get_certificate(ssl);
Emeric Brun87855892012-10-17 17:39:35 +02006780 if (!crt)
6781 goto out;
6782
6783 name = X509_get_subject_name(crt);
6784 if (!name)
6785 goto out;
6786
Willy Tarreau47ca5452012-12-23 20:22:19 +01006787 smp_trash = get_trash_chunk();
Elliot Otchet71f82972020-01-15 08:12:14 -05006788 if (args && args[0].type == ARGT_STR && args[0].data.str.data > 0) {
Emeric Brun87855892012-10-17 17:39:35 +02006789 int pos = 1;
6790
6791 if (args[1].type == ARGT_SINT)
6792 pos = args[1].data.sint;
Emeric Brun87855892012-10-17 17:39:35 +02006793
6794 if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0)
6795 goto out;
6796 }
Elliot Otchet71f82972020-01-15 08:12:14 -05006797 else if (args && args[2].type == ARGT_STR && args[2].data.str.data > 0) {
6798 if (ssl_sock_get_dn_formatted(name, &args[2].data.str, smp_trash) <= 0)
6799 goto out;
6800 }
Emeric Brun87855892012-10-17 17:39:35 +02006801 else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0)
6802 goto out;
6803
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006804 smp->data.type = SMP_T_STR;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006805 smp->data.u.str = *smp_trash;
Emeric Brun87855892012-10-17 17:39:35 +02006806 ret = 1;
6807out:
Emeric Brunba841a12014-04-30 17:05:08 +02006808 /* SSL_get_peer_certificate, it increase X509 * ref count */
6809 if (cert_peer && crt)
Emeric Brun87855892012-10-17 17:39:35 +02006810 X509_free(crt);
6811 return ret;
6812}
Emeric Brun9143d372012-12-20 15:44:16 +01006813
6814/* integer, returns true if current session use a client certificate */
6815static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006816smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun9143d372012-12-20 15:44:16 +01006817{
6818 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006819 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006820 SSL *ssl;
Emeric Brun9143d372012-12-20 15:44:16 +01006821
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006822 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006823 ssl = ssl_sock_get_ssl_object(conn);
6824 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006825 return 0;
6826
Willy Tarreau911db9b2020-01-23 16:27:54 +01006827 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun9143d372012-12-20 15:44:16 +01006828 smp->flags |= SMP_F_MAY_CHANGE;
6829 return 0;
6830 }
6831
6832 /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006833 crt = SSL_get_peer_certificate(ssl);
Emeric Brun9143d372012-12-20 15:44:16 +01006834 if (crt) {
6835 X509_free(crt);
6836 }
6837
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006838 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006839 smp->data.u.sint = (crt != NULL);
Emeric Brun9143d372012-12-20 15:44:16 +01006840 return 1;
6841}
6842
Emeric Brunba841a12014-04-30 17:05:08 +02006843/* integer, returns the certificate version
6844 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6845 * should be use.
6846 */
Emeric Bruna7359fd2012-10-17 15:03:11 +02006847static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006848smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Bruna7359fd2012-10-17 15:03:11 +02006849{
Emeric Brunba841a12014-04-30 17:05:08 +02006850 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Bruna7359fd2012-10-17 15:03:11 +02006851 X509 *crt;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006852 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006853 SSL *ssl;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006854
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006855 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006856 ssl = ssl_sock_get_ssl_object(conn);
6857 if (!ssl)
Emeric Bruna7359fd2012-10-17 15:03:11 +02006858 return 0;
6859
Willy Tarreau911db9b2020-01-23 16:27:54 +01006860 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Bruna7359fd2012-10-17 15:03:11 +02006861 smp->flags |= SMP_F_MAY_CHANGE;
6862 return 0;
6863 }
6864
Emeric Brunba841a12014-04-30 17:05:08 +02006865 if (cert_peer)
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006866 crt = SSL_get_peer_certificate(ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006867 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006868 crt = SSL_get_certificate(ssl);
Emeric Bruna7359fd2012-10-17 15:03:11 +02006869 if (!crt)
6870 return 0;
6871
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02006872 smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt));
Emeric Brunba841a12014-04-30 17:05:08 +02006873 /* SSL_get_peer_certificate increase X509 * ref count */
6874 if (cert_peer)
6875 X509_free(crt);
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006876 smp->data.type = SMP_T_SINT;
Emeric Bruna7359fd2012-10-17 15:03:11 +02006877
6878 return 1;
6879}
6880
Emeric Brunba841a12014-04-30 17:05:08 +02006881/* string, returns the certificate's signature algorithm.
6882 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6883 * should be use.
6884 */
Emeric Brun7f56e742012-10-19 18:15:40 +02006885static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006886smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun7f56e742012-10-19 18:15:40 +02006887{
Emeric Brunba841a12014-04-30 17:05:08 +02006888 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun7f56e742012-10-19 18:15:40 +02006889 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006890 __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm;
Emeric Brun7f56e742012-10-19 18:15:40 +02006891 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006892 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006893 SSL *ssl;
Emeric Brun7f56e742012-10-19 18:15:40 +02006894
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006895 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006896 ssl = ssl_sock_get_ssl_object(conn);
6897 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006898 return 0;
6899
Willy Tarreau911db9b2020-01-23 16:27:54 +01006900 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun7f56e742012-10-19 18:15:40 +02006901 smp->flags |= SMP_F_MAY_CHANGE;
6902 return 0;
6903 }
6904
Emeric Brunba841a12014-04-30 17:05:08 +02006905 if (cert_peer)
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006906 crt = SSL_get_peer_certificate(ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006907 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006908 crt = SSL_get_certificate(ssl);
Emeric Brun7f56e742012-10-19 18:15:40 +02006909 if (!crt)
6910 return 0;
6911
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006912 X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt));
6913 nid = OBJ_obj2nid(algorithm);
Emeric Brun7f56e742012-10-19 18:15:40 +02006914
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006915 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
6916 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02006917 /* SSL_get_peer_certificate increase X509 * ref count */
6918 if (cert_peer)
6919 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02006920 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02006921 }
Emeric Brun7f56e742012-10-19 18:15:40 +02006922
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006923 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01006924 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006925 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02006926 /* SSL_get_peer_certificate increase X509 * ref count */
6927 if (cert_peer)
6928 X509_free(crt);
Emeric Brun7f56e742012-10-19 18:15:40 +02006929
6930 return 1;
6931}
6932
Emeric Brunba841a12014-04-30 17:05:08 +02006933/* string, returns the certificate's key algorithm.
6934 * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate
6935 * should be use.
6936 */
Emeric Brun521a0112012-10-22 12:22:55 +02006937static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006938smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun521a0112012-10-22 12:22:55 +02006939{
Emeric Brunba841a12014-04-30 17:05:08 +02006940 int cert_peer = (kw[4] == 'c') ? 1 : 0;
Emeric Brun521a0112012-10-22 12:22:55 +02006941 X509 *crt;
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006942 ASN1_OBJECT *algorithm;
Emeric Brun521a0112012-10-22 12:22:55 +02006943 int nid;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006944 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006945 SSL *ssl;
Emeric Brun521a0112012-10-22 12:22:55 +02006946
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006947 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006948 ssl = ssl_sock_get_ssl_object(conn);
6949 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006950 return 0;
6951
Willy Tarreau911db9b2020-01-23 16:27:54 +01006952 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brun521a0112012-10-22 12:22:55 +02006953 smp->flags |= SMP_F_MAY_CHANGE;
6954 return 0;
6955 }
6956
Emeric Brunba841a12014-04-30 17:05:08 +02006957 if (cert_peer)
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006958 crt = SSL_get_peer_certificate(ssl);
Emeric Brunba841a12014-04-30 17:05:08 +02006959 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02006960 crt = SSL_get_certificate(ssl);
Emeric Brun521a0112012-10-22 12:22:55 +02006961 if (!crt)
6962 return 0;
6963
Dirkjan Bussink1866d6d2016-08-29 13:26:37 +02006964 X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt));
6965 nid = OBJ_obj2nid(algorithm);
Emeric Brun521a0112012-10-22 12:22:55 +02006966
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006967 smp->data.u.str.area = (char *)OBJ_nid2sn(nid);
6968 if (!smp->data.u.str.area) {
Emeric Brunba841a12014-04-30 17:05:08 +02006969 /* SSL_get_peer_certificate increase X509 * ref count */
6970 if (cert_peer)
6971 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02006972 return 0;
Emeric Brun9bf3ba22013-10-07 14:31:44 +02006973 }
Emeric Brun521a0112012-10-22 12:22:55 +02006974
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006975 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01006976 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02006977 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brunba841a12014-04-30 17:05:08 +02006978 if (cert_peer)
6979 X509_free(crt);
Emeric Brun521a0112012-10-22 12:22:55 +02006980
6981 return 1;
6982}
6983
Emeric Brun645ae792014-04-30 14:21:06 +02006984/* boolean, returns true if front conn. transport layer is SSL.
6985 * This function is also usable on backend conn if the fetch keyword 5th
6986 * char is 'b'.
6987 */
Willy Tarreau7875d092012-09-10 08:20:03 +02006988static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006989smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02006990{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02006991 struct connection *conn;
6992
Christopher Fauletf98e6262020-05-06 09:42:04 +02006993 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02006994 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
6995 else
6996 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
6997 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02006998
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02006999 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007000 smp->data.u.sint = (conn && conn->xprt == &ssl_sock);
Willy Tarreau7875d092012-09-10 08:20:03 +02007001 return 1;
7002}
7003
Emeric Brun2525b6b2012-10-18 15:59:43 +02007004/* boolean, returns true if client present a SNI */
Willy Tarreau7875d092012-09-10 08:20:03 +02007005static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007006smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007007{
7008#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007009 struct connection *conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007010 SSL *ssl = ssl_sock_get_ssl_object(conn);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007011
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007012 smp->data.type = SMP_T_BOOL;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007013 smp->data.u.sint = ssl && SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) != NULL;
Willy Tarreau7875d092012-09-10 08:20:03 +02007014 return 1;
7015#else
7016 return 0;
7017#endif
7018}
7019
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007020/* boolean, returns true if client session has been resumed.
7021 * This function is also usable on backend conn if the fetch keyword 5th
7022 * char is 'b'.
7023 */
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007024static int
7025smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private)
7026{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007027 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007028 SSL *ssl;
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007029
Christopher Fauletf98e6262020-05-06 09:42:04 +02007030 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007031 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
7032 else
7033 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7034 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Emeric Brun74f7ffa2018-02-19 16:14:12 +01007035
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007036 ssl = ssl_sock_get_ssl_object(conn);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007037
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007038 smp->data.type = SMP_T_BOOL;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007039 smp->data.u.sint = ssl && SSL_session_reused(ssl);
Nenad Merdanovic26ea8222015-05-18 02:28:57 +02007040 return 1;
7041}
7042
Emeric Brun645ae792014-04-30 14:21:06 +02007043/* string, returns the used cipher if front conn. transport layer is SSL.
7044 * This function is also usable on backend conn if the fetch keyword 5th
7045 * char is 'b'.
7046 */
Emeric Brun589fcad2012-10-16 14:13:26 +02007047static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007048smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007049{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007050 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007051 SSL *ssl;
Emeric Brun589fcad2012-10-16 14:13:26 +02007052
Christopher Fauletf98e6262020-05-06 09:42:04 +02007053 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007054 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
7055 else
7056 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7057 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7058
Willy Tarreaube508f12016-03-10 11:47:01 +01007059 smp->flags = 0;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007060 ssl = ssl_sock_get_ssl_object(conn);
7061 if (!ssl)
Emeric Brun589fcad2012-10-16 14:13:26 +02007062 return 0;
7063
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007064 smp->data.u.str.area = (char *)SSL_get_cipher_name(ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007065 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02007066 return 0;
7067
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007068 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007069 smp->flags |= SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007070 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02007071
7072 return 1;
7073}
7074
Emeric Brun645ae792014-04-30 14:21:06 +02007075/* integer, returns the algoritm's keysize if front conn. transport layer
7076 * is SSL.
7077 * This function is also usable on backend conn if the fetch keyword 5th
7078 * char is 'b'.
7079 */
Emeric Brun589fcad2012-10-16 14:13:26 +02007080static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007081smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007082{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007083 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007084 SSL *ssl;
Willy Tarreaue237fe12016-03-10 17:05:28 +01007085 int sint;
Willy Tarreaube508f12016-03-10 11:47:01 +01007086
Christopher Fauletf98e6262020-05-06 09:42:04 +02007087 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007088 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
7089 else
7090 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7091 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7092
Emeric Brun589fcad2012-10-16 14:13:26 +02007093 smp->flags = 0;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007094 ssl = ssl_sock_get_ssl_object(conn);
7095 if (!ssl)
Emeric Brun589fcad2012-10-16 14:13:26 +02007096 return 0;
7097
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007098 if (!SSL_get_cipher_bits(ssl, &sint))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007099 return 0;
7100
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007101 smp->data.u.sint = sint;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007102 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02007103
7104 return 1;
7105}
7106
Emeric Brun645ae792014-04-30 14:21:06 +02007107/* integer, returns the used keysize if front conn. transport layer is SSL.
7108 * This function is also usable on backend conn if the fetch keyword 5th
7109 * char is 'b'.
7110 */
Emeric Brun589fcad2012-10-16 14:13:26 +02007111static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007112smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007113{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007114 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007115 SSL *ssl;
Willy Tarreaube508f12016-03-10 11:47:01 +01007116
Christopher Fauletf98e6262020-05-06 09:42:04 +02007117 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007118 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
7119 else
7120 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7121 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7122
Emeric Brun589fcad2012-10-16 14:13:26 +02007123 smp->flags = 0;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007124 ssl = ssl_sock_get_ssl_object(conn);
7125 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007126 return 0;
7127
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007128 smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ssl, NULL);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007129 if (!smp->data.u.sint)
Emeric Brun589fcad2012-10-16 14:13:26 +02007130 return 0;
7131
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007132 smp->data.type = SMP_T_SINT;
Emeric Brun589fcad2012-10-16 14:13:26 +02007133
7134 return 1;
7135}
7136
Bernard Spil13c53f82018-02-15 13:34:58 +01007137#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau7875d092012-09-10 08:20:03 +02007138static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007139smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreaua33c6542012-10-15 13:19:06 +02007140{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007141 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007142 SSL *ssl;
Willy Tarreau105599c2020-02-25 08:59:23 +01007143 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007144
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007145 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007146 smp->data.type = SMP_T_STR;
Willy Tarreaua33c6542012-10-15 13:19:06 +02007147
Christopher Fauletf98e6262020-05-06 09:42:04 +02007148 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007149 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
7150 else
7151 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7152 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7153
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007154 ssl = ssl_sock_get_ssl_object(conn);
7155 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007156 return 0;
7157
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007158 smp->data.u.str.area = NULL;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007159 SSL_get0_next_proto_negotiated(ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01007160 (const unsigned char **)&smp->data.u.str.area,
7161 &len);
Willy Tarreaua33c6542012-10-15 13:19:06 +02007162
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007163 if (!smp->data.u.str.area)
Willy Tarreaua33c6542012-10-15 13:19:06 +02007164 return 0;
7165
Willy Tarreau105599c2020-02-25 08:59:23 +01007166 smp->data.u.str.data = len;
Willy Tarreaua33c6542012-10-15 13:19:06 +02007167 return 1;
Willy Tarreaua33c6542012-10-15 13:19:06 +02007168}
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02007169#endif
Willy Tarreaua33c6542012-10-15 13:19:06 +02007170
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +01007171#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Willy Tarreauab861d32013-04-02 02:30:41 +02007172static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007173smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauab861d32013-04-02 02:30:41 +02007174{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007175 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007176 SSL *ssl;
Willy Tarreau105599c2020-02-25 08:59:23 +01007177 unsigned int len = 0;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007178
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007179 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007180 smp->data.type = SMP_T_STR;
Willy Tarreauab861d32013-04-02 02:30:41 +02007181
Christopher Fauletf98e6262020-05-06 09:42:04 +02007182 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007183 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
7184 else
7185 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7186 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
Olivier Houchard6b77f492018-11-22 18:18:29 +01007187
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007188 ssl = ssl_sock_get_ssl_object(conn);
7189 if (!ssl)
Willy Tarreauab861d32013-04-02 02:30:41 +02007190 return 0;
7191
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007192 smp->data.u.str.area = NULL;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007193 SSL_get0_alpn_selected(ssl,
Willy Tarreau105599c2020-02-25 08:59:23 +01007194 (const unsigned char **)&smp->data.u.str.area,
7195 &len);
Willy Tarreauab861d32013-04-02 02:30:41 +02007196
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007197 if (!smp->data.u.str.area)
Willy Tarreauab861d32013-04-02 02:30:41 +02007198 return 0;
7199
Willy Tarreau105599c2020-02-25 08:59:23 +01007200 smp->data.u.str.data = len;
Willy Tarreauab861d32013-04-02 02:30:41 +02007201 return 1;
7202}
7203#endif
7204
Emeric Brun645ae792014-04-30 14:21:06 +02007205/* string, returns the used protocol if front conn. transport layer is SSL.
7206 * This function is also usable on backend conn if the fetch keyword 5th
7207 * char is 'b'.
7208 */
Willy Tarreaua33c6542012-10-15 13:19:06 +02007209static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007210smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brun589fcad2012-10-16 14:13:26 +02007211{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007212 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007213 SSL *ssl;
Willy Tarreaube508f12016-03-10 11:47:01 +01007214
Christopher Fauletf98e6262020-05-06 09:42:04 +02007215 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007216 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
7217 else
7218 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7219 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7220
Emeric Brun589fcad2012-10-16 14:13:26 +02007221 smp->flags = 0;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007222 ssl = ssl_sock_get_ssl_object(conn);
7223 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007224 return 0;
7225
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007226 smp->data.u.str.area = (char *)SSL_get_version(ssl);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007227 if (!smp->data.u.str.area)
Emeric Brun589fcad2012-10-16 14:13:26 +02007228 return 0;
7229
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007230 smp->data.type = SMP_T_STR;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007231 smp->flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007232 smp->data.u.str.data = strlen(smp->data.u.str.area);
Emeric Brun589fcad2012-10-16 14:13:26 +02007233
7234 return 1;
7235}
7236
Willy Tarreau87b09662015-04-03 00:22:06 +02007237/* binary, returns the SSL stream id if front conn. transport layer is SSL.
Emeric Brun645ae792014-04-30 14:21:06 +02007238 * This function is also usable on backend conn if the fetch keyword 5th
7239 * char is 'b'.
7240 */
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007241#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun589fcad2012-10-16 14:13:26 +02007242static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007243smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunfe68f682012-10-16 14:59:28 +02007244{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007245 struct connection *conn;
Willy Tarreaue237fe12016-03-10 17:05:28 +01007246 SSL_SESSION *ssl_sess;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007247 SSL *ssl;
Willy Tarreau105599c2020-02-25 08:59:23 +01007248 unsigned int len = 0;
Willy Tarreaube508f12016-03-10 11:47:01 +01007249
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007250 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007251 smp->data.type = SMP_T_BIN;
Emeric Brunfe68f682012-10-16 14:59:28 +02007252
Christopher Fauletf98e6262020-05-06 09:42:04 +02007253 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007254 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
7255 else
7256 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7257 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7258
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007259 ssl = ssl_sock_get_ssl_object(conn);
7260 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007261 return 0;
7262
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007263 ssl_sess = SSL_get_session(ssl);
Willy Tarreau192252e2015-04-04 01:47:55 +02007264 if (!ssl_sess)
Emeric Brunfe68f682012-10-16 14:59:28 +02007265 return 0;
7266
Willy Tarreau105599c2020-02-25 08:59:23 +01007267 smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, &len);
Dragan Dosenf35d69e2020-05-04 09:07:28 +02007268 if (!smp->data.u.str.area || !len)
Emeric Brunfe68f682012-10-16 14:59:28 +02007269 return 0;
7270
Willy Tarreau105599c2020-02-25 08:59:23 +01007271 smp->data.u.str.data = len;
Emeric Brunfe68f682012-10-16 14:59:28 +02007272 return 1;
Emeric Brunfe68f682012-10-16 14:59:28 +02007273}
Patrick Hemmer41966772018-04-28 19:15:48 -04007274#endif
7275
Emeric Brunfe68f682012-10-16 14:59:28 +02007276
Emmanuel Hocdet839af572019-05-14 16:27:35 +02007277#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmere0275472018-04-28 19:15:51 -04007278static int
Patrick Hemmer65674662019-06-04 08:13:03 -04007279smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private)
7280{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007281 struct connection *conn;
Patrick Hemmer65674662019-06-04 08:13:03 -04007282 struct buffer *data;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007283 SSL *ssl;
Patrick Hemmer65674662019-06-04 08:13:03 -04007284
Christopher Fauletf98e6262020-05-06 09:42:04 +02007285 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007286 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
7287 else
7288 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7289 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7290
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007291 ssl = ssl_sock_get_ssl_object(conn);
7292 if (!ssl)
Patrick Hemmer65674662019-06-04 08:13:03 -04007293 return 0;
Patrick Hemmer65674662019-06-04 08:13:03 -04007294
7295 data = get_trash_chunk();
7296 if (kw[7] == 'c')
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007297 data->data = SSL_get_client_random(ssl,
Patrick Hemmer65674662019-06-04 08:13:03 -04007298 (unsigned char *) data->area,
7299 data->size);
7300 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007301 data->data = SSL_get_server_random(ssl,
Patrick Hemmer65674662019-06-04 08:13:03 -04007302 (unsigned char *) data->area,
7303 data->size);
7304 if (!data->data)
7305 return 0;
7306
7307 smp->flags = 0;
7308 smp->data.type = SMP_T_BIN;
7309 smp->data.u.str = *data;
7310
7311 return 1;
7312}
7313
7314static int
Patrick Hemmere0275472018-04-28 19:15:51 -04007315smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private)
7316{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007317 struct connection *conn;
Patrick Hemmere0275472018-04-28 19:15:51 -04007318 SSL_SESSION *ssl_sess;
Willy Tarreau83061a82018-07-13 11:56:34 +02007319 struct buffer *data;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007320 SSL *ssl;
Patrick Hemmere0275472018-04-28 19:15:51 -04007321
Christopher Fauletf98e6262020-05-06 09:42:04 +02007322 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007323 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
7324 else
7325 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7326 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7327
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007328 ssl = ssl_sock_get_ssl_object(conn);
7329 if (!ssl)
Patrick Hemmere0275472018-04-28 19:15:51 -04007330 return 0;
7331
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007332 ssl_sess = SSL_get_session(ssl);
Patrick Hemmere0275472018-04-28 19:15:51 -04007333 if (!ssl_sess)
7334 return 0;
7335
7336 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007337 data->data = SSL_SESSION_get_master_key(ssl_sess,
7338 (unsigned char *) data->area,
7339 data->size);
7340 if (!data->data)
Patrick Hemmere0275472018-04-28 19:15:51 -04007341 return 0;
7342
7343 smp->flags = 0;
7344 smp->data.type = SMP_T_BIN;
7345 smp->data.u.str = *data;
7346
7347 return 1;
7348}
7349#endif
7350
Patrick Hemmer41966772018-04-28 19:15:48 -04007351#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Emeric Brunfe68f682012-10-16 14:59:28 +02007352static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007353smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau7875d092012-09-10 08:20:03 +02007354{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007355 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007356 SSL *ssl;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007357
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01007358 smp->flags = SMP_F_CONST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007359 smp->data.type = SMP_T_STR;
Willy Tarreau7875d092012-09-10 08:20:03 +02007360
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007361 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007362 ssl = ssl_sock_get_ssl_object(conn);
7363 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007364 return 0;
7365
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007366 smp->data.u.str.area = (char *)SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007367 if (!smp->data.u.str.area)
Willy Tarreau3e394c92012-09-14 23:56:58 +02007368 return 0;
7369
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007370 smp->data.u.str.data = strlen(smp->data.u.str.area);
Willy Tarreau7875d092012-09-10 08:20:03 +02007371 return 1;
Willy Tarreau7875d092012-09-10 08:20:03 +02007372}
Patrick Hemmer41966772018-04-28 19:15:48 -04007373#endif
Willy Tarreau7875d092012-09-10 08:20:03 +02007374
David Sc1ad52e2014-04-08 18:48:47 -04007375static int
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007376smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private)
7377{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007378 struct connection *conn;
7379 struct ssl_capture *capture;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007380 SSL *ssl;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007381
7382 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007383 ssl = ssl_sock_get_ssl_object(conn);
7384 if (!ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007385 return 0;
7386
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007387 capture = SSL_get_ex_data(ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007388 if (!capture)
7389 return 0;
7390
7391 smp->flags = SMP_F_CONST;
7392 smp->data.type = SMP_T_BIN;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007393 smp->data.u.str.area = capture->ciphersuite;
7394 smp->data.u.str.data = capture->ciphersuite_len;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007395 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007396}
7397
7398static int
7399smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private)
7400{
Willy Tarreau83061a82018-07-13 11:56:34 +02007401 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007402
7403 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
7404 return 0;
7405
7406 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007407 dump_binary(data, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007408 smp->data.type = SMP_T_BIN;
7409 smp->data.u.str = *data;
7410 return 1;
7411}
7412
7413static int
7414smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private)
7415{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007416 struct connection *conn;
7417 struct ssl_capture *capture;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007418 SSL *ssl;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007419
7420 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007421 ssl = ssl_sock_get_ssl_object(conn);
7422 if (!ssl)
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007423 return 0;
7424
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007425 capture = SSL_get_ex_data(ssl, ssl_capture_ptr_index);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007426 if (!capture)
7427 return 0;
7428
7429 smp->data.type = SMP_T_SINT;
7430 smp->data.u.sint = capture->xxh64;
7431 return 1;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007432}
7433
7434static int
7435smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private)
7436{
Willy Tarreau5db847a2019-05-09 14:13:35 +02007437#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL)
Willy Tarreau83061a82018-07-13 11:56:34 +02007438 struct buffer *data;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007439 int i;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007440
7441 if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private))
7442 return 0;
7443
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007444 data = get_trash_chunk();
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007445 for (i = 0; i + 1 < smp->data.u.str.data; i += 2) {
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02007446 const char *str;
7447 const SSL_CIPHER *cipher;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007448 const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i;
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02007449 uint16_t id = (bin[0] << 8) | bin[1];
7450#if defined(OPENSSL_IS_BORINGSSL)
7451 cipher = SSL_get_cipher_by_value(id);
7452#else
Willy Tarreaub7290772018-10-15 11:01:59 +02007453 struct connection *conn = __objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007454 SSL *ssl = ssl_sock_get_ssl_object(conn);
7455 cipher = SSL_CIPHER_find(ssl, bin);
Emmanuel Hocdetddcde192017-09-01 17:32:08 +02007456#endif
7457 str = SSL_CIPHER_get_name(cipher);
7458 if (!str || strcmp(str, "(NONE)") == 0)
7459 chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id);
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007460 else
7461 chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str);
7462 }
7463 smp->data.type = SMP_T_STR;
7464 smp->data.u.str = *data;
7465 return 1;
7466#else
7467 return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private);
7468#endif
7469}
7470
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007471#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01007472static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007473smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private)
David Sc1ad52e2014-04-08 18:48:47 -04007474{
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007475 struct connection *conn;
David Sc1ad52e2014-04-08 18:48:47 -04007476 int finished_len;
Willy Tarreau83061a82018-07-13 11:56:34 +02007477 struct buffer *finished_trash;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007478 SSL *ssl;
David Sc1ad52e2014-04-08 18:48:47 -04007479
Christopher Fauletf98e6262020-05-06 09:42:04 +02007480 if (obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
Christopher Fauletd92ea7f2020-04-30 10:03:55 +02007481 conn = (kw[4] != 'b') ? cs_conn(__objt_check(smp->sess->origin)->cs) : NULL;
7482 else
7483 conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) :
7484 smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL;
7485
David Sc1ad52e2014-04-08 18:48:47 -04007486 smp->flags = 0;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007487 ssl = ssl_sock_get_ssl_object(conn);
7488 if (!ssl)
David Sc1ad52e2014-04-08 18:48:47 -04007489 return 0;
7490
Willy Tarreau911db9b2020-01-23 16:27:54 +01007491 if (conn->flags & CO_FL_WAIT_XPRT) {
David Sc1ad52e2014-04-08 18:48:47 -04007492 smp->flags |= SMP_F_MAY_CHANGE;
7493 return 0;
7494 }
7495
7496 finished_trash = get_trash_chunk();
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007497 if (!SSL_session_reused(ssl))
7498 finished_len = SSL_get_peer_finished(ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007499 finished_trash->area,
7500 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04007501 else
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007502 finished_len = SSL_get_finished(ssl,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007503 finished_trash->area,
7504 finished_trash->size);
David Sc1ad52e2014-04-08 18:48:47 -04007505
7506 if (!finished_len)
7507 return 0;
7508
Willy Tarreau843b7cb2018-07-13 10:54:26 +02007509 finished_trash->data = finished_len;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02007510 smp->data.u.str = *finished_trash;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007511 smp->data.type = SMP_T_BIN;
David Sc1ad52e2014-04-08 18:48:47 -04007512
7513 return 1;
David Sc1ad52e2014-04-08 18:48:47 -04007514}
Patrick Hemmer41966772018-04-28 19:15:48 -04007515#endif
David Sc1ad52e2014-04-08 18:48:47 -04007516
Emeric Brun2525b6b2012-10-18 15:59:43 +02007517/* integer, returns the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02007518static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007519smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02007520{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007521 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007522 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007523
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007524 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007525 if (!conn || conn->xprt != &ssl_sock)
7526 return 0;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007527 ctx = conn->xprt_ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007528
Willy Tarreau911db9b2020-01-23 16:27:54 +01007529 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02007530 smp->flags = SMP_F_MAY_CHANGE;
7531 return 0;
7532 }
7533
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007534 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007535 smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02007536 smp->flags = 0;
7537
7538 return 1;
7539}
7540
Emeric Brun2525b6b2012-10-18 15:59:43 +02007541/* integer, returns the depth of the first verify error in CA chain of client certificate chain. */
Emeric Brunf282a812012-09-21 15:27:54 +02007542static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007543smp_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 +02007544{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007545 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007546 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007547
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007548 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007549 if (!conn || conn->xprt != &ssl_sock)
Emeric Brunf282a812012-09-21 15:27:54 +02007550 return 0;
7551
Willy Tarreau911db9b2020-01-23 16:27:54 +01007552 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02007553 smp->flags = SMP_F_MAY_CHANGE;
7554 return 0;
7555 }
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007556 ctx = conn->xprt_ctx;
Emeric Brunf282a812012-09-21 15:27:54 +02007557
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007558 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007559 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02007560 smp->flags = 0;
7561
7562 return 1;
7563}
7564
Emeric Brun2525b6b2012-10-18 15:59:43 +02007565/* integer, returns the first verify error on client certificate */
Emeric Brunf282a812012-09-21 15:27:54 +02007566static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007567smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunf282a812012-09-21 15:27:54 +02007568{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007569 struct connection *conn;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007570 struct ssl_sock_ctx *ctx;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007571
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007572 conn = objt_conn(smp->sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007573 if (!conn || conn->xprt != &ssl_sock)
7574 return 0;
7575
Willy Tarreau911db9b2020-01-23 16:27:54 +01007576 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunf282a812012-09-21 15:27:54 +02007577 smp->flags = SMP_F_MAY_CHANGE;
7578 return 0;
7579 }
7580
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007581 ctx = conn->xprt_ctx;
7582
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007583 smp->data.type = SMP_T_SINT;
Olivier Houchard7b5fd1e2019-02-28 18:10:45 +01007584 smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st);
Emeric Brunf282a812012-09-21 15:27:54 +02007585 smp->flags = 0;
7586
7587 return 1;
7588}
7589
Emeric Brun2525b6b2012-10-18 15:59:43 +02007590/* integer, returns the verify result on client cert */
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007591static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02007592smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private)
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007593{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007594 struct connection *conn;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007595 SSL *ssl;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007596
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02007597 conn = objt_conn(smp->sess->origin);
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007598 ssl = ssl_sock_get_ssl_object(conn);
7599 if (!ssl)
Willy Tarreaub363a1f2013-10-01 10:45:07 +02007600 return 0;
7601
Willy Tarreau911db9b2020-01-23 16:27:54 +01007602 if (conn->flags & CO_FL_WAIT_XPRT) {
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007603 smp->flags = SMP_F_MAY_CHANGE;
7604 return 0;
7605 }
7606
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02007607 smp->data.type = SMP_T_SINT;
Dragan Dosen2dec6a32020-05-11 17:25:19 +02007608 smp->data.u.sint = (long long int)SSL_get_verify_result(ssl);
Emeric Brunbaf8ffb2012-09-21 15:27:20 +02007609 smp->flags = 0;
7610
7611 return 1;
7612}
7613
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01007614/* for ca-file and ca-verify-file */
7615static 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 +02007616{
7617 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01007618 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02007619 return ERR_ALERT | ERR_FATAL;
7620 }
7621
Willy Tarreauef934602016-12-22 23:12:01 +01007622 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01007623 memprintf(ca_file_p, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02007624 else
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01007625 memprintf(ca_file_p, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02007626
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01007627 if (!ssl_store_load_locations_file(*ca_file_p)) {
7628 memprintf(err, "'%s' : unable to load %s", args[cur_arg], *ca_file_p);
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02007629 return ERR_ALERT | ERR_FATAL;
7630 }
Emeric Brund94b3fe2012-09-20 18:23:56 +02007631 return 0;
7632}
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01007633
7634/* parse the "ca-file" bind keyword */
7635static int ssl_bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
7636{
7637 return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_file, err);
7638}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007639static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7640{
7641 return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err);
7642}
Emeric Brund94b3fe2012-09-20 18:23:56 +02007643
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +01007644/* parse the "ca-verify-file" bind keyword */
7645static int ssl_bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
7646{
7647 return ssl_bind_parse_ca_file_common(args, cur_arg, &conf->ca_verify_file, err);
7648}
7649static int bind_parse_ca_verify_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7650{
7651 return ssl_bind_parse_ca_verify_file(args, cur_arg, px, &conf->ssl_conf, err);
7652}
7653
Christopher Faulet31af49d2015-06-09 17:29:50 +02007654/* parse the "ca-sign-file" bind keyword */
7655static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7656{
7657 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01007658 memprintf(err, "'%s' : missing CAfile path", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02007659 return ERR_ALERT | ERR_FATAL;
7660 }
7661
Willy Tarreauef934602016-12-22 23:12:01 +01007662 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
7663 memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02007664 else
7665 memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]);
7666
7667 return 0;
7668}
7669
Bertrand Jacquinff13c062016-11-13 16:37:11 +00007670/* parse the "ca-sign-pass" bind keyword */
Christopher Faulet31af49d2015-06-09 17:29:50 +02007671static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7672{
7673 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01007674 memprintf(err, "'%s' : missing CAkey password", args[cur_arg]);
Christopher Faulet31af49d2015-06-09 17:29:50 +02007675 return ERR_ALERT | ERR_FATAL;
7676 }
7677 memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]);
7678 return 0;
7679}
7680
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007681/* parse the "ciphers" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007682static 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 +02007683{
7684 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02007685 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007686 return ERR_ALERT | ERR_FATAL;
7687 }
7688
Emeric Brun76d88952012-10-05 15:47:31 +02007689 free(conf->ciphers);
Willy Tarreau4348fad2012-09-20 16:48:07 +02007690 conf->ciphers = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007691 return 0;
7692}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007693static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7694{
7695 return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err);
7696}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02007697
Emmanuel Hocdet839af572019-05-14 16:27:35 +02007698#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02007699/* parse the "ciphersuites" bind keyword */
7700static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
7701{
7702 if (!*args[cur_arg + 1]) {
7703 memprintf(err, "'%s' : missing cipher suite", args[cur_arg]);
7704 return ERR_ALERT | ERR_FATAL;
7705 }
7706
7707 free(conf->ciphersuites);
7708 conf->ciphersuites = strdup(args[cur_arg + 1]);
7709 return 0;
7710}
7711static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7712{
7713 return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err);
7714}
7715#endif
7716
Willy Tarreaubbc91962019-10-16 16:42:19 +02007717/* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */
Willy Tarreau4348fad2012-09-20 16:48:07 +02007718static 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 +02007719{
Willy Tarreau38011032013-08-13 16:59:39 +02007720 char path[MAXPATHLEN];
Willy Tarreaub75d6922014-04-14 18:05:41 +02007721
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007722 if (!*args[cur_arg + 1]) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02007723 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007724 return ERR_ALERT | ERR_FATAL;
7725 }
7726
Willy Tarreauef934602016-12-22 23:12:01 +01007727 if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) {
7728 if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) {
Emeric Brunc8e8d122012-10-02 18:42:10 +02007729 memprintf(err, "'%s' : path too long", args[cur_arg]);
7730 return ERR_ALERT | ERR_FATAL;
7731 }
Willy Tarreauef934602016-12-22 23:12:01 +01007732 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]);
Willy Tarreaubbc91962019-10-16 16:42:19 +02007733 return ssl_sock_load_cert(path, conf, err);
Emeric Brunc8e8d122012-10-02 18:42:10 +02007734 }
7735
Willy Tarreaubbc91962019-10-16 16:42:19 +02007736 return ssl_sock_load_cert(args[cur_arg + 1], conf, err);
Emeric Brund94b3fe2012-09-20 18:23:56 +02007737}
7738
Willy Tarreaubbc91962019-10-16 16:42:19 +02007739/* 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 +01007740static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7741{
Willy Tarreaubbc91962019-10-16 16:42:19 +02007742 int err_code;
7743
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01007744 if (!*args[cur_arg + 1]) {
7745 memprintf(err, "'%s' : missing certificate location", args[cur_arg]);
7746 return ERR_ALERT | ERR_FATAL;
7747 }
7748
William Lallemand6be66ec2020-03-06 22:26:32 +01007749 err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], 0, conf, px, err);
Willy Tarreaubbc91962019-10-16 16:42:19 +02007750 if (err_code)
Willy Tarreauad1731d2013-04-02 17:35:58 +02007751 memprintf(err, "'%s' : %s", args[cur_arg], *err);
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01007752
Willy Tarreaubbc91962019-10-16 16:42:19 +02007753 return err_code;
Emmanuel Hocdetfe616562013-01-22 15:31:15 +01007754}
7755
Emeric Brunfb510ea2012-10-05 12:00:26 +02007756/* parse the "crl-file" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007757static 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 +02007758{
Emeric Brun051cdab2012-10-02 19:25:50 +02007759#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01007760 memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]);
Emeric Brun051cdab2012-10-02 19:25:50 +02007761 return ERR_ALERT | ERR_FATAL;
7762#else
Emeric Brund94b3fe2012-09-20 18:23:56 +02007763 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01007764 memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02007765 return ERR_ALERT | ERR_FATAL;
7766 }
Emeric Brun2b58d042012-09-20 17:10:03 +02007767
Willy Tarreauef934602016-12-22 23:12:01 +01007768 if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base)
7769 memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02007770 else
7771 memprintf(&conf->crl_file, "%s", args[cur_arg + 1]);
Emeric Brunc8e8d122012-10-02 18:42:10 +02007772
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01007773 if (!ssl_store_load_locations_file(conf->crl_file)) {
7774 memprintf(err, "'%s' : unable to load %s", args[cur_arg], conf->crl_file);
7775 return ERR_ALERT | ERR_FATAL;
7776 }
Emeric Brun2b58d042012-09-20 17:10:03 +02007777 return 0;
Emeric Brun051cdab2012-10-02 19:25:50 +02007778#endif
Emeric Brun2b58d042012-09-20 17:10:03 +02007779}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007780static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7781{
7782 return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err);
7783}
Emeric Brun2b58d042012-09-20 17:10:03 +02007784
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01007785/* parse the "curves" bind keyword keyword */
7786static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
7787{
Lukas Tribusd14b49c2019-11-24 18:20:40 +01007788#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01007789 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01007790 memprintf(err, "'%s' : missing curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01007791 return ERR_ALERT | ERR_FATAL;
7792 }
7793 conf->curves = strdup(args[cur_arg + 1]);
7794 return 0;
7795#else
Tim Duesterhus93128532019-11-23 23:45:10 +01007796 memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]);
Emmanuel Hocdete7f2b732017-01-09 16:15:54 +01007797 return ERR_ALERT | ERR_FATAL;
7798#endif
7799}
7800static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7801{
7802 return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err);
7803}
7804
Bertrand Jacquinff13c062016-11-13 16:37:11 +00007805/* parse the "ecdhe" bind keyword keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007806static 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 +02007807{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007808#if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL
Tim Duesterhus93128532019-11-23 23:45:10 +01007809 memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02007810 return ERR_ALERT | ERR_FATAL;
7811#elif defined(OPENSSL_NO_ECDH)
Tim Duesterhus93128532019-11-23 23:45:10 +01007812 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 +02007813 return ERR_ALERT | ERR_FATAL;
7814#else
7815 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01007816 memprintf(err, "'%s' : missing named curve", args[cur_arg]);
Emeric Brun2b58d042012-09-20 17:10:03 +02007817 return ERR_ALERT | ERR_FATAL;
7818 }
7819
7820 conf->ecdhe = strdup(args[cur_arg + 1]);
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007821
7822 return 0;
Emeric Brun2b58d042012-09-20 17:10:03 +02007823#endif
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007824}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007825static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7826{
7827 return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err);
7828}
Willy Tarreau79eeafa2012-09-14 07:53:05 +02007829
Bertrand Jacquinff13c062016-11-13 16:37:11 +00007830/* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */
Emeric Brun81c00f02012-09-21 14:31:21 +02007831static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7832{
7833 int code;
7834 char *p = args[cur_arg + 1];
7835 unsigned long long *ignerr = &conf->crt_ignerr;
7836
7837 if (!*p) {
Tim Duesterhus93128532019-11-23 23:45:10 +01007838 memprintf(err, "'%s' : missing error IDs list", args[cur_arg]);
Emeric Brun81c00f02012-09-21 14:31:21 +02007839 return ERR_ALERT | ERR_FATAL;
7840 }
7841
7842 if (strcmp(args[cur_arg], "ca-ignore-err") == 0)
7843 ignerr = &conf->ca_ignerr;
7844
7845 if (strcmp(p, "all") == 0) {
7846 *ignerr = ~0ULL;
7847 return 0;
7848 }
7849
7850 while (p) {
7851 code = atoi(p);
7852 if ((code <= 0) || (code > 63)) {
Tim Duesterhus93128532019-11-23 23:45:10 +01007853 memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'",
7854 args[cur_arg], code, args[cur_arg + 1]);
Emeric Brun81c00f02012-09-21 14:31:21 +02007855 return ERR_ALERT | ERR_FATAL;
7856 }
7857 *ignerr |= 1ULL << code;
7858 p = strchr(p, ',');
7859 if (p)
7860 p++;
7861 }
7862
Emeric Brun2cb7ae52012-10-05 14:14:21 +02007863 return 0;
7864}
7865
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007866/* parse tls_method_options "no-xxx" and "force-xxx" */
7867static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err)
Emeric Brun2cb7ae52012-10-05 14:14:21 +02007868{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007869 uint16_t v;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007870 char *p;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007871 p = strchr(arg, '-');
7872 if (!p)
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007873 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007874 p++;
7875 if (!strcmp(p, "sslv3"))
7876 v = CONF_SSLV3;
7877 else if (!strcmp(p, "tlsv10"))
7878 v = CONF_TLSV10;
7879 else if (!strcmp(p, "tlsv11"))
7880 v = CONF_TLSV11;
7881 else if (!strcmp(p, "tlsv12"))
7882 v = CONF_TLSV12;
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +02007883 else if (!strcmp(p, "tlsv13"))
7884 v = CONF_TLSV13;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007885 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007886 goto fail;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007887 if (!strncmp(arg, "no-", 3))
7888 methods->flags |= methodVersions[v].flag;
7889 else if (!strncmp(arg, "force-", 6))
7890 methods->min = methods->max = v;
7891 else
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007892 goto fail;
Emeric Brun2d0c4822012-10-02 13:45:20 +02007893 return 0;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007894 fail:
Tim Duesterhus93128532019-11-23 23:45:10 +01007895 memprintf(err, "'%s' : option not implemented", arg);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007896 return ERR_ALERT | ERR_FATAL;
Emeric Brun2d0c4822012-10-02 13:45:20 +02007897}
7898
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007899static 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 +02007900{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02007901 return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err);
Emeric Brun2cb7ae52012-10-05 14:14:21 +02007902}
7903
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007904static 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 +02007905{
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007906 return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err);
7907}
7908
7909/* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */
7910static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err)
7911{
7912 uint16_t i, v = 0;
7913 char *argv = args[cur_arg + 1];
7914 if (!*argv) {
Tim Duesterhus93128532019-11-23 23:45:10 +01007915 memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007916 return ERR_ALERT | ERR_FATAL;
7917 }
7918 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
7919 if (!strcmp(argv, methodVersions[i].name))
7920 v = i;
7921 if (!v) {
Tim Duesterhus93128532019-11-23 23:45:10 +01007922 memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]);
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02007923 return ERR_ALERT | ERR_FATAL;
7924 }
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007925 if (!strcmp("ssl-min-ver", args[cur_arg]))
7926 methods->min = v;
7927 else if (!strcmp("ssl-max-ver", args[cur_arg]))
7928 methods->max = v;
7929 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01007930 memprintf(err, "'%s' : option not implemented", args[cur_arg]);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007931 return ERR_ALERT | ERR_FATAL;
7932 }
Emeric Brun2cb7ae52012-10-05 14:14:21 +02007933 return 0;
Emeric Brun2cb7ae52012-10-05 14:14:21 +02007934}
Emeric Brun2cb7ae52012-10-05 14:14:21 +02007935
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +02007936static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
7937{
Willy Tarreau9a1ab082019-05-09 13:26:41 +02007938#if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL)
Christopher Faulet767a84b2017-11-24 16:50:31 +01007939 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 +02007940#endif
7941 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err);
7942}
7943
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007944static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7945{
Emmanuel Hocdet43664762017-08-09 18:26:20 +02007946 return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err);
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02007947}
7948
7949static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
7950{
7951 return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err);
7952}
7953
Emeric Brun2d0c4822012-10-02 13:45:20 +02007954/* parse the "no-tls-tickets" bind keyword */
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01007955static 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 +02007956{
Emeric Brun89675492012-10-05 13:48:26 +02007957 conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS;
Emeric Brun81c00f02012-09-21 14:31:21 +02007958 return 0;
7959}
Emeric Brun2d0c4822012-10-02 13:45:20 +02007960
Olivier Houchardc2aae742017-09-22 18:26:28 +02007961/* parse the "allow-0rtt" bind keyword */
7962static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
7963{
7964 conf->early_data = 1;
7965 return 0;
7966}
7967
7968static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
7969{
Olivier Houchard9679ac92017-10-27 14:58:08 +02007970 conf->ssl_conf.early_data = 1;
Olivier Houchardc2aae742017-09-22 18:26:28 +02007971 return 0;
7972}
7973
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02007974/* parse the "npn" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01007975static 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 +02007976{
Bernard Spil13c53f82018-02-15 13:34:58 +01007977#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02007978 char *p1, *p2;
7979
7980 if (!*args[cur_arg + 1]) {
7981 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]);
7982 return ERR_ALERT | ERR_FATAL;
7983 }
7984
7985 free(conf->npn_str);
7986
Willy Tarreau3724da12016-02-12 17:11:12 +01007987 /* the NPN string is built as a suite of (<len> <name>)*,
7988 * so we reuse each comma to store the next <len> and need
7989 * one more for the end of the string.
7990 */
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02007991 conf->npn_len = strlen(args[cur_arg + 1]) + 1;
Willy Tarreau3724da12016-02-12 17:11:12 +01007992 conf->npn_str = calloc(1, conf->npn_len + 1);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02007993 memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len);
7994
7995 /* replace commas with the name length */
7996 p1 = conf->npn_str;
7997 p2 = p1 + 1;
7998 while (1) {
7999 p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1));
8000 if (!p2)
8001 p2 = p1 + 1 + strlen(p1 + 1);
8002
8003 if (p2 - (p1 + 1) > 255) {
8004 *p2 = '\0';
8005 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1);
8006 return ERR_ALERT | ERR_FATAL;
8007 }
8008
8009 *p1 = p2 - (p1 + 1);
8010 p1 = p2;
8011
8012 if (!*p2)
8013 break;
8014
8015 *(p2++) = '\0';
8016 }
8017 return 0;
8018#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008019 memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]);
Willy Tarreau6c9a3d52012-10-18 18:57:14 +02008020 return ERR_ALERT | ERR_FATAL;
8021#endif
8022}
8023
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008024static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8025{
8026 return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err);
8027}
8028
Christopher Fauletd75f57e2020-04-20 18:32:29 +02008029
8030/* Parses a alpn string and converts it to the right format for the SSL api */
8031int ssl_sock_parse_alpn(char *arg, char **alpn_str, int *alpn_len, char **err)
Willy Tarreauab861d32013-04-02 02:30:41 +02008032{
Christopher Fauletd75f57e2020-04-20 18:32:29 +02008033 char *p1, *p2, *alpn = NULL;
8034 int len, ret = 0;
Willy Tarreauab861d32013-04-02 02:30:41 +02008035
Christopher Fauletd75f57e2020-04-20 18:32:29 +02008036 *alpn_str = NULL;
8037 *alpn_len = 0;
Willy Tarreauab861d32013-04-02 02:30:41 +02008038
Christopher Fauletd75f57e2020-04-20 18:32:29 +02008039 if (!*arg) {
8040 memprintf(err, "missing the comma-delimited ALPN protocol suite");
8041 goto error;
8042 }
Willy Tarreauab861d32013-04-02 02:30:41 +02008043
Marcoen Hirschbergbef60912016-02-12 17:05:24 +01008044 /* the ALPN string is built as a suite of (<len> <name>)*,
8045 * so we reuse each comma to store the next <len> and need
8046 * one more for the end of the string.
8047 */
Christopher Fauletd75f57e2020-04-20 18:32:29 +02008048 len = strlen(arg) + 1;
8049 alpn = calloc(1, len+1);
8050 if (!alpn) {
8051 memprintf(err, "'%s' : out of memory", arg);
8052 goto error;
8053 }
8054 memcpy(alpn+1, arg, len);
Willy Tarreauab861d32013-04-02 02:30:41 +02008055
8056 /* replace commas with the name length */
Christopher Fauletd75f57e2020-04-20 18:32:29 +02008057 p1 = alpn;
Willy Tarreauab861d32013-04-02 02:30:41 +02008058 p2 = p1 + 1;
8059 while (1) {
Christopher Fauletd75f57e2020-04-20 18:32:29 +02008060 p2 = memchr(p1 + 1, ',', alpn + len - (p1 + 1));
Willy Tarreauab861d32013-04-02 02:30:41 +02008061 if (!p2)
8062 p2 = p1 + 1 + strlen(p1 + 1);
8063
8064 if (p2 - (p1 + 1) > 255) {
8065 *p2 = '\0';
Christopher Fauletd75f57e2020-04-20 18:32:29 +02008066 memprintf(err, "ALPN protocol name too long : '%s'", p1 + 1);
8067 goto error;
Willy Tarreauab861d32013-04-02 02:30:41 +02008068 }
8069
8070 *p1 = p2 - (p1 + 1);
8071 p1 = p2;
8072
8073 if (!*p2)
8074 break;
8075
8076 *(p2++) = '\0';
8077 }
Christopher Fauletd75f57e2020-04-20 18:32:29 +02008078
8079 *alpn_str = alpn;
8080 *alpn_len = len;
8081
8082 out:
8083 return ret;
8084
8085 error:
8086 free(alpn);
8087 ret = ERR_ALERT | ERR_FATAL;
8088 goto out;
8089}
8090
8091/* parse the "alpn" bind keyword */
8092static int ssl_bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8093{
8094#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
8095 int ret;
8096
8097 free(conf->alpn_str);
8098
8099 ret = ssl_sock_parse_alpn(args[cur_arg + 1], &conf->alpn_str, &conf->alpn_len, err);
8100 if (ret)
8101 memprintf(err, "'%s' : %s", args[cur_arg], *err);
8102 return ret;
Willy Tarreauab861d32013-04-02 02:30:41 +02008103#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008104 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]);
Willy Tarreauab861d32013-04-02 02:30:41 +02008105 return ERR_ALERT | ERR_FATAL;
8106#endif
8107}
8108
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008109static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8110{
8111 return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err);
8112}
8113
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008114/* parse the "ssl" bind keyword */
Willy Tarreau4348fad2012-09-20 16:48:07 +02008115static 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 +02008116{
Willy Tarreau71a8c7c2016-12-21 22:04:54 +01008117 conf->xprt = &ssl_sock;
Willy Tarreau4348fad2012-09-20 16:48:07 +02008118 conf->is_ssl = 1;
Emeric Brun76d88952012-10-05 15:47:31 +02008119
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008120 if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers)
8121 conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers);
Jerome Magninb203ff62020-04-03 15:28:22 +02008122#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
8123 if (global_ssl.listen_default_curves && !conf->ssl_conf.curves)
8124 conf->ssl_conf.curves = strdup(global_ssl.listen_default_curves);
8125#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008126#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008127 if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites)
8128 conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
8129#endif
Emmanuel Hocdet4608ed92017-01-20 13:06:27 +01008130 conf->ssl_options |= global_ssl.listen_default_ssloptions;
Emmanuel Hocdet43664762017-08-09 18:26:20 +02008131 conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags;
8132 if (!conf->ssl_conf.ssl_methods.min)
8133 conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min;
8134 if (!conf->ssl_conf.ssl_methods.max)
8135 conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max;
Emeric Brun76d88952012-10-05 15:47:31 +02008136
Willy Tarreau79eeafa2012-09-14 07:53:05 +02008137 return 0;
8138}
8139
Lukas Tribus53ae85c2017-05-04 15:45:40 +00008140/* parse the "prefer-client-ciphers" bind keyword */
8141static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8142{
8143 conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH;
8144 return 0;
8145}
8146
Christopher Faulet31af49d2015-06-09 17:29:50 +02008147/* parse the "generate-certificates" bind keyword */
8148static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8149{
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01008150#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Christopher Faulet31af49d2015-06-09 17:29:50 +02008151 conf->generate_certs = 1;
8152#else
8153 memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n",
8154 err && *err ? *err : "");
8155#endif
8156 return 0;
8157}
8158
Emmanuel Hocdet65623372013-01-24 17:17:15 +01008159/* parse the "strict-sni" bind keyword */
8160static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8161{
8162 conf->strict_sni = 1;
8163 return 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008164}
8165
8166/* parse the "tls-ticket-keys" bind keyword */
8167static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8168{
8169#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
Christopher Faulete566f3d2019-10-21 09:55:49 +02008170 FILE *f = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008171 int i = 0;
8172 char thisline[LINESIZE];
Christopher Faulete566f3d2019-10-21 09:55:49 +02008173 struct tls_keys_ref *keys_ref = NULL;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008174
8175 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008176 memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02008177 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008178 }
8179
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008180 keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]);
Willy Tarreau17b4aa12018-07-17 10:05:32 +02008181 if (keys_ref) {
8182 keys_ref->refcount++;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008183 conf->keys_ref = keys_ref;
8184 return 0;
8185 }
8186
Christopher Faulete566f3d2019-10-21 09:55:49 +02008187 keys_ref = calloc(1, sizeof(*keys_ref));
Emeric Brun09852f72019-01-10 10:51:13 +01008188 if (!keys_ref) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008189 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02008190 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01008191 }
8192
Emeric Brun9e754772019-01-10 17:51:55 +01008193 keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key));
Emeric Brun09852f72019-01-10 10:51:13 +01008194 if (!keys_ref->tlskeys) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008195 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02008196 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01008197 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008198
8199 if ((f = fopen(args[cur_arg + 1], "r")) == NULL) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008200 memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02008201 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008202 }
8203
Nenad Merdanovic146defa2015-05-09 08:46:00 +02008204 keys_ref->filename = strdup(args[cur_arg + 1]);
Emeric Brun09852f72019-01-10 10:51:13 +01008205 if (!keys_ref->filename) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008206 memprintf(err, "'%s' : allocation error", args[cur_arg+1]);
Christopher Faulete566f3d2019-10-21 09:55:49 +02008207 goto fail;
Emeric Brun09852f72019-01-10 10:51:13 +01008208 }
Nenad Merdanovic146defa2015-05-09 08:46:00 +02008209
Emeric Brun9e754772019-01-10 17:51:55 +01008210 keys_ref->key_size_bits = 0;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008211 while (fgets(thisline, sizeof(thisline), f) != NULL) {
8212 int len = strlen(thisline);
Emeric Brun9e754772019-01-10 17:51:55 +01008213 int dec_size;
8214
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008215 /* Strip newline characters from the end */
8216 if(thisline[len - 1] == '\n')
8217 thisline[--len] = 0;
8218
8219 if(thisline[len - 1] == '\r')
8220 thisline[--len] = 0;
8221
Emeric Brun9e754772019-01-10 17:51:55 +01008222 dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key));
8223 if (dec_size < 0) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008224 memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02008225 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008226 }
Emeric Brun9e754772019-01-10 17:51:55 +01008227 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) {
8228 keys_ref->key_size_bits = 128;
8229 }
8230 else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) {
8231 keys_ref->key_size_bits = 256;
8232 }
8233 else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256)))
8234 || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128)))
8235 || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008236 memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1);
Christopher Faulete566f3d2019-10-21 09:55:49 +02008237 goto fail;
Emeric Brun9e754772019-01-10 17:51:55 +01008238 }
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008239 i++;
8240 }
8241
8242 if (i < TLS_TICKETS_NO) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008243 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 +02008244 goto fail;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008245 }
8246
8247 fclose(f);
8248
8249 /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */
Nenad Merdanovic17891152016-03-25 22:16:57 +01008250 i -= 2;
8251 keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO;
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008252 keys_ref->unique_id = -1;
Willy Tarreau17b4aa12018-07-17 10:05:32 +02008253 keys_ref->refcount = 1;
Christopher Faulet16f45c82018-02-16 11:23:49 +01008254 HA_RWLOCK_INIT(&keys_ref->lock);
Nenad Merdanovic146defa2015-05-09 08:46:00 +02008255 conf->keys_ref = keys_ref;
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008256
Nenad Merdanovic200b0fa2015-05-09 08:46:01 +02008257 LIST_ADD(&tlskeys_reference, &keys_ref->list);
8258
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008259 return 0;
Christopher Faulete566f3d2019-10-21 09:55:49 +02008260
8261 fail:
8262 if (f)
8263 fclose(f);
8264 if (keys_ref) {
8265 free(keys_ref->filename);
8266 free(keys_ref->tlskeys);
8267 free(keys_ref);
8268 }
8269 return ERR_ALERT | ERR_FATAL;
8270
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008271#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008272 memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]);
Nenad Merdanovic05552d42015-02-27 19:56:49 +01008273 return ERR_ALERT | ERR_FATAL;
8274#endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
Emmanuel Hocdet65623372013-01-24 17:17:15 +01008275}
8276
Emeric Brund94b3fe2012-09-20 18:23:56 +02008277/* parse the "verify" bind keyword */
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008278static 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 +02008279{
8280 if (!*args[cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008281 memprintf(err, "'%s' : missing verify method", args[cur_arg]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008282 return ERR_ALERT | ERR_FATAL;
8283 }
8284
8285 if (strcmp(args[cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008286 conf->verify = SSL_SOCK_VERIFY_NONE;
Emeric Brund94b3fe2012-09-20 18:23:56 +02008287 else if (strcmp(args[cur_arg + 1], "optional") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008288 conf->verify = SSL_SOCK_VERIFY_OPTIONAL;
Emeric Brund94b3fe2012-09-20 18:23:56 +02008289 else if (strcmp(args[cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008290 conf->verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brund94b3fe2012-09-20 18:23:56 +02008291 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01008292 memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n",
8293 args[cur_arg], args[cur_arg + 1]);
Emeric Brund94b3fe2012-09-20 18:23:56 +02008294 return ERR_ALERT | ERR_FATAL;
8295 }
8296
8297 return 0;
8298}
Emmanuel Hocdet98263292016-12-29 18:26:15 +01008299static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8300{
8301 return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err);
8302}
Emeric Brund94b3fe2012-09-20 18:23:56 +02008303
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +02008304/* parse the "no-ca-names" bind keyword */
8305static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err)
8306{
8307 conf->no_ca_names = 1;
8308 return 0;
8309}
8310static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err)
8311{
8312 return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err);
8313}
8314
Willy Tarreau92faadf2012-10-10 23:04:25 +02008315/************** "server" keywords ****************/
8316
Olivier Houchardc7566002018-11-20 23:33:50 +01008317/* parse the "npn" bind keyword */
8318static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8319{
8320#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
8321 char *p1, *p2;
8322
8323 if (!*args[*cur_arg + 1]) {
8324 memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]);
8325 return ERR_ALERT | ERR_FATAL;
8326 }
8327
8328 free(newsrv->ssl_ctx.npn_str);
8329
8330 /* the NPN string is built as a suite of (<len> <name>)*,
8331 * so we reuse each comma to store the next <len> and need
8332 * one more for the end of the string.
8333 */
8334 newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1;
8335 newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1);
8336 memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1],
8337 newsrv->ssl_ctx.npn_len);
8338
8339 /* replace commas with the name length */
8340 p1 = newsrv->ssl_ctx.npn_str;
8341 p2 = p1 + 1;
8342 while (1) {
8343 p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str +
8344 newsrv->ssl_ctx.npn_len - (p1 + 1));
8345 if (!p2)
8346 p2 = p1 + 1 + strlen(p1 + 1);
8347
8348 if (p2 - (p1 + 1) > 255) {
8349 *p2 = '\0';
8350 memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1);
8351 return ERR_ALERT | ERR_FATAL;
8352 }
8353
8354 *p1 = p2 - (p1 + 1);
8355 p1 = p2;
8356
8357 if (!*p2)
8358 break;
8359
8360 *(p2++) = '\0';
8361 }
8362 return 0;
8363#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008364 memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01008365 return ERR_ALERT | ERR_FATAL;
8366#endif
8367}
8368
Olivier Houchard92150142018-12-21 19:47:01 +01008369/* parse the "alpn" or the "check-alpn" server keyword */
Olivier Houchardc7566002018-11-20 23:33:50 +01008370static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8371{
8372#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Olivier Houchard92150142018-12-21 19:47:01 +01008373 char **alpn_str;
8374 int *alpn_len;
Christopher Fauletd75f57e2020-04-20 18:32:29 +02008375 int ret;
Olivier Houchardc7566002018-11-20 23:33:50 +01008376
Olivier Houchard92150142018-12-21 19:47:01 +01008377 if (*args[*cur_arg] == 'c') {
8378 alpn_str = &newsrv->check.alpn_str;
8379 alpn_len = &newsrv->check.alpn_len;
8380 } else {
8381 alpn_str = &newsrv->ssl_ctx.alpn_str;
8382 alpn_len = &newsrv->ssl_ctx.alpn_len;
8383
8384 }
Olivier Houchardc7566002018-11-20 23:33:50 +01008385
Olivier Houchard92150142018-12-21 19:47:01 +01008386 free(*alpn_str);
Christopher Fauletd75f57e2020-04-20 18:32:29 +02008387 ret = ssl_sock_parse_alpn(args[*cur_arg + 1], alpn_str, alpn_len, err);
8388 if (ret)
8389 memprintf(err, "'%s' : %s", args[*cur_arg], *err);
8390 return ret;
Olivier Houchardc7566002018-11-20 23:33:50 +01008391#else
Tim Duesterhus93128532019-11-23 23:45:10 +01008392 memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]);
Olivier Houchardc7566002018-11-20 23:33:50 +01008393 return ERR_ALERT | ERR_FATAL;
8394#endif
8395}
8396
Emeric Brunef42d922012-10-11 16:11:36 +02008397/* parse the "ca-file" server keyword */
8398static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8399{
8400 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008401 memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02008402 return ERR_ALERT | ERR_FATAL;
8403 }
8404
Willy Tarreauef934602016-12-22 23:12:01 +01008405 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
8406 memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008407 else
8408 memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]);
8409
Emmanuel Hocdetd4f9a602019-10-24 11:32:47 +02008410 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.ca_file)) {
8411 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.ca_file);
8412 return ERR_ALERT | ERR_FATAL;
8413 }
Emeric Brunef42d922012-10-11 16:11:36 +02008414 return 0;
8415}
8416
Olivier Houchard9130a962017-10-17 17:33:43 +02008417/* parse the "check-sni" server keyword */
8418static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8419{
8420 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008421 memprintf(err, "'%s' : missing SNI", args[*cur_arg]);
Olivier Houchard9130a962017-10-17 17:33:43 +02008422 return ERR_ALERT | ERR_FATAL;
8423 }
8424
8425 newsrv->check.sni = strdup(args[*cur_arg + 1]);
8426 if (!newsrv->check.sni) {
8427 memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]);
8428 return ERR_ALERT | ERR_FATAL;
8429 }
8430 return 0;
8431
8432}
8433
Willy Tarreau92faadf2012-10-10 23:04:25 +02008434/* parse the "check-ssl" server keyword */
8435static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8436{
8437 newsrv->check.use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01008438 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
8439 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008440#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008441 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
8442 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
8443#endif
Willy Tarreauef934602016-12-22 23:12:01 +01008444 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008445 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
8446 if (!newsrv->ssl_ctx.methods.min)
8447 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
8448 if (!newsrv->ssl_ctx.methods.max)
8449 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
8450
Willy Tarreau92faadf2012-10-10 23:04:25 +02008451 return 0;
8452}
8453
8454/* parse the "ciphers" server keyword */
8455static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8456{
8457 if (!*args[*cur_arg + 1]) {
8458 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
8459 return ERR_ALERT | ERR_FATAL;
8460 }
8461
8462 free(newsrv->ssl_ctx.ciphers);
8463 newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]);
8464 return 0;
8465}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008466
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008467#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008468/* parse the "ciphersuites" server keyword */
8469static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8470{
8471 if (!*args[*cur_arg + 1]) {
8472 memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]);
8473 return ERR_ALERT | ERR_FATAL;
8474 }
8475
8476 free(newsrv->ssl_ctx.ciphersuites);
8477 newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]);
8478 return 0;
8479}
8480#endif
Willy Tarreau92faadf2012-10-10 23:04:25 +02008481
Emeric Brunef42d922012-10-11 16:11:36 +02008482/* parse the "crl-file" server keyword */
8483static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8484{
8485#ifndef X509_V_FLAG_CRL_CHECK
Tim Duesterhus93128532019-11-23 23:45:10 +01008486 memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02008487 return ERR_ALERT | ERR_FATAL;
8488#else
8489 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008490 memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02008491 return ERR_ALERT | ERR_FATAL;
8492 }
8493
Willy Tarreauef934602016-12-22 23:12:01 +01008494 if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base)
8495 memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008496 else
8497 memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]);
8498
Emmanuel Hocdetb270e812019-11-21 19:09:31 +01008499 if (!ssl_store_load_locations_file(newsrv->ssl_ctx.crl_file)) {
8500 memprintf(err, "'%s' : unable to load %s", args[*cur_arg], newsrv->ssl_ctx.crl_file);
8501 return ERR_ALERT | ERR_FATAL;
8502 }
Emeric Brunef42d922012-10-11 16:11:36 +02008503 return 0;
8504#endif
8505}
8506
Emeric Bruna7aa3092012-10-26 12:58:00 +02008507/* parse the "crt" server keyword */
8508static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8509{
8510 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008511 memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02008512 return ERR_ALERT | ERR_FATAL;
8513 }
8514
Willy Tarreauef934602016-12-22 23:12:01 +01008515 if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base)
Christopher Fauletff3a41e2017-11-23 09:13:32 +01008516 memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]);
Emeric Bruna7aa3092012-10-26 12:58:00 +02008517 else
8518 memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]);
8519
8520 return 0;
8521}
Emeric Brunef42d922012-10-11 16:11:36 +02008522
Frédéric Lécaille340ae602017-03-13 10:38:04 +01008523/* parse the "no-check-ssl" server keyword */
8524static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8525{
Christopher Fauletf61f33a2020-03-27 18:55:49 +01008526 newsrv->check.use_ssl = -1;
Frédéric Lécaille340ae602017-03-13 10:38:04 +01008527 free(newsrv->ssl_ctx.ciphers);
8528 newsrv->ssl_ctx.ciphers = NULL;
8529 newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions;
8530 return 0;
8531}
8532
Frédéric Lécaillee892c4c2017-03-13 12:08:01 +01008533/* parse the "no-send-proxy-v2-ssl" server keyword */
8534static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8535{
8536 newsrv->pp_opts &= ~SRV_PP_V2;
8537 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
8538 return 0;
8539}
8540
8541/* parse the "no-send-proxy-v2-ssl-cn" server keyword */
8542static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8543{
8544 newsrv->pp_opts &= ~SRV_PP_V2;
8545 newsrv->pp_opts &= ~SRV_PP_V2_SSL;
8546 newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN;
8547 return 0;
8548}
8549
Frédéric Lécaillee381d762017-03-13 11:54:17 +01008550/* parse the "no-ssl" server keyword */
8551static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8552{
Christopher Fauletf61f33a2020-03-27 18:55:49 +01008553 newsrv->use_ssl = -1;
Frédéric Lécaillee381d762017-03-13 11:54:17 +01008554 free(newsrv->ssl_ctx.ciphers);
8555 newsrv->ssl_ctx.ciphers = NULL;
8556 return 0;
8557}
8558
Olivier Houchard522eea72017-11-03 16:27:47 +01008559/* parse the "allow-0rtt" server keyword */
8560static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8561{
8562 newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA;
8563 return 0;
8564}
8565
Willy Tarreau2a3fb1c2015-02-05 16:47:07 +01008566/* parse the "no-ssl-reuse" server keyword */
8567static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8568{
8569 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE;
8570 return 0;
8571}
8572
Emeric Brunf9c5c472012-10-11 15:28:34 +02008573/* parse the "no-tls-tickets" server keyword */
8574static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8575{
8576 newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS;
8577 return 0;
8578}
David Safb76832014-05-08 23:42:08 -04008579/* parse the "send-proxy-v2-ssl" server keyword */
8580static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8581{
8582 newsrv->pp_opts |= SRV_PP_V2;
8583 newsrv->pp_opts |= SRV_PP_V2_SSL;
8584 return 0;
8585}
8586
8587/* parse the "send-proxy-v2-ssl-cn" server keyword */
8588static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8589{
8590 newsrv->pp_opts |= SRV_PP_V2;
8591 newsrv->pp_opts |= SRV_PP_V2_SSL;
8592 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
8593 return 0;
8594}
Emeric Brunf9c5c472012-10-11 15:28:34 +02008595
Willy Tarreau732eac42015-07-09 11:40:25 +02008596/* parse the "sni" server keyword */
8597static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8598{
8599#ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME
8600 memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]);
8601 return ERR_ALERT | ERR_FATAL;
8602#else
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01008603 char *arg;
Willy Tarreau732eac42015-07-09 11:40:25 +02008604
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01008605 arg = args[*cur_arg + 1];
8606 if (!*arg) {
Willy Tarreau732eac42015-07-09 11:40:25 +02008607 memprintf(err, "'%s' : missing sni expression", args[*cur_arg]);
8608 return ERR_ALERT | ERR_FATAL;
8609 }
8610
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01008611 free(newsrv->sni_expr);
8612 newsrv->sni_expr = strdup(arg);
Willy Tarreau732eac42015-07-09 11:40:25 +02008613
Willy Tarreau732eac42015-07-09 11:40:25 +02008614 return 0;
8615#endif
8616}
8617
Willy Tarreau92faadf2012-10-10 23:04:25 +02008618/* parse the "ssl" server keyword */
8619static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8620{
8621 newsrv->use_ssl = 1;
Willy Tarreauef934602016-12-22 23:12:01 +01008622 if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers)
8623 newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +02008624#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02008625 if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites)
8626 newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
8627#endif
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02008628 newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions;
8629 newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags;
8630
8631 if (!newsrv->ssl_ctx.methods.min)
8632 newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min;
8633
8634 if (!newsrv->ssl_ctx.methods.max)
8635 newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max;
8636
8637
Willy Tarreau92faadf2012-10-10 23:04:25 +02008638 return 0;
8639}
8640
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01008641/* parse the "ssl-reuse" server keyword */
8642static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8643{
8644 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE;
8645 return 0;
8646}
8647
Frédéric Lécaille2cfcdbe2017-03-13 11:32:20 +01008648/* parse the "tls-tickets" server keyword */
8649static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8650{
8651 newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS;
8652 return 0;
8653}
8654
Emeric Brunef42d922012-10-11 16:11:36 +02008655/* parse the "verify" server keyword */
8656static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8657{
8658 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008659 memprintf(err, "'%s' : missing verify method", args[*cur_arg]);
Emeric Brunef42d922012-10-11 16:11:36 +02008660 return ERR_ALERT | ERR_FATAL;
8661 }
8662
8663 if (strcmp(args[*cur_arg + 1], "none") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008664 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE;
Emeric Brunef42d922012-10-11 16:11:36 +02008665 else if (strcmp(args[*cur_arg + 1], "required") == 0)
Emeric Brun850efd52014-01-29 12:24:34 +01008666 newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED;
Emeric Brunef42d922012-10-11 16:11:36 +02008667 else {
Tim Duesterhus93128532019-11-23 23:45:10 +01008668 memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n",
8669 args[*cur_arg], args[*cur_arg + 1]);
Emeric Brunef42d922012-10-11 16:11:36 +02008670 return ERR_ALERT | ERR_FATAL;
8671 }
8672
Evan Broderbe554312013-06-27 00:05:25 -07008673 return 0;
8674}
8675
8676/* parse the "verifyhost" server keyword */
8677static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
8678{
8679 if (!*args[*cur_arg + 1]) {
Tim Duesterhus93128532019-11-23 23:45:10 +01008680 memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]);
Evan Broderbe554312013-06-27 00:05:25 -07008681 return ERR_ALERT | ERR_FATAL;
8682 }
8683
Frédéric Lécaille273f3212017-03-13 15:52:01 +01008684 free(newsrv->ssl_ctx.verify_host);
Evan Broderbe554312013-06-27 00:05:25 -07008685 newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]);
8686
Emeric Brunef42d922012-10-11 16:11:36 +02008687 return 0;
8688}
8689
Emeric Brun2c86cbf2014-10-30 15:56:50 +01008690/* parse the "ssl-default-bind-options" keyword in global section */
8691static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx,
8692 struct proxy *defpx, const char *file, int line,
8693 char **err) {
8694 int i = 1;
8695
8696 if (*(args[i]) == 0) {
8697 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
8698 return -1;
8699 }
8700 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008701 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01008702 global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS;
Lukas Tribus53ae85c2017-05-04 15:45:40 +00008703 else if (!strcmp(args[i], "prefer-client-ciphers"))
8704 global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008705 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
8706 if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err))
8707 i++;
8708 else {
8709 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
8710 return -1;
8711 }
8712 }
8713 else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01008714 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
8715 return -1;
8716 }
8717 i++;
8718 }
8719 return 0;
8720}
8721
8722/* parse the "ssl-default-server-options" keyword in global section */
8723static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx,
8724 struct proxy *defpx, const char *file, int line,
8725 char **err) {
8726 int i = 1;
8727
8728 if (*(args[i]) == 0) {
8729 memprintf(err, "global statement '%s' expects an option as an argument.", args[0]);
8730 return -1;
8731 }
8732 while (*(args[i])) {
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +02008733 if (!strcmp(args[i], "no-tls-tickets"))
Willy Tarreauef934602016-12-22 23:12:01 +01008734 global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS;
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +02008735 else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) {
8736 if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err))
8737 i++;
8738 else {
8739 memprintf(err, "%s on global statement '%s'.", *err, args[0]);
8740 return -1;
8741 }
8742 }
8743 else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) {
Emeric Brun2c86cbf2014-10-30 15:56:50 +01008744 memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]);
8745 return -1;
8746 }
8747 i++;
8748 }
8749 return 0;
8750}
8751
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01008752/* parse the "ca-base" / "crt-base" keywords in global section.
8753 * Returns <0 on alert, >0 on warning, 0 on success.
8754 */
8755static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx,
8756 struct proxy *defpx, const char *file, int line,
8757 char **err)
8758{
8759 char **target;
8760
Willy Tarreauef934602016-12-22 23:12:01 +01008761 target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01008762
8763 if (too_many_args(1, args, err, NULL))
8764 return -1;
8765
8766 if (*target) {
8767 memprintf(err, "'%s' already specified.", args[0]);
8768 return -1;
8769 }
8770
8771 if (*(args[1]) == 0) {
8772 memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]);
8773 return -1;
8774 }
8775 *target = strdup(args[1]);
8776 return 0;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008777}
8778
Emmanuel Hocdetc3b7e742020-04-22 11:06:19 +02008779/* parse the "ssl-skip-self-issued-ca" keyword in global section. */
8780static int ssl_parse_skip_self_issued_ca(char **args, int section_type, struct proxy *curpx,
8781 struct proxy *defpx, const char *file, int line,
8782 char **err)
8783{
8784 global_ssl.skip_self_issued_ca = 1;
8785 return 0;
8786}
8787
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01008788/* "issuers-chain-path" load chain certificate in global */
8789static int ssl_load_global_issuer_from_BIO(BIO *in, char *fp, char **err)
8790{
8791 X509 *ca;
8792 X509_NAME *name = NULL;
8793 ASN1_OCTET_STRING *skid = NULL;
8794 STACK_OF(X509) *chain = NULL;
8795 struct issuer_chain *issuer;
8796 struct eb64_node *node;
8797 char *path;
8798 u64 key;
8799 int ret = 0;
8800
8801 while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) {
8802 if (chain == NULL) {
8803 chain = sk_X509_new_null();
8804 skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL);
8805 name = X509_get_subject_name(ca);
8806 }
8807 if (!sk_X509_push(chain, ca)) {
8808 X509_free(ca);
8809 goto end;
8810 }
8811 }
8812 if (!chain) {
8813 memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp);
8814 goto end;
8815 }
8816 if (!skid) {
8817 memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp);
8818 goto end;
8819 }
8820 if (!name) {
8821 memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp);
8822 goto end;
8823 }
8824 key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0);
William Lallemande0f3fd52020-02-25 14:53:06 +01008825 for (node = eb64_lookup(&cert_issuer_tree, key); node; node = eb64_next(node)) {
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01008826 issuer = container_of(node, typeof(*issuer), node);
8827 if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) {
8828 memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path);
8829 goto end;
8830 }
8831 }
8832 issuer = calloc(1, sizeof *issuer);
8833 path = strdup(fp);
8834 if (!issuer || !path) {
8835 free(issuer);
8836 free(path);
8837 goto end;
8838 }
8839 issuer->node.key = key;
8840 issuer->path = path;
8841 issuer->chain = chain;
8842 chain = NULL;
William Lallemande0f3fd52020-02-25 14:53:06 +01008843 eb64_insert(&cert_issuer_tree, &issuer->node);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01008844 ret = 1;
8845 end:
8846 if (skid)
8847 ASN1_OCTET_STRING_free(skid);
8848 if (chain)
8849 sk_X509_pop_free(chain, X509_free);
8850 return ret;
8851}
8852
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +01008853static struct issuer_chain* ssl_get0_issuer_chain(X509 *cert)
Emmanuel Hocdet75a7aa12020-02-18 15:19:24 +01008854{
8855 AUTHORITY_KEYID *akid;
8856 struct issuer_chain *issuer = NULL;
8857
8858 akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL);
8859 if (akid) {
8860 struct eb64_node *node;
8861 u64 hk;
8862 hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0);
8863 for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) {
8864 struct issuer_chain *ti = container_of(node, typeof(*issuer), node);
8865 if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) {
8866 issuer = ti;
8867 break;
8868 }
8869 }
8870 AUTHORITY_KEYID_free(akid);
8871 }
8872 return issuer;
8873}
8874
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01008875static void ssl_free_global_issuers(void)
8876{
8877 struct eb64_node *node, *back;
8878 struct issuer_chain *issuer;
8879
William Lallemande0f3fd52020-02-25 14:53:06 +01008880 node = eb64_first(&cert_issuer_tree);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01008881 while (node) {
8882 issuer = container_of(node, typeof(*issuer), node);
8883 back = eb64_next(node);
8884 eb64_delete(node);
8885 free(issuer->path);
8886 sk_X509_pop_free(issuer->chain, X509_free);
8887 free(issuer);
8888 node = back;
8889 }
8890}
8891
8892static int ssl_load_global_issuers_from_path(char **args, int section_type, struct proxy *curpx,
8893 struct proxy *defpx, const char *file, int line,
8894 char **err)
8895{
8896 char *path;
8897 struct dirent **de_list;
8898 int i, n;
8899 struct stat buf;
8900 char *end;
8901 char fp[MAXPATHLEN+1];
8902
8903 if (too_many_args(1, args, err, NULL))
8904 return -1;
8905
8906 path = args[1];
8907 if (*path == 0 || stat(path, &buf)) {
8908 memprintf(err, "%sglobal statement '%s' expects a directory path as an argument.\n",
8909 err && *err ? *err : "", args[0]);
8910 return -1;
8911 }
8912 if (S_ISDIR(buf.st_mode) == 0) {
8913 memprintf(err, "%sglobal statement '%s': %s is not a directory.\n",
8914 err && *err ? *err : "", args[0], path);
8915 return -1;
8916 }
8917
8918 /* strip trailing slashes, including first one */
8919 for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--)
8920 *end = 0;
8921 /* path already parsed? */
8922 if (global_ssl.issuers_chain_path && strcmp(global_ssl.issuers_chain_path, path) == 0)
8923 return 0;
8924 /* overwrite old issuers_chain_path */
8925 free(global_ssl.issuers_chain_path);
8926 global_ssl.issuers_chain_path = strdup(path);
8927 ssl_free_global_issuers();
8928
8929 n = scandir(path, &de_list, 0, alphasort);
8930 if (n < 0) {
8931 memprintf(err, "%sglobal statement '%s': unable to scan directory '%s' : %s.\n",
8932 err && *err ? *err : "", args[0], path, strerror(errno));
8933 return -1;
8934 }
8935 for (i = 0; i < n; i++) {
8936 struct dirent *de = de_list[i];
8937 BIO *in = NULL;
8938 char *warn = NULL;
8939
8940 snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name);
8941 free(de);
8942 if (stat(fp, &buf) != 0) {
8943 ha_warning("unable to stat certificate from file '%s' : %s.\n", fp, strerror(errno));
8944 goto next;
8945 }
8946 if (!S_ISREG(buf.st_mode))
8947 goto next;
8948
8949 in = BIO_new(BIO_s_file());
8950 if (in == NULL)
8951 goto next;
8952 if (BIO_read_filename(in, fp) <= 0)
8953 goto next;
8954 ssl_load_global_issuer_from_BIO(in, fp, &warn);
8955 if (warn) {
Tim Duesterhuse8aa5f22020-02-19 11:41:13 +01008956 ha_warning("%s", warn);
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +01008957 free(warn);
8958 warn = NULL;
8959 }
8960 next:
8961 if (in)
8962 BIO_free(in);
8963 }
8964 free(de_list);
8965
8966 return 0;
8967}
8968
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008969/* parse the "ssl-mode-async" keyword in global section.
8970 * Returns <0 on alert, >0 on warning, 0 on success.
8971 */
8972static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx,
8973 struct proxy *defpx, const char *file, int line,
8974 char **err)
8975{
Willy Tarreau5db847a2019-05-09 14:13:35 +02008976#if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC)
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008977 global_ssl.async = 1;
Emeric Brunece0c332017-12-06 13:51:49 +01008978 global.ssl_used_async_engines = nb_engines;
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008979 return 0;
8980#else
8981 memprintf(err, "'%s': openssl library does not support async mode", args[0]);
8982 return -1;
8983#endif
8984}
8985
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02008986#ifndef OPENSSL_NO_ENGINE
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008987static int ssl_check_async_engine_count(void) {
8988 int err_code = 0;
8989
Emeric Brun3854e012017-05-17 20:42:48 +02008990 if (global_ssl.async && (openssl_engines_initialized > 32)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01008991 ha_alert("ssl-mode-async only supports a maximum of 32 engines.\n");
Grant Zhangfa6c7ee2017-01-14 01:42:15 +00008992 err_code = ERR_ABORT;
8993 }
8994 return err_code;
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +01008995}
8996
Grant Zhang872f9c22017-01-21 01:10:18 +00008997/* parse the "ssl-engine" keyword in global section.
8998 * Returns <0 on alert, >0 on warning, 0 on success.
8999 */
9000static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx,
9001 struct proxy *defpx, const char *file, int line,
9002 char **err)
9003{
9004 char *algo;
9005 int ret = -1;
9006
9007 if (*(args[1]) == 0) {
9008 memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]);
9009 return ret;
9010 }
9011
9012 if (*(args[2]) == 0) {
9013 /* if no list of algorithms is given, it defaults to ALL */
9014 algo = strdup("ALL");
9015 goto add_engine;
9016 }
9017
9018 /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */
9019 if (strcmp(args[2], "algo") != 0) {
9020 memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]);
9021 return ret;
9022 }
9023
9024 if (*(args[3]) == 0) {
9025 memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]);
9026 return ret;
9027 }
9028 algo = strdup(args[3]);
9029
9030add_engine:
9031 if (ssl_init_single_engine(args[1], algo)==0) {
9032 openssl_engines_initialized++;
9033 ret = 0;
9034 }
9035 free(algo);
9036 return ret;
9037}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +02009038#endif
Grant Zhang872f9c22017-01-21 01:10:18 +00009039
Willy Tarreauf22e9682016-12-21 23:23:19 +01009040/* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords
9041 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9042 */
9043static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx,
9044 struct proxy *defpx, const char *file, int line,
9045 char **err)
9046{
9047 char **target;
9048
Willy Tarreauef934602016-12-22 23:12:01 +01009049 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers;
Willy Tarreauf22e9682016-12-21 23:23:19 +01009050
9051 if (too_many_args(1, args, err, NULL))
9052 return -1;
9053
9054 if (*(args[1]) == 0) {
9055 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9056 return -1;
9057 }
9058
9059 free(*target);
9060 *target = strdup(args[1]);
9061 return 0;
9062}
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009063
Emmanuel Hocdet839af572019-05-14 16:27:35 +02009064#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02009065/* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords
9066 * in global section. Returns <0 on alert, >0 on warning, 0 on success.
9067 */
9068static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx,
9069 struct proxy *defpx, const char *file, int line,
9070 char **err)
9071{
9072 char **target;
9073
9074 target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites;
9075
9076 if (too_many_args(1, args, err, NULL))
9077 return -1;
9078
9079 if (*(args[1]) == 0) {
9080 memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]);
9081 return -1;
9082 }
9083
9084 free(*target);
9085 *target = strdup(args[1]);
9086 return 0;
9087}
9088#endif
Willy Tarreauf22e9682016-12-21 23:23:19 +01009089
Jerome Magninb203ff62020-04-03 15:28:22 +02009090#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
9091/*
9092 * parse the "ssl-default-bind-curves" keyword in a global section.
9093 * Returns <0 on alert, >0 on warning, 0 on success.
9094 */
9095static int ssl_parse_global_curves(char **args, int section_type, struct proxy *curpx,
9096 struct proxy *defpx, const char *file, int line,
9097 char **err)
9098{
9099 char **target;
9100 target = &global_ssl.listen_default_curves;
9101
9102 if (too_many_args(1, args, err, NULL))
9103 return -1;
9104
9105 if (*(args[1]) == 0) {
9106 memprintf(err, "global statement '%s' expects a curves suite as an arguments.", args[0]);
9107 return -1;
9108 }
9109
9110 free(*target);
9111 *target = strdup(args[1]);
9112 return 0;
9113}
9114#endif
Willy Tarreau9ceda382016-12-21 23:13:03 +01009115/* parse various global tune.ssl settings consisting in positive integers.
9116 * Returns <0 on alert, >0 on warning, 0 on success.
9117 */
9118static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx,
9119 struct proxy *defpx, const char *file, int line,
9120 char **err)
9121{
9122 int *target;
9123
9124 if (strcmp(args[0], "tune.ssl.cachesize") == 0)
9125 target = &global.tune.sslcachesize;
9126 else if (strcmp(args[0], "tune.ssl.maxrecord") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009127 target = (int *)&global_ssl.max_record;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009128 else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0)
Willy Tarreauef934602016-12-22 23:12:01 +01009129 target = &global_ssl.ctx_cache;
Willy Tarreau0bea58d2016-12-21 23:17:25 +01009130 else if (strcmp(args[0], "maxsslconn") == 0)
9131 target = &global.maxsslconn;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009132 else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0)
9133 target = &global_ssl.capture_cipherlist;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009134 else {
9135 memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]);
9136 return -1;
9137 }
9138
9139 if (too_many_args(1, args, err, NULL))
9140 return -1;
9141
9142 if (*(args[1]) == 0) {
9143 memprintf(err, "'%s' expects an integer argument.", args[0]);
9144 return -1;
9145 }
9146
9147 *target = atoi(args[1]);
9148 if (*target < 0) {
9149 memprintf(err, "'%s' expects a positive numeric value.", args[0]);
9150 return -1;
9151 }
9152 return 0;
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009153}
9154
9155static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx,
9156 struct proxy *defpx, const char *file, int line,
9157 char **err)
9158{
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009159 int ret;
9160
9161 ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err);
9162 if (ret != 0)
9163 return ret;
9164
Willy Tarreaubafbe012017-11-24 17:34:44 +01009165 if (pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009166 memprintf(err, "'%s' is already configured.", args[0]);
9167 return -1;
9168 }
9169
Willy Tarreaubafbe012017-11-24 17:34:44 +01009170 pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED);
9171 if (!pool_head_ssl_capture) {
Thierry FOURNIER5bf77322017-02-25 12:45:22 +01009172 memprintf(err, "Out of memory error.");
9173 return -1;
9174 }
9175 return 0;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009176}
9177
9178/* parse "ssl.force-private-cache".
9179 * Returns <0 on alert, >0 on warning, 0 on success.
9180 */
9181static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx,
9182 struct proxy *defpx, const char *file, int line,
9183 char **err)
9184{
9185 if (too_many_args(0, args, err, NULL))
9186 return -1;
9187
Willy Tarreauef934602016-12-22 23:12:01 +01009188 global_ssl.private_cache = 1;
Willy Tarreau9ceda382016-12-21 23:13:03 +01009189 return 0;
9190}
9191
9192/* parse "ssl.lifetime".
9193 * Returns <0 on alert, >0 on warning, 0 on success.
9194 */
9195static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx,
9196 struct proxy *defpx, const char *file, int line,
9197 char **err)
9198{
9199 const char *res;
9200
9201 if (too_many_args(1, args, err, NULL))
9202 return -1;
9203
9204 if (*(args[1]) == 0) {
9205 memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]);
9206 return -1;
9207 }
9208
Willy Tarreauef934602016-12-22 23:12:01 +01009209 res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S);
Willy Tarreau9faebe32019-06-07 19:00:37 +02009210 if (res == PARSE_TIME_OVER) {
9211 memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).",
9212 args[1], args[0]);
9213 return -1;
9214 }
9215 else if (res == PARSE_TIME_UNDER) {
9216 memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).",
9217 args[1], args[0]);
9218 return -1;
9219 }
9220 else if (res) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009221 memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]);
9222 return -1;
9223 }
9224 return 0;
9225}
9226
9227#ifndef OPENSSL_NO_DH
Willy Tarreau14e36a12016-12-21 23:28:13 +01009228/* parse "ssl-dh-param-file".
9229 * Returns <0 on alert, >0 on warning, 0 on success.
9230 */
9231static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx,
9232 struct proxy *defpx, const char *file, int line,
9233 char **err)
9234{
9235 if (too_many_args(1, args, err, NULL))
9236 return -1;
9237
9238 if (*(args[1]) == 0) {
9239 memprintf(err, "'%s' expects a file path as an argument.", args[0]);
9240 return -1;
9241 }
9242
9243 if (ssl_sock_load_global_dh_param_from_file(args[1])) {
9244 memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]);
9245 return -1;
9246 }
9247 return 0;
9248}
9249
Willy Tarreau9ceda382016-12-21 23:13:03 +01009250/* parse "ssl.default-dh-param".
9251 * Returns <0 on alert, >0 on warning, 0 on success.
9252 */
9253static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx,
9254 struct proxy *defpx, const char *file, int line,
9255 char **err)
9256{
9257 if (too_many_args(1, args, err, NULL))
9258 return -1;
9259
9260 if (*(args[1]) == 0) {
9261 memprintf(err, "'%s' expects an integer argument.", args[0]);
9262 return -1;
9263 }
9264
Willy Tarreauef934602016-12-22 23:12:01 +01009265 global_ssl.default_dh_param = atoi(args[1]);
9266 if (global_ssl.default_dh_param < 1024) {
Willy Tarreau9ceda382016-12-21 23:13:03 +01009267 memprintf(err, "'%s' expects a value >= 1024.", args[0]);
9268 return -1;
9269 }
9270 return 0;
9271}
9272#endif
9273
William Lallemand3af48e72020-02-03 17:15:52 +01009274
9275/*
9276 * parse "ssl-load-extra-files".
9277 * multiple arguments are allowed: "bundle", "sctl", "ocsp", "issuer", "all", "none"
9278 */
9279static int ssl_parse_global_extra_files(char **args, int section_type, struct proxy *curpx,
9280 struct proxy *defpx, const char *file, int line,
9281 char **err)
9282{
9283 int i;
9284 int gf = SSL_GF_NONE;
9285
9286 if (*(args[1]) == 0)
9287 goto err_arg;
9288
9289 for (i = 1; *args[i]; i++) {
9290
9291 if (!strcmp("bundle", args[i])) {
9292 gf |= SSL_GF_BUNDLE;
9293
9294 } else if (!strcmp("sctl", args[i])) {
9295 gf |= SSL_GF_SCTL;
9296
9297 } else if (!strcmp("ocsp", args[i])){
9298 gf |= SSL_GF_OCSP;
9299
9300 } else if (!strcmp("issuer", args[i])){
9301 gf |= SSL_GF_OCSP_ISSUER;
9302
William Lallemand4c5adbf2020-02-24 14:23:22 +01009303 } else if (!strcmp("key", args[i])) {
9304 gf |= SSL_GF_KEY;
9305
William Lallemand3af48e72020-02-03 17:15:52 +01009306 } else if (!strcmp("none", args[i])) {
9307 if (gf != SSL_GF_NONE)
9308 goto err_alone;
9309 gf = SSL_GF_NONE;
9310 i++;
9311 break;
9312
9313 } else if (!strcmp("all", args[i])) {
9314 if (gf != SSL_GF_NONE)
9315 goto err_alone;
9316 gf = SSL_GF_ALL;
9317 i++;
9318 break;
9319 } else {
9320 goto err_arg;
9321 }
9322 }
9323 /* break from loop but there are still arguments */
9324 if (*args[i])
9325 goto err_alone;
9326
9327 global_ssl.extra_files = gf;
9328
9329 return 0;
9330
9331err_alone:
9332 memprintf(err, "'%s' 'none' and 'all' can be only used alone", args[0]);
9333 return -1;
9334
9335err_arg:
9336 memprintf(err, "'%s' expects one or multiple arguments (none, all, bundle, sctl, ocsp, issuer).", args[0]);
9337 return -1;
9338}
9339
Willy Tarreau9ceda382016-12-21 23:13:03 +01009340
William Lallemand32af2032016-10-29 18:09:35 +02009341/* This function is used with TLS ticket keys management. It permits to browse
9342 * each reference. The variable <getnext> must contain the current node,
9343 * <end> point to the root node.
9344 */
9345#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9346static inline
9347struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end)
9348{
9349 struct tls_keys_ref *ref = getnext;
9350
9351 while (1) {
9352
9353 /* Get next list entry. */
9354 ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list);
9355
9356 /* If the entry is the last of the list, return NULL. */
9357 if (&ref->list == end)
9358 return NULL;
9359
9360 return ref;
9361 }
9362}
9363
9364static inline
9365struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference)
9366{
9367 int id;
9368 char *error;
9369
9370 /* If the reference starts by a '#', this is numeric id. */
9371 if (reference[0] == '#') {
9372 /* Try to convert the numeric id. If the conversion fails, the lookup fails. */
9373 id = strtol(reference + 1, &error, 10);
9374 if (*error != '\0')
9375 return NULL;
9376
9377 /* Perform the unique id lookup. */
9378 return tlskeys_ref_lookupid(id);
9379 }
9380
9381 /* Perform the string lookup. */
9382 return tlskeys_ref_lookup(reference);
9383}
9384#endif
9385
9386
9387#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
9388
9389static int cli_io_handler_tlskeys_files(struct appctx *appctx);
9390
9391static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) {
9392 return cli_io_handler_tlskeys_files(appctx);
9393}
9394
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009395/* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1
9396 * (next index to be dumped), and cli.p0 (next key reference).
9397 */
William Lallemand32af2032016-10-29 18:09:35 +02009398static int cli_io_handler_tlskeys_files(struct appctx *appctx) {
9399
9400 struct stream_interface *si = appctx->owner;
9401
9402 switch (appctx->st2) {
9403 case STAT_ST_INIT:
9404 /* Display the column headers. If the message cannot be sent,
Joseph Herlant017b3da2018-11-15 09:07:59 -08009405 * quit the function with returning 0. The function is called
William Lallemand32af2032016-10-29 18:09:35 +02009406 * later and restart at the state "STAT_ST_INIT".
9407 */
9408 chunk_reset(&trash);
9409
9410 if (appctx->io_handler == cli_io_handler_tlskeys_entries)
9411 chunk_appendf(&trash, "# id secret\n");
9412 else
9413 chunk_appendf(&trash, "# id (file)\n");
9414
Willy Tarreau06d80a92017-10-19 14:32:15 +02009415 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01009416 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02009417 return 0;
9418 }
9419
William Lallemand32af2032016-10-29 18:09:35 +02009420 /* Now, we start the browsing of the references lists.
9421 * Note that the following call to LIST_ELEM return bad pointer. The only
9422 * available field of this pointer is <list>. It is used with the function
9423 * tlskeys_list_get_next() for retruning the first available entry
9424 */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009425 if (appctx->ctx.cli.p0 == NULL) {
9426 appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list);
9427 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +02009428 }
9429
9430 appctx->st2 = STAT_ST_LIST;
9431 /* fall through */
9432
9433 case STAT_ST_LIST:
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009434 while (appctx->ctx.cli.p0) {
9435 struct tls_keys_ref *ref = appctx->ctx.cli.p0;
William Lallemand32af2032016-10-29 18:09:35 +02009436
9437 chunk_reset(&trash);
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009438 if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0)
William Lallemand32af2032016-10-29 18:09:35 +02009439 chunk_appendf(&trash, "# ");
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009440
9441 if (appctx->ctx.cli.i1 == 0)
9442 chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename);
9443
William Lallemand32af2032016-10-29 18:09:35 +02009444 if (appctx->io_handler == cli_io_handler_tlskeys_entries) {
Christopher Faulet16f45c82018-02-16 11:23:49 +01009445 int head;
9446
9447 HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock);
9448 head = ref->tls_ticket_enc_index;
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009449 while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) {
Willy Tarreau83061a82018-07-13 11:56:34 +02009450 struct buffer *t2 = get_trash_chunk();
William Lallemand32af2032016-10-29 18:09:35 +02009451
9452 chunk_reset(t2);
9453 /* should never fail here because we dump only a key in the t2 buffer */
Emeric Brun9e754772019-01-10 17:51:55 +01009454 if (ref->key_size_bits == 128) {
9455 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
9456 sizeof(struct tls_sess_key_128),
9457 t2->area, t2->size);
9458 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
9459 t2->area);
9460 }
9461 else if (ref->key_size_bits == 256) {
9462 t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO),
9463 sizeof(struct tls_sess_key_256),
9464 t2->area, t2->size);
9465 chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1,
9466 t2->area);
9467 }
9468 else {
9469 /* This case should never happen */
9470 chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1);
9471 }
William Lallemand32af2032016-10-29 18:09:35 +02009472
Willy Tarreau06d80a92017-10-19 14:32:15 +02009473 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +02009474 /* let's try again later from this stream. We add ourselves into
9475 * this stream's users so that it can remove us upon termination.
9476 */
Christopher Faulet16f45c82018-02-16 11:23:49 +01009477 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreaudb398432018-11-15 11:08:52 +01009478 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02009479 return 0;
9480 }
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009481 appctx->ctx.cli.i1++;
William Lallemand32af2032016-10-29 18:09:35 +02009482 }
Christopher Faulet16f45c82018-02-16 11:23:49 +01009483 HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock);
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009484 appctx->ctx.cli.i1 = 0;
William Lallemand32af2032016-10-29 18:09:35 +02009485 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02009486 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand32af2032016-10-29 18:09:35 +02009487 /* let's try again later from this stream. We add ourselves into
9488 * this stream's users so that it can remove us upon termination.
9489 */
Willy Tarreaudb398432018-11-15 11:08:52 +01009490 si_rx_room_blk(si);
William Lallemand32af2032016-10-29 18:09:35 +02009491 return 0;
9492 }
9493
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009494 if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */
William Lallemand32af2032016-10-29 18:09:35 +02009495 break;
9496
9497 /* get next list entry and check the end of the list */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009498 appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference);
William Lallemand32af2032016-10-29 18:09:35 +02009499 }
9500
9501 appctx->st2 = STAT_ST_FIN;
9502 /* fall through */
9503
9504 default:
9505 appctx->st2 = STAT_ST_FIN;
9506 return 1;
9507 }
9508 return 0;
9509}
9510
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009511/* sets cli.i0 to non-zero if only file lists should be dumped */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02009512static int cli_parse_show_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +02009513{
William Lallemand32af2032016-10-29 18:09:35 +02009514 /* no parameter, shows only file list */
9515 if (!*args[2]) {
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009516 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +02009517 appctx->io_handler = cli_io_handler_tlskeys_files;
Willy Tarreau3067bfa2016-12-05 14:50:15 +01009518 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02009519 }
9520
9521 if (args[2][0] == '*') {
9522 /* list every TLS ticket keys */
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009523 appctx->ctx.cli.i0 = 1;
William Lallemand32af2032016-10-29 18:09:35 +02009524 } else {
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009525 appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]);
Willy Tarreau9d008692019-08-09 11:21:01 +02009526 if (!appctx->ctx.cli.p0)
9527 return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +02009528 }
William Lallemand32af2032016-10-29 18:09:35 +02009529 appctx->io_handler = cli_io_handler_tlskeys_entries;
Willy Tarreau3067bfa2016-12-05 14:50:15 +01009530 return 0;
William Lallemand32af2032016-10-29 18:09:35 +02009531}
9532
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02009533static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +02009534{
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009535 struct tls_keys_ref *ref;
Willy Tarreau1c913e42018-08-22 05:26:57 +02009536 int ret;
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009537
William Lallemand32af2032016-10-29 18:09:35 +02009538 /* Expect two parameters: the filename and the new new TLS key in encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +02009539 if (!*args[3] || !*args[4])
9540 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 +02009541
Willy Tarreauf5f26e82016-12-16 18:47:27 +01009542 ref = tlskeys_ref_lookup_ref(args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02009543 if (!ref)
9544 return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n");
William Lallemand32af2032016-10-29 18:09:35 +02009545
Willy Tarreau1c913e42018-08-22 05:26:57 +02009546 ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +02009547 if (ret < 0)
9548 return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n");
Emeric Brun9e754772019-01-10 17:51:55 +01009549
Willy Tarreau1c913e42018-08-22 05:26:57 +02009550 trash.data = ret;
Willy Tarreau9d008692019-08-09 11:21:01 +02009551 if (ssl_sock_update_tlskey_ref(ref, &trash) < 0)
9552 return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n");
William Lallemand32af2032016-10-29 18:09:35 +02009553
Willy Tarreau9d008692019-08-09 11:21:01 +02009554 return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +02009555}
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +01009556#endif
William Lallemand32af2032016-10-29 18:09:35 +02009557
William Lallemanda6ffd5b2020-03-09 13:35:19 +01009558/*
9559 * Take an ssl_bind_conf structure and append the configuration line used to
9560 * create it in the buffer
9561 */
9562static void dump_crtlist_sslconf(struct buffer *buf, const struct ssl_bind_conf *conf)
9563{
9564 int space = 0;
9565
9566 if (conf == NULL)
9567 return;
9568
9569 chunk_appendf(buf, " [");
9570#ifdef OPENSSL_NPN_NEGOTIATED
9571 if (conf->npn_str) {
9572 int len = conf->npn_len;
9573 char *ptr = conf->npn_str;
9574 int comma = 0;
9575
9576 if (space) chunk_appendf(buf, " ");
9577 chunk_appendf(buf, "npn ");
9578 while (len) {
9579 unsigned short size;
9580
9581 size = *ptr;
9582 ptr++;
9583 if (comma)
9584 chunk_memcat(buf, ",", 1);
9585 chunk_memcat(buf, ptr, size);
9586 ptr += size;
9587 len -= size + 1;
9588 comma = 1;
9589 }
9590 chunk_memcat(buf, "", 1); /* finish with a \0 */
9591 space++;
9592 }
9593#endif
9594#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
9595 if (conf->alpn_str) {
9596 int len = conf->alpn_len;
9597 char *ptr = conf->alpn_str;
9598 int comma = 0;
9599
9600 if (space) chunk_appendf(buf, " ");
9601 chunk_appendf(buf, "alpn ");
9602 while (len) {
9603 unsigned short size;
9604
9605 size = *ptr;
9606 ptr++;
9607 if (comma)
9608 chunk_memcat(buf, ",", 1);
9609 chunk_memcat(buf, ptr, size);
9610 ptr += size;
9611 len -= size + 1;
9612 comma = 1;
9613 }
9614 chunk_memcat(buf, "", 1); /* finish with a \0 */
9615 space++;
9616 }
9617#endif
9618 /* verify */
9619 {
9620 if (conf->verify == SSL_SOCK_VERIFY_NONE) {
9621 if (space) chunk_appendf(buf, " ");
9622 chunk_appendf(buf, "verify none");
9623 space++;
9624 } else if (conf->verify == SSL_SOCK_VERIFY_OPTIONAL) {
9625 if (space) chunk_appendf(buf, " ");
9626 chunk_appendf(buf, "verify optional");
9627 space++;
9628 } else if (conf->verify == SSL_SOCK_VERIFY_REQUIRED) {
9629 if (space) chunk_appendf(buf, " ");
9630 chunk_appendf(buf, "verify required");
9631 space++;
9632 }
9633 }
9634
9635 if (conf->no_ca_names) {
9636 if (space) chunk_appendf(buf, " ");
9637 chunk_appendf(buf, "no-ca-names");
9638 space++;
9639 }
9640
9641 if (conf->early_data) {
9642 if (space) chunk_appendf(buf, " ");
9643 chunk_appendf(buf, "allow-0rtt");
9644 space++;
9645 }
9646 if (conf->ca_file) {
9647 if (space) chunk_appendf(buf, " ");
9648 chunk_appendf(buf, "ca-file %s", conf->ca_file);
9649 space++;
9650 }
9651 if (conf->crl_file) {
9652 if (space) chunk_appendf(buf, " ");
9653 chunk_appendf(buf, "crl-file %s", conf->crl_file);
9654 space++;
9655 }
9656 if (conf->ciphers) {
9657 if (space) chunk_appendf(buf, " ");
9658 chunk_appendf(buf, "ciphers %s", conf->ciphers);
9659 space++;
9660 }
9661#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
9662 if (conf->ciphersuites) {
9663 if (space) chunk_appendf(buf, " ");
9664 chunk_appendf(buf, "ciphersuites %s", conf->ciphersuites);
9665 space++;
9666 }
9667#endif
9668 if (conf->curves) {
9669 if (space) chunk_appendf(buf, " ");
9670 chunk_appendf(buf, "curves %s", conf->curves);
9671 space++;
9672 }
9673 if (conf->ecdhe) {
9674 if (space) chunk_appendf(buf, " ");
9675 chunk_appendf(buf, "ecdhe %s", conf->ecdhe);
9676 space++;
9677 }
9678
9679 /* the crt-lists only support ssl-min-ver and ssl-max-ver */
9680 /* XXX: this part need to be revamp so we don't dump the default settings */
9681 if (conf->ssl_methods.min) {
9682 if (space) chunk_appendf(buf, " ");
9683 chunk_appendf(buf, "ssl-min-ver %s", methodVersions[conf->ssl_methods.min].name);
9684 space++;
9685 }
9686
9687 if (conf->ssl_methods.max) {
9688 if (space) chunk_appendf(buf, " ");
9689 chunk_appendf(buf, "ssl-max-ver %s", methodVersions[conf->ssl_methods.max].name);
9690 space++;
9691 }
9692
William Lallemand58a52222020-04-02 18:11:47 +02009693 chunk_appendf(buf, "]");
William Lallemanda6ffd5b2020-03-09 13:35:19 +01009694
9695 return;
9696}
9697
9698/* dump a list of filters */
9699static void dump_crtlist_filters(struct buffer *buf, struct crtlist_entry *entry)
9700{
William Lallemanda6ffd5b2020-03-09 13:35:19 +01009701 int i;
9702
9703 if (!entry->fcount)
9704 return;
9705
9706 for (i = 0; i < entry->fcount; i++) {
William Lallemand58a52222020-04-02 18:11:47 +02009707 chunk_appendf(buf, " %s", entry->filters[i]);
William Lallemanda6ffd5b2020-03-09 13:35:19 +01009708 }
9709 return;
9710}
9711
9712/* CLI IO handler for '(show|dump) ssl crt-list' */
9713static int cli_io_handler_dump_crtlist(struct appctx *appctx)
9714{
9715 struct buffer *trash = alloc_trash_chunk();
9716 struct stream_interface *si = appctx->owner;
9717 struct ebmb_node *lnode;
9718
9719 if (trash == NULL)
9720 return 1;
9721
9722 /* dump the list of crt-lists */
9723 lnode = appctx->ctx.cli.p1;
9724 if (lnode == NULL)
9725 lnode = ebmb_first(&crtlists_tree);
9726 while (lnode) {
9727 chunk_appendf(trash, "%s\n", lnode->key);
9728 if (ci_putchk(si_ic(si), trash) == -1) {
9729 si_rx_room_blk(si);
9730 goto yield;
9731 }
9732 lnode = ebmb_next(lnode);
9733 }
William Lallemand2ea1b492020-03-17 15:13:11 +01009734 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +01009735 return 1;
9736yield:
9737 appctx->ctx.cli.p1 = lnode;
William Lallemand2ea1b492020-03-17 15:13:11 +01009738 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +01009739 return 0;
9740}
9741
9742/* CLI IO handler for '(show|dump) ssl crt-list <filename>' */
9743static int cli_io_handler_dump_crtlist_entries(struct appctx *appctx)
9744{
9745 struct buffer *trash = alloc_trash_chunk();
9746 struct crtlist *crtlist;
9747 struct stream_interface *si = appctx->owner;
9748 struct crtlist_entry *entry;
9749
9750 if (trash == NULL)
9751 return 1;
9752
9753 crtlist = ebmb_entry(appctx->ctx.cli.p0, struct crtlist, node);
9754
9755 entry = appctx->ctx.cli.p1;
9756 if (entry == NULL) {
9757 entry = LIST_ELEM((crtlist->ord_entries).n, typeof(entry), by_crtlist);
9758 chunk_appendf(trash, "# %s\n", crtlist->node.key);
9759 if (ci_putchk(si_ic(si), trash) == -1) {
9760 si_rx_room_blk(si);
9761 goto yield;
9762 }
9763 }
9764
9765 list_for_each_entry_from(entry, &crtlist->ord_entries, by_crtlist) {
9766 struct ckch_store *store;
9767 const char *filename;
9768
9769 store = entry->node.key;
9770 filename = store->path;
William Lallemanda6ffd5b2020-03-09 13:35:19 +01009771 chunk_appendf(trash, "%s", filename);
William Lallemandc69f02d2020-04-06 19:07:03 +02009772 if (appctx->ctx.cli.i0 == 's') /* show */
9773 chunk_appendf(trash, ":%d", entry->linenum);
William Lallemanda6ffd5b2020-03-09 13:35:19 +01009774 dump_crtlist_sslconf(trash, entry->ssl_conf);
9775 dump_crtlist_filters(trash, entry);
9776 chunk_appendf(trash, "\n");
9777
9778 if (ci_putchk(si_ic(si), trash) == -1) {
9779 si_rx_room_blk(si);
9780 goto yield;
9781 }
9782 }
William Lallemand2ea1b492020-03-17 15:13:11 +01009783 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +01009784 return 1;
9785yield:
9786 appctx->ctx.cli.p1 = entry;
William Lallemand2ea1b492020-03-17 15:13:11 +01009787 free_trash_chunk(trash);
William Lallemanda6ffd5b2020-03-09 13:35:19 +01009788 return 0;
9789}
9790
9791/* CLI argument parser for '(show|dump) ssl crt-list' */
9792static int cli_parse_dump_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
9793{
9794 struct ebmb_node *lnode;
William Lallemandc69f02d2020-04-06 19:07:03 +02009795 char *filename = NULL;
William Lallemanda6ffd5b2020-03-09 13:35:19 +01009796 int mode;
9797
9798 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
9799 return 1;
9800
9801 appctx->ctx.cli.p0 = NULL;
9802 appctx->ctx.cli.p1 = NULL;
William Lallemanda6ffd5b2020-03-09 13:35:19 +01009803
William Lallemandc69f02d2020-04-06 19:07:03 +02009804 if (*args[3] && !strcmp(args[3], "-n")) {
9805 mode = 's';
9806 filename = args[4];
9807 } else {
9808 mode = 'd';
9809 filename = args[3];
9810 }
William Lallemanda6ffd5b2020-03-09 13:35:19 +01009811
William Lallemandc69f02d2020-04-06 19:07:03 +02009812 if (mode == 's' && !*args[4])
9813 return cli_err(appctx, "'show ssl crt-list -n' expects a filename or a directory\n");
9814
9815 if (filename && *filename) {
9816 lnode = ebst_lookup(&crtlists_tree, filename);
William Lallemanda6ffd5b2020-03-09 13:35:19 +01009817 if (lnode == NULL)
9818 return cli_err(appctx, "didn't find the specified filename\n");
9819
9820 appctx->ctx.cli.p0 = lnode;
9821 appctx->io_handler = cli_io_handler_dump_crtlist_entries;
9822 }
9823 appctx->ctx.cli.i0 = mode;
9824
9825 return 0;
9826}
William Lallemand44b35322019-10-17 16:28:40 +02009827
William Lallemande67c80b2020-03-25 14:42:37 +01009828/* release function of the "add ssl crt-list' command, free things and unlock
9829 the spinlock */
9830static void cli_release_add_crtlist(struct appctx *appctx)
9831{
9832 struct crtlist_entry *entry = appctx->ctx.cli.p1;
9833
9834 if (appctx->st2 != SETCERT_ST_FIN) {
9835 struct ckch_inst *inst, *inst_s;
9836 /* upon error free the ckch_inst and everything inside */
9837 ebpt_delete(&entry->node);
9838 LIST_DEL(&entry->by_crtlist);
9839 LIST_DEL(&entry->by_ckch_store);
9840
9841 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_ckchs) {
William Lallemandd9d5d1b2020-04-09 16:31:05 +02009842 ckch_inst_free(inst);
William Lallemande67c80b2020-03-25 14:42:37 +01009843 }
9844 crtlist_free_filters(entry->filters);
9845 ssl_sock_free_ssl_conf(entry->ssl_conf);
9846 free(entry->ssl_conf);
9847 free(entry);
9848 }
9849
9850 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
9851}
9852
9853
9854/* IO Handler for the "add ssl crt-list" command It adds a new entry in the
9855 * crt-list and generates the ckch_insts for each bind_conf that uses this crt-list
9856 *
9857 * The logic is the same as the "commit ssl cert" command but without the
9858 * freeing of the old structures, because there are none.
9859 */
9860static int cli_io_handler_add_crtlist(struct appctx *appctx)
9861{
9862 struct bind_conf_list *bind_conf_node;
9863 struct stream_interface *si = appctx->owner;
9864 struct crtlist *crtlist = appctx->ctx.cli.p0;
9865 struct crtlist_entry *entry = appctx->ctx.cli.p1;
9866 struct ckch_store *store = entry->node.key;
9867 struct buffer *trash = alloc_trash_chunk();
9868 struct ckch_inst *new_inst;
9869 char *err = NULL;
9870 int i = 0;
9871 int errcode = 0;
9872
9873 if (trash == NULL)
9874 goto error;
9875
9876 /* for each bind_conf which use the crt-list, a new ckch_inst must be
9877 * created.
9878 */
9879 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
9880 goto error;
9881
9882 while (1) {
9883 switch (appctx->st2) {
9884 case SETCERT_ST_INIT:
9885 /* This state just print the update message */
9886 chunk_printf(trash, "Inserting certificate '%s' in crt-list '%s'", store->path, crtlist->node.key);
9887 if (ci_putchk(si_ic(si), trash) == -1) {
9888 si_rx_room_blk(si);
9889 goto yield;
9890 }
9891 appctx->st2 = SETCERT_ST_GEN;
9892 /* fallthrough */
9893 case SETCERT_ST_GEN:
9894 bind_conf_node = appctx->ctx.cli.p2; /* get the previous ptr from the yield */
9895 if (bind_conf_node == NULL)
9896 bind_conf_node = crtlist->bind_conf;
9897 for (; bind_conf_node; bind_conf_node = bind_conf_node->next) {
9898 struct bind_conf *bind_conf = bind_conf_node->bind_conf;
9899 struct sni_ctx *sni;
9900
9901 /* yield every 10 generations */
9902 if (i > 10) {
9903 appctx->ctx.cli.p2 = bind_conf_node;
9904 goto yield;
9905 }
9906
9907 /* we don't support multi-cert bundles, only simple ones */
9908 errcode |= ckch_inst_new_load_store(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &new_inst, &err);
9909 if (errcode & ERR_CODE)
9910 goto error;
9911
9912 /* we need to initialize the SSL_CTX generated */
9913 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
9914 list_for_each_entry(sni, &new_inst->sni_ctx, by_ckch_inst) {
9915 if (!sni->order) { /* we initialized only the first SSL_CTX because it's the same in the other sni_ctx's */
9916 errcode |= ssl_sock_prepare_ctx(bind_conf, new_inst->ssl_conf, sni->ctx, &err);
9917 if (errcode & ERR_CODE)
9918 goto error;
9919 }
9920 }
9921 /* display one dot for each new instance */
9922 chunk_appendf(trash, ".");
9923 i++;
9924 LIST_ADDQ(&store->ckch_inst, &new_inst->by_ckchs);
9925 }
9926 appctx->st2 = SETCERT_ST_INSERT;
9927 /* fallthrough */
9928 case SETCERT_ST_INSERT:
9929 /* insert SNIs in bind_conf */
9930 list_for_each_entry(new_inst, &store->ckch_inst, by_ckchs) {
9931 HA_RWLOCK_WRLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock);
9932 ssl_sock_load_cert_sni(new_inst, new_inst->bind_conf);
9933 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &new_inst->bind_conf->sni_lock);
9934 }
William Lallemandc69f02d2020-04-06 19:07:03 +02009935 entry->linenum = ++crtlist->linecount;
William Lallemande67c80b2020-03-25 14:42:37 +01009936 appctx->st2 = SETCERT_ST_FIN;
9937 goto end;
9938 }
9939 }
9940
9941end:
9942 chunk_appendf(trash, "\n");
9943 if (errcode & ERR_WARN)
9944 chunk_appendf(trash, "%s", err);
9945 chunk_appendf(trash, "Success!\n");
9946 if (ci_putchk(si_ic(si), trash) == -1)
9947 si_rx_room_blk(si);
9948 free_trash_chunk(trash);
9949 /* success: call the release function and don't come back */
9950 return 1;
9951yield:
9952 /* store the state */
9953 if (ci_putchk(si_ic(si), trash) == -1)
9954 si_rx_room_blk(si);
9955 free_trash_chunk(trash);
9956 si_rx_endp_more(si); /* let's come back later */
9957 return 0; /* should come back */
9958
9959error:
9960 /* spin unlock and free are done in the release function */
9961 if (trash) {
9962 chunk_appendf(trash, "\n%sFailed!\n", err);
9963 if (ci_putchk(si_ic(si), trash) == -1)
9964 si_rx_room_blk(si);
9965 free_trash_chunk(trash);
9966 }
9967 /* error: call the release function and don't come back */
9968 return 1;
9969}
9970
9971
9972/*
9973 * Parse a "add ssl crt-list <crt-list> <certfile>" line.
William Lallemandc7c7a6b2020-04-01 17:32:46 +02009974 * Filters and option must be passed through payload:
William Lallemande67c80b2020-03-25 14:42:37 +01009975 */
9976static int cli_parse_add_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
9977{
William Lallemandc7c7a6b2020-04-01 17:32:46 +02009978 int cfgerr = 0;
William Lallemande67c80b2020-03-25 14:42:37 +01009979 struct ckch_store *store;
9980 char *err = NULL;
William Lallemandc7c7a6b2020-04-01 17:32:46 +02009981 char path[MAXPATHLEN+1];
9982 char *crtlist_path;
9983 char *cert_path = NULL;
William Lallemande67c80b2020-03-25 14:42:37 +01009984 struct ebmb_node *eb;
9985 struct ebpt_node *inserted;
9986 struct crtlist *crtlist;
William Lallemandc7c7a6b2020-04-01 17:32:46 +02009987 struct crtlist_entry *entry = NULL;
William Lallemande67c80b2020-03-25 14:42:37 +01009988
9989 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
9990 return 1;
9991
William Lallemandc7c7a6b2020-04-01 17:32:46 +02009992 if (!*args[3] || (!payload && !*args[4]))
William Lallemande67c80b2020-03-25 14:42:37 +01009993 return cli_err(appctx, "'add ssl crtlist' expects a filename and a certificate name\n");
9994
9995 crtlist_path = args[3];
William Lallemande67c80b2020-03-25 14:42:37 +01009996
9997 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
9998 return cli_err(appctx, "Operations on certificates are currently locked!\n");
9999
10000 eb = ebst_lookup(&crtlists_tree, crtlist_path);
10001 if (!eb) {
10002 memprintf(&err, "crt-list '%s' does not exist!", crtlist_path);
10003 goto error;
10004 }
10005 crtlist = ebmb_entry(eb, struct crtlist, node);
10006
William Lallemande718dfb2020-04-10 11:09:25 +020010007 entry = crtlist_entry_new();
William Lallemandc7c7a6b2020-04-01 17:32:46 +020010008 if (entry == NULL) {
10009 memprintf(&err, "Not enough memory!");
10010 goto error;
10011 }
William Lallemandc7c7a6b2020-04-01 17:32:46 +020010012
10013 if (payload) {
10014 char *lf;
10015
10016 lf = strrchr(payload, '\n');
10017 if (lf) {
10018 memprintf(&err, "only one line of payload is supported!");
10019 goto error;
10020 }
10021 /* cert_path is filled here */
10022 cfgerr |= crtlist_parse_line(payload, &cert_path, entry, "CLI", 1, &err);
10023 if (cfgerr & ERR_CODE)
10024 goto error;
10025 } else {
10026 cert_path = args[4];
10027 }
10028
10029 if (!cert_path) {
10030 memprintf(&err, "'add ssl crtlist' should contain the certificate name in the payload");
10031 cfgerr |= ERR_ALERT | ERR_FATAL;
10032 goto error;
10033 }
10034
William Lallemand916d0b52020-04-21 18:29:12 +020010035 if (eb_gettag(crtlist->entries.b[EB_RGHT])) {
10036 char *slash;
10037
10038 slash = strrchr(cert_path, '/');
10039 if (!slash) {
10040 memprintf(&err, "'%s' is a directory, certificate path '%s' must contain the directory path", (char *)crtlist->node.key, cert_path);
10041 goto error;
10042 }
10043 /* temporary replace / by 0 to do an strcmp */
10044 *slash = '\0';
10045 if (strcmp(cert_path, (char*)crtlist->node.key) != 0) {
10046 *slash = '/';
10047 memprintf(&err, "'%s' is a directory, certificate path '%s' must contain the directory path", (char *)crtlist->node.key, cert_path);
10048 goto error;
10049 }
10050 *slash = '/';
10051 }
10052
William Lallemandc7c7a6b2020-04-01 17:32:46 +020010053 if (*cert_path != '/' && global_ssl.crt_base) {
10054 if ((strlen(global_ssl.crt_base) + 1 + strlen(cert_path)) > MAXPATHLEN) {
10055 memprintf(&err, "'%s' : path too long", cert_path);
10056 cfgerr |= ERR_ALERT | ERR_FATAL;
10057 goto error;
10058 }
10059 snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, cert_path);
10060 cert_path = path;
10061 }
10062
William Lallemande67c80b2020-03-25 14:42:37 +010010063 store = ckchs_lookup(cert_path);
10064 if (store == NULL) {
10065 memprintf(&err, "certificate '%s' does not exist!", cert_path);
10066 goto error;
10067 }
William Lallemand36ccc392020-04-08 10:57:24 +020010068 if (store->multi) {
10069 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);
10070 goto error;
10071 }
William Lallemande67c80b2020-03-25 14:42:37 +010010072 if (store->ckch == NULL || store->ckch->cert == NULL) {
10073 memprintf(&err, "certificate '%s' is empty!", cert_path);
10074 goto error;
10075 }
10076
William Lallemande67c80b2020-03-25 14:42:37 +010010077 /* check if it's possible to insert this new crtlist_entry */
10078 entry->node.key = store;
10079 inserted = ebpt_insert(&crtlist->entries, &entry->node);
10080 if (inserted != &entry->node) {
10081 memprintf(&err, "file already exists in this directory!");
William Lallemande67c80b2020-03-25 14:42:37 +010010082 goto error;
10083 }
10084
William Lallemandb74d5642020-04-21 16:54:19 +020010085 /* this is supposed to be a directory (EB_ROOT_UNIQUE), so no ssl_conf are allowed */
10086 if ((entry->ssl_conf || entry->filters) && eb_gettag(crtlist->entries.b[EB_RGHT])) {
10087 memprintf(&err, "this is a directory, SSL configuration and filters are not allowed");
10088 goto error;
10089 }
10090
William Lallemande67c80b2020-03-25 14:42:37 +010010091 LIST_ADDQ(&crtlist->ord_entries, &entry->by_crtlist);
William Lallemande67c80b2020-03-25 14:42:37 +010010092 entry->crtlist = crtlist;
10093 LIST_ADDQ(&store->crtlist_entry, &entry->by_ckch_store);
10094
10095 appctx->st2 = SETCERT_ST_INIT;
10096 appctx->ctx.cli.p0 = crtlist;
10097 appctx->ctx.cli.p1 = entry;
10098
10099 /* unlock is done in the release handler */
10100 return 0;
10101
10102error:
William Lallemande718dfb2020-04-10 11:09:25 +020010103 crtlist_entry_free(entry);
William Lallemande67c80b2020-03-25 14:42:37 +010010104 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10105 err = memprintf(&err, "Can't edit the crt-list: %s\n", err ? err : "");
10106 return cli_dynerr(appctx, err);
10107}
10108
William Lallemand0a9b9412020-04-06 17:43:05 +020010109/* Parse a "del ssl crt-list <crt-list> <certfile>" line. */
10110static int cli_parse_del_crtlist(char **args, char *payload, struct appctx *appctx, void *private)
10111{
10112 struct ckch_store *store;
10113 char *err = NULL;
10114 char *crtlist_path, *cert_path;
10115 struct ebmb_node *ebmb;
10116 struct ebpt_node *ebpt;
10117 struct crtlist *crtlist;
10118 struct crtlist_entry *entry = NULL;
10119 struct ckch_inst *inst, *inst_s;
10120 int linenum = 0;
10121 char *colons;
10122
10123 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
10124 return 1;
10125
10126 if (!*args[3] || !*args[4])
10127 return cli_err(appctx, "'del ssl crtlist' expects a filename and a certificate name\n");
10128
William Lallemand463b5242020-04-08 10:30:44 +020010129 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10130 return cli_err(appctx, "Can't delete!\nOperations on certificates are currently locked!\n");
10131
William Lallemand0a9b9412020-04-06 17:43:05 +020010132 crtlist_path = args[3];
10133 cert_path = args[4];
10134
10135 colons = strchr(cert_path, ':');
10136 if (colons) {
10137 char *endptr;
10138
10139 linenum = strtol(colons + 1, &endptr, 10);
10140 if (colons + 1 == endptr || *endptr != '\0') {
10141 memprintf(&err, "wrong line number after colons in '%s'!", cert_path);
10142 goto error;
10143 }
10144 *colons = '\0';
10145 }
10146 /* look for crtlist */
10147 ebmb = ebst_lookup(&crtlists_tree, crtlist_path);
10148 if (!ebmb) {
10149 memprintf(&err, "crt-list '%s' does not exist!", crtlist_path);
10150 goto error;
10151 }
10152 crtlist = ebmb_entry(ebmb, struct crtlist, node);
10153
10154 /* look for store */
10155 store = ckchs_lookup(cert_path);
10156 if (store == NULL) {
10157 memprintf(&err, "certificate '%s' does not exist!", cert_path);
10158 goto error;
10159 }
William Lallemand36ccc392020-04-08 10:57:24 +020010160 if (store->multi) {
10161 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);
10162 goto error;
10163 }
William Lallemand0a9b9412020-04-06 17:43:05 +020010164 if (store->ckch == NULL || store->ckch->cert == NULL) {
10165 memprintf(&err, "certificate '%s' is empty!", cert_path);
10166 goto error;
10167 }
10168
10169 ebpt = ebpt_lookup(&crtlist->entries, store);
10170 if (!ebpt) {
10171 memprintf(&err, "certificate '%s' can't be found in crt-list '%s'!", cert_path, crtlist_path);
10172 goto error;
10173 }
10174
10175 /* list the line number of entries for errors in err, and select the right ebpt */
10176 for (; ebpt; ebpt = ebpt_next_dup(ebpt)) {
10177 struct crtlist_entry *tmp;
10178
10179 tmp = ebpt_entry(ebpt, struct crtlist_entry, node);
10180 memprintf(&err, "%s%s%d", err ? err : "", err ? ", " : "", tmp->linenum);
10181
10182 /* select the entry we wanted */
10183 if (linenum == 0 || tmp->linenum == linenum) {
10184 if (!entry)
10185 entry = tmp;
10186 }
10187 }
10188
10189 /* we didn't found the specified entry */
10190 if (!entry) {
10191 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);
10192 goto error;
10193 }
10194
10195 /* we didn't specified a line number but there were several entries */
10196 if (linenum == 0 && ebpt_next_dup(&entry->node)) {
10197 memprintf(&err, "found the certificate '%s' in several entries, please specify a line number preceded by colons (%s)!", cert_path, err ? err : NULL);
10198 goto error;
10199 }
10200
10201 /* upon error free the ckch_inst and everything inside */
10202
10203 ebpt_delete(&entry->node);
10204 LIST_DEL(&entry->by_crtlist);
10205 LIST_DEL(&entry->by_ckch_store);
10206
10207 list_for_each_entry_safe(inst, inst_s, &entry->ckch_inst, by_crtlist_entry) {
10208 struct sni_ctx *sni, *sni_s;
10209
10210 HA_RWLOCK_WRLOCK(SNI_LOCK, &inst->bind_conf->sni_lock);
10211 list_for_each_entry_safe(sni, sni_s, &inst->sni_ctx, by_ckch_inst) {
10212 ebmb_delete(&sni->name);
10213 LIST_DEL(&sni->by_ckch_inst);
William Lallemand02e19a52020-04-08 16:11:26 +020010214 SSL_CTX_free(sni->ctx);
William Lallemand0a9b9412020-04-06 17:43:05 +020010215 free(sni);
10216 }
10217 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &inst->bind_conf->sni_lock);
10218 LIST_DEL(&inst->by_ckchs);
10219 free(inst);
10220 }
10221
10222 crtlist_free_filters(entry->filters);
10223 ssl_sock_free_ssl_conf(entry->ssl_conf);
10224 free(entry->ssl_conf);
10225 free(entry);
10226
10227 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10228 err = memprintf(&err, "Entry '%s' deleted in crtlist '%s'!\n", cert_path, crtlist_path);
10229 return cli_dynmsg(appctx, LOG_NOTICE, err);
10230
10231error:
10232 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10233 err = memprintf(&err, "Can't delete the entry: %s\n", err ? err : "");
10234 return cli_dynerr(appctx, err);
10235}
William Lallemande67c80b2020-03-25 14:42:37 +010010236
William Lallemand44b35322019-10-17 16:28:40 +020010237/* Type of SSL payloads that can be updated over the CLI */
10238
10239enum {
10240 CERT_TYPE_PEM = 0,
William Lallemand4c5adbf2020-02-24 14:23:22 +010010241 CERT_TYPE_KEY,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010242#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010243 CERT_TYPE_OCSP,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010244#endif
William Lallemand44b35322019-10-17 16:28:40 +020010245 CERT_TYPE_ISSUER,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010246#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010247 CERT_TYPE_SCTL,
Emmanuel Hocdetf6ac4fa2019-10-30 17:41:27 +010010248#endif
William Lallemand44b35322019-10-17 16:28:40 +020010249 CERT_TYPE_MAX,
10250};
10251
10252struct {
10253 const char *ext;
10254 int type;
10255 int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err);
10256 /* add a parsing callback */
William Lallemandf29cdef2019-10-23 15:00:52 +020010257} cert_exts[CERT_TYPE_MAX+1] = {
William Lallemand44b35322019-10-17 16:28:40 +020010258 [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */
William Lallemand4c5adbf2020-02-24 14:23:22 +010010259 [CERT_TYPE_KEY] = { "key", CERT_TYPE_KEY, &ssl_sock_load_key_into_ckch },
William Lallemand541a5342019-10-23 14:11:54 +020010260#if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010261 [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020010262#endif
10263#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
William Lallemand44b35322019-10-17 16:28:40 +020010264 [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file },
William Lallemand541a5342019-10-23 14:11:54 +020010265#endif
William Lallemand44b35322019-10-17 16:28:40 +020010266 [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch },
William Lallemandf29cdef2019-10-23 15:00:52 +020010267 [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL },
William Lallemand44b35322019-10-17 16:28:40 +020010268};
10269
William Lallemand8f840d72019-10-23 10:53:05 +020010270
William Lallemandd4f946c2019-12-05 10:26:40 +010010271/* release function of the `show ssl cert' command */
10272static void cli_release_show_cert(struct appctx *appctx)
10273{
10274 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10275}
10276
10277/* IO handler of "show ssl cert <filename>" */
10278static int cli_io_handler_show_cert(struct appctx *appctx)
10279{
10280 struct buffer *trash = alloc_trash_chunk();
10281 struct ebmb_node *node;
10282 struct stream_interface *si = appctx->owner;
10283 struct ckch_store *ckchs;
William Lallemandd4f946c2019-12-05 10:26:40 +010010284
10285 if (trash == NULL)
10286 return 1;
10287
10288 if (!appctx->ctx.ssl.old_ckchs) {
10289 if (ckchs_transaction.old_ckchs) {
10290 ckchs = ckchs_transaction.old_ckchs;
10291 chunk_appendf(trash, "# transaction\n");
10292 if (!ckchs->multi) {
10293 chunk_appendf(trash, "*%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010010294#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010010295 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010010296 int n;
10297
William Lallemandd4f946c2019-12-05 10:26:40 +010010298 chunk_appendf(trash, "*%s:", ckchs->path);
10299 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
10300 if (ckchs->ckch[n].cert)
10301 chunk_appendf(trash, " %s.%s\n", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
10302 }
10303 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010010304#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010010305 }
10306 }
10307 }
10308
10309 if (!appctx->ctx.cli.p0) {
10310 chunk_appendf(trash, "# filename\n");
10311 node = ebmb_first(&ckchs_tree);
10312 } else {
10313 node = &((struct ckch_store *)appctx->ctx.cli.p0)->node;
10314 }
10315 while (node) {
10316 ckchs = ebmb_entry(node, struct ckch_store, node);
10317 if (!ckchs->multi) {
10318 chunk_appendf(trash, "%s\n", ckchs->path);
William Lallemandba22e902019-12-18 20:36:01 +010010319#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandd4f946c2019-12-05 10:26:40 +010010320 } else {
William Lallemanda25a19f2020-01-29 00:04:24 +010010321 int n;
10322
William Lallemandd4f946c2019-12-05 10:26:40 +010010323 chunk_appendf(trash, "%s:", ckchs->path);
10324 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
10325 if (ckchs->ckch[n].cert)
10326 chunk_appendf(trash, " %s.%s", ckchs->path, SSL_SOCK_KEYTYPE_NAMES[n]);
10327 }
10328 chunk_appendf(trash, "\n");
William Lallemandba22e902019-12-18 20:36:01 +010010329#endif
William Lallemandd4f946c2019-12-05 10:26:40 +010010330 }
10331
10332 node = ebmb_next(node);
10333 if (ci_putchk(si_ic(si), trash) == -1) {
10334 si_rx_room_blk(si);
10335 goto yield;
10336 }
10337 }
10338
10339 appctx->ctx.cli.p0 = NULL;
10340 free_trash_chunk(trash);
10341 return 1;
10342yield:
10343
10344 free_trash_chunk(trash);
10345 appctx->ctx.cli.p0 = ckchs;
10346 return 0; /* should come back */
10347}
10348
10349/* IO handler of the details "show ssl cert <filename>" */
10350static int cli_io_handler_show_cert_detail(struct appctx *appctx)
10351{
10352 struct stream_interface *si = appctx->owner;
10353 struct ckch_store *ckchs = appctx->ctx.cli.p0;
10354 struct buffer *out = alloc_trash_chunk();
10355 struct buffer *tmp = alloc_trash_chunk();
10356 X509_NAME *name = NULL;
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010010357 STACK_OF(X509) *chain;
Willy Tarreau105599c2020-02-25 08:59:23 +010010358 unsigned int len = 0;
William Lallemandd4f946c2019-12-05 10:26:40 +010010359 int write = -1;
10360 BIO *bio = NULL;
William Lallemand35f4a9d2020-02-25 11:56:32 +010010361 int i;
William Lallemandd4f946c2019-12-05 10:26:40 +010010362
10363 if (!tmp || !out)
William Lallemand18eeb8e2020-03-20 14:42:36 +010010364 goto end_no_putchk;
William Lallemandd4f946c2019-12-05 10:26:40 +010010365
10366 if (!ckchs->multi) {
10367 chunk_appendf(out, "Filename: ");
10368 if (ckchs == ckchs_transaction.new_ckchs)
10369 chunk_appendf(out, "*");
10370 chunk_appendf(out, "%s\n", ckchs->path);
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010010371
William Lallemand59c16fc2020-03-19 20:26:02 +010010372 chunk_appendf(out, "Status: ");
10373 if (ckchs->ckch->cert == NULL)
10374 chunk_appendf(out, "Empty\n");
10375 else if (LIST_ISEMPTY(&ckchs->ckch_inst))
10376 chunk_appendf(out, "Unused\n");
10377 else
10378 chunk_appendf(out, "Used\n");
10379
William Lallemandea987ed2020-03-19 16:48:33 +010010380 if (ckchs->ckch->cert == NULL)
10381 goto end;
10382
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010010383 chain = ckchs->ckch->chain;
10384 if (chain == NULL) {
10385 struct issuer_chain *issuer;
Emmanuel Hocdetef87e0a2020-03-23 11:29:11 +010010386 issuer = ssl_get0_issuer_chain(ckchs->ckch->cert);
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010010387 if (issuer) {
10388 chain = issuer->chain;
10389 chunk_appendf(out, "Chain Filename: ");
10390 chunk_appendf(out, "%s\n", issuer->path);
10391 }
10392 }
William Lallemandd4f946c2019-12-05 10:26:40 +010010393 chunk_appendf(out, "Serial: ");
10394 if (ssl_sock_get_serial(ckchs->ckch->cert, tmp) == -1)
10395 goto end;
10396 dump_binary(out, tmp->area, tmp->data);
10397 chunk_appendf(out, "\n");
10398
10399 chunk_appendf(out, "notBefore: ");
10400 chunk_reset(tmp);
10401 if ((bio = BIO_new(BIO_s_mem())) == NULL)
10402 goto end;
10403 if (ASN1_TIME_print(bio, X509_getm_notBefore(ckchs->ckch->cert)) == 0)
10404 goto end;
10405 write = BIO_read(bio, tmp->area, tmp->size-1);
10406 tmp->area[write] = '\0';
10407 BIO_free(bio);
William Lallemand67b991d2020-03-20 14:10:17 +010010408 bio = NULL;
William Lallemandd4f946c2019-12-05 10:26:40 +010010409 chunk_appendf(out, "%s\n", tmp->area);
10410
10411 chunk_appendf(out, "notAfter: ");
10412 chunk_reset(tmp);
10413 if ((bio = BIO_new(BIO_s_mem())) == NULL)
10414 goto end;
10415 if (ASN1_TIME_print(bio, X509_getm_notAfter(ckchs->ckch->cert)) == 0)
10416 goto end;
10417 if ((write = BIO_read(bio, tmp->area, tmp->size-1)) <= 0)
10418 goto end;
10419 tmp->area[write] = '\0';
10420 BIO_free(bio);
William Lallemand67b991d2020-03-20 14:10:17 +010010421 bio = NULL;
William Lallemandd4f946c2019-12-05 10:26:40 +010010422 chunk_appendf(out, "%s\n", tmp->area);
10423
William Lallemandd4f946c2019-12-05 10:26:40 +010010424#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
10425 chunk_appendf(out, "Subject Alternative Name: ");
10426 if (ssl_sock_get_san_oneline(ckchs->ckch->cert, out) == -1)
10427 goto end;
10428 *(out->area + out->data) = '\0';
10429 chunk_appendf(out, "\n");
10430#endif
10431 chunk_reset(tmp);
10432 chunk_appendf(out, "Algorithm: ");
10433 if (cert_get_pkey_algo(ckchs->ckch->cert, tmp) == 0)
10434 goto end;
10435 chunk_appendf(out, "%s\n", tmp->area);
10436
10437 chunk_reset(tmp);
10438 chunk_appendf(out, "SHA1 FingerPrint: ");
Willy Tarreau105599c2020-02-25 08:59:23 +010010439 if (X509_digest(ckchs->ckch->cert, EVP_sha1(), (unsigned char *) tmp->area, &len) == 0)
William Lallemandd4f946c2019-12-05 10:26:40 +010010440 goto end;
Willy Tarreau105599c2020-02-25 08:59:23 +010010441 tmp->data = len;
William Lallemandd4f946c2019-12-05 10:26:40 +010010442 dump_binary(out, tmp->area, tmp->data);
10443 chunk_appendf(out, "\n");
William Lallemand35f4a9d2020-02-25 11:56:32 +010010444
William Lallemanda90e5932020-02-25 14:07:58 +010010445 chunk_appendf(out, "Subject: ");
10446 if ((name = X509_get_subject_name(ckchs->ckch->cert)) == NULL)
10447 goto end;
10448 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
10449 goto end;
10450 *(tmp->area + tmp->data) = '\0';
10451 chunk_appendf(out, "%s\n", tmp->area);
10452
10453 chunk_appendf(out, "Issuer: ");
10454 if ((name = X509_get_issuer_name(ckchs->ckch->cert)) == NULL)
10455 goto end;
10456 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
10457 goto end;
10458 *(tmp->area + tmp->data) = '\0';
10459 chunk_appendf(out, "%s\n", tmp->area);
10460
William Lallemand35f4a9d2020-02-25 11:56:32 +010010461 /* Displays subject of each certificate in the chain */
Emmanuel Hocdetcf8cf6c2020-02-18 16:06:14 +010010462 for (i = 0; i < sk_X509_num(chain); i++) {
10463 X509 *ca = sk_X509_value(chain, i);
William Lallemand35f4a9d2020-02-25 11:56:32 +010010464
William Lallemandbb7288a2020-02-25 14:04:33 +010010465 chunk_appendf(out, "Chain Subject: ");
William Lallemand35f4a9d2020-02-25 11:56:32 +010010466 if ((name = X509_get_subject_name(ca)) == NULL)
10467 goto end;
10468 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
10469 goto end;
10470 *(tmp->area + tmp->data) = '\0';
10471 chunk_appendf(out, "%s\n", tmp->area);
10472
William Lallemandbb7288a2020-02-25 14:04:33 +010010473 chunk_appendf(out, "Chain Issuer: ");
10474 if ((name = X509_get_issuer_name(ca)) == NULL)
10475 goto end;
10476 if ((ssl_sock_get_dn_oneline(name, tmp)) == -1)
10477 goto end;
10478 *(tmp->area + tmp->data) = '\0';
10479 chunk_appendf(out, "%s\n", tmp->area);
William Lallemand35f4a9d2020-02-25 11:56:32 +010010480 }
William Lallemandd4f946c2019-12-05 10:26:40 +010010481 }
10482
William Lallemandea987ed2020-03-19 16:48:33 +010010483end:
William Lallemandd4f946c2019-12-05 10:26:40 +010010484 if (ci_putchk(si_ic(si), out) == -1) {
10485 si_rx_room_blk(si);
10486 goto yield;
10487 }
10488
William Lallemand18eeb8e2020-03-20 14:42:36 +010010489end_no_putchk:
William Lallemand67b991d2020-03-20 14:10:17 +010010490 if (bio)
10491 BIO_free(bio);
William Lallemandd4f946c2019-12-05 10:26:40 +010010492 free_trash_chunk(tmp);
10493 free_trash_chunk(out);
10494 return 1;
10495yield:
10496 free_trash_chunk(tmp);
10497 free_trash_chunk(out);
10498 return 0; /* should come back */
10499}
10500
10501/* parsing function for 'show ssl cert [certfile]' */
10502static int cli_parse_show_cert(char **args, char *payload, struct appctx *appctx, void *private)
10503{
10504 struct ckch_store *ckchs;
10505
10506 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
10507 return cli_err(appctx, "Can't allocate memory!\n");
10508
10509 /* The operations on the CKCH architecture are locked so we can
10510 * manipulate ckch_store and ckch_inst */
10511 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10512 return cli_err(appctx, "Can't show!\nOperations on certificates are currently locked!\n");
10513
10514 /* check if there is a certificate to lookup */
10515 if (*args[3]) {
10516 if (*args[3] == '*') {
10517 if (!ckchs_transaction.new_ckchs)
10518 goto error;
10519
10520 ckchs = ckchs_transaction.new_ckchs;
10521
10522 if (strcmp(args[3] + 1, ckchs->path))
10523 goto error;
10524
10525 } else {
10526 if ((ckchs = ckchs_lookup(args[3])) == NULL)
10527 goto error;
10528
10529 }
10530
10531 if (ckchs->multi)
10532 goto error;
10533
10534 appctx->ctx.cli.p0 = ckchs;
10535 /* use the IO handler that shows details */
10536 appctx->io_handler = cli_io_handler_show_cert_detail;
10537 }
10538
10539 return 0;
10540
10541error:
10542 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10543 return cli_err(appctx, "Can't display the certificate: Not found or the certificate is a bundle!\n");
10544}
10545
William Lallemand430413e2019-10-28 14:30:47 +010010546/* release function of the `set ssl cert' command, free things and unlock the spinlock */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010547static void cli_release_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020010548{
10549 struct ckch_store *new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010550
William Lallemand430413e2019-10-28 14:30:47 +010010551 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand8f840d72019-10-23 10:53:05 +020010552
William Lallemand430413e2019-10-28 14:30:47 +010010553 if (appctx->st2 != SETCERT_ST_FIN) {
William Lallemand8f840d72019-10-23 10:53:05 +020010554 /* 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 +010010555 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010556
William Lallemandbeea2a42019-10-30 17:45:33 +010010557 /* if the allocation failed, we need to free everything from the temporary list */
William Lallemandba1c33f2020-04-08 17:55:45 +020010558 ckch_store_free(new_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020010559 }
10560}
10561
10562
10563/*
10564 * This function tries to create the new ckch_inst and their SNIs
10565 */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010566static int cli_io_handler_commit_cert(struct appctx *appctx)
William Lallemand8f840d72019-10-23 10:53:05 +020010567{
10568 struct stream_interface *si = appctx->owner;
10569 int y = 0;
10570 char *err = NULL;
10571 int errcode = 0;
10572 struct ckch_store *old_ckchs, *new_ckchs = NULL;
10573 struct ckch_inst *ckchi, *ckchis;
William Lallemand8f840d72019-10-23 10:53:05 +020010574 struct buffer *trash = alloc_trash_chunk();
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010575 struct sni_ctx *sc0, *sc0s;
William Lallemand90afe902020-03-30 19:29:45 +020010576 struct crtlist_entry *entry;
William Lallemand8f840d72019-10-23 10:53:05 +020010577
William Lallemand33cc76f2019-10-31 11:43:45 +010010578 if (trash == NULL)
10579 goto error;
10580
William Lallemand8f840d72019-10-23 10:53:05 +020010581 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
10582 goto error;
10583
William Lallemand430413e2019-10-28 14:30:47 +010010584 while (1) {
10585 switch (appctx->st2) {
10586 case SETCERT_ST_INIT:
10587 /* This state just print the update message */
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010588 chunk_printf(trash, "Committing %s", ckchs_transaction.path);
William Lallemand430413e2019-10-28 14:30:47 +010010589 if (ci_putchk(si_ic(si), trash) == -1) {
10590 si_rx_room_blk(si);
William Lallemand8f840d72019-10-23 10:53:05 +020010591 goto yield;
William Lallemand430413e2019-10-28 14:30:47 +010010592 }
10593 appctx->st2 = SETCERT_ST_GEN;
10594 /* fallthrough */
10595 case SETCERT_ST_GEN:
10596 /*
10597 * This state generates the ckch instances with their
10598 * sni_ctxs and SSL_CTX.
10599 *
William Lallemand430413e2019-10-28 14:30:47 +010010600 * Since the SSL_CTX generation can be CPU consumer, we
10601 * yield every 10 instances.
10602 */
William Lallemand8f840d72019-10-23 10:53:05 +020010603
William Lallemandbeea2a42019-10-30 17:45:33 +010010604 old_ckchs = appctx->ctx.ssl.old_ckchs;
10605 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010606
William Lallemandbeea2a42019-10-30 17:45:33 +010010607 if (!new_ckchs)
10608 continue;
William Lallemand8f840d72019-10-23 10:53:05 +020010609
William Lallemandbeea2a42019-10-30 17:45:33 +010010610 /* get the next ckchi to regenerate */
10611 ckchi = appctx->ctx.ssl.next_ckchi;
10612 /* we didn't start yet, set it to the first elem */
10613 if (ckchi == NULL)
10614 ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs);
William Lallemand8f840d72019-10-23 10:53:05 +020010615
William Lallemandbeea2a42019-10-30 17:45:33 +010010616 /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */
10617 list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) {
10618 struct ckch_inst *new_inst;
William Lallemand38df1c82019-12-04 15:39:35 +010010619 char **sni_filter = NULL;
10620 int fcount = 0;
William Lallemand8f840d72019-10-23 10:53:05 +020010621
William Lallemandbeea2a42019-10-30 17:45:33 +010010622 /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */
10623 if (y >= 10) {
10624 /* save the next ckchi to compute */
10625 appctx->ctx.ssl.next_ckchi = ckchi;
10626 goto yield;
10627 }
William Lallemandcaa16192020-04-08 16:29:15 +020010628
10629 if (ckchi->crtlist_entry) {
10630 sni_filter = ckchi->crtlist_entry->filters;
10631 fcount = ckchi->crtlist_entry->fcount;
William Lallemand67630162020-03-09 16:56:39 +010010632 }
William Lallemand38df1c82019-12-04 15:39:35 +010010633
William Lallemandbeea2a42019-10-30 17:45:33 +010010634 if (new_ckchs->multi)
William Lallemand38df1c82019-12-04 15:39:35 +010010635 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 +010010636 else
William Lallemand38df1c82019-12-04 15:39:35 +010010637 errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, sni_filter, fcount, &new_inst, &err);
10638
William Lallemandbeea2a42019-10-30 17:45:33 +010010639 if (errcode & ERR_CODE)
10640 goto error;
William Lallemand8f840d72019-10-23 10:53:05 +020010641
William Lallemand21724f02019-11-04 17:56:13 +010010642 /* if the previous ckchi was used as the default */
10643 if (ckchi->is_default)
10644 new_inst->is_default = 1;
10645
William Lallemand8ef0c2a2019-11-21 16:30:34 +010010646 /* we need to initialize the SSL_CTX generated */
William Lallemand696f3172020-02-07 20:45:24 +010010647 /* this iterate on the newly generated SNIs in the new instance to prepare their SSL_CTX */
10648 list_for_each_entry_safe(sc0, sc0s, &new_inst->sni_ctx, by_ckch_inst) {
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050010649 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 +010010650 errcode |= ssl_sock_prepare_ctx(ckchi->bind_conf, ckchi->ssl_conf, sc0->ctx, &err);
10651 if (errcode & ERR_CODE)
10652 goto error;
10653 }
10654 }
10655
10656
William Lallemandbeea2a42019-10-30 17:45:33 +010010657 /* display one dot per new instance */
10658 chunk_appendf(trash, ".");
10659 /* link the new ckch_inst to the duplicate */
10660 LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs);
10661 y++;
10662 }
William Lallemand430413e2019-10-28 14:30:47 +010010663 appctx->st2 = SETCERT_ST_INSERT;
10664 /* fallthrough */
10665 case SETCERT_ST_INSERT:
10666 /* The generation is finished, we can insert everything */
William Lallemand8f840d72019-10-23 10:53:05 +020010667
William Lallemandbeea2a42019-10-30 17:45:33 +010010668 old_ckchs = appctx->ctx.ssl.old_ckchs;
10669 new_ckchs = appctx->ctx.ssl.new_ckchs;
William Lallemand8f840d72019-10-23 10:53:05 +020010670
William Lallemandbeea2a42019-10-30 17:45:33 +010010671 if (!new_ckchs)
10672 continue;
William Lallemand430413e2019-10-28 14:30:47 +010010673
William Lallemand90afe902020-03-30 19:29:45 +020010674 /* get the list of crtlist_entry in the old store, and update the pointers to the store */
10675 LIST_SPLICE(&new_ckchs->crtlist_entry, &old_ckchs->crtlist_entry);
10676 list_for_each_entry(entry, &new_ckchs->crtlist_entry, by_ckch_store) {
10677 ebpt_delete(&entry->node);
10678 /* change the ptr and reinsert the node */
10679 entry->node.key = new_ckchs;
10680 ebpt_insert(&entry->crtlist->entries, &entry->node);
10681 }
10682
William Lallemand21724f02019-11-04 17:56:13 +010010683 /* First, we insert every new SNIs in the trees, also replace the default_ctx */
William Lallemandbeea2a42019-10-30 17:45:33 +010010684 list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) {
10685 HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10686 ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf);
10687 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock);
10688 }
William Lallemand8f840d72019-10-23 10:53:05 +020010689
William Lallemandbeea2a42019-10-30 17:45:33 +010010690 /* delete the old sni_ctx, the old ckch_insts and the ckch_store */
10691 list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) {
Willy Tarreaua6cd0782020-05-01 11:38:39 +020010692 struct bind_conf __maybe_unused *bind_conf = ckchi->bind_conf;
William Lallemandd5e93772020-04-09 17:12:16 +020010693
10694 HA_RWLOCK_WRLOCK(SNI_LOCK, &bind_conf->sni_lock);
William Lallemandd9d5d1b2020-04-09 16:31:05 +020010695 ckch_inst_free(ckchi);
William Lallemandd5e93772020-04-09 17:12:16 +020010696 HA_RWLOCK_WRUNLOCK(SNI_LOCK, &bind_conf->sni_lock);
William Lallemandbeea2a42019-10-30 17:45:33 +010010697 }
William Lallemand8f840d72019-10-23 10:53:05 +020010698
William Lallemandbeea2a42019-10-30 17:45:33 +010010699 /* Replace the old ckchs by the new one */
William Lallemandba1c33f2020-04-08 17:55:45 +020010700 ckch_store_free(old_ckchs);
William Lallemandbeea2a42019-10-30 17:45:33 +010010701 ebst_insert(&ckchs_tree, &new_ckchs->node);
William Lallemand430413e2019-10-28 14:30:47 +010010702 appctx->st2 = SETCERT_ST_FIN;
10703 /* fallthrough */
10704 case SETCERT_ST_FIN:
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010705 /* we achieved the transaction, we can set everything to NULL */
10706 free(ckchs_transaction.path);
10707 ckchs_transaction.path = NULL;
10708 ckchs_transaction.new_ckchs = NULL;
10709 ckchs_transaction.old_ckchs = NULL;
William Lallemand430413e2019-10-28 14:30:47 +010010710 goto end;
10711 }
William Lallemand8f840d72019-10-23 10:53:05 +020010712 }
William Lallemand430413e2019-10-28 14:30:47 +010010713end:
William Lallemand8f840d72019-10-23 10:53:05 +020010714
William Lallemanded442432019-11-21 16:41:07 +010010715 chunk_appendf(trash, "\n");
10716 if (errcode & ERR_WARN)
Tim Duesterhusc0e820c2019-11-23 23:52:30 +010010717 chunk_appendf(trash, "%s", err);
William Lallemanded442432019-11-21 16:41:07 +010010718 chunk_appendf(trash, "Success!\n");
William Lallemand430413e2019-10-28 14:30:47 +010010719 if (ci_putchk(si_ic(si), trash) == -1)
10720 si_rx_room_blk(si);
10721 free_trash_chunk(trash);
10722 /* success: call the release function and don't come back */
10723 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020010724yield:
10725 /* store the state */
10726 if (ci_putchk(si_ic(si), trash) == -1)
10727 si_rx_room_blk(si);
10728 free_trash_chunk(trash);
10729 si_rx_endp_more(si); /* let's come back later */
William Lallemand8f840d72019-10-23 10:53:05 +020010730 return 0; /* should come back */
10731
10732error:
10733 /* spin unlock and free are done in the release function */
William Lallemand33cc76f2019-10-31 11:43:45 +010010734 if (trash) {
10735 chunk_appendf(trash, "\n%sFailed!\n", err);
10736 if (ci_putchk(si_ic(si), trash) == -1)
10737 si_rx_room_blk(si);
10738 free_trash_chunk(trash);
10739 }
William Lallemand430413e2019-10-28 14:30:47 +010010740 /* error: call the release function and don't come back */
10741 return 1;
William Lallemand8f840d72019-10-23 10:53:05 +020010742}
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010743
10744/*
10745 * Parsing function of 'commit ssl cert'
10746 */
10747static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private)
10748{
10749 char *err = NULL;
10750
William Lallemand230662a2019-12-03 13:32:54 +010010751 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
10752 return 1;
10753
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010754 if (!*args[3])
10755 return cli_err(appctx, "'commit ssl cert expects a filename\n");
10756
10757 /* The operations on the CKCH architecture are locked so we can
10758 * manipulate ckch_store and ckch_inst */
10759 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10760 return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n");
10761
10762 if (!ckchs_transaction.path) {
10763 memprintf(&err, "No ongoing transaction! !\n");
10764 goto error;
10765 }
10766
10767 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
10768 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]);
10769 goto error;
10770 }
10771
William Lallemand4c5adbf2020-02-24 14:23:22 +010010772#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
10773 if (ckchs_transaction.new_ckchs->multi) {
10774 int n;
10775
10776 for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) {
10777 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)) {
10778 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
10779 goto error;
10780 }
10781 }
10782 } else
10783#endif
10784 {
10785 if (!X509_check_private_key(ckchs_transaction.new_ckchs->ckch->cert, ckchs_transaction.new_ckchs->ckch->key)) {
10786 memprintf(&err, "inconsistencies between private key and certificate loaded '%s'.\n", ckchs_transaction.path);
10787 goto error;
10788 }
10789 }
10790
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010791 /* init the appctx structure */
10792 appctx->st2 = SETCERT_ST_INIT;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010793 appctx->ctx.ssl.next_ckchi = NULL;
10794 appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs;
10795 appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs;
10796
10797 /* we don't unlock there, it will be unlock after the IO handler, in the release handler */
10798 return 0;
10799
10800error:
10801
10802 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
10803 err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]);
10804
10805 return cli_dynerr(appctx, err);
10806}
10807
William Lallemand8f840d72019-10-23 10:53:05 +020010808/*
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010809 * Parsing function of `set ssl cert`, it updates or creates a temporary ckch.
William Lallemand8f840d72019-10-23 10:53:05 +020010810 */
William Lallemand150bfa82019-09-19 17:12:49 +020010811static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private)
10812{
William Lallemand0c3b7d92019-10-18 11:27:07 +020010813 struct ckch_store *new_ckchs = NULL;
William Lallemand8f840d72019-10-23 10:53:05 +020010814 struct ckch_store *old_ckchs = NULL;
William Lallemand150bfa82019-09-19 17:12:49 +020010815 char *err = NULL;
William Lallemand963b2e72019-10-14 11:38:36 +020010816 int i;
William Lallemand849eed62019-10-17 16:23:50 +020010817 int bundle = -1; /* TRUE if >= 0 (ckch index) */
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010818 int errcode = 0;
William Lallemand44b35322019-10-17 16:28:40 +020010819 char *end;
10820 int type = CERT_TYPE_PEM;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010821 struct cert_key_and_chain *ckch;
10822 struct buffer *buf;
William Lallemand8f840d72019-10-23 10:53:05 +020010823
William Lallemand230662a2019-12-03 13:32:54 +010010824 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
10825 return 1;
10826
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010827 if ((buf = alloc_trash_chunk()) == NULL)
10828 return cli_err(appctx, "Can't allocate memory\n");
William Lallemand150bfa82019-09-19 17:12:49 +020010829
10830 if (!*args[3] || !payload)
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050010831 return cli_err(appctx, "'set ssl cert expects a filename and a certificate as a payload\n");
William Lallemand150bfa82019-09-19 17:12:49 +020010832
10833 /* The operations on the CKCH architecture are locked so we can
10834 * manipulate ckch_store and ckch_inst */
10835 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
10836 return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n");
10837
William Lallemand8f840d72019-10-23 10:53:05 +020010838 if (!chunk_strcpy(buf, args[3])) {
10839 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
10840 errcode |= ERR_ALERT | ERR_FATAL;
10841 goto end;
10842 }
10843
William Lallemand44b35322019-10-17 16:28:40 +020010844 /* check which type of file we want to update */
William Lallemandf29cdef2019-10-23 15:00:52 +020010845 for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) {
William Lallemand8f840d72019-10-23 10:53:05 +020010846 end = strrchr(buf->area, '.');
William Lallemand44b35322019-10-17 16:28:40 +020010847 if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) {
10848 *end = '\0';
10849 type = cert_exts[i].type;
10850 break;
10851 }
10852 }
10853
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010854 appctx->ctx.ssl.old_ckchs = NULL;
10855 appctx->ctx.ssl.new_ckchs = NULL;
William Lallemand849eed62019-10-17 16:23:50 +020010856
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010857 /* if there is an ongoing transaction */
10858 if (ckchs_transaction.path) {
10859 /* 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 +020010860#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010861 if (ckchs_transaction.new_ckchs->multi) {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010862 char *end;
William Lallemand963b2e72019-10-14 11:38:36 +020010863 int j;
William Lallemand150bfa82019-09-19 17:12:49 +020010864
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010865 /* check if it was used in a bundle by removing the
William Lallemand963b2e72019-10-14 11:38:36 +020010866 * .dsa/.rsa/.ecdsa at the end of the filename */
William Lallemand8f840d72019-10-23 10:53:05 +020010867 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010868 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemand963b2e72019-10-14 11:38:36 +020010869 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
10870 bundle = j; /* keep the type of certificate so we insert it at the right place */
10871 *end = '\0'; /* it's a bundle let's end the string*/
10872 break;
10873 }
William Lallemand150bfa82019-09-19 17:12:49 +020010874 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010875 if (bundle < 0) {
10876 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);
10877 errcode |= ERR_ALERT | ERR_FATAL;
10878 goto end;
10879 }
10880 }
10881#endif
10882
10883 /* if there is an ongoing transaction, check if this is the same file */
10884 if (strcmp(ckchs_transaction.path, buf->area) != 0) {
10885 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area);
10886 errcode |= ERR_ALERT | ERR_FATAL;
10887 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020010888 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010889
10890 appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs;
10891
10892 } else {
10893 struct ckch_store *find_ckchs[2] = { NULL, NULL };
10894
10895 /* lookup for the certificate in the tree:
10896 * check if this is used as a bundle AND as a unique certificate */
10897 for (i = 0; i < 2; i++) {
10898
10899 if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) {
10900 /* only the bundle name is in the tree and you should
10901 * never update a bundle name, only a filename */
10902 if (bundle < 0 && find_ckchs[i]->multi) {
10903 /* we tried to look for a non-bundle and we found a bundle */
10904 memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n",
10905 err ? err : "", args[3], args[3]);
10906 errcode |= ERR_ALERT | ERR_FATAL;
10907 goto end;
10908 }
William Lallemand3246d942019-11-04 14:02:11 +010010909 /* If we want a bundle but this is not a bundle
10910 * example: When you try to update <file>.rsa, but
10911 * <file> is a regular file */
10912 if (bundle >= 0 && find_ckchs[i]->multi == 0) {
10913 find_ckchs[i] = NULL;
10914 break;
10915 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010916 }
10917#if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL
10918 {
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010919 char *end;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010920 int j;
10921
10922 /* check if it was used in a bundle by removing the
10923 * .dsa/.rsa/.ecdsa at the end of the filename */
10924 end = strrchr(buf->area, '.');
Emmanuel Hocdet40f2f1e2019-10-30 17:31:28 +010010925 for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010926 if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) {
10927 bundle = j; /* keep the type of certificate so we insert it at the right place */
10928 *end = '\0'; /* it's a bundle let's end the string*/
10929 break;
10930 }
10931 }
William Lallemand37031b82019-11-04 13:38:53 +010010932 if (bundle < 0) /* we didn't find a bundle extension */
10933 break;
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010934 }
William Lallemand963b2e72019-10-14 11:38:36 +020010935#else
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010936 /* bundles are not supported here, so we don't need to lookup again */
10937 break;
William Lallemand963b2e72019-10-14 11:38:36 +020010938#endif
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010939 }
10940
10941 if (find_ckchs[0] && find_ckchs[1]) {
10942 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",
10943 err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path);
10944 errcode |= ERR_ALERT | ERR_FATAL;
10945 goto end;
10946 }
10947
10948 appctx->ctx.ssl.old_ckchs = find_ckchs[0] ? find_ckchs[0] : find_ckchs[1];
William Lallemand150bfa82019-09-19 17:12:49 +020010949 }
10950
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010951 if (!appctx->ctx.ssl.old_ckchs) {
10952 memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n",
William Lallemand150bfa82019-09-19 17:12:49 +020010953 err ? err : "");
Emeric Brunf69ed1d2019-10-17 11:56:56 +020010954 errcode |= ERR_ALERT | ERR_FATAL;
William Lallemand8f840d72019-10-23 10:53:05 +020010955 goto end;
William Lallemand150bfa82019-09-19 17:12:49 +020010956 }
10957
William Lallemand8a7fdf02019-11-04 10:59:32 +010010958 if (!appctx->ctx.ssl.path) {
10959 /* this is a new transaction, set the path of the transaction */
10960 appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path);
10961 if (!appctx->ctx.ssl.path) {
10962 memprintf(&err, "%sCan't allocate memory\n", err ? err : "");
10963 errcode |= ERR_ALERT | ERR_FATAL;
10964 goto end;
10965 }
10966 }
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010967
10968 old_ckchs = appctx->ctx.ssl.old_ckchs;
10969
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010970 /* duplicate the ckch store */
10971 new_ckchs = ckchs_dup(old_ckchs);
10972 if (!new_ckchs) {
10973 memprintf(&err, "%sCannot allocate memory!\n",
10974 err ? err : "");
10975 errcode |= ERR_ALERT | ERR_FATAL;
10976 goto end;
10977 }
10978
10979 if (!new_ckchs->multi)
10980 ckch = new_ckchs->ckch;
10981 else
10982 ckch = &new_ckchs->ckch[bundle];
10983
10984 /* appply the change on the duplicate */
10985 if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) {
10986 memprintf(&err, "%sCan't load the payload\n", err ? err : "");
10987 errcode |= ERR_ALERT | ERR_FATAL;
10988 goto end;
10989 }
10990
10991 appctx->ctx.ssl.new_ckchs = new_ckchs;
10992
10993 /* we succeed, we can save the ckchs in the transaction */
10994
10995 /* if there wasn't a transaction, update the old ckchs */
William Dauchyc8bb1532019-11-24 15:04:20 +010010996 if (!ckchs_transaction.old_ckchs) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010010997 ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs;
10998 ckchs_transaction.path = appctx->ctx.ssl.path;
10999 err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path);
11000 } else {
11001 err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path);
11002
11003 }
11004
11005 /* free the previous ckchs if there was a transaction */
William Lallemandba1c33f2020-04-08 17:55:45 +020011006 ckch_store_free(ckchs_transaction.new_ckchs);
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011007
11008 ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs;
11009
11010
William Lallemand8f840d72019-10-23 10:53:05 +020011011 /* creates the SNI ctxs later in the IO handler */
William Lallemand150bfa82019-09-19 17:12:49 +020011012
William Lallemand8f840d72019-10-23 10:53:05 +020011013end:
11014 free_trash_chunk(buf);
William Lallemand150bfa82019-09-19 17:12:49 +020011015
Emeric Brunf69ed1d2019-10-17 11:56:56 +020011016 if (errcode & ERR_CODE) {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011017
William Lallemandba1c33f2020-04-08 17:55:45 +020011018 ckch_store_free(appctx->ctx.ssl.new_ckchs);
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011019 appctx->ctx.ssl.new_ckchs = NULL;
11020
11021 appctx->ctx.ssl.old_ckchs = NULL;
11022
11023 free(appctx->ctx.ssl.path);
11024 appctx->ctx.ssl.path = NULL;
11025
11026 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
William Lallemand44b35322019-10-17 16:28:40 +020011027 return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3]));
William Lallemand430413e2019-10-28 14:30:47 +010011028 } else {
William Lallemandbc6ca7c2019-10-29 23:48:19 +010011029
11030 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11031 return cli_dynmsg(appctx, LOG_NOTICE, err);
William Lallemand430413e2019-10-28 14:30:47 +010011032 }
William Lallemand8f840d72019-10-23 10:53:05 +020011033 /* TODO: handle the ERR_WARN which are not handled because of the io_handler */
William Lallemand150bfa82019-09-19 17:12:49 +020011034}
11035
William Lallemand0bc9c8a2019-11-19 15:51:51 +010011036/* parsing function of 'abort ssl cert' */
11037static int cli_parse_abort_cert(char **args, char *payload, struct appctx *appctx, void *private)
11038{
11039 char *err = NULL;
11040
William Lallemand230662a2019-12-03 13:32:54 +010011041 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11042 return 1;
11043
William Lallemand0bc9c8a2019-11-19 15:51:51 +010011044 if (!*args[3])
11045 return cli_err(appctx, "'abort ssl cert' expects a filename\n");
11046
11047 /* The operations on the CKCH architecture are locked so we can
11048 * manipulate ckch_store and ckch_inst */
11049 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11050 return cli_err(appctx, "Can't abort!\nOperations on certificates are currently locked!\n");
11051
11052 if (!ckchs_transaction.path) {
11053 memprintf(&err, "No ongoing transaction!\n");
11054 goto error;
11055 }
11056
11057 if (strcmp(ckchs_transaction.path, args[3]) != 0) {
11058 memprintf(&err, "The ongoing transaction is about '%s' but you are trying to abort a transaction for '%s'\n", ckchs_transaction.path, args[3]);
11059 goto error;
11060 }
11061
11062 /* Only free the ckchs there, because the SNI and instances were not generated yet */
William Lallemandba1c33f2020-04-08 17:55:45 +020011063 ckch_store_free(ckchs_transaction.new_ckchs);
William Lallemand0bc9c8a2019-11-19 15:51:51 +010011064 ckchs_transaction.new_ckchs = NULL;
William Lallemandba1c33f2020-04-08 17:55:45 +020011065 ckch_store_free(ckchs_transaction.old_ckchs);
William Lallemand0bc9c8a2019-11-19 15:51:51 +010011066 ckchs_transaction.old_ckchs = NULL;
11067 free(ckchs_transaction.path);
11068 ckchs_transaction.path = NULL;
11069
11070 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11071
11072 err = memprintf(&err, "Transaction aborted for certificate '%s'!\n", args[3]);
11073 return cli_dynmsg(appctx, LOG_NOTICE, err);
11074
11075error:
11076 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11077
11078 return cli_dynerr(appctx, err);
11079}
11080
William Lallemandea987ed2020-03-19 16:48:33 +010011081/* parsing function of 'new ssl cert' */
11082static int cli_parse_new_cert(char **args, char *payload, struct appctx *appctx, void *private)
11083{
11084 struct ckch_store *store;
11085 char *err = NULL;
11086 char *path;
11087
11088 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11089 return 1;
11090
11091 if (!*args[3])
11092 return cli_err(appctx, "'new ssl cert' expects a filename\n");
11093
11094 path = args[3];
11095
11096 /* The operations on the CKCH architecture are locked so we can
11097 * manipulate ckch_store and ckch_inst */
11098 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11099 return cli_err(appctx, "Can't create a certificate!\nOperations on certificates are currently locked!\n");
11100
11101 store = ckchs_lookup(path);
11102 if (store != NULL) {
11103 memprintf(&err, "Certificate '%s' already exists!\n", path);
11104 store = NULL; /* we don't want to free it */
11105 goto error;
11106 }
William Lallemand8a874e42020-04-09 10:32:53 +020011107 /* we won't support multi-certificate bundle here */
11108 store = ckch_store_new(path, 1);
William Lallemandea987ed2020-03-19 16:48:33 +010011109 if (!store) {
11110 memprintf(&err, "unable to allocate memory.\n");
11111 goto error;
11112 }
William Lallemandea987ed2020-03-19 16:48:33 +010011113
11114 /* insert into the ckchs tree */
William Lallemandea987ed2020-03-19 16:48:33 +010011115 ebst_insert(&ckchs_tree, &store->node);
11116 memprintf(&err, "New empty certificate store '%s'!\n", args[3]);
11117
11118 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11119 return cli_dynmsg(appctx, LOG_NOTICE, err);
11120error:
11121 free(store);
11122 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11123 return cli_dynerr(appctx, err);
11124}
11125
William Lallemand419e6342020-04-08 12:05:39 +020011126/* parsing function of 'del ssl cert' */
11127static int cli_parse_del_cert(char **args, char *payload, struct appctx *appctx, void *private)
11128{
11129 struct ckch_store *store;
11130 char *err = NULL;
11131 char *filename;
11132
11133 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
11134 return 1;
11135
11136 if (!*args[3])
11137 return cli_err(appctx, "'del ssl cert' expects a certificate name\n");
11138
11139 if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock))
11140 return cli_err(appctx, "Can't delete the certificate!\nOperations on certificates are currently locked!\n");
11141
11142 filename = args[3];
11143
11144 store = ckchs_lookup(filename);
11145 if (store == NULL) {
11146 memprintf(&err, "certificate '%s' doesn't exist!\n", filename);
11147 goto error;
11148 }
11149 if (!LIST_ISEMPTY(&store->ckch_inst)) {
11150 memprintf(&err, "certificate '%s' in use, can't be deleted!\n", filename);
11151 goto error;
11152 }
11153
11154 ebmb_delete(&store->node);
William Lallemandba1c33f2020-04-08 17:55:45 +020011155 ckch_store_free(store);
William Lallemand419e6342020-04-08 12:05:39 +020011156
11157 memprintf(&err, "Certificate '%s' deleted!\n", filename);
11158
11159 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11160 return cli_dynmsg(appctx, LOG_NOTICE, err);
11161
11162error:
11163 memprintf(&err, "Can't remove the certificate: %s\n", err ? err : "");
11164 HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock);
11165 return cli_dynerr(appctx, err);
11166}
11167
11168
11169
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +020011170static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand32af2032016-10-29 18:09:35 +020011171{
11172#if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP)
11173 char *err = NULL;
Willy Tarreau1c913e42018-08-22 05:26:57 +020011174 int i, j, ret;
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020011175
11176 if (!payload)
11177 payload = args[3];
William Lallemand32af2032016-10-29 18:09:35 +020011178
11179 /* Expect one parameter: the new response in base64 encoding */
Willy Tarreau9d008692019-08-09 11:21:01 +020011180 if (!*payload)
11181 return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n");
Aurélien Nephtali1e0867c2018-04-18 14:04:58 +020011182
11183 /* remove \r and \n from the payload */
11184 for (i = 0, j = 0; payload[i]; i++) {
11185 if (payload[i] == '\r' || payload[i] == '\n')
11186 continue;
11187 payload[j++] = payload[i];
11188 }
11189 payload[j] = 0;
William Lallemand32af2032016-10-29 18:09:35 +020011190
Willy Tarreau1c913e42018-08-22 05:26:57 +020011191 ret = base64dec(payload, j, trash.area, trash.size);
Willy Tarreau9d008692019-08-09 11:21:01 +020011192 if (ret < 0)
11193 return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020011194
Willy Tarreau1c913e42018-08-22 05:26:57 +020011195 trash.data = ret;
William Lallemand32af2032016-10-29 18:09:35 +020011196 if (ssl_sock_update_ocsp_response(&trash, &err)) {
Willy Tarreau9d008692019-08-09 11:21:01 +020011197 if (err)
11198 return cli_dynerr(appctx, memprintf(&err, "%s.\n", err));
11199 else
11200 return cli_err(appctx, "Failed to update OCSP response.\n");
William Lallemand32af2032016-10-29 18:09:35 +020011201 }
Willy Tarreau9d008692019-08-09 11:21:01 +020011202
11203 return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n");
William Lallemand32af2032016-10-29 18:09:35 +020011204#else
Willy Tarreau9d008692019-08-09 11:21:01 +020011205 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 +020011206#endif
11207
Nenad Merdanovicc31499d2019-03-23 11:00:32 +010011208}
11209
Elliot Otchet71f82972020-01-15 08:12:14 -050011210/* Argument validation functions */
11211
11212/* This function is used to validate the arguments passed to any "x_dn" ssl
11213 * keywords. These keywords support specifying a third parameter that must be
11214 * either empty or the value "rfc2253". Returns 0 on error, non-zero if OK.
11215 */
11216int val_dnfmt(struct arg *arg, char **err_msg)
11217{
11218 if (arg && arg[2].type == ARGT_STR && arg[2].data.str.data > 0 && (strcmp(arg[2].data.str.area, "rfc2253") != 0)) {
11219 memprintf(err_msg, "only rfc2253 or a blank value are currently supported as the format argument.");
11220 return 0;
11221 }
11222 return 1;
11223}
11224
William Lallemand32af2032016-10-29 18:09:35 +020011225/* register cli keywords */
11226static struct cli_kw_list cli_kws = {{ },{
11227#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
11228 { { "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 +020011229 { { "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 +020011230#endif
Emmanuel Hocdetfdec7892017-01-13 17:48:18 +010011231 { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL },
William Lallemandea987ed2020-03-19 16:48:33 +010011232 { { "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 +010011233 { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL },
11234 { { "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 +010011235 { { "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 +020011236 { { "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 +010011237 { { "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 +010011238 { { "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 +020011239 { { "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 +020011240 { { "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 +020011241 { { NULL }, NULL, NULL, NULL }
11242}};
11243
Willy Tarreau0108d902018-11-25 19:14:37 +010011244INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
William Lallemand32af2032016-10-29 18:09:35 +020011245
Willy Tarreau7875d092012-09-10 08:20:03 +020011246/* Note: must not be declared <const> as its list will be overwritten.
11247 * Please take care of keeping this list alphabetically sorted.
11248 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011249static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Emeric Brun645ae792014-04-30 14:21:06 +020011250 { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011251 { "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 +010011252#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Jérôme Magnine064a802018-12-03 22:21:04 +010011253 { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010011254#endif
Emeric Brun645ae792014-04-30 14:21:06 +020011255 { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Olivier Houchard6b77f492018-11-22 18:18:29 +010011256#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
11257 { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
11258#endif
Emeric Brun74f7ffa2018-02-19 16:14:12 +010011259 { "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 +020011260 { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV },
Emeric Brunb73a9b02014-04-30 18:49:19 +020011261 { "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 +020011262 { "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 +020011263#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brun645ae792014-04-30 14:21:06 +020011264 { "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 -040011265#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011266#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040011267 { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
11268 { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
Patrick Hemmere0275472018-04-28 19:15:51 -040011269 { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV },
11270#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011271 { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
11272 { "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 +010011273 { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011274 { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050011275 { "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 +020011276 { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11277 { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11278 { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11279 { "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 -050011280 { "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 +020011281 { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11282 { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011283 { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011284 { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
11285 { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Emeric Brun43e79582014-10-29 19:03:26 +010011286 { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Elliot Otchet71f82972020-01-15 08:12:14 -050011287 { "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 +020011288 { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11289 { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11290 { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11291 { "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 -050011292 { "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 +020011293 { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Emeric Brun55f4fa82014-04-30 17:11:25 +020011294 { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011295 { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011296 { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011297 { "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 +010011298 { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau80aca902013-01-07 15:42:20 +010011299 { "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 +020011300 { "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 +010011301 { "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 +020011302 { "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 +010011303#if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG)
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011304 { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreaua33c6542012-10-15 13:19:06 +020011305#endif
Dirkjan Bussink48f1c4e2014-02-13 12:29:42 +010011306#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011307 { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreauab861d32013-04-02 02:30:41 +020011308#endif
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011309 { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011310#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Emeric Brunb73a9b02014-04-30 18:49:19 +020011311 { "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 -040011312#endif
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +020011313 { "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 +020011314#if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011315 { "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 -040011316#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011317#if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L
Patrick Hemmer65674662019-06-04 08:13:03 -040011318 { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11319 { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
Patrick Hemmere0275472018-04-28 19:15:51 -040011320 { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11321#endif
Patrick Hemmer41966772018-04-28 19:15:48 -040011322#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011323 { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
Patrick Hemmer41966772018-04-28 19:15:48 -040011324#endif
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010011325 { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11326 { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI },
11327 { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI },
11328 { "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 +020011329 { NULL, NULL, 0, 0, 0 },
11330}};
11331
Willy Tarreau0108d902018-11-25 19:14:37 +010011332INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
11333
Willy Tarreau7875d092012-09-10 08:20:03 +020011334/* Note: must not be declared <const> as its list will be overwritten.
11335 * Please take care of keeping this list alphabetically sorted.
11336 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011337static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011338 { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END },
11339 { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG },
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011340 { /* END */ },
Willy Tarreau7875d092012-09-10 08:20:03 +020011341}};
11342
Willy Tarreau0108d902018-11-25 19:14:37 +010011343INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws);
11344
Willy Tarreau79eeafa2012-09-14 07:53:05 +020011345/* Note: must not be declared <const> as its list will be overwritten.
11346 * Please take care of keeping this list alphabetically sorted, doing so helps
11347 * all code contributors.
11348 * Optional keywords are also declared with a NULL ->parse() function so that
11349 * the config parser can report an appropriate error when a known keyword was
11350 * not enabled.
11351 */
William Lallemand557823f2020-04-01 17:42:47 +020011352
11353/* the <ssl_bind_kws> keywords are used for crt-list parsing, they *MUST* be safe
11354 * with their proxy argument NULL and must only fill the ssl_bind_conf */
William Lallemandd4632b22020-05-12 14:46:24 +020011355struct ssl_bind_kw ssl_bind_kws[] = {
Olivier Houchardc2aae742017-09-22 18:26:28 +020011356 { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011357 { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +010011358 { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */
11359 { "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 +010011360 { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011361#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011362 { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
11363#endif
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050011364 { "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 +010011365 { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011366 { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020011367 { "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 +010011368 { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */
Emmanuel Hocdetdf701a22017-05-18 12:46:50 +020011369 { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */
11370 { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */
Emmanuel Hocdet98263292016-12-29 18:26:15 +010011371 { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */
11372 { NULL, NULL, 0 },
11373};
11374
Willy Tarreau0108d902018-11-25 19:14:37 +010011375/* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */
11376
Willy Tarreau51fb7652012-09-18 18:24:39 +020011377static struct bind_kw_list bind_kws = { "SSL", { }, {
Olivier Houchardc2aae742017-09-22 18:26:28 +020011378 { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011379 { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */
Emmanuel Hocdet842e94e2019-12-16 16:39:17 +010011380 { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process ca-names and verify on client cert */
11381 { "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 +020011382 { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */
11383 { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */
11384 { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */
11385 { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011386#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011387 { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */
11388#endif
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050011389 { "crl-file", bind_parse_crl_file, 1 }, /* set certificate revocation list file use on client cert verify */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011390 { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */
Ilya Shipitsin77e3b4a2020-03-10 12:06:11 +050011391 { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth == 0 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011392 { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */
11393 { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */
11394 { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */
11395 { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */
11396 { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */
11397 { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */
11398 { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020011399 { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011400 { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */
Emmanuel Hocdet174dfe52017-07-28 15:01:05 +020011401 { "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 +020011402 { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */
11403 { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */
11404 { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */
11405 { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */
Emmanuel Hocdet42fb9802017-03-30 19:29:39 +020011406 { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011407 { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */
11408 { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011409 { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */
11410 { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */
Emmanuel Hocdet5db33cb2017-03-30 19:19:37 +020011411 { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */
11412 { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */
11413 { "verify", bind_parse_verify, 1 }, /* set SSL verify method */
11414 { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */
11415 { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */
Willy Tarreau79eeafa2012-09-14 07:53:05 +020011416 { NULL, NULL, 0 },
11417}};
Emeric Brun46591952012-05-18 15:47:34 +020011418
Willy Tarreau0108d902018-11-25 19:14:37 +010011419INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws);
11420
Willy Tarreau92faadf2012-10-10 23:04:25 +020011421/* Note: must not be declared <const> as its list will be overwritten.
11422 * Please take care of keeping this list alphabetically sorted, doing so helps
11423 * all code contributors.
11424 * Optional keywords are also declared with a NULL ->parse() function so that
11425 * the config parser can report an appropriate error when a known keyword was
11426 * not enabled.
11427 */
11428static struct srv_kw_list srv_kws = { "SSL", { }, {
Olivier Houchard522eea72017-11-03 16:27:47 +010011429 { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */
Olivier Houchardc7566002018-11-20 23:33:50 +010011430 { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011431 { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */
Olivier Houchard92150142018-12-21 19:47:01 +010011432 { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */
Olivier Houchard9130a962017-10-17 17:33:43 +020011433 { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011434 { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */
11435 { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011436#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011437 { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */
11438#endif
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011439 { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */
11440 { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */
11441 { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */
11442 { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */
11443 { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */
11444 { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */
11445 { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */
11446 { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */
11447 { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */
11448 { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */
11449 { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */
11450 { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */
11451 { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */
11452 { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */
11453 { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */
11454 { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */
11455 { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */
11456 { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */
Olivier Houchardc7566002018-11-20 23:33:50 +010011457 { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */
Emmanuel Hocdete1c722b2017-03-31 15:02:54 +020011458 { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */
11459 { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */
11460 { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */
11461 { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */
11462 { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */
11463 { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */
11464 { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */
11465 { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */
11466 { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */
11467 { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */
Willy Tarreau92faadf2012-10-10 23:04:25 +020011468 { NULL, NULL, 0, 0 },
11469}};
11470
Willy Tarreau0108d902018-11-25 19:14:37 +010011471INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
11472
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011473static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau8c3b0fd2016-12-21 22:44:46 +010011474 { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base },
11475 { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base },
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010011476 { CFG_GLOBAL, "issuers-chain-path", ssl_load_global_issuers_from_path },
Willy Tarreau0bea58d2016-12-21 23:17:25 +010011477 { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011478 { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options },
11479 { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options },
Willy Tarreau14e36a12016-12-21 23:28:13 +010011480#ifndef OPENSSL_NO_DH
11481 { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file },
11482#endif
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000011483 { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011484#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011485 { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine },
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011486#endif
Emmanuel Hocdetc3b7e742020-04-22 11:06:19 +020011487 { CFG_GLOBAL, "ssl-skip-self-issued-ca", ssl_parse_skip_self_issued_ca },
Willy Tarreau9ceda382016-12-21 23:13:03 +010011488 { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int },
11489#ifndef OPENSSL_NO_DH
11490 { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh },
11491#endif
11492 { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache },
11493 { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime },
11494 { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int },
11495 { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int },
Thierry FOURNIER5bf77322017-02-25 12:45:22 +010011496 { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist },
Willy Tarreauf22e9682016-12-21 23:23:19 +010011497 { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers },
11498 { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers },
Jerome Magninb203ff62020-04-03 15:28:22 +020011499#if ((HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) || defined(LIBRESSL_VERSION_NUMBER))
11500 { CFG_GLOBAL, "ssl-default-bind-curves", ssl_parse_global_curves },
11501#endif
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011502#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011503 { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites },
11504 { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites },
11505#endif
William Lallemand3af48e72020-02-03 17:15:52 +010011506 { CFG_GLOBAL, "ssl-load-extra-files", ssl_parse_global_extra_files },
Emeric Brun2c86cbf2014-10-30 15:56:50 +010011507 { 0, NULL, NULL },
11508}};
11509
Willy Tarreau0108d902018-11-25 19:14:37 +010011510INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
11511
Willy Tarreauf7bc57c2012-10-03 00:19:48 +020011512/* transport-layer operations for SSL sockets */
Willy Tarreaud9f5cca2016-12-22 21:08:52 +010011513static struct xprt_ops ssl_sock = {
Emeric Brun46591952012-05-18 15:47:34 +020011514 .snd_buf = ssl_sock_from_buf,
11515 .rcv_buf = ssl_sock_to_buf,
Olivier Houcharddf357842019-03-21 16:30:07 +010011516 .subscribe = ssl_subscribe,
11517 .unsubscribe = ssl_unsubscribe,
Olivier Houchard5149b592019-05-23 17:47:36 +020011518 .remove_xprt = ssl_remove_xprt,
Olivier Houchard2e055482019-05-27 19:50:12 +020011519 .add_xprt = ssl_add_xprt,
Emeric Brun46591952012-05-18 15:47:34 +020011520 .rcv_pipe = NULL,
11521 .snd_pipe = NULL,
11522 .shutr = NULL,
11523 .shutw = ssl_sock_shutw,
11524 .close = ssl_sock_close,
11525 .init = ssl_sock_init,
Willy Tarreau55d37912016-12-21 23:38:39 +010011526 .prepare_bind_conf = ssl_sock_prepare_bind_conf,
Willy Tarreau795cdab2016-12-22 17:30:54 +010011527 .destroy_bind_conf = ssl_sock_destroy_bind_conf,
Willy Tarreau17d45382016-12-22 21:16:08 +010011528 .prepare_srv = ssl_sock_prepare_srv_ctx,
11529 .destroy_srv = ssl_sock_free_srv_ctx,
Willy Tarreau8743f7e2016-12-04 18:44:29 +010011530 .get_alpn = ssl_sock_get_alpn,
Willy Tarreau8e0bb0a2016-11-24 16:58:12 +010011531 .name = "SSL",
Emeric Brun46591952012-05-18 15:47:34 +020011532};
11533
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011534enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px,
11535 struct session *sess, struct stream *s, int flags)
11536{
11537 struct connection *conn;
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011538 struct conn_stream *cs;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011539
11540 conn = objt_conn(sess->origin);
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011541 cs = objt_cs(s->si[0].end);
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011542
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011543 if (conn && cs) {
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011544 if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) {
Olivier Houchard6fa63d92017-11-27 18:41:32 +010011545 cs->flags |= CS_FL_WAIT_FOR_HS;
Olivier Houchardccaa7de2017-10-02 11:51:03 +020011546 s->req.flags |= CF_READ_NULL;
11547 return ACT_RET_YIELD;
11548 }
11549 }
11550 return (ACT_RET_CONT);
11551}
11552
11553static enum act_parse_ret ssl_parse_wait_for_hs(const char **args, int *orig_arg, struct proxy *px, struct act_rule *rule, char **err)
11554{
11555 rule->action_ptr = ssl_action_wait_for_hs;
11556
11557 return ACT_RET_PRS_OK;
11558}
11559
11560static struct action_kw_list http_req_actions = {ILH, {
11561 { "wait-for-handshake", ssl_parse_wait_for_hs },
11562 { /* END */ }
11563}};
11564
Willy Tarreau0108d902018-11-25 19:14:37 +010011565INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions);
11566
Willy Tarreau5db847a2019-05-09 14:13:35 +020011567#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011568
11569static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
11570{
11571 if (ptr) {
11572 chunk_destroy(ptr);
11573 free(ptr);
11574 }
11575}
11576
11577#endif
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010011578static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
11579{
Willy Tarreaubafbe012017-11-24 17:34:44 +010011580 pool_free(pool_head_ssl_capture, ptr);
Emmanuel Hocdetaaee7502017-03-07 18:34:58 +010011581}
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011582
Emeric Brun46591952012-05-18 15:47:34 +020011583__attribute__((constructor))
Willy Tarreau92faadf2012-10-10 23:04:25 +020011584static void __ssl_sock_init(void)
11585{
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011586#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020011587 STACK_OF(SSL_COMP)* cm;
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011588 int n;
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011589#endif
Emeric Brun46591952012-05-18 15:47:34 +020011590
Willy Tarreauef934602016-12-22 23:12:01 +010011591 if (global_ssl.listen_default_ciphers)
11592 global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers);
11593 if (global_ssl.connect_default_ciphers)
11594 global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers);
Emmanuel Hocdet839af572019-05-14 16:27:35 +020011595#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L)
Dirkjan Bussink415150f2018-09-14 11:14:21 +020011596 if (global_ssl.listen_default_ciphersuites)
11597 global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites);
11598 if (global_ssl.connect_default_ciphersuites)
11599 global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites);
11600#endif
Willy Tarreau610f04b2014-02-13 11:36:41 +010011601
Willy Tarreau13e14102016-12-22 20:25:26 +010011602 xprt_register(XPRT_SSL, &ssl_sock);
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011603#if HA_OPENSSL_VERSION_NUMBER < 0x10100000L
Emeric Brun46591952012-05-18 15:47:34 +020011604 SSL_library_init();
Rosen Penev68185952018-12-14 08:47:02 -080011605#endif
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011606#if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION))
Emeric Brun46591952012-05-18 15:47:34 +020011607 cm = SSL_COMP_get_compression_methods();
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011608 n = sk_SSL_COMP_num(cm);
11609 while (n--) {
11610 (void) sk_SSL_COMP_pop(cm);
11611 }
Ilya Shipitsin0590f442019-05-25 19:30:50 +050011612#endif
Ilya Shipitsine242f3d2019-05-25 03:38:14 +050011613
Willy Tarreau5db847a2019-05-09 14:13:35 +020011614#if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Emeric Brun821bb9b2017-06-15 16:37:39 +020011615 ssl_locking_init();
11616#endif
Willy Tarreau5db847a2019-05-09 14:13:35 +020011617#if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL)
Janusz Dziemidowicz2c701b52015-03-07 23:03:59 +010011618 sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func);
11619#endif
Thierry FOURNIER28962c92018-06-17 21:37:05 +020011620 ssl_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
Thierry FOURNIER16ff0502018-06-17 21:33:01 +020011621 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 +020011622#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011623 ENGINE_load_builtin_engines();
Grant Zhangfa6c7ee2017-01-14 01:42:15 +000011624 hap_register_post_check(ssl_check_async_engine_count);
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011625#endif
Willy Tarreaud1c57502016-12-22 22:46:15 +010011626#if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0)
11627 hap_register_post_check(tlskeys_finalize_config);
11628#endif
Willy Tarreau80713382018-11-26 10:19:54 +010011629
11630 global.ssl_session_max_cost = SSL_SESSION_MAX_COST;
11631 global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST;
11632
Emmanuel Hocdet70df7bf2019-01-04 11:08:20 +010011633 hap_register_post_deinit(ssl_free_global_issuers);
11634
Willy Tarreau80713382018-11-26 10:19:54 +010011635#ifndef OPENSSL_NO_DH
11636 ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
11637 hap_register_post_deinit(ssl_free_dh);
11638#endif
11639#ifndef OPENSSL_NO_ENGINE
11640 hap_register_post_deinit(ssl_free_engines);
11641#endif
11642 /* Load SSL string for the verbose & debug mode. */
11643 ERR_load_SSL_strings();
Olivier Houcharda8955d52019-04-07 22:00:38 +020011644 ha_meth = BIO_meth_new(0x666, "ha methods");
11645 BIO_meth_set_write(ha_meth, ha_ssl_write);
11646 BIO_meth_set_read(ha_meth, ha_ssl_read);
11647 BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl);
11648 BIO_meth_set_create(ha_meth, ha_ssl_new);
11649 BIO_meth_set_destroy(ha_meth, ha_ssl_free);
11650 BIO_meth_set_puts(ha_meth, ha_ssl_puts);
11651 BIO_meth_set_gets(ha_meth, ha_ssl_gets);
William Lallemand150bfa82019-09-19 17:12:49 +020011652
11653 HA_SPIN_INIT(&ckch_lock);
Dragan Dosen1e7ed042020-05-08 18:30:00 +020011654
Dragan Dosen9ac98092020-05-11 15:51:45 +020011655 /* Try to register dedicated SSL/TLS protocol message callbacks for
11656 * heartbleed attack (CVE-2014-0160) and clienthello.
11657 */
11658 hap_register_post_check(ssl_sock_register_msg_callbacks);
11659
Dragan Dosen1e7ed042020-05-08 18:30:00 +020011660 /* Try to free all callbacks that were registered by using
11661 * ssl_sock_register_msg_callback().
11662 */
11663 hap_register_post_deinit(ssl_sock_unregister_msg_callbacks);
Willy Tarreau80713382018-11-26 10:19:54 +010011664}
Willy Tarreaud92aa5c2015-01-15 21:34:39 +010011665
Willy Tarreau80713382018-11-26 10:19:54 +010011666/* Compute and register the version string */
11667static void ssl_register_build_options()
11668{
11669 char *ptr = NULL;
11670 int i;
11671
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011672 memprintf(&ptr, "Built with OpenSSL version : "
11673#ifdef OPENSSL_IS_BORINGSSL
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010011674 "BoringSSL");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011675#else /* OPENSSL_IS_BORINGSSL */
11676 OPENSSL_VERSION_TEXT
11677 "\nRunning on OpenSSL version : %s%s",
Rosen Penev68185952018-12-14 08:47:02 -080011678 OpenSSL_version(OPENSSL_VERSION),
Willy Tarreau1d158ab2019-05-09 13:41:45 +020011679 ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : "");
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011680#endif
11681 memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : "
Willy Tarreau9a1ab082019-05-09 13:26:41 +020011682#if HA_OPENSSL_VERSION_NUMBER < 0x00907000L
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011683 "no (library version too old)"
11684#elif defined(OPENSSL_NO_TLSEXT)
11685 "no (disabled via OPENSSL_NO_TLSEXT)"
11686#else
11687 "yes"
11688#endif
11689 "", ptr);
11690
11691 memprintf(&ptr, "%s\nOpenSSL library supports SNI : "
11692#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
11693 "yes"
11694#else
11695#ifdef OPENSSL_NO_TLSEXT
11696 "no (because of OPENSSL_NO_TLSEXT)"
11697#else
11698 "no (version might be too old, 0.9.8f min needed)"
11699#endif
11700#endif
11701 "", ptr);
11702
Emmanuel Hocdetf80bc242017-07-12 14:25:38 +020011703 memprintf(&ptr, "%s\nOpenSSL library supports :", ptr);
11704 for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++)
11705 if (methodVersions[i].option)
11706 memprintf(&ptr, "%s %s", ptr, methodVersions[i].name);
Emmanuel Hocdet50e25e12017-03-24 15:20:03 +010011707
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011708 hap_register_build_opts(ptr, 1);
Willy Tarreau80713382018-11-26 10:19:54 +010011709}
Willy Tarreauc2c0b612016-12-21 19:23:20 +010011710
Willy Tarreau80713382018-11-26 10:19:54 +010011711INITCALL0(STG_REGISTER, ssl_register_build_options);
Remi Gacogne4f902b82015-05-28 16:23:00 +020011712
Emeric Brun46591952012-05-18 15:47:34 +020011713
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011714#ifndef OPENSSL_NO_ENGINE
Grant Zhang872f9c22017-01-21 01:10:18 +000011715void ssl_free_engines(void) {
11716 struct ssl_engine_list *wl, *wlb;
11717 /* free up engine list */
11718 list_for_each_entry_safe(wl, wlb, &openssl_engines, list) {
11719 ENGINE_finish(wl->e);
11720 ENGINE_free(wl->e);
11721 LIST_DEL(&wl->list);
11722 free(wl);
11723 }
11724}
Emmanuel Hocdet9ac143b2017-05-29 14:36:20 +020011725#endif
Christopher Faulet31af49d2015-06-09 17:29:50 +020011726
Remi Gacogned3a23c32015-05-28 16:39:47 +020011727#ifndef OPENSSL_NO_DH
Grant Zhang872f9c22017-01-21 01:10:18 +000011728void ssl_free_dh(void) {
11729 if (local_dh_1024) {
11730 DH_free(local_dh_1024);
11731 local_dh_1024 = NULL;
11732 }
11733 if (local_dh_2048) {
11734 DH_free(local_dh_2048);
11735 local_dh_2048 = NULL;
11736 }
11737 if (local_dh_4096) {
11738 DH_free(local_dh_4096);
11739 local_dh_4096 = NULL;
11740 }
Remi Gacogne47783ef2015-05-29 15:53:22 +020011741 if (global_dh) {
11742 DH_free(global_dh);
11743 global_dh = NULL;
11744 }
Grant Zhang872f9c22017-01-21 01:10:18 +000011745}
11746#endif
11747
11748__attribute__((destructor))
11749static void __ssl_sock_deinit(void)
11750{
11751#if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES)
Emeric Brun821bb9b2017-06-15 16:37:39 +020011752 if (ssl_ctx_lru_tree) {
11753 lru64_destroy(ssl_ctx_lru_tree);
Christopher Faulet2a944ee2017-11-07 10:42:54 +010011754 HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock);
Emeric Brun821bb9b2017-06-15 16:37:39 +020011755 }
Remi Gacogned3a23c32015-05-28 16:39:47 +020011756#endif
11757
Willy Tarreau5db847a2019-05-09 14:13:35 +020011758#if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020011759 ERR_remove_state(0);
11760 ERR_free_strings();
11761
11762 EVP_cleanup();
Rosen Penev68185952018-12-14 08:47:02 -080011763#endif
Remi Gacogned3a23c32015-05-28 16:39:47 +020011764
Willy Tarreau5db847a2019-05-09 14:13:35 +020011765#if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L)
Remi Gacogned3a23c32015-05-28 16:39:47 +020011766 CRYPTO_cleanup_all_ex_data();
11767#endif
Olivier Houcharda8955d52019-04-07 22:00:38 +020011768 BIO_meth_free(ha_meth);
Remi Gacogned3a23c32015-05-28 16:39:47 +020011769}
11770
11771
Emeric Brun46591952012-05-18 15:47:34 +020011772/*
11773 * Local variables:
11774 * c-indent-level: 8
11775 * c-basic-offset: 8
11776 * End:
11777 */